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

Đang mở Phù hợp với người mới
#1,747 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 5 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
mysql, python
Lĩnh vực
databases, testing

Hướng nghiên cứu

Bắt đầu từ Union.create trong src/datajoint/expression.py và so sánh nó với đường dẫn join gọi assert_join_compatibility trong src/datajoint/condition.py. Tái hiện đoạn mã, sau đó làm cho union thực hiện cùng kiểm tra lineage trước khi hợp nhất các mục trùng tên và thêm các trường hợp chỉ có khóa, thuộc tính phụ và đối chứng tương đồng vào tests/integration/test_semantic_matching.py. Hoàn tất nghĩa là các union có lineage không khớp ném cùng lỗi như join, trong khi các union tương đồng vẫn truy xuất đúng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
Python
Star
198
Fork
103
Merge trung bình
5 ngày 1 giờ
Pull request đã merge (30 ngày)
12

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của datajoint/datajoint-python

Tất cả issue của datajoint/datajoint-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.