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

enhancement(optimizer): merge common aggregation functions #12305

Closed
fuyufjh opened this issue Sep 14, 2023 · 1 comment · Fixed by #12683
Closed

enhancement(optimizer): merge common aggregation functions #12305

fuyufjh opened this issue Sep 14, 2023 · 1 comment · Fixed by #12683
Assignees
Milestone

Comments

@fuyufjh
Copy link
Member

fuyufjh commented Sep 14, 2023

Discussed with @st1page

For example,

select avg(x) a, sum(x) s, count(x) c from t;

will be executed as

select expr1 / expr2, s, c from 
  (select sum(x) as expr1, count(x) as expr2, sum(x) s , count(x) c from t);

Here, the two sum(x) and two count(x) can be simply reused.

Another example is NexMark Q15

StreamHashAgg [append_only] { group_key: [$expr1, bidder, auction, flag], aggs: [count, count filter((price < 10000:Int32)), count filter((price >= 10000:Int32) AND (price < 1000000:Int32)), count filter((price >= 1000000:Int32)), count filter((price < 10000:Int32)), count filter((price >= 10000:Int32) AND (price < 1000000:Int32)), count filter((price >= 1000000:Int32)), count filter((price < 10000:Int32)), count filter((price >= 10000:Int32) AND (price < 1000000:Int32)), count filter((price >= 1000000:Int32))] } { intermediate state table: 1, state tables: [], distinct tables: [] }

StreamHashAgg [append_only] {
  group_key: [$expr1, bidder, auction, flag],
  aggs: [
    count, 
    count filter((price < 10000:Int32)), 
    count filter((price >= 10000:Int32) AND (price < 1000000:Int32)), 
    count filter((price >= 1000000:Int32)),
    count filter((price < 10000:Int32)), 
    count filter((price >= 10000:Int32) AND (price < 1000000:Int32)), 
    count filter((price >= 1000000:Int32)),
    count filter((price < 10000:Int32)), 
    count filter((price >= 10000:Int32) AND (price < 1000000:Int32)), 
    count filter((price >= 1000000:Int32))] 

Obviously the 9 count agg functions can be merged to 3, so that we reduce the aggregation state size to 1/3.

@st1page
Copy link
Contributor

st1page commented Sep 14, 2023

We do not need to implement it in Agg executor but implement it as an optimizer rule we can write a project ahead 🤔

@fuyufjh fuyufjh changed the title Optimize: merge common aggregation functions enhancement(optimizer): merge common aggregation functions Sep 14, 2023
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 a pull request may close this issue.

3 participants