Search in sources :

Example 66 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project drill by apache.

the class DrillRelOptUtil method analyzeSimpleEquiJoin.

/**
 * Returns whether the join condition is a simple equi-join or not. A simple equi-join is
 * defined as an two-table equality join (no self-join)
 * @param join input join
 * @param joinFieldOrdinals join field ordinal w.r.t. the underlying inputs to the join
 * @return TRUE if the join is a simple equi-join (not a self-join), FALSE otherwise
 */
public static boolean analyzeSimpleEquiJoin(Join join, int[] joinFieldOrdinals) {
    RexNode joinExp = join.getCondition();
    if (joinExp.getKind() != SqlKind.EQUALS) {
        return false;
    } else {
        RexCall binaryExpression = (RexCall) joinExp;
        RexNode leftComparand = binaryExpression.operands.get(0);
        RexNode rightComparand = binaryExpression.operands.get(1);
        if (!(leftComparand instanceof RexInputRef)) {
            return false;
        } else if (!(rightComparand instanceof RexInputRef)) {
            return false;
        } else {
            int leftFieldCount = join.getLeft().getRowType().getFieldCount();
            int rightFieldCount = join.getRight().getRowType().getFieldCount();
            RexInputRef leftFieldAccess = (RexInputRef) leftComparand;
            RexInputRef rightFieldAccess = (RexInputRef) rightComparand;
            if (leftFieldAccess.getIndex() >= leftFieldCount + rightFieldCount || rightFieldAccess.getIndex() >= leftFieldCount + rightFieldCount) {
                return false;
            }
            /* Both columns reference same table */
            if ((leftFieldAccess.getIndex() >= leftFieldCount && rightFieldAccess.getIndex() >= leftFieldCount) || (leftFieldAccess.getIndex() < leftFieldCount && rightFieldAccess.getIndex() < leftFieldCount)) {
                return false;
            } else {
                if (leftFieldAccess.getIndex() < leftFieldCount) {
                    joinFieldOrdinals[0] = leftFieldAccess.getIndex();
                    joinFieldOrdinals[1] = rightFieldAccess.getIndex() - leftFieldCount;
                } else {
                    joinFieldOrdinals[0] = rightFieldAccess.getIndex();
                    joinFieldOrdinals[1] = leftFieldAccess.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 67 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project drill by apache.

the class DrillProjectRelBase method getSimpleFieldCount.

private int getSimpleFieldCount() {
    int cnt = 0;
    final ComplexFieldWithNamedSegmentIdentifier complexFieldIdentifer = new ComplexFieldWithNamedSegmentIdentifier();
    // a + b, a * 10 + b, etc are not simple fields, since they are expressions.
    for (RexNode expr : this.getProjects()) {
        if (expr instanceof RexInputRef) {
            // Simple Field reference.
            cnt++;
        } else if (expr instanceof RexCall && expr.accept(complexFieldIdentifer)) {
            // Complex field with named segments only.
            cnt++;
        }
    }
    return cnt;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 68 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project drill by apache.

the class DrillRelOptUtil method containIdentity.

/**
 * Returns whether the leading edge of a given array of expressions is
 * wholly {@link RexInputRef} objects with types and names corresponding
 * to the underlying row type.
 */
private static boolean containIdentity(List<? extends RexNode> exps, RelDataType rowType, RelDataType childRowType) {
    List<RelDataTypeField> fields = rowType.getFieldList();
    List<RelDataTypeField> childFields = childRowType.getFieldList();
    int fieldCount = childFields.size();
    if (exps.size() != fieldCount) {
        return false;
    }
    for (int i = 0; i < exps.size(); i++) {
        RexNode exp = exps.get(i);
        if (!(exp instanceof RexInputRef)) {
            return false;
        }
        RexInputRef var = (RexInputRef) exp;
        if (var.getIndex() != i) {
            return false;
        }
        if (!fields.get(i).getName().equals(childFields.get(i).getName())) {
            return false;
        }
        if (!fields.get(i).getType().equals(childFields.get(i).getType())) {
            return false;
        }
    }
    return true;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 69 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project drill by apache.

the class DrillRelMdSelectivity method findRexInputRef.

private static RexInputRef findRexInputRef(final RexNode node) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {

            @Override
            public Void visitCall(RexCall call) {
                for (RexNode child : call.getOperands()) {
                    child.accept(this);
                }
                return super.visitCall(call);
            }

            @Override
            public Void visitInputRef(RexInputRef inputRef) {
                throw new Util.FoundOne(inputRef);
            }
        };
        node.accept(visitor);
        return null;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return (RexInputRef) e.getNode();
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptUtil(org.apache.calcite.plan.RelOptUtil) RelMdUtil(org.apache.calcite.rel.metadata.RelMdUtil) RexUtil(org.apache.calcite.rex.RexUtil) DrillRelOptUtil(org.apache.drill.exec.planner.common.DrillRelOptUtil) PrelUtil(org.apache.drill.exec.planner.physical.PrelUtil) Util(org.apache.calcite.util.Util) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl) RexNode(org.apache.calcite.rex.RexNode)

Example 70 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project drill by apache.

the class FieldsReWriterUtil method getFieldNameFromItemStarField.

/**
 * Checks if operator call is using item star field.
 * Will return field name if true. null otherwise.
 *
 * @param rexCall operator call
 * @param fieldNames list of field names
 * @return field name, null otherwise
 */
public static String getFieldNameFromItemStarField(RexCall rexCall, List<String> fieldNames) {
    if (!SqlStdOperatorTable.ITEM.equals(rexCall.getOperator())) {
        return null;
    }
    if (rexCall.getOperands().size() != 2) {
        return null;
    }
    if (!(rexCall.getOperands().get(0) instanceof RexInputRef && rexCall.getOperands().get(1) instanceof RexLiteral)) {
        return null;
    }
    // get parent field reference from the first operand (ITEM($0, 'col_name' -> $0)
    // and check if it corresponds to the dynamic star
    RexInputRef rexInputRef = (RexInputRef) rexCall.getOperands().get(0);
    String parentFieldName = fieldNames.get(rexInputRef.getIndex());
    if (!SchemaPath.DYNAMIC_STAR.equals(parentFieldName)) {
        return null;
    }
    // get field name from the second operand (ITEM($0, 'col_name') -> col_name)
    RexLiteral rexLiteral = (RexLiteral) rexCall.getOperands().get(1);
    if (SqlTypeName.CHAR.equals(rexLiteral.getType().getSqlTypeName())) {
        return RexLiteral.stringValue(rexLiteral);
    }
    return null;
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RexInputRef(org.apache.calcite.rex.RexInputRef)

Aggregations

RexInputRef (org.apache.calcite.rex.RexInputRef)241 RexNode (org.apache.calcite.rex.RexNode)200 ArrayList (java.util.ArrayList)103 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