Skip to content

Commit

Permalink
[CALCITE-6192] DEFAULT expression with NULL value throws unexpected e…
Browse files Browse the repository at this point in the history
…xception
  • Loading branch information
zstan authored and mihaibudiu committed Jan 9, 2024
1 parent ee4cb35 commit 3deb77c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -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()) {
Expand Down
45 changes: 45 additions & 0 deletions server/src/test/resources/sql/table.iq
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3deb77c

Please sign in to comment.