• Magne Mahre's avatar
    Bug#57986 ORDER BY clause is not used after a UNION, · 1b645167
    Magne Mahre authored
              if embedded in a SELECT
                
    An ORDER BY clause was bound to the incorrect
    (sub-)statement when used in a UNION context.
                
    In a query like:
    SELECT * FROM a UNION SELECT * FROM b ORDER BY c
    the result of SELECT * FROM b is sorted, and then
    combined with a.  The correct behaviour is that
    the ORDER BY clause should be applied on the
    final set.   Similar behaviour was seen on LIMIT
    clauses as well.
                
    In a UNION statement, there will be a select_lex
    object for each of the two selects, and a 
    select_lex_unit object that describes the UNION
    itself.  Similarly, the same behaviour was also
    seen on derived tables.
                
    The bug was caused by using a grammar rule for
    ORDER BY and LIMIT that bound these elements
    to thd->lex->current_select, which points to the
    last of the two selects, instead of to the 
    fake_select_lex member of the master select_lex_unit
    object.
    
    
    sql/sql_yacc.yy:
      Need to use (opt_)union_order_or_limit to
      bind to the correct select_lex object.
    1b645167
union.result 49.6 KB