Restricting by a dj.Top expression fails on MySQL (error 1235)

Đang mở
#1,669 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 6 ngày

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

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
62/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, postgresql, python, sql
Lĩnh vực
databases

Hướng nghiên cứu

Start at make_condition in condition.py around line 455, where a query-expression restriction is rendered as an IN subquery, and check how a _top adds ORDER BY and LIMIT to that SQL. Run the reproducer from the issue against MySQL 8.0 and PostgreSQL 16 to confirm the error 1235 on MySQL and the count of 6 on PostgreSQL. Done when the query returns 6 on both backends, with the MySQL-compatible form the issue describes (a derived table) as one option.

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

Mô tả

bug

Using a dj.Top expression as a restriction (S & (T & dj.Top(...))) fails on MySQL, which does not support LIMIT inside an IN subquery. PostgreSQL returns the correct result.

Reproduced on master @ 3c912c46 against MySQL 8.0 and PostgreSQL 16. Connection via DJ_HOST/DJ_USER/DJ_PASS/DJ_BACKEND. Found while benchmarking subquery pruning (#1665).

Reproducible example
import datajoint as dj

schema = dj.Schema("repro_top")


@schema
class T(dj.Manual):
    definition = """
    t_id : int32
    ---
    x : int32
    y : int32
    """


@schema
class S(dj.Manual):
    definition = """
    -> T
    s_id : int32
    """


T.insert([(i, i, 100 - i) for i in range(10)], skip_duplicates=True)
S.insert([(i, j) for i in range(10) for j in range(2)], skip_duplicates=True)

q = S & (T & dj.Top(3, "x"))  # S rows whose T is among the 3 with the smallest x
print(len(q.to_dicts()))  # expected: 6
Actual

MySQL:

pymysql.err.NotSupportedError: (1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")

PostgreSQL: 6 (correct).

Expected

6 on both backends.

Cause

make_condition (condition.py:455) renders a query-expression restriction as (attrs) IN (<condition.make_sql(attrs)>). When the condition has a _top, its SQL ends in ORDER BY ... LIMIT n, which MySQL rejects inside IN.

The usual workaround is to wrap it in a derived table, IN (SELECT ... FROM (<subquery with LIMIT>) AS x). MySQL accepts that form, and it is equivalent on PostgreSQL.

Ngôn ngữ chính
Python
Star
198
Fork
103
Merge trung bình
6 ngày 1 giờ
Pull request đã merge (30 ngày)
10

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.