• Ole John Aske's avatar
    Fix for bug#59308: Incorrect result for SELECT DISTINCT <col>... ORDER BY <col> DESC. · 221ce922
    Ole John Aske authored
          
    Also fix bug#59110: Memory leak of QUICK_SELECT_I allocated memory.
    Includes Jørgen Lølands review comments.
          
    Root cause of these bugs are that test_if_skip_sort_order() decided to
    revert the 'skip_sort_order' descision (and use filesort) after the
    query plan has been updated to reflect a 'skip' of the sort order.
          
    This might happen in 'check_reverse_order:' if we have a 
    select->quick which could not be made descending by appending 
    a QUICK_SELECT_DESC. ().
          
    The original 'save_quick' was then restored after the QEP has been modified,
    which caused:
          
      - An incorrect 'precomputed_group_by= TRUE' may have been set, 
        and not reverted, as part of the already modifified QEP (Bug#59308)
      - A 'select->quick' might have been created which we fail to delete (bug#59110).
          
    This fix is a refactorication of test_if_skip_sort_order() where all logic
    related to modification of QEP (controlled by argument 'bool no_changes'), is
    moved to the end of test_if_skip_sort_order(), and done after *all* 'test_if_skip'
    checks has been performed - including the 'check_reverse_order:' checks.
          
    The refactorication above contains now intentional changes to the logic which 
    has been moved to the end of the function.
          
    Furthermore, a smaller part of the fix address the handling of the 
    select->quick objects which may already exists when we call 
    'test_if_skip_sort_order()' (save_quick) -and
    new select->quick's created during test_if_skip_sort_order():
          
      - Before new select->quick may be created by calling ::test_quick_select(), we
        set 'select->quick= 0' to avoid that ::test_quick_select() prematurely
        delete the save_quick's. (After this call we may have both a 'save_quick' 
        and 'select->quick')
          
      - All returns from ::test_if_skip_sort_order() where we may have both a
        'save_quick' and a 'select->quick' has been changed to goto's to the
        exit points 'skiped_sort_order:' or 'need_filesort:' where we
        decide which of the QUICK_SELECT's to keep, and delete the other.
    221ce922
sql_select.cc 553 KB