Union does not check lineage: unrelated tables whose keys share a name are merged

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

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

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

評価

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

調査の方向性

Union.create から始め、これは src/datajoint/expression.py にあり、assert_join_compatibility を呼び出す join の経路と比較してください。この呼び出しは src/datajoint/condition.py にあります。スニペットを再現し、その後、同名のものをマージする前に union が同じ lineage チェックを実行するようにして、キーのみ、セカンダリ属性、相同対照のケースを tests/integration/test_semantic_matching.py に追加してください。完了とは、lineage が一致しない union が join と同じエラーを発生させ、一方で相同な union は引き続き正しく取得できることを意味します。

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

説明

bug

Summary

A + B never checks that namesake attributes share a lineage. The join of the same operands is rejected by the semantic check, but the union is accepted and merges unrelated entities whose keys happen to have the same name.

Union.create checks the connection, the primary-key names, and the overlap of secondary attributes, but it never calls assert_join_compatibility:

Reproduction (master @ 207068d, MySQL 8.0)

@schema
class Mouse(dj.Manual):
    definition = """
    id : int32
    ---
    mouse_name : varchar(32)
    """

@schema
class Probe(dj.Manual):
    definition = """
    id : int32
    ---
    probe_serial : varchar(32)
    """

Mouse.insert([(1, "alpha"), (2, "beta")])
Probe.insert([(2, "P-0002"), (3, "P-0003")])

Mouse * Probe                              # rejected: different lineages
(Mouse.proj() + Probe.proj()).to_dicts()   # accepted

The join is rejected:

DataJointError: Cannot join on attribute `id`: different lineages (djbugs_union.mouse.id vs djbugs_union.probe.id). Use .proj() to rename one of the attributes.

Mouse & Probe and Mouse.proj() * Probe.proj() are rejected with the same error. The union is accepted:

[{'id': 1}, {'id': 2}, {'id': 3}]
SELECT * FROM ((SELECT `id` FROM `djbugs_union`.`mouse`) UNION (SELECT `id` FROM `djbugs_union`.`probe`)) as `_u2`

Further consequences:

  • Order-dependent lineage. The result's id takes the lineage of the first operand: mouse.id for Mouse.proj() + Probe.proj(), probe.id when the operands are swapped.
  • Probe ids pass as mouse ids. The union then passes the semantic check against Mouse: (Mouse.proj() + Probe.proj()) - Mouse returns [{'id': 3}], a probe id presented as a mouse id.
  • Late failure with secondary attributes. Mouse + Probe is constructed without error and fails only at fetch, when make_sql runs its internal left join (expression.py#L1469 → join → assert_join_compatibility).

Expected

A + B should reject non-homologous namesakes when the expression is built, as A * B and A & B do. That is what the library and the docs state:

  • lineage.py: "Union (A + B): requires all namesakes to have matching lineage"
  • datajoint-docs reference/specs/semantic-matching.md (§ A + B) and explanation/semantic-matching.md (operator table: Union, "Namesakes must be homologous")

Proposed fix

Call assert_join_compatibility(arg1, arg2) in Union.create, and add union cases to tests/integration/test_semantic_matching.py, which has none today:

  • key-only operands;
  • operands with secondary attributes;
  • a homologous control case that must still work.
主要言語
Python
スター
198
フォーク
103
平均マージ
5日 1時間
マージ済み PR(30日)
12

環境構築

はじめの一歩

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

datajoint/datajoint-python のほかの issue

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

似ている issue

Python の issue をもっと見る

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

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