aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-03-01 23:25:01 +0900
committerGitHub <noreply@github.com>2019-03-01 23:25:01 +0900
commit6568d7da636a146578a13cb3a9a096d69dd811a7 (patch)
tree01486c267083ec1743f60ce601b0eb1a4cc682da /activerecord
parentc82705b15faaf12d568272e86afb6ad2c151007f (diff)
parentbff5e799c056a1914653ae53166949b4adff3d4b (diff)
downloadrails-6568d7da636a146578a13cb3a9a096d69dd811a7.tar.gz
rails-6568d7da636a146578a13cb3a9a096d69dd811a7.tar.bz2
rails-6568d7da636a146578a13cb3a9a096d69dd811a7.zip
Merge pull request #35441 from kamipo/allow_from_with_index_hint
Relax table name detection in `from` to allow any extension like INDEX hint
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb7
-rw-r--r--activerecord/test/cases/relations_test.rb32
2 files changed, 27 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index f88493df8a..24a50db619 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -1091,14 +1091,17 @@ module ActiveRecord
field = klass.attribute_alias(field) if klass.attribute_alias?(field)
from = from_clause.name || from_clause.value
- if klass.columns_hash.key?(field) &&
- (!from || from == table.name || from == connection.quote_table_name(table.name))
+ if klass.columns_hash.key?(field) && (!from || table_name_matches?(from))
arel_attribute(field)
else
yield
end
end
+ def table_name_matches?(from)
+ /(?:\A|(?<!FROM)\s)(?:\b#{table.name}\b|#{connection.quote_table_name(table.name)})(?!\.)/i.match?(from.to_s)
+ end
+
def reverse_sql_order(order_query)
if order_query.empty?
return [arel_attribute(primary_key).desc] if primary_key
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index a7f09e6de0..85719cb8d0 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -182,27 +182,27 @@ class RelationTest < ActiveRecord::TestCase
end
end
- def test_select_with_original_table_name_in_from
+ def test_select_with_from_includes_original_table_name
relation = Comment.joins(:post).select(:id).order(:id)
- subquery = Comment.from(Comment.table_name).joins(:post).select(:id).order(:id)
+ subquery = Comment.from("#{Comment.table_name} /*! USE INDEX (PRIMARY) */").joins(:post).select(:id).order(:id)
assert_equal relation.map(&:id), subquery.map(&:id)
end
- def test_pluck_with_original_table_name_in_from
+ def test_pluck_with_from_includes_original_table_name
relation = Comment.joins(:post).order(:id)
- subquery = Comment.from(Comment.table_name).joins(:post).order(:id)
+ subquery = Comment.from("#{Comment.table_name} /*! USE INDEX (PRIMARY) */").joins(:post).order(:id)
assert_equal relation.pluck(:id), subquery.pluck(:id)
end
- def test_select_with_quoted_original_table_name_in_from
+ def test_select_with_from_includes_quoted_original_table_name
relation = Comment.joins(:post).select(:id).order(:id)
- subquery = Comment.from(Comment.quoted_table_name).joins(:post).select(:id).order(:id)
+ subquery = Comment.from("#{Comment.quoted_table_name} /*! USE INDEX (PRIMARY) */").joins(:post).select(:id).order(:id)
assert_equal relation.map(&:id), subquery.map(&:id)
end
- def test_pluck_with_quoted_original_table_name_in_from
+ def test_pluck_with_from_includes_quoted_original_table_name
relation = Comment.joins(:post).order(:id)
- subquery = Comment.from(Comment.quoted_table_name).joins(:post).order(:id)
+ subquery = Comment.from("#{Comment.quoted_table_name} /*! USE INDEX (PRIMARY) */").joins(:post).order(:id)
assert_equal relation.pluck(:id), subquery.pluck(:id)
end
@@ -221,13 +221,25 @@ class RelationTest < ActiveRecord::TestCase
def test_select_with_subquery_in_from_does_not_use_original_table_name
relation = Comment.group(:type).select("COUNT(post_id) AS post_count, type")
- subquery = Comment.from(relation).select("type", "post_count")
+ subquery = Comment.from(relation, "grouped_#{Comment.table_name}").select("type", "post_count")
assert_equal(relation.map(&:post_count).sort, subquery.map(&:post_count).sort)
end
def test_group_with_subquery_in_from_does_not_use_original_table_name
relation = Comment.group(:type).select("COUNT(post_id) AS post_count,type")
- subquery = Comment.from(relation).group("type").average("post_count")
+ subquery = Comment.from(relation, "grouped_#{Comment.table_name}").group("type").average("post_count")
+ assert_equal(relation.map(&:post_count).sort, subquery.values.sort)
+ end
+
+ def test_select_with_subquery_string_in_from_does_not_use_original_table_name
+ relation = Comment.group(:type).select("COUNT(post_id) AS post_count, type")
+ subquery = Comment.from("(#{relation.to_sql}) #{Comment.table_name}_grouped").select("type", "post_count")
+ assert_equal(relation.map(&:post_count).sort, subquery.map(&:post_count).sort)
+ end
+
+ def test_group_with_subquery_string_in_from_does_not_use_original_table_name
+ relation = Comment.group(:type).select("COUNT(post_id) AS post_count,type")
+ subquery = Comment.from("(#{relation.to_sql}) #{Comment.table_name}_grouped").group("type").average("post_count")
assert_equal(relation.map(&:post_count).sort, subquery.values.sort)
end