#7693·questdb

Join on UNNEST output fails with "UNNEST requires a master factory"

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

Describe the bug

A query fails when a table follows an UNNEST and is linked to the UNNEST output (or to the model right before the next outer join), with the UNNEST in the same FROM list as its source table. The query is valid, but QuestDB fails with AssertionError: UNNEST requires a master factory under -ea. Without -ea, it fails with a NullPointerException in SqlCodeGenerator.generateUnnest (masterFactory.getMetadata()). EXPLAIN fails the same way.

SqlOptimiser gives an UNNEST model no ordering edge to the tables its expressions read. The model stays out of the ordering graph and doReorderTables appends it after its source table, in model order, only as long as nothing links it. Any edge out of it makes it an ordering root: a join key that reads an UNNEST column (JOIN d ON d.x = u.v, or WHERE g.b = u.v), or the dependency that addOuterJoinExpression adds from the model before an outer join to that join (linkDependencies(parent, joinIndex - 1, joinIndex)). doReorderTables then runs the UNNEST first, before its source table (f0 below) has joined, and SqlCodeGenerator.generateJoins reaches the JOIN_UNNEST branch with master == null.

Seen on master 641aa5fee5. Wrapping the UNNEST in a sub-query works around it.

To reproduce

sql
CREATE TABLE f0 (a0 INT, arr0 DOUBLE[], ts TIMESTAMP) TIMESTAMP(ts);
INSERT INTO f0 VALUES (1, ARRAY[1.0, 2.0], '2024-01-01T00:00:01'), (2, ARRAY[3.0], '2024-01-01T00:00:03');
CREATE TABLE f1 (a1 INT, arr1 DOUBLE[], ts TIMESTAMP) TIMESTAMP(ts);
INSERT INTO f1 VALUES (1, ARRAY[0.5], '2024-01-01T00:00:00'), (3, ARRAY[2.5, 4.0], '2024-01-01T00:00:02');
CREATE TABLE g1 (b1 DOUBLE, k1 INT);
INSERT INTO g1 VALUES (1.0, 1), (2.5, 3);
CREATE TABLE d (k INT, x DOUBLE);
INSERT INTO d VALUES (1, 1.5), (3, 2.5), (2, 3.5);

-- all fail with "UNNEST requires a master factory"
SELECT * FROM f0, UNNEST(f0.arr0) u(v) JOIN d ON d.x = u.v;
SELECT * FROM f0, UNNEST(f0.arr0) u(v) LEFT JOIN d ON d.x = u.v;
SELECT * FROM f0, UNNEST(f0.arr0) u(v) LEFT JOIN d ON d.x > u.v;
SELECT * FROM f0, UNNEST(f0.arr0) u(v) CROSS JOIN g1 WHERE g1.b1 = u.v;
SELECT * FROM f0 CROSS JOIN f1, UNNEST(f1.arr1) u(v) LEFT JOIN d ON d.x > u.v;
SELECT * FROM f0 CROSS JOIN g1, UNNEST(f0.arr0) u(v) LEFT JOIN d ON d.x > g1.b1;

The sub-query form of the first query returns the expected empty result (no v equals a d.x), and SELECT * FROM (SELECT * FROM f0, UNNEST(f0.arr0) u(v)) t LEFT JOIN d ON d.x > t.v returns 6 rows.

Expected fix direction

Give every UNNEST model an ordering edge from each model its UNNEST expressions read, the way createImpliedDependencies gives a time-series join a context parent 0. The UNNEST then stays after its source whatever edges leave it. This changes the join order, and so the plans, of UNNEST queries that work today (for example t, UNNEST(t.arr) u JOIN t2 ON t2.id = t.id would run the UNNEST before the hash join), so plan assertions in UnnestTest need a review.

QuestDB version

master (641aa5fee5); also branch fix/join-optimiser-lost-rows (db69fb3f4a)

Found while working on #7674.