Skip to content

Conversation

@github-actions
Copy link
Contributor

Cherry-picked from #58974

…opNAgg but query is not (#58974)

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
@github-actions github-actions bot requested a review from yiguolei as a code owner December 16, 2025 02:31
@hello-stephen
Copy link
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@dataroaring dataroaring reopened this Dec 16, 2025
@hello-stephen
Copy link
Contributor

run buildall

@hello-stephen
Copy link
Contributor

FE UT Coverage Report

Increment line coverage 0.00% (0/2) 🎉
Increment coverage report
Complete coverage report

@github-actions github-actions bot added the approved Indicates a PR has been approved by one committer. label Dec 17, 2025
@github-actions
Copy link
Contributor Author

PR approved by at least one committer and no changes requested.

@github-actions
Copy link
Contributor Author

PR approved by anyone and no changes requested.

@yiguolei yiguolei merged commit c06d94f into branch-4.0 Dec 18, 2025
26 of 29 checks passed
@github-actions github-actions bot deleted the auto-pick-58974-branch-4.0 branch December 18, 2025 04:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants