Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix limit with GROUP BY in Oracle #129

Closed
wants to merge 1 commit into from

2 participants

@cdinger

I think this fix is fairly straight-forward. There's also a test case here demonstrating the problem.

References #128

@matthewd matthewd closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 14, 2012
  1. Wrap group_by sql with a subquery for limiting rows in Oracle.

    Chris Dinger authored
    References #128
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 1 deletion.
  1. +1 −1  lib/arel/visitors/oracle.rb
  2. +10 −0 test/visitors/test_oracle.rb
View
2  lib/arel/visitors/oracle.rb
@@ -8,7 +8,7 @@ def visit_Arel_Nodes_SelectStatement o
# if need to select first records without ORDER BY and GROUP BY and without DISTINCT
# then can use simple ROWNUM in WHERE clause
- if o.limit && o.orders.empty? && !o.offset && o.cores.first.projections.first !~ /^DISTINCT /
+ if o.limit && o.orders.empty? && o.cores.first.groups.empty? && !o.offset && o.cores.first.projections.first !~ /^DISTINCT /
o.cores.last.wheres.push Nodes::LessThanOrEqual.new(
Nodes::SqlLiteral.new('ROWNUM'), o.limit.expr
)
View
10 test/visitors/test_oracle.rb
@@ -83,6 +83,16 @@ module Visitors
}
end
+ it 'creates a subquery when there is group by' do
+ stmt = Nodes::SelectStatement.new
+ stmt.cores.first.groups << Nodes::SqlLiteral.new('foo')
+ stmt.limit = Nodes::Limit.new(10)
+ sql = @visitor.accept stmt
+ sql.must_be_like %{
+ SELECT * FROM (SELECT GROUP BY foo) WHERE ROWNUM <= 10
+ }
+ end
+
it 'creates a subquery when there is DISTINCT' do
stmt = Nodes::SelectStatement.new
stmt.cores.first.projections << Nodes::SqlLiteral.new('DISTINCT id')
Something went wrong with that request. Please try again.