Aggregated find(): order_by on a JSONB dimension or a measure fails with GroupingError

Abierto
#610 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
graphql, postgresql, python
Área
backend, database

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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_sql is built by _build_order_by_sql before the GROUP BY branch, from the OrderBySet alone. It never sees group_by_exprs or the aggregate expressions.
  • _build_jsonb_field_expr renders a dimension as "data"->>'leaf', while OrderBy.to_sql renders data -> '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_by key → its GROUP BY expression, native or JSONB;
  • an aggregations alias → 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.

Lenguaje dominante
Python
Estrellas
23
Forks
3
Merge medio
10 h 6 min
PR fusionados (30 d)
7

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de fraiseql/fraiseql-python

Todos los issues de fraiseql/fraiseql-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.