#8231·turso

LEFT JOIN emits unmatched rows without applying a WHERE equality that spans both tables

Author: LeMikaelFCreated Aug 5, 2026Updated Sep 30, 2026
Labelsoptimizercorrectnessjoins

Description

An equality WHERE term of the form <expression over the right table> = <expression over the left table> is promoted to a hash-join key. For a LEFT JOIN that turns the term into part of the match condition instead of a filter, so null-extended rows are emitted without it ever being applied.

Reproducer

sql
CREATE TABLE o(a);
CREATE TABLE p(a, b);
INSERT INTO o VALUES (0);
INSERT INTO p VALUES (2, 2);

SELECT o.a, p.a, p.b FROM o LEFT JOIN p ON o.a = p.a WHERE coalesce(p.b, 1) = o.a;
-- Turso:  0||
-- SQLite: (no rows)

On the null-extended row coalesce(NULL, 1) is 1 and o.a is 0, so the WHERE clause is false. Turso computes exactly that and emits the row anyway:

sql
SELECT o.a, p.b, coalesce(p.b, 1), coalesce(p.b, 1) = o.a FROM o LEFT JOIN p ON o.a = p.a;
-- both engines: 0||1|0

Matched rows are filtered correctly; only the unmatched ones leak through.

Which shapes are affected

Same schema and data, varying only the WHERE clause:

WHERE clause Turso SQLite
coalesce(p.b, 1) = o.a 0| (no rows)
o.a = coalesce(p.b, 1) 0| (no rows)
coalesce(p.b, 1) + 0 = o.a 0| (no rows)
CASE WHEN p.b IS NULL THEN 1 ELSE p.b END = o.a 0| (no rows)
coalesce(p.b, 1) < o.a (no rows) (no rows)
coalesce(p.b, 1) = 0 (no rows) (no rows)
ifnull(p.b, o.c) = 999 (no rows) (no rows)

The break needs = specifically, and each side must reference exactly one of the two tables. Writing the query as p RIGHT JOIN o gives the same wrong row.

Bytecode

For the reproducer, the coalesce is computed into the probe key register rather than evaluated as a filter, and the unmatched-row path jumps straight to the output subroutine:

16|ColumnRange|1|0|7|2|   r[7..8]=p.a..b
17|NotNull|8|19|0|        r[8]!=NULL -> goto 19
18|Integer|1|8|0|         r[8]=1              <- coalesce(p.b,1) built into the join key
19|Affinity|7|2|0|
...
34|NullRow|1|0|0|         Set cursor 1 to a (pseudo) NULL row
35|HashScanUnmatched|1|12|40|
36|SeekRowid|0|12|40|
37|Gosub|13|26|0|         <- ResultRow subroutine, no condition evaluated

Where

find_equijoin_conditions (core/translate/optimizer/access_method.rs:1027-1080) accepts any unconsumed = term whose two sides reference one table each, without checking where_term.from_outer_join or whether the join is an outer join. A plain WHERE term has to be applied after null-extension, so it cannot serve as the join key of an outer join. mark_seek_constraints_consumed (core/translate/optimizer/mod.rs:2831) carries exactly that guard; this path does not.

Found with a differential fuzzer against SQLite 3.54.0. main @ bc62e487, debug build.

Related but distinct: #8220 covers FULL OUTER JOIN chains of three or more tables where the filter sits on a build-side table; this one is a two-table LEFT JOIN filtering the null-extended side.


This issue brought to you by Mikaël and Claude Code.