Skip to content

Centralize unparser select-scope boundary logic and add characterization tests#22683

Open
kosiew wants to merge 4 commits into
apache:mainfrom
kosiew:centralize-unparser-01-22668
Open

Centralize unparser select-scope boundary logic and add characterization tests#22683
kosiew wants to merge 4 commits into
apache:mainfrom
kosiew:centralize-unparser-01-22668

Conversation

@kosiew
Copy link
Copy Markdown
Contributor

@kosiew kosiew commented Jun 1, 2026

Which issue does this PR close?

Rationale for this change

The SQL unparser currently determines when a logical plan must preserve an independent SELECT scope in multiple places, with different operator sets depending on context. While those differences are intentional, the underlying rationale is not centralized or documented, making future changes harder to reason about.

This PR adds characterization tests for the current behavior and introduces a single helper that makes the context-specific boundary decisions explicit without changing generated SQL.

What changes are included in this PR?

  • Introduce a private SelectScopeContext enum to describe the two select-scope boundary contexts:

    • WindowInput
    • SubqueryAliasChild
  • Add a centralized helper:

    • plan_requires_independent_select_scope(plan, context)
  • Refactor:

    • window_input_requires_derived_subquery()
    • requires_derived_subquery()

    so they delegate to the new helper while preserving existing behavior.

  • Add characterization tests covering SubqueryAlias children that must remain derived:

    • aggregate child
    • window child
    • sort child
    • limit child
    • union child
  • Add characterization tests covering Window inputs that must remain derived:

    • sort input
    • union input
  • Add a shared helper for constructing ROW_NUMBER() window expressions used by the new tests.

No SQL output changes are intended by this refactor.

Are these changes tested?

Yes.

New tests added in datafusion/sql/tests/cases/plan_to_sql.rs:

  • test_unparse_subquery_alias_select_scope_boundaries
  • test_unparse_window_over_sort_without_projection
  • test_unparse_window_over_union_without_projection

These tests validate the current derived-subquery boundary behavior and ensure the refactor remains behavior-preserving.

Are there any user-facing changes?

No.

This change is a refactor and test expansion that preserves existing SQL generation behavior and does not introduce user-facing functionality changes.

LLM-generated code disclosure

This PR includes LLM-generated code and comments. All LLM-generated content has been manually reviewed and tested.

kosiew added 4 commits June 1, 2026 15:03
- Added `test_unparse_subquery_alias_select_scope_boundaries` to cover aggregate, window, sort, limit, and union under SubqueryAlias.
- Added `test_unparse_window_over_sort_without_projection` to test window functions over sort without projection.
- Added `test_unparse_window_over_union_without_projection` to test window functions over union without projection.
…ependent_select_scope

- Introduced a private SelectScopeContext.
- Centralized the plan_requires_independent_select_scope function.
- Updated existing wrappers to delegate to:
- requires_derived_subquery
- window_input_requires_derived_subquery
- Removed private SelectScopeContext and dispatcher helper from the unparser.
- Inlined exact matches in both callers for improved clarity.
- Added row_number_over helper and removed redundant window expression setup in new test cases.
- Inlined throwaway SQL variables in test snapshots to enhance readability.
…n unparser

- Restored SelectScopeContext in the SQL unparser
- Added central function plan_requires_independent_select_scope()
- Made requires_derived_subquery() and window_input_requires_derived_subquery() delegate to both wrappers
@github-actions github-actions Bot added the sql SQL Planner label Jun 1, 2026
@kosiew kosiew marked this pull request as ready for review June 1, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant