danny0405 commented on a change in pull request #1984:
URL: https://github.com/apache/calcite/pull/1984#discussion_r427731026



##########
File path: 
core/src/main/java/org/apache/calcite/sql/validate/implicit/TypeCoercionImpl.java
##########
@@ -154,6 +164,36 @@ public boolean binaryArithmeticCoercion(SqlCallBinding 
binding) {
     return coerced;
   }
 
+  /** For NUMERIC and DATE operands, cast NUMERIC to an INTERVAL_DAY.
+   *  <p>Only enable this coercion in Babel conformance mode.
+   */
+  protected boolean binaryArithmeticWithDateAndNumeric(
+      SqlCallBinding binding, RelDataType left, RelDataType right) {
+    // For expression "NUMERIC <OP> DATE" (or vice versa),
+    // TeradataSQL and Oracle would coerce the NUMERIC to an INTERVAL_DAY.
+    if (validator.getConformance() != SqlConformanceEnum.BABEL) {
+      return false;
+    }
+    int numericIndex;
+    if (SqlTypeUtil.isNumeric(left) && SqlTypeUtil.isDate(right)) {
+      numericIndex = 0;
+    } else if (SqlTypeUtil.isDate(left) && SqlTypeUtil.isNumeric(right)) {
+      numericIndex = 1;
+    } else {
+      return false;
+    }
+    SqlCall call = binding.getCall();
+    SqlNumericLiteral numeric = (SqlNumericLiteral) 
call.getOperandList().get(numericIndex);
+    SqlIntervalQualifier qualifier =

Review comment:
       How would you make sure the numeric operand is a literal ? Could it be 
another expression ?

##########
File path: core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
##########
@@ -476,6 +476,15 @@ private static String 
cannotStreamResultsForNonStreamingInputs(String inputs) {
     expr("mod(5.1, 3)").ok();
     expr("mod(2,5.1)").ok();
     expr("exp(3.67)").ok();
+
+    expr("CURRENT_DATE+1")
+        .withConformance(SqlConformanceEnum.BABEL).ok();
+    expr("1+CURRENT_DATE")
+        .withConformance(SqlConformanceEnum.BABEL).ok();

Review comment:
       We better also check the type.

##########
File path: 
core/src/main/java/org/apache/calcite/sql/validate/implicit/TypeCoercionImpl.java
##########
@@ -154,6 +164,36 @@ public boolean binaryArithmeticCoercion(SqlCallBinding 
binding) {
     return coerced;
   }
 
+  /** For NUMERIC and DATE operands, cast NUMERIC to an INTERVAL_DAY.
+   *  <p>Only enable this coercion in Babel conformance mode.
+   */
+  protected boolean binaryArithmeticWithDateAndNumeric(
+      SqlCallBinding binding, RelDataType left, RelDataType right) {
+    // For expression "NUMERIC <OP> DATE" (or vice versa),
+    // TeradataSQL and Oracle would coerce the NUMERIC to an INTERVAL_DAY.
+    if (validator.getConformance() != SqlConformanceEnum.BABEL) {
+      return false;
+    }
+    int numericIndex;
+    if (SqlTypeUtil.isNumeric(left) && SqlTypeUtil.isDate(right)) {
+      numericIndex = 0;
+    } else if (SqlTypeUtil.isDate(left) && SqlTypeUtil.isNumeric(right)) {
+      numericIndex = 1;
+    } else {
+      return false;
+    }
+    SqlCall call = binding.getCall();
+    SqlNumericLiteral numeric = (SqlNumericLiteral) 
call.getOperandList().get(numericIndex);
+    SqlIntervalQualifier qualifier =

Review comment:
       Why not just add a `CAST` there, let the planner decide how to translate 
to interval value.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to