ClickHouse aggregate-alias rewrite changes column bindings inside scalar subqueries

Aperta Adatta ai principianti
#2,885 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 4 giorni

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Inizia in core/wren/src/wren/mdl/clickhouse_compat.py, leggi translate e il controllo di scope dell'aggregato esterno intorno alle righe 263–282, poi esegui lo script di riproduzione dell'issue. Il punto chiave è che agg.find_all(exp.Column) scende nei SELECT annidati, quindi la riscrittura deve fermarsi allo scope SELECT proprio dell'aggregato. Fatto significa che la c interna resta legata a u mentre l'alias dell'aggregato esterno viene ancora gestito; aggiungi o esegui un test di regressione per l'esempio.

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

Descrizione

Hullo folks,

I’m doing some research for Tessl Code Review and ran it against your repo. It flagged a potential issue, and I figured it made sense to share it with you:

Describe the bug

The aggregate-alias compatibility rewrite descends into nested SELECTs. When an outer aggregate is aliased as c, it qualifies an unqualified c inside a scalar subquery with the outer table, even though that column belongs to the subquery's table.

This is a separate case from #2782: the alias fix introduced by #2783 is useful for the outer aggregate, but should not change a child SELECT's column binding.

To reproduce

With the current core/wren/src/wren/mdl/clickhouse_compat.py and SQLGlot 29.0.1:

from sqlglot import parse_one
from wren.mdl import clickhouse_compat

ast = parse_one(
    "SELECT t.id, SUM((SELECT c FROM u WHERE u.id = t.id)) AS c "
    "FROM t GROUP BY t.id"
)
print(ast.sql(dialect="clickhouse"))
clickhouse_compat.translate(ast)
print(ast.sql(dialect="clickhouse"))

Observed output from executing the current module directly:

-- Before
SELECT t.id, SUM((SELECT c FROM u WHERE u.id = t.id)) AS c FROM t GROUP BY t.id
-- After
SELECT t.id, SUM((SELECT t.c FROM u WHERE u.id = t.id)) AS c FROM t GROUP BY t.id

For tables t(id) and u(id, c), the generated t.c does not exist. If t also has a c column, this instead changes which value is aggregated.

Expected behavior

Preserve the inner c binding to u. The qualifier rewrite should only visit columns belonging to the aggregate's own SELECT scope. The current outer-aggregate scope check excludes nested aggregates, but its subsequent agg.find_all(exp.Column) still visits columns inside nested SELECTs.

Wren AI information and validation

Checked source 48de0b6cbd02cbbe3753f29e37f4be24e8a9a7fd (core/wren 0.18.0), Python 3.14.2, SQLGlot 29.0.1 on macOS arm64. The reproduction directly executes the current compatibility module and confirms the AST/output rewrite. It does not launch WrenEngine or a ClickHouse server.

The finding came from a retrospective review of merged PR #2783 and remains reproducible at this commit.

Full transparency: I work at Tessl, and we've spotted that using Tessl Code Review - using repository-level context and specific review lenses that improve over time through feedback. That’s how this finding surfaced.

We’re just getting started and want to offer the tool free to open-source projects. If this is useful, happy to give you access.

Bap

Lingua principale
Python
Stelle
17.8k
Fork
2k
Merge medio
5g 5h
PR unite (30g)
21

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 Canner/WrenAI

Tutte le issue di Canner/WrenAI

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.