Skip to content

refactor(sql): stabilize binder alias semantics across aggregate, window, and clause rewrites#19618

Open
forsaken628 wants to merge 26 commits intodatabendlabs:mainfrom
forsaken628:projection-bind
Open

refactor(sql): stabilize binder alias semantics across aggregate, window, and clause rewrites#19618
forsaken628 wants to merge 26 commits intodatabendlabs:mainfrom
forsaken628:projection-bind

Conversation

@forsaken628
Copy link
Copy Markdown
Collaborator

@forsaken628 forsaken628 commented Mar 25, 2026

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

This PR refactors the SQL binder to preserve a stable semantic view of SELECT aliases across WHERE, HAVING, QUALIFY, and ORDER BY, while separating prepass aggregate discovery from final projection rewriting.

  • Introduce aggregate prepass alias/catalog analysis to detect aggregate and window dependencies before clause binding.
  • Keep clause alias resolution based on original SELECT semantics, avoiding accidental dependence on already-rewritten projection state.
  • Refactor projection handling around SelectInfo, so projection, DISTINCT, and ORDER BY share the same source/projection scalar model.
  • Tighten QUALIFY, window, grouping, and aggregate interactions, including UDAF and SRF edge cases.
  • Expand binder semantic coverage with focused golden tests for clauses, grouping, window paths, named windows, and subquery metadata side effects.

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@github-actions github-actions bot added the pr-refactor this PR changes the code base without new features or bugfix label Mar 25, 2026
@forsaken628 forsaken628 marked this pull request as draft March 25, 2026 06:34
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a3e27b5af4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

# Conflicts:
#	src/query/sql/src/planner/binder/distinct.rs
#	src/query/sql/src/planner/binder/having.rs
#	src/query/sql/src/planner/binder/project.rs
#	src/query/sql/src/planner/binder/qualify.rs
#	src/query/sql/src/planner/binder/sort.rs
#	src/query/sql/src/planner/binder/window.rs
#	src/query/sql/src/planner/semantic/grouping_check.rs
#	src/query/sql/tests/it/semantic/binder.rs
#	src/query/sql/tests/it/semantic/binder.txt
@forsaken628 forsaken628 changed the title refactor(sql): projection-bind refactor(sql): unify select output planning and preserve aggregate reuse in window analysis Apr 1, 2026
@forsaken628 forsaken628 force-pushed the projection-bind branch 2 times, most recently from 256446d to a6ceca1 Compare April 1, 2026 12:52
@forsaken628 forsaken628 marked this pull request as ready for review April 1, 2026 13:01
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6ceca131c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@forsaken628 forsaken628 marked this pull request as draft April 2, 2026 02:53
@forsaken628 forsaken628 marked this pull request as ready for review April 2, 2026 07:09
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d119cbbbdf

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@forsaken628
Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@forsaken628 forsaken628 requested review from TCeason and b41sh April 3, 2026 04:08
@forsaken628 forsaken628 marked this pull request as draft April 3, 2026 06:36
@forsaken628 forsaken628 changed the title refactor(sql): unify select output planning and preserve aggregate reuse in window analysis refactor(sql): stabilize binder alias semantics across aggregate, window, and clause rewrites Apr 3, 2026
@forsaken628 forsaken628 marked this pull request as ready for review April 3, 2026 09:23
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 49600e97fa

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

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

Labels

pr-refactor this PR changes the code base without new features or bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants