diff --git a/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java b/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java index 7b92872ee6f9..f5268bd8fbec 100644 --- a/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java +++ b/extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/sql/resources/SqlStatementResource.java @@ -536,8 +536,8 @@ private Optional getSampleResults( rows = 0L; size = 0L; for (PageInformation pageInformation : pageList.get()) { - rows += pageInformation.getNumRows(); - size += pageInformation.getSizeInBytes(); + rows += pageInformation.getNumRows() != null ? pageInformation.getNumRows() : 0L; + size += pageInformation.getSizeInBytes() != null ? pageInformation.getSizeInBytes() : 0L; } } diff --git a/extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/sql/SqlMSQStatementResourcePostTest.java b/extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/sql/SqlMSQStatementResourcePostTest.java index 70ac5386ba6c..4f96b132ef07 100644 --- a/extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/sql/SqlMSQStatementResourcePostTest.java +++ b/extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/sql/SqlMSQStatementResourcePostTest.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.druid.common.config.NullHandling; import org.apache.druid.error.DruidException; import org.apache.druid.java.util.common.StringUtils; import org.apache.druid.msq.indexing.MSQControllerTask; @@ -336,6 +337,90 @@ public void testWithDurableStorage() throws IOException ))); } + @Test + public void testInsert() + { + Response response = resource.doPost(new SqlQuery( + "insert into foo1 select __time, dim1 , count(*) as cnt from foo where dim1 is not null group by 1, 2 PARTITIONED by day clustered by dim1", + null, + false, + false, + false, + defaultAsyncContext(), + null + ), SqlStatementResourceTest.makeOkRequest()); + Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + SqlStatementResult actual = (SqlStatementResult) response.getEntity(); + + + SqlStatementResult expected = new SqlStatementResult( + actual.getQueryId(), + SqlStatementState.SUCCESS, + MSQTestOverlordServiceClient.CREATED_TIME, + null, + MSQTestOverlordServiceClient.DURATION, + new ResultSetInformation(NullHandling.sqlCompatible() ? 6L : 5L, 0L, null, "foo1", null, null), + null + ); + Assert.assertEquals(expected, actual); + + Response getResponse = resource.doGetStatus(actual.getQueryId(), SqlStatementResourceTest.makeOkRequest()); + Assert.assertEquals(Response.Status.OK.getStatusCode(), getResponse.getStatus()); + Assert.assertEquals(expected, getResponse.getEntity()); + + Response resultsResponse = resource.doGetResults( + actual.getQueryId(), + null, + SqlStatementResourceTest.makeOkRequest() + ); + Assert.assertEquals(Response.Status.OK.getStatusCode(), resultsResponse.getStatus()); + Assert.assertNull(resultsResponse.getEntity()); + } + + + @Test + public void testReplaceAll() + { + Response response = resource.doPost(new SqlQuery( + "replace into foo1 overwrite all select __time, dim1 , count(*) as cnt from foo where dim1 is not null group by 1, 2 PARTITIONED by day clustered by dim1", + null, + false, + false, + false, + defaultAsyncContext(), + null + ), SqlStatementResourceTest.makeOkRequest()); + Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + SqlStatementResult actual = (SqlStatementResult) response.getEntity(); + + + SqlStatementResult expected = new SqlStatementResult( + actual.getQueryId(), + SqlStatementState.SUCCESS, + MSQTestOverlordServiceClient.CREATED_TIME, + null, + MSQTestOverlordServiceClient.DURATION, + new ResultSetInformation(NullHandling.sqlCompatible() ? 6L : 5L, 0L, null, "foo1", null, null), + null + ); + Assert.assertEquals(expected, actual); + + Response getResponse = resource.doGetStatus(actual.getQueryId(), SqlStatementResourceTest.makeOkRequest()); + Assert.assertEquals(Response.Status.OK.getStatusCode(), getResponse.getStatus()); + Assert.assertEquals(expected, getResponse.getEntity()); + + Response resultsResponse = resource.doGetResults( + actual.getQueryId(), + null, + SqlStatementResourceTest.makeOkRequest() + ); + Assert.assertEquals(Response.Status.OK.getStatusCode(), resultsResponse.getStatus()); + Assert.assertNull(resultsResponse.getEntity()); + } + + private static Map defaultAsyncContext() { Map context = new HashMap();