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 摘要。