use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField 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.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class ModifiableViewTable method getNewColumnMapping.
/**
* Creates a mapping from the view index to the index in the underlying table.
*/
private static ImmutableIntList getNewColumnMapping(Table underlying, ImmutableIntList oldColumnMapping, List<RelDataTypeField> extendedColumns, RelDataTypeFactory typeFactory) {
final List<RelDataTypeField> baseColumns = underlying.getRowType(typeFactory).getFieldList();
final Map<String, Integer> nameToIndex = mapNameToIndex(baseColumns);
final ImmutableList.Builder<Integer> newMapping = ImmutableList.builder();
newMapping.addAll(oldColumnMapping);
int newMappedIndex = baseColumns.size();
for (RelDataTypeField extendedColumn : extendedColumns) {
if (nameToIndex.containsKey(extendedColumn.getName())) {
// The extended column duplicates a column in the underlying table.
// Map to the index in the underlying table.
newMapping.add(nameToIndex.get(extendedColumn.getName()));
} else {
// The extended column is not in the underlying table.
newMapping.add(newMappedIndex++);
}
}
return ImmutableIntList.copyOf(newMapping.build());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class RexUtil method generateCastExpressions.
/**
* Generates a cast for a row type.
*
* @param rexBuilder RexBuilder to use for constructing casts
* @param lhsRowType target row type
* @param rhsExps expressions to be cast
* @return cast expressions
*/
public static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, List<RexNode> rhsExps) {
List<RelDataTypeField> lhsFields = lhsRowType.getFieldList();
List<RexNode> castExps = new ArrayList<>();
for (Pair<RelDataTypeField, RexNode> pair : Pair.zip(lhsFields, rhsExps, true)) {
RelDataTypeField lhsField = pair.left;
RelDataType lhsType = lhsField.getType();
final RexNode rhsExp = pair.right;
RelDataType rhsType = rhsExp.getType();
if (lhsType.equals(rhsType)) {
castExps.add(rhsExp);
} else {
castExps.add(rexBuilder.makeCast(lhsType, rhsExp));
}
}
return castExps;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class RexUtil method generateCastExpressions.
/**
* Generates a cast from one row type to another
*
* @param rexBuilder RexBuilder to use for constructing casts
* @param lhsRowType target row type
* @param rhsRowType source row type; fields must be 1-to-1 with lhsRowType,
* in same order
* @return cast expressions
*/
public static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, RelDataType rhsRowType) {
final List<RelDataTypeField> fieldList = rhsRowType.getFieldList();
int n = fieldList.size();
assert n == lhsRowType.getFieldCount() : "field count: lhs [" + lhsRowType + "] rhs [" + rhsRowType + "]";
List<RexNode> rhsExps = new ArrayList<>();
for (RelDataTypeField field : fieldList) {
rhsExps.add(rexBuilder.makeInputRef(field.getType(), field.getIndex()));
}
return generateCastExpressions(rexBuilder, lhsRowType, rhsExps);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class RexUtil method compatibleTypes.
/**
* Returns whether the type of an array of expressions is compatible with a
* struct type.
*
* @param exprs Array of expressions
* @param type Type
* @param litmus What to do if an error is detected (there is a mismatch)
*
* @return Whether every expression has the same type as the corresponding
* member of the struct type
*
* @see RelOptUtil#eq(String, RelDataType, String, RelDataType, org.apache.calcite.util.Litmus)
*/
public static boolean compatibleTypes(List<RexNode> exprs, RelDataType type, Litmus litmus) {
final List<RelDataTypeField> fields = type.getFieldList();
if (exprs.size() != fields.size()) {
return litmus.fail("rowtype mismatches expressions");
}
for (int i = 0; i < fields.size(); i++) {
final RelDataType exprType = exprs.get(i).getType();
final RelDataType fieldType = fields.get(i).getType();
if (!RelOptUtil.eq("type1", exprType, "type2", fieldType, litmus)) {
return litmus.fail(null);
}
}
return litmus.succeed();
}
Aggregations