Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

bugfix: some aggregations lacked DISTINCT emission #140

Merged
merged 1 commit into from Sep 24, 2012
Jump to file or symbol
Failed to load files and symbols.
+43 −4
Split
@@ -283,22 +283,22 @@ def visit_Arel_Nodes_Count o
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
@@ -48,6 +48,45 @@ def dispatch
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)