Search in sources :

Example 1 with SqlCall

use of org.apache.calcite.sql.SqlCall 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 2 with SqlCall

use of org.apache.calcite.sql.SqlCall 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 3 with SqlCall

use of org.apache.calcite.sql.SqlCall in project calcite by apache.

the class SqlOverlapsOperator method arg.

void arg(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec, int i) {
    if (SqlUtil.isCallTo(call.operand(i), SqlStdOperatorTable.ROW)) {
        SqlCall row = call.operand(i);
        writer.keyword("PERIOD");
        writer.sep("(", true);
        row.operand(0).unparse(writer, leftPrec, rightPrec);
        writer.sep(",", true);
        row.operand(1).unparse(writer, leftPrec, rightPrec);
        writer.sep(")", true);
    } else {
        call.operand(i).unparse(writer, leftPrec, rightPrec);
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall)

Example 4 with SqlCall

use of org.apache.calcite.sql.SqlCall in project calcite by apache.

the class SqlRollupOperator method unparseCube.

private static void unparseCube(SqlWriter writer, SqlCall call) {
    writer.keyword(call.getOperator().getName());
    final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
    for (SqlNode operand : call.getOperandList()) {
        writer.sep(",");
        if (operand.getKind() == SqlKind.ROW) {
            final SqlWriter.Frame frame2 = writer.startList(SqlWriter.FrameTypeEnum.SIMPLE, "(", ")");
            for (SqlNode operand2 : ((SqlCall) operand).getOperandList()) {
                writer.sep(",");
                operand2.unparse(writer, 0, 0);
            }
            writer.endList(frame2);
        } else if (operand instanceof SqlNodeList && ((SqlNodeList) operand).size() == 0) {
            writer.keyword("()");
        } else {
            operand.unparse(writer, 0, 0);
        }
    }
    writer.endList(frame);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with SqlCall

use of org.apache.calcite.sql.SqlCall in project calcite by apache.

the class SqlTesterImpl method checkIntervalConv.

public void checkIntervalConv(String sql, String expected) {
    SqlValidator validator = getValidator();
    final SqlCall n = (SqlCall) parseAndValidate(validator, sql);
    SqlNode node = null;
    for (int i = 0; i < n.operandCount(); i++) {
        node = stripAs(n.operand(i));
        if (node instanceof SqlCall) {
            node = ((SqlCall) node).operand(0);
            break;
        }
    }
    assertNotNull(node);
    SqlIntervalLiteral intervalLiteral = (SqlIntervalLiteral) node;
    SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) intervalLiteral.getValue();
    long l = interval.getIntervalQualifier().isYearMonth() ? SqlParserUtil.intervalToMonths(interval) : SqlParserUtil.intervalToMillis(interval);
    String actual = l + "";
    assertEquals(expected, actual);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlCall (org.apache.calcite.sql.SqlCall)108 SqlNode (org.apache.calcite.sql.SqlNode)81 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)32 SqlNodeList (org.apache.calcite.sql.SqlNodeList)32 RelDataType (org.apache.calcite.rel.type.RelDataType)30 SqlOperator (org.apache.calcite.sql.SqlOperator)26 BitString (org.apache.calcite.util.BitString)19 ArrayList (java.util.ArrayList)18 SqlSelect (org.apache.calcite.sql.SqlSelect)17 RexNode (org.apache.calcite.rex.RexNode)13 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)12 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)11 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 SqlKind (org.apache.calcite.sql.SqlKind)9 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)9 List (java.util.List)8 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)8 Pair (org.apache.calcite.util.Pair)8 RelNode (org.apache.calcite.rel.RelNode)7