Skip to content

feat: add a config to disable subquery_sort_elimination#21614

Open
haohuaijin wants to merge 6 commits intoapache:mainfrom
haohuaijin:config-order-remove
Open

feat: add a config to disable subquery_sort_elimination#21614
haohuaijin wants to merge 6 commits intoapache:mainfrom
haohuaijin:config-order-remove

Conversation

@haohuaijin
Copy link
Copy Markdown
Contributor

@haohuaijin haohuaijin commented Apr 14, 2026

Which issue does this PR close?

Rationale for this change

What changes are included in this PR?

  • Added a new optimizer config option: datafusion.sql_parser.enable_subquery_sort_elimination
  • Added a SessionConfig builder method: with_enable_subquery_sort_elimination(...)
  • Updated SQL relation planning so subquery/CTE ORDER BY elimination only happens when this config is enabled
  • Kept the current behavior as the default to avoid changing existing behavior unexpectedly

Are these changes tested?

Added SQL integration tests that verify:

  • subquery ORDER BY is removed by default
  • subquery ORDER BY is preserved when enable_subquery_sort_elimination is set to false
  • add slt test case

Are there any user-facing changes?

no

@github-actions github-actions bot added sql SQL Planner common Related to common crate execution Related to the execution crate labels Apr 14, 2026
@github-actions github-actions bot added documentation Improvements or additions to documentation sqllogictest SQL Logic Tests (.slt) labels Apr 14, 2026
Copy link
Copy Markdown
Member

@asolimando asolimando left a comment

Choose a reason for hiding this comment

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

Nice improvement!

In Apache Calcite we had a similar discussion a while ago (CALCITE-4160 for the details) and we reached the same conclusion, preserving ordering for subqueries is an optional feature and it's technically safe to always remove it, but it's good to let the user control this behavior.

There is a typo in the PR title where "disalbe" should be "disable", as this goes into the commit message we should fix it before merging.

Nit: it would be nice to have an SLT test with SET datafusion.sql_parser.enable_subquery_sort_elimination = false in addition to the unit tests, to check if the user-facing propagation works as expected.

@haohuaijin haohuaijin changed the title feat: add a config to disalbe subquery_sort_elimination feat: add a config to disable subquery_sort_elimination Apr 14, 2026
@haohuaijin
Copy link
Copy Markdown
Contributor Author

haohuaijin commented Apr 14, 2026

thanks for your reviews @asolimando and the information from Apache Calcite, i added the .slt test case

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

Labels

common Related to common crate documentation Improvements or additions to documentation execution Related to the execution crate sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DISCUSSION] Sorts being removed from subqueries

2 participants