Wrong results: COALESCE on a volatile operand evaluates it two times, and fails when the output is declared non-nullable
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 55/100
Direzione di ricerca
Start in datafusion/functions/src/core/coalesce.rs, especially simplify and invoke_with_args, then compare the physical-expression approach described in pull request 25476. Reproduce the query with datafusion-cli and inspect the EXPLAIN output. Done means the volatile operand is evaluated once per row and the query returns only 1 or -1 without Arrow nullability errors.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
COALESCE on a volatile operand evaluates the operand two times. The null test and the returned value are two different draws of random(). When the last argument is a non-null literal, the planner declares the output column non-nullable, and the query fails at run time with an Arrow error. With a nullable last argument the query completes and returns rows where COALESCE returned NULL, which is not possible for a single evaluation.
The cause is the rewrite in datafusion/functions/src/core/coalesce.rs: simplify turns coalesce(a, b) into CASE WHEN a IS NOT NULL THEN a ELSE b END, which names a two times. The rewrite cannot be skipped for a volatile a, because coalesce has no runtime kernel: invoke_with_args returns an internal error that says the function must be simplified first.
This is the same mechanism as https://github.com/apache/datafusion/issues/25457 (BETWEEN), which https://github.com/apache/datafusion/pull/25476 fixes with a physical expression that evaluates the operand one time. COALESCE needs its own fix, either a runtime kernel for the volatile case or a rewrite that binds a one time.
To Reproduce
main at 0e292dcbfd, and the leaf-pushdown integration branch, with datafusion-cli:
CREATE TABLE v AS SELECT value AS a FROM generate_series(1, 100000);
SELECT count(*), count(c) FROM (SELECT coalesce(nullif(floor(random() * 2), 0), -1) AS c FROM v);
Error: Arrow error: Invalid argument error: Column 'c' is declared as non-nullable but contains null values
EXPLAIN shows the two evaluations:
ProjectionExec: expr=[CASE WHEN nullif(floor(random() * 2), 0) IS NOT NULL THEN nullif(floor(random() * 2), 0) ELSE -1 END as c]
Expected behavior
The query completes. c is 1 for about half of the rows and -1 for the other half, and it is never NULL, because random() is evaluated one time per row.
DuckDB 1.5.2 is not a reference for this shape: it also re-evaluates the volatile operand, and its counts for the same query do not add up to the row count.
Additional context
Found while fixing https://github.com/apache/datafusion/issues/25457. The evaluation-site invariant in https://github.com/apache/datafusion/pull/25458 reports this shape.
Tracked in the leaf-pushdown EPIC: https://github.com/apache/datafusion/issues/25459
- Lingua principale
- Rust
- Stelle
- 9.3k
- Fork
- 2.4k
- Merge medio
- 3g 8h
- PR unite (30g)
- 354
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di apache/datafusion
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
apache/datafusion#25266 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
apache/datafusion#25213 · 1 commento ·
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
apache/datafusion#25168 · 1 commento ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
apache/datafusion#24998 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
apache/datafusion#24913 ·
Tutte le issue di apache/datafusion
Issue simili
-
has_tail_capacity wraps and get_writable_raw_unchecked commits raw_len before the bounds check Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
stratum-mining/stratum#2404 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
Axis areas are always keyboard-focusable (Sense::drag), even with allow_axis_zoom_drag(false) Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
bug team:backend track:services-maintenance
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
cowprotocol/services#4950 ·