Skip to content

Conversation

@github-actions
Copy link
Contributor

Cherry-picked from #57741

…sets (#57741)

Fix query rewrite by mv fail when both use cte and group sets

Such as mv def is as following:
```sql
            select l_shipdate, o_orderdate, l_partkey, l_suppkey,
            sum(o_totalprice) as sum_total,
            max(o_totalprice) as max_total,
            min(o_totalprice) as min_total,
            count(*) as count_all,
            bitmap_union(to_bitmap(case when o_shippriority > 1 and o_orderkey IN (1, 3) then o_custkey else null end)) as bitmap_union_basic
            from lineitem
            left join orders on lineitem.l_orderkey = orders.o_orderkey and l_shipdate = o_orderdate
            group by
            l_shipdate,
            o_orderdate,
            l_partkey,
            l_suppkey;
```

query is as fllowing, use both group sets and cte, would rewrite fail,
the pr fix this

```sql
            with t as (
                select t1.l_partkey, t1.l_suppkey, o_orderdate,
                grouping(t1.l_suppkey),
                grouping(o_orderdate),
                grouping_id(t1.l_partkey, t1.l_suppkey),
                grouping_id(t1.l_partkey, t1.l_suppkey, o_orderdate),
                sum(o_totalprice),
                max(o_totalprice),
                min(o_totalprice),
                count(*),
                count(distinct case when o_shippriority > 1 and o_orderkey IN (1, 3) then o_custkey else null end)
                from (select * from lineitem where l_shipdate = '2023-12-11') t1
                left join orders on t1.l_orderkey = orders.o_orderkey and t1.l_shipdate = o_orderdate
                group by
                GROUPING SETS ((l_shipdate, o_orderdate, l_partkey), (l_partkey, l_suppkey), (l_suppkey), ())
            )
            select t1.l_suppkey, t2.o_orderdate
            from
            t t1
            inner join
            t t2 on t1.l_partkey = t2.l_partkey;
```
@github-actions github-actions bot requested a review from yiguolei as a code owner December 12, 2025 05:59
@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 12, 2025
@hello-stephen
Copy link
Contributor

run buildall

@hello-stephen
Copy link
Contributor

FE UT Coverage Report

Increment line coverage 50.00% (10/20) 🎉
Increment coverage report
Complete coverage report

@github-actions
Copy link
Contributor Author

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

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

PR approved by anyone and no changes requested.

@yiguolei yiguolei merged commit 10666f2 into branch-4.0 Dec 17, 2025
24 of 27 checks passed
@github-actions github-actions bot deleted the auto-pick-57741-branch-4.0 branch December 17, 2025 06:58
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.

4 participants