Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

len() of an expression with an empty primary key generates invalid SQL

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

I maintainer di solito rispondono entro 1 giorno

@dimitri-yatsenko ci sta già lavorando.

Dal 17/10/2026.

  • #1605 di @dimitri-yatsenko — aperta

Valutazione

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

Direzione di ricerca

Il bug si trova in QueryExpression.__len__ in expression.py (righe 1163-1174), che segue il ramo a colonna singola quando la chiave primaria è vuota ed emette SELECT count(DISTINCT ). Confrontalo con Aggregation.__len__, che racchiude la query in una sottoquery e funziona. Esegui la riproduzione della issue su dj.U() & File e su una sottoquery ristretta dj.U().aggr(...). È completato quando len() restituisce 1 o 0 in questi casi e i nuovi test coprono tabelle vuote e non vuote.

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

Descrizione

len() raises QuerySyntaxError for any query expression whose primary key is empty and that is not an Aggregation, including dj.U() & table, which the query-algebra spec documents as a single-row result.

Versions: v2.3.4 and master at 68fdea59, MySQL 8.0.

Reproduction

@schema
class File(dj.Manual):
    definition = """
    file_id : int32
    ---
    filename : varchar(255)
    """

File.insert([(1, "a"), (2, "b")])

len(dj.U().aggr(File, n="count(*)"))                        # 1
len(dj.U() & File)                                          # QuerySyntaxError
len(dj.U().aggr(File, n="count(*)").make_subquery())        # QuerySyntaxError
len(dj.U().aggr(File, n="count(*)").make_subquery() & "n = 2")   # QuerySyntaxError

Expected: 1 for each, or 0 when the restriction leaves no row. to_dicts() and bool() on the same expressions work.

Cause

QueryExpression.__len__ (expression.py:1163-1174) picks the single-column branch when len(primary_key) <= 1, so an empty primary key produces:

SELECT count(DISTINCT ) FROM ...

Aggregation.__len__ is not affected, because it counts SELECT count(1) FROM (<subquery>).

Proposed fix

When the primary key is empty, the expression holds at most one row, so return int(bool(result)), or count over SELECT 1 FROM ... LIMIT 1. Add tests for len(dj.U() & T) on empty and non-empty T, and for len() of a restricted dj.U() aggregate subquery.

Related: #1580 (restricting a dj.U() aggregation).

Lingua principale
Python
Stelle
197
Fork
99
Merge medio
2g 10h
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.