A rename after a join, aggr, or union produces invalid SQL on PostgreSQL

未關閉
#1,676 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 6 天內回覆

還沒有人認領這個 Issue。

評估

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

研究方向

Start in src/datajoint/heading.py at Heading.join() and read how Heading.select() uses table_info to quote renamed attributes. Check the existing regression tests for joins, projections, aggregation, and union, then reproduce the examples on PostgreSQL and MySQL. Done means renames after each listed operation produce valid SQL and return correct rows on both backends.

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

描述

bug

A rename after a join, a left join, an aggr, or a union produces SQL that mixes backticks into PostgreSQL identifiers, and the query fails with syntax error at or near "as". MySQL is not affected.

This is the follow-up raised in the reviews of #1643. #1643 keeps table_info in Heading.set_primary_key, and #1657 keeps it in Heading.make_subquery_heading. Heading.join() (src/datajoint/heading.py, the return Heading(attrs, lineage_available=...) line) still builds the joined heading without table_info. A later proj() rename therefore reaches Heading.select with no adapter and falls back to a backtick-quoted name. aggr (through join.heading.set_primary_key) and Union (through arg1.heading.join(arg2.heading)) inherit the joined heading, so they fail the same way.

Reproducible example (PostgreSQL)
@schema
class A(dj.Manual):
    definition = """
    a : int32
    ---
    x : int32
    """


@schema
class B(dj.Manual):
    definition = """
    -> A
    k : int32
    ---
    b : int32
    """


A.insert([(1, 10), (2, 20), (3, 30)])
B.insert([(1, 1, 100), (1, 2, 101), (2, 1, 200)])
Query SQL on PostgreSQL
(A * B).proj(c="b") SELECT "a","k",`b` as "c" FROM ... JOIN ... USING ("a")
B.join(A, left=True).proj(c="x") SELECT "a","k",`x` as "c" FROM ... LEFT JOIN ...
A.aggr(B, "x", n="count(*)").proj(xx="x") SELECT "a",`x` as "xx" FROM ... GROUP BY "a"
A.aggr(B, n="count(*)").proj(m="n") SELECT "a",`n` as "m" FROM (SELECT ... GROUP BY "a") as "$c"
((B & "a=1").proj() + (B & "a=2").proj()).proj(kk="k") SELECT "a",`k` as "kk" FROM (... UNION ...)

Every one raises QuerySyntaxError: syntax error at or near "as" on PostgreSQL 15, on master and on master with #1643 and #1657 applied. All return the correct rows on MySQL.

Suggested fix

Carry table_info through the join, as Heading.select already does:

return Heading(attrs, table_info=self.table_info or other.table_info, lineage_available=...)

With this one-line change, all the cases above pass on both backends. A regression test parametrized over both backends should cover a rename after *, a left join, aggr (both a grouped attribute and a computed one), a union, and a subquery over a join.

主要語言
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 摘要。