gianm closed pull request #6769: SQL: Fix ordering of sort, sortProject in 
DruidSemiJoin.
URL: https://github.com/apache/incubator-druid/pull/6769
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidSemiJoin.java 
b/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidSemiJoin.java
index f5f023d1068..aff8434f12a 100644
--- a/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidSemiJoin.java
+++ b/sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidSemiJoin.java
@@ -375,14 +375,14 @@ public RelOptCost computeSelfCost(final RelOptPlanner 
planner, final RelMetadata
         newPartialQuery = 
newPartialQuery.withAggregateProject(leftPartialQuery.getAggregateProject());
       }
 
-      if (leftPartialQuery.getSortProject() != null) {
-        newPartialQuery = 
newPartialQuery.withSortProject(leftPartialQuery.getSortProject());
-      }
-
       if (leftPartialQuery.getSort() != null) {
         newPartialQuery = newPartialQuery.withSort(leftPartialQuery.getSort());
       }
 
+      if (leftPartialQuery.getSortProject() != null) {
+        newPartialQuery = 
newPartialQuery.withSortProject(leftPartialQuery.getSortProject());
+      }
+
       return left.withPartialQuery(newPartialQuery);
     } else {
       return null;
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 2bacbe08515..179157e7592 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
@@ -6768,7 +6768,7 @@ public void testUsingSubqueryAsFilterWithInnerSort() 
throws Exception
   }
 
   @Test
-  public void testSemiJoinWithOuterTimeExtract() throws Exception
+  public void testSemiJoinWithOuterTimeExtractScan() throws Exception
   {
     testQuery(
         "SELECT dim1, EXTRACT(MONTH FROM __time) FROM druid.foo\n"
@@ -6808,6 +6808,77 @@ public void testSemiJoinWithOuterTimeExtract() throws 
Exception
     );
   }
 
+  @Test
+  public void testSemiJoinWithOuterTimeExtractAggregateWithOrderBy() throws 
Exception
+  {
+    testQuery(
+        "SELECT COUNT(DISTINCT dim1), EXTRACT(MONTH FROM __time) FROM 
druid.foo\n"
+        + " WHERE dim2 IN (\n"
+        + "   SELECT dim2\n"
+        + "   FROM druid.foo\n"
+        + "   WHERE dim1 = 'def'\n"
+        + " ) AND dim1 <> ''"
+        + "GROUP BY EXTRACT(MONTH FROM __time)\n"
+        + "ORDER BY EXTRACT(MONTH FROM __time)",
+        ImmutableList.of(
+            GroupByQuery
+                .builder()
+                .setDataSource(CalciteTests.DATASOURCE1)
+                .setInterval(QSS(Filtration.eternity()))
+                .setGranularity(Granularities.ALL)
+                .setDimensions(DIMS(new DefaultDimensionSpec("dim2", "d0")))
+                .setDimFilter(SELECTOR("dim1", "def", null))
+                .setContext(QUERY_CONTEXT_DEFAULT)
+                .build(),
+            GroupByQuery
+                .builder()
+                .setDataSource(CalciteTests.DATASOURCE1)
+                .setVirtualColumns(
+                    EXPRESSION_VIRTUAL_COLUMN("d0:v", 
"timestamp_extract(\"__time\",'MONTH','UTC')", ValueType.LONG)
+                )
+                .setDimFilter(
+                    AND(
+                        NOT(SELECTOR("dim1", "", null)),
+                        SELECTOR("dim2", "abc", null)
+                    )
+                )
+                .setDimensions(DIMS(new DefaultDimensionSpec("d0:v", "d0", 
ValueType.LONG)))
+                .setInterval(QSS(Filtration.eternity()))
+                .setGranularity(Granularities.ALL)
+                .setAggregatorSpecs(
+                    AGGS(
+                        new CardinalityAggregatorFactory(
+                            "a0",
+                            null,
+                            ImmutableList.of(
+                                new DefaultDimensionSpec("dim1", "dim1", 
ValueType.STRING)
+                            ),
+                            false,
+                            true
+                        )
+                    )
+                )
+                .setLimitSpec(
+                    new DefaultLimitSpec(
+                        ImmutableList.of(
+                            new OrderByColumnSpec(
+                                "d0",
+                                OrderByColumnSpec.Direction.ASCENDING,
+                                StringComparators.NUMERIC
+                            )
+                        ),
+                        Integer.MAX_VALUE
+                    )
+                )
+                .setContext(QUERY_CONTEXT_DEFAULT)
+                .build()
+        ),
+        ImmutableList.of(
+            new Object[]{1L, 1L}
+        )
+    );
+  }
+
   @Test
   public void testUsingSubqueryWithExtractionFns() throws Exception
   {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to