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

Aperta Adatta ai principianti
#1,747 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
mysql, python
Ambito
databases, testing

Direzione di ricerca

Parti da Union.create in src/datajoint/expression.py e confrontalo con il percorso di join che chiama assert_join_compatibility in src/datajoint/condition.py. Riproduci lo snippet, poi fai in modo che union esegua lo stesso controllo di lineage prima di unire omonimi e aggiungi casi di sola chiave, di attributo secondario e di controllo omologo a tests/integration/test_semantic_matching.py. Fatto significa che le union con lineage non corrispondente sollevano lo stesso errore dei join, mentre le union omologhe recuperano ancora correttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
Python
Stelle
198
Fork
103
Merge medio
5g 1h
PR unite (30g)
12

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di datajoint/datajoint-python

Tutte le issue di datajoint/datajoint-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.