Class: PgCanary::Rules::QueryComplexity

Inherits:
Base
  • Object
show all
Defined in:
lib/pg_canary/rules/definitions/query_complexity.rb

Overview

"spaghetti query" guard — too many joins or too much subquery nesting. Thresholds:

config.rules.query_complexity.max_joins (default 8)
config.rules.query_complexity.max_depth (default 4)

Instance Method Summary collapse

Methods inherited from Base

all, check, default_enabled, enabled?, #initialize, option, options, rule_name

Constructor Details

This class inherits a constructor from PgCanary::Rules::Base

Instance Method Details

#checkObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pg_canary/rules/definitions/query_complexity.rb', line 16

def check
  max_joins = rule_config.max_joins
  max_depth = rule_config.max_depth
  stmt = parse_result.tree.stmts.first&.stmt
  return [] unless stmt

  problems = []
  joins = count_joins(stmt)
  depth = max_select_depth(stmt)
  problems << "#{joins} joins (max #{max_joins})" if joins > max_joins
  problems << "subquery depth #{depth} (max #{max_depth})" if depth > max_depth
  return [] if problems.empty?

  [detection(
    message: "Query complexity exceeds thresholds: #{problems.join(", ")}.",
    suggestion: "Consider splitting the query, precomputing intermediate results, " \
                "or reviewing whether every join/subquery is necessary."
  )]
end