Skip to content

Add optimizer to convert min_by/max_by to row number function #25190

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 23, 2025

Description

This optimization converts queries like

select id, max(ds), max_by(feature1, ds), max_by(feature2, ds) from table group by id

to

select id, ds, feature1, feature2 from (select id, ds, feature1, feature2, row_number() over (partition by id order by ds desc) row_num) where row_num = 1

Here feature1, feature2 are maps. This rewrite can avoid the expensive cost of aggregations on feature1 and feature2. This is commonly used in getting latest features in machine learning workload.

Motivation and Context

Query optimization to reduce cost.

Impact

Query optimization to reduce cost.

Test Plan

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
* Add a new optimization `MinMaxByToWindowFunction` to rewrite min_by/max_by aggregations with row_number window function

@feilong-liu feilong-liu requested a review from ZacBlanco May 23, 2025 20:57
@prestodb-ci prestodb-ci added the from:Meta PR from Meta label May 23, 2025
@feilong-liu feilong-liu marked this pull request as draft May 23, 2025 20:57
@feilong-liu feilong-liu requested a review from kaikalur May 23, 2025 20:57
@feilong-liu feilong-liu force-pushed the feature_dedup branch 2 times, most recently from 1278149 to dbc5e09 Compare May 23, 2025 21:15
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.

2 participants