WL#461: Derived tables dependent of outer select.

Affects: Server-8.0   —   Status: Complete

WHAT
====

If we have a top query, which contains a subquery, and a derived table is
contained in the subquery, this WL wants to allow the definition of the derived
table to reference the tables used by the top query.
This has been allowed by the SQL standard for long (since SQL92?), but not by MySQL.
It is not the same thing as LATERAL derived tables (WL#8652): in WL#8652,
references are to the tables in the same FROM clause as the derived table; in
this WL, they are to tables outer to that clause.

EXAMPLE
=======

Here "derived table" means "derived table or CTE (recursive or not)".

Let's define "outer reference" with an example: below, t1.b is an outer reference:

SELECT * FROM t1
WHERE t1.e>(SELECT avg(tt.a) + t1.b
                FROM (SELECT sum(t3.a) FROM 
                      t3 GROUP BY t3.d) tt
            WHERE tt.a>10))

This query is valid in MySQL.
But if the subquery contains a derived table in its FROM clause, and the outer
reference t1.b is in that derived table, MySQL rejects it:

SELECT * FROM t1
WHERE t1.e>(SELECT avg(tt.a)
                FROM (SELECT sum(t3.a) FROM 
                      t3 WHERE t3.b=t1.b GROUP BY t3.d) tt
                                     ^^
            WHERE tt.a>10))
->error

The SQL standard allows this query.

References:
http://stackoverflow.com/questions/21011270/mysql-reference-outer-table-alias-in-subquery-error?rq=1
http://stackoverflow.com/questions/28889171/mysql-derived-table-outer-reference

WARNING
=======

For simplification purposes, this WL will be designed, described, implemented
and documented as a piece of WL#8652 (LATERAL). Because the necessary code
changes are very linked together.