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

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


The following commit(s) were added to refs/heads/main by this push:
     new 11c540686a [CALCITE-5130] AssertionError: "Conversion to relational 
algebra failed to preserve datatypes" when union VARCHAR literal and CAST(null 
AS INTEGER)
11c540686a is described below

commit 11c540686a47765f8c6232058ec9eb1f418e4efa
Author: Yingyu Wang <[email protected]>
AuthorDate: Wed May 11 11:28:22 2022 -0400

    [CALCITE-5130] AssertionError: "Conversion to relational algebra failed to 
preserve datatypes" when union VARCHAR literal and CAST(null AS INTEGER)
    
    In AbstractTypeCoercion#promoteToVarChar() return resultType with 
nullability
    if any of the types being promoted are nullable.
---
 .../calcite/sql/validate/implicit/AbstractTypeCoercion.java  |  6 ++++++
 .../org/apache/calcite/test/TypeCoercionConverterTest.java   |  8 ++++++++
 .../test/java/org/apache/calcite/test/TypeCoercionTest.java  |  2 +-
 .../org/apache/calcite/test/TypeCoercionConverterTest.xml    | 12 ++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
 
b/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
index 167d769f85..99465934b2 100644
--- 
a/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
+++ 
b/core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
@@ -476,6 +476,12 @@ public abstract class AbstractTypeCoercion implements 
TypeCoercion {
     if (SqlTypeUtil.isCharacter(type1) && SqlTypeUtil.isAtomic(type2)) {
       resultType = factory.createSqlType(SqlTypeName.VARCHAR);
     }
+
+    if (null != resultType) {
+      resultType = factory.createTypeWithNullability(resultType,
+          type1.isNullable() || type2.isNullable());
+    }
+
     return resultType;
   }
 
diff --git 
a/core/src/test/java/org/apache/calcite/test/TypeCoercionConverterTest.java 
b/core/src/test/java/org/apache/calcite/test/TypeCoercionConverterTest.java
index a0c33cde2b..b3514def3c 100644
--- a/core/src/test/java/org/apache/calcite/test/TypeCoercionConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/TypeCoercionConverterTest.java
@@ -179,4 +179,12 @@ class TypeCoercionConverterTest extends SqlToRelTestBase {
     sql(sql).ok();
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5130";>[CALCITE-5130]
+   * AssertionError: "Conversion to relational algebra failed to preserve 
datatypes"
+   * when union VARCHAR literal and CAST(null AS INTEGER) </a>. */
+  @Test void testCastNullAsIntUnionChar() {
+    String sql = "select CAST(null AS INTEGER) union select '10'";
+    sql(sql).ok();
+  }
 }
diff --git a/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java 
b/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java
index c53f4f4808..616658dee7 100644
--- a/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java
+++ b/core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java
@@ -216,7 +216,7 @@ class TypeCoercionTest {
     sql("select '1' from (values(true)) union values 2")
         .type("RecordType(VARCHAR NOT NULL EXPR$0) NOT NULL");
     sql("select (select 1+2 from (values true)) tt from (values(true)) union 
values '2'")
-        .type("RecordType(VARCHAR NOT NULL TT) NOT NULL");
+        .type("RecordType(VARCHAR TT) NOT NULL");
     // union with star
     sql("select * from (values(1, '3')) union select * from (values('2', 4))")
         .type("RecordType(VARCHAR NOT NULL EXPR$0, VARCHAR NOT NULL EXPR$1) 
NOT NULL");
diff --git 
a/core/src/test/resources/org/apache/calcite/test/TypeCoercionConverterTest.xml 
b/core/src/test/resources/org/apache/calcite/test/TypeCoercionConverterTest.xml
index f381bbb05e..310934bf6e 100644
--- 
a/core/src/test/resources/org/apache/calcite/test/TypeCoercionConverterTest.xml
+++ 
b/core/src/test/resources/org/apache/calcite/test/TypeCoercionConverterTest.xml
@@ -74,6 +74,18 @@ LogicalProject(EXPR$0=[||('1':VARCHAR, 'a')])
       <![CDATA[
 LogicalProject(EXPR$0=[CAST($3):DECIMAL(19, 0) NOT NULL])
   LogicalTableScan(table=[[CATALOG, SALES, T2]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testCastNullAsIntUnionChar">
+    <Resource name="sql">
+      <![CDATA[select CAST(null AS INTEGER) union select '10']]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[
+LogicalUnion(all=[false])
+  LogicalValues(tuples=[[{ null }]])
+  LogicalValues(tuples=[[{ '10' }]])
 ]]>
     </Resource>
   </TestCase>

Reply via email to