somu-imply commented on code in PR #13976:
URL: https://github.com/apache/druid/pull/13976#discussion_r1153451653


##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteArraysQueryTest.java:
##########
@@ -4168,4 +4169,118 @@ public void 
testUnnestWithMultipleOrFiltersOnUnnestedColumnsAndOnOriginalColumnD
         )
     );
   }
+
+  @Test
+  public void testUnnestWithCountOnColumn()
+  {
+    skipVectorize();
+    cannotVectorize();
+    testQuery(
+        "SELECT count(*) d3 FROM druid.numfoo, UNNEST(MV_TO_ARRAY(dim3)) as 
unnested (d3)",
+        QUERY_CONTEXT_UNNEST,
+        ImmutableList.of(
+            Druids.newTimeseriesQueryBuilder()
+                  .dataSource(UnnestDataSource.create(
+                      new TableDataSource(CalciteTests.DATASOURCE3),
+                      expressionVirtualColumn("j0.unnest", "\"dim3\"", 
ColumnType.STRING),
+                      null
+                  ))
+                  .intervals(querySegmentSpec(Filtration.eternity()))
+                  .context(QUERY_CONTEXT_UNNEST)
+                  .aggregators(aggregators(new CountAggregatorFactory("a0")))
+                  .build()
+        ),
+        ImmutableList.of(
+            new Object[]{8L}
+        )
+    );
+  }
+
+  @Test
+  public void testUnnestWithGroupByHavingSelector()
+  {
+    skipVectorize();
+    cannotVectorize();
+    testQuery(
+        "SELECT d3, COUNT(*) FROM druid.numfoo, UNNEST(MV_TO_ARRAY(dim3)) AS 
unnested(d3) GROUP BY d3 HAVING d3='b'",
+        QUERY_CONTEXT_UNNEST,
+        ImmutableList.of(
+            GroupByQuery.builder()
+                        .setDataSource(UnnestDataSource.create(
+                            new TableDataSource(CalciteTests.DATASOURCE3),
+                            expressionVirtualColumn("j0.unnest", "\"dim3\"", 
ColumnType.STRING),
+                            null
+                        ))
+                        .setInterval(querySegmentSpec(Filtration.eternity()))
+                        .setContext(QUERY_CONTEXT_UNNEST)
+                        .setDimensions(new DefaultDimensionSpec("j0.unnest", 
"_d0", ColumnType.STRING))
+                        .setGranularity(Granularities.ALL)
+                        .setDimFilter(selector("j0.unnest", "b", null))

Review Comment:
   Ideally Calcite should have pushed the filters on top of the unnestRel. I 
tried adding the rules to help this
   ```
         retVal.add(FilterMergeRule.INSTANCE);
         retVal.add(FilterAggregateTransposeRule.INSTANCE);
         retVal.add(FilterProjectTransposeRule.INSTANCE);
         retVal.add(FilterCorrelateRule.INSTANCE);
   ```
   With the debugger I can see Calcite merging the filters and passing them on 
top of uncollect. With this I see 2 things:
   1. We have a rule that pushes Filters on the left side of Correlate above 
Correlate and now the FilterCorrelate rule pushes the filter down, so that is a 
concern for two rules undoing the work done by the other. We need a cost-based 
approach to mitigate the situation.
   2. Multiple plans are still being generated and we are picking the plan with 
the dim filter on the outside which tells me that the cost computation needs 
some looking into. 
   
   Both of these can be done in a future PR to unblock this regression issue.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to