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

未关闭
#610 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
graphql, postgresql, python
领域
backend, database

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
23
派生
3
平均合并
10 小时 6 分钟
30 天内合并 PR
7

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

fraiseql/fraiseql-python 的其他 Issue

查看 fraiseql/fraiseql-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。