Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix limit with GROUP BY in Oracle #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/arel/visitors/oracle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
10 changes: 10 additions & 0 deletions test/visitors/test_oracle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down