len() of an expression with an empty primary key generates invalid SQL
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
此 bug 位於 expression.py(第 1163-1174 行)的 QueryExpression.__len__ 中。當主鍵為空時,它會走單欄分支並產生 SELECT count(DISTINCT )。請將其與 Aggregation.__len__ 比較,後者會把查詢包裝成子查詢,能正常運作。請針對 dj.U() & File 以及受限的 dj.U().aggr(...) 子查詢執行 issue 中的重現步驟。當這些情況下 len() 回傳 1 或 0,且新測試涵蓋空表與非空表時,即視為完成。
由索引模型根據 Issue 內容生成。
描述
len() raises QuerySyntaxError for any query expression whose primary key is empty and that is not an Aggregation, including dj.U() & table, which the query-algebra spec documents as a single-row result.
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"), (2, "b")])
len(dj.U().aggr(File, n="count(*)")) # 1
len(dj.U() & File) # QuerySyntaxError
len(dj.U().aggr(File, n="count(*)").make_subquery()) # QuerySyntaxError
len(dj.U().aggr(File, n="count(*)").make_subquery() & "n = 2") # QuerySyntaxError
Expected: 1 for each, or 0 when the restriction leaves no row. to_dicts() and bool() on the same expressions work.
Cause
QueryExpression.__len__ (expression.py:1163-1174) picks the single-column branch when len(primary_key) <= 1, so an empty primary key produces:
SELECT count(DISTINCT ) FROM ...
Aggregation.__len__ is not affected, because it counts SELECT count(1) FROM (<subquery>).
Proposed fix
When the primary key is empty, the expression holds at most one row, so return int(bool(result)), or count over SELECT 1 FROM ... LIMIT 1. Add tests for len(dj.U() & T) on empty and non-empty T, and for len() of a restricted dj.U() aggregate subquery.
Related: #1580 (restricting a dj.U() aggregation).
- 主要語言
- 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 小時 新手友好度 76/100
datajoint/datajoint-python#1539 · 3 則留言 ·
維護者通常 1 天內回覆
-
_job_start_time records the populating host's local time, with no zone可能已有人在做 @dimitri-yatsenko 今天認領。 未關閉
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 天內回覆
-
難度 3/5 1-2 天 新手友好度 40/100
datajoint/datajoint-python#1580 ·
維護者通常 1 天內回覆
-
A column copied by a foreign key loses the parent's core type (int32 becomes int)可能已有人在做 @dimitri-yatsenko 於 3 天前認領。 未關閉
難度 3/5 半天 新手友好度 25/100
datajoint/datajoint-python#1578 ·
維護者通常 1 天內回覆
查看 datajoint/datajoint-python 的全部 Issue
相似的 Issue
-
enhancement from-review
難度 2/5 1-3 小時 新手友好度 78/100
khuisman/mcp-gee-sweet#1010 ·
維護者通常 1 天內回覆
-
bug confirmed issue
難度 2/5 1-3 小時 新手友好度 72/100
open-webui/open-webui#32333 · 2 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
ll7/robot_sf_ll7#10368 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 62/100
-
難度 2/5 1-3 小時 新手友好度 68/100
flop-labs/technocore-chat#982 ·
維護者通常 1 天內回覆