In summary, the problem that needs to be solved is that
column references in the ORDER/GROUP BY clauses must
be resolved in a different way from column references in
the WHERE and SELECT clauses.
The implementation in find_order_in_list() is limited only
to Item_field and Item_ref. In general the approach to resolve
ORDER/GROUP BY clauses in a different way compared to the
rest seems wrong.
As discussed with Sanja, the right way to have a flexible and
generic name resolution that supports different name resolution
algorithms, is to reuse the Item::fix_fields mechanism in the
following way.
Items have to be marked correctly with their location in the query.
The name resolution algorithms for each group of clauses have to
be implemented as separate methods of the most suitable class.
Then the corresponding fix_fields() method should select the
correct name resolution algorithm based on the location of the
item in the query.
Notice that when e.g. an Item_field in an ORDER/GROUP BY clause
is inside a subquery, the name resolution algorithm must be changed
so that it first searches the subquery itself in the same way the WHERE
clause is resolved, and only if not found in the subquery, name resolution
should proceed to search the outer query, giving preference to aliases.
This is a small re-engineering task, and not a simple bug fix. My estimate
is at least 4-5 full developer days if there are no nasty surprises. Changing
name resolution is quite risky, so this task should be planned for a release
after 5.3.
Solution:
In summary, the problem that needs to be solved is that
column references in the ORDER/GROUP BY clauses must
be resolved in a different way from column references in
the WHERE and SELECT clauses.
The implementation in find_order_ in_list( ) is limited only
to Item_field and Item_ref. In general the approach to resolve
ORDER/GROUP BY clauses in a different way compared to the
rest seems wrong.
As discussed with Sanja, the right way to have a flexible and
generic name resolution that supports different name resolution
algorithms, is to reuse the Item::fix_fields mechanism in the
following way.
Items have to be marked correctly with their location in the query.
The name resolution algorithms for each group of clauses have to
be implemented as separate methods of the most suitable class.
Then the corresponding fix_fields() method should select the
correct name resolution algorithm based on the location of the
item in the query.
Notice that when e.g. an Item_field in an ORDER/GROUP BY clause
is inside a subquery, the name resolution algorithm must be changed
so that it first searches the subquery itself in the same way the WHERE
clause is resolved, and only if not found in the subquery, name resolution
should proceed to search the outer query, giving preference to aliases.
This is a small re-engineering task, and not a simple bug fix. My estimate
is at least 4-5 full developer days if there are no nasty surprises. Changing
name resolution is quite risky, so this task should be planned for a release
after 5.3.