diff --git a/sql/src/main/java/org/apache/druid/sql/http/SqlResource.java b/sql/src/main/java/org/apache/druid/sql/http/SqlResource.java index 03173eea3ba..94004ba24f9 100644 --- a/sql/src/main/java/org/apache/druid/sql/http/SqlResource.java +++ b/sql/src/main/java/org/apache/druid/sql/http/SqlResource.java @@ -134,7 +134,9 @@ public class SqlResource for (int i = 0; i < fieldList.size(); i++) { final Object value; - if (timeColumns[i]) { + if (row[i] == null) { + value = null; + } else if (timeColumns[i]) { value = ISODateTimeFormat.dateTime().print( Calcites.calciteTimestampToJoda((long) row[i], timeZone) ); diff --git a/sql/src/test/java/org/apache/druid/sql/http/SqlResourceTest.java b/sql/src/test/java/org/apache/druid/sql/http/SqlResourceTest.java index 123277e491e..07febe1be14 100644 --- a/sql/src/test/java/org/apache/druid/sql/http/SqlResourceTest.java +++ b/sql/src/test/java/org/apache/druid/sql/http/SqlResourceTest.java @@ -325,6 +325,31 @@ public class SqlResourceTest extends CalciteTestBase ); } + @Test + public void testTimestampsInResponseWithNulls() throws Exception + { + final List> rows = doPost( + new SqlQuery( + "SELECT MAX(__time) as t1, MAX(__time) FILTER(WHERE dim1 = 'non_existing') as t2 FROM druid.foo", + ResultFormat.OBJECT, + false, + null, + null + ) + ).rhs; + + Assert.assertEquals( + NullHandling.replaceWithDefault() ? + ImmutableList.of( + ImmutableMap.of("t1", "2001-01-03T00:00:00.000Z", "t2", "-292275055-05-16T16:47:04.192Z") // t2 represents Long.MIN converted to a timestamp + ) : + ImmutableList.of( + Maps.transformValues(ImmutableMap.of("t1", "2001-01-03T00:00:00.000Z", "t2", ""), (val) -> "".equals(val) ? null : val) + ), + rows + ); + } + @Test public void testFieldAliasingSelect() throws Exception {