aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2015-12-23 22:38:41 +0900
committerRyuta Kamizono <kamipo@gmail.com>2015-12-23 22:47:44 +0900
commit9c5732d23447a2880dafc7f80254b1c9e42863f8 (patch)
treeaf10e717cc2feb94c56fc0eb54fbb9cabd52bf45 /activerecord
parenta093afd8ce6d15ea38972141f6000fe3798b6667 (diff)
downloadrails-9c5732d23447a2880dafc7f80254b1c9e42863f8.tar.gz
rails-9c5732d23447a2880dafc7f80254b1c9e42863f8.tar.bz2
rails-9c5732d23447a2880dafc7f80254b1c9e42863f8.zip
Avoid `distinct` if a subquery has already materialized
Follow up to #19359 and avoid #22241.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb8
1 files changed, 5 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index f8c9e13392..b9a9c22198 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -965,11 +965,13 @@ module ActiveRecord
subsubselect = select.clone
subsubselect.projections = [key]
+ # Materialize subquery by adding distinct
+ # to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
+ subsubselect.distinct unless select.limit || select.offset || select.orders.any?
+
subselect = Arel::SelectManager.new(select.engine)
subselect.project Arel.sql(key.name)
- # Materialized subquery by adding distinct
- # to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
- subselect.from subsubselect.distinct.as('__active_record_temp')
+ subselect.from subsubselect.as('__active_record_temp')
end
def mariadb?