fix: don't leak joined branch's sort into implicit ORDER BY for take#6015
Open
lukapeschke wants to merge 1 commit into
Open
fix: don't leak joined branch's sort into implicit ORDER BY for take#6015lukapeschke wants to merge 1 commit into
lukapeschke wants to merge 1 commit into
Conversation
When a pipeline sorts both the left and the joined branch (both wrapped in CTEs by a preceding `group`) and is then truncated with `take`, prqlc synthesized the implicit ORDER BY for the LIMIT from the *joined* branch's sort column. That column is anchored to the joined branch's source table, which is no longer in scope once the branch is materialized as a CTE, producing invalid SQL (e.g. `ORDER BY bar.a` where `bar` lives inside a CTE). The `Flattener` folds a join's input (left) first, setting the running sort frame to the left's order, then folds the join's `with` sub-pipeline, which overwrote the frame with the right branch's sort. The join transform's own sort was cleared, but `self.sort` was left pointing at the joined branch, so downstream transforms (`take`) inherited the wrong column. Snapshot and restore the input's sort frame around folding a join/append kind, implementing the documented "a join retains the left side's order" semantic. The implicit ORDER BY now references the left CTE alias. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
prql-bot
approved these changes
Jun 17, 2026
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.
When a pipeline sorts both the left and the joined branch (both wrapped in CTEs by a preceding
group) and is then truncated withtake, prqlc synthesized the implicit ORDER BY for the LIMIT from the joined branch's sort column. That column is anchored to the joined branch's source table, which is no longer in scope once the branch is materialized as a CTE, producing invalid SQL (e.g.ORDER BY bar.awherebarlives inside a CTE).The
Flattenerfolds a join's input (left) first, setting the running sort frame to the left's order, then folds the join'swithsub-pipeline, which overwrote the frame with the right branch's sort. The join transform's own sort was cleared, butself.sortwas left pointing at the joined branch, so downstream transforms (take) inherited the wrong column.Snapshot and restore the input's sort frame around folding a join/append kind, implementing the documented "a join retains the left side's order" semantic. The implicit ORDER BY now references the left CTE alias.