Skip to content

Conversation

@max-sixty
Copy link
Member

Summary

Fixes #4633 - sort inside group should not leak to outer query after join.

Per the PRQL spec, group resets the order. When using group (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:

  • Track whether sorting originated from DISTINCT ON (internal row selection)
  • Clear such sorting at join boundaries, while preserving explicit user sorts
  • Add regression tests for both the bug fix and edge cases

Why DISTINCT ON specifically?

  • DISTINCT ON creates a separate SqlTransform::Sort followed by DistinctOn
  • ROW_NUMBER (used for take > 1 or non-Postgres) embeds sorting in the window function, so it doesn't have this issue

Test plan

  • New regression test verifies no ORDER BY in outer query after join
  • New test verifies explicit sorts after group are preserved
  • Existing tests pass (including snapshot updates for correct behavior)
  • task prqlc:pull-request passes

🤖 Generated with Claude Code

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 <[email protected]>
@max-sixty max-sixty merged commit 2aa7477 into PRQL:main Dec 21, 2025
35 checks passed
@max-sixty max-sixty deleted the bug-fixes branch December 21, 2025 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

After Join generated extra ORDER BY section with phantom field

1 participant