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.

Constant Summary

Constants included from PgQuerySupport

PgQuerySupport::COMPARISON_OPS

Instance Method Summary collapse

Methods inherited from Base

all, #enabled?, options, rule_name

Instance Method Details

#check(query) ⇒ Object



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(query)
  detections = []
  query.each_scope do |scope|
    next unless scope.where_clause

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

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

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

#default_enabledObject



9
10
11
# File 'lib/pg_canary/rules/definitions/not_in_subquery.rb', line 9

def default_enabled
  true
end