Search in sources :

Example 1 with ExtensibleTable

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

the class ModifiableViewTable method extend.

/**
 * Extends the underlying table and returns a new view with updated row-type
 * and column-mapping.
 *
 * <p>The type factory is used to perform some scratch calculations, viz the
 * type mapping, but the "real" row-type will be assigned later, when the
 * table has been bound to the statement's type factory. The is important,
 * because adding types to type factories that do not belong to a statement
 * could potentially leak memory.
 *
 * @param extendedColumns Extended fields
 * @param typeFactory Type factory
 */
public final ModifiableViewTable extend(List<RelDataTypeField> extendedColumns, RelDataTypeFactory typeFactory) {
    final ExtensibleTable underlying = unwrap(ExtensibleTable.class);
    assert underlying != null;
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    final RelDataType rowType = getRowType(typeFactory);
    for (RelDataTypeField column : rowType.getFieldList()) {
        builder.add(column);
    }
    for (RelDataTypeField column : extendedColumns) {
        builder.add(column);
    }
    // The characteristics of the new view.
    final RelDataType newRowType = builder.build();
    final ImmutableIntList newColumnMapping = getNewColumnMapping(underlying, getColumnMapping(), extendedColumns, typeFactory);
    // Extend the underlying table with only the fields that
    // duplicate column names in neither the view nor the base table.
    final List<RelDataTypeField> underlyingColumns = underlying.getRowType(typeFactory).getFieldList();
    final List<RelDataTypeField> columnsOfExtendedBaseTable = RelOptUtil.deduplicateColumns(underlyingColumns, extendedColumns);
    final List<RelDataTypeField> extendColumnsOfBaseTable = columnsOfExtendedBaseTable.subList(underlyingColumns.size(), columnsOfExtendedBaseTable.size());
    final Table extendedTable = underlying.extend(extendColumnsOfBaseTable);
    return extend(extendedTable, RelDataTypeImpl.proto(newRowType), newColumnMapping);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelOptTable(org.apache.calcite.plan.RelOptTable) Table(org.apache.calcite.schema.Table) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable)

Example 2 with ExtensibleTable

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

the class SqlValidatorUtil method getExtendedColumns.

/**
 * Gets a list of extended columns with field indices to the underlying table.
 */
public static List<RelDataTypeField> getExtendedColumns(RelDataTypeFactory typeFactory, SqlValidatorTable table, SqlNodeList extendedColumns) {
    final ImmutableList.Builder<RelDataTypeField> extendedFields = ImmutableList.builder();
    final ExtensibleTable extTable = table.unwrap(ExtensibleTable.class);
    int extendedFieldOffset = extTable == null ? table.getRowType().getFieldCount() : extTable.getExtendedColumnOffset();
    for (final Pair<SqlIdentifier, SqlDataTypeSpec> pair : pairs(extendedColumns)) {
        final SqlIdentifier identifier = pair.left;
        final SqlDataTypeSpec type = pair.right;
        extendedFields.add(new RelDataTypeFieldImpl(identifier.toString(), extendedFieldOffset++, type.deriveType(typeFactory)));
    }
    return extendedFields.build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ImmutableList(com.google.common.collect.ImmutableList) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable)

Example 3 with ExtensibleTable

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

the class TableNamespace method extend.

/**
 * Creates a TableNamespace based on the same table as this one, but with
 * extended fields.
 *
 * <p>Extended fields are "hidden" or undeclared fields that may nevertheless
 * be present if you ask for them. Phoenix uses them, for instance, to access
 * rarely used fields in the underlying HBase table.
 */
public TableNamespace extend(SqlNodeList extendList) {
    final List<SqlNode> identifierList = Util.quotientList(extendList.getList(), 2, 0);
    SqlValidatorUtil.checkIdentifierListForDuplicates(identifierList, validator.getValidationErrorFunction());
    final ImmutableList.Builder<RelDataTypeField> builder = ImmutableList.builder();
    builder.addAll(this.extendedFields);
    builder.addAll(SqlValidatorUtil.getExtendedColumns(validator.getTypeFactory(), getTable(), extendList));
    final List<RelDataTypeField> extendedFields = builder.build();
    final Table schemaTable = table.unwrap(Table.class);
    if (schemaTable != null && table instanceof RelOptTable && (schemaTable instanceof ExtensibleTable || schemaTable instanceof ModifiableViewTable)) {
        checkExtendedColumnTypes(extendList);
        final RelOptTable relOptTable = ((RelOptTable) table).extend(extendedFields);
        final SqlValidatorTable validatorTable = relOptTable.unwrap(SqlValidatorTable.class);
        return new TableNamespace(validator, validatorTable, ImmutableList.<RelDataTypeField>of());
    }
    return new TableNamespace(validator, table, extendedFields);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Table(org.apache.calcite.schema.Table) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ImmutableList(com.google.common.collect.ImmutableList) RelOptTable(org.apache.calcite.plan.RelOptTable) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)3 ExtensibleTable (org.apache.calcite.schema.ExtensibleTable)3 ImmutableList (com.google.common.collect.ImmutableList)2 RelOptTable (org.apache.calcite.plan.RelOptTable)2 Table (org.apache.calcite.schema.Table)2 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)1 ModifiableViewTable (org.apache.calcite.schema.impl.ModifiableViewTable)1 SqlDataTypeSpec (org.apache.calcite.sql.SqlDataTypeSpec)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlNode (org.apache.calcite.sql.SqlNode)1 ImmutableIntList (org.apache.calcite.util.ImmutableIntList)1