Skip to content

Fix SQL Server DISTINCT/ORDER BY error for limited lists with to-many children - #986

Merged
hugo-vrijswijk merged 5 commits into
typelevel:mainfrom
Bhup-GitHUB:fix/mssql-distinct-orderby-898
Nov 25, 2026
Merged

hugo-vrijswijk merged 5 commits into
typelevel:mainfrom
Bhup-GitHUB:fix/mssql-distinct-orderby-898

Conversation

@Bhup-GitHUB

Copy link
Copy Markdown
Contributor

Fixes #898

Problem

On SQL Server, a query that applies a limit or offset to a list field with a to-many child fails, for example countries(limit: 2) { name cities { name } } or countries(offset: 1) { name cities { name } }. The error is ORDER BY items must appear in the select list if SELECT DISTINCT is specified. The same query works on Postgres.

Root cause and approach

For this shape the parent keys are selected in a SELECT DISTINCT subquery with the limit or offset applied. That subquery reused the caller's ordering. SQL Server has no NULLS LAST, so the dialect emulates it with a leading CASE WHEN … IS NULL sort term, which SQL Server doesn't allow under SELECT DISTINCT because it isn't in the select list.

The subquery already filters out null keys, and its rows are unique keys, so null placement and any ordering after the keys can't change its result. It now orders by the key columns only, in the requested direction, using the backend's natural null placement. On SQL Server the computed term is gone; other backends generate the same order as before. Regression tests for the limit and offset variants are added to the shared SqlWorldSuite, so they run on every backend.

Before

before-898.png

After

after-898.png

cc @hugo-vrijswijk

On SQL Server the predicate subquery built for a limit or offset over a
to-many child selected DISTINCT key columns but kept the caller's
ordering, whose nulls-last emulation adds a CASE term that is not in the
select list, so SQL Server rejected the statement.

Keys in that subquery are non-null and unique, so ordering by the key
columns in the requested direction, with the backend's natural null
placement, gives the same order on every backend.

Fixes typelevel#898

@hugo-vrijswijk hugo-vrijswijk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments, but mostly looks good

…ey subquery

SQL Server rejects the extra expression used to change null placement in a DISTINCT key query. Use the natural placement for the requested direction because the query already filters out null keys.
… order comes first

Keep the ORDER BY on the DISTINCT subquery to the keys so SQL Server does not see duplicate or computed sort terms there. Keep the window expression and the outer query ordering unchanged.
Keep the first selection of each term. Repeating a term is redundant and can otherwise crowd out another column of a composite key when the query builds its DISTINCT order.
@Bhup-GitHUB

Copy link
Copy Markdown
Contributor Author

@hugo-vrijswijk Thanks for the review, all points are addressed and tests cover each case. Could you take another look?

@hugo-vrijswijk hugo-vrijswijk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for the fix and quick responses

@hugo-vrijswijk
hugo-vrijswijk merged commit 1d67811 into typelevel:main Nov 25, 2026
13 checks passed
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.

On MSSQL a DISTINCT subquery can order by a CASE expression that is not in its select list

2 participants