Skip to content

Commit

Permalink
Include selects in group query with having clause
Browse files Browse the repository at this point in the history
When a grouped calculation contains a having clause that references a
selected value, we need to include that selected value in the query.

Postgres doesn't support referencing a selected value in a having
clause, but other databases do; we can skip the test on the pg adapter
but run it for the others.

This was fixed before in 9a298a1, but the test coverage was lost in
5a05207. The fix regressed in 6311975 and was removed in 97d46c1.

(cherry picked from commit 1812568)
  • Loading branch information
eugeneius authored and pixeltrix committed Feb 26, 2017
1 parent 6a48e56 commit bd0a6c0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Fix regression of #1969 with SELECT aliases in HAVING clause.

*Eugene Kenny*

* Fix `wait_timeout` to configurable for mysql2 adapter.

Fixes #26556.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -282,7 +282,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
operation,
distinct).as(aggregate_alias)
]
select_values += select_values unless having_clause.empty?
select_values += self.select_values unless having_clause.empty?

select_values.concat group_columns.map { |aliaz, field|
if field.respond_to?(:as)
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/cases/calculations_test.rb
Expand Up @@ -235,7 +235,8 @@ def test_should_group_by_summed_field_having_condition
end

def test_should_group_by_summed_field_having_condition_from_select
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit)
skip if current_adapter?(:PostgreSQLAdapter)
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit)
assert_nil c[1]
assert_equal 60, c[2]
assert_equal 53, c[9]
Expand Down

0 comments on commit bd0a6c0

Please sign in to comment.