1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/arel/table.rb

118 lines
2.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-02-24 01:45:50 -05:00
2018-02-24 02:41:47 -05:00
module Arel # :nodoc: all
2010-08-12 14:24:16 -04:00
class Table
include Arel::FactoryMethods
include Arel::AliasPredication
2010-08-12 14:24:16 -04:00
@engine = nil
class << self; attr_accessor :engine; end
attr_accessor :name, :table_alias
2010-08-12 18:40:58 -04:00
# TableAlias and Table both have a #table_name which is the name of the underlying table
alias :table_name :name
def initialize(name, as: nil, klass: nil, type_caster: klass&.type_caster)
2018-02-24 01:45:50 -05:00
@name = name.to_s
@klass = klass
@type_caster = type_caster
# Sometime AR sends an :as parameter to table, to let the table know
# that it is an Alias. We may want to override new, and return a
# TableAlias node?
if as.to_s == @name
as = nil
2010-09-21 16:50:53 -04:00
end
@table_alias = as
2010-08-18 19:32:32 -04:00
end
2018-02-24 01:45:50 -05:00
def alias(name = "#{self.name}_2")
Nodes::TableAlias.new(self, name)
2010-08-12 14:24:16 -04:00
end
def from
SelectManager.new(self)
2010-09-08 20:32:44 -04:00
end
2018-02-24 01:45:50 -05:00
def join(relation, klass = Nodes::InnerJoin)
return from unless relation
case relation
when String, Nodes::SqlLiteral
2017-01-17 23:13:37 -05:00
raise EmptyJoinError if relation.empty?
klass = Nodes::StringJoin
end
from.join(relation, klass)
2010-08-18 19:32:32 -04:00
end
2018-02-24 01:45:50 -05:00
def outer_join(relation)
2013-05-12 13:09:07 -04:00
join(relation, Nodes::OuterJoin)
end
2018-02-24 01:45:50 -05:00
def group(*columns)
from.group(*columns)
2010-09-07 19:07:37 -04:00
end
2018-02-24 01:45:50 -05:00
def order(*expr)
from.order(*expr)
2010-09-06 20:17:49 -04:00
end
2018-02-24 01:45:50 -05:00
def where(condition)
from.where condition
end
2018-02-24 01:45:50 -05:00
def project(*things)
from.project(*things)
2010-08-13 17:13:38 -04:00
end
2018-02-24 01:45:50 -05:00
def take(amount)
from.take amount
2010-08-16 18:02:37 -04:00
end
2018-02-24 01:45:50 -05:00
def skip(amount)
from.skip amount
end
2018-02-24 01:45:50 -05:00
def having(expr)
from.having expr
2010-09-08 18:29:22 -04:00
end
def [](name, table = self)
name = name.to_s if name.is_a?(Symbol)
name = @klass.attribute_aliases[name] || name if @klass
Attribute.new(table, name)
2010-08-12 14:24:16 -04:00
end
def hash
# Perf note: aliases and table alias is excluded from the hash
# aliases can have a loop back to this table breaking hashes in parent
# relations, for the vast majority of cases @name is unique to a query
@name.hash
end
2018-02-24 01:45:50 -05:00
def eql?(other)
self.class == other.class &&
self.name == other.name &&
self.table_alias == other.table_alias
end
alias :== :eql?
def type_cast_for_database(attr_name, value)
type_caster.type_cast_for_database(attr_name, value)
end
Perf: Improve performance of where when using an array of values A coworker at GitHub found a few months back that if we used `santitize_sql` over `where` when we knew the values going into `where` it was a lot faster than `where`. This PR adds a new Arel node type called `HomogenousIn` that will be used when Rails knows the values are all homogenous and can therefore pick a faster codepath. This new codepath skips some of the required processing by `where` to make `wheres` with homogenous arrays faster without requiring the application author to know when to use which query type. Using our benchmark code: ```ruby ids = (1..1000).each.map do |n| Post.create!.id end Benchmark.ips do |x| x.report("where with ids") do Post.where(id: ids).to_a end x.report("where with sanitize") do Post.where(ActiveRecord::Base.sanitize_sql(["id IN (?)", ids])).to_a end x.compare! end ``` Before this PR comparing where with a list of IDs to santitize sql: ``` Warming up -------------------------------------- where with ids 11.000 i/100ms where with sanitize 17.000 i/100ms Calculating ------------------------------------- where with ids 115.733 (± 4.3%) i/s - 583.000 in 5.045828s where with sanitize 174.231 (± 4.0%) i/s - 884.000 in 5.081495s Comparison: where with sanitize: 174.2 i/s where with ids: 115.7 i/s - 1.51x slower ``` After this PR comparing where with a list of IDs to santitize sql: ``` Warming up -------------------------------------- where with ids 16.000 i/100ms where with sanitize 19.000 i/100ms Calculating ------------------------------------- where with ids 158.293 (± 6.3%) i/s - 800.000 in 5.072208s where with sanitize 169.141 (± 3.5%) i/s - 855.000 in 5.060878s Comparison: where with sanitize: 169.1 i/s where with ids: 158.3 i/s - same-ish: difference falls within error ``` Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
2020-01-14 16:14:39 -05:00
def type_for_attribute(name)
type_caster.type_for_attribute(name)
end
def able_to_type_cast?
!type_caster.nil?
end
private
2018-02-24 01:45:50 -05:00
attr_reader :type_caster
2010-08-12 14:24:16 -04:00
end
end