Skip to content

Conversation

@max-sixty
Copy link
Member

Summary

  • Fixes invalid MSSQL SQL generated when combining SELECT DISTINCT with FETCH (pagination)
  • The issue was that ORDER BY (SELECT NULL) was used, but MSSQL requires ORDER BY items to appear in the SELECT list when DISTINCT is used
  • The fix extracts the first column from the projection for use in ORDER BY when DISTINCT is present

Fixes #5628

Test plan

  • Added test case test_mssql_distinct_fetch() covering the reported issue
  • Verified non-MSSQL dialects (PostgreSQL, MySQL, SQLite) are unaffected
  • All existing tests pass

🤖 Generated with Claude Code

When a query combines DISTINCT and FETCH (pagination) in MSSQL, the
compiler was generating ORDER BY (SELECT NULL) which is invalid because
MSSQL requires ORDER BY items to appear in the SELECT list when DISTINCT
is used.

This fix extracts the first column from the projection for use in ORDER
BY when DISTINCT is present, producing valid MSSQL syntax.

Fixes PRQL#5628

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@max-sixty max-sixty merged commit a0b58ea into PRQL:main Dec 20, 2025
34 of 35 checks passed
@max-sixty max-sixty deleted the 5628 branch December 20, 2025 23:29
max-sixty added a commit to max-sixty/prql that referenced this pull request Dec 20, 2025
Expands test_mssql_distinct_fetch() to cover additional code paths:
- ExprWithAlias: column with alias uses alias for ORDER BY
- Multiple columns: first column is used for ORDER BY

Follow-up to PRQL#5630 to improve code coverage.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@lukapeschke
Copy link
Contributor

Great, thanks @max-sixty 🎉

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.

When doing a FETCH with SELECT DISTINCT with MsSql, ORDER BY (SELECT NULL) causes a syntax error

2 participants