• Igor Babaev's avatar
    MDEV-29361 Infinite recursive calls when detecting CTE dependencies · 28ae3618
    Igor Babaev authored
    This patch resolves the problem of improper name resolution of table
    references to embedded CTEs for some queries. This improper binding could
    lead to
      - infinite sequence of calls of recursive functions
      - crashes due to resolution of null pointers
      - wrong result sets returned by queries
      - bogus error messages
    
    If the definition of a CTE contains with clauses then such CTE is called
    embedding CTE while CTEs from the with clauses are called embedded CTEs.
    If a table reference used in the definition of an embedded CTE cannot be
    resolved within the unit that contains this reference it still may be
    resolved against a CTE definition from the with clause with one of the
    embedding CTEs.
    A table reference can be resolved against a CTE definition if it used in
    the the scope of this definition and it refers to the name of the CTE.
    Table reference t is in the scope of the CTE definition of CTE cte if
    - the definition of cte is an element of a with clause declared as
      RECURSIVE and the reference t belongs either to the unit to which
      this with clause is attached or to one of the elements of this clause
    - the definition of cte is an element of a with clause without RECURSIVE
      specifier and the reference t belongs either to the unit to which this
      with clause is attached or to one of the elements from this clause that
      are placed before the definition of cte.
    If a table reference can be resolved against several CTE definitions then
    it is bound to the most embedded.
    
    The code before this patch not always resolved table references used in
    embedded CTE according to the above rules.
    
    Approved by Oleksandr Byelkin <sanja@mariadb.com>
    28ae3618
sql_yacc_ora.yy 565 KB