You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by jo...@apache.org on 2019/12/18 19:48:29 UTC

[incubator-druid] branch 0.17.0-incubating updated: Fix NPE for subquery with limit (#8775) (#9060)

This is an automated email from the ASF dual-hosted git repository.

jonwei pushed a commit to branch 0.17.0-incubating
in repository https://gitbox.apache.org/repos/asf/incubator-druid.git


The following commit(s) were added to refs/heads/0.17.0-incubating by this push:
     new f8abdb4  Fix NPE for subquery with limit (#8775) (#9060)
f8abdb4 is described below

commit f8abdb45aa6e2ab7a8eb2987c8fac25b6024f58e
Author: Jonathan Wei <jo...@users.noreply.github.com>
AuthorDate: Wed Dec 18 11:47:57 2019 -0800

    Fix NPE for subquery with limit (#8775) (#9060)
    
    * Fix NPE for subquery with limit
    
    * Mark it as unplannable by returning null
    
    * Migrate testcases from SqlResourceTest to CalciteQueryTest
    
    * Throw CannotBuildQueryException
    
    * Fix typo
    
    * Patch comments
---
 .../druid/sql/calcite/rel/DruidOuterQueryRel.java  |  8 +++++-
 .../apache/druid/sql/calcite/CalciteQueryTest.java | 33 ++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidOuterQueryRel.java b/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidOuterQueryRel.java
index 9544cd0..a1b1910 100644
--- a/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidOuterQueryRel.java
+++ b/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidOuterQueryRel.java
@@ -36,6 +36,7 @@ import org.apache.druid.java.util.common.guava.Sequence;
 import org.apache.druid.java.util.common.guava.Sequences;
 import org.apache.druid.query.QueryDataSource;
 import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.groupby.GroupByQuery;
 import org.apache.druid.sql.calcite.table.RowSignature;
 
 import javax.annotation.Nullable;
@@ -128,9 +129,14 @@ public class DruidOuterQueryRel extends DruidRel<DruidOuterQueryRel>
       return null;
     }
 
+    final GroupByQuery groupByQuery = subQuery.toGroupByQuery();
+    if (groupByQuery == null) {
+      throw new CannotBuildQueryException("Subquery could not be converted to GroupBy query");
+    }
+
     final RowSignature sourceRowSignature = subQuery.getOutputRowSignature();
     return partialQuery.build(
-        new QueryDataSource(subQuery.toGroupByQuery()),
+        new QueryDataSource(groupByQuery),
         sourceRowSignature,
         getPlannerContext(),
         getCluster().getRexBuilder(),
diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java
index 3d721dc..65e2d0f 100644
--- a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java
+++ b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java
@@ -7777,6 +7777,39 @@ public class CalciteQueryTest extends BaseCalciteQueryTest
   }
 
   @Test
+  public void testUsingSubqueryWithLimit() throws Exception
+  {
+    expectedException.expect(CannotBuildQueryException.class);
+    expectedException.expectMessage("Subquery could not be converted to GroupBy query");
+
+    testQuery(
+        "SELECT COUNT(*) AS cnt FROM ( SELECT * FROM druid.foo LIMIT 10 ) tmpA",
+        ImmutableList.of(),
+        ImmutableList.of()
+    );
+  }
+
+  @Test
+  public void testUsingSubqueryWithoutLimit() throws Exception
+  {
+    testQuery(
+        "SELECT COUNT(*) AS cnt FROM ( SELECT * FROM druid.foo ) tmpA",
+        ImmutableList.of(
+            Druids.newTimeseriesQueryBuilder()
+                  .dataSource(CalciteTests.DATASOURCE1)
+                  .intervals(querySegmentSpec(Filtration.eternity()))
+                  .granularity(Granularities.ALL)
+                  .aggregators(aggregators(new CountAggregatorFactory("a0")))
+                  .context(TIMESERIES_CONTEXT_DEFAULT)
+                  .build()
+        ),
+        ImmutableList.of(
+            new Object[]{6L}
+        )
+    );
+  }
+
+  @Test
   public void testUnicodeFilterAndGroupBy() throws Exception
   {
     testQuery(


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org