Search in sources :

Example 96 with Table

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);
}
Also used : Table(org.apache.calcite.schema.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 97 with Table

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;
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) BitString(org.apache.calcite.util.BitString)

Example 98 with Table

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);
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) BigInteger(java.math.BigInteger) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 99 with Table

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"));
}
Also used : CatalogTable(org.apache.flink.table.catalog.CatalogTable) Table(org.apache.calcite.schema.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) CatalogManager(org.apache.flink.table.catalog.CatalogManager) Test(org.junit.Test)

Example 100 with Table

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"));
}
Also used : CatalogTable(org.apache.flink.table.catalog.CatalogTable) Table(org.apache.calcite.schema.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) CatalogManager(org.apache.flink.table.catalog.CatalogManager) Test(org.junit.Test)

Aggregations

Table (org.apache.calcite.schema.Table)104 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)38 RelOptTable (org.apache.calcite.plan.RelOptTable)33 Test (org.junit.Test)27 RelDataType (org.apache.calcite.rel.type.RelDataType)22 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)20 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)17 ScannableTable (org.apache.calcite.schema.ScannableTable)17 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)16 FilterableTable (org.apache.calcite.schema.FilterableTable)15 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)15 StreamableTable (org.apache.calcite.schema.StreamableTable)14 ArrayList (java.util.ArrayList)13 ModifiableViewTable (org.apache.calcite.schema.impl.ModifiableViewTable)13 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)12 RelNode (org.apache.calcite.rel.RelNode)12 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 BitString (org.apache.calcite.util.BitString)10 ImmutableMap (com.google.common.collect.ImmutableMap)9