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);
}
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();
}
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);
}
Aggregations