Class: Gitlab::Database::QueryAnalyzers::PreventSetOperatorMismatch::CommonTableExpressions

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/query_analyzers/prevent_set_operator_mismatch/common_table_expressions.rb

Class Method Summary collapse

Class Method Details

.references(node, cte_refs) ⇒ Object

Convert CTEs available within this SELECT statement into a set of References.

Parameters:

  • node (PgQuery::Node)

    The PgQuery SELECT statement node containing the CTEs.

  • cte_refs (References)

    Inherited CTEs from scopes that wrap this SELECT statement.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/database/query_analyzers/prevent_set_operator_mismatch/common_table_expressions.rb', line 21

def references(node, cte_refs)
  return cte_refs if node&.with_clause.nil?

  refs = cte_refs.dup

  node.with_clause.ctes.each do |cte|
    cte_name = name(cte)
    cte_select_stmt = select_stmt(cte)

    # Resolve the CTE type to dynamic/static/error.
    refs[cte_name] = if node.with_clause.recursive
                       # Recursive CTEs need special handling to avoid infinite loops.
                       recursive_refs(cte_refs, cte_name, cte_select_stmt)
                     else
                       SelectStmt.new(cte_select_stmt, cte_refs).types
                     end
  end

  refs
end