Restricting a dj.U() aggregation raises a bare AssertionError
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 40/100
研究方向
The failing assertion is in Aggregation.make_sql in expression.py near line 1358, and the PostgreSQL subquery gate uses _grouping_attributes; read condition.py for the no-common-attributes restriction semantics. First reproduce with dj.U().aggr(File, n="count(*)") & "n = 3" on MySQL. Done when the assertion is gone, the HAVING and subquery paths both return correct rows on MySQL and PostgreSQL, and tests cover string and dict restrictions with matching and non-matching counts plus restricting another table.
由索引模型根據 Issue 內容生成。
描述
Restricting a universal aggregation, dj.U().aggr(...) & <condition>, raises an AssertionError with no message, on any backend. The aggregation itself returns the expected single row; only the restriction fails, including when the restricted aggregate is used to restrict another table.
Versions: v2.3.4 and master at 68fdea59, MySQL 8.0.
Reproduction
@schema
class File(dj.Manual):
definition = """
file_id : int32
---
filename : varchar(255)
"""
File.insert([(1, "a_part1.dat"), (2, "a_part2.dat"), (3, "a_meta.json")])
total = dj.U().aggr(File, n="count(*)")
total.to_dicts() # [{'n': 3}]
(total & "n = 3").to_dicts() # AssertionError
(total & {"n": 3}).to_dicts() # AssertionError
len(File & (total & "n = 3")) # AssertionError
Expected: (total & "n = 3") returns [{'n': 3}] (and [] when the count differs), and File & (total & "n = 3") keeps every row of File when the restricted aggregate is non-empty and none when it is empty, as restriction by an expression with no common attributes does elsewhere (condition.py, "without common attributes, any non-empty set matches everything").
Cause
Aggregation.make_sql (expression.py:1358):
assert self._grouping_attributes or not self.restriction
A restriction on an aggregation is emitted as HAVING (MySQL) or as an outer WHERE around a subquery (PostgreSQL), but both paths assume grouping attributes exist. The PostgreSQL branch is also gated on them (use_subquery_for_having = ... and self._grouping_attributes).
Proposed fix
- Remove the assertion.
- MySQL: emit
HAVINGwithoutGROUP BY, which MySQL accepts for an ungrouped aggregate and which may reference the select alias (SELECT count(*) AS n FROM t HAVING n = 3). - PostgreSQL: take the existing subquery path whenever the aggregation is restricted, with or without grouping attributes.
- Tests: restrict
dj.U().aggr(T, n="count(*)")by a string and by a dict, on both backends, with matching and non-matching counts; and restrict another table by the restricted aggregate.
Workaround
Wrap the aggregate in a subquery before restricting it:
gate = dj.U().aggr(File, n="count(*)").make_subquery() & {"n": 3}
File & gate # all rows when the count is 3, none otherwise
len(gate) still fails on this form; see the companion issue on len() of an expression with an empty primary key.
- 主要語言
- Python
- 星號
- 197
- 分支
- 98
- 平均合併
- 15 小時 31 分鐘
- 30 天內合併 PR
- 9
環境準備
- 提供 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
datajoint/datajoint-python 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 70/100
datajoint/datajoint-python#1581 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
datajoint/datajoint-python#1539 · 3 則留言 ·
維護者通常 1 天內回覆
-
_job_start_time records the populating host's local time, with no zone可能已有人在做 @dimitri-yatsenko 於 1 天前認領。 未關閉
datajoint/datajoint-python#1584 · 已指派 1 人 ·
維護者通常 1 天內回覆
-
database.name should namespace schema names on MySQL, as it does on PostgreSQL可能已有人在做 @dimitri-yatsenko 於 3 天前認領。 未關閉feature
難度 4/5 3-5 天 新手友好度 15/100
datajoint/datajoint-python#1582 ·
維護者通常 1 天內回覆
-
A column copied by a foreign key loses the parent's core type (int32 becomes int)可能已有人在做 @dimitri-yatsenko 於 4 天前認領。 未關閉
難度 3/5 半天 新手友好度 25/100
datajoint/datajoint-python#1578 ·
維護者通常 1 天內回覆
查看 datajoint/datajoint-python 的全部 Issue
相似的 Issue
-
bug difficulty:2 good first issue help wanted
難度 2/5 1-3 小時 新手友好度 88/100
MakazhanAlpamys/Soup#1865 ·
維護者通常 1 天內回覆
-
area: agents area: docs good first issue
難度 2/5 1-3 小時 新手友好度 88/100
openapi/gknowledge#23 ·
-
useless-suppression reported for deprecated-* messages that should be exempt可能已有人在做 @adityasinha-ghub 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 88/100
pylint-dev/pylint#11656 ·
維護者通常 1 天內回覆
-
area:provisioning effort:S priority:P2 provider:local-libvirt risk:daytime-only status:in-progress type:bug
難度 2/5 1-3 小時 新手友好度 78/100
randomparity/kdive#3203 · 2 則留言 ·
維護者通常 1 天內回覆
-
get() for StrEnum可能已有人在做 @FeJo5952 今天認領。 未關閉good first issue
難度 2/5 1-3 小時 新手友好度 72/100
collective/icalendar#1921 · 1 則留言 ·
維護者通常 1 天內回覆