Analysis:
This is an architectural problem in the way ORDER BY expressions
and function arguments are resolved.
Order by expressions are resolved by the procedure find_order_in_list().
This procedure correctly defines the name resolution order to search
first in the SELECT clause and to prefer aliases. The name resolution is
implemented via the call:
/* Lookup the current GROUP/ORDER field in the SELECT clause. */
select_item= find_item_in_list(order_item, fields, &counter, REPORT_EXCEPT_NOT_FOUND, &resolution);
Note however that find_item_in_list() works only for fields (Item_field)
and not functions.
Thus functions in the ORDER clause are not resolved against the
SELECT clause, and find_order_in_list() proceeds until it calls
Item::fix_fields() for these Items.
For functions, fix_fields() calls recursively fix_fields() for its
arguments. Finally we end up calling Item_field::fix_fields() for
the 'LanguageName' argument of e.g. the Collate function.
However Item_field::fix_fields() is designed to resolve fields
mainly in the WHERE and other clauses, where the search order
is different from the one in the ORDER clause. Specifically,
fix_fields() searches first in the FROM clause. As a result, matching
fields in the FROM clause have priority over aliases in the SELECT
clause.
This explains why the first example is resolved correctly, and the
next two - incorrectly.
Analysis:
This is an architectural problem in the way ORDER BY expressions
and function arguments are resolved.
Order by expressions are resolved by the procedure find_order_ in_list( ).
This procedure correctly defines the name resolution order to search
first in the SELECT clause and to prefer aliases. The name resolution is
implemented via the call:
/* Lookup the current GROUP/ORDER field in the SELECT clause. */ in_list( order_item, fields, &counter,
REPORT_ EXCEPT_ NOT_FOUND, &resolution);
select_item= find_item_
Note however that find_item_in_list() works only for fields (Item_field)
and not functions.
Thus functions in the ORDER clause are not resolved against the in_list( ) proceeds until it calls
SELECT clause, and find_order_
Item::fix_fields() for these Items.
For functions, fix_fields() calls recursively fix_fields() for its :fix_fields( ) for
arguments. Finally we end up calling Item_field:
the 'LanguageName' argument of e.g. the Collate function.
However Item_field: :fix_fields( ) is designed to resolve fields
mainly in the WHERE and other clauses, where the search order
is different from the one in the ORDER clause. Specifically,
fix_fields() searches first in the FROM clause. As a result, matching
fields in the FROM clause have priority over aliases in the SELECT
clause.
This explains why the first example is resolved correctly, and the
next two - incorrectly.