nsivarajan commented on code in PR #63974:
URL: https://github.com/apache/doris/pull/63974#discussion_r3598375409


##########
fe/fe-core/src/main/java/org/apache/doris/statistics/query/QueryStatsRecorder.java:
##########
@@ -254,153 +263,272 @@ private static void walkPlan(Plan plan,
             Aggregate<?> agg = (Aggregate<?>) plan;
             // GROUP BY keys
             for (Expression expr : agg.getGroupByExpressions()) {
-                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas, derivedSlotInputs);
             }
             // Columns consumed by aggregate functions (e.g. k2 in SUM(k2))
             for (NamedExpression expr : agg.getOutputExpressions()) {
-                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas, derivedSlotInputs);
+            }
+            // HAVING: link each non-scan-backed aggregate output to its own 
inputs, however
+            // computed those already are (e.g. HAVING SUM(x) where x is 
itself computed).
+            for (NamedExpression ne : agg.getOutputExpressions()) {
+                if (exprIdToScan.containsKey(ne.getExprId()) || ne instanceof 
Slot) {
+                    // Bare pass-through output (e.g. two-phase DISTINCT's 
merge phase reusing the
+                    // local phase's slot) — already linked there; 
getInputSlots() on a bare Slot
+                    // includes itself, which would self-loop here.
+                    continue;
+                }
+                Set<Slot> inputSlots = ne.getInputSlots();
+                if (!inputSlots.isEmpty()) {
+                    derivedSlotInputs.put(ne.getExprId(), inputSlots);
+                }
             }
         }
         if (plan instanceof AbstractPhysicalSort) {
             for (OrderKey orderKey : ((AbstractPhysicalSort<?>) 
plan).getOrderKeys()) {
-                recordInputSlotsAsQueryHit(orderKey.getExpr(), exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(orderKey.getExpr(), exprIdToScan, 
exprIdToColName, deltas,
+                        derivedSlotInputs);
             }
         }
         // PhysicalPartitionTopN does not extend AbstractPhysicalSort but also 
has ORDER BY and
         // partition keys (used for row_number() / rank() per partition).
         if (plan instanceof PhysicalPartitionTopN) {
             PhysicalPartitionTopN<?> ptn = (PhysicalPartitionTopN<?>) plan;
             for (Expression partKey : ptn.getPartitionKeys()) {
-                recordInputSlotsAsQueryHit(partKey, exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(partKey, exprIdToScan, 
exprIdToColName, deltas, derivedSlotInputs);
             }
             for (OrderKey orderKey : ptn.getOrderKeys()) {
-                recordInputSlotsAsQueryHit(orderKey.getExpr(), exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(orderKey.getExpr(), exprIdToScan, 
exprIdToColName, deltas,
+                        derivedSlotInputs);
             }
         }
         // PhysicalRepeat handles ROLLUP/CUBE: group sets are like GROUP BY 
keys.
         if (plan instanceof PhysicalRepeat) {
             PhysicalRepeat<?> repeat = (PhysicalRepeat<?>) plan;
             for (List<Expression> groupSet : repeat.getGroupingSets()) {
                 for (Expression expr : groupSet) {
-                    recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas);
+                    recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas, derivedSlotInputs);
                 }
             }
             for (NamedExpression expr : repeat.getOutputExpressions()) {
-                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas, derivedSlotInputs);
             }
         }
         if (plan instanceof PhysicalWindow) {
             WindowFrameGroup wfg = ((PhysicalWindow<?>) 
plan).getWindowFrameGroup();
             Set<Expression> partitionKeys = wfg.getPartitionKeys();
             for (Expression expr : partitionKeys) {
-                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(expr, exprIdToScan, 
exprIdToColName, deltas, derivedSlotInputs);
             }
             for (OrderExpression orderExpr : wfg.getOrderKeys()) {
-                recordInputSlotsAsQueryHit(orderExpr.child(), exprIdToScan, 
exprIdToColName, deltas);
+                recordInputSlotsAsQueryHit(orderExpr.child(), exprIdToScan, 
exprIdToColName, deltas,
+                        derivedSlotInputs);
             }
-            // queryHit for the window function value columns (e.g. k2 in 
SUM(k2) OVER (...)).
+            // queryHit for window value columns (e.g. k2 in SUM(k2) OVER 
(...)), and link the
+            // alias to those inputs for a QUALIFY-style filter above; 
positional functions
+            // like ROW_NUMBER have no column arguments, so nothing gets 
linked for those.
             for (NamedExpression windowAlias : wfg.getGroups()) {
                 Expression windowExpr = windowAlias.child(0);
                 if (windowExpr instanceof WindowExpression) {
-                    recordInputSlotsAsQueryHit(
-                            ((WindowExpression) windowExpr).getFunction(),
-                            exprIdToScan, exprIdToColName, deltas);
+                    Expression function = ((WindowExpression) 
windowExpr).getFunction();
+                    recordInputSlotsAsQueryHit(function, exprIdToScan, 
exprIdToColName, deltas, derivedSlotInputs);
+                    if (!exprIdToScan.containsKey(windowAlias.getExprId())) {
+                        Set<Slot> inputSlots = function.getInputSlots();

Review Comment:
   Fixed. Switched the link source from the window function's own inputs to the 
full window expression's inputs, so ROW_NUMBER/RANK/DENSE_RANK (zero function 
args) still capture PARTITION BY/ORDER BY dependencies



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