You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2021/01/04 02:31:04 UTC

[GitHub] [calcite] xwkuang5 opened a new pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

xwkuang5 opened a new pull request #2312:
URL: https://github.com/apache/calcite/pull/2312


   … Kuang)
   
   Override `validateCall` and `deriveType` in `SqlCastFunction` and
   wrap `UnsupportedOperationException` thrown from the base class
   implementation.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] julianhyde commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
julianhyde commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r553507779



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       As I said in the JIRA case, this is not an exceptional case, and UnsupportedOperationException should not be thrown anywhere along this code path.
   
   Don't approach this task with the framing 'how do we handle this exception?', but rather 'how do we make the validator do its job and clearly identify to the user types that are invalid?'.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] vlsi commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
vlsi commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r551894513



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));
+    }
+  }
+
+  @Override public RelDataType deriveType(final SqlValidator validator,
+      final SqlValidatorScope scope, final SqlCall call) {
+    try {
+      return super.deriveType(validator, scope, call);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;

Review comment:
       Could you please remove the assert from here?
   What is the purpose of the assert?

##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;

Review comment:
       Could you please remove the assert from here?
   What is the purpose of the assert?

##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       Should the original `UnsupportedOperationException` be kept as a `cause` of the exception?

##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));
+    }
+  }
+
+  @Override public RelDataType deriveType(final SqlValidator validator,
+      final SqlValidatorScope scope, final SqlCall call) {
+    try {
+      return super.deriveType(validator, scope, call);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       Should the original `UnsupportedOperationException` be kept as a `cause` of the exception?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] julianhyde commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
julianhyde commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r552969406



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       no




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r552327762



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;

Review comment:
       I forgot to remove the assert. This was added during development and test. 
   
   I will remove it when I push the updates once the other comment is resolved.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 closed pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 closed pull request #2312:
URL: https://github.com/apache/calcite/pull/2312


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] danny0405 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
danny0405 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r563433210



##########
File path: core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
##########
@@ -1212,17 +1212,15 @@ public void _testLikeAndSimilarFails() {
   }
 
   @Test void testCastRegisteredType() {
-    expr("cast(123 as customBigInt)")
-        .fails("class org.apache.calcite.sql.SqlIdentifier: CUSTOMBIGINT");
+    wholeExpr("cast(123 as customBigInt)")
+        .fails("Cast function does not support casting to type `CUSTOMBIGINT`");
     expr("cast(123 as sales.customBigInt)")

Review comment:
       Instead of message `Cast function does not support casting to type`, i guess `Cast from {fromType} to {toType} is not supported` is more clear ?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] vlsi commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
vlsi commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r552474088



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));
+    }
+  }
+
+  @Override public RelDataType deriveType(final SqlValidator validator,
+      final SqlValidatorScope scope, final SqlCall call) {
+    try {
+      return super.deriveType(validator, scope, call);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       Then I'm not sure the current patch is a net win. I guess `UnsupportedOperationException` can be thrown for lots of reasons, and producing `cannotCastToType` can be misleading.
   
   It did not expect adding a cause would require that level of refactoring, however, I agree something has to be done with `ExInst` and `ExInstWithCause`.
   
   For instance: `ExInst withCause(Throwable)` could be added to `ExInst`.
   An alternative option is probably to remove `ExInstWithCause` altogether.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 commented on pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#issuecomment-766099481


   I sent a new pull request at #2326 for this. Should I close this pull request?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r553092255



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       The doc string of `SqlValidatorException` [link](https://github.com/apache/calcite/blob/4d413bb21fb0a18882c5066f1d75a02d5b021bac/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorException.java#L36) says that it is a checked exception. I'm guessing Julian is saying that wrapping `UnsupportedOperationException`, a runtime exception, in a checked exception does not make sense?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r563743974



##########
File path: core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
##########
@@ -1212,17 +1212,15 @@ public void _testLikeAndSimilarFails() {
   }
 
   @Test void testCastRegisteredType() {
-    expr("cast(123 as customBigInt)")
-        .fails("class org.apache.calcite.sql.SqlIdentifier: CUSTOMBIGINT");
+    wholeExpr("cast(123 as customBigInt)")
+        .fails("Cast function does not support casting to type `CUSTOMBIGINT`");
     expr("cast(123 as sales.customBigInt)")

Review comment:
       Thanks for your review Danny. Based on the discussion in CALCITE-4265, I am not planning to move forward with this PR. Instead, I created a simpler PR at https://github.com/apache/calcite/pull/2326. Can you review that one instead? Thanks!




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] vlsi commented on pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
vlsi commented on pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#issuecomment-755773294


   Why not?
   
   >
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r557019660



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));
+    }
+  }
+
+  @Override public RelDataType deriveType(final SqlValidator validator,
+      final SqlValidatorScope scope, final SqlCall call) {
+    try {
+      return super.deriveType(validator, scope, call);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       I sent a new pull request at https://github.com/apache/calcite/pull/2326 for this. Should I close this pull request?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r552327648



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java
##########
@@ -93,6 +95,29 @@ public SqlCastFunction() {
 
   //~ Methods ----------------------------------------------------------------
 
+  @Override public void validateCall(final SqlCall call, final SqlValidator validator,
+      final SqlValidatorScope scope,
+      final SqlValidatorScope operandScope) {
+    try {
+      super.validateCall(call, validator, scope, operandScope);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));
+    }
+  }
+
+  @Override public RelDataType deriveType(final SqlValidator validator,
+      final SqlValidatorScope scope, final SqlCall call) {
+    try {
+      return super.deriveType(validator, scope, call);
+    } catch (UnsupportedOperationException ex) {
+      assert call.operandCount() == 2;
+      throw validator.newValidationError(call,
+          RESOURCE.cannotCastToType(call.operand(1).toString()));

Review comment:
       You are right, I think it should. 
   
   However, I found that incorporating the cause will make this a much larger change than it is right now. Currently, `SqlValidator` interface only has a `newValidationError` method that takes an `ExInst`, which represents an exception without cause. We can add a new method `newValidationErrorWithCause` to the `SqlValidator` interface. There will need to be some refactoring for `SqlUtil.newContextException` as well. Note that hacking this by adding `initCause` to the resulting exception returned from `newValidationError` does not work because the exception has already been initialized with a cause during construction.
   
   Overall, I think this can be split into a refactoring commit + this commit. I think the combined LOC will be  >100. Not sure if the extra code added and refactoring is worth the benefit: on one hand, having the cause provides better context. On the other hand, the cast function is not that complicated `CAST(value as type)` and the current exception message might be enough for debugging.
   
   What do you think?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] xwkuang5 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
xwkuang5 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r563743974



##########
File path: core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
##########
@@ -1212,17 +1212,15 @@ public void _testLikeAndSimilarFails() {
   }
 
   @Test void testCastRegisteredType() {
-    expr("cast(123 as customBigInt)")
-        .fails("class org.apache.calcite.sql.SqlIdentifier: CUSTOMBIGINT");
+    wholeExpr("cast(123 as customBigInt)")
+        .fails("Cast function does not support casting to type `CUSTOMBIGINT`");
     expr("cast(123 as sales.customBigInt)")

Review comment:
       Thanks for your review Danny. Based on the discussion in CALCITE-4265, I am not planning to move forward with this PR. Instead, I created a simpler PR at https://github.com/apache/calcite/pull/2326. Can you review that one instead? Thanks!




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [calcite] danny0405 commented on a change in pull request #2312: [CALCITE-4265] Improve error message when CAST to unknown type (Louis…

Posted by GitBox <gi...@apache.org>.
danny0405 commented on a change in pull request #2312:
URL: https://github.com/apache/calcite/pull/2312#discussion_r563433210



##########
File path: core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
##########
@@ -1212,17 +1212,15 @@ public void _testLikeAndSimilarFails() {
   }
 
   @Test void testCastRegisteredType() {
-    expr("cast(123 as customBigInt)")
-        .fails("class org.apache.calcite.sql.SqlIdentifier: CUSTOMBIGINT");
+    wholeExpr("cast(123 as customBigInt)")
+        .fails("Cast function does not support casting to type `CUSTOMBIGINT`");
     expr("cast(123 as sales.customBigInt)")

Review comment:
       Instead of message `Cast function does not support casting to type`, i guess `Cast from {fromType} to {toType} is not supported` is more clear ?




----------------------------------------------------------------
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:
users@infra.apache.org