Skip to content
  • Neeraj Bisht's avatar
    8323ef3e
    Bug#17634335 - SELECT DISTINCT...GROUP BY RETURNS WRONG RESULTS IN SOME CASES · 8323ef3e
    Neeraj Bisht authored
    Problem:
    Query creating temporary table to find the distinct value and have 
    constant value in projected list doesn't give correct value.
    
    Analysis:
    
    When we have to find the distinct value in our record,
    We will call JOIN_TAB::remove_duplicates().
    To find the position of the item, from which we have to find the 
    distinct value, we will use this 
    -->Number of item in the table - number of non-constant items of the select list.
    
    so in the case like
    SELECT DISTINCT a, b
    FROM (SELECT * FROM foo ORDER BY a, b) AS f
    GROUP BY a,c;
    
    
    The fields of the temporary table will be c, a, b in that order
    (as first it is considering the item that are not in the select 
    list, then select list distinct item).  
    That is, the number of items in the list will be 3.
    The number of items in the select list which are not constant will be 
    2(a,b).
    so 3-2=1, that's why we will considered items a, b to find the distinct values.
    
    In the case of bug we have 
    
    SELECT DISTINCT a, b, 0 as d
    FROM (SELECT * FROM foo ORDER BY a, b) AS f
    GROUP BY a,c;
    
    Resulting in
    4(Number of item in the table that is (c,a,b,0)) - 2(number of non-constant items of the select list that is (a,b))
    which is equal to 2, So we start our distinct from b instead of a, which result 
    in incorrect result.
    
    SOLUTION:
    Instead of doing, Number of item in the table - Number of item in the projected 
    list which are not constant.
    Do Number of item in the table - Number of item in the select list. Then we will get the 
    correct point, from which we have to find the distinct item.
    8323ef3e
    Bug#17634335 - SELECT DISTINCT...GROUP BY RETURNS WRONG RESULTS IN SOME CASES
    Neeraj Bisht authored
    Problem:
    Query creating temporary table to find the distinct value and have 
    constant value in projected list doesn't give correct value.
    
    Analysis:
    
    When we have to find the distinct value in our record,
    We will call JOIN_TAB::remove_duplicates().
    To find the position of the item, from which we have to find the 
    distinct value, we will use this 
    -->Number of item in the table - number of non-constant items of the select list.
    
    so in the case like
    SELECT DISTINCT a, b
    FROM (SELECT * FROM foo ORDER BY a, b) AS f
    GROUP BY a,c;
    
    
    The fields of the temporary table will be c, a, b in that order
    (as first it is considering the item that are not in the select 
    list, then select list distinct item).  
    That is, the number of items in the list will be 3.
    The number of items in the select list which are not constant will be 
    2(a,b).
    so 3-2=1, that's why we will considered items a, b to find the distinct values.
    
    In the case of bug we have 
    
    SELECT DISTINCT a, b, 0 as d
    FROM (SELECT * FROM foo ORDER BY a, b) AS f
    GROUP BY a,c;
    
    Resulting in
    4(Number of item in the table that is (c,a,b,0)) - 2(number of non-constant items of the select list that is (a,b))
    which is equal to 2, So we start our distinct from b instead of a, which result 
    in incorrect result.
    
    SOLUTION:
    Instead of doing, Number of item in the table - Number of item in the projected 
    list which are not constant.
    Do Number of item in the table - Number of item in the select list. Then we will get the 
    correct point, from which we have to find the distinct item.
Loading