Search in sources :

Example 81 with RexCall

use of org.apache.beam.vendor.calcite.v1_28_0.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 82 with RexCall

use of org.apache.beam.vendor.calcite.v1_28_0.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)

Example 83 with RexCall

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

the class TimeCeilOperatorConversion method toDruidExpression.

@Override
@Nullable
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    final RexCall call = (RexCall) rexNode;
    final List<DruidExpression> functionArgs = TimeFloorOperatorConversion.toTimestampFloorOrCeilArgs(plannerContext, rowSignature, call.getOperands());
    if (functionArgs == null) {
        return null;
    }
    return DruidExpression.ofFunctionCall(Calcites.getColumnTypeForRelDataType(rexNode.getType()), "timestamp_ceil", functionArgs);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) Nullable(javax.annotation.Nullable)

Example 84 with RexCall

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

the class TimeFloorOperatorConversion method toDruidExpression.

@Override
@Nullable
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    final RexCall call = (RexCall) rexNode;
    final List<DruidExpression> functionArgs = toTimestampFloorOrCeilArgs(plannerContext, rowSignature, call.getOperands());
    if (functionArgs == null) {
        return null;
    }
    return DruidExpression.ofFunctionCall(Calcites.getColumnTypeForRelDataType(rexNode.getType()), "timestamp_floor", functionArgs);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) Nullable(javax.annotation.Nullable)

Example 85 with RexCall

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

the class TimeParseOperatorConversion method toDruidExpression.

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    final RexCall call = (RexCall) rexNode;
    final RexNode timeArg = call.getOperands().get(0);
    final DruidExpression timeExpression = Expressions.toDruidExpression(plannerContext, rowSignature, timeArg);
    if (timeExpression == null) {
        return null;
    }
    final String pattern = OperatorConversions.getOperandWithDefault(call.getOperands(), 1, RexLiteral::stringValue, null);
    final DateTimeZone timeZone = OperatorConversions.getOperandWithDefault(call.getOperands(), 2, operand -> DateTimes.inferTzFromString(RexLiteral.stringValue(operand)), plannerContext.getTimeZone());
    return DruidExpression.ofFunctionCall(Calcites.getColumnTypeForRelDataType(rexNode.getType()), "timestamp_parse", ImmutableList.of(timeExpression, DruidExpression.ofStringLiteral(pattern), DruidExpression.ofStringLiteral(timeZone.getID())));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexLiteral(org.apache.calcite.rex.RexLiteral) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) DateTimeZone(org.joda.time.DateTimeZone) 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)60 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 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)10 RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9