-
Sreeharsha Ramanavarapu authored
LEFT JOIN(SUBQUERY) AND ORDER BY Issue: ------ Example query: SELECT t1.a, subq.st_value FROM t1 LEFT JOIN (SELECT t2.t1_a, 'red' AS st_value FROM t2) AS subq ON subq.t1_a = t1.a ORDER BY t1.a; Assume that table t2 is empty and hence the derived table returns zero rows. This query incorrectly returns the contents of t1.a and 'red'. The expected result would be the contents of t1.a and a NULL. Root cause: ----------- The presence of ORDER BY results makes the optimizer use a temporary table. The contents of this temporary table will be columns t1.a and subq.st_value. Here st_value is a "constant", in the sense that the value is not changing with the underlying table . But given that the type of join is an OUTER JOIN, it will still be influenced by the contents of table t2 (inner table of an outer join). Since st_value is marked as constant in the temporary table, this value is always added to the result rows. Solution: ---------- A new Item_direct_view_ref::used_tables() function is created to take into account whether an Item in a derived table / view is part of an inner table of an outer join.
Sreeharsha Ramanavarapu authoredLEFT JOIN(SUBQUERY) AND ORDER BY Issue: ------ Example query: SELECT t1.a, subq.st_value FROM t1 LEFT JOIN (SELECT t2.t1_a, 'red' AS st_value FROM t2) AS subq ON subq.t1_a = t1.a ORDER BY t1.a; Assume that table t2 is empty and hence the derived table returns zero rows. This query incorrectly returns the contents of t1.a and 'red'. The expected result would be the contents of t1.a and a NULL. Root cause: ----------- The presence of ORDER BY results makes the optimizer use a temporary table. The contents of this temporary table will be columns t1.a and subq.st_value. Here st_value is a "constant", in the sense that the value is not changing with the underlying table . But given that the type of join is an OUTER JOIN, it will still be influenced by the contents of table t2 (inner table of an outer join). Since st_value is marked as constant in the temporary table, this value is always added to the result rows. Solution: ---------- A new Item_direct_view_ref::used_tables() function is created to take into account whether an Item in a derived table / view is part of an inner table of an outer join.
Loading