Restore Sort unparser guard for correct ORDER BY placement#20658
Open
krinart wants to merge 3 commits intoapache:mainfrom
Open
Restore Sort unparser guard for correct ORDER BY placement#20658krinart wants to merge 3 commits intoapache:mainfrom
krinart wants to merge 3 commits intoapache:mainfrom
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.
Which issue does this PR close?
Restore the
already_projected()guard in the Sort case ofselect_to_sql_recursively. Without it, queries with ORDER BY expressions generate invalid SQL when the Sort node follows an outer Projection.Problem
Two regressions in the DuckDB federation unparser after upgrading to DataFusion 52:
SELECT "SearchPhrase" ... ORDER BY to_timestamp("EventTime") LIMIT 10produce ORDER BY outside the subquery, referencing table names ("hits") that are out of scope.GROUPING()expressions loses thederived_sortsubquery alias, placing sort references outside their valid scope.Root Cause
DF52's optimizer merges
LimitintoSortas a fetch parameter, changing the plan shape fromProjection → Limit → Sort → ...toProjection → Sort(fetch) → ....The Sort case previously had a guard that wrapped the sort into aderived_sortsubquery whenalready_projected()was true. This guard was removed in DF52, causing ORDER BY and LIMIT to be placed on the outer query where inner table references are invalid.Fix
Re-add the guard at the top of the Sort match arm in select_to_sql_recursively: