diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-09-24 11:24:14 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-09-24 11:24:14 -0700 |
commit | 0138356d9100ab1a6cccf0250932f93df7950a62 (patch) | |
tree | 2535264d9f42b9a38c4b6e08455161e6359b6d48 | |
parent | 08d034a89659f680438092c3eee29baead2e2d83 (diff) | |
parent | efdda5030a460e8af58ef0f7f4cd5ce8d7f1b541 (diff) | |
download | rails-0138356d9100ab1a6cccf0250932f93df7950a62.tar.gz rails-0138356d9100ab1a6cccf0250932f93df7950a62.tar.bz2 rails-0138356d9100ab1a6cccf0250932f93df7950a62.zip |
Merge pull request #140 from sunaku/GH-139
bugfix: some aggregations lacked DISTINCT emission
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 8 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 39 |
2 files changed, 43 insertions, 4 deletions
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index f5be934ba5..b194319002 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -287,22 +287,22 @@ key on UpdateManager using UpdateManager#key= end def visit_Arel_Nodes_Sum o - "SUM(#{o.expressions.map { |x| + "SUM(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" end def visit_Arel_Nodes_Max o - "MAX(#{o.expressions.map { |x| + "MAX(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" end def visit_Arel_Nodes_Min o - "MIN(#{o.expressions.map { |x| + "MIN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" end def visit_Arel_Nodes_Avg o - "AVG(#{o.expressions.map { |x| + "AVG(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index 064f14739c..08cf4566d1 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -48,6 +48,45 @@ module Arel sql.must_be_like %{ omg(*) = 2 } end + it 'should visit built-in functions' do + function = Nodes::Count.new([Arel.star]) + assert_equal 'COUNT(*)', @visitor.accept(function) + + function = Nodes::Sum.new([Arel.star]) + assert_equal 'SUM(*)', @visitor.accept(function) + + function = Nodes::Max.new([Arel.star]) + assert_equal 'MAX(*)', @visitor.accept(function) + + function = Nodes::Min.new([Arel.star]) + assert_equal 'MIN(*)', @visitor.accept(function) + + function = Nodes::Avg.new([Arel.star]) + assert_equal 'AVG(*)', @visitor.accept(function) + end + + it 'should visit built-in functions operating on distinct values' do + function = Nodes::Count.new([Arel.star]) + function.distinct = true + assert_equal 'COUNT(DISTINCT *)', @visitor.accept(function) + + function = Nodes::Sum.new([Arel.star]) + function.distinct = true + assert_equal 'SUM(DISTINCT *)', @visitor.accept(function) + + function = Nodes::Max.new([Arel.star]) + function.distinct = true + assert_equal 'MAX(DISTINCT *)', @visitor.accept(function) + + function = Nodes::Min.new([Arel.star]) + function.distinct = true + assert_equal 'MIN(DISTINCT *)', @visitor.accept(function) + + function = Nodes::Avg.new([Arel.star]) + function.distinct = true + assert_equal 'AVG(DISTINCT *)', @visitor.accept(function) + end + it 'works with lists' do function = Nodes::NamedFunction.new('omg', [Arel.star, Arel.star]) assert_equal 'omg(*, *)', @visitor.accept(function) |