Skip to content

[SPARK-52798] [SQL] Add function approx_top_k_combine #51505

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

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

Conversation

yhuang-db
Copy link
Contributor

@yhuang-db yhuang-db commented Jul 15, 2025

What changes were proposed in this pull request?

This PR adds a SQL function: approx_top_k_accumulate, an aggregation function that merges multiple sketches into a single sketch.

Syntax

approx_top_k_combine(expr[, maxItemsTracked])

Arguments

  • expr: An expression of sketch structs
  • maxItemsTracked: An optional INTEGER literal. If maxItemsTracked is specified, use this value for the newly generated combined sketch. If maxItemsTracked is not specified, all input sketches must have the same maxItemsTracked, and the output sketch would use the same value as well.

Returns

The return of this function is a STRUCT with four fields: sketch, itemDataType, maxItemsTracked and typeCode. The return is exactly the same as for approx_top_k_accumulate.

Why are the changes needed?

They are useful sibling functions for approx_top_k queries.

Does this PR introduce any user-facing change?

Yes, this PR introduces a new user-facing SQL function. See user examples as below.

SELECT approx_top_k_estimate(approx_top_k_combine(sketch, 10000), 5) FROM (SELECT approx_top_k_accumulate(expr) AS sketch FROM VALUES (0), (0), (1), (1) AS tab(expr) UNION ALL SELECT approx_top_k_accumulate(expr) AS sketch FROM VALUES (2), (3), (4), (4) AS tab(expr))

How was this patch tested?

Unit tests for end-to-end SQL queries and invalid input for expressions.

Was this patch authored or co-authored using generative AI tooling?

@github-actions github-actions bot added the SQL label Jul 15, 2025
@yhuang-db yhuang-db marked this pull request as ready for review July 16, 2025 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant