• Igor Babaev's avatar
    MDEV-29307 Wrong result when joining two derived tables over the same view · 4d38267f
    Igor Babaev authored
    This bug could affect queries containing a join of derived tables over
    grouping views such that one of the derived tables contains a window
    function while another uses view V with dependent subquery DSQ containing
    a set function aggregated outside of the subquery in the view V. The
    subquery also refers to the fields from the group clause of the view.Due to
    this bug execution of such queries could produce wrong result sets.
    
    When the fix_fields() method performs context analysis of a set function AF
    first, at the very beginning the function Item_sum::init_sum_func_check()
    is called. The function copies the pointer to the embedding set function,
    if any, stored in THD::LEX::in_sum_func into the corresponding field of the
    set function AF simultaneously changing the value of THD::LEX::in_sum_func
    to point to AF. When at the very end of the fix_fields() method the function
    Item_sum::check_sum_func() is called it is supposed to restore the value
    of THD::LEX::in_sum_func to point to the embedding set function. And in
    fact Item_sum::check_sum_func() did it, but only for regular set functions,
    not for those used in window functions. As a result after the context
    analysis of AF had finished THD::LEX::in_sum_func still pointed to AF.
    It confused the further context analysis. In particular it led to wrong
    resolution of Item_outer_ref objects in the fix_inner_refs() function.
    This wrong resolution forced reading the values of grouping fields referred
    in DSQ not from the temporary table used for aggregation from which they
    were supposed to be read, but from the table used as the source table for
    aggregation.
    
    This patch guarantees that the value of THD::LEX::in_sum_func is properly
    restored after the call of fix_fields() for any set function.
    4d38267f
tempfiles_encrypted.result 103 KB