diff options
author | Raimonds Simanovskis <raimonds.simanovskis@gmail.com> | 2010-06-19 16:02:51 +0300 |
---|---|---|
committer | Raimonds Simanovskis <raimonds.simanovskis@gmail.com> | 2010-06-19 16:02:51 +0300 |
commit | 0ffaa8d4628702bc44a1302fb9a37b92a52f9a22 (patch) | |
tree | 7ae0a55b92415dd733a2a21b441903f3584f709f | |
parent | bbddfe7641bf1f81ba47bd3deaa56edd13d5083c (diff) | |
download | rails-0ffaa8d4628702bc44a1302fb9a37b92a52f9a22.tar.gz rails-0ffaa8d4628702bc44a1302fb9a37b92a52f9a22.tar.bz2 rails-0ffaa8d4628702bc44a1302fb9a37b92a52f9a22.zip |
use subquery to get first records if DISTINCT is used (in Oracle compiler)
-rw-r--r-- | lib/arel/engines/sql/compilers/oracle_compiler.rb | 4 | ||||
-rw-r--r-- | spec/engines/sql/unit/relations/take_spec.rb | 8 |
2 files changed, 10 insertions, 2 deletions
diff --git a/lib/arel/engines/sql/compilers/oracle_compiler.rb b/lib/arel/engines/sql/compilers/oracle_compiler.rb index ce575c2170..560022445a 100644 --- a/lib/arel/engines/sql/compilers/oracle_compiler.rb +++ b/lib/arel/engines/sql/compilers/oracle_compiler.rb @@ -5,9 +5,9 @@ module Arel def select_sql where_clauses_array = where_clauses if limit_or_offset = !taken.blank? || !skipped.blank? - # if need to select first records without ORDER BY and GROUP BY + # if need to select first records without ORDER BY and GROUP BY and without DISTINCT # then can use simple ROWNUM in WHERE clause - if skipped.blank? && groupings.blank? && orders.blank? + if skipped.blank? && groupings.blank? && orders.blank? && select_clauses[0] !~ /^DISTINCT / where_clauses_array << "ROWNUM <= #{taken}" if !taken.blank? && skipped.blank? && groupings.blank? && orders.blank? limit_or_offset = false end diff --git a/spec/engines/sql/unit/relations/take_spec.rb b/spec/engines/sql/unit/relations/take_spec.rb index 9f2967a0bd..cc3c3dbbf5 100644 --- a/spec/engines/sql/unit/relations/take_spec.rb +++ b/spec/engines/sql/unit/relations/take_spec.rb @@ -34,6 +34,14 @@ module Arel ORDER BY "USERS"."ID" ASC) where rownum <= 4 }) + + sql_with_distinct = Take.new(@relation.project('DISTINCT "USERS"."ID"'), @taken).to_sql + sql_with_distinct.should be_like(%Q{ + select * from + (SELECT DISTINCT "USERS"."ID" + FROM "USERS") + where rownum <= 4 + }) end adapter_is_not :mysql, :oracle do |