Repository navigation
Fix SQL Server DISTINCT/ORDER BY error for limited lists with to-many children - #986
Merged
hugo-vrijswijk merged 5 commits intoNov 25, 2026
Merged
Conversation
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
left a comment
Contributor
There was a problem hiding this comment.
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.
Contributor
Author
|
@hugo-vrijswijk Thanks for the review, all points are addressed and tests cover each case. Could you take another look? |
hugo-vrijswijk
approved these changes
Nov 25, 2026
hugo-vrijswijk
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Thank you for the fix and quick responses
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.
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 } }orcountries(offset: 1) { name cities { name } }. The error isORDER 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 DISTINCTsubquery with the limit or offset applied. That subquery reused the caller's ordering. SQL Server has noNULLS LAST, so the dialect emulates it with a leadingCASE WHEN … IS NULLsort term, which SQL Server doesn't allow underSELECT DISTINCTbecause 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
After
cc @hugo-vrijswijk