bool() ignores dj.Top

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

維護者通常 6 天內回覆

還沒有人認領這個 Issue。

評估

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

研究方向

從 expression.py 中的 QueryExpression.__bool__(約第 1178 行)開始,並與 __len__(約第 1158 行)比較,後者在設定了 self._top 時已將查詢包裝在子查詢中。使用 DJ_HOST/DJ_USER/DJ_PASS/DJ_BACKEND 針對 MySQL 或 PostgreSQL 執行重現腳本,以確認 0/True 不一致的問題,然後檢查超出結尾的 offset 與 limit 為 0 時的 bool(q) == (len(q) > 0)。當現有的查詢表達式測試仍然通過,且兩個後端對該範例都回傳 False 時,即完成。

由索引模型根據 Issue 內容生成。

描述

bug

bool(q) returns True for an empty dj.Top result, because it evaluates the query without its limit and offset.

Reproduced on master @ 3c912c46 against MySQL 8.0 and PostgreSQL 16. Connection via DJ_HOST/DJ_USER/DJ_PASS/DJ_BACKEND. Found while benchmarking subquery pruning (#1665).

Reproducible example
import datajoint as dj

schema = dj.Schema("repro_top")


@schema
class T(dj.Manual):
    definition = """
    t_id : int32
    ---
    x : int32
    y : int32
    """


@schema
class S(dj.Manual):
    definition = """
    -> T
    s_id : int32
    """


T.insert([(i, i, 100 - i) for i in range(10)], skip_duplicates=True)
S.insert([(i, j) for i in range(10) for j in range(2)], skip_duplicates=True)

q = T & dj.Top(limit=5, offset=20, order_by="x")  # offset past the end of 10 rows
print(len(q.to_dicts()))  # 0
print(bool(q))  # expected: False
Actual

Both backends:

0
True
Expected

False: the query returns no rows. More generally, bool(q) == (len(q) > 0).

Cause

QueryExpression.__bool__ (expression.py:1178) runs SELECT EXISTS(SELECT 1 FROM <from_clause><where_clause>). It ignores self._top, so an OFFSET past the end (or a LIMIT of 0) is never applied. __len__ handles this case correctly by wrapping the query in a subquery when _top is set (:1158), and __bool__ should do the same.

主要語言
Python
星號
198
分支
103
平均合併
6 天 1 小時
30 天內合併 PR
10

環境準備

  • 提供 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 摘要。