PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/activerecord/lib/active_record/relation/calculations.rb

https://gitlab.com/patrickread/rails
Ruby | 384 lines | 201 code | 49 blank | 134 comment | 45 complexity | 5357ea32764bb4f60ea5dff7ac3c39cd MD5 | raw file
  1. module ActiveRecord
  2. module Calculations
  3. # Count the records.
  4. #
  5. # Person.count
  6. # # => the total count of all people
  7. #
  8. # Person.count(:age)
  9. # # => returns the total count of all people whose age is present in database
  10. #
  11. # Person.count(:all)
  12. # # => performs a COUNT(*) (:all is an alias for '*')
  13. #
  14. # Person.distinct.count(:age)
  15. # # => counts the number of different age values
  16. #
  17. # If +count+ is used with +group+, it returns a Hash whose keys represent the aggregated column,
  18. # and the values are the respective amounts:
  19. #
  20. # Person.group(:city).count
  21. # # => { 'Rome' => 5, 'Paris' => 3 }
  22. #
  23. # If +count+ is used with +group+ for multiple columns, it returns a Hash whose
  24. # keys are an array containing the individual values of each column and the value
  25. # of each key would be the +count+.
  26. #
  27. # Article.group(:status, :category).count
  28. # # => {["draft", "business"]=>10, ["draft", "technology"]=>4,
  29. # ["published", "business"]=>0, ["published", "technology"]=>2}
  30. #
  31. # If +count+ is used with +select+, it will count the selected columns:
  32. #
  33. # Person.select(:age).count
  34. # # => counts the number of different age values
  35. #
  36. # Note: not all valid +select+ expressions are valid +count+ expressions. The specifics differ
  37. # between databases. In invalid cases, an error from the database is thrown.
  38. def count(column_name = nil)
  39. calculate(:count, column_name)
  40. end
  41. # Calculates the average value on a given column. Returns +nil+ if there's
  42. # no row. See +calculate+ for examples with options.
  43. #
  44. # Person.average(:age) # => 35.8
  45. def average(column_name)
  46. calculate(:average, column_name)
  47. end
  48. # Calculates the minimum value on a given column. The value is returned
  49. # with the same data type of the column, or +nil+ if there's no row. See
  50. # +calculate+ for examples with options.
  51. #
  52. # Person.minimum(:age) # => 7
  53. def minimum(column_name)
  54. calculate(:minimum, column_name)
  55. end
  56. # Calculates the maximum value on a given column. The value is returned
  57. # with the same data type of the column, or +nil+ if there's no row. See
  58. # +calculate+ for examples with options.
  59. #
  60. # Person.maximum(:age) # => 93
  61. def maximum(column_name)
  62. calculate(:maximum, column_name)
  63. end
  64. # Calculates the sum of values on a given column. The value is returned
  65. # with the same data type of the column, 0 if there's no row. See
  66. # +calculate+ for examples with options.
  67. #
  68. # Person.sum(:age) # => 4562
  69. def sum(*args)
  70. calculate(:sum, *args)
  71. end
  72. # This calculates aggregate values in the given column. Methods for count, sum, average,
  73. # minimum, and maximum have been added as shortcuts.
  74. #
  75. # There are two basic forms of output:
  76. #
  77. # * Single aggregate value: The single value is type cast to Fixnum for COUNT, Float
  78. # for AVG, and the given column's type for everything else.
  79. #
  80. # * Grouped values: This returns an ordered hash of the values and groups them. It
  81. # takes either a column name, or the name of a belongs_to association.
  82. #
  83. # values = Person.group('last_name').maximum(:age)
  84. # puts values["Drake"]
  85. # # => 43
  86. #
  87. # drake = Family.find_by(last_name: 'Drake')
  88. # values = Person.group(:family).maximum(:age) # Person belongs_to :family
  89. # puts values[drake]
  90. # # => 43
  91. #
  92. # values.each do |family, max_age|
  93. # ...
  94. # end
  95. #
  96. # Person.calculate(:count, :all) # The same as Person.count
  97. # Person.average(:age) # SELECT AVG(age) FROM people...
  98. #
  99. # # Selects the minimum age for any family without any minors
  100. # Person.group(:last_name).having("min(age) > 17").minimum(:age)
  101. #
  102. # Person.sum("2 * age")
  103. def calculate(operation, column_name)
  104. if column_name.is_a?(Symbol) && attribute_alias?(column_name)
  105. column_name = attribute_alias(column_name)
  106. end
  107. if has_include?(column_name)
  108. construct_relation_for_association_calculations.calculate(operation, column_name)
  109. else
  110. perform_calculation(operation, column_name)
  111. end
  112. end
  113. # Use <tt>pluck</tt> as a shortcut to select one or more attributes without
  114. # loading a bunch of records just to grab the attributes you want.
  115. #
  116. # Person.pluck(:name)
  117. #
  118. # instead of
  119. #
  120. # Person.all.map(&:name)
  121. #
  122. # Pluck returns an <tt>Array</tt> of attribute values type-casted to match
  123. # the plucked column names, if they can be deduced. Plucking an SQL fragment
  124. # returns String values by default.
  125. #
  126. # Person.pluck(:id)
  127. # # SELECT people.id FROM people
  128. # # => [1, 2, 3]
  129. #
  130. # Person.pluck(:id, :name)
  131. # # SELECT people.id, people.name FROM people
  132. # # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
  133. #
  134. # Person.pluck('DISTINCT role')
  135. # # SELECT DISTINCT role FROM people
  136. # # => ['admin', 'member', 'guest']
  137. #
  138. # Person.where(age: 21).limit(5).pluck(:id)
  139. # # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
  140. # # => [2, 3]
  141. #
  142. # Person.pluck('DATEDIFF(updated_at, created_at)')
  143. # # SELECT DATEDIFF(updated_at, created_at) FROM people
  144. # # => ['0', '27761', '173']
  145. #
  146. def pluck(*column_names)
  147. column_names.map! do |column_name|
  148. if column_name.is_a?(Symbol) && attribute_alias?(column_name)
  149. attribute_alias(column_name)
  150. else
  151. column_name.to_s
  152. end
  153. end
  154. if has_include?(column_names.first)
  155. construct_relation_for_association_calculations.pluck(*column_names)
  156. else
  157. relation = spawn
  158. relation.select_values = column_names.map { |cn|
  159. columns_hash.key?(cn) ? arel_table[cn] : cn
  160. }
  161. result = klass.connection.select_all(relation.arel, nil, bound_attributes)
  162. result.cast_values(klass.attribute_types)
  163. end
  164. end
  165. # Pluck all the ID's for the relation using the table's primary key
  166. #
  167. # Person.ids # SELECT people.id FROM people
  168. # Person.joins(:companies).ids # SELECT people.id FROM people INNER JOIN companies ON companies.person_id = people.id
  169. def ids
  170. pluck primary_key
  171. end
  172. private
  173. def has_include?(column_name)
  174. eager_loading? || (includes_values.present? && column_name && column_name != :all)
  175. end
  176. def perform_calculation(operation, column_name)
  177. operation = operation.to_s.downcase
  178. # If #count is used with #distinct / #uniq it is considered distinct. (eg. relation.distinct.count)
  179. distinct = self.distinct_value
  180. if operation == "count"
  181. column_name ||= select_for_count
  182. unless arel.ast.grep(Arel::Nodes::OuterJoin).empty?
  183. distinct = true
  184. end
  185. column_name = primary_key if column_name == :all && distinct
  186. distinct = nil if column_name =~ /\s*DISTINCT[\s(]+/i
  187. end
  188. if group_values.any?
  189. execute_grouped_calculation(operation, column_name, distinct)
  190. else
  191. execute_simple_calculation(operation, column_name, distinct)
  192. end
  193. end
  194. def aggregate_column(column_name)
  195. if @klass.column_names.include?(column_name.to_s)
  196. Arel::Attribute.new(@klass.unscoped.table, column_name)
  197. else
  198. Arel.sql(column_name == :all ? "*" : column_name.to_s)
  199. end
  200. end
  201. def operation_over_aggregate_column(column, operation, distinct)
  202. operation == 'count' ? column.count(distinct) : column.send(operation)
  203. end
  204. def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
  205. # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
  206. relation = unscope(:order)
  207. column_alias = column_name
  208. if operation == "count" && (relation.limit_value || relation.offset_value)
  209. # Shortcut when limit is zero.
  210. return 0 if relation.limit_value == 0
  211. query_builder = build_count_subquery(relation, column_name, distinct)
  212. else
  213. column = aggregate_column(column_name)
  214. select_value = operation_over_aggregate_column(column, operation, distinct)
  215. column_alias = select_value.alias
  216. column_alias ||= @klass.connection.column_name_for_operation(operation, select_value)
  217. relation.select_values = [select_value]
  218. query_builder = relation.arel
  219. end
  220. result = @klass.connection.select_all(query_builder, nil, bound_attributes)
  221. row = result.first
  222. value = row && row.values.first
  223. column = result.column_types.fetch(column_alias) do
  224. type_for(column_name)
  225. end
  226. type_cast_calculated_value(value, column, operation)
  227. end
  228. def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
  229. group_attrs = group_values
  230. if group_attrs.first.respond_to?(:to_sym)
  231. association = @klass._reflect_on_association(group_attrs.first)
  232. associated = group_attrs.size == 1 && association && association.belongs_to? # only count belongs_to associations
  233. group_fields = Array(associated ? association.foreign_key : group_attrs)
  234. else
  235. group_fields = group_attrs
  236. end
  237. group_aliases = group_fields.map { |field|
  238. column_alias_for(field)
  239. }
  240. group_columns = group_aliases.zip(group_fields).map { |aliaz,field|
  241. [aliaz, field]
  242. }
  243. group = group_fields
  244. if operation == 'count' && column_name == :all
  245. aggregate_alias = 'count_all'
  246. else
  247. aggregate_alias = column_alias_for([operation, column_name].join(' '))
  248. end
  249. select_values = [
  250. operation_over_aggregate_column(
  251. aggregate_column(column_name),
  252. operation,
  253. distinct).as(aggregate_alias)
  254. ]
  255. select_values += select_values unless having_clause.empty?
  256. select_values.concat group_fields.zip(group_aliases).map { |field,aliaz|
  257. if field.respond_to?(:as)
  258. field.as(aliaz)
  259. else
  260. "#{field} AS #{aliaz}"
  261. end
  262. }
  263. relation = except(:group)
  264. relation.group_values = group
  265. relation.select_values = select_values
  266. calculated_data = @klass.connection.select_all(relation, nil, relation.bound_attributes)
  267. if association
  268. key_ids = calculated_data.collect { |row| row[group_aliases.first] }
  269. key_records = association.klass.base_class.where(association.klass.base_class.primary_key => key_ids)
  270. key_records = Hash[key_records.map { |r| [r.id, r] }]
  271. end
  272. Hash[calculated_data.map do |row|
  273. key = group_columns.map { |aliaz, col_name|
  274. column = calculated_data.column_types.fetch(aliaz) do
  275. type_for(col_name)
  276. end
  277. type_cast_calculated_value(row[aliaz], column)
  278. }
  279. key = key.first if key.size == 1
  280. key = key_records[key] if associated
  281. column_type = calculated_data.column_types.fetch(aggregate_alias) { type_for(column_name) }
  282. [key, type_cast_calculated_value(row[aggregate_alias], column_type, operation)]
  283. end]
  284. end
  285. # Converts the given keys to the value that the database adapter returns as
  286. # a usable column name:
  287. #
  288. # column_alias_for("users.id") # => "users_id"
  289. # column_alias_for("sum(id)") # => "sum_id"
  290. # column_alias_for("count(distinct users.id)") # => "count_distinct_users_id"
  291. # column_alias_for("count(*)") # => "count_all"
  292. # column_alias_for("count", "id") # => "count_id"
  293. def column_alias_for(keys)
  294. if keys.respond_to? :name
  295. keys = "#{keys.relation.name}.#{keys.name}"
  296. end
  297. table_name = keys.to_s.downcase
  298. table_name.gsub!(/\*/, 'all')
  299. table_name.gsub!(/\W+/, ' ')
  300. table_name.strip!
  301. table_name.gsub!(/ +/, '_')
  302. @klass.connection.table_alias_for(table_name)
  303. end
  304. def type_for(field)
  305. field_name = field.respond_to?(:name) ? field.name.to_s : field.to_s.split('.').last
  306. @klass.type_for_attribute(field_name)
  307. end
  308. def type_cast_calculated_value(value, type, operation = nil)
  309. case operation
  310. when 'count' then value.to_i
  311. when 'sum' then type.deserialize(value || 0)
  312. when 'average' then value.respond_to?(:to_d) ? value.to_d : value
  313. else type.deserialize(value)
  314. end
  315. end
  316. # TODO: refactor to allow non-string `select_values` (eg. Arel nodes).
  317. def select_for_count
  318. if select_values.present?
  319. select_values.join(", ")
  320. else
  321. :all
  322. end
  323. end
  324. def build_count_subquery(relation, column_name, distinct)
  325. column_alias = Arel.sql('count_column')
  326. subquery_alias = Arel.sql('subquery_for_count')
  327. aliased_column = aggregate_column(column_name == :all ? 1 : column_name).as(column_alias)
  328. relation.select_values = [aliased_column]
  329. subquery = relation.arel.as(subquery_alias)
  330. sm = Arel::SelectManager.new relation.engine
  331. select_value = operation_over_aggregate_column(column_alias, 'count', distinct)
  332. sm.project(select_value).from(subquery)
  333. end
  334. end
  335. end