Search in sources :

Example 71 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator 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 72 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator 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 73 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project druid by druid-io.

the class DruidConvertletTable method createConvertletMap.

private static Map<SqlOperator, SqlRexConvertlet> createConvertletMap(final PlannerContext plannerContext) {
    final SqlRexConvertlet currentTimestampAndFriends = new CurrentTimestampAndFriendsConvertlet(plannerContext);
    final Map<SqlOperator, SqlRexConvertlet> table = new HashMap<>();
    for (SqlOperator operator : CURRENT_TIME_CONVERTLET_OPERATORS) {
        table.put(operator, currentTimestampAndFriends);
    }
    return table;
}
Also used : HashMap(java.util.HashMap) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlRexConvertlet(org.apache.calcite.sql2rel.SqlRexConvertlet)

Example 74 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project hazelcast by hazelcast.

the class HazelcastSqlToRelConverter method convertBetween.

/**
 * Convert "val BETWEEN lower_bound AND upper_bound" expression to
 * 1. If ASYMMETRIC : "val >= lower_bound AND val =< upper_bound" expression. Default mode.
 * 2. If SYMMETRIC : "(val >= lower_bound AND val =< upper_bound) OR (val <= lower_bound AND val >= upper_bound)"
 * expression
 */
public RexNode convertBetween(SqlCall call, Blackboard blackboard) {
    SqlOperator currentOperator = call.getOperator();
    assert currentOperator instanceof HazelcastBetweenOperator;
    final RexBuilder rexBuilder = getRexBuilder();
    final HazelcastBetweenOperator betweenOp = (HazelcastBetweenOperator) currentOperator;
    final List<RexNode> list = convertExpressionList(rexBuilder, blackboard, call.getOperandList(), betweenOp.getOperandTypeChecker().getConsistency());
    final RexNode valueOperand = list.get(SqlBetweenOperator.VALUE_OPERAND);
    final RexNode lowerOperand = list.get(SqlBetweenOperator.LOWER_OPERAND);
    final RexNode upperOperand = list.get(SqlBetweenOperator.UPPER_OPERAND);
    RexNode ge1 = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, valueOperand, lowerOperand);
    RexNode le1 = rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, valueOperand, upperOperand);
    RexNode and1 = rexBuilder.makeCall(SqlStdOperatorTable.AND, ge1, le1);
    RexNode res;
    final SqlBetweenOperator.Flag symmetric = betweenOp.getFlag();
    switch(symmetric) {
        case ASYMMETRIC:
            res = and1;
            break;
        case SYMMETRIC:
            RexNode ge2 = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, valueOperand, upperOperand);
            RexNode le2 = rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, valueOperand, lowerOperand);
            RexNode and2 = rexBuilder.makeCall(SqlStdOperatorTable.AND, ge2, le2);
            res = rexBuilder.makeCall(SqlStdOperatorTable.OR, and1, and2);
            break;
        default:
            throw Util.unexpected(symmetric);
    }
    if (betweenOp.isNegated()) {
        res = rexBuilder.makeCall(SqlStdOperatorTable.NOT, res);
    }
    return res;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) HazelcastBetweenOperator(com.hazelcast.jet.sql.impl.validate.operators.predicate.HazelcastBetweenOperator) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 75 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project hazelcast by hazelcast.

the class HazelcastCallBinding method newValidationSignatureError.

@Override
public CalciteException newValidationSignatureError() {
    SqlOperator operator = getOperator();
    SqlValidator validator = getValidator();
    SqlCall call = getCall();
    String operandTypes = getOperandTypes(validator, call, getScope());
    Resources.ExInst<SqlValidatorException> error;
    String operatorName = '\'' + operator.getName() + '\'';
    switch(operator.getSyntax()) {
        case FUNCTION:
        case FUNCTION_STAR:
        case FUNCTION_ID:
            error = RESOURCES.invalidFunctionOperands(operatorName, operandTypes);
            break;
        default:
            error = RESOURCES.invalidOperatorOperands(operatorName, operandTypes);
    }
    return validator.newValidationError(call, error);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) Resources(org.apache.calcite.runtime.Resources) SqlValidatorException(org.apache.calcite.sql.validate.SqlValidatorException)

Aggregations

SqlOperator (org.apache.calcite.sql.SqlOperator)129 ArrayList (java.util.ArrayList)44 RexNode (org.apache.calcite.rex.RexNode)41 RelDataType (org.apache.calcite.rel.type.RelDataType)25 SqlCall (org.apache.calcite.sql.SqlCall)24 RexCall (org.apache.calcite.rex.RexCall)21 SqlNode (org.apache.calcite.sql.SqlNode)21 SqlKind (org.apache.calcite.sql.SqlKind)15 List (java.util.List)13 SqlFunction (org.apache.calcite.sql.SqlFunction)13 RelNode (org.apache.calcite.rel.RelNode)12 RexBuilder (org.apache.calcite.rex.RexBuilder)11 RexInputRef (org.apache.calcite.rex.RexInputRef)11 NlsString (org.apache.calcite.util.NlsString)11 Test (org.junit.Test)11 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)10 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)10 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)9 SqlOperator (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator)9 RexLiteral (org.apache.calcite.rex.RexLiteral)9