order_by ignores registered table_columns: a field with its own SQL column sorts on the JSONB
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
- 45/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- graphql, postgresql, python
- Ambito
- backend-api-design, database
Direzione di ricerca
Read where_normalization.py around the direct-column branch, then trace db.py's ORDER BY construction through OrderBy._flat_column and resolve_native_column. Start by checking how table_columns, native_columns, and column_mapping reach the shared resolver, including the grouped-query constraint. Done means the registered view column sorts using its SQL type and the reproduction orders the IP addresses as expected.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
For a view registered with register_type_for_view(..., table_columns={...}), db.find() uses those columns for where but not for order_by. A GraphQL orderBy on a field that has its own SQL column still sorts on the JSONB document, unless the resolver also passes that column in native_dimensions (or declares it in column_mapping).
The two halves of one query then disagree on what a field is. The filter compares the typed column, while the sort reads the serialised value.
Where (1.31.2)
where_normalization.py, the direct-column branch (~l. 320): a top-level field found intable_columnsgetslookup_strategy = "sql_column".db.py, the ORDER BY build (~l. 3545-3563):order_set.to_sql(table_ref, native_columns=native_columns, column_mapping=column_mapping).native_columnsis onlykwargs["native_dimensions"], or every top-level field when the view is flat. The registeredtable_columnsare never consulted.OrderBy._flat_column→resolve_native_column(field, native_columns=..., column_mapping=...): notable_columnsinput either.
Reproduction
CREATE TABLE tb_host (id int PRIMARY KEY, ip_address inet NOT NULL);
INSERT INTO tb_host VALUES (1, '10.0.0.9'), (2, '10.0.0.10'), (3, '10.0.0.100');
CREATE VIEW v_host AS
SELECT id, ip_address,
jsonb_build_object('id', id, 'ip_address', host(ip_address)) AS data
FROM tb_host;
register_type_for_view("v_host", Host, table_columns={"id", "ip_address", "data"}, has_jsonb_data=True)
# Resolver forwarding the generated HostOrderByInput: [{ipAddress: ASC}]
await db.find("v_host", order_by=order_by)
- Expected:
10.0.0.9,10.0.0.10,10.0.0.100, i.e.ORDER BY t."ip_address" ASCon the inet column. - Actual:
10.0.0.10,10.0.0.100,10.0.0.9. The sort is ondata -> 'ip_address', a JSON string, so it is lexicographic. - Workaround:
db.find("v_host", order_by=order_by, native_dimensions={"ip_address"})gives the expected order. Withoutgroup_by,native_dimensionshas no other effect, but nothing about the name says it is needed here.
The same applies to any column whose type sorts differently from its JSON text: inet, date/timestamptz serialised in another format, numeric serialised as a string, ltree, and text sorted under a column collation.
Suggestion
Feed the registered table_columns into the ORDER BY resolution, the way where already does: a top-level sort field that is a column of the view sorts on t."col". Constraints:
- Keep one resolver.
resolve_native_columnexists so that GROUP BY, WHERE and ORDER BY cannot drift (#467). Addingtable_columnsas a fourth input, afternative_columnsandcolumn_mapping, keeps that property. A separate fix in_build_order_by_sqlwould bring the drift back. - It is a visible change. NULL placement flips (a jsonb null sorts first ASC, a SQL NULL last), and the sort type changes for every view whose column and JSONB value differ. That is the point of the fix, but it may want a release note or an opt-out (e.g.
order_by_columns=Falseon registration), rather than an opt-in that leaves every existing query on the JSONB. - Aggregated queries. A sort key resolved to a column must still be grouped. Restricting the new input to the non-aggregated path would be the conservative first step, if the GROUP BY interaction is not wanted in the same change.
Found while fixing an address list sorted as strings in a downstream application. It is worked around there with native_dimensions.
- Lingua principale
- Python
- Stelle
- 23
- Fork
- 3
- Merge medio
- 10h 6m
- PR unite (30g)
- 7
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Ha un 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 fraiseql/fraiseql-python
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 68/100
fraiseql/fraiseql-python#610 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 4/5 1-2 giorni Idoneità per principianti 52/100
fraiseql/fraiseql-python#575 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di fraiseql/fraiseql-python
Issue simili
-
docs(contributing): the two things that silently block a PRForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertaavailable documentation good first issue
Difficoltà 1/5 1-3 ore Idoneità per principianti 95/100
Nixitov/Riptide-Native#944 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
JTHCode/secretguard#24 ·
I maintainer di solito rispondono entro 1 giorno
-
area: middleware documentation good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
good first issue
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
omarelshamy25x/shamy-hijri#63 ·
I maintainer di solito rispondono entro 1 giorno