• mithun's avatar
    Bug#17217128 : BAD INTERACTION BETWEEN MIN/MAX AND · 4c4def90
    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."
    4c4def90
group_min_max.result 112 KB