Class: PgCanary::Rules::NotInSubquery

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

Overview

NOT IN (SELECT ...) is a double trap: if the subquery ever returns a NULL the whole predicate yields no rows, and the planner cannot use an anti-join as effectively as with NOT EXISTS.

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pg_canary/rules/definitions/not_in_subquery.rb', line 13

def check
  detections = []
  each_scope do |scope|
    next unless scope.where_clause

    scope.where_clause.walk_scope do |node|
      next unless not_expr?(node)

      node.args.each do |arg|
        sublink = arg.unwrap
        next unless any_sublink?(sublink)

        detections << build(scope, sublink)
      end
    end
  end
  detections
end