Search in sources :

Example 6 with ColumnStrategy

use of org.apache.calcite.schema.ColumnStrategy 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();
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelOptCluster(org.apache.calcite.plan.RelOptCluster) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.calcite.rel.type.RelRecordType) QueryableTable(org.apache.calcite.schema.QueryableTable) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TranslatableTable(org.apache.calcite.schema.TranslatableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 7 with ColumnStrategy

use of org.apache.calcite.schema.ColumnStrategy in project calcite by apache.

the class RelOptTableImpl method realOrdinal.

/**
 * Converts the ordinal of a field into the ordinal of a stored field.
 * That is, it subtracts the number of virtual fields that come before it.
 */
public static int realOrdinal(final RelOptTable table, int i) {
    List<ColumnStrategy> strategies = table.getColumnStrategies();
    int n = 0;
    for (int j = 0; j < i; j++) {
        switch(strategies.get(j)) {
            case VIRTUAL:
                ++n;
        }
    }
    return i - n;
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint)

Example 8 with ColumnStrategy

use of org.apache.calcite.schema.ColumnStrategy in project calcite by apache.

the class RelOptTableImpl method realRowType.

/**
 * Returns the row type of a table after any {@link ColumnStrategy#VIRTUAL}
 * columns have been removed. This is the type of the records that are
 * actually stored.
 */
public static RelDataType realRowType(RelOptTable table) {
    final RelDataType rowType = table.getRowType();
    final List<ColumnStrategy> strategies = columnStrategies(table);
    if (!strategies.contains(ColumnStrategy.VIRTUAL)) {
        return rowType;
    }
    final RelDataTypeFactory.Builder builder = table.getRelOptSchema().getTypeFactory().builder();
    for (RelDataTypeField field : rowType.getFieldList()) {
        if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
            builder.add(field);
        }
    }
    return builder.build();
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 9 with ColumnStrategy

use of org.apache.calcite.schema.ColumnStrategy in project calcite by apache.

the class SqlValidatorImpl method checkFieldCount.

private void checkFieldCount(SqlNode node, SqlValidatorTable table, SqlNode source, RelDataType logicalSourceRowType, RelDataType logicalTargetRowType) {
    final int sourceFieldCount = logicalSourceRowType.getFieldCount();
    final int targetFieldCount = logicalTargetRowType.getFieldCount();
    if (sourceFieldCount != targetFieldCount) {
        throw newValidationError(node, RESOURCE.unmatchInsertColumn(targetFieldCount, sourceFieldCount));
    }
    // Ensure that non-nullable fields are targeted.
    final InitializerContext rexBuilder = new InitializerContext() {

        public RexBuilder getRexBuilder() {
            return new RexBuilder(typeFactory);
        }

        public RexNode convertExpression(SqlNode e) {
            throw new UnsupportedOperationException();
        }
    };
    final List<ColumnStrategy> strategies = table.unwrap(RelOptTable.class).getColumnStrategies();
    for (final RelDataTypeField field : table.getRowType().getFieldList()) {
        final RelDataTypeField targetField = logicalTargetRowType.getField(field.getName(), true, false);
        switch(strategies.get(field.getIndex())) {
            case NOT_NULLABLE:
                assert !field.getType().isNullable();
                if (targetField == null) {
                    throw newValidationError(node, RESOURCE.columnNotNullable(field.getName()));
                }
                break;
            case NULLABLE:
                assert field.getType().isNullable();
                break;
            case VIRTUAL:
            case STORED:
                if (targetField != null && !isValuesWithDefault(source, targetField.getIndex())) {
                    throw newValidationError(node, RESOURCE.insertIntoAlwaysGenerated(field.getName()));
                }
        }
    }
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) InitializerContext(org.apache.calcite.sql2rel.InitializerContext) RexBuilder(org.apache.calcite.rex.RexBuilder) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

ColumnStrategy (org.apache.calcite.schema.ColumnStrategy)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)6 RelOptTable (org.apache.calcite.plan.RelOptTable)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 RexNode (org.apache.calcite.rex.RexNode)3 SqlNode (org.apache.calcite.sql.SqlNode)3 TranslatableTable (org.apache.calcite.schema.TranslatableTable)2 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)2 InitializerContext (org.apache.calcite.sql2rel.InitializerContext)2 NlsString (org.apache.calcite.util.NlsString)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelNode (org.apache.calcite.rel.RelNode)1