starocean999 opened a new pull request, #67362:
URL: https://github.com/apache/doris/pull/67362

   ### What problem does this PR solve?
   
   Issue Number: close #xxx
   
   Related PR: #xxx
   
   Problem Summary:
   Given a sync materialized view on `sync_tz_base`:
   
   ```sql
   CREATE MATERIALIZED VIEW sync_tz_day AS
   SELECT date_trunc(ts, 'day') AS day_ts, sum(v) AS day_sum
   FROM sync_tz_base WHERE ts IS NOT NULL
   GROUP BY date_trunc(ts, 'day');
   ```
   
   the following query fails during planning with an NPE:
   
   ```sql
   SELECT CAST(date_trunc(ts, 'day') AS STRING) AS day_ts, SUM(v)
   FROM sync_tz_base WHERE ts IS NOT NULL
   GROUP BY date_trunc(ts, 'day');
   ```
   
   ```
   java.lang.NullPointerException: Cannot invoke 
"org.apache.doris.analysis.Expr.getChildren()" because "root" is null
        at org.apache.doris.analysis.Expr.extractSlots(Expr.java:173)
        at 
org.apache.doris.nereids.glue.translator.PhysicalPlanTranslator.visitPhysicalProject(PhysicalPlanTranslator.java:2173)
   ```
   
   **Root cause**
   
   In `AbstractMaterializedViewAggregateRule.aggregateRewriteByView`, the group 
by keys of the rewritten aggregate were collected from the query **top plan** 
output expressions. For a `project -> aggregate` structure, 
`topPlanSplitToGroupAndFunction` classifies the derived projection 
`cast(date_trunc(ts, 'day') AS STRING)` as a group-by expression, because it is 
derived from the real group key. This derived projection was then wrongly added 
as a group by key of the rewritten aggregate:
   
   ```
   finalGroupExpressions = [cast(day_ts#7 as TEXT) AS #9, day_ts#7]
   finalOutputExpressions = [cast(day_ts#7 as TEXT) AS #9, sum(day_sum#8) AS 
#10]
   ```
   
   This led to two problems:
   
   1. A redundant group by key `cast(day_ts#7 as TEXT)` which is only a 
projection of the real group key `day_ts#7`.
   2. The real group key `day_ts#7` was added by the group-by compensation 
logic but was **not** present in the aggregate output expressions, so the top 
project referenced a slot that the physical aggregate never produced, causing 
the `"root" is null` NPE during physical plan translation.
   
   **Fix**
   
   The rewritten aggregate is now built directly from the query bottom 
aggregate:
   
   - The group by keys are the query bottom aggregate's group by expressions 
rewritten against the MV scan (always correct, so the previous group-by 
compensation is no longer needed).
   - The aggregate output contains the rewritten group keys and the rolled-up 
aggregate functions.
   - The query top plan output expressions — including derived projections of 
group keys such as `cast(date_trunc(ts, 'day') AS STRING)` — are recomputed by 
a `LogicalProject` above the rewritten aggregate when they cannot be produced 
by the aggregate directly.
   
   After the fix the rewritten plan is valid and the query returns correct 
results:
   
   ```
   Project [cast(day_ts#7 as TEXT) AS day_ts#3, sum(day_sum#8) AS SUM(v)#4]
     Aggregate [group by [day_ts#7], output [day_ts#7, sum(day_sum#8)]]
       MVScan(sync_tz_day)
   ```
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Test <!-- At least one of them must be included. -->
       - [ ] Regression test
       - [ ] Unit Test
       - [ ] Manual test (add detailed scripts or steps below)
       - [ ] No need to test or manual test. Explain why:
           - [ ] This is a refactor/code format and no logic has been changed.
           - [ ] Previous test can cover this change.
           - [ ] No code files have been changed.
           - [ ] Other reason <!-- Add your reason?  -->
   
   - Behavior changed:
       - [ ] No.
       - [ ] Yes. <!-- Explain the behavior change -->
   
   - Does this need documentation?
       - [ ] No.
       - [ ] Yes. <!-- Add document PR link here. eg: 
https://github.com/apache/doris-website/pull/1214 -->
   
   ### Check List (For Reviewer who merge this PR)
   
   - [ ] Confirm the release note
   - [ ] Confirm test cases
   - [ ] Confirm document
   - [ ] Add branch pick label <!-- Add branch pick label that this PR should 
merge into -->
   
   


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