Search in sources :

Example 46 with RexInputRef

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef in project calcite by apache.

the class PigFilter method getSingleFilterCondition.

private String getSingleFilterCondition(Implementor implementor, String op, RexCall call) {
    final String fieldName;
    final String literal;
    final RexNode left = call.operands.get(0);
    final RexNode right = call.operands.get(1);
    if (left.getKind() == LITERAL) {
        if (right.getKind() != INPUT_REF) {
            throw new IllegalArgumentException("Expected a RexCall with a single field and single literal");
        } else {
            fieldName = implementor.getFieldName(this, ((RexInputRef) right).getIndex());
            literal = getLiteralAsString((RexLiteral) left);
        }
    } else if (right.getKind() == LITERAL) {
        if (left.getKind() != INPUT_REF) {
            throw new IllegalArgumentException("Expected a RexCall with a single field and single literal");
        } else {
            fieldName = implementor.getFieldName(this, ((RexInputRef) left).getIndex());
            literal = getLiteralAsString((RexLiteral) right);
        }
    } else {
        throw new IllegalArgumentException("Expected a RexCall with a single field and single literal");
    }
    return '(' + fieldName + ' ' + op + ' ' + literal + ')';
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 47 with RexInputRef

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef in project calcite by apache.

the class RelOptUtil method analyzeSimpleEquiJoin.

// to be removed before 2.0
@Deprecated
public static boolean analyzeSimpleEquiJoin(LogicalJoin join, int[] joinFieldOrdinals) {
    RexNode joinExp = join.getCondition();
    if (joinExp.getKind() != SqlKind.EQUALS) {
        return false;
    }
    RexCall binaryExpression = (RexCall) joinExp;
    RexNode leftComparand = binaryExpression.operands.get(0);
    RexNode rightComparand = binaryExpression.operands.get(1);
    if (!(leftComparand instanceof RexInputRef)) {
        return false;
    }
    if (!(rightComparand instanceof RexInputRef)) {
        return false;
    }
    final int leftFieldCount = join.getLeft().getRowType().getFieldCount();
    RexInputRef leftFieldAccess = (RexInputRef) leftComparand;
    if (!(leftFieldAccess.getIndex() < leftFieldCount)) {
        // left field must access left side of join
        return false;
    }
    RexInputRef rightFieldAccess = (RexInputRef) rightComparand;
    if (!(rightFieldAccess.getIndex() >= leftFieldCount)) {
        // right field must access right side of join
        return false;
    }
    joinFieldOrdinals[0] = leftFieldAccess.getIndex();
    joinFieldOrdinals[1] = rightFieldAccess.getIndex() - leftFieldCount;
    return true;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 48 with RexInputRef

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef in project calcite by apache.

the class RelOptUtil method pushDownEqualJoinConditions.

/**
 * Pushes down parts of a join condition.
 *
 * <p>For example, given
 * "emp JOIN dept ON emp.deptno + 1 = dept.deptno", adds a project above
 * "emp" that computes the expression
 * "emp.deptno + 1". The resulting join condition is a simple combination
 * of AND, equals, and input fields.
 */
private static RexNode pushDownEqualJoinConditions(RexNode node, int leftCount, int rightCount, List<RexNode> extraLeftExprs, List<RexNode> extraRightExprs) {
    switch(node.getKind()) {
        case AND:
        case EQUALS:
            final RexCall call = (RexCall) node;
            final List<RexNode> list = new ArrayList<>();
            List<RexNode> operands = Lists.newArrayList(call.getOperands());
            for (int i = 0; i < operands.size(); i++) {
                RexNode operand = operands.get(i);
                final int left2 = leftCount + extraLeftExprs.size();
                final int right2 = rightCount + extraRightExprs.size();
                final RexNode e = pushDownEqualJoinConditions(operand, leftCount, rightCount, extraLeftExprs, extraRightExprs);
                final List<RexNode> remainingOperands = Util.skip(operands, i + 1);
                final int left3 = leftCount + extraLeftExprs.size();
                fix(remainingOperands, left2, left3);
                fix(list, left2, left3);
                list.add(e);
            }
            if (!list.equals(call.getOperands())) {
                return call.clone(call.getType(), list);
            }
            return call;
        case OR:
        case INPUT_REF:
        case LITERAL:
            return node;
        default:
            final ImmutableBitSet bits = RelOptUtil.InputFinder.bits(node);
            final int mid = leftCount + extraLeftExprs.size();
            switch(Side.of(bits, mid)) {
                case LEFT:
                    fix(extraRightExprs, mid, mid + 1);
                    extraLeftExprs.add(node);
                    return new RexInputRef(mid, node.getType());
                case RIGHT:
                    final int index2 = mid + rightCount + extraRightExprs.size();
                    extraRightExprs.add(node);
                    return new RexInputRef(index2, node.getType());
                case BOTH:
                case EMPTY:
                default:
                    return node;
            }
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ArrayList(java.util.ArrayList) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 49 with RexInputRef

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef in project calcite by apache.

the class RelOptUtil method splitCorrelatedFilterCondition.

private static void splitCorrelatedFilterCondition(LogicalFilter filter, RexNode condition, List<RexInputRef> joinKeys, List<RexNode> correlatedJoinKeys, List<RexNode> nonEquiList) {
    if (condition instanceof RexCall) {
        RexCall call = (RexCall) condition;
        if (call.getOperator().getKind() == SqlKind.AND) {
            for (RexNode operand : call.getOperands()) {
                splitCorrelatedFilterCondition(filter, operand, joinKeys, correlatedJoinKeys, nonEquiList);
            }
            return;
        }
        if (call.getOperator().getKind() == SqlKind.EQUALS) {
            final List<RexNode> operands = call.getOperands();
            RexNode op0 = operands.get(0);
            RexNode op1 = operands.get(1);
            if (!(RexUtil.containsInputRef(op0)) && (op1 instanceof RexInputRef)) {
                correlatedJoinKeys.add(op0);
                joinKeys.add((RexInputRef) op1);
                return;
            } else if ((op0 instanceof RexInputRef) && !(RexUtil.containsInputRef(op1))) {
                joinKeys.add((RexInputRef) op0);
                correlatedJoinKeys.add(op1);
                return;
            }
        }
    }
    // The operator is not of RexCall type
    // So we fail. Fall through.
    // Add this condition to the list of non-equi-join conditions.
    nonEquiList.add(condition);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 50 with RexInputRef

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef in project calcite by apache.

the class RelOptUtil method projectJoinInputs.

// to be removed before 2.0
@Deprecated
public static void projectJoinInputs(RelNode[] inputRels, List<RexNode> leftJoinKeys, List<RexNode> rightJoinKeys, int systemColCount, List<Integer> leftKeys, List<Integer> rightKeys, List<Integer> outputProj) {
    RelNode leftRel = inputRels[0];
    RelNode rightRel = inputRels[1];
    final RelOptCluster cluster = leftRel.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RelDataTypeSystem typeSystem = cluster.getTypeFactory().getTypeSystem();
    int origLeftInputSize = leftRel.getRowType().getFieldCount();
    int origRightInputSize = rightRel.getRowType().getFieldCount();
    final List<RexNode> newLeftFields = new ArrayList<>();
    final List<String> newLeftFieldNames = new ArrayList<>();
    final List<RexNode> newRightFields = new ArrayList<>();
    final List<String> newRightFieldNames = new ArrayList<>();
    int leftKeyCount = leftJoinKeys.size();
    int rightKeyCount = rightJoinKeys.size();
    int i;
    for (i = 0; i < systemColCount; i++) {
        outputProj.add(i);
    }
    for (i = 0; i < origLeftInputSize; i++) {
        final RelDataTypeField field = leftRel.getRowType().getFieldList().get(i);
        newLeftFields.add(rexBuilder.makeInputRef(field.getType(), i));
        newLeftFieldNames.add(field.getName());
        outputProj.add(systemColCount + i);
    }
    int newLeftKeyCount = 0;
    for (i = 0; i < leftKeyCount; i++) {
        RexNode leftKey = leftJoinKeys.get(i);
        if (leftKey instanceof RexInputRef) {
            // already added to the projected left fields
            // only need to remember the index in the join key list
            leftKeys.add(((RexInputRef) leftKey).getIndex());
        } else {
            newLeftFields.add(leftKey);
            newLeftFieldNames.add(null);
            leftKeys.add(origLeftInputSize + newLeftKeyCount);
            newLeftKeyCount++;
        }
    }
    int leftFieldCount = origLeftInputSize + newLeftKeyCount;
    for (i = 0; i < origRightInputSize; i++) {
        final RelDataTypeField field = rightRel.getRowType().getFieldList().get(i);
        newRightFields.add(rexBuilder.makeInputRef(field.getType(), i));
        newRightFieldNames.add(field.getName());
        outputProj.add(systemColCount + leftFieldCount + i);
    }
    int newRightKeyCount = 0;
    for (i = 0; i < rightKeyCount; i++) {
        RexNode rightKey = rightJoinKeys.get(i);
        if (rightKey instanceof RexInputRef) {
            // already added to the projected left fields
            // only need to remember the index in the join key list
            rightKeys.add(((RexInputRef) rightKey).getIndex());
        } else {
            newRightFields.add(rightKey);
            newRightFieldNames.add(null);
            rightKeys.add(origRightInputSize + newRightKeyCount);
            newRightKeyCount++;
        }
    }
    final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
    // fields
    if (newLeftKeyCount > 0) {
        leftRel = relBuilder.push(leftRel).project(newLeftFields, newLeftFieldNames, true).build();
    }
    if (newRightKeyCount > 0) {
        rightRel = relBuilder.push(rightRel).project(newRightFields, newRightFieldNames).build();
    }
    inputRels[0] = leftRel;
    inputRels[1] = rightRel;
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) ArrayList(java.util.ArrayList) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexInputRef (org.apache.calcite.rex.RexInputRef)241 RexNode (org.apache.calcite.rex.RexNode)200 ArrayList (java.util.ArrayList)105 RelNode (org.apache.calcite.rel.RelNode)85 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)80 RexCall (org.apache.calcite.rex.RexCall)67 RelDataType (org.apache.calcite.rel.type.RelDataType)63 RexBuilder (org.apache.calcite.rex.RexBuilder)54 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)52 HashMap (java.util.HashMap)47 AggregateCall (org.apache.calcite.rel.core.AggregateCall)36 List (java.util.List)35 HashSet (java.util.HashSet)32 Pair (org.apache.calcite.util.Pair)32 RexLiteral (org.apache.calcite.rex.RexLiteral)29 Map (java.util.Map)24 RelOptUtil (org.apache.calcite.plan.RelOptUtil)24 Set (java.util.Set)20 ImmutableList (com.google.common.collect.ImmutableList)19 LinkedHashMap (java.util.LinkedHashMap)19