Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat](mtmv) Support grouping_sets rewrite when query rewrite by materialized view (#36056) #37436

Merged

Conversation

seawinde
Copy link
Contributor

@seawinde seawinde commented Jul 8, 2024

Proposed changes

cherry pick to 2.1
pr: #36056
commitId: 569c977

…rialized view (apache#36056)

Support grouping_sets, cube, rollup query rewrite by materialized view,
if mv group by fields contains all the group by fields in query.
For example as following:
       
CREATE MATERIALIZED VIEW mv_1 BUILD IMMEDIATE REFRESH AUTO ON MANUAL
DISTRIBUTED BY RANDOM BUCKETS 2 PROPERTIES ('replication_num' = '1') AS 
select 
  o_orderstatus, 
  o_orderdate, 
  o_orderpriority, 
  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 
  orders 
group by 
  o_orderstatus, 
  o_orderdate, 
  o_orderpriority;


the query following  can rewrite successfully by mv above

    select o_orderstatus, o_orderdate, o_orderpriority,
       grouping_id(o_orderstatus, o_orderdate, o_orderpriority),
       grouping_id(o_orderstatus, o_orderdate),
       grouping(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 orders
       group by
       GROUPING SETS ((o_orderstatus, o_orderdate), (o_orderpriority), (o_orderstatus), ());

if query group by fields is sub of mv group by fields, and the query aggregate function extends `RollupTrait`
it can also rewrites successfully, for example query as following.
this is applicable for `CUBE`, `ROLLUP`

    select o_orderstatus, o_orderdate,
    grouping_id(o_orderstatus, o_orderdate),
    grouping(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 orders
    group by
    GROUPING SETS ((o_orderstatus, o_orderdate), (o_orderdate),());
@doris-robot
Copy link

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

Since 2024-03-18, the Document has been moved to doris-website.
See Doris Document.

@seawinde
Copy link
Contributor Author

seawinde commented Jul 8, 2024

run buildall

@morrySnow morrySnow merged commit fbc954e into apache:branch-2.1 Jul 8, 2024
21 of 22 checks passed
@yiguolei yiguolei mentioned this pull request Jul 19, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants