mrhhsg commented on code in PR #64293:
URL: https://github.com/apache/doris/pull/64293#discussion_r3380822871


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/CountLiteralRewrite.java:
##########
@@ -107,10 +114,29 @@ private boolean isCountLiteral(AggregateFunction aggFunc) 
{
                 && aggFunc.child(0).isLiteral();
     }
 
+    private boolean isCountConstantExpression(AggregateFunction aggFunc) {
+        if (aggFunc.isDistinct()
+                || !(aggFunc instanceof Count)
+                || aggFunc.children().size() != 1) {
+            return false;
+        }
+        Expression arg = aggFunc.child(0);
+        return !arg.isLiteral()
+                && arg.foldable()
+                && !arg.containsNondeterministic()
+                && !arg.containsVolatileExpression()
+                && arg.getInputSlots().isEmpty()
+                && !arg.containsType(AggregateFunction.class, 
SubqueryExpr.class,
+                        TableGeneratingFunction.class, WindowExpression.class);
+    }
+
     private Expression rewrite(Count count) {
         if (count.child(0).isNullLiteral()) {
             return new BigIntLiteral(0);

Review Comment:
   Thanks, addressed with a minimal targeted fix. I removed the broader 
`isSafeToEvaluateAboveAggregate` direction and kept the normal count-constant 
rewrite unchanged. For the empty-input case, `EliminateEmptyRelation` now 
recognizes the `CountLiteralRewrite`-generated `if(arg is null, 0, count(*))` 
projection above an empty aggregate and rewrites that result to literal `0`, so 
the original count argument is no longer evaluated when there are no input rows.
   
   I also added regression coverage for both sides:
   - empty table + invalid JSON path returns `0`
   - non-empty table + the same invalid JSON path still raises `Invalid Json 
Path`
   
   Validated with:
   - `./run-fe-ut.sh --run 
org.apache.doris.nereids.rules.rewrite.CountLiteralRewriteTest`
   - `doris-local-regression ... all -d nereids_rules_p0/count_constant_rewrite 
-s count_constant_rewrite -forceGenOut`
   - `doris-local-regression ... all -d nereids_rules_p0/count_constant_rewrite 
-s count_constant_rewrite`
   - `git diff --check`



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