Search in sources :

Example 76 with RexCall

use of org.apache.calcite.rex.RexCall in project druid by druid-io.

the class DruidJoinRule method decomposeAnd.

@VisibleForTesting
static List<RexNode> decomposeAnd(final RexNode condition) {
    final List<RexNode> retVal = new ArrayList<>();
    final Stack<RexNode> stack = new Stack<>();
    stack.push(condition);
    while (!stack.empty()) {
        final RexNode current = stack.pop();
        if (current.isA(SqlKind.AND)) {
            final List<RexNode> operands = ((RexCall) current).getOperands();
            // Add right-to-left, so when we unwind the stack, the operands are in the original order.
            for (int i = operands.size() - 1; i >= 0; i--) {
                stack.push(operands.get(i));
            }
        } else {
            retVal.add(current);
        }
    }
    return retVal;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode) Stack(java.util.Stack) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 77 with RexCall

use of org.apache.calcite.rex.RexCall in project druid by druid-io.

the class OperatorConversions method convertCall.

@Nullable
public static DruidExpression convertCall(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final DruidExpression.DruidExpressionBuilder expressionFunction) {
    final RexCall call = (RexCall) rexNode;
    final List<DruidExpression> druidExpressions = Expressions.toDruidExpressions(plannerContext, rowSignature, call.getOperands());
    if (druidExpressions == null) {
        return null;
    }
    return expressionFunction.buildExpression(druidExpressions);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) Nullable(javax.annotation.Nullable)

Example 78 with RexCall

use of org.apache.calcite.rex.RexCall in project druid by druid-io.

the class OperatorConversions method convertCallWithPostAggOperands.

@Nullable
public static DruidExpression convertCallWithPostAggOperands(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final DruidExpression.DruidExpressionBuilder expressionFunction, final PostAggregatorVisitor postAggregatorVisitor) {
    final RexCall call = (RexCall) rexNode;
    final List<DruidExpression> druidExpressions = Expressions.toDruidExpressionsWithPostAggOperands(plannerContext, rowSignature, call.getOperands(), postAggregatorVisitor);
    if (druidExpressions == null) {
        return null;
    }
    return expressionFunction.buildExpression(druidExpressions);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) Nullable(javax.annotation.Nullable)

Example 79 with RexCall

use of org.apache.calcite.rex.RexCall in project druid by druid-io.

the class OperatorConversions method toPostAggregator.

/**
 * Translate a Calcite {@code RexNode} to a Druid PostAggregator
 *
 * @param plannerContext        SQL planner context
 * @param rowSignature          signature of the rows to be extracted from
 * @param rexNode               expression meant to be applied on top of the rows
 * @param postAggregatorVisitor visitor that manages postagg names and tracks postaggs that were created
 *                              by the translation
 *
 * @return rexNode referring to fields in rowOrder, or null if not possible
 */
@Nullable
public static PostAggregator toPostAggregator(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final PostAggregatorVisitor postAggregatorVisitor) {
    final SqlKind kind = rexNode.getKind();
    if (kind == SqlKind.INPUT_REF) {
        // Translate field references.
        final RexInputRef ref = (RexInputRef) rexNode;
        final String columnName = rowSignature.getColumnName(ref.getIndex());
        if (columnName == null) {
            throw new ISE("PostAggregator referred to nonexistent index[%d]", ref.getIndex());
        }
        return new FieldAccessPostAggregator(postAggregatorVisitor.getOutputNamePrefix() + postAggregatorVisitor.getAndIncrementCounter(), columnName);
    } else if (rexNode instanceof RexCall) {
        final SqlOperator operator = ((RexCall) rexNode).getOperator();
        final SqlOperatorConversion conversion = plannerContext.getOperatorTable().lookupOperatorConversion(operator);
        if (conversion == null) {
            return null;
        } else {
            return conversion.toPostAggregator(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
        }
    } else if (kind == SqlKind.LITERAL) {
        return null;
    } else {
        throw new IAE("Unknown rexnode kind: " + kind);
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) SqlOperator(org.apache.calcite.sql.SqlOperator) RexInputRef(org.apache.calcite.rex.RexInputRef) ISE(org.apache.druid.java.util.common.ISE) SqlKind(org.apache.calcite.sql.SqlKind) IAE(org.apache.druid.java.util.common.IAE) Nullable(javax.annotation.Nullable)

Example 80 with RexCall

use of org.apache.calcite.rex.RexCall in project druid by druid-io.

the class Expressions method rexCallToDruidExpression.

private static DruidExpression rexCallToDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final PostAggregatorVisitor postAggregatorVisitor) {
    final SqlOperator operator = ((RexCall) rexNode).getOperator();
    final SqlOperatorConversion conversion = plannerContext.getOperatorTable().lookupOperatorConversion(operator);
    if (conversion == null) {
        plannerContext.setPlanningError("SQL query requires '%s' operator that is not supported.", operator.getName());
        return null;
    } else {
        if (postAggregatorVisitor != null) {
            // try making postagg first
            PostAggregator postAggregator = conversion.toPostAggregator(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
            if (postAggregator != null) {
                postAggregatorVisitor.addPostAgg(postAggregator);
                String exprName = postAggregator.getName();
                return DruidExpression.ofColumn(postAggregator.getType(rowSignature), exprName);
            }
        }
        DruidExpression expression = conversion.toDruidExpressionWithPostAggOperands(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
        return expression;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) SqlOperator(org.apache.calcite.sql.SqlOperator)

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