Skip to content

Commit 9f98066

Browse files
authored
Merge pull request rails#43547 from denkungsart/ar-query-endless-range
Properly handle impossible cases in (not_)between
2 parents 790705c + 9260451 commit 9f98066

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

activerecord/lib/arel/predications.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ def between(other)
3939
self.in([])
4040
elsif open_ended?(other.begin)
4141
if open_ended?(other.end)
42-
not_in([])
42+
if infinity?(other.begin) == 1 || infinity?(other.end) == -1
43+
self.in([])
44+
else
45+
not_in([])
46+
end
4347
elsif other.exclude_end?
4448
lt(other.end)
4549
else
@@ -80,7 +84,11 @@ def not_between(other)
8084
not_in([])
8185
elsif open_ended?(other.begin)
8286
if open_ended?(other.end)
83-
self.in([])
87+
if infinity?(other.begin) == 1 || infinity?(other.end) == -1
88+
not_in([])
89+
else
90+
self.in([])
91+
end
8492
elsif other.exclude_end?
8593
gteq(other.end)
8694
else

activerecord/test/cases/arel/attributes/attribute_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,20 @@ class AttributeTest < Arel::Spec
668668
)
669669
end
670670

671+
it "can be constructed with an endless range starting from Infinity" do
672+
attribute = Attribute.new nil, nil
673+
node = attribute.between(::Float::INFINITY..)
674+
675+
_(node).must_equal Nodes::In.new(attribute, [])
676+
end
677+
678+
it "can be constructed with a beginless range ending in -Infinity" do
679+
attribute = Attribute.new nil, nil
680+
node = attribute.between(..-::Float::INFINITY)
681+
682+
_(node).must_equal Nodes::In.new(attribute, [])
683+
end
684+
671685
it "can be constructed with an exclusive range" do
672686
attribute = Attribute.new nil, nil
673687
node = attribute.between(0...3)
@@ -877,6 +891,20 @@ class AttributeTest < Arel::Spec
877891
)
878892
end
879893

894+
it "can be constructed with an endless range starting from Infinity" do
895+
attribute = Attribute.new nil, nil
896+
node = attribute.not_between(::Float::INFINITY..)
897+
898+
_(node).must_equal Nodes::NotIn.new(attribute, [])
899+
end
900+
901+
it "can be constructed with a beginless range ending in -Infinity" do
902+
attribute = Attribute.new nil, nil
903+
node = attribute.not_between(..-::Float::INFINITY)
904+
905+
_(node).must_equal Nodes::NotIn.new(attribute, [])
906+
end
907+
880908
it "can be constructed with an exclusive range" do
881909
attribute = Attribute.new nil, nil
882910
node = attribute.not_between(0...3)

0 commit comments

Comments
 (0)