aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/arel/engines/sql/compilers/oracle_compiler.rb4
-rw-r--r--spec/engines/sql/unit/relations/take_spec.rb8
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