Skip to content

Commit 2a0ff84

Browse files
committed
Fix NPE due to NULL default column values
1 parent 2ae8d98 commit 2a0ff84

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3934,6 +3934,9 @@ private static void checkDefaultColumnValue(Session session, PlannerContext plan
39343934
if (type instanceof BooleanType) {
39353935
checkArgument(value instanceof Boolean, "Value must be true or false '%s'".formatted(value));
39363936
}
3937+
if (value == null) {
3938+
return;
3939+
}
39373940
if (type instanceof CharType charType) {
39383941
int targetLength = charType.getLength();
39393942
Slice slice = (Slice) value;

core/trino-main/src/test/java/io/trino/util/TestColumnDefaultOptions.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ public TestColumnDefaultOptions()
6262
plannerContext = plannerContextBuilder().build();
6363
}
6464

65-
@Test
66-
void testNull()
67-
{
68-
assertDefaultColumnValue(TINYINT, new NullLiteral(LOCATION));
69-
}
70-
7165
@Test
7266
void testBoolean()
7367
{
@@ -84,6 +78,7 @@ void testTinyint()
8478
{
8579
assertDefaultColumnValue(TINYINT, new LongLiteral(LOCATION, "-128"));
8680
assertDefaultColumnValue(TINYINT, new LongLiteral(LOCATION, "127"));
81+
assertDefaultColumnValue(TINYINT, new NullLiteral(LOCATION));
8782

8883
assertInvalidDefaultColumnValue(TINYINT, new LongLiteral(LOCATION, "-129"), "Out of range for tinyint: -129");
8984
assertInvalidDefaultColumnValue(TINYINT, new LongLiteral(LOCATION, "128"), "Out of range for tinyint: 128");
@@ -94,6 +89,7 @@ void testSmallint()
9489
{
9590
assertDefaultColumnValue(SMALLINT, new LongLiteral(LOCATION, "-32768"));
9691
assertDefaultColumnValue(SMALLINT, new LongLiteral(LOCATION, "32767"));
92+
assertDefaultColumnValue(SMALLINT, new NullLiteral(LOCATION));
9793

9894
assertInvalidDefaultColumnValue(SMALLINT, new LongLiteral(LOCATION, "-32769"), " Out of range for smallint: -32769");
9995
assertInvalidDefaultColumnValue(SMALLINT, new LongLiteral(LOCATION, "32768"), " Out of range for smallint: 32768");
@@ -104,6 +100,7 @@ void testInteger()
104100
{
105101
assertDefaultColumnValue(INTEGER, new LongLiteral(LOCATION, "-2147483648"));
106102
assertDefaultColumnValue(INTEGER, new LongLiteral(LOCATION, "2147483647"));
103+
assertDefaultColumnValue(INTEGER, new NullLiteral(LOCATION));
107104

108105
assertInvalidDefaultColumnValue(INTEGER, new LongLiteral(LOCATION, "-2147483649"), "Out of range for integer: -2147483649");
109106
assertInvalidDefaultColumnValue(INTEGER, new LongLiteral(LOCATION, "2147483648"), "Out of range for integer: 2147483648");
@@ -114,6 +111,7 @@ void testBigint()
114111
{
115112
assertDefaultColumnValue(BIGINT, new LongLiteral(LOCATION, "-9223372036854775808"));
116113
assertDefaultColumnValue(BIGINT, new LongLiteral(LOCATION, "9223372036854775807"));
114+
assertDefaultColumnValue(BIGINT, new NullLiteral(LOCATION));
117115

118116
// LongLiteral disallows values outside the range of a long
119117
}
@@ -128,6 +126,7 @@ void testReal()
128126
assertDefaultColumnValue(REAL, new GenericLiteral(LOCATION, "REAL", "NaN"));
129127
assertDefaultColumnValue(REAL, new GenericLiteral(LOCATION, "REAL", "+Infinity"));
130128
assertDefaultColumnValue(REAL, new GenericLiteral(LOCATION, "REAL", "-Infinity"));
129+
assertDefaultColumnValue(REAL, new NullLiteral(LOCATION));
131130
}
132131

133132
@Test
@@ -141,6 +140,7 @@ void testDouble()
141140
assertDefaultColumnValue(DOUBLE, new GenericLiteral(LOCATION, "DOUBLE", "NaN"));
142141
assertDefaultColumnValue(DOUBLE, new GenericLiteral(LOCATION, "DOUBLE", "+Infinity"));
143142
assertDefaultColumnValue(DOUBLE, new GenericLiteral(LOCATION, "DOUBLE", "-Infinity"));
143+
assertDefaultColumnValue(DOUBLE, new NullLiteral(LOCATION));
144144
}
145145

146146
@Test
@@ -154,6 +154,8 @@ void testDecimal()
154154
assertDefaultColumnValue(createDecimalType(30, 5), new DecimalLiteral(LOCATION, "-3141592653589793238462643.38327"));
155155
assertDefaultColumnValue(createDecimalType(38), new DecimalLiteral(LOCATION, "27182818284590452353602874713526624977"));
156156
assertDefaultColumnValue(createDecimalType(38), new DecimalLiteral(LOCATION, "-27182818284590452353602874713526624977"));
157+
assertDefaultColumnValue(createDecimalType(3), new NullLiteral(LOCATION));
158+
assertDefaultColumnValue(createDecimalType(38), new NullLiteral(LOCATION));
157159
}
158160

159161
@Test
@@ -177,6 +179,7 @@ void testChar()
177179
assertDefaultColumnValue(createCharType(10), new StringLiteral(LOCATION, "test"));
178180
assertDefaultColumnValue(createCharType(5), new StringLiteral(LOCATION, "攻殻機動隊"));
179181
assertDefaultColumnValue(createCharType(1), new StringLiteral(LOCATION, "😂"));
182+
assertDefaultColumnValue(createCharType(3), new NullLiteral(LOCATION));
180183
}
181184

182185
@Test
@@ -200,6 +203,8 @@ void testVarchar()
200203
assertDefaultColumnValue(VARCHAR, new StringLiteral(LOCATION, "test"));
201204
assertDefaultColumnValue(createVarcharType(5), new StringLiteral(LOCATION, "攻殻機動隊"));
202205
assertDefaultColumnValue(createVarcharType(1), new StringLiteral(LOCATION, "😂"));
206+
assertDefaultColumnValue(VARCHAR, new NullLiteral(LOCATION));
207+
assertDefaultColumnValue(createVarcharType(255), new NullLiteral(LOCATION));
203208
}
204209

205210
@Test
@@ -233,6 +238,9 @@ void testTime()
233238
assertDefaultColumnValue(createTimeType(10), new GenericLiteral(LOCATION, "TIME", "00:00:00.1234567890"));
234239
assertDefaultColumnValue(createTimeType(11), new GenericLiteral(LOCATION, "TIME", "00:00:00.12345678901"));
235240
assertDefaultColumnValue(createTimeType(12), new GenericLiteral(LOCATION, "TIME", "00:00:00.123456789012"));
241+
242+
assertDefaultColumnValue(createTimeType(0), new NullLiteral(LOCATION));
243+
assertDefaultColumnValue(createTimeType(12), new NullLiteral(LOCATION));
236244
}
237245

238246
@Test
@@ -242,6 +250,7 @@ void testDate()
242250
assertDefaultColumnValue(DATE, new GenericLiteral(LOCATION, "DATE", "1969-12-31"));
243251
assertDefaultColumnValue(DATE, new GenericLiteral(LOCATION, "DATE", "1970-01-01"));
244252
assertDefaultColumnValue(DATE, new GenericLiteral(LOCATION, "DATE", "9999-12-31"));
253+
assertDefaultColumnValue(DATE, new NullLiteral(LOCATION));
245254
}
246255

247256
@Test
@@ -269,6 +278,9 @@ void testTimestamp()
269278
assertDefaultColumnValue(createTimestampType(12), new GenericLiteral(LOCATION, "TIMESTAMP", "1970-01-01 00:00:00.9999"));
270279
assertDefaultColumnValue(createTimestampType(12), new GenericLiteral(LOCATION, "TIMESTAMP", "1970-01-01 00:00:00.99999"));
271280
assertDefaultColumnValue(createTimestampType(12), new GenericLiteral(LOCATION, "TIMESTAMP", "1970-01-01 00:00:00.999999"));
281+
282+
assertDefaultColumnValue(createTimestampType(0), new NullLiteral(LOCATION));
283+
assertDefaultColumnValue(createTimestampType(12), new NullLiteral(LOCATION));
272284
}
273285

274286
@Test
@@ -302,6 +314,9 @@ void testTimestampWithTimeZone()
302314
assertDefaultColumnValue(createTimestampWithTimeZoneType(12), new GenericLiteral(LOCATION, "TIMESTAMP", "1970-01-01 00:00:00.9 UTC"));
303315
assertDefaultColumnValue(createTimestampWithTimeZoneType(12), new GenericLiteral(LOCATION, "TIMESTAMP", "1970-01-01 00:00:00.99 UTC"));
304316
assertDefaultColumnValue(createTimestampWithTimeZoneType(12), new GenericLiteral(LOCATION, "TIMESTAMP", "1970-01-01 00:00:00.999 UTC"));
317+
318+
assertDefaultColumnValue(createTimestampWithTimeZoneType(0), new NullLiteral(LOCATION));
319+
assertDefaultColumnValue(createTimestampWithTimeZoneType(12), new NullLiteral(LOCATION));
305320
}
306321

307322
@Test

0 commit comments

Comments
 (0)