• Igor Babaev's avatar
    MDEV-19324 Wrong results from query, using brackets with ORDER BY ..LIMIT · 5dee4a99
    Igor Babaev authored
    If a select query was of the form (SELECT ... ORDER BY ...) LIMIT ...
    then in most cases it returned incorrect result. It happened because
    SELECT ... ORDER BY ... was wrapped into a select with materialized
    derived table:
      SELECT ... ORDER BY ... =>
      SELECT * FROM (SELECT ... ORDER BY ...) dt.
    Yet for any materialized derived table ORDER BY without LIMIT is ignored.
    This patch resolves the problem by the conversion
      (SELECT ... ORDER BY ...) LIMIT ... =>
      SELECT ... ORDER BY ... LIMIT ...
    at the parser stage.
    
    Similarly
      ((SELECT ... UNION ...) ORDER BY ...) LIMIT ...
    is converted to
      (SELECT ... UNION ...) ORDER BY ... LIMIT ...
    This conversion optimizes execution of the query because the result of
    (SELECT ... UNION ...) ORDER BY ... is not materialized into a temporary
    table anymore.
    5dee4a99
brackets.result 13.2 KB