github-actions[bot] commented on code in PR #65703:
URL: https://github.com/apache/doris/pull/65703#discussion_r3594262435


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/eageraggregation/EagerAggRewriter.java:
##########
@@ -156,10 +156,15 @@ public Plan visitLogicalJoin(LogicalJoin<? extends Plan, 
? extends Plan> join, P
 
         Plan newLeft = join.left();
         Plan newRight = join.right();
-        if (leftChildContext.isPresent()) {
+        // Scalar (GROUP BY empty) child aggregate: returns 1 NULL row even on 
0 input rows (SQL standard).
+        // If pushed below a join, that 1 row would cross-join with the other 
side, producing phantom rows.
+        // Forbid pushdown when the child aggregate has no GROUP BY keys.
+        // Note: parent scalar aggregates are already filtered at 
PushDownAggregation before reaching here.
+        // See regression test: scalar_agg_pushdown.groovy
+        if (leftChildContext.isPresent() && 
!leftChildContext.get().getGroupKeys().isEmpty()) {

Review Comment:
   This guard is evaluated before later context rewrites, so it does not 
prevent every scalar partial aggregate. A reachable reduced tree after constant 
propagation is:
   
   ```text
   Aggregate(group=[k], sum(z))
     CrossJoin
       Project(1 AS k, A.v + B.v AS z)
         InnerJoin(A.id = B.id)
           Scan A
           Scan B
       Scan R
   ```
   
   `ConstantPropagation` intentionally retains `k` when it is the only constant 
group key, so this check passes. `createContextFromProject` then maps `k` to 
the literal's empty input-slot set and `sum(z)` to `sum(A.v + B.v)`. That 
aggregate spans both inner-join children, so `visitLogicalJoin` falls back to 
`genAggregate` with `groupKeys=[]`. If the inner join is empty, the scalar 
partial emits one row and the outer cross join produces a phantom group, 
whereas the original tree produces no rows.
   
   Please reject an empty key list at `genAggregate` (or another actual 
aggregate-creation boundary). That placement also preserves safe paths where 
`visitLogicalFilter` adds keys after this point; this early return currently 
disables those valid reductions.



##########
regression-test/suites/query_p0/eager_agg/scalar_agg_pushdown.groovy:
##########
@@ -0,0 +1,54 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("scalar_agg_pushdown") {
+    sql "set eager_aggregation_mode=1;"
+    sql "set eager_aggregation_on_join=true;"
+    sql "set disable_nereids_rules='SALT_JOIN';"
+
+    // Test: scalar aggregation (no GROUP BY) must not be pushed below a join,
+    // because scalar agg on empty input returns 1 NULL row (SQL standard),
+    // which would be cross-joined with the other side to produce phantom rows.
+    // Regression test for: scalar agg pushdown producing phantom rows when
+    // constant propagation replaces GROUP BY key with a literal that doesn't
+    // exist in the table.
+
+    sql """drop table if exists t1;"""
+    sql """create table t1 (a int, b int) distributed by hash(a) 
properties("replication_num" = "1");"""
+    sql """insert into t1 values (1,10),(2,20);"""
+
+    sql """drop table if exists t2;"""
+    sql """create table t2 (c int) distributed by hash(c) 
properties("replication_num" = "1");"""
+    sql """insert into t2 values (1),(2);"""
+
+    // WHERE t1.a = 999 matches no rows in t1.
+    // GROUP BY includes t1.a, t2.c (cross-table GROUP BY).
+    // Without the fix, eager aggregation pushes a scalar partial aggregate
+    // below the join, which on empty input returns 1 NULL row.
+    // That NULL row cross-joins with t2's 2 rows, producing 2 phantom rows.
+    // With the fix, pushdown is forbidden for scalar child aggregates.
+    qt_scalar_agg_no_pushdown """
+        SELECT t1.a, SUM(t1.b), t2.c
+        FROM t1, t2
+        WHERE t1.a = 999
+        GROUP BY t1.a, t2.c
+        ORDER BY t2.c;
+    """
+
+    sql """drop table if exists t1;"""

Review Comment:
   Please remove these trailing drops. The repository test convention is to 
drop tables before setup and leave them in place afterward so a failed run can 
be inspected; lines 30 and 34 already provide the required cleanup.



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