[SPARK-31561][SQL] Add QUALIFY Clause#55019
Open
sunchao wants to merge 1 commit intoapache:masterfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR adds support for the
QUALIFYclause in Spark SQL. The implementation updates the SQL parser and AST builder to recognizeQUALIFY, carries it through analysis with a dedicated marker expression, and rewrites it after window extraction so the predicate is evaluated after window functions are materialized.In addition to basic
QUALIFYsupport, the analyzer enforces the intended semantics for the current query:QUALIFYrequires at least one window function in the currentSELECTlist orQUALIFYpredicate.QUALIFYpredicate are rejected with a targeted error that points at the offending aggregate expression.QUALIFYHAVING ... QUALIFY ...is handled correctly soHAVINGfilters grouped rows before window evaluation andQUALIFYfilters after the window result is computed.The PR also adds documentation and parser/analyzer coverage for positive and negative cases, including alias-based predicates, non-ANSI keyword handling, and grouped queries.
Why are the changes needed?
QUALIFYis supported by several popular SQL engines including Snowflake, Databricks SQL etc, and users expect it when porting SQL that filters on window-function results. Without it, equivalent Spark queries need an extra subquery or CTE just to filter on a window alias.This change closes that gap and makes Spark SQL more compatible with existing SQL workloads while preserving clear analyzer rules around window and aggregate semantics.
Does this PR introduce any user-facing change?
Yes. Spark SQL can now parse and analyze queries that use
QUALIFY, for example:This PR also introduces user-visible analysis errors for invalid
QUALIFYusage, such as using aggregate functions directly in theQUALIFYpredicate.How was this patch tested?
Added new unit tests and e2e tests
Was this patch authored or co-authored using generative AI tooling?
Yes. Generated by ChatGPT 5.4 High.