• Igor Babaev's avatar
    MDEV-26825 Bogus error for query with two usage of CTE referring another CTE · 8f24f5fe
    Igor Babaev authored
      This bug affected queries with two or more references to a CTE referring
    another CTE if the definition of the latter contained an invocation of
    a stored function that used a base table. The bug could lead to a bogus
    error message or to an assertion failure.
      For any non-first reference to CTE cte1 With_element::clone_parsed_spec()
    is called that parses the specification of cte1 to construct the unit
    structure for this usage of cte1. If cte1 refers to another CTE cte2
    outside of the specification of cte1 then With_element::clone_parsed_spec()
    has to be called for cte2 as well. This call is made by the function
    LEX::resolve_references_to_cte() within the invocation of the function
    With_element::clone_parsed_spec() for cte1.
      When the specification of a CTE is parsed all table references encountered
    in it must be added to the global list of table references for the query.
    As the specification for the non-first usage of a CTE is parsed at a
    recursive call of the parser the function With_element::clone_parsed_spec()
    invoked at this recursive call should takes care of appending the list of
    table references encountered in the specification of this CTE cte1 to the
    list of table references created for the query. And it should do it after
    the call of LEX::resolve_references_to_cte() that resolves references to
    CTEs defined outside of the specification of cte1 because this call may
    invoke the parser again for specifications of other CTEs and  the table
    references from their specifications must ultimately appear in the global
    list of table references of the query.
      The code of With_element::clone_parsed_spec() misplaced the call of
    LEX::resolve_references_to_cte(). As a result LEX::query_tables_last used
    for the query that was supposed to point to the field 'next_global' of the
    last element in the global list of table references actually pointed to
    'next_global' of the previous element.
      The above inconsistency certainly caused serious problems when table
    references used in the stored functions invoked in cloned specifications
    of CTEs were added to the global list of table references.
    8f24f5fe
sql_cte.cc 52.5 KB