Search in sources :

Example 51 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 FilterSetOpTransposeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
    Filter filterRel = call.rel(0);
    SetOp setOp = call.rel(1);
    RexNode condition = filterRel.getCondition();
    // create filters on top of each setop child, modifying the filter
    // condition to reference each setop child
    RexBuilder rexBuilder = filterRel.getCluster().getRexBuilder();
    final RelBuilder relBuilder = call.builder();
    List<RelDataTypeField> origFields = setOp.getRowType().getFieldList();
    int[] adjustments = new int[origFields.size()];
    final List<RelNode> newSetOpInputs = new ArrayList<>();
    for (RelNode input : setOp.getInputs()) {
        RexNode newCondition = condition.accept(new RelOptUtil.RexInputConverter(rexBuilder, origFields, input.getRowType().getFieldList(), adjustments));
        newSetOpInputs.add(relBuilder.push(input).filter(newCondition).build());
    }
    // create a new setop whose children are the filters created above
    SetOp newSetOp = setOp.copy(setOp.getTraitSet(), newSetOpInputs);
    call.transformTo(newSetOp);
}
Also used : SetOp(org.apache.calcite.rel.core.SetOp) RelBuilder(org.apache.calcite.tools.RelBuilder) RelOptUtil(org.apache.calcite.plan.RelOptUtil) ArrayList(java.util.ArrayList) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 52 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 SqlValidatorImpl method checkTypeAssignment.

protected void checkTypeAssignment(RelDataType sourceRowType, RelDataType targetRowType, final SqlNode query) {
    // NOTE jvs 23-Feb-2006: subclasses may allow for extra targets
    // representing system-maintained columns, so stop after all sources
    // matched
    List<RelDataTypeField> sourceFields = sourceRowType.getFieldList();
    List<RelDataTypeField> targetFields = targetRowType.getFieldList();
    final int sourceCount = sourceFields.size();
    for (int i = 0; i < sourceCount; ++i) {
        RelDataType sourceType = sourceFields.get(i).getType();
        RelDataType targetType = targetFields.get(i).getType();
        if (!SqlTypeUtil.canAssignFrom(targetType, sourceType)) {
            // FRG-255:  account for UPDATE rewrite; there's
            // probably a better way to do this.
            int iAdjusted = i;
            if (query instanceof SqlUpdate) {
                int nUpdateColumns = ((SqlUpdate) query).getTargetColumnList().size();
                assert sourceFields.size() >= nUpdateColumns;
                iAdjusted -= sourceFields.size() - nUpdateColumns;
            }
            SqlNode node = getNthExpr(query, iAdjusted, sourceCount);
            String targetTypeString;
            String sourceTypeString;
            if (SqlTypeUtil.areCharacterSetsMismatched(sourceType, targetType)) {
                sourceTypeString = sourceType.getFullTypeString();
                targetTypeString = targetType.getFullTypeString();
            } else {
                sourceTypeString = sourceType.toString();
                targetTypeString = targetType.toString();
            }
            throw newValidationError(node, RESOURCE.typeNotAssignable(targetFields.get(i).getName(), targetTypeString, sourceFields.get(i).getName(), sourceTypeString));
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlUpdate(org.apache.calcite.sql.SqlUpdate) SqlNode(org.apache.calcite.sql.SqlNode)

Example 53 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 SqlValidatorImpl method createTargetRowType.

/**
 * Derives a row-type for INSERT and UPDATE operations.
 *
 * @param table            Target table for INSERT/UPDATE
 * @param targetColumnList List of target columns, or null if not specified
 * @param append           Whether to append fields to those in <code>
 *                         baseRowType</code>
 * @return Rowtype
 */
protected RelDataType createTargetRowType(SqlValidatorTable table, SqlNodeList targetColumnList, boolean append) {
    RelDataType baseRowType = table.getRowType();
    if (targetColumnList == null) {
        return baseRowType;
    }
    List<RelDataTypeField> targetFields = baseRowType.getFieldList();
    final List<Map.Entry<String, RelDataType>> types = new ArrayList<>();
    if (append) {
        for (RelDataTypeField targetField : targetFields) {
            types.add(Pair.of(SqlUtil.deriveAliasFromOrdinal(types.size()), targetField.getType()));
        }
    }
    final Set<Integer> assignedFields = new HashSet<>();
    final RelOptTable relOptTable = table instanceof RelOptTable ? ((RelOptTable) table) : null;
    for (SqlNode node : targetColumnList) {
        SqlIdentifier id = (SqlIdentifier) node;
        RelDataTypeField targetField = SqlValidatorUtil.getTargetField(baseRowType, typeFactory, id, catalogReader, relOptTable);
        if (targetField == null) {
            throw newValidationError(id, RESOURCE.unknownTargetColumn(id.toString()));
        }
        if (!assignedFields.add(targetField.getIndex())) {
            throw newValidationError(id, RESOURCE.duplicateTargetColumn(targetField.getName()));
        }
        types.add(targetField);
    }
    return typeFactory.createStructType(types);
}
Also used : BigInteger(java.math.BigInteger) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 54 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 SqlValidatorImpl method validateValues.

/**
 * Validates a VALUES clause.
 *
 * @param node          Values clause
 * @param targetRowType Row type which expression must conform to
 * @param scope         Scope within which clause occurs
 */
protected void validateValues(SqlCall node, RelDataType targetRowType, final SqlValidatorScope scope) {
    assert node.getKind() == SqlKind.VALUES;
    final List<SqlNode> operands = node.getOperandList();
    for (SqlNode operand : operands) {
        if (!(operand.getKind() == SqlKind.ROW)) {
            throw Util.needToImplement("Values function where operands are scalars");
        }
        SqlCall rowConstructor = (SqlCall) operand;
        if (conformance.isInsertSubsetColumnsAllowed() && targetRowType.isStruct() && rowConstructor.operandCount() < targetRowType.getFieldCount()) {
            targetRowType = typeFactory.createStructType(targetRowType.getFieldList().subList(0, rowConstructor.operandCount()));
        } else if (targetRowType.isStruct() && rowConstructor.operandCount() != targetRowType.getFieldCount()) {
            return;
        }
        inferUnknownTypes(targetRowType, scope, rowConstructor);
        if (targetRowType.isStruct()) {
            for (Pair<SqlNode, RelDataTypeField> pair : Pair.zip(rowConstructor.getOperandList(), targetRowType.getFieldList())) {
                if (!pair.right.getType().isNullable() && SqlUtil.isNullLiteral(pair.left, false)) {
                    throw newValidationError(node, RESOURCE.columnNotNullable(pair.right.getName()));
                }
            }
        }
    }
    for (SqlNode operand : operands) {
        operand.validate(this, scope);
    }
    // validate that all row types have the same number of columns
    // and that expressions in each column are compatible.
    // A values expression is turned into something that looks like
    // ROW(type00, type01,...), ROW(type11,...),...
    final int rowCount = operands.size();
    if (rowCount >= 2) {
        SqlCall firstRow = (SqlCall) operands.get(0);
        final int columnCount = firstRow.operandCount();
        // 1. check that all rows have the same cols length
        for (SqlNode operand : operands) {
            SqlCall thisRow = (SqlCall) operand;
            if (columnCount != thisRow.operandCount()) {
                throw newValidationError(node, RESOURCE.incompatibleValueType(SqlStdOperatorTable.VALUES.getName()));
            }
        }
        // 2. check if types at i:th position in each row are compatible
        for (int col = 0; col < columnCount; col++) {
            final int c = col;
            final RelDataType type = typeFactory.leastRestrictive(new AbstractList<RelDataType>() {

                public RelDataType get(int row) {
                    SqlCall thisRow = (SqlCall) operands.get(row);
                    return deriveType(scope, thisRow.operand(c));
                }

                public int size() {
                    return rowCount;
                }
            });
            if (null == type) {
                throw newValidationError(node, RESOURCE.incompatibleValueType(SqlStdOperatorTable.VALUES.getName()));
            }
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 55 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 SqlValidatorImpl method lookupFieldNamespace.

SqlValidatorNamespace lookupFieldNamespace(RelDataType rowType, String name) {
    final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
    final RelDataTypeField field = nameMatcher.field(rowType, name);
    return new FieldNamespace(this, field.getType());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField)

Aggregations

RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)383 RelDataType (org.apache.calcite.rel.type.RelDataType)206 RexNode (org.apache.calcite.rex.RexNode)185 ArrayList (java.util.ArrayList)177 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)25 List (java.util.List)23 LinkedHashSet (java.util.LinkedHashSet)22 RelOptUtil (org.apache.calcite.plan.RelOptUtil)22