Restricting a union by dj.Top produces invalid SQL

オープン
#1,667 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 6 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
半日
初心者へのやさしさ
75/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
mysql, postgresql, python
領域
databases

調査の方向性

Start in Union.make_sql in expression.py around line 1437, where the derived-table alias is built as f"_u{n}{self.sorting_clauses()}", so ORDER BY and LIMIT land inside the quoted identifier. Run the reproducer from the issue on MySQL or PostgreSQL to see the syntax error. Move the sorting and limit clauses after the alias, and also make the secondary-attribute branch (around lines 1444-1446) honor _top. Done when (A + B) & dj.Top(3, "t_id DESC") returns [7, 8, 9] on both backends.

索引モデルが issue の本文から書いたものです。

説明

bug

(A + B) & dj.Top(...) fails with an SQL syntax error on both backends.

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_union_top")


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


@schema
class A(dj.Manual):
    definition = """
    -> T
    """


@schema
class B(dj.Manual):
    definition = """
    -> T
    """


T.insert([(i,) for i in range(10)], skip_duplicates=True)
A.insert([(i,) for i in range(0, 6)], skip_duplicates=True)
B.insert([(i,) for i in range(4, 10)], skip_duplicates=True)

q = (A + B) & dj.Top(3, "t_id DESC")
print(sorted(r["t_id"] for r in q.to_dicts()))  # expected: [7, 8, 9]
Actual

MySQL:

datajoint.errors.QuerySyntaxError: You have an error in your SQL syntax; ... near 't_id` DESC LIMIT 3`' at line 1

PostgreSQL:

datajoint.errors.QuerySyntaxError: syntax error at or near "t_id"
LINE 1: ...d" FROM "repro_union_top"."b")) as "_u0 ORDER BY "t_id" DESC...
Expected

[7, 8, 9].

Cause

Union.make_sql (expression.py:1437) builds the derived-table alias as f"_u{n}{self.sorting_clauses()}". ORDER BY ... LIMIT therefore ends up inside the quoted alias identifier. The ORDER BY/LIMIT should go after the alias instead.

The union form with secondary attributes (:1444-1446) ignores _top altogether.

主要言語
Python
スター
198
フォーク
103
平均マージ
6日 1時間
マージ済み PR(30日)
10

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

datajoint/datajoint-python のほかの issue

datajoint/datajoint-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。