Skip to content

[SPARK-55356][SQL] Support alias for PIVOT clause#54137

Open
filipdavidovic wants to merge 3 commits intoapache:masterfrom
filipdavidovic:feat/sql-pivot-alias
Open

[SPARK-55356][SQL] Support alias for PIVOT clause#54137
filipdavidovic wants to merge 3 commits intoapache:masterfrom
filipdavidovic:feat/sql-pivot-alias

Conversation

@filipdavidovic
Copy link

@filipdavidovic filipdavidovic commented Feb 4, 2026

What changes were proposed in this pull request?

Add support for alias in PIVOT clause, following the same syntax as T-SQL and BigQuery.

Short spec to describe the improvement:

Extend the PIVOT clause to accept an optional alias (PIVOT (...) AS alias), allowing the pivoted result set to be referenced by name in the surrounding query context. The alias qualifies all columns produced by the pivot operation, including both the grouping columns and the dynamically generated value columns. When an alias is provided, columns may be referenced as alias.column_name. When omitted, current unqualified resolution behaviour is preserved. In queries with multiple pivots or joins, the alias disambiguates column references. The alias scope is limited to the query block in which the pivot appears.

Why are the changes needed?

Main reason for the improvement is parity with platforms like SQL Server. When customers want to migrate, their queries using PIVOT alias can't be automatically transpiled, requiring a human in the loop.

Does this PR introduce any user-facing change?

Yes, it introduces an optional alias and updates the documentation to reflect it. Change is fully backwards compatible.

How was this patch tested?

Added end-to-end tests in SQLQueryTestSuite and unit tests in PivotParserSuite.

build/sbt "catalyst/testOnly *PivotParserSuite"

build/sbt "sql/testOnly *SQLQueryTestSuite -- -z pivot.sql"

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

Implemented with help of Claude Code.

@github-actions
Copy link

github-actions bot commented Feb 4, 2026

JIRA Issue Information

=== Improvement SPARK-55356 ===
Summary: Support alias in Spark SQL PIVOT
Assignee: None
Status: Open
Affected: ["4.1.1"]


This comment was automatically generated by GitHub Actions

FOR course IN ('dotNET', 'Java')
) AS pv;

-- pivot with alias (without AS keyword)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: SQL golden file is for integration test. Tiny cases like this should go to unit test (which we already did and we can just remove this query here)

Copy link
Author

Choose a reason for hiding this comment

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

Moved most of the tests from golden file to unit test suite. Left two complex ones for sanity checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants