Permalink
Browse files

use subquery for limit when DISTINCT is used (in Oracle)

  • Loading branch information...
1 parent 538faf2 commit e619462165bfab5bee85f9761df7f48a6b8f30f0 @rsim rsim committed Sep 29, 2010
Showing with 14 additions and 2 deletions.
  1. +4 −2 lib/arel/visitors/oracle.rb
  2. +10 −0 spec/visitors/oracle_spec.rb
@@ -6,7 +6,9 @@ class Oracle < Arel::Visitors::ToSql
def visit_Arel_Nodes_SelectStatement o
o = order_hacks(o)
- if o.limit && o.orders.empty? && !o.offset
+ # 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 /
o.cores.last.wheres.push Nodes::LessThanOrEqual.new(
Nodes::SqlLiteral.new('ROWNUM'), o.limit
)
@@ -31,7 +33,7 @@ def visit_Arel_Nodes_SelectStatement o
eosql
end
- if o.limit && !o.orders.empty?
+ if o.limit
o = o.dup
limit = o.limit
o.limit = nil
@@ -71,6 +71,16 @@ module Visitors
}
end
+ it 'creates a subquery when there is DISTINCT' do
+ stmt = Nodes::SelectStatement.new
+ stmt.cores.first.projections << Nodes::SqlLiteral.new('DISTINCT id')
+ stmt.limit = 10
+ sql = @visitor.accept stmt
+ sql.should be_like %{
+ SELECT * FROM (SELECT DISTINCT id) WHERE ROWNUM <= 10
+ }
+ end
+
it 'creates a different subquery when there is an offset' do
stmt = Nodes::SelectStatement.new
stmt.limit = 10

0 comments on commit e619462

Please sign in to comment.