use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project calcite by apache.
the class EnumerableTableScanRule method convert.
@Override
public RelNode convert(RelNode rel) {
LogicalTableScan scan = (LogicalTableScan) rel;
final RelOptTable relOptTable = scan.getTable();
final Table table = relOptTable.unwrap(Table.class);
if (!EnumerableTableScan.canHandle(table)) {
return null;
}
final Expression expression = relOptTable.getExpression(Object.class);
if (expression == null) {
return null;
}
return EnumerableTableScan.create(scan.getCluster(), relOptTable);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project calcite by apache.
the class TableScanNode method createQueryable.
private static TableScanNode createQueryable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects, QueryableTable queryableTable) {
final DataContext root = compiler.getDataContext();
final RelOptTable relOptTable = rel.getTable();
final Type elementType = queryableTable.getElementType();
SchemaPlus schema = root.getRootSchema();
for (String name : Util.skipLast(relOptTable.getQualifiedName())) {
schema = schema.getSubSchema(name);
}
final Enumerable<Row> rowEnumerable;
if (elementType instanceof Class) {
// noinspection unchecked
final Queryable<Object> queryable = Schemas.queryable(root, (Class) elementType, relOptTable.getQualifiedName());
ImmutableList.Builder<Field> fieldBuilder = ImmutableList.builder();
Class type = (Class) elementType;
for (Field field : type.getFields()) {
if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
fieldBuilder.add(field);
}
}
final List<Field> fields = fieldBuilder.build();
rowEnumerable = queryable.select(new Function1<Object, Row>() {
public Row apply(Object o) {
final Object[] values = new Object[fields.size()];
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
try {
values[i] = field.get(o);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return new Row(values);
}
});
} else {
rowEnumerable = Schemas.queryable(root, Row.class, relOptTable.getQualifiedName());
}
return createEnumerable(compiler, rel, rowEnumerable, null, filters, projects);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project calcite by apache.
the class CalcitePrepareImpl method analyze_.
private AnalyzeViewResult analyze_(SqlValidator validator, String sql, SqlNode sqlNode, RelRoot root, boolean fail) {
final RexBuilder rexBuilder = root.rel.getCluster().getRexBuilder();
RelNode rel = root.rel;
final RelNode viewRel = rel;
Project project;
if (rel instanceof Project) {
project = (Project) rel;
rel = project.getInput();
} else {
project = null;
}
Filter filter;
if (rel instanceof Filter) {
filter = (Filter) rel;
rel = filter.getInput();
} else {
filter = null;
}
TableScan scan;
if (rel instanceof TableScan) {
scan = (TableScan) rel;
} else {
scan = null;
}
if (scan == null) {
if (fail) {
throw validator.newValidationError(sqlNode, RESOURCE.modifiableViewMustBeBasedOnSingleTable());
}
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, null, null, null, null, false);
}
final RelOptTable targetRelTable = scan.getTable();
final RelDataType targetRowType = targetRelTable.getRowType();
final Table table = targetRelTable.unwrap(Table.class);
final List<String> tablePath = targetRelTable.getQualifiedName();
assert table != null;
List<Integer> columnMapping;
final Map<Integer, RexNode> projectMap = new HashMap<>();
if (project == null) {
columnMapping = ImmutableIntList.range(0, targetRowType.getFieldCount());
} else {
columnMapping = new ArrayList<>();
for (Ord<RexNode> node : Ord.zip(project.getProjects())) {
if (node.e instanceof RexInputRef) {
RexInputRef rexInputRef = (RexInputRef) node.e;
int index = rexInputRef.getIndex();
if (projectMap.get(index) != null) {
if (fail) {
throw validator.newValidationError(sqlNode, RESOURCE.moreThanOneMappedColumn(targetRowType.getFieldList().get(index).getName(), Util.last(tablePath)));
}
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, null, null, null, null, false);
}
projectMap.put(index, rexBuilder.makeInputRef(viewRel, node.i));
columnMapping.add(index);
} else {
columnMapping.add(-1);
}
}
}
final RexNode constraint;
if (filter != null) {
constraint = filter.getCondition();
} else {
constraint = rexBuilder.makeLiteral(true);
}
final List<RexNode> filters = new ArrayList<>();
// If we put a constraint in projectMap above, then filters will not be empty despite
// being a modifiable view.
final List<RexNode> filters2 = new ArrayList<>();
boolean retry = false;
RelOptUtil.inferViewPredicates(projectMap, filters, constraint);
if (fail && !filters.isEmpty()) {
final Map<Integer, RexNode> projectMap2 = new HashMap<>();
RelOptUtil.inferViewPredicates(projectMap2, filters2, constraint);
if (!filters2.isEmpty()) {
throw validator.newValidationError(sqlNode, RESOURCE.modifiableViewMustHaveOnlyEqualityPredicates());
}
retry = true;
}
// Check that all columns that are not projected have a constant value
for (RelDataTypeField field : targetRowType.getFieldList()) {
final int x = columnMapping.indexOf(field.getIndex());
if (x >= 0) {
assert Util.skip(columnMapping, x + 1).indexOf(field.getIndex()) < 0 : "column projected more than once; should have checked above";
// target column is projected
continue;
}
if (projectMap.get(field.getIndex()) != null) {
// constant expression
continue;
}
if (field.getType().isNullable()) {
// don't need expression for nullable columns; NULL suffices
continue;
}
if (fail) {
throw validator.newValidationError(sqlNode, RESOURCE.noValueSuppliedForViewColumn(field.getName(), Util.last(tablePath)));
}
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, null, null, null, null, false);
}
final boolean modifiable = filters.isEmpty() || retry && filters2.isEmpty();
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, modifiable ? table : null, ImmutableList.copyOf(tablePath), constraint, ImmutableIntList.copyOf(columnMapping), modifiable);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project calcite by apache.
the class RelOptTableImpl method toRel.
public RelNode toRel(ToRelContext context) {
// RelOptTable by replacing with immutable RelRecordType using the same field list.
if (this.getRowType().isDynamicStruct()) {
final RelDataType staticRowType = new RelRecordType(getRowType().getFieldList());
final RelOptTable relOptTable = this.copy(staticRowType);
return relOptTable.toRel(context);
}
// If there are any virtual columns, create a copy of this table without
// those virtual columns.
final List<ColumnStrategy> strategies = getColumnStrategies();
if (strategies.contains(ColumnStrategy.VIRTUAL)) {
final RelDataTypeFactory.Builder b = context.getCluster().getTypeFactory().builder();
for (RelDataTypeField field : rowType.getFieldList()) {
if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
b.add(field.getName(), field.getType());
}
}
final RelOptTable relOptTable = new RelOptTableImpl(this.schema, b.build(), this.names, this.table, this.expressionFunction, this.rowCount) {
@Override
public <T> T unwrap(Class<T> clazz) {
if (clazz.isAssignableFrom(InitializerExpressionFactory.class)) {
return clazz.cast(NullInitializerExpressionFactory.INSTANCE);
}
return super.unwrap(clazz);
}
};
return relOptTable.toRel(context);
}
if (table instanceof TranslatableTable) {
return ((TranslatableTable) table).toRel(context, this);
}
final RelOptCluster cluster = context.getCluster();
if (Hook.ENABLE_BINDABLE.get(false)) {
return LogicalTableScan.create(cluster, this);
}
if (CalcitePrepareImpl.ENABLE_ENUMERABLE && table instanceof QueryableTable) {
return EnumerableTableScan.create(cluster, this);
}
if (table instanceof ScannableTable || table instanceof FilterableTable || table instanceof ProjectableFilterableTable) {
return LogicalTableScan.create(cluster, this);
}
if (CalcitePrepareImpl.ENABLE_ENUMERABLE) {
return EnumerableTableScan.create(cluster, this);
}
throw new AssertionError();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable in project calcite by apache.
the class RelMetadataTest method testCollation.
/**
* Unit test for
* {@link org.apache.calcite.rel.metadata.RelMdCollation#project}
* and other helper functions for deducing collations.
*/
@Test
public void testCollation() {
final Project rel = (Project) convertSql("select * from emp, dept");
final Join join = (Join) rel.getInput();
final RelOptTable empTable = join.getInput(0).getTable();
final RelOptTable deptTable = join.getInput(1).getTable();
Frameworks.withPlanner(new Frameworks.PlannerAction<Void>() {
public Void apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
checkCollation(cluster, empTable, deptTable);
return null;
}
});
}
Aggregations