keys() and to_arrays(attr) fail on an aggregation with dj.Top ordered by an aggregate
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
- 65/100
Hướng nghiên cứu
Start by running the reproduction against a local backend to see keys() and to_arrays("t_id") fail while to_dicts() succeeds. The cause points at Aggregation.make_sql(fields) around expression.py:1355: read how the select list is built versus how the ORDER BY and HAVING clauses are carried through, including the PostgreSQL HAVING wrapping path. Done means the ORDER BY attributes and HAVING aliases are always selected inside the aggregation and the requested fields are projected by an outer SELECT.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
An aggregation restricted by dj.Top and ordered by a computed attribute works with to_dicts(), to_arrays() and len(). It fails with keys() and to_arrays("attr") whenever the ordering attribute is not among the selected fields.
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 = T.aggr(S, n="count(*)") & dj.Top(3, "n DESC, t_id")
print(q.to_dicts()) # works
print(len(q)) # works
print(q.keys()) # fails
print(q.to_arrays("t_id")) # fails
Actual
MySQL:
[{'t_id': 0, 'n': 2}, {'t_id': 1, 'n': 2}, {'t_id': 2, 'n': 2}]
3
datajoint.errors.UnknownAttributeError: Unknown column 'n' in 'order clause'
PostgreSQL:
datajoint.errors.UnknownAttributeError: column "n" does not exist
LINE 1: ..._top"."s" USING ("t_id") GROUP BY "t_id" ORDER BY "n" DESC, ...
SQL generated for q.proj():
SELECT DISTINCT `t_id` FROM `repro_top`.`t` LEFT JOIN `repro_top`.`s` USING (`t_id`) GROUP BY `t_id` ORDER BY `n` DESC, `t_id` LIMIT 3
Expected
keys() returns the 3 keys, and to_arrays("t_id") returns an array of 3 ids.
Cause
Fetching specific fields calls Aggregation.make_sql(fields) (expression.py:1355), which selects only those fields. Its ORDER BY, however, still names the aggregate alias n. Once n is not in the select list, the alias no longer exists. The PostgreSQL path wraps the aggregation for HAVING, but the same problem applies there.
Fix: always select the ORDER BY attributes (and HAVING aliases) inside the aggregation. When the requested fields differ, wrap the aggregation in an outer SELECT <fields>.
- 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
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của datajoint/datajoint-python
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
datajoint/datajoint-python#1703 ·
Maintainer thường phản hồi trong vòng 6 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
datajoint/datajoint-python#1702 ·
Maintainer thường phản hồi trong vòng 6 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
datajoint/datajoint-python#1701 ·
Maintainer thường phản hồi trong vòng 6 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
datajoint/datajoint-python#1691 ·
Maintainer thường phản hồi trong vòng 6 ngày
-
dependencies needs-discussion
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
datajoint/datajoint-python#1687 ·
Maintainer thường phản hồi trong vòng 6 ngày
Tất cả issue của datajoint/datajoint-python
Issue tương tự
-
needs-triage
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 85/100
gke-labs/kube-agents#6620 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
open-webui/open-webui#34719 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
cognition
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
One non-object JSON record makes the session catalog exitCó thể đã có người làm @he-yufeng đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[Suggestion]: Geonode MCP ServerĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
collabnix/awesome-mcp-lists#433 ·
Maintainer thường phản hồi trong vòng 4 ngày