Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

len() of an expression with an empty primary key generates invalid SQL

未關閉 適合新手
#1,581 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
70/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
mysql, python
領域
databases

研究方向

此 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 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

datajoint/datajoint-python 的其他 Issue

查看 datajoint/datajoint-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。