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

Offen Anfängerfreundlich
#1,747 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 5 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
78/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
mysql, python
Bereich
databases, testing

Rechercherichtung

Beginne bei Union.create in src/datajoint/expression.py und vergleiche es mit dem Join-Pfad, der assert_join_compatibility in src/datajoint/condition.py aufruft. Reproduziere das Snippet und veranlasse dann, dass union dieselbe Lineage-Prüfung durchführt, bevor Namensvetter zusammengeführt werden, und füge Fälle für nur Schlüssel, Sekundärattribut und homologe Kontrolle zu tests/integration/test_semantic_matching.py hinzu. Fertig bedeutet, dass Unions mit nicht übereinstimmender Lineage denselben Fehler wie Joins auslösen, während homologe Unions weiterhin korrekt abrufen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.
Vorherrschende Sprache
Python
Sterne
198
Forks
103
Ø Merge
5 T. 1 Std.
Gemergte PRs (30 T.)
12

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus datajoint/datajoint-python

Alle Issues in datajoint/datajoint-python

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.