#7701·questdb

Join ON clause can reference a table joined later in the FROM clause

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

Describe the bug

QuestDB accepts a join ON clause that references a table joined later in the same FROM clause. PostgreSQL rejects such a query with "invalid reference to FROM-clause entry". QuestDB either compiles it or fails with an Invalid column error that points at the forward reference, and some shapes return wrong rows without an error.

  • INNER JOIN forward references work when no RIGHT/FULL OUTER join follows: the optimiser moves the ON conjunct into the WHERE filter.
  • LEFT JOIN (and other outer/ASOF) forward references only work when the LEFT JOIN happens to run after the referenced table. doReorderTables appends a context-free model to the end of the order (SqlOptimiser.java:3888-3895, :3935-3938 on branch fix/join-optimiser-lost-rows), and the LEFT JOIN's ON clause is compiled at its own position. In a differential fuzz run over 165 random chains with a LEFT JOIN forward reference and an equi RIGHT/FULL join, 110 fail with Invalid column on master.
  • A prefix ON clause that references the RIGHT/FULL OUTER join's slave has no defined meaning, because the outer join's master is the prefix.

To reproduce

sql
CREATE TABLE f0 (a0 INT, b0 INT);
INSERT INTO f0 VALUES (1, 1), (2, 3);
CREATE TABLE f1 (a1 INT, b1 INT);
INSERT INTO f1 VALUES (1, 2), (3, 1);
CREATE TABLE f2 (a2 INT, b2 INT);
INSERT INTO f2 VALUES (1, 4), (2, 2), (4, 1);
CREATE TABLE f3 (a3 INT, b3 INT);
INSERT INTO f3 VALUES (1, 1), (2, 9);
CREATE TABLE f4 (a4 INT, b4 INT);
INSERT INTO f4 VALUES (1, 1), (3, 3);

-- LEFT JOIN f1 reads a2 from the later f2: compiles, returns 2 rows
SELECT * FROM f0 LEFT JOIN f1 ON b1 = a2 JOIN f2 ON a2 = a0;

-- circular: f1's ON reads f2 and f2's ON reads f1. On master this returns
-- no rows. Evaluating f1's ON after f2 joins (sub-query form below) keeps
-- both f4 rows, NULL-extended, because the RIGHT JOIN preserves them.
-- SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM f0 CROSS JOIN f2)
--     LEFT JOIN f1 ON b1 < b2 WHERE b2 < b1) LEFT JOIN f3 ON a3 >= a1)
--     RIGHT JOIN f4 ON b4 = b0;
SELECT * FROM f0 LEFT JOIN f1 ON b1 < b2 JOIN f2 ON b2 < b1
    LEFT JOIN f3 ON a3 >= a1 RIGHT JOIN f4 ON b4 = b0;

-- prefix ON reads the RIGHT JOIN's slave
SELECT * FROM f0 JOIN f1 ON a1 < a3 CROSS JOIN f2 RIGHT JOIN f3 ON b3 = b0;

Expected fix direction

Reject an ON clause that references a table joined after it, with an error positioned at the offending column, as PostgreSQL does. If QuestDB keeps accepting INNER JOIN forward references for compatibility, reject at least the outer/ASOF join forms and any reference past a RIGHT/FULL OUTER join. LateralJoinRewriter builds __qdb_outer_ref__ joins whose ON clause references later models (see the LATERAL issue), so the check must run on the user's query before that rewrite or exempt the rewriter's joins.

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), an INNER ON equality that references the slave of a later LEFT, RIGHT, FULL, ASOF or LT join fails with Invalid column, as on master. The circular forward references ahead of a RIGHT/FULL join (the second example above) still return the same wrong rows as master. Rejecting forward references outright, as suggested above, would turn queries that master answers correctly into errors (for example INNER JOIN forward references with no RIGHT/FULL join after them), so #7674 does not do it.