20 package org.apache.calcite.sql2rel;
26 import org.apache.calcite.avatica.util.DateTimeUtils;
27 import org.apache.calcite.avatica.util.TimeUnit;
28 import org.apache.calcite.plan.RelOptUtil;
29 import org.apache.calcite.rel.type.RelDataType;
30 import org.apache.calcite.rel.type.RelDataTypeFactory;
31 import org.apache.calcite.rel.type.RelDataTypeFamily;
32 import org.apache.calcite.rex.RexBuilder;
33 import org.apache.calcite.rex.RexCall;
34 import org.apache.calcite.rex.RexCallBinding;
35 import org.apache.calcite.rex.RexLiteral;
36 import org.apache.calcite.rex.RexNode;
37 import org.apache.calcite.rex.RexRangeRef;
38 import org.apache.calcite.rex.RexUtil;
39 import org.apache.calcite.sql.SqlAggFunction;
40 import org.apache.calcite.sql.SqlBinaryOperator;
41 import org.apache.calcite.sql.SqlCall;
42 import org.apache.calcite.sql.SqlDataTypeSpec;
43 import org.apache.calcite.sql.SqlFunction;
44 import org.apache.calcite.sql.SqlFunctionCategory;
45 import org.apache.calcite.sql.SqlIdentifier;
46 import org.apache.calcite.sql.SqlIntervalLiteral;
47 import org.apache.calcite.sql.SqlIntervalQualifier;
48 import org.apache.calcite.sql.SqlJdbcFunctionCall;
49 import org.apache.calcite.sql.SqlKind;
50 import org.apache.calcite.sql.SqlLiteral;
51 import org.apache.calcite.sql.SqlNode;
52 import org.apache.calcite.sql.SqlNodeList;
53 import org.apache.calcite.sql.SqlNumericLiteral;
55 import org.apache.calcite.sql.SqlUtil;
56 import org.apache.calcite.sql.SqlWindowTableFunction;
57 import org.apache.calcite.sql.fun.SqlArrayValueConstructor;
58 import org.apache.calcite.sql.fun.SqlBetweenOperator;
59 import org.apache.calcite.sql.fun.SqlCase;
60 import org.apache.calcite.sql.fun.SqlDatetimeSubtractionOperator;
61 import org.apache.calcite.sql.fun.SqlExtractFunction;
62 import org.apache.calcite.sql.fun.SqlJsonValueFunction;
63 import org.apache.calcite.sql.fun.SqlLibraryOperators;
64 import org.apache.calcite.sql.fun.SqlLiteralChainOperator;
65 import org.apache.calcite.sql.fun.SqlMapValueConstructor;
66 import org.apache.calcite.sql.fun.SqlMultisetQueryConstructor;
67 import org.apache.calcite.sql.fun.SqlMultisetValueConstructor;
68 import org.apache.calcite.sql.fun.SqlOverlapsOperator;
69 import org.apache.calcite.sql.fun.SqlRowOperator;
70 import org.apache.calcite.sql.fun.SqlSequenceValueOperator;
71 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
72 import org.apache.calcite.sql.fun.SqlTrimFunction;
73 import org.apache.calcite.sql.parser.SqlParserPos;
74 import org.apache.calcite.sql.type.SqlOperandTypeChecker;
75 import org.apache.calcite.sql.type.SqlTypeFamily;
76 import org.apache.calcite.sql.type.SqlTypeName;
77 import org.apache.calcite.sql.type.SqlTypeUtil;
78 import org.apache.calcite.sql.validate.SqlValidator;
80 import org.apache.calcite.util.Pair;
81 import org.apache.calcite.util.Util;
83 import com.google.common.collect.ImmutableList;
84 import com.google.common.collect.Lists;
86 import java.math.BigDecimal;
87 import java.math.RoundingMode;
88 import java.util.ArrayList;
89 import java.util.List;
90 import java.util.Objects;
106 addAlias(SqlStdOperatorTable.CHARACTER_LENGTH, SqlStdOperatorTable.CHAR_LENGTH);
107 addAlias(SqlStdOperatorTable.IS_UNKNOWN, SqlStdOperatorTable.IS_NULL);
108 addAlias(SqlStdOperatorTable.IS_NOT_UNKNOWN, SqlStdOperatorTable.IS_NOT_NULL);
109 addAlias(SqlStdOperatorTable.PERCENT_REMAINDER, SqlStdOperatorTable.MOD);
112 registerOp(SqlStdOperatorTable.CAST, this::convertCast);
113 registerOp(SqlLibraryOperators.INFIX_CAST, this::convertCast);
119 registerOp(SqlStdOperatorTable.IS_DISTINCT_FROM,
121 registerOp(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
124 registerOp(SqlStdOperatorTable.PLUS, this::convertPlus);
126 registerOp(SqlStdOperatorTable.MINUS, (cx, call) -> {
128 cx, call.getOperator(), call.getOperandList());
129 switch (e.getOperands().get(0).getType().getSqlTypeName()) {
140 SqlLibraryOperators.LTRIM,
new TrimConvertlet(SqlTrimFunction.Flag.LEADING));
142 SqlLibraryOperators.RTRIM,
new TrimConvertlet(SqlTrimFunction.Flag.TRAILING));
144 registerOp(SqlLibraryOperators.GREATEST,
new GreatestConvertlet());
145 registerOp(SqlLibraryOperators.LEAST,
new GreatestConvertlet());
147 registerOp(SqlLibraryOperators.NVL, (cx, call) -> {
148 final RexBuilder rexBuilder = cx.getRexBuilder();
149 final RexNode operand0 = cx.convertExpression(call.getOperandList().
get(0));
150 final RexNode operand1 = cx.convertExpression(call.getOperandList().
get(1));
151 final RelDataType
type = cx.getValidator().getValidatedNodeType(call);
152 return rexBuilder.makeCall(
type,
153 SqlStdOperatorTable.CASE,
155 rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, operand0),
156 rexBuilder.makeCast(
type, operand0),
157 rexBuilder.makeCast(
type, operand1)));
160 registerOp(SqlLibraryOperators.DECODE, (cx, call) -> {
161 final RexBuilder rexBuilder = cx.getRexBuilder();
162 final List<RexNode> operands = convertExpressionList(
163 cx, call.getOperandList(), SqlOperandTypeChecker.Consistency.NONE);
164 final RelDataType
type = cx.getValidator().getValidatedNodeType(call);
165 final List<RexNode> exprs =
new ArrayList<>();
166 for (
int i = 1; i < operands.size() - 1; i += 2) {
167 exprs.add(RelOptUtil.isDistinctFrom(
168 rexBuilder, operands.get(0), operands.get(i),
true));
169 exprs.add(operands.get(i + 1));
171 if (operands.size() % 2 == 0) {
172 exprs.add(Util.last(operands));
174 exprs.add(rexBuilder.makeNullLiteral(
type));
176 return rexBuilder.makeCall(
type, SqlStdOperatorTable.CASE, exprs);
180 registerOp(SqlStdOperatorTable.NOT_LIKE,
182 -> cx.convertExpression(
183 SqlStdOperatorTable.NOT.createCall(SqlParserPos.ZERO,
184 SqlStdOperatorTable.LIKE.createCall(
185 SqlParserPos.ZERO, call.getOperandList()))));
188 registerOp(SqlStdOperatorTable.NOT_SIMILAR_TO,
190 -> cx.convertExpression(
191 SqlStdOperatorTable.NOT.createCall(SqlParserPos.ZERO,
192 SqlStdOperatorTable.SIMILAR_TO.createCall(
193 SqlParserPos.ZERO, call.getOperandList()))));
196 registerOp(SqlStdOperatorTable.UNARY_PLUS,
197 (cx, call) -> cx.convertExpression(call.operand(0)));
200 registerOp(SqlStdOperatorTable.DOT,
202 -> cx.getRexBuilder().makeFieldAccess(
203 cx.convertExpression(call.operand(0)),
208 SqlStdOperatorTable.AS, (cx, call) -> cx.convertExpression(call.operand(0)));
210 registerOp(SqlStdOperatorTable.SQRT,
212 -> cx.convertExpression(SqlStdOperatorTable.POWER.createCall(
215 SqlLiteral.createExactNumeric(
"0.5", SqlParserPos.ZERO))));
233 registerOp(SqlStdOperatorTable.AVG,
new AvgVarianceConvertlet(SqlKind.AVG));
234 registerOp(SqlStdOperatorTable.STDDEV_POP,
235 new AvgVarianceConvertlet(SqlKind.STDDEV_POP));
236 registerOp(SqlStdOperatorTable.STDDEV_SAMP,
237 new AvgVarianceConvertlet(SqlKind.STDDEV_SAMP));
239 SqlStdOperatorTable.STDDEV,
new AvgVarianceConvertlet(SqlKind.STDDEV_SAMP));
240 registerOp(SqlStdOperatorTable.VAR_POP,
new AvgVarianceConvertlet(SqlKind.VAR_POP));
241 registerOp(SqlStdOperatorTable.VAR_SAMP,
new AvgVarianceConvertlet(SqlKind.VAR_SAMP));
242 registerOp(SqlStdOperatorTable.VARIANCE,
new AvgVarianceConvertlet(SqlKind.VAR_SAMP));
243 registerOp(SqlStdOperatorTable.COVAR_POP,
244 new RegrCovarianceConvertlet(SqlKind.COVAR_POP));
245 registerOp(SqlStdOperatorTable.COVAR_SAMP,
246 new RegrCovarianceConvertlet(SqlKind.COVAR_SAMP));
248 SqlStdOperatorTable.REGR_SXX,
new RegrCovarianceConvertlet(SqlKind.REGR_SXX));
250 SqlStdOperatorTable.REGR_SYY,
new RegrCovarianceConvertlet(SqlKind.REGR_SYY));
252 final SqlRexConvertlet floorCeilConvertlet =
new FloorCeilConvertlet();
253 registerOp(SqlStdOperatorTable.FLOOR, floorCeilConvertlet);
254 registerOp(SqlStdOperatorTable.CEIL, floorCeilConvertlet);
256 registerOp(SqlStdOperatorTable.TIMESTAMP_ADD,
new TimestampAddConvertlet());
257 registerOp(SqlStdOperatorTable.TIMESTAMP_DIFF,
new TimestampDiffConvertlet());
259 registerOp(SqlStdOperatorTable.INTERVAL, StandardConvertletTable::convertInterval);
264 registerOp(SqlStdOperatorTable.ELEMENT, (cx, call) -> {
265 assert call.operandCount() == 1;
266 final SqlNode operand = call.operand(0);
267 final RelDataType
type = cx.getValidator().getValidatedNodeType(operand);
268 if (!type.getComponentType().isStruct()) {
269 return cx.convertExpression(SqlStdOperatorTable.ELEMENT_SLICE.createCall(
270 SqlParserPos.ZERO, operand));
274 return StandardConvertletTable.this.convertCall(cx, call);
280 registerOp(SqlStdOperatorTable.ELEMENT_SLICE, (cx, call) -> {
281 assert call.operandCount() == 1;
282 final SqlNode operand = call.operand(0);
283 final RexNode expr = cx.convertExpression(
284 SqlStdOperatorTable.ELEMENT.createCall(SqlParserPos.ZERO, operand));
285 return cx.getRexBuilder().makeFieldAccess(expr, 0);
296 final SqlNode
n = call.operand(0);
297 final SqlIntervalQualifier intervalQualifier = call.operand(1);
298 final SqlIntervalLiteral literal = SqlLiteral.createInterval(
299 1,
"1", intervalQualifier, call.getParserPosition());
300 final SqlCall multiply =
301 SqlStdOperatorTable.MULTIPLY.createCall(call.getParserPosition(), n, literal);
302 return cx.convertExpression(multiply);
307 private RexNode
or(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
308 return rexBuilder.makeCall(SqlStdOperatorTable.OR, a0, a1);
311 private RexNode
eq(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
312 return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, a0, a1);
315 private RexNode
ge(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
316 return rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, a0, a1);
319 private RexNode
le(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
320 return rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, a0, a1);
323 private RexNode
and(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
324 return rexBuilder.makeCall(SqlStdOperatorTable.AND, a0, a1);
327 private static RexNode
divideInt(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
328 return rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE_INTEGER, a0, a1);
331 private RexNode
plus(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
332 return rexBuilder.makeCall(SqlStdOperatorTable.PLUS, a0, a1);
335 private RexNode
minus(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
336 return rexBuilder.makeCall(SqlStdOperatorTable.MINUS, a0, a1);
339 private static RexNode
multiply(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
340 return rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, a0, a1);
343 private RexNode
case_(RexBuilder rexBuilder, RexNode...
args) {
344 return rexBuilder.makeCall(SqlStdOperatorTable.CASE,
args);
349 private SqlCall
plus(SqlParserPos pos, SqlNode a0, SqlNode a1) {
350 return SqlStdOperatorTable.PLUS.createCall(pos, a0, a1);
357 SqlNodeList whenList = call.getWhenOperands();
358 SqlNodeList thenList = call.getThenOperands();
359 assert whenList.size() == thenList.size();
361 RexBuilder rexBuilder = cx.getRexBuilder();
362 final List<RexNode> exprList =
new ArrayList<>();
363 final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
364 final RexLiteral unknownLiteral =
365 rexBuilder.makeNullLiteral(typeFactory.createSqlType(SqlTypeName.BOOLEAN));
366 final RexLiteral nullLiteral =
367 rexBuilder.makeNullLiteral(typeFactory.createSqlType(SqlTypeName.NULL));
368 for (
int i = 0; i < whenList.size(); i++) {
369 if (SqlUtil.isNullLiteral(whenList.get(i),
false)) {
370 exprList.add(unknownLiteral);
372 exprList.add(cx.convertExpression(whenList.get(i)));
374 if (SqlUtil.isNullLiteral(thenList.get(i),
false)) {
375 exprList.add(nullLiteral);
377 exprList.add(cx.convertExpression(thenList.get(i)));
380 if (SqlUtil.isNullLiteral(call.getElseOperand(),
false)) {
381 exprList.add(nullLiteral);
383 exprList.add(cx.convertExpression(call.getElseOperand()));
386 RelDataType
type = rexBuilder.deriveReturnType(call.getOperator(), exprList);
387 for (
int i : elseArgs(exprList.size())) {
388 exprList.set(i, rexBuilder.ensureType(
type, exprList.get(i),
false));
390 return rexBuilder.makeCall(
type, SqlStdOperatorTable.CASE, exprList);
394 SqlRexContext cx, SqlMultisetValueConstructor op, SqlCall call) {
395 final RelDataType originalType = cx.getValidator().getValidatedNodeType(call);
396 RexRangeRef rr = cx.getSubQueryExpr(call);
398 RelDataType msType = rr.getType().getFieldList().get(0).getType();
399 RexNode expr = cx.getRexBuilder().makeInputRef(msType, rr.getOffset());
400 assert msType.getComponentType().isStruct();
401 if (!originalType.getComponentType().isStruct()) {
408 expr = cx.getRexBuilder().makeCall(
409 originalType, SqlStdOperatorTable.SLICE, ImmutableList.of(expr));
415 SqlRexContext cx, SqlArrayValueConstructor op, SqlCall call) {
416 return convertCall(cx, call);
419 public RexNode
convertMap(SqlRexContext cx, SqlMapValueConstructor op, SqlCall call) {
420 return convertCall(cx, call);
424 SqlRexContext cx, SqlMultisetQueryConstructor op, SqlCall call) {
425 final RelDataType originalType = cx.getValidator().getValidatedNodeType(call);
426 RexRangeRef rr = cx.getSubQueryExpr(call);
428 RelDataType msType = rr.getType().getFieldList().get(0).getType();
429 RexNode expr = cx.getRexBuilder().makeInputRef(msType, rr.getOffset());
430 assert msType.getComponentType().isStruct();
431 if (!originalType.getComponentType().isStruct()) {
438 expr = cx.getRexBuilder().makeCall(SqlStdOperatorTable.SLICE, expr);
443 public RexNode
convertJdbc(SqlRexContext cx, SqlJdbcFunctionCall op, SqlCall call) {
446 final SqlCall convertedCall = op.getLookupCall();
447 return cx.convertExpression(convertedCall);
450 protected RexNode
convertCast(SqlRexContext cx,
final SqlCall call) {
451 RelDataTypeFactory typeFactory = cx.getTypeFactory();
452 assert call.getKind() == SqlKind.CAST;
453 final SqlNode left = call.operand(0);
454 final SqlNode right = call.operand(1);
455 if (right instanceof SqlIntervalQualifier) {
456 final SqlIntervalQualifier intervalQualifier = (SqlIntervalQualifier) right;
457 if (left instanceof SqlIntervalLiteral) {
458 RexLiteral sourceInterval = (RexLiteral) cx.convertExpression(left);
459 BigDecimal sourceValue = (BigDecimal) sourceInterval.getValue();
460 RexLiteral castedInterval =
461 cx.getRexBuilder().makeIntervalLiteral(sourceValue, intervalQualifier);
462 return castToValidatedType(cx, call, castedInterval);
463 }
else if (left instanceof SqlNumericLiteral) {
464 RexLiteral sourceInterval = (RexLiteral) cx.convertExpression(left);
465 BigDecimal sourceValue = (BigDecimal) sourceInterval.getValue();
466 final BigDecimal multiplier = intervalQualifier.getUnit().multiplier;
467 sourceValue = sourceValue.multiply(multiplier);
468 RexLiteral castedInterval =
469 cx.getRexBuilder().makeIntervalLiteral(sourceValue, intervalQualifier);
470 return castToValidatedType(cx, call, castedInterval);
472 return castToValidatedType(cx, call, cx.convertExpression(left));
474 SqlDataTypeSpec dataType = (SqlDataTypeSpec) right;
475 RelDataType
type = dataType.deriveType(cx.getValidator());
477 type = cx.getValidator().getValidatedNodeType(dataType.getTypeName());
479 RexNode arg = cx.convertExpression(left);
480 if (arg.getType().isNullable()) {
481 type = typeFactory.createTypeWithNullability(
type,
true);
483 if (SqlUtil.isNullLiteral(left,
false)) {
485 validator.setValidatedNodeType(left,
type);
486 return cx.convertExpression(left);
488 if (null != dataType.getCollectionsTypeName()) {
489 final RelDataType argComponentType = arg.getType().getComponentType();
490 final RelDataType componentType = type.getComponentType();
491 if (argComponentType.isStruct() && !componentType.isStruct()) {
492 RelDataType tt = typeFactory.builder()
493 .add(argComponentType.getFieldList().get(0).getName(),
496 tt = typeFactory.createTypeWithNullability(tt, componentType.isNullable());
497 boolean isn = type.isNullable();
498 type = typeFactory.createMultisetType(tt, -1);
499 type = typeFactory.createTypeWithNullability(
type, isn);
502 return cx.getRexBuilder().makeCast(type, arg);
507 RelDataTypeFactory typeFactory = cx.getTypeFactory();
509 final SqlNode left = call.operand(0);
510 final SqlNode right = call.operand(1);
512 SqlDataTypeSpec dataType = (SqlDataTypeSpec) right;
513 RelDataType
type = dataType.deriveType(cx.getValidator());
515 type = cx.getValidator().getValidatedNodeType(dataType.getTypeName());
517 RexNode arg = cx.convertExpression(left);
518 if (arg.getType().isNullable()) {
519 type = typeFactory.createTypeWithNullability(
type,
true);
521 if (SqlUtil.isNullLiteral(left,
false)) {
523 validator.setValidatedNodeType(left,
type);
524 return cx.convertExpression(left);
526 return cx.getRexBuilder().makeCall(
532 final boolean floor = call.getKind() == SqlKind.FLOOR;
534 if (call.operandCount() == 1 && call.operand(0) instanceof SqlIntervalLiteral) {
535 final SqlIntervalLiteral literal = call.operand(0);
536 SqlIntervalLiteral.IntervalValue interval =
537 literal.getValueAs(SqlIntervalLiteral.IntervalValue.class);
538 BigDecimal val = interval.getIntervalQualifier().getStartUnit().multiplier;
539 RexNode rexInterval = cx.convertExpression(literal);
541 final RexBuilder rexBuilder = cx.getRexBuilder();
542 RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0));
543 RexNode cond = ge(rexBuilder, rexInterval, zero);
545 RexNode pad = rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE));
546 RexNode cast = rexBuilder.makeReinterpretCast(
547 rexInterval.getType(), pad, rexBuilder.makeLiteral(
false));
548 RexNode sum = floor ? minus(rexBuilder, rexInterval, cast)
549 : plus(rexBuilder, rexInterval, cast);
551 RexNode kase = floor ? case_(rexBuilder, rexInterval, cond, sum)
552 : case_(rexBuilder, sum, cond, rexInterval);
554 RexNode factor = rexBuilder.makeExactLiteral(val);
555 RexNode div = divideInt(rexBuilder, kase, factor);
556 return multiply(rexBuilder, div, factor);
560 return convertFunction(cx, (SqlFunction) call.getOperator(), call);
568 public RexNode
convertExtract(SqlRexContext cx, SqlExtractFunction op, SqlCall call) {
569 return convertFunction(cx, (SqlFunction) call.getOperator(), call);
573 RexBuilder rexBuilder, RelDataType resType, RexNode
res, BigDecimal val) {
574 if (val.equals(BigDecimal.ONE)) {
577 return rexBuilder.makeCall(
578 SqlStdOperatorTable.MOD,
res, rexBuilder.makeExactLiteral(val, resType));
581 private static RexNode
divide(RexBuilder rexBuilder, RexNode
res, BigDecimal val) {
582 if (val.equals(BigDecimal.ONE)) {
587 if (val.compareTo(BigDecimal.ONE) < 0 && val.signum() == 1) {
589 final BigDecimal reciprocal =
590 BigDecimal.ONE.divide(val, RoundingMode.UNNECESSARY);
591 return multiply(rexBuilder, res, rexBuilder.makeExactLiteral(reciprocal));
592 }
catch (ArithmeticException e) {
596 return divideInt(rexBuilder, res, rexBuilder.makeExactLiteral(val));
600 SqlRexContext cx, SqlDatetimeSubtractionOperator op, SqlCall call) {
602 final RexBuilder rexBuilder = cx.getRexBuilder();
603 final List<SqlNode> operands = call.getOperandList();
604 final List<RexNode> exprs =
605 convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
607 final RelDataType resType = cx.getValidator().getValidatedNodeType(call);
608 return rexBuilder.makeCall(resType, op, exprs.subList(0, 2));
612 final List<SqlNode> operands = call.getOperandList();
613 final List<RexNode> exprs =
614 convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
615 if (fun.getFunctionType() == SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR) {
616 return makeConstructorCall(cx, fun, exprs);
618 RelDataType returnType = cx.getValidator().getValidatedNodeTypeIfKnown(call);
619 if (returnType == null) {
620 returnType = cx.getRexBuilder().deriveReturnType(fun, exprs);
622 return cx.getRexBuilder().makeCall(returnType, fun, exprs);
626 SqlRexContext cx, SqlWindowTableFunction fun, SqlCall call) {
628 final List<SqlNode> operands = Util.skip(call.getOperandList(), 1);
629 final List<RexNode> exprs =
630 convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
631 RelDataType returnType = cx.getValidator().getValidatedNodeTypeIfKnown(call);
632 if (returnType == null) {
633 returnType = cx.getRexBuilder().deriveReturnType(fun, exprs);
635 return cx.getRexBuilder().makeCall(returnType, fun, exprs);
639 SqlRexContext cx, SqlJsonValueFunction fun, SqlCall call) {
643 List<SqlNode> operands = call.getOperandList();
644 boolean hasExplicitReturningType = SqlJsonValueFunction.hasExplicitTypeSpec(
645 operands.toArray(SqlNode.EMPTY_ARRAY));
646 if (hasExplicitReturningType) {
647 operands = SqlJsonValueFunction.removeTypeSpecOperands(call);
649 final List<RexNode> exprs =
650 convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
651 RelDataType returnType = cx.getValidator().getValidatedNodeTypeIfKnown(call);
652 return cx.getRexBuilder().makeCall(returnType, fun, exprs);
656 SqlRexContext cx, SqlSequenceValueOperator fun, SqlCall call) {
657 final List<SqlNode> operands = call.getOperandList();
658 assert operands.size() == 1;
659 assert operands.get(0) instanceof SqlIdentifier;
660 final SqlIdentifier
id = (SqlIdentifier) operands.get(0);
661 final String key = Util.listToString(id.names);
662 RelDataType returnType = cx.getValidator().getValidatedNodeType(call);
663 return cx.getRexBuilder().makeCall(
664 returnType, fun, ImmutableList.of(cx.getRexBuilder().makeLiteral(key)));
668 SqlRexContext cx, SqlAggFunction fun, SqlCall call) {
669 final List<SqlNode> operands = call.getOperandList();
670 final List<RexNode> exprs;
671 if (call.isCountStar()) {
672 exprs = ImmutableList.of();
674 exprs = convertExpressionList(cx, operands, SqlOperandTypeChecker.Consistency.NONE);
676 RelDataType returnType = cx.getValidator().getValidatedNodeTypeIfKnown(call);
677 final int groupCount = cx.getGroupCount();
678 if (returnType == null) {
679 RexCallBinding binding =
680 new RexCallBinding(cx.getTypeFactory(), fun, exprs, ImmutableList.of()) {
682 public int getGroupCount() {
686 returnType = fun.inferReturnType(binding);
688 return cx.getRexBuilder().makeCall(returnType, fun, exprs);
692 SqlRexContext cx, SqlFunction constructor, List<RexNode> exprs) {
693 final RexBuilder rexBuilder = cx.getRexBuilder();
694 RelDataType
type = rexBuilder.deriveReturnType(constructor, exprs);
696 int n = type.getFieldCount();
697 ImmutableList.Builder<RexNode> initializationExprs = ImmutableList.builder();
698 final InitializerContext initializerContext =
new InitializerContext() {
699 public RexBuilder getRexBuilder() {
703 public SqlNode validateExpression(RelDataType rowType, SqlNode expr) {
704 throw new UnsupportedOperationException();
707 public RexNode convertExpression(SqlNode e) {
708 throw new UnsupportedOperationException();
711 for (
int i = 0; i <
n; ++i) {
712 initializationExprs.add(
713 cx.getInitializerExpressionFactory().newAttributeInitializer(
714 type, constructor, i, exprs, initializerContext));
717 List<RexNode> defaultCasts = RexUtil.generateCastExpressions(
718 rexBuilder,
type, initializationExprs.build());
720 return rexBuilder.makeNewInvocation(
type, defaultCasts);
734 return convertCall(cx, call.getOperator(), call.getOperandList());
742 final RexBuilder rexBuilder = cx.getRexBuilder();
743 final SqlOperandTypeChecker.Consistency consistency =
744 op.getOperandTypeChecker() == null
745 ? SqlOperandTypeChecker.Consistency.NONE
747 final List<RexNode> exprs = convertExpressionList(cx, operands, consistency);
748 RelDataType
type = rexBuilder.deriveReturnType(op, exprs);
749 return rexBuilder.makeCall(
type, op, RexUtil.flatten(exprs, op));
755 final List<Integer> list =
new ArrayList<>();
756 for (
int i = count % 2;;) {
769 SqlOperandTypeChecker.Consistency consistency) {
770 final List<RexNode> exprs =
new ArrayList<>();
771 for (SqlNode node : nodes) {
772 exprs.add(cx.convertExpression(node));
774 if (exprs.size() > 1) {
775 final RelDataType
type = consistentType(cx, consistency, RexUtil.types(exprs));
777 final List<RexNode> oldExprs = Lists.newArrayList(exprs);
779 for (RexNode expr : oldExprs) {
780 exprs.add(cx.getRexBuilder().ensureType(type, expr,
true));
788 SqlOperandTypeChecker.Consistency consistency,
789 List<RelDataType> types) {
790 switch (consistency) {
792 if (SqlTypeUtil.areSameFamily(types)) {
796 final List<RelDataType> nonCharacterTypes =
new ArrayList<>();
797 for (RelDataType
type : types) {
798 if (
type.getFamily() != SqlTypeFamily.CHARACTER) {
799 nonCharacterTypes.add(
type);
802 if (!nonCharacterTypes.isEmpty()) {
803 final int typeCount = types.size();
804 types = nonCharacterTypes;
805 if (nonCharacterTypes.size() < typeCount) {
806 final RelDataTypeFamily family = nonCharacterTypes.get(0).getFamily();
807 if (family instanceof SqlTypeFamily) {
810 switch ((SqlTypeFamily) family) {
813 nonCharacterTypes.add(
814 cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT));
820 case LEAST_RESTRICTIVE:
821 return cx.getTypeFactory().leastRestrictive(types);
828 final RexNode rex = convertCall(cx, call);
829 switch (rex.getType().getSqlTypeName()) {
835 final RexBuilder rexBuilder = cx.getRexBuilder();
836 List<RexNode> operands = ((RexCall) rex).getOperands();
837 if (operands.size() == 2) {
838 final SqlTypeName sqlTypeName = operands.get(0).getType().getSqlTypeName();
839 switch (sqlTypeName) {
841 case INTERVAL_YEAR_MONTH:
844 case INTERVAL_DAY_HOUR:
845 case INTERVAL_DAY_MINUTE:
846 case INTERVAL_DAY_SECOND:
848 case INTERVAL_HOUR_MINUTE:
849 case INTERVAL_HOUR_SECOND:
850 case INTERVAL_MINUTE:
851 case INTERVAL_MINUTE_SECOND:
852 case INTERVAL_SECOND:
853 operands = ImmutableList.of(operands.get(1), operands.get(0));
856 return rexBuilder.makeCall(
857 rex.getType(), SqlStdOperatorTable.DATETIME_PLUS, operands);
864 RexNode op0 = cx.convertExpression(call.operand(0));
865 RexNode op1 = cx.convertExpression(call.operand(1));
866 return RelOptUtil.isDistinctFrom(cx.getRexBuilder(), op0, op1, neg);
874 public RexNode
convertBetween(SqlRexContext cx, SqlBetweenOperator op, SqlCall call) {
875 final List<RexNode> list = convertExpressionList(
876 cx, call.getOperandList(), op.getOperandTypeChecker().getConsistency());
877 final RexNode x = list.get(SqlBetweenOperator.VALUE_OPERAND);
878 final RexNode y = list.get(SqlBetweenOperator.LOWER_OPERAND);
879 final RexNode z = list.get(SqlBetweenOperator.UPPER_OPERAND);
881 final RexBuilder rexBuilder = cx.getRexBuilder();
882 RexNode ge1 = ge(rexBuilder, x, y);
883 RexNode le1 = le(rexBuilder, x, z);
884 RexNode and1 = and(rexBuilder, ge1, le1);
887 final SqlBetweenOperator.Flag symmetric = op.flag;
893 RexNode ge2 = ge(rexBuilder, x, z);
894 RexNode le2 = le(rexBuilder, x, y);
895 RexNode and2 = and(rexBuilder, ge2, le2);
896 res = or(rexBuilder, and1, and2);
899 throw Util.unexpected(symmetric);
901 final SqlBetweenOperator betweenOp = (SqlBetweenOperator) call.getOperator();
902 if (betweenOp.isNegated()) {
903 res = rexBuilder.makeCall(SqlStdOperatorTable.NOT,
res);
915 SqlRexContext cx, SqlLiteralChainOperator op, SqlCall call) {
918 SqlLiteral sum = SqlLiteralChainOperator.concatenateOperands(call);
919 return cx.convertLiteral(sum);
927 public RexNode
convertRow(SqlRexContext cx, SqlRowOperator op, SqlCall call) {
928 if (cx.getValidator().getValidatedNodeType(call).getSqlTypeName()
929 != SqlTypeName.COLUMN_LIST) {
930 return convertCall(cx, call);
932 final RexBuilder rexBuilder = cx.getRexBuilder();
933 final List<RexNode> columns =
new ArrayList<>();
934 for (SqlNode operand : call.getOperandList()) {
935 columns.add(rexBuilder.makeLiteral(((SqlIdentifier) operand).getSimple()));
937 final RelDataType
type =
938 rexBuilder.deriveReturnType(SqlStdOperatorTable.COLUMN_LIST, columns);
939 return rexBuilder.makeCall(
type, SqlStdOperatorTable.COLUMN_LIST, columns);
947 public RexNode
convertOverlaps(SqlRexContext cx, SqlOverlapsOperator op, SqlCall call) {
950 assert call.getOperandList().size() == 2;
952 final Pair<RexNode, RexNode> left =
953 convertOverlapsOperand(cx, call.getParserPosition(), call.operand(0));
954 final RexNode r0 = left.left;
955 final RexNode r1 = left.right;
956 final Pair<RexNode, RexNode> right =
957 convertOverlapsOperand(cx, call.getParserPosition(), call.operand(1));
958 final RexNode r2 = right.left;
959 final RexNode r3 = right.right;
962 final RexBuilder rexBuilder = cx.getRexBuilder();
963 RexNode leftSwap = le(rexBuilder, r0, r1);
964 final RexNode s0 = case_(rexBuilder, leftSwap, r0, r1);
965 final RexNode e0 = case_(rexBuilder, leftSwap, r1, r0);
966 RexNode rightSwap = le(rexBuilder, r2, r3);
967 final RexNode s1 = case_(rexBuilder, rightSwap, r2, r3);
968 final RexNode e1 = case_(rexBuilder, rightSwap, r3, r2);
972 return and(rexBuilder, ge(rexBuilder, e0, s1), ge(rexBuilder, e1, s0));
974 return and(rexBuilder, le(rexBuilder, s0, s1), ge(rexBuilder, e0, e1));
976 return and(rexBuilder, eq(rexBuilder, s0, s1), eq(rexBuilder, e0, e1));
978 return le(rexBuilder, e0, s1);
979 case IMMEDIATELY_PRECEDES:
980 return eq(rexBuilder, e0, s1);
982 return ge(rexBuilder, s0, e1);
983 case IMMEDIATELY_SUCCEEDS:
984 return eq(rexBuilder, s0, e1);
986 throw new AssertionError(op);
991 SqlRexContext cx, SqlParserPos pos, SqlNode operand) {
994 switch (operand.getKind()) {
996 a0 = ((SqlCall) operand).operand(0);
997 final SqlNode a10 = ((SqlCall) operand).operand(1);
998 final RelDataType t1 = cx.getValidator().getValidatedNodeType(a10);
999 if (SqlTypeUtil.isInterval(t1)) {
1001 a1 = plus(pos, a0, a10);
1011 final RexNode r0 = cx.convertExpression(a0);
1012 final RexNode r1 = cx.convertExpression(a1);
1013 return Pair.of(r0, r1);
1022 return castToValidatedType(call, value, cx.getValidator(), cx.getRexBuilder());
1031 SqlNode node, RexNode e, SqlValidator validator, RexBuilder rexBuilder) {
1032 final RelDataType
type = validator.getValidatedNodeType(node);
1033 if (e.getType() ==
type) {
1036 return rexBuilder.makeCast(
type, e);
1051 assert call.operandCount() == 2;
1052 final SqlNode arg1 = call.operand(0);
1053 final SqlNode arg2 = call.operand(1);
1055 final RelDataType
type = cx.getValidator().getValidatedNodeType(call);
1058 expr = expandCovariance(arg1, arg2, null, type, cx,
true);
1061 expr = expandCovariance(arg1, arg2, null, type, cx,
false);
1064 expr = expandRegrSzz(arg2, arg1, type, cx,
true);
1067 expr = expandRegrSzz(arg1, arg2, type, cx,
true);
1070 throw Util.unexpected(kind);
1072 RexNode rex = cx.convertExpression(expr);
1073 return cx.getRexBuilder().ensureType(type, rex,
true);
1078 final RelDataType avgType,
1079 final SqlRexContext cx,
1081 final SqlParserPos pos = SqlParserPos.ZERO;
1082 final SqlNode count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg1, arg2);
1083 final SqlNode varPop =
1084 expandCovariance(arg1, variance ? arg1 : arg2, arg2, avgType, cx,
true);
1085 final RexNode varPopRex = cx.convertExpression(varPop);
1086 final SqlNode varPopCast;
1087 varPopCast = getCastedSqlNode(varPop, avgType, pos, varPopRex);
1088 return SqlStdOperatorTable.MULTIPLY.createCall(pos, varPopCast, count);
1092 final SqlNode arg1Input,
1093 final SqlNode dependent,
1094 final RelDataType varType,
1095 final SqlRexContext cx,
1104 final SqlParserPos pos = SqlParserPos.ZERO;
1105 final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
1107 final RexNode arg0Rex = cx.convertExpression(arg0Input);
1108 final RexNode arg1Rex = cx.convertExpression(arg1Input);
1110 final SqlNode arg0 = getCastedSqlNode(arg0Input, varType, pos, arg0Rex);
1111 final SqlNode arg1 = getCastedSqlNode(arg1Input, varType, pos, arg1Rex);
1112 final SqlNode argSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, arg0, arg1);
1113 final SqlNode sumArgSquared;
1116 final SqlNode count;
1117 if (dependent == null) {
1118 sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared);
1119 sum0 = SqlStdOperatorTable.SUM.createCall(pos, arg0, arg1);
1120 sum1 = SqlStdOperatorTable.SUM.createCall(pos, arg1, arg0);
1121 count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg0, arg1);
1123 sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared, dependent);
1124 sum0 = SqlStdOperatorTable.SUM.createCall(
1125 pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent);
1126 sum1 = SqlStdOperatorTable.SUM.createCall(
1127 pos, arg1, Objects.equals(dependent, arg1Input) ? arg0 : dependent);
1128 count = SqlStdOperatorTable.REGR_COUNT.createCall(
1129 pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent);
1132 final SqlNode sumSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, sum0, sum1);
1133 final SqlNode countCasted =
1134 getCastedSqlNode(count, varType, pos, cx.convertExpression(count));
1136 final SqlNode avgSumSquared =
1137 SqlStdOperatorTable.DIVIDE.createCall(pos, sumSquared, countCasted);
1138 final SqlNode diff =
1139 SqlStdOperatorTable.MINUS.createCall(pos, sumArgSquared, avgSumSquared);
1140 SqlNode denominator;
1142 denominator = countCasted;
1144 final SqlNumericLiteral one = SqlLiteral.createExactNumeric(
"1", pos);
1145 denominator =
new SqlCase(SqlParserPos.ZERO,
1148 SqlStdOperatorTable.EQUALS.createCall(pos, countCasted, one)),
1149 SqlNodeList.of(getCastedSqlNode(nullLiteral, varType, pos, null)),
1150 SqlStdOperatorTable.MINUS.createCall(pos, countCasted, one));
1153 return SqlStdOperatorTable.DIVIDE.createCall(pos, diff, denominator);
1157 SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex) {
1159 if (argRex != null && !argRex.getType().equals(varType)) {
1160 arg = SqlStdOperatorTable.CAST.createCall(
1161 pos, argInput, SqlTypeUtil.convertTypeToSpec(varType));
1181 assert call.operandCount() == 1;
1182 final SqlNode arg = call.operand(0);
1184 final RelDataType
type = cx.getValidator().getValidatedNodeType(call);
1187 expr = expandAvg(arg, type, cx);
1190 expr = expandVariance(arg, type, cx,
true,
true);
1193 expr = expandVariance(arg, type, cx,
false,
true);
1196 expr = expandVariance(arg, type, cx,
true,
false);
1199 expr = expandVariance(arg, type, cx,
false,
false);
1202 throw Util.unexpected(kind);
1204 RexNode rex = cx.convertExpression(expr);
1205 return cx.getRexBuilder().ensureType(type, rex,
true);
1209 final SqlNode arg,
final RelDataType avgType,
final SqlRexContext cx) {
1210 final SqlParserPos pos = SqlParserPos.ZERO;
1211 final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg);
1212 final RexNode sumRex = cx.convertExpression(sum);
1213 final SqlNode sumCast;
1214 sumCast = getCastedSqlNode(sum, avgType, pos, sumRex);
1215 final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg);
1216 return SqlStdOperatorTable.DIVIDE.createCall(pos, sumCast, count);
1220 final RelDataType varType,
1221 final SqlRexContext cx,
1243 final SqlParserPos pos = SqlParserPos.ZERO;
1246 getCastedSqlNode(argInput, varType, pos, cx.convertExpression(argInput));
1248 final SqlNode argSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, arg, arg);
1249 final SqlNode argSquaredCasted = getCastedSqlNode(
1250 argSquared, varType, pos, cx.convertExpression(argSquared));
1251 final SqlNode sumArgSquared =
1252 SqlStdOperatorTable.SUM.createCall(pos, argSquaredCasted);
1253 final SqlNode sumArgSquaredCasted = getCastedSqlNode(
1254 sumArgSquared, varType, pos, cx.convertExpression(sumArgSquared));
1255 final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg);
1256 final SqlNode sumCasted =
1257 getCastedSqlNode(sum, varType, pos, cx.convertExpression(sum));
1258 final SqlNode sumSquared =
1259 SqlStdOperatorTable.MULTIPLY.createCall(pos, sumCasted, sumCasted);
1260 final SqlNode sumSquaredCasted = getCastedSqlNode(
1261 sumSquared, varType, pos, cx.convertExpression(sumSquared));
1262 final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg);
1263 final SqlNode countCasted =
1264 getCastedSqlNode(count, varType, pos, cx.convertExpression(count));
1265 final SqlNode avgSumSquared =
1266 SqlStdOperatorTable.DIVIDE.createCall(pos, sumSquaredCasted, countCasted);
1267 final SqlNode avgSumSquaredCasted = getCastedSqlNode(
1268 avgSumSquared, varType, pos, cx.convertExpression(avgSumSquared));
1269 final SqlNode diff = SqlStdOperatorTable.MINUS.createCall(
1270 pos, sumArgSquaredCasted, avgSumSquaredCasted);
1271 final SqlNode diffCasted =
1272 getCastedSqlNode(diff, varType, pos, cx.convertExpression(diff));
1273 final SqlNode denominator;
1275 denominator = countCasted;
1277 final SqlNumericLiteral one = SqlLiteral.createExactNumeric(
"1", pos);
1278 final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
1279 denominator =
new SqlCase(SqlParserPos.ZERO,
1281 SqlNodeList.of(SqlStdOperatorTable.EQUALS.createCall(pos, count, one)),
1282 SqlNodeList.of(getCastedSqlNode(nullLiteral, varType, pos, null)),
1283 SqlStdOperatorTable.MINUS.createCall(pos, count, one));
1286 SqlStdOperatorTable.DIVIDE.createCall(pos, diffCasted, denominator);
1287 final SqlNode divCasted =
1288 getCastedSqlNode(div, varType, pos, cx.convertExpression(div));
1292 final SqlNumericLiteral half = SqlLiteral.createExactNumeric(
"0.5", pos);
1293 result = SqlStdOperatorTable.POWER.createCall(pos, divCasted, half);
1299 SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex) {
1301 if (argRex != null && !argRex.getType().equals(varType)) {
1302 arg = SqlStdOperatorTable.CAST.createCall(
1303 pos, argInput, SqlTypeUtil.convertTypeToSpec(varType));
1316 private final SqlTrimFunction.Flag
flag;
1323 final RexBuilder rexBuilder = cx.getRexBuilder();
1324 final RexNode operand = cx.convertExpression(call.getOperandList().
get(0));
1325 return rexBuilder.makeCall(SqlStdOperatorTable.TRIM,
1326 rexBuilder.makeFlag(flag),
1327 rexBuilder.makeLiteral(
" "),
1349 final RexBuilder rexBuilder = cx.getRexBuilder();
1350 final RelDataType
type = cx.getValidator().getValidatedNodeType(call);
1351 final SqlBinaryOperator op;
1352 switch (call.getKind()) {
1354 op = SqlStdOperatorTable.GREATER_THAN;
1357 op = SqlStdOperatorTable.LESS_THAN;
1360 throw new AssertionError();
1362 final List<RexNode> exprs = convertExpressionList(
1363 cx, call.getOperandList(), SqlOperandTypeChecker.Consistency.NONE);
1364 final List<RexNode> list =
new ArrayList<>();
1365 final List<RexNode> orList =
new ArrayList<>();
1366 for (RexNode expr : exprs) {
1367 orList.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, expr));
1369 list.add(RexUtil.composeDisjunction(rexBuilder, orList));
1370 list.add(rexBuilder.makeNullLiteral(
type));
1371 for (
int i = 0; i < exprs.size() - 1; i++) {
1372 RexNode expr = exprs.get(i);
1373 final List<RexNode> andList =
new ArrayList<>();
1374 for (
int j = i + 1; j < exprs.size(); j++) {
1375 final RexNode expr2 = exprs.get(j);
1376 andList.add(rexBuilder.makeCall(op, expr, expr2));
1378 list.add(RexUtil.composeConjunction(rexBuilder, andList));
1381 list.add(exprs.get(exprs.size() - 1));
1382 return rexBuilder.makeCall(
type, SqlStdOperatorTable.CASE, list);
1389 return convertFloorCeil(cx, call);
1398 final RexBuilder rexBuilder = cx.getRexBuilder();
1399 final SqlLiteral unitLiteral = call.operand(0);
1400 final TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
1401 RexNode interval2Add;
1402 SqlIntervalQualifier qualifier =
1403 new SqlIntervalQualifier(unit, null, unitLiteral.getParserPosition());
1404 RexNode op1 = cx.convertExpression(call.operand(1));
1408 interval2Add = divide(rexBuilder,
1409 multiply(rexBuilder,
1410 rexBuilder.makeIntervalLiteral(BigDecimal.ONE, qualifier),
1412 BigDecimal.ONE.divide(unit.multiplier, RoundingMode.UNNECESSARY));
1415 interval2Add = multiply(rexBuilder,
1416 rexBuilder.makeIntervalLiteral(unit.multiplier, qualifier),
1420 return rexBuilder.makeCall(SqlStdOperatorTable.DATETIME_PLUS,
1421 cx.convertExpression(call.operand(2)),
1431 final RexBuilder rexBuilder = cx.getRexBuilder();
1432 final SqlLiteral unitLiteral = call.operand(0);
1433 TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
1434 BigDecimal multiplier = BigDecimal.ONE;
1435 BigDecimal divider = BigDecimal.ONE;
1436 SqlTypeName sqlTypeName =
1437 unit == TimeUnit.NANOSECOND ? SqlTypeName.BIGINT : SqlTypeName.INTEGER;
1443 multiplier = BigDecimal.valueOf(DateTimeUtils.MILLIS_PER_SECOND);
1444 divider = unit.multiplier;
1445 unit = TimeUnit.SECOND;
1448 divider = unit.multiplier;
1449 unit = TimeUnit.MONTH;
1452 final SqlIntervalQualifier qualifier =
1453 new SqlIntervalQualifier(unit, null, SqlParserPos.ZERO);
1454 final RexNode op2 = cx.convertExpression(call.operand(2));
1455 final RexNode op1 = cx.convertExpression(call.operand(1));
1456 final RelDataType intervalType = cx.getTypeFactory().createTypeWithNullability(
1457 cx.getTypeFactory().createSqlIntervalType(qualifier),
1458 op1.getType().isNullable() || op2.getType().isNullable());
1459 final RexCall rexCall = (RexCall) rexBuilder.makeCall(
1460 intervalType, SqlStdOperatorTable.MINUS_DATE, ImmutableList.of(op2, op1));
1461 final RelDataType intType = cx.getTypeFactory().createTypeWithNullability(
1462 cx.getTypeFactory().createSqlType(sqlTypeName),
1463 SqlTypeUtil.containsNullable(rexCall.getType()));
1464 RexNode e = rexBuilder.makeCast(intType, rexCall);
1465 return rexBuilder.multiplyDivide(e, multiplier, divider);
RexNode convertOverlaps(SqlRexContext cx, SqlOverlapsOperator op, SqlCall call)
RexNode convertSequenceValue(SqlRexContext cx, SqlSequenceValueOperator fun, SqlCall call)
SqlNode expandRegrSzz(final SqlNode arg1, final SqlNode arg2, final RelDataType avgType, final SqlRexContext cx, boolean variance)
RexNode mod(RexBuilder rexBuilder, RelDataType resType, RexNode res, BigDecimal val)
static final SqlFunction TRY_CAST
RexNode ge(RexBuilder rexBuilder, RexNode a0, RexNode a1)
RexNode plus(RexBuilder rexBuilder, RexNode a0, RexNode a1)
SqlNode expandCovariance(final SqlNode arg0Input, final SqlNode arg1Input, final SqlNode dependent, final RelDataType varType, final SqlRexContext cx, boolean biased)
List< Integer > elseArgs(int count)
RexNode eq(RexBuilder rexBuilder, RexNode a0, RexNode a1)
RexNode or(RexBuilder rexBuilder, RexNode a0, RexNode a1)
static final StandardConvertletTable INSTANCE
SqlNode getCastedSqlNode(SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
RexNode convertMultisetQuery(SqlRexContext cx, SqlMultisetQueryConstructor op, SqlCall call)
RexNode convertWindowFunction(SqlRexContext cx, SqlWindowTableFunction fun, SqlCall call)
RexNode convertCall(SqlRexContext cx, SqlCall call)
RexNode convertCall(SqlRexContext cx, SqlCall call)
RexNode convertFloorCeil(SqlRexContext cx, SqlCall call)
final SqlTrimFunction.Flag flag
static RexNode convertInterval(SqlRexContext cx, SqlCall call)
RegrCovarianceConvertlet(SqlKind kind)
SqlCall plus(SqlParserPos pos, SqlNode a0, SqlNode a1)
Pair< RexNode, RexNode > convertOverlapsOperand(SqlRexContext cx, SqlParserPos pos, SqlNode operand)
RexNode convertCall(SqlRexContext cx, SqlCall call)
RexNode convertCall(SqlRexContext cx, SqlCall call)
SqlNode expandVariance(final SqlNode argInput, final RelDataType varType, final SqlRexContext cx, boolean biased, boolean sqrt)
AvgVarianceConvertlet(SqlKind kind)
RexNode castToValidatedType(SqlRexContext cx, SqlCall call, RexNode value)
RexNode convertJsonValueFunction(SqlRexContext cx, SqlJsonValueFunction fun, SqlCall call)
RexNode convertAggregateFunction(SqlRexContext cx, SqlAggFunction fun, SqlCall call)
RexNode convertBetween(SqlRexContext cx, SqlBetweenOperator op, SqlCall call)
static RexNode multiply(RexBuilder rexBuilder, RexNode a0, RexNode a1)
RexNode convertCall(SqlRexContext cx, SqlCall call)
TrimConvertlet(SqlTrimFunction.Flag flag)
static RexNode divide(RexBuilder rexBuilder, RexNode res, BigDecimal val)
RexNode convertCase(SqlRexContext cx, SqlCase call)
RexNode convertLiteralChain(SqlRexContext cx, SqlLiteralChainOperator op, SqlCall call)
RexNode and(RexBuilder rexBuilder, RexNode a0, RexNode a1)
RexNode convertPlus(SqlRexContext cx, SqlCall call)
static RelDataType consistentType(SqlRexContext cx, SqlOperandTypeChecker.Consistency consistency, List< RelDataType > types)
static RexNode castToValidatedType(SqlNode node, RexNode e, SqlValidator validator, RexBuilder rexBuilder)
RexNode convertFunction(SqlRexContext cx, SqlFunction fun, SqlCall call)
static List< RexNode > convertExpressionList(SqlRexContext cx, List< SqlNode > nodes, SqlOperandTypeChecker.Consistency consistency)
std::string toString(const Executor::ExtModuleKinds &kind)
RexNode convertExtract(SqlRexContext cx, SqlExtractFunction op, SqlCall call)
SqlNode expandAvg(final SqlNode arg, final RelDataType avgType, final SqlRexContext cx)
RexNode convertCall(SqlRexContext cx, SqlOperator op, List< SqlNode > operands)
StandardConvertletTable()
RexNode convertCall(SqlRexContext cx, SqlCall call)
RexNode convertIsDistinctFrom(SqlRexContext cx, SqlCall call, boolean neg)
RexNode convertCall(SqlRexContext cx, SqlCall call)
RexNode convertCall(SqlRexContext cx, SqlCall call)
SqlNode getCastedSqlNode(SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
RexNode convertArray(SqlRexContext cx, SqlArrayValueConstructor op, SqlCall call)
static RexNode divideInt(RexBuilder rexBuilder, RexNode a0, RexNode a1)
RexNode convertJdbc(SqlRexContext cx, SqlJdbcFunctionCall op, SqlCall call)
RexNode convertRow(SqlRexContext cx, SqlRowOperator op, SqlCall call)
RexNode convertMap(SqlRexContext cx, SqlMapValueConstructor op, SqlCall call)
RexNode convertMultiset(SqlRexContext cx, SqlMultisetValueConstructor op, SqlCall call)
SqlOperandTypeChecker getOperandTypeChecker()
RexNode le(RexBuilder rexBuilder, RexNode a0, RexNode a1)
RexNode minus(RexBuilder rexBuilder, RexNode a0, RexNode a1)
RexNode convertCast(SqlRexContext cx, final SqlCall call)
RexNode case_(RexBuilder rexBuilder, RexNode...args)
RexNode convertTryCast(SqlRexContext cx, final SqlCall call)
RexNode convertDatetimeMinus(SqlRexContext cx, SqlDatetimeSubtractionOperator op, SqlCall call)
static RexNode makeConstructorCall(SqlRexContext cx, SqlFunction constructor, List< RexNode > exprs)