Aggregated find(): order_by on a JSONB dimension or a measure fails with GroupingError
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 68/100
调研方向
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_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.
- 主要语言
- Python
- 星标
- 23
- 派生
- 3
- 平均合并
- 10 小时 6 分钟
- 30 天内合并 PR
- 7
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
fraiseql/fraiseql-python 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 45/100
fraiseql/fraiseql-python#609 ·
维护者通常 1 天内回复
-
bug
难度 4/5 1-2 天 新手友好度 52/100
fraiseql/fraiseql-python#575 ·
维护者通常 1 天内回复
查看 fraiseql/fraiseql-python 的全部 Issue
相似的 Issue
-
docs(contributing): the two things that silently block a PR可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭available documentation good first issue
难度 1/5 1-3 小时 新手友好度 95/100
Nixitov/Riptide-Native#944 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 88/100
JTHCode/secretguard#24 ·
维护者通常 1 天内回复
-
area: middleware documentation good first issue
难度 2/5 1-3 小时 新手友好度 88/100
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
good first issue
难度 1/5 1 小时以内 新手友好度 95/100
omarelshamy25x/shamy-hijri#63 ·
维护者通常 1 天内回复