use of org.apache.calcite.schema.Table in project flink by apache.
the class LogicalTableScan method create.
// END FLINK MODIFICATION
/**
* Creates a LogicalTableScan.
*
* @param cluster Cluster
* @param relOptTable Table
* @param hints The hints
*/
public static LogicalTableScan create(RelOptCluster cluster, final RelOptTable relOptTable, List<RelHint> hints) {
final Table table = relOptTable.unwrap(Table.class);
final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE).replaceIfs(RelCollationTraitDef.INSTANCE, () -> {
if (table != null) {
return table.getStatistic().getCollations();
}
return ImmutableList.of();
});
return new LogicalTableScan(cluster, traitSet, hints, relOptTable);
}
use of org.apache.calcite.schema.Table in project flink by apache.
the class SqlValidatorImpl method isRolledUpColumn.
// Returns true iff the given column is actually rolled up.
private boolean isRolledUpColumn(SqlIdentifier identifier, SqlValidatorScope scope) {
Pair<String, String> pair = findTableColumnPair(identifier, scope);
if (pair == null) {
return false;
}
String columnName = pair.right;
SqlValidatorTable sqlValidatorTable = scope.fullyQualify(identifier).namespace.getTable();
if (sqlValidatorTable != null) {
Table table = sqlValidatorTable.unwrap(Table.class);
return table.isRolledUp(columnName);
}
return false;
}
use of org.apache.calcite.schema.Table in project flink by apache.
the class SqlValidatorImpl method checkConstraint.
/**
* Validates insert values against the constraint of a modifiable view.
*
* @param validatorTable Table that may wrap a ModifiableViewTable
* @param source The values being inserted
* @param targetRowType The target type for the view
*/
private void checkConstraint(SqlValidatorTable validatorTable, SqlNode source, RelDataType targetRowType) {
final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
if (modifiableViewTable != null && source instanceof SqlCall) {
final Table table = modifiableViewTable.unwrap(Table.class);
final RelDataType tableRowType = table.getRowType(typeFactory);
final List<RelDataTypeField> tableFields = tableRowType.getFieldList();
// Get the mapping from column indexes of the underlying table
// to the target columns and view constraints.
final Map<Integer, RelDataTypeField> tableIndexToTargetField = SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
// Determine columns (indexed to the underlying table) that need
// to be validated against the view constraint.
final ImmutableBitSet targetColumns = ImmutableBitSet.of(tableIndexToTargetField.keySet());
final ImmutableBitSet constrainedColumns = ImmutableBitSet.of(projectMap.keySet());
final ImmutableBitSet constrainedTargetColumns = targetColumns.intersect(constrainedColumns);
// Validate insert values against the view constraint.
final List<SqlNode> values = ((SqlCall) source).getOperandList();
for (final int colIndex : constrainedTargetColumns.asList()) {
final String colName = tableFields.get(colIndex).getName();
final RelDataTypeField targetField = tableIndexToTargetField.get(colIndex);
for (SqlNode row : values) {
final SqlCall call = (SqlCall) row;
final SqlNode sourceValue = call.operand(targetField.getIndex());
final ValidationError validationError = new ValidationError(sourceValue, RESOURCE.viewConstraintNotSatisfied(colName, Util.last(validatorTable.getQualifiedName())));
RelOptUtil.validateValueAgainstConstraint(sourceValue, projectMap.get(colIndex), validationError);
}
}
}
}
use of org.apache.calcite.schema.Table in project flink by apache.
the class DatabaseCalciteSchemaTest method testPermanentTableWithPrimaryKey.
@Test
public void testPermanentTableWithPrimaryKey() {
final CatalogManager catalogManager = CatalogManagerMocks.createEmptyCatalogManager();
final DatabaseCalciteSchema calciteSchema = new DatabaseCalciteSchema(DEFAULT_CATALOG, DEFAULT_DATABASE, catalogManager, true);
catalogManager.createTable(createTable(), ObjectIdentifier.of(DEFAULT_CATALOG, DEFAULT_DATABASE, TABLE_NAME), false);
final Table table = calciteSchema.getTable(TABLE_NAME);
assertThat(table, instanceOf(CatalogSchemaTable.class));
assertThat(((CatalogSchemaTable) table).getStatistic().getUniqueKeys().iterator().next(), containsInAnyOrder("a", "b"));
}
use of org.apache.calcite.schema.Table in project flink by apache.
the class DatabaseCalciteSchemaTest method testTemporaryTableWithPrimaryKey.
@Test
public void testTemporaryTableWithPrimaryKey() {
final CatalogManager catalogManager = CatalogManagerMocks.createEmptyCatalogManager();
final DatabaseCalciteSchema calciteSchema = new DatabaseCalciteSchema("other_catalog", "other_database", catalogManager, true);
catalogManager.createTemporaryTable(createTable(), ObjectIdentifier.of("other_catalog", "other_database", TABLE_NAME), false);
final Table table = calciteSchema.getTable(TABLE_NAME);
assertThat(table, instanceOf(CatalogSchemaTable.class));
assertThat(((CatalogSchemaTable) table).getStatistic().getUniqueKeys().iterator().next(), containsInAnyOrder("a", "b"));
}
Aggregations