fix: Prevent DISTINCT ON internal sorting from leaking past joins#5633
Merged
fix: Prevent DISTINCT ON internal sorting from leaking past joins#5633
Conversation
Per the PRQL spec, `group` resets the order - any `sort` inside a group is for internal row selection (determining which row to keep), not output ordering. Previously, when using DISTINCT ON (Postgres), this internal sorting was incorrectly propagating to the outer query after a join. This fix tracks whether sorting originated from DISTINCT ON and clears it at join boundaries, while preserving explicit user-requested sorts that come after the group. Fixes PRQL#4633 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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.
Summary
Fixes #4633 - sort inside group should not leak to outer query after join.
Per the PRQL spec,
groupresets the order. When usinggroup (sort x | take 1), the sorting is for row selection (which row to keep per group), not output ordering. Previously, this internal DISTINCT ON sorting was incorrectly propagating to the outer query after a join.Changes:
Why DISTINCT ON specifically?
SqlTransform::Sortfollowed byDistinctOntake > 1or non-Postgres) embeds sorting in the window function, so it doesn't have this issueTest plan
task prqlc:pull-requestpasses🤖 Generated with Claude Code