Search in sources :

Example 76 with SqlNode

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

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

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

the class StandardConvertletTable method convertAggregateFunction.

public RexNode convertAggregateFunction(SqlRexContext cx, SqlAggFunction fun, SqlCall call) {
    final List<SqlNode> operands = call.getOperandList();
    final List<RexNode> exprs;
    if (call.isCountStar()) {
        exprs = ImmutableList.of();
    } else {
        exprs = convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
    }
    RelDataType returnType = cx.getValidator().getValidatedNodeTypeIfKnown(call);
    final int groupCount = cx.getGroupCount();
    if (returnType == null) {
        RexCallBinding binding = new RexCallBinding(cx.getTypeFactory(), fun, exprs, ImmutableList.<RelCollation>of()) {

            @Override
            public int getGroupCount() {
                return groupCount;
            }
        };
        returnType = fun.inferReturnType(binding);
    }
    return cx.getRexBuilder().makeCall(returnType, fun, exprs);
}
Also used : RexCallBinding(org.apache.calcite.rex.RexCallBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 79 with SqlNode

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

Example 80 with SqlNode

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

the class StandardConvertletTable method convertFunction.

public RexNode convertFunction(SqlRexContext cx, SqlFunction fun, SqlCall call) {
    final List<SqlNode> operands = call.getOperandList();
    final List<RexNode> exprs = convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
    if (fun.getFunctionType() == SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR) {
        return makeConstructorCall(cx, fun, exprs);
    }
    RelDataType returnType = cx.getValidator().getValidatedNodeTypeIfKnown(call);
    if (returnType == null) {
        returnType = cx.getRexBuilder().deriveReturnType(fun, exprs);
    }
    return cx.getRexBuilder().makeCall(returnType, fun, exprs);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)510 RelDataType (org.apache.calcite.rel.type.RelDataType)141 SqlNodeList (org.apache.calcite.sql.SqlNodeList)98 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)84 SqlCall (org.apache.calcite.sql.SqlCall)81 ArrayList (java.util.ArrayList)78 RelNode (org.apache.calcite.rel.RelNode)60 Test (org.junit.Test)59 BitString (org.apache.calcite.util.BitString)46 SqlSelect (org.apache.calcite.sql.SqlSelect)45 SqlWriter (org.apache.calcite.sql.SqlWriter)42 RexNode (org.apache.calcite.rex.RexNode)40 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)33 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)25 SqlLiteral (org.apache.calcite.sql.SqlLiteral)23 SqlOperator (org.apache.calcite.sql.SqlOperator)23 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 ImmutableList (com.google.common.collect.ImmutableList)20 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)20