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

   ### What problem does this PR solve?
   
   Fix mv rewrite failed when mv is rewritten by LimitAggToTopNAgg but query is 
not
   such as query and mv def sql is as fllowing:
   
   ```sql
   SELECT
                 c_name,
                 c_custkey,
                 o_orderkey,
                 o_orderdate,
                 o_totalprice,
                 sum(l_quantity)
               FROM
                 customer,
                 orders,
                 lineitem
               WHERE
                 o_orderkey IN (
                   SELECT l_orderkey
                   FROM
                     lineitem
                   GROUP BY
                     l_orderkey
                   HAVING
                     sum(l_quantity) > 300
                 )
                 AND c_custkey = o_custkey
                 AND o_orderkey = l_orderkey
               GROUP BY
                 c_name,
                 c_custkey,
                 o_orderkey,
                 o_orderdate,
                 o_totalprice
               ORDER BY
                 o_totalprice DESC,
                 o_orderdate
               LIMIT 100
   ```
   
   if query plan is rewrtten by rule `LimitAggToTopNAgg`, plan would be like as 
following, tonN contains all goup by key
   ```sql
    LogicalResultSink[812] ( outputExprs=[c_name#1, c_custkey#0, o_orderkey#8, 
o_orderdate#12, o_totalprice#11, __sum_5#50] )                                  
                                                                                
                                                                                
                                                      
   **| +--LogicalTopN ( limit=100, offset=0, orderKeys=[o_totalprice#11 desc, 
o_orderdate#12 asc null first, c_name#1 asc, c_custkey#0 asc, o_orderkey#8 asc] 
)**                                                                             
                                                                                
                                                            
   
   ```
   
   if mv plan is not rewrtten by rule `LimitAggToTopNAgg`, plan would be like 
as following, tonN not contain all goup by key
   ```sql
   LogicalResultSink[801] ( outputExprs=[c_name#1, c_custkey#0, o_orderkey#8, 
o_orderdate#12, o_totalprice#11, __sum_5#50] )
       **+--LogicalTopN ( limit=100, offset=0, orderKeys=[o_totalprice#11 desc, 
o_orderdate#12 asc null first] )**
   ```
   
   this would cause 
`doris/regression-test/suites/nereids_rules_p0/mv/tpch/mv_tpch_test.groovy` 
mv18 rewrite fail, the pr fix this
   
   
   Issue Number: close #xxx
   
   Related PR: #xxx
   
   Problem Summary:
   
   ### 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.
           - [x] 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