Search in sources :

Example 11 with SqlNode

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

the class SqlItemOperator method reduceExpr.

@Override
public ReduceResult reduceExpr(int ordinal, TokenSequence list) {
    SqlNode left = list.node(ordinal - 1);
    SqlNode right = list.node(ordinal + 1);
    return new ReduceResult(ordinal - 1, ordinal + 2, createCall(SqlParserPos.sum(Arrays.asList(left.getParserPosition(), right.getParserPosition(), list.pos(ordinal))), left, right));
}
Also used : SqlNode(org.apache.calcite.sql.SqlNode)

Example 12 with SqlNode

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

the class SqlItemOperator method checkOperandTypes.

@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    final SqlNode left = callBinding.operand(0);
    final SqlNode right = callBinding.operand(1);
    if (!ARRAY_OR_MAP.checkSingleOperandType(callBinding, left, 0, throwOnFailure)) {
        return false;
    }
    final RelDataType operandType = callBinding.getOperandType(0);
    final SqlSingleOperandTypeChecker checker = getChecker(operandType);
    return checker.checkSingleOperandType(callBinding, right, 0, throwOnFailure);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlSingleOperandTypeChecker(org.apache.calcite.sql.type.SqlSingleOperandTypeChecker) SqlNode(org.apache.calcite.sql.SqlNode)

Example 13 with SqlNode

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

the class SqlLikeOperator method reduceExpr.

public ReduceResult reduceExpr(final int opOrdinal, TokenSequence list) {
    // Example:
    // a LIKE b || c ESCAPE d || e AND f
    // |  |    |      |      |      |
    // exp0    exp1          exp2
    SqlNode exp0 = list.node(opOrdinal - 1);
    SqlOperator op = list.op(opOrdinal);
    assert op instanceof SqlLikeOperator;
    SqlNode exp1 = SqlParserUtil.toTreeEx(list, opOrdinal + 1, getRightPrec(), SqlKind.ESCAPE);
    SqlNode exp2 = null;
    if ((opOrdinal + 2) < list.size()) {
        if (list.isOp(opOrdinal + 2)) {
            final SqlOperator op2 = list.op(opOrdinal + 2);
            if (op2.getKind() == SqlKind.ESCAPE) {
                exp2 = SqlParserUtil.toTreeEx(list, opOrdinal + 3, getRightPrec(), SqlKind.ESCAPE);
            }
        }
    }
    final SqlNode[] operands;
    int end;
    if (exp2 != null) {
        operands = new SqlNode[] { exp0, exp1, exp2 };
        end = opOrdinal + 4;
    } else {
        operands = new SqlNode[] { exp0, exp1 };
        end = opOrdinal + 2;
    }
    SqlCall call = createCall(SqlParserPos.ZERO, operands);
    return new ReduceResult(opOrdinal - 1, end, call);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 14 with SqlNode

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

the class SqlBetweenOperator method reduceExpr.

public ReduceResult reduceExpr(int opOrdinal, TokenSequence list) {
    SqlOperator op = list.op(opOrdinal);
    assert op == this;
    // Break the expression up into expressions. For example, a simple
    // expression breaks down as follows:
    // 
    // opOrdinal   endExp1
    // |           |
    // a + b BETWEEN c + d AND e + f
    // |_____|       |_____|   |_____|
    // exp0          exp1      exp2
    // Create the expression between 'BETWEEN' and 'AND'.
    SqlNode exp1 = SqlParserUtil.toTreeEx(list, opOrdinal + 1, 0, SqlKind.AND);
    if ((opOrdinal + 2) >= list.size()) {
        SqlParserPos lastPos = list.pos(list.size() - 1);
        final int line = lastPos.getEndLineNum();
        final int col = lastPos.getEndColumnNum() + 1;
        SqlParserPos errPos = new SqlParserPos(line, col, line, col);
        throw SqlUtil.newContextException(errPos, RESOURCE.betweenWithoutAnd());
    }
    if (!list.isOp(opOrdinal + 2) || list.op(opOrdinal + 2).getKind() != SqlKind.AND) {
        SqlParserPos errPos = list.pos(opOrdinal + 2);
        throw SqlUtil.newContextException(errPos, RESOURCE.betweenWithoutAnd());
    }
    // Create the expression after 'AND', but stopping if we encounter an
    // operator of lower precedence.
    // 
    // For example,
    // a BETWEEN b AND c + d OR e
    // becomes
    // (a BETWEEN b AND c + d) OR e
    // because OR has lower precedence than BETWEEN.
    SqlNode exp2 = SqlParserUtil.toTreeEx(list, opOrdinal + 3, getRightPrec(), SqlKind.OTHER);
    // Create the call.
    SqlNode exp0 = list.node(opOrdinal - 1);
    SqlCall newExp = createCall(list.pos(opOrdinal), exp0, exp1, exp2);
    // Replace all of the matched nodes with the single reduced node.
    return new ReduceResult(opOrdinal - 1, opOrdinal + 4, newExp);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 15 with SqlNode

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

the class SqlCastFunction method checkOperandTypes.

/**
 * Makes sure that the number and types of arguments are allowable.
 * Operators (such as "ROW" and "AS") which do not check their arguments can
 * override this method.
 */
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    final SqlNode left = callBinding.operand(0);
    final SqlNode right = callBinding.operand(1);
    if (SqlUtil.isNullLiteral(left, false) || left instanceof SqlDynamicParam) {
        return true;
    }
    RelDataType validatedNodeType = callBinding.getValidator().getValidatedNodeType(left);
    RelDataType returnType = callBinding.getValidator().deriveType(callBinding.getScope(), right);
    if (!SqlTypeUtil.canCastFrom(returnType, validatedNodeType, true)) {
        if (throwOnFailure) {
            throw callBinding.newError(RESOURCE.cannotCastValue(validatedNodeType.toString(), returnType.toString()));
        }
        return false;
    }
    if (SqlTypeUtil.areCharacterSetsMismatched(validatedNodeType, returnType)) {
        if (throwOnFailure) {
            // set mismatch.
            throw callBinding.newError(RESOURCE.cannotCastValue(validatedNodeType.getFullTypeString(), returnType.getFullTypeString()));
        }
        return false;
    }
    return true;
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

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