mihaibudiu commented on code in PR #4636:
URL: https://github.com/apache/calcite/pull/4636#discussion_r2543852261


##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java:
##########
@@ -425,39 +426,118 @@ private static RelBuilder 
convertSingletonDistinct(RelBuilder relBuilder,
     return relBuilder;
   }
 
+  /**
+   * Rewrite aggregates that use GROUPING SETS. The following SQL/plan example
+   * serves as the concrete blueprint, starting from the original statement and
+   * plan-before outputs and then rebuilding the plan-after tree from the 
bottom
+   * (line 7) back to the top (line 1):
+   *
+  * <p>Original SQL:
+  * <pre>{@code
+  * SELECT deptno, COUNT(DISTINCT sal)
+  * FROM emp
+  * GROUP BY ROLLUP(deptno)
+  * }</pre>
+   *
+  * <p>Plan before rewrite:
+  * <pre>{@code
+  * LogicalAggregate(group=[{0}], groups=[[{0}, {}]], EXPR$1=[COUNT(DISTINCT 
$1)])
+  *   LogicalProject(DEPTNO=[$7], SAL=[$5])
+  *     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+  * }</pre>
+   *
+  * <p>Plan after rewrite (lines referenced below):
+  * <pre>{@code
+  * 1 LogicalProject(DEPTNO=[$0],
+  *     EXPR$1=[CAST(CASE(=($5, 0), $1, =($5, 1), $2,
+  *         null:BIGINT)):BIGINT NOT NULL])
+  * 2  LogicalFilter(condition=[OR(
+  *     AND(=($5, 0), >($3, 0)), =($5, 1))])
+  * 3    LogicalAggregate(group=[{0}], groups=[[{0}, {}]],
+  *      EXPR$1_g0=[COUNT($1) FILTER $2],
+  *      EXPR$1_g1=[COUNT($1) FILTER $4],
+  *      $g_present_0=[COUNT() FILTER $3],
+  *      $g_present_1=[COUNT() FILTER $5],
+  *      $g_final=[GROUPING($0)])
+  * 4      LogicalProject(DEPTNO=[$0], SAL=[$1],

Review Comment:
   you can have very simple plans if they can be wrong
   correctness should come first
   



-- 
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]

Reply via email to