diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-01-15 01:45:51 +0900 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2017-02-06 22:41:44 -0700 |
commit | 28977f1fa3d7b15c1608174a165e60b71ddf3995 (patch) | |
tree | 9aab1ec695f9c01b6be74e95b73230946624e893 /activerecord | |
parent | 968e66b5df92e5ae88c033dc552e17b4c736874b (diff) | |
download | rails-28977f1fa3d7b15c1608174a165e60b71ddf3995.tar.gz rails-28977f1fa3d7b15c1608174a165e60b71ddf3995.tar.bz2 rails-28977f1fa3d7b15c1608174a165e60b71ddf3995.zip |
Remove useless `column_alias` in `subquery_for_count`
If select clause is specified and last column has a column alias,
additional column alias causes a statement invalid.
Add test coverage for counting a single column with NULL values.
Fixes #27676, #27682, and #27705.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 5 |
2 files changed, 5 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 35c670f1a1..e8aba02611 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -364,15 +364,14 @@ module ActiveRecord end def build_count_subquery(relation, column_name, distinct) - column_alias = Arel.sql("count_column") subquery_alias = Arel.sql("subquery_for_count") - aliased_column = aggregate_column(column_name == :all ? 1 : column_name).as(column_alias) + aliased_column = aggregate_column(column_name == :all ? "1" : column_name) relation.select_values = [aliased_column] subquery = relation.arel.as(subquery_alias) sm = Arel::SelectManager.new relation.engine - select_value = operation_over_aggregate_column(column_alias, "count", distinct) + select_value = operation_over_aggregate_column(Arel.star, "count", distinct) sm.project(select_value).from(subquery) end end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index b29733a676..0de2fea4c2 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -166,14 +166,15 @@ class CalculationsTest < ActiveRecord::TestCase end def test_limit_should_apply_before_count - accounts = Account.limit(3).where("firm_id IS NOT NULL") + accounts = Account.limit(4) assert_equal 3, accounts.count(:firm_id) assert_equal 3, accounts.select(:firm_id).count + assert_equal 3, accounts.select("firm_id firm_id").count end def test_limit_should_apply_before_count_arel_attribute - accounts = Account.limit(3).where("firm_id IS NOT NULL") + accounts = Account.limit(4) firm_id_attribute = Account.arel_table[:firm_id] assert_equal 3, accounts.count(firm_id_attribute) |