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 3deb77cd6f [CALCITE-6192] DEFAULT expression with NULL value throws 
unexpected exception
3deb77cd6f is described below

commit 3deb77cd6faf811d8a3ca27d6379bd20afb721cf
Author: zstan <[email protected]>
AuthorDate: Mon Jan 8 18:48:45 2024 +0300

    [CALCITE-6192] DEFAULT expression with NULL value throws unexpected 
exception
---
 .../calcite/sql2rel/SqlNodeToRexConverterImpl.java | 13 ++++++-
 server/src/test/resources/sql/table.iq             | 45 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java 
b/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java
index eb8cee0cab..afdfe92fe9 100644
--- 
a/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java
+++ 
b/core/src/main/java/org/apache/calcite/sql2rel/SqlNodeToRexConverterImpl.java
@@ -26,6 +26,7 @@ import org.apache.calcite.sql.SqlIntervalQualifier;
 import org.apache.calcite.sql.SqlLiteral;
 import org.apache.calcite.sql.SqlTimeLiteral;
 import org.apache.calcite.sql.SqlTimestampLiteral;
+import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.util.BitString;
 import org.apache.calcite.util.DateString;
 import org.apache.calcite.util.NlsString;
@@ -77,8 +78,16 @@ public class SqlNodeToRexConverterImpl implements 
SqlNodeToRexConverter {
       SqlLiteral literal) {
     final RexBuilder rexBuilder = cx.getRexBuilder();
     if (literal.getValue() == null) {
-      RelDataType type = cx.getValidator().getValidatedNodeType(literal);
-      return rexBuilder.makeNullLiteral(type);
+      if (literal.getTypeName() == SqlTypeName.NULL) {
+        RelDataType type = 
cx.getValidator().getValidatedNodeTypeIfKnown(literal);
+        if (type == null) {
+          type = rexBuilder.getTypeFactory().createSqlType(SqlTypeName.NULL);
+        }
+        return rexBuilder.makeNullLiteral(type);
+      } else {
+        RelDataType type = cx.getValidator().getValidatedNodeType(literal);
+        return rexBuilder.makeNullLiteral(type);
+      }
     }
 
     switch (literal.getTypeName()) {
diff --git a/server/src/test/resources/sql/table.iq 
b/server/src/test/resources/sql/table.iq
index 8726b2521d..b40f7b0be3 100755
--- a/server/src/test/resources/sql/table.iq
+++ b/server/src/test/resources/sql/table.iq
@@ -419,4 +419,49 @@ select * from tdef order by i;
 
 !ok
 
+# Create a basic table with DEFAULT constraint column
+create table tdef1 (i int not null, col1 int default null, col2 varchar 
default null);
+(0 rows modified)
+
+!update
+
+insert into tdef1(i, col1) values (1, DEFAULT);
+(1 row modified)
+
+!update
+
+insert into tdef1(i, col1, col2) values (2, DEFAULT, DEFAULT);
+(1 row modified)
+
+!update
+
+insert into tdef1(i, col1, col2) values (3, 100, DEFAULT);
+(1 row modified)
+
+!update
+
+insert into tdef1(i, col1, col2) values (4, DEFAULT, 100);
+(1 row modified)
+
+!update
+
+insert into tdef1(i) values (5);
+(1 row modified)
+
+!update
+
+select * from tdef1 order by i;
++---+------+------+
+| I | COL1 | COL2 |
++---+------+------+
+| 1 |      |      |
+| 2 |      |      |
+| 3 |  100 |      |
+| 4 |      | 100  |
+| 5 |      |      |
++---+------+------+
+(5 rows)
+
+!ok
+
 # End table.iq

Reply via email to