SPLICE JOIN after a non-equi RIGHT/FULL JOIN returns outer-join values on slave-only rows
Impact: wrong rows, silently. Affects SPLICE JOIN queries written after a non-equi RIGHT/FULL OUTER JOIN (e.g. RIGHT JOIN t4 ON t4.id > 1), with no INNER join between them. Rare query shape.
Location: core/src/main/java/io/questdb/griffin/SqlOptimiser.java, doReorderTables() (master 641aa5fee5: method at line 3878, "add pure crosses at the end of ordered table list" at line 3933).
Describe the bug
A SPLICE JOIN written after a non-equi RIGHT or FULL OUTER JOIN executes ahead of the outer join. SPLICE emits slave rows that have no matching master row, with all master columns NULL. After the reorder, the outer join pairs those slave-only rows with rows of its right-hand table, so they come back with non-NULL values in columns that belong to the splice's master. When the outer join's right side has several matching rows, the slave-only row is also duplicated.
In the written order, the splice's master is the outer join's output. That output has no designated
timestamp, so the query could reasonably be rejected with "left side of time series join has no
timestamp", as it already is for the equi form (RIGHT JOIN t4 ON t4.id = t1.id). Instead it runs
in a different order and returns different rows.
Cause: SqlOptimiser.doReorderTables() appends context-free joins last ("add pure crosses at the
end"), including the CROSS RIGHT/FULL join that a non-equi outer join becomes. The SPLICE join has
an implied dependency on the first model and runs before it.
To reproduce
CREATE TABLE t1 (id INT, ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY DAY;
INSERT INTO t1 VALUES (1, '2024-01-01T00:00:01'), (2, '2024-01-01T00:00:02'), (3, '2024-01-01T00:00:03');
CREATE TABLE t3 (id INT, ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY DAY;
INSERT INTO t3 VALUES (1, '2024-01-01T00:00:00'), (3, '2024-01-01T00:00:04'), (2, '2024-01-01T00:00:05');
CREATE TABLE t4 (id INT, ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY DAY;
INSERT INTO t4 VALUES (4, '2024-01-01T00:00:01');
SELECT t1.id, t3.id, t4.id FROM t1 RIGHT JOIN t4 ON t4.id > 1 SPLICE JOIN t3 ON t3.id = t1.id;Plan: Nested Loop Right Join (filter 1<t4.id) <- Splice Join(t1, t3), with t4 on the right.
Actual:
id id1 id2
null 1 4
1 null 4
2 null 4
3 null 4
3 3 4
2 2 4The written order, with the outer join's output materialized as a timestamped table and spliced:
CREATE TABLE m AS (SELECT t1.id a, t4.id c, t1.ts FROM t1 RIGHT JOIN t4 ON t4.id > 1 ORDER BY ts) TIMESTAMP(ts);
SELECT m.a, t3.id, m.c FROM m SPLICE JOIN t3 ON t3.id = m.a;a id c
null 1 null
1 null 4
...The first row differs: the slave-only t3 row carries t4.id = 4 instead of NULL. The keyless form
... SPLICE JOIN t3 behaves the same way.
Expected behavior
Either reject the query with "left side of time series join has no timestamp", the same as the equi RIGHT/FULL form, or keep the master columns of slave-only splice rows NULL. ASOF and LT joins are not affected: they keep one row per master row, so they commute with the outer join.
Environment
Reproduced on master 641aa5fee5.
Suggested fix
Don't let a SPLICE join execute ahead of a CROSS RIGHT/FULL join written before it. Either pin the outer join before the SPLICE join in doReorderTables(), or have the code generator reject the query with "left side of time series join has no timestamp", as it already does for the equi RIGHT/FULL form.
Where found
Found while reviewing PR #7674 (branch fix/join-optimiser-lost-rows). That PR stops collecting SPLICE joins as "time-series joins ahead" of a non-equi outer join, which fixes the variant with an INNER join between the outer join and the SPLICE. This variant, with no INNER join between, comes from master's doReorderTables() and is unchanged by the PR.
Source: questdb/questdb