Aggregated find(): order_by on a JSONB dimension or a measure fails with GroupingError
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
- 68/100
Direzione di ricerca
Start in db.py at _build_find_query and trace how _build_order_by_sql handles grouped queries; compare its expressions with _build_jsonb_field_expr and the partial-period UNION path in _union_order_terms / _branch_sort_exprs. Reproduce with the provided db.find() call, then cover JSONB dimensions and aggregation aliases in tests. Done means grouped keys sort by their grouped expression, measures by their aggregate expression, and ungrouped keys are ignored.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
In an aggregated db.find() (group_by + aggregations, or auto-aggregation), an order_by on a JSONB dimension or on a measure fails in PostgreSQL. The ORDER BY generator renders its own expression (data -> 'field'), not the one the query groups on ("data"->>'field'). A key is only sortable here as the grouped expression, an aggregate, or something built from them.
Only native keys (native_dimensions, column_mapping / native_dimension_mapping) work today, because they go through the shared resolver (#467) and render t."col" on both sides.
Found while fixing #609, and reproduced on dev @ 8d25ce85d without that change.
Where (1.31.2)
db.py,_build_find_query:order_sqlis built by_build_order_by_sqlbefore the GROUP BY branch, from theOrderBySetalone. It never seesgroup_by_exprsor the aggregate expressions._build_jsonb_field_exprrenders a dimension as"data"->>'leaf', whileOrderBy.to_sqlrendersdata -> 'leaf'.
Reproduction
await db.find(
"v_host",
group_by=["ip_address"],
aggregations={"n": "COUNT(*)"},
order_by=[{"ip_address": "ASC"}], # or [{"n": "DESC"}]
)
SELECT json_build_object('ip_address', "data"->>'ip_address', 'n', COUNT(*))::text
FROM "v_host" GROUP BY "data"->>'ip_address'
ORDER BY data -> 'ip_address' ASC -- order_by on a dimension
-- ORDER BY data -> 'n' DESC -- order_by on a measure: not even COUNT(*)
psycopg.errors.GroupingError: column "v_host.data" must appear in the GROUP BY clause
or be used in an aggregate function
A nested dimension (dimensions.region) fails the same way.
Suggestion
Sort an aggregated query on the expression its SELECT list renders for that key, as the partial-period UNION path already does (_union_order_terms / _branch_sort_exprs, #468):
- a
group_bykey → its GROUP BY expression, native or JSONB; - an
aggregationsalias → its aggregate expression; - direction and collation as today.
A key that is neither grouped nor aggregated can't be sorted in an aggregated query. The UNION path drops such an instruction. Doing the same here gives both aggregated paths one behaviour.
Note that a JSONB dimension is grouped as text (->>), so it sorts as text. This matches the UNION path; a typed sort needs a native column.
- 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
-
order_by ignores registered table_columns: a field with its own SQL column sorts on the JSONBAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
fraiseql/fraiseql-python#609 ·
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