• mithun's avatar
    Bug#17217128 : BAD INTERACTION BETWEEN MIN/MAX AND · f2202335
    mithun authored
                   "HAVING SUM(DISTINCT)": WRONG RESULTS.
    ISSUE:
    ------
    If a query uses loose index scan and it has both
    AGG(DISTINCT) and MIN()/MAX()functions. Then, result values
    of MIN/MAX() is set improperly.
    When query has AGG(DISTINCT) then end_select is set to
    end_send_group. "end_send_group" keeps doing aggregation
    until it sees a record from next group. And, then it will
    send out the result row of that group.
    Since query also has MIN()/MAX() and loose index scan is
    used, values of MIN/MAX() are set as part of loose index
    scan itself. Setting MIN()/MAX() values as part of loose
    index scan overwrites values computed in end_send_group.
    This caused invalid result.
    For such queries to work loose index scan should stop
    performing MIN/MAX() aggregation. And, let end_send_group to
    do the same. But according to current design loose index
    scan can produce only one row per group key. If we have both
    MIN() and MAX() then it has to give two records out. This is
    not possible as interface has to use common buffer
    record[0]! for both records at a time.
    
    SOLUTIONS:
    ----------
    For such queries to work we need a new interface for loose
    index scan. Hence, do not choose loose_index_scan for such
    cases. So a new rule SA7 is introduced to take care of the
    same.
    
    SA7: "If Q has both AGG_FUNC(DISTINCT ...) and
          MIN/MAX() functions then loose index scan access
          method is not used."
    
    mysql-test/r/group_min_max.result:
      Expected result.
    mysql-test/t/group_min_max.test:
      1. Test with various combination of AGG(DISTINCT) and
      MIN(), MAX() functions.
      2. Corrected the plan for old queries.
    sql/opt_range.cc:
      A new rule SA7 is introduced.
    f2202335
group_min_max.result 112 KB