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

Added Sum_IF() aggregate function #24174

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

DHRUV6029
Copy link

@DHRUV6029 DHRUV6029 commented Nov 30, 2024

Description

Add new aggregate function sum_if(x, y) and sum_if(x, y, n) that returns the sum of values matching a condition. This provides a more concise way to write conditional sums without using CASE expressions.

Motivation and Context

Currently, to sum values conditionally, users need to write verbose CASE expressions like sum(CASE WHEN x THEN y END). The new sum_if function provides a cleaner, more readable alternative similar to the existing count_if function. This also aligns with common SQL developer requests for simplified conditional aggregations.

Issue/Feature Request #19060

Impact

Adds new SQL aggregate function sum_if
No breaking changes to existing functionality
Improves SQL readability for conditional sum operations

Test Plan

Added unit tests for basic functionality
Added tests for null handling
Added tests for default value parameter
Added tests for various data types
Added tests for edge cases (empty sets, all nulls)
Added integration tests with real data scenarios

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.

@DHRUV6029 DHRUV6029 requested a review from a team as a code owner November 30, 2024 06:00
@DHRUV6029 DHRUV6029 requested a review from presto-oss November 30, 2024 06:00
Copy link

linux-foundation-easycla bot commented Nov 30, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@steveburnett
Copy link
Contributor

In Contributor checklist I see that

Documented new properties (with its default value), SQL syntax, functions, or other functionality.

is checked, but there is no documentation for this new function.

Please add documentation for this new function, possibly in this file: https://github.com/prestodb/presto/blob/master/presto-docs/src/main/sphinx/functions/aggregate.rst.

@DHRUV6029
Copy link
Author

@steveburnett @elharo I have added the documentation for the function , can you please review it.
Thanks

steveburnett
steveburnett previously approved these changes Feb 17, 2025
Copy link
Contributor

@steveburnett steveburnett left a comment

Choose a reason for hiding this comment

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

LGTM! (docs)

Pull branch, local doc build, looks good. Thanks!

Copy link
Contributor

@ZacBlanco ZacBlanco left a comment

Choose a reason for hiding this comment

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

It looks like there is also a RealSumAggregation function. Just to be complete, can we also add a version which supports the Real type?

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