Search in sources :

Example 31 with RelDataTypeField

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);
}
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 32 with RelDataTypeField

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());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ImmutableList(com.google.common.collect.ImmutableList)

Example 33 with RelDataTypeField

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;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 34 with RelDataTypeField

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);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList)

Example 35 with RelDataTypeField

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();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)388 RelDataType (org.apache.calcite.rel.type.RelDataType)210 RexNode (org.apache.calcite.rex.RexNode)185 ArrayList (java.util.ArrayList)179 RelNode (org.apache.calcite.rel.RelNode)130 RexBuilder (org.apache.calcite.rex.RexBuilder)76 RexInputRef (org.apache.calcite.rex.RexInputRef)72 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)65 Pair (org.apache.calcite.util.Pair)55 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)47 HashMap (java.util.HashMap)39 Map (java.util.Map)35 AggregateCall (org.apache.calcite.rel.core.AggregateCall)35 SqlNode (org.apache.calcite.sql.SqlNode)32 ImmutableList (com.google.common.collect.ImmutableList)31 RelBuilder (org.apache.calcite.tools.RelBuilder)29 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)26 List (java.util.List)23 LinkedHashSet (java.util.LinkedHashSet)22 RelOptUtil (org.apache.calcite.plan.RelOptUtil)22