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);
}
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);
}
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);
}
}
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);
}
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);
}
Aggregations