On MSSQL a DISTINCT subquery can order by a CASE expression that is not in its select list
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 65/100
Direzione di ricerca
Inizia leggendo DoobieMSSqlMapping.orderToFragment e distinctOnToFragment, quindi esegui la query riprodotta tramite SqlWorldMapping e WorldSuite. Confronta l’SQL MSSQL e la copertura esistente di SqlWorldSuite per le join dei figli, i limiti o gli offset, l’ordinamento delle chiavi e la gestione di null. Il lavoro è concluso quando la query riprodotta viene eseguita correttamente e i test MSSQL pertinenti hanno esito positivo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Co-authored by an LLM. Reviewed by me. And now without further ado:
MSSQL has no NULLS FIRST / NULLS LAST, so DoobieMSSqlMapping.orderToFragment emulates it by prepending a computed CASE WHEN ... IS NULL THEN 1 ELSE 0 END term to the ORDER BY. MSSQL also has no DISTINCT ON, so distinctOnToFragment renders a plain DISTINCT instead.
Where those two meet in the same select, the result is rejected:
com.microsoft.sqlserver.jdbc.SQLServerException: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Reproducing
SqlWorldMapping is already wired into doobie-mssql as WorldSuite, so no new fixture or mapping is needed:
query {
countries(limit: 2) {
name
cities {
name
}
}
}
The statement MSSQL receives, captured from the JDBC driver's own logging since the query never returns:
SELECT city.id, city.name, country.code, country.name AS name_alias_0
FROM country
INNER JOIN (
SELECT DISTINCT country.code
FROM country
WHERE (country.code IS NOT NULL)
ORDER BY CASE WHEN (country.code COLLATE DATABASE_DEFAULT) IS NULL THEN 1 ELSE 0 END ASC,
(country.code COLLATE DATABASE_DEFAULT)
OFFSET 0 ROWS FETCH FIRST 2 ROWS ONLY
) country_city_pred ON ((country_city_pred.code = country.code))
LEFT JOIN city ON ((city.countrycode = country.code))
WHERE ((country.code = country_city_pred.code))
The country_city_pred subquery selects country.code and nothing else. Its first ORDER BY term is the CASE expression, which is not in that select list. The second term, the collate-wrapped column, MSSQL is happy to match against country.code.
An offset alone reproduces it just as a limit does; countries(offset: 1) { name cities { name } } fails with the same message.
What has to line up for it to trigger
Three things at once, which is why it has gone unnoticed:
- A to-many child in the selection (
cities) together with a limit or an offset on the parent field. That combination is what produces the_predsubquery carrying theDISTINCT; the child join alone does not. - An ordering whose nulls handling takes the
CASEbranch oforderToFragment. - Ordering by the key column, so that
distinctOrderColumndoes not kick in.
Existing MSSQL coverage misses it by falling on the wrong side of one condition each time. SqlWorldSuite's "simple query with limit" is countries(limit: 3) { name }, which has the limit but no child join, so there is no DISTINCT subquery at all. "query with top-level ordering, limit and subobjects" is countries(limit: 3, byPopulation: true) { name population cities { name } }, which does have both, but ordering by a non-key column routes it through FirstValueColumn, splitting the query so the DISTINCT sits on an inner select and the CASE lands on the outer one, where it is legal. That split renders as:
SELECT dist.code FROM (
SELECT DISTINCT country.code, first_value(country.population) OVER (...) AS population
FROM country WHERE ... ORDER BY (country.code COLLATE DATABASE_DEFAULT) OFFSET 0 ROWS
) dist
ORDER BY CASE WHEN dist.population IS NULL THEN 1 ELSE 0 END ASC, dist.population, (dist.code COLLATE DATABASE_DEFAULT)
OFFSET 0 ROWS FETCH FIRST 2 ROWS ONLY
(That fragment is from an equivalent capture at limit: 2, so its row count differs from the test's; the shape is the point.)
Not a general problem with the shape
The same query against the same mapping on doobie-pg returns the expected data. Postgres has both native nulls ordering and DISTINCT ON, so neither emulation applies and no computed term appears in the ORDER BY:
INNER JOIN (
SELECT DISTINCT ON ((country.code COLLATE "C")) (country.code COLLATE "C")
FROM country WHERE (country.code IS NOT NULL)
ORDER BY (country.code COLLATE "C") LIMIT 2
) AS country_city_pred ...
It also appears to be specific to MSSQL among the backends that share the CASE WHEN emulation. The MySQL backend proposed in #872 and the MariaDB backend in #873 both emulate nulls ordering the same way, but MySQL 8.4 and MariaDB were checked against live servers and accept the shape, so neither looks affected.
Note
I ran into this while wiring #871's SqlQualifiedNamesSuite into doobie-mssql, where a test of this shape failed. Reproducing it on SqlWorldMapping, which has no schema-qualified tables in it, ruled out any connection to that PR. Six of that suite's eight tests pass on MSSQL; one fails on this bug, and one on an unrelated CHAR padding artifact in the MSSQL fixture I wrote, which is mine to sort out. The suite stays unwired on MSSQL until both are dealt with.
- Lingua principale
- Scala
- Stelle
- 189
- Fork
- 32
- Merge medio
- 18h 27m
- PR unite (30g)
- 21
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di typelevel/grackle
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Add http(4s) moduleAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 28/100
typelevel/grackle#946 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
conformance
Difficoltà 3/5 1-2 giorni Idoneità per principianti 48/100
I maintainer di solito rispondono entro 1 giorno
-
Postgres enum supportApertaenhancement
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
I maintainer di solito rispondono entro 1 giorno
-
Sort out union subtypingApertabug
Difficoltà 5/5 Più di una settimana Idoneità per principianti 30/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di typelevel/grackle
Issue simili
-
go 🏃 testing 🧪
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
valkey-io/valkey-glide#7239 ·
I maintainer di solito rispondono entro 3 giorni
-
itype:bug stat:needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
ML Scorer validation passes identical non-numeric column types that scikit-learn cannot scoreAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
apache/texera#8756 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
com-lihaoyi/mill#7640 ·
I maintainer di solito rispondono entro 1 giorno
-
Setup dependabotAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
software-mansion/starknet-jvm#631 ·