This is an automated email from the ASF dual-hosted git repository.

hongze pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new a8f4623  [CALCITE-3045] NullPointerException when casting null literal 
to composite user defined type
a8f4623 is described below

commit a8f46239509f01162c4e3ecf640a59cf0e0dfcad
Author: Hongze Zhang <[email protected]>
AuthorDate: Wed May 1 20:57:16 2019 +0800

    [CALCITE-3045] NullPointerException when casting null literal to composite 
user defined type
---
 .../org/apache/calcite/sql2rel/StandardConvertletTable.java  | 12 ++++++------
 core/src/test/java/org/apache/calcite/test/UdtTest.java      |  9 +++++++++
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java 
b/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java
index aa0db73..f79e247 100644
--- a/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java
+++ b/core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java
@@ -527,18 +527,18 @@ public class StandardConvertletTable extends 
ReflectiveConvertletTable {
     }
     SqlDataTypeSpec dataType = (SqlDataTypeSpec) right;
     RelDataType type = dataType.deriveType(typeFactory);
-    if (SqlUtil.isNullLiteral(left, false)) {
-      final SqlValidatorImpl validator = (SqlValidatorImpl) cx.getValidator();
-      validator.setValidatedNodeType(left, type);
-      return cx.convertExpression(left);
-    }
-    RexNode arg = cx.convertExpression(left);
     if (type == null) {
       type = cx.getValidator().getValidatedNodeType(dataType.getTypeName());
     }
+    RexNode arg = cx.convertExpression(left);
     if (arg.getType().isNullable()) {
       type = typeFactory.createTypeWithNullability(type, true);
     }
+    if (SqlUtil.isNullLiteral(left, false)) {
+      final SqlValidatorImpl validator = (SqlValidatorImpl) cx.getValidator();
+      validator.setValidatedNodeType(left, type);
+      return cx.convertExpression(left);
+    }
     if (null != dataType.getCollectionsTypeName()) {
       final RelDataType argComponentType =
           arg.getType().getComponentType();
diff --git a/core/src/test/java/org/apache/calcite/test/UdtTest.java 
b/core/src/test/java/org/apache/calcite/test/UdtTest.java
index 8741844..127eb3d 100644
--- a/core/src/test/java/org/apache/calcite/test/UdtTest.java
+++ b/core/src/test/java/org/apache/calcite/test/UdtTest.java
@@ -58,6 +58,15 @@ public class UdtTest {
         + "from (VALUES ROW(1, 'SameName')) AS \"t\" (\"id\", \"desc\")";
     withUdt().query(sql).returns("LD=1\n");
   }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-3045";>[CALCITE-3045]
+   * NullPointerException when casting null literal to composite user defined 
type</a>. */
+  @Test public void testCastNullLiteralToCompositeUdt() {
+    final String sql = "select CAST(null AS \"adhoc\".mytype2) as c "
+        + "from (VALUES (1))";
+    withUdt().query(sql).returns("C=null\n");
+  }
 }
 
 // End UdtTest.java

Reply via email to