Skip to content

Extend filtered aggregation optimizer to support only masked partial aggregation cases #25171

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

feilong-liu
Copy link
Contributor

@feilong-liu feilong-liu commented May 21, 2025

Description

The current optimizer MergePartialAggregationsWithFilter only supports case where there are both aggregations with and without filter on the same filter. For example,
select sum(totalprice), sum(totalprice) filter (where orderpriority='2-HIGH') from orders group by orderdate
will work, but
select sum(totalprice) filter (where orderstatus='F'), sum(totalprice) filter (where orderpriority='2-HIGH') from orders group by orderdate
will not work.
In this change, I extend it to work for the second case too.

Motivation and Context

Extend existing optimizer to a broader range of queries.

Impact

Extend existing optimizer to a broader range of queries.

Test Plan

Add unit tests

Contributor checklist

  • Please make sure your submission complies with our contributing guide, in particular code style and commit standards.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.

Release Notes

Please follow release notes guidelines and fill in the release notes below.

== RELEASE NOTES ==

General Changes
* Extend  MergePartialAggregationsWithFilter to work for queries where all aggregations have mask

@feilong-liu feilong-liu requested a review from ZacBlanco May 21, 2025 18:21
@prestodb-ci prestodb-ci added the from:Meta PR from Meta label May 21, 2025
@feilong-liu feilong-liu marked this pull request as draft May 21, 2025 18:21
@feilong-liu feilong-liu requested a review from kaikalur May 21, 2025 18:21
@feilong-liu feilong-liu force-pushed the fix_merge_agg_filter branch from cf74f97 to e322aec Compare May 21, 2025 18:36
context.get().getPartialResultToMask().putAll(aggregationsToMergeOutputCombined.entrySet().stream()
.collect(toImmutableMap(Map.Entry::getValue, x -> x.getKey().getMask().get())));
context.get().getPartialOutputMapping().putAll(aggregationsToMergeOutputCombined.entrySet().stream()
.collect(toImmutableMap(Map.Entry::getValue, x -> aggregationsWithoutMaskToOutputCombined.get(removeFilterAndMask(x.getKey())))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we actually add the OR of the filters in this case?

So SUM(IF(p1, x)), SUM(IF(p2, x))

will have

SUM(IF(p1 or p2, x))

in the partial agg.

@feilong-liu feilong-liu force-pushed the fix_merge_agg_filter branch from e322aec to 40b134f Compare May 23, 2025 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
from:Meta PR from Meta
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants