org.apache.calcite.schema.ScannableTable Java Examples

The following examples show how to use org.apache.calcite.schema.ScannableTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnumerableTableScan.java    From Bats with Apache License 2.0 6 votes vote down vote up
public static Class deduceElementType(Table table) {
  if (table instanceof QueryableTable) {
    final QueryableTable queryableTable = (QueryableTable) table;
    final Type type = queryableTable.getElementType();
    if (type instanceof Class) {
      return (Class) type;
    } else {
      return Object[].class;
    }
  } else if (table instanceof ScannableTable
      || table instanceof FilterableTable
      || table instanceof StreamableTable) {
    return Object[].class;
  } else {
    return Object.class;
  }
}
 
Example #2
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a {@link TableFunctionImpl} from a method. */
public static TableFunction create(final Method method) {
  if (!Modifier.isStatic(method.getModifiers())) {
    Class clazz = method.getDeclaringClass();
    if (!classHasPublicZeroArgsConstructor(clazz)) {
      throw RESOURCE.requireDefaultConstructor(clazz.getName()).ex();
    }
  }
  final Class<?> returnType = method.getReturnType();
  if (!QueryableTable.class.isAssignableFrom(returnType)
      && !ScannableTable.class.isAssignableFrom(returnType)) {
    return null;
  }
  CallImplementor implementor = createImplementor(method);
  return new TableFunctionImpl(method, implementor);
}
 
Example #3
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Expression toRows(PhysType physType, Expression expression) {
  if (physType.getFormat() == JavaRowFormat.SCALAR
      && Object[].class.isAssignableFrom(elementType)
      && getRowType().getFieldCount() == 1
      && (table.unwrap(ScannableTable.class) != null
          || table.unwrap(FilterableTable.class) != null
          || table.unwrap(ProjectableFilterableTable.class) != null)) {
    return Expressions.call(BuiltInMethod.SLICE0.method, expression);
  }
  JavaRowFormat oldFormat = format();
  if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) {
    return expression;
  }
  final ParameterExpression row_ =
      Expressions.parameter(elementType, "row");
  final int fieldCount = table.getRowType().getFieldCount();
  List<Expression> expressionList = new ArrayList<>(fieldCount);
  for (int i = 0; i < fieldCount; i++) {
    expressionList.add(fieldExpression(row_, i, physType, oldFormat));
  }
  return Expressions.call(expression,
      BuiltInMethod.SELECT.method,
      Expressions.lambda(Function1.class, physType.record(expressionList),
          row_));
}
 
Example #4
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static Class deduceElementType(Table table) {
  if (table instanceof QueryableTable) {
    final QueryableTable queryableTable = (QueryableTable) table;
    final Type type = queryableTable.getElementType();
    if (type instanceof Class) {
      return (Class) type;
    } else {
      return Object[].class;
    }
  } else if (table instanceof ScannableTable
      || table instanceof FilterableTable
      || table instanceof ProjectableFilterableTable
      || table instanceof StreamableTable) {
    return Object[].class;
  } else {
    return Object.class;
  }
}
 
Example #5
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Function<Class, Expression> getClassExpressionFunction(
    final SchemaPlus schema, final String tableName, final Table table) {
  if (table instanceof QueryableTable) {
    final QueryableTable queryableTable = (QueryableTable) table;
    return clazz -> queryableTable.getExpression(schema, tableName, clazz);
  } else if (table instanceof ScannableTable
      || table instanceof FilterableTable
      || table instanceof ProjectableFilterableTable) {
    return clazz -> Schemas.tableExpression(schema, Object[].class, tableName,
        table.getClass());
  } else if (table instanceof StreamableTable) {
    return getClassExpressionFunction(schema, tableName,
        ((StreamableTable) table).stream());
  } else {
    return input -> {
      throw new UnsupportedOperationException();
    };
  }
}
 
Example #6
Source File: EnumerableTableScan.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns whether EnumerableTableScan can generate code to handle a
 * particular variant of the Table SPI. */
public static boolean canHandle(Table table) {
  // FilterableTable and ProjectableFilterableTable cannot be handled in
  // enumerable convention because they might reject filters and those filters
  // would need to be handled dynamically.
  return table instanceof QueryableTable
      || table instanceof ScannableTable;
}
 
Example #7
Source File: Smalls.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static ScannableTable generate2(
    @Parameter(name = "WIDTH") int width,
    @Parameter(name = "HEIGHT") int height,
    @Parameter(name = "SEED", optional = true) Integer seed) {
  return new MazeTable(
      String.format(Locale.ROOT, "generate2(w=%d, h=%d, s=%d)", width,
          height, seed));
}
 
Example #8
Source File: TableScanNode.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static TableScanNode createScannable(Compiler compiler, TableScan rel,
    ImmutableList<RexNode> filters, ImmutableIntList projects,
    ScannableTable scannableTable) {
  final Enumerable<Row> rowEnumerable =
      Enumerables.toRow(scannableTable.scan(compiler.getDataContext()));
  return createEnumerable(compiler, rel, rowEnumerable, null, filters,
      projects);
}
 
Example #9
Source File: TableScanNode.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a TableScanNode.
 *
 * <p>Tries various table SPIs, and negotiates with the table which filters
 * and projects it can implement. Adds to the Enumerable implementations of
 * any filters and projects that cannot be implemented by the table. */
static TableScanNode create(Compiler compiler, TableScan rel,
    ImmutableList<RexNode> filters, ImmutableIntList projects) {
  final RelOptTable relOptTable = rel.getTable();
  final ProjectableFilterableTable pfTable =
      relOptTable.unwrap(ProjectableFilterableTable.class);
  if (pfTable != null) {
    return createProjectableFilterable(compiler, rel, filters, projects,
        pfTable);
  }
  final FilterableTable filterableTable =
      relOptTable.unwrap(FilterableTable.class);
  if (filterableTable != null) {
    return createFilterable(compiler, rel, filters, projects,
        filterableTable);
  }
  final ScannableTable scannableTable =
      relOptTable.unwrap(ScannableTable.class);
  if (scannableTable != null) {
    return createScannable(compiler, rel, filters, projects,
        scannableTable);
  }
  //noinspection unchecked
  final Enumerable<Row> enumerable = relOptTable.unwrap(Enumerable.class);
  if (enumerable != null) {
    return createEnumerable(compiler, rel, enumerable, null, filters,
        projects);
  }
  final QueryableTable queryableTable =
      relOptTable.unwrap(QueryableTable.class);
  if (queryableTable != null) {
    return createQueryable(compiler, rel, filters, projects,
        queryableTable);
  }
  throw new AssertionError("cannot convert table " + relOptTable
      + " to enumerable");
}
 
Example #10
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Type getElementType(List<Object> arguments) {
  final Table table = apply(arguments);
  if (table instanceof QueryableTable) {
    QueryableTable queryableTable = (QueryableTable) table;
    return queryableTable.getElementType();
  } else if (table instanceof ScannableTable) {
    return Object[].class;
  }
  throw new AssertionError("Invalid table class: " + table + " "
      + table.getClass());
}
 
Example #11
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns whether EnumerableTableScan can generate code to handle a
 * particular variant of the Table SPI.
 * @deprecated
 **/
@Deprecated
public static boolean canHandle(Table table) {
  if (table instanceof TransientTable) {
    // CALCITE-3673: TransientTable can't be implemented with Enumerable
    return false;
  }
  // See org.apache.calcite.prepare.RelOptTableImpl.getClassExpressionFunction
  return table instanceof QueryableTable
      || table instanceof FilterableTable
      || table instanceof ProjectableFilterableTable
      || table instanceof ScannableTable;
}
 
Example #12
Source File: SqlLatticeStatisticProvider.java    From calcite with Apache License 2.0 5 votes vote down vote up
private double cardinality(Lattice lattice, Lattice.Column column) {
  final String sql = lattice.countSql(ImmutableBitSet.of(column.ordinal));
  final Table table =
      new MaterializationService.DefaultTableFactory()
          .createTable(lattice.rootSchema, sql, ImmutableList.of());
  final Object[] values =
      Iterables.getOnlyElement(((ScannableTable) table).scan(null));
  return ((Number) values[0]).doubleValue();
}
 
Example #13
Source File: ProfilerLatticeStatisticProvider.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a ProfilerLatticeStatisticProvider. */
private ProfilerLatticeStatisticProvider(Lattice lattice) {
  Objects.requireNonNull(lattice);
  this.profile = Suppliers.memoize(() -> {
    final ProfilerImpl profiler =
        ProfilerImpl.builder()
            .withPassSize(200)
            .withMinimumSurprise(0.3D)
            .build();
    final List<Profiler.Column> columns = new ArrayList<>();
    for (Lattice.Column column : lattice.columns) {
      columns.add(new Profiler.Column(column.ordinal, column.alias));
    }
    final String sql =
        lattice.sql(ImmutableBitSet.range(lattice.columns.size()),
            false, ImmutableList.of());
    final Table table =
        new MaterializationService.DefaultTableFactory()
            .createTable(lattice.rootSchema, sql, ImmutableList.of());
    final ImmutableList<ImmutableBitSet> initialGroups =
        ImmutableList.of();
    final Enumerable<List<Comparable>> rows =
        ((ScannableTable) table).scan(null)
            .select(values -> {
              for (int i = 0; i < values.length; i++) {
                if (values[i] == null) {
                  values[i] = NullSentinel.INSTANCE;
                }
              }
              //noinspection unchecked
              return (List<Comparable>) (List) Arrays.asList(values);
            });
    return profiler.profile(rows, columns, initialGroups);
  })::get;
}
 
Example #14
Source File: SqlLatticeStatisticProvider.java    From Bats with Apache License 2.0 5 votes vote down vote up
private double cardinality(Lattice lattice, Lattice.Column column) {
  final String sql = lattice.countSql(ImmutableBitSet.of(column.ordinal));
  final Table table =
      new MaterializationService.DefaultTableFactory()
          .createTable(lattice.rootSchema, sql, ImmutableList.of());
  final Object[] values =
      Iterables.getOnlyElement(((ScannableTable) table).scan(null));
  return ((Number) values[0]).doubleValue();
}
 
Example #15
Source File: DruidQuery.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public Enumerable<Object[]> bind(DataContext dataContext) {
  return table.unwrap(ScannableTable.class).scan(dataContext);
}
 
Example #16
Source File: Bindables.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static boolean canHandle(RelOptTable table) {
  return table.unwrap(ScannableTable.class) != null
      || table.unwrap(FilterableTable.class) != null
      || table.unwrap(ProjectableFilterableTable.class) != null;
}
 
Example #17
Source File: PushDownLogicTableRule.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public static boolean canHandle(RelOptTable table) {
    return table.unwrap(ScannableTable.class) != null
            || table.unwrap(FilterableTable.class) != null
            || table.unwrap(ProjectableFilterableTable.class) != null;
}
 
Example #18
Source File: Smalls.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** A function that generates the Fibonacci sequence.
 * Interesting because it has one column and no arguments. */
public static ScannableTable fibonacciTable() {
  return fibonacciTableWithLimit(-1L);
}
 
Example #19
Source File: Smalls.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static ScannableTable generate(int width, int height, int seed) {
  return new MazeTable(
      String.format(Locale.ROOT, "generate(w=%d, h=%d, s=%d)", width,
          height, seed));
}
 
Example #20
Source File: Smalls.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static ScannableTable generate3(
    @Parameter(name = "FOO") String foo) {
  return new MazeTable(
      String.format(Locale.ROOT, "generate3(foo=%s)", foo));
}
 
Example #21
Source File: MazeTable.java    From calcite with Apache License 2.0 2 votes vote down vote up
/** Table function that generates a maze with a solution.
 *
 * <p>Called by reflection based on the definition of the user-defined
 * function in the schema.
 *
 * @param width Width of maze
 * @param height Height of maze
 * @param seed Random number seed, or -1 to create an unseeded random
 * @return Table that prints the maze in text form, with solution shown
 */
@SuppressWarnings("unused") // called via reflection
public static ScannableTable solve(int width, int height, int seed) {
  return new MazeTable(width, height, seed, true);
}
 
Example #22
Source File: MazeTable.java    From calcite with Apache License 2.0 2 votes vote down vote up
/** Table function that generates a maze.
 *
 * <p>Called by reflection based on the definition of the user-defined
 * function in the schema.
 *
 * @param width Width of maze
 * @param height Height of maze
 * @param seed Random number seed, or -1 to create an unseeded random
 * @return Table that prints the maze in text form
 */
@SuppressWarnings("unused") // called via reflection
public static ScannableTable generate(int width, int height, int seed) {
  return new MazeTable(width, height, seed, false);
}