Restricting a union by dj.Top produces invalid SQL

Aperta
#1,667 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 6 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
mysql, postgresql, python
Ambito
databases

Direzione di ricerca

Start in Union.make_sql in expression.py around line 1437, where the derived-table alias is built as f"_u{n}{self.sorting_clauses()}", so ORDER BY and LIMIT land inside the quoted identifier. Run the reproducer from the issue on MySQL or PostgreSQL to see the syntax error. Move the sorting and limit clauses after the alias, and also make the secondary-attribute branch (around lines 1444-1446) honor _top. Done when (A + B) & dj.Top(3, "t_id DESC") returns [7, 8, 9] on both backends.

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

Descrizione

bug

(A + B) & dj.Top(...) fails with an SQL syntax error on both backends.

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_union_top")


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


@schema
class A(dj.Manual):
    definition = """
    -> T
    """


@schema
class B(dj.Manual):
    definition = """
    -> T
    """


T.insert([(i,) for i in range(10)], skip_duplicates=True)
A.insert([(i,) for i in range(0, 6)], skip_duplicates=True)
B.insert([(i,) for i in range(4, 10)], skip_duplicates=True)

q = (A + B) & dj.Top(3, "t_id DESC")
print(sorted(r["t_id"] for r in q.to_dicts()))  # expected: [7, 8, 9]
Actual

MySQL:

datajoint.errors.QuerySyntaxError: You have an error in your SQL syntax; ... near 't_id` DESC LIMIT 3`' at line 1

PostgreSQL:

datajoint.errors.QuerySyntaxError: syntax error at or near "t_id"
LINE 1: ...d" FROM "repro_union_top"."b")) as "_u0 ORDER BY "t_id" DESC...
Expected

[7, 8, 9].

Cause

Union.make_sql (expression.py:1437) builds the derived-table alias as f"_u{n}{self.sorting_clauses()}". ORDER BY ... LIMIT therefore ends up inside the quoted alias identifier. The ORDER BY/LIMIT should go after the alias instead.

The union form with secondary attributes (:1444-1446) ignores _top altogether.

Lingua principale
Python
Stelle
198
Fork
103
Merge medio
6g 1h
PR unite (30g)
10

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.