Skip to content

Commit

Permalink
Merge pull request #34303 from kamipo/lazy_checking_boundable
Browse files Browse the repository at this point in the history
Lazy checking whether or not values in IN clause are boundable
  • Loading branch information
rafaelfranca committed Oct 24, 2018
2 parents eba2ba8 + ce40073 commit d3e6465
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
Expand Up @@ -10,8 +10,17 @@ def accept(*)
super
end

def visit_Arel_Nodes_In(*)
def visit_Arel_Nodes_In(o, collector)
@preparable = false

if Array === o.right && !o.right.empty?
o.right.delete_if do |bind|
if Arel::Nodes::BindParam === bind && Relation::QueryAttribute === bind.value
!bind.value.boundable?
end
end
end

super
end

Expand Down
Expand Up @@ -22,9 +22,8 @@ def call(attribute, value)
when 1 then predicate_builder.build(attribute, values.first)
else
values.map! do |v|
bind = predicate_builder.build_bind_attribute(attribute.name, v)
bind if bind.value.boundable?
end.compact!
predicate_builder.build_bind_attribute(attribute.name, v)
end
values.empty? ? NullPredicate : attribute.in(values)
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -34,7 +34,7 @@ class EagerLoadingTooManyIdsTest < ActiveRecord::TestCase
fixtures :citations

def test_preloading_too_many_ids
assert_equal Citation.count, Citation.preload(:citations).to_a.size
assert_equal Citation.count, Citation.preload(:reference_of).to_a.size
end

def test_eager_loading_too_may_ids
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/bind_parameter_test.rb
Expand Up @@ -36,7 +36,7 @@ def teardown

def test_too_many_binds
bind_params_length = @connection.send(:bind_params_length)
topics = Topic.where(id: (1 .. bind_params_length + 1).to_a)
topics = Topic.where(id: (1 .. bind_params_length).to_a << 2**63)
assert_equal Topic.count, topics.count
end

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/citations.yml
@@ -1,4 +1,5 @@
<% 65536.times do |i| %>
fixture_no_<%= i %>:
id: <%= i %>
book2_id: <%= i*i %>
<% end %>
6 changes: 3 additions & 3 deletions activerecord/test/schema/schema.rb
Expand Up @@ -93,7 +93,7 @@
t.integer :pirate_id
end

create_table :books, force: true do |t|
create_table :books, id: :integer, force: true do |t|
t.references :author
t.string :format
t.column :name, :string
Expand Down Expand Up @@ -158,8 +158,8 @@
end

create_table :citations, force: true do |t|
t.column :book1_id, :integer
t.column :book2_id, :integer
t.references :book1
t.references :book2
t.references :citation
end

Expand Down

0 comments on commit d3e6465

Please sign in to comment.