Search in sources :

Example 46 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.

the class StandardConvertletTable method convertFloorCeil.

protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) {
    final boolean floor = call.getKind() == SqlKind.FLOOR;
    // Rewrite floor, ceil of interval
    if (call.operandCount() == 1 && call.operand(0) instanceof SqlIntervalLiteral) {
        final SqlIntervalLiteral literal = call.operand(0);
        SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue();
        BigDecimal val = interval.getIntervalQualifier().getStartUnit().multiplier;
        RexNode rexInterval = cx.convertExpression(literal);
        final RexBuilder rexBuilder = cx.getRexBuilder();
        RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0));
        RexNode cond = ge(rexBuilder, rexInterval, zero);
        RexNode pad = rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE));
        RexNode cast = rexBuilder.makeReinterpretCast(rexInterval.getType(), pad, rexBuilder.makeLiteral(false));
        RexNode sum = floor ? minus(rexBuilder, rexInterval, cast) : plus(rexBuilder, rexInterval, cast);
        RexNode kase = floor ? case_(rexBuilder, rexInterval, cond, sum) : case_(rexBuilder, sum, cond, rexInterval);
        RexNode factor = rexBuilder.makeExactLiteral(val);
        RexNode div = divideInt(rexBuilder, kase, factor);
        return multiply(rexBuilder, div, factor);
    }
    // normal floor, ceil function
    return convertFunction(cx, (SqlFunction) call.getOperator(), call);
}
Also used : SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) RexBuilder(org.apache.calcite.rex.RexBuilder) BigDecimal(java.math.BigDecimal) RexNode(org.apache.calcite.rex.RexNode)

Example 47 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.

the class StandardConvertletTable method convertCall.

/**
 * Converts a {@link SqlCall} to a {@link RexCall} with a perhaps different
 * operator.
 */
private RexNode convertCall(SqlRexContext cx, SqlCall call, SqlOperator op) {
    final List<SqlNode> operands = call.getOperandList();
    final RexBuilder rexBuilder = cx.getRexBuilder();
    final SqlOperandTypeChecker.Consistency consistency = op.getOperandTypeChecker() == null ? SqlOperandTypeChecker.Consistency.NONE : op.getOperandTypeChecker().getConsistency();
    final List<RexNode> exprs = convertExpressionList(cx, operands, consistency);
    RelDataType type = rexBuilder.deriveReturnType(op, exprs);
    return rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));
}
Also used : RexBuilder(org.apache.calcite.rex.RexBuilder) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 48 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.

the class StandardConvertletTable method convertDatetimeMinus.

public RexNode convertDatetimeMinus(SqlRexContext cx, SqlDatetimeSubtractionOperator op, SqlCall call) {
    // Rewrite datetime minus
    final RexBuilder rexBuilder = cx.getRexBuilder();
    final List<SqlNode> operands = call.getOperandList();
    final List<RexNode> exprs = convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
    RelDataType int8Type = cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
    final RexNode[] casts = new RexNode[2];
    casts[0] = rexBuilder.makeCast(cx.getTypeFactory().createTypeWithNullability(int8Type, exprs.get(0).getType().isNullable()), exprs.get(0));
    casts[1] = rexBuilder.makeCast(cx.getTypeFactory().createTypeWithNullability(int8Type, exprs.get(1).getType().isNullable()), exprs.get(1));
    final RelDataType resType = cx.getValidator().getValidatedNodeType(call);
    return rexBuilder.makeCall(resType, op, exprs.subList(0, 2));
}
Also used : RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 49 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.

the class StandardConvertletTable method convertBetween.

/**
 * Converts a BETWEEN expression.
 *
 * <p>Called automatically via reflection.
 */
public RexNode convertBetween(SqlRexContext cx, SqlBetweenOperator op, SqlCall call) {
    final List<RexNode> list = convertExpressionList(cx, call.getOperandList(), op.getOperandTypeChecker().getConsistency());
    final RexNode x = list.get(SqlBetweenOperator.VALUE_OPERAND);
    final RexNode y = list.get(SqlBetweenOperator.LOWER_OPERAND);
    final RexNode z = list.get(SqlBetweenOperator.UPPER_OPERAND);
    final RexBuilder rexBuilder = cx.getRexBuilder();
    RexNode ge1 = ge(rexBuilder, x, y);
    RexNode le1 = le(rexBuilder, x, z);
    RexNode and1 = and(rexBuilder, ge1, le1);
    RexNode res;
    final SqlBetweenOperator.Flag symmetric = op.flag;
    switch(symmetric) {
        case ASYMMETRIC:
            res = and1;
            break;
        case SYMMETRIC:
            RexNode ge2 = ge(rexBuilder, x, z);
            RexNode le2 = le(rexBuilder, x, y);
            RexNode and2 = and(rexBuilder, ge2, le2);
            res = or(rexBuilder, and1, and2);
            break;
        default:
            throw Util.unexpected(symmetric);
    }
    final SqlBetweenOperator betweenOp = (SqlBetweenOperator) call.getOperator();
    if (betweenOp.isNegated()) {
        res = rexBuilder.makeCall(SqlStdOperatorTable.NOT, res);
    }
    return res;
}
Also used : SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 50 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project calcite by apache.

the class StandardConvertletTable method makeConstructorCall.

private static RexNode makeConstructorCall(SqlRexContext cx, SqlFunction constructor, List<RexNode> exprs) {
    final RexBuilder rexBuilder = cx.getRexBuilder();
    RelDataType type = rexBuilder.deriveReturnType(constructor, exprs);
    int n = type.getFieldCount();
    ImmutableList.Builder<RexNode> initializationExprs = ImmutableList.builder();
    final InitializerContext initializerContext = new InitializerContext() {

        public RexBuilder getRexBuilder() {
            return rexBuilder;
        }

        public RexNode convertExpression(SqlNode e) {
            throw new UnsupportedOperationException();
        }
    };
    for (int i = 0; i < n; ++i) {
        initializationExprs.add(cx.getInitializerExpressionFactory().newAttributeInitializer(type, constructor, i, exprs, initializerContext));
    }
    List<RexNode> defaultCasts = RexUtil.generateCastExpressions(rexBuilder, type, initializationExprs.build());
    return rexBuilder.makeNewInvocation(type, defaultCasts);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

RexBuilder (org.apache.calcite.rex.RexBuilder)314 RexNode (org.apache.calcite.rex.RexNode)248 ArrayList (java.util.ArrayList)151 RelNode (org.apache.calcite.rel.RelNode)121 RelDataType (org.apache.calcite.rel.type.RelDataType)121 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)77 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)59 RexInputRef (org.apache.calcite.rex.RexInputRef)49 RelBuilder (org.apache.calcite.tools.RelBuilder)49 AggregateCall (org.apache.calcite.rel.core.AggregateCall)48 List (java.util.List)41 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)41 RelOptCluster (org.apache.calcite.plan.RelOptCluster)36 HashMap (java.util.HashMap)33 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)24 Project (org.apache.calcite.rel.core.Project)24 RexCall (org.apache.calcite.rex.RexCall)24 BigDecimal (java.math.BigDecimal)23 Collectors (java.util.stream.Collectors)23 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)22