-
-
Notifications
You must be signed in to change notification settings - Fork 246
Description
Submitted by: @dyemanov
Is related to CORE94
Is related to CORE2378
Is related to QA322
Relate to CORE3693
Consider this query:
select *
from TA, TB
left join TC on TA.COL1 = TC.COL1
where TA.COL2 = TB.COL2
You can see that the outer join condition references the table from the implicit join. Currently, such queries either throw a famous "no current record to fetch" error or return incorrect result set, depending on the execution plan. Perhaps one lucky day they could even deliver a correct answer, I'm not sure ;-)
So far it has been considered being an optimizer bug and we have a few related tickets in the tracker. However, I was recently wondering whether this query is correct at all. I've found that both MSSQL and PostgreSQL throw an "invalid column reference" error for this query. The SQL specification mentions that a "joined_table" (i.e. an explicit join) is a table derived from the join expression. But we know that a derived table is prepared in its own (independent) context and cannot reference fields from the outer contexts. For me this clearly means that implicitly joined tables cannot be visible from the explicitly joined ones.
So I'm proposing to prohibit such field references as being invalid. Below are examples that should still work:
select *
from TA
join TB on TA.COL2 = TB.COL2
left join TC on TA.COL1 = TC.COL1
select *
from TB, TA
left join TC on TA.COL1 = TC.COL1
where TA.COL2 = TB.COL2
select *
from TA
left join TC on TA.COL1 = TC.COL1
, TB
where TA.COL2 = TB.COL2
I doubt this limitation could badly affect any existing applications, but theoretically this is possible and should be taken into account. I have to admit though that I wouldn't feel comfortable with a yet another legacy compatibility configuration switch :-)
Commits: 401f31a 18a9e37 f9f4f90 90beb3c
====== Test Details ======
See also (in russian): http://sql.ru/forum/actualutils.aspx?action=gotomsg&tid=804601&msg=9751146
Additional explanations about why following WORKS in FB 3.0:
select 'case-4' msg, a.id from t1 b, t1 a join t1 c on a.id=c.id where a.id=b.id;
-- see in the letter by dimitr, 02-apr-2015 19:34