#7700·questdb

Outer join whose ON clause reads CROSS-joined tables fails with Invalid column

Author: bluestreak01Created Sep 30, 2026Updated Sep 30, 2026

Describe the bug

An outer join (LEFT, RIGHT or FULL) whose ON clause references columns of two earlier tables fails to compile when those two earlier tables are not joined to each other by an equi-key (a CROSS JOIN, a comma join, or a non-equi INNER join). The query is valid, but QuestDB rejects it with Invalid column: <earlier table>.<column>. EXPLAIN fails the same way.

The optimiser runs the context-free earlier table (c below) after the outer join. SqlCodeGenerator.generateJoins then compiles the outer join's ON clause (compileJoinFilter(slaveModel.getOuterJoinExpressionClause(), ...)) against join metadata that does not contain c yet, and FunctionParser throws Invalid column.

On master e756069d95 the equi-key forms (d.k = c.k AND d.k = a.k) failed with AssertionError: wtf? c.k in generateSelectChoose under -ea (an ArrayIndexOutOfBoundsException without -ea) and leaked the hash-join map (16,640 bytes of NATIVE_JOIN_MAP per attempt). Branch fix/join-optimiser-lost-rows (b746306b14) changes that into the Invalid column SqlException below and no longer leaks. The form with a non-key predicate (d.k + 0 = a.k) fails with Invalid column: a.k on both revisions.

To reproduce

sql
CREATE TABLE a (k INT, id INT);
INSERT INTO a VALUES (1, 1), (2, 2);
CREATE TABLE c (k INT, id INT);
INSERT INTO c VALUES (1, 10), (3, 30);
CREATE TABLE d (k INT, id INT);
INSERT INTO d VALUES (1, 100), (2, 200), (3, 300);

-- all fail with "Invalid column: c.k" (or a.k)
SELECT * FROM a CROSS JOIN c LEFT JOIN d ON d.k = c.k AND d.k = a.k ORDER BY a.id, c.id;
SELECT * FROM a CROSS JOIN c LEFT JOIN d ON d.k = a.k AND d.k = c.k ORDER BY a.id, c.id;
SELECT * FROM a, c LEFT JOIN d ON d.k = c.k AND d.k = a.k ORDER BY a.id, c.id;
SELECT * FROM a JOIN c ON c.id > a.id LEFT JOIN d ON d.k = c.k AND d.k = a.k ORDER BY a.id, c.id;
SELECT * FROM a CROSS JOIN c RIGHT JOIN d ON d.k = c.k AND d.k = a.k ORDER BY d.id;
SELECT * FROM a CROSS JOIN c FULL JOIN d ON d.k = c.k AND d.k = a.k ORDER BY d.id, a.id, c.id;
SELECT * FROM a CROSS JOIN c LEFT JOIN d ON d.k = c.k AND d.k + 0 = a.k ORDER BY a.id, c.id;

The sub-query form compiles and returns the expected result for the first query:

sql
SELECT * FROM (SELECT a.k ak, a.id aid, c.k ck, c.id cid FROM a CROSS JOIN c) t
LEFT JOIN d ON d.k = t.ck AND d.k = t.ak ORDER BY t.aid, t.cid;
ak  aid  ck  cid  k     id
1   1    1   10   1     100
1   1    3   30   null  null
2   2    1   10   null  null
2   2    3   30   null  null

Expected fix direction

SqlOptimiser.doReorderTables appends a model without a join context and without dependencies (the CROSS JOIN c) after every model it can order. An outer join must run after every earlier table its ON clause references. Record an ordering edge from each table the outer join's ON clause (key or outerJoinExpressionClause) references to the outer join, or pin every model before the outer join to run before it. The RIGHT/FULL prefix pinning proposed for "equi RIGHT/FULL join runs before an earlier CROSS JOIN" would cover the RIGHT/FULL forms but not LEFT.

QuestDB version

master (e756069d95), and branch fix/join-optimiser-lost-rows (b746306b14)

Status on #7674

On the branch of #7674 (fix/join-optimiser-lost-rows), the plain-key forms and the RIGHT/FULL forms above return rows. LEFT joins whose ON clause compares an earlier CROSS, comma or non-equi-joined table through a non-key condition still fail (checked at db69fb3f4a), for example:

sql
CREATE TABLE f0 (a0 INT, b0 INT);
CREATE TABLE f1 (a1 INT, b1 INT);
CREATE TABLE f2 (a2 INT, b2 INT);
CREATE TABLE f3 (a3 INT, b3 INT);
SELECT * FROM f0 CROSS JOIN f1 JOIN f2 ON b2 = a0 LEFT JOIN f3 ON a3 = a0 AND b3 >= a1;
-- Invalid column: a1

Two fixes that add ordering edges from such conditions were tried and rejected in review: an edge can make an UNNEST the first model of the join order (#7693), and a join can count as ordered after its own edge was dropped, which lets a later join run before a table its ON clause reads.