Class: PgCanary::Rules::UnionInsteadOfUnionAll

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

Overview

Tier 2 (opt-in): UNION without ALL deduplicates the combined result by sorting/hashing all rows. When the branches cannot overlap (or duplicates are acceptable), UNION ALL skips that work. Whether deduplication is intended is the author's call, hence opt-in.

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



14
15
16
17
18
19
20
21
22
23
# File 'lib/pg_canary/rules/definitions/union_instead_of_union_all.rb', line 14

def check(query)
  union = query.scopes.find { |scope| scope.stmt.op == :SETOP_UNION && !scope.stmt.all }
  return [] unless union

  [detection(
    query,
    message: "UNION (without ALL) sorts/hashes the entire combined result to remove duplicates.",
    suggestion: "If the branches cannot produce overlapping rows — or duplicates are acceptable — use UNION ALL."
  )]
end

#default_enabledObject



10
11
12
# File 'lib/pg_canary/rules/definitions/union_instead_of_union_all.rb', line 10

def default_enabled
  false
end