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
{{ message }}
Repository navigation
Unexpected results when using natural right join #7879
CREATETABLEt0(c0 INT, c1 INT);
CREATETABLEt1(c0 INT);
INSERT INTO t0(c0, c1) VALUES (1, 2);
INSERT INTO t1( c0) VALUES (3);
SELECT*FROM t0 NATURAL RIGHT JOIN t1; -- 3 <null>SELECT (c0 IN (c0, c1)) FROM t0 NATURAL RIGHT JOIN t1; -- <true>SELECT*FROM t0 NATURAL RIGHT JOIN t1 WHERE ((c0 IN (c0, c1)));
-- Expected: 3 <null>-- Actual: empty
The third SELECT returns an empty result, which is surprising: If the result of second query is true, the value of the IN expression should be true, and thus the third query should return the row of the JOIN , that is 3 <null>, same as the first query.
I found this in version 6.0.0.154 where I built from source code ab37234
IIRC, every unqualified (without an alias) field reference inside a select list of A NATURAL JOIN B is replaced with COALESCE(A, B) under the hood. So the expression actually becomes COALESCE(T0.C0, T1.C0) IN (COALESCE(T0.C0, T1.C0), T1.C1) inside the select list and thus evaluates to TRUE. If fields are qualified properly, the result is different:
SELECT (t1.c0 IN (t0.c0, t0.c1)) FROM t0 NATURAL RIGHT JOIN t1
-- null
(in all FB versions)
and this:
SELECT * FROM t0 NATURAL RIGHT JOIN t1 WHERE ((t1.c0 IN (t0.c0, t0.c1)));
Explained plan shows that RIGHT JOIN was changed to INNER JOIN in v5/v6, due to WHERE condition not caring about NULLs. This looks wrong. Maybe the COALESCE replacement happens after this check. I will look closer.
Activity
dyemanov commentedon Nov 27, 2023
WHEREcondition is applied after a join.(c0 IN (c0, c1))means(c0 = c0 OR c0 = c1)andNULL = <something>can never be true.mrotteveel commentedon Nov 27, 2023
Why do you expect 3 NULL values, that will never happen. Do you understand what a NATURAL JOIN does? To quote from the Language reference:
That means it is equivalent to a join with
USING (c0)orON t0.c0 = t1.c0, which evaluates tofalse, so there is no row in the result.dyemanov commentedon Nov 27, 2023
It appears that the second query wrongly returns TRUE then.
dyemanov commentedon Nov 27, 2023
@mrotteveel It's RIGHT join, so the false join condition does not eliminate the rows from the output.
dyemanov commentedon Nov 27, 2023
Firebird v4 returns {3, null} for the third query, while Firebird v5/v6 return no rows.
dyemanov commentedon Nov 27, 2023
IIRC, every unqualified (without an alias) field reference inside a select list of
A NATURAL JOIN Bis replaced with COALESCE(A, B) under the hood. So the expression actually becomesCOALESCE(T0.C0, T1.C0) IN (COALESCE(T0.C0, T1.C0), T1.C1)inside the select list and thus evaluates toTRUE. If fields are qualified properly, the result is different:SELECT (t1.c0 IN (t0.c0, t0.c1)) FROM t0 NATURAL RIGHT JOIN t1-- null
(in all FB versions)
and this:
SELECT * FROM t0 NATURAL RIGHT JOIN t1 WHERE ((t1.c0 IN (t0.c0, t0.c1)));returns no rows even in v4.
mrotteveel commentedon Nov 27, 2023
UNKNOWN OR TRUE is TRUE
or more correctly TRUE OR UNKNOWN is TRUE
mrotteveel commentedon Nov 27, 2023
See my previous comment, that is the correct result:
c0 in (c0, c1)isc0 = c0 OR c0 = c1, is3 = 3 OR 3 = NULL, is TRUE OR UNKNOWN, is TRUE.mrotteveel commentedon Nov 27, 2023
Interestingly enough, the equivalent using
= ANYdoes produce the right result:dyemanov commentedon Nov 27, 2023
Yes, if
c0is wrapped withCOALESCE. I impliedc0is actuallyt0.c0when was writing it's incorrect, asNULL = NULL => UNKNOWN.mrotteveel commentedon Nov 27, 2023
Sure, but IIRC there should be no
t0.c0ort1.c0after a natural join, there should only be a singlec0of the join result.dyemanov commentedon Nov 27, 2023
Explained plan shows that
RIGHT JOINwas changed toINNER JOINin v5/v6, due toWHEREcondition not caring about NULLs. This looks wrong. Maybe theCOALESCEreplacement happens after this check. I will look closer.This should fix #7879: Unexpected results when using natural right join
dyemanov commentedon Nov 27, 2023
Should be fixed in the next snapshot (tomorrow).
[-]Unexpected Results when Using Natural Right Join[/-][+]Unexpected results when using natural right join[/+]This should fix FirebirdSQL#7879: Unexpected results when using natur…