Search in sources :

Example 51 with RexCall

use of org.apache.calcite.rex.RexCall in project drill by axbaretto.

the class RewriteProjectToFlatten method visitProject.

@Override
public Prel visitProject(ProjectPrel node, Object unused) throws RelConversionException {
    ProjectPrel project = node;
    List<RexNode> exprList = new ArrayList<>();
    boolean rewrite = false;
    List<RelDataTypeField> relDataTypes = new ArrayList<>();
    int i = 0;
    RexNode flatttenExpr = null;
    for (RexNode rex : project.getChildExps()) {
        RexNode newExpr = rex;
        if (rex instanceof RexCall) {
            RexCall function = (RexCall) rex;
            String functionName = function.getOperator().getName();
            if (functionName.equalsIgnoreCase("flatten")) {
                rewrite = true;
                if (function.getOperands().size() != 1) {
                    throw new RelConversionException("Flatten expression expects a single input.");
                }
                newExpr = function.getOperands().get(0);
                RexBuilder builder = new RexBuilder(factory);
                flatttenExpr = builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), i);
            }
        }
        relDataTypes.add(project.getRowType().getFieldList().get(i));
        i++;
        exprList.add(newExpr);
    }
    if (rewrite == true) {
        // TODO - figure out what is the right setting for the traits
        Prel newChild = ((Prel) project.getInput(0)).accept(this, null);
        ProjectPrel newProject = new ProjectPrel(node.getCluster(), project.getTraitSet(), newChild, exprList, new RelRecordType(relDataTypes));
        FlattenPrel flatten = new FlattenPrel(project.getCluster(), project.getTraitSet(), newProject, flatttenExpr);
        return flatten;
    }
    Prel child = ((Prel) project.getInput()).accept(this, null);
    return (Prel) project.copy(project.getTraitSet(), child, exprList, new RelRecordType(relDataTypes));
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) ArrayList(java.util.ArrayList) RelDataTypeDrillImpl(org.apache.drill.exec.planner.types.RelDataTypeDrillImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RelConversionException(org.apache.calcite.tools.RelConversionException) Prel(org.apache.drill.exec.planner.physical.Prel) ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexCall(org.apache.calcite.rex.RexCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataTypeHolder(org.apache.drill.exec.planner.types.RelDataTypeHolder) RexNode(org.apache.calcite.rex.RexNode)

Example 52 with RexCall

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

the class RexVisitorComplexExprSplitter method visitCall.

@Override
public RexNode visitCall(RexCall call) {
    final List<RexNode> newOps = new ArrayList<>();
    for (RexNode operand : call.operands) {
        RexNode newOp = operand.accept(this);
        newOps.add(newOp);
    }
    final RexCall newCall = call.clone(call.getType(), newOps);
    String functionName = call.getOperator().getName();
    if (funcReg.isFunctionComplexOutput(functionName)) {
        RexNode ret = rexBuilder.makeInputRef(newCall.getType(), lastUsedIndex++);
        complexExprs.add(newCall);
        return ret;
    } else {
        return newCall;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode)

Example 53 with RexCall

use of org.apache.calcite.rex.RexCall 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 54 with RexCall

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

the class NumericEquiDepthHistogram method getValuesRange.

private Range<Double> getValuesRange(List<RexNode> filterList, Range<Double> fullRange, List<RexNode> unkownFilterList) {
    Range<Double> currentRange = fullRange;
    for (RexNode filter : filterList) {
        if (filter instanceof RexCall) {
            Double value = getLiteralValue(filter);
            if (value == null) {
                unkownFilterList.add(filter);
            } else {
                // get the operator
                SqlOperator op = ((RexCall) filter).getOperator();
                Range range;
                switch(op.getKind()) {
                    case GREATER_THAN:
                        range = Range.greaterThan(value);
                        currentRange = currentRange.intersection(range);
                        break;
                    case GREATER_THAN_OR_EQUAL:
                        range = Range.atLeast(value);
                        currentRange = currentRange.intersection(range);
                        break;
                    case LESS_THAN:
                        range = Range.lessThan(value);
                        currentRange = currentRange.intersection(range);
                        break;
                    case LESS_THAN_OR_EQUAL:
                        range = Range.atMost(value);
                        currentRange = currentRange.intersection(range);
                        break;
                    default:
                        break;
                }
            }
        }
    }
    return currentRange;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) SqlOperator(org.apache.calcite.sql.SqlOperator) Range(org.apache.drill.shaded.guava.com.google.common.collect.Range) RexNode(org.apache.calcite.rex.RexNode)

Example 55 with RexCall

use of org.apache.calcite.rex.RexCall 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)

Aggregations

RexCall (org.apache.calcite.rex.RexCall)213 RexNode (org.apache.calcite.rex.RexNode)172 RexInputRef (org.apache.calcite.rex.RexInputRef)61 ArrayList (java.util.ArrayList)59 RexLiteral (org.apache.calcite.rex.RexLiteral)44 Nullable (javax.annotation.Nullable)35 RelNode (org.apache.calcite.rel.RelNode)26 RelDataType (org.apache.calcite.rel.type.RelDataType)24 SqlOperator (org.apache.calcite.sql.SqlOperator)23 List (java.util.List)22 RexBuilder (org.apache.calcite.rex.RexBuilder)22 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)19 SqlKind (org.apache.calcite.sql.SqlKind)14 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)14 RelOptUtil (org.apache.calcite.plan.RelOptUtil)11 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)11 RexCall (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall)10 RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)10 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9