Class: PgCanary::Rules::ImplicitCast

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

Overview

Comparing an integer-family column with a numeric/float literal (age = 1.5) makes PostgreSQL cast the column to numeric, which disables its index. Restricted to cases provable from the AST alone.

Constant Summary collapse

INTEGER_TYPES =
%w[smallint integer bigint].freeze
NUMERIC_TYPE_NAMES =
%w[numeric decimal float4 float8].freeze

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
# File 'lib/pg_canary/rules/definitions/implicit_cast.rb', line 16

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

    scope.where_clause.walk_scope do |node|
      next unless node.is_a?(PgQuery::A_Expr) && node.comparison?

      detections << inspect_comparison(scope, node)
    end
  end
  detections.compact
end