• Sreeharsha Ramanavarapu's avatar
    Bug #24595639: INCORRECT BEHAVIOR IN QUERY WITH UNION AND · c34f2e51
    Sreeharsha Ramanavarapu authored
                   GROUP BY
    
    Issue 1:
    --------
    This problem occurs in the following conditions:
    1) A UNION is present in the subquery of select list and
       handles multiple columns.
    2) Query has a GROUP BY.
    
    A temporary table is created to handle the UNION.
    Item_field objects are based on the expressions of the
    result of the UNION (ie. the fake_select_lex). While
    checking validity of the columns in the GROUP BY list, the
    columns of the temporary table are checked in
    Item_ident::local_column. But the Item_field objects
    created for the temporary table don't have information like
    the Name_resolution_context that they belong to or whether
    they are dependent on an outer query. Since these members
    are null, incorrect behavior is caused.
    
    This can happen when such Item objects are cached to apply
    the IN-to-EXISTS transform for Item_row.
    
    Solution to Issue 1:
    --------------------
    Context information of the first select in the UNION will
    be assigned to the new Item_field objects.
    
    
    Issue 2:
    --------
    This problem occurs in the following conditions:
    1) A UNION is present in the subquery of select list.
    2) A column in the UNION's first SELECT refers to a table
       in the outer-query making it a dependent union.
    3) GROUP BY column refers to the outer-referencing column.
    
    While resolving the select list with an outer-reference, an
    Item_outer_ref object is created to handle the
    outer-query's GROUP BY list. The Item_outer_ref object
    replaces the Item_field object in the item tree.
    Item_outer_ref::fix_fields will be called only while fixing
    the inner references of the outer query.
    
    Before resolving the outer-query, an Item_type_holder
    object needs to be created to handle the UNION. But as
    explained above, the Item_outer_ref object has not been
    fixed yet. Having a fixed Item object is a pre-condition
    for creating an Item_type_holder.
    
    Solution to Issue 2:
    --------------------
    Use the reference (real_item()) of an Item_outer_ref object
    instead of the object itself while creating an
    Item_type_holder.
    c34f2e51
item.cc 233 KB