You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ORDER BY applied on top of FULL OUTER JOIN does not sort the result
correctly when the key column has NULLs on the NULL-padded side. Rows are
emitted out of order compared to any standard-compliant engine.
For the minimal repro below, (NULL, NULL, 14, NULL) is emitted before (NULL, NULL, 13, 'c'), violating ORDER BY t1.s, t2.s, t1.v, t2.v
(13 < 14 but MO outputs 14 first).
Expected Behavior
Rows should be produced in strict ascending order of the ORDER BY key list,
exactly like MySQL / PostgreSQL / any rewrite via LEFT JOIN ... UNION ALL RIGHT JOIN.
Steps to Reproduce
dropdatabase if exists fj_orderby_bug;
createdatabasefj_orderby_bug;
use fj_orderby_bug;
createtablet1(s int, v varchar(5));
createtablet2(s int, v varchar(5));
insert into t1 values (1,'a'),(5,'b'),(NULL,'x');
insert into t2 values (13,'c'),(14,NULL);
selectt1.s, t1.v, t2.s, t2.vfrom t1 full outer join t2 ont1.s=t2.sorder byt1.s, t2.s, t1.v, t2.v;
Observed output (MO):
NULL x NULL NULL
NULL NULL 14 NULL <-- should come AFTER (NULL,NULL,13,'c')
NULL NULL 13 c
1 a NULL NULL
5 b NULL NULL
Expected output (MySQL via LEFT UNION RIGHT rewrite on the same data):
NULL NULL 13 c
NULL NULL 14 NULL
NULL x NULL NULL
1 a NULL NULL
5 b NULL NULL
The MySQL engine's MySQL-style NULL ordering ("NULL first ascending") gives
a slightly different placement of the NULL x NULL NULL row, but both
engines agree that 13, c must precede 14, NULL. MO does not.
Additional information
EXPLAIN shows the Sort node above the FULL OUTER Join with the correct Sort Key, so the plan is right but the executor does not honor it.
The issue reproduces only when the join produces mixed matched + NULL-padded
rows; trivial cases (only unmatched, or only matched) sort correctly.
A related, simpler case: ORDER BY t1.S1 alone on the 12-row / 10-row t1/t2
dataset used in test/distributed/cases/join/fulljoin.sql returns rows like NULL aaa NULL NULL interleaved between non-matching groups rather than
being grouped with the other NULL-key rows.
Any FULL OUTER JOIN query with user-visible ordering produces wrong row
orders. Downstream logic that relies on ordering (LIMIT, window functions,
client-side "first N rows", GROUP_CONCAT via ORDER BY, etc.) is affected.
Is there an existing issue for the same bug?
Branch Name
main
Commit ID
f0a2c64
Other Environment Information
Actual Behavior
ORDER BYapplied on top ofFULL OUTER JOINdoes not sort the resultcorrectly when the key column has NULLs on the NULL-padded side. Rows are
emitted out of order compared to any standard-compliant engine.
For the minimal repro below,
(NULL, NULL, 14, NULL)is emitted before(NULL, NULL, 13, 'c'), violatingORDER BY t1.s, t2.s, t1.v, t2.v(13 < 14 but MO outputs 14 first).
Expected Behavior
Rows should be produced in strict ascending order of the
ORDER BYkey list,exactly like MySQL / PostgreSQL / any rewrite via
LEFT JOIN ... UNION ALL RIGHT JOIN.Steps to Reproduce
Observed output (MO):
Expected output (MySQL via LEFT UNION RIGHT rewrite on the same data):
The MySQL engine's MySQL-style NULL ordering ("NULL first ascending") gives
a slightly different placement of the
NULL x NULL NULLrow, but bothengines agree that
13, cmust precede14, NULL. MO does not.Additional information
Sortnode above theFULL OUTERJoin with the correctSort Key, so the plan is right but the executor does not honor it.rows; trivial cases (only unmatched, or only matched) sort correctly.
ORDER BY t1.S1alone on the 12-row / 10-row t1/t2dataset used in
test/distributed/cases/join/fulljoin.sqlreturns rows likeNULL aaa NULL NULLinterleaved between non-matching groups rather thanbeing grouped with the other NULL-key rows.
Impact
Any FULL OUTER JOIN query with user-visible ordering produces wrong row
orders. Downstream logic that relies on ordering (LIMIT, window functions,
client-side "first N rows",
GROUP_CONCATvia ORDER BY, etc.) is affected.