Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support Float::INFINITY in ranges #164

Merged
merged 1 commit into from

3 participants

@tpope

As discussed on rubyonrails-core.

@lunks

I have recently tried to create a query this way intuitively. Nice to see support for it!

@rafaelfranca

This seems good. I just need to check if we need to support ruby 1.8 in arel master.

@tpope

I can rewrite it with the gross old 1.0/0 if need be.

@rafaelfranca rafaelfranca merged commit da8f6c7 into rails:master
@tpope tpope deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 19, 2013
  1. @tpope
This page is out of date. Refresh to see the latest.
Showing with 52 additions and 2 deletions.
  1. +18 −2 lib/arel/predications.rb
  2. +34 −0 test/visitors/test_to_sql.rb
View
20 lib/arel/predications.rb
@@ -29,7 +29,15 @@ def in other
when Arel::SelectManager
Arel::Nodes::In.new(self, other.ast)
when Range
- if other.exclude_end?
+ if other.begin == -Float::INFINITY && other.end == Float::INFINITY
+ Nodes::NotIn.new self, []
+ elsif other.end == Float::INFINITY
+ Nodes::GreaterThanOrEqual.new(self, other.begin)
+ elsif other.begin == -Float::INFINITY && other.exclude_end?
+ Nodes::LessThan.new(self, other.end)
+ elsif other.begin == -Float::INFINITY
+ Nodes::LessThanOrEqual.new(self, other.end)
+ elsif other.exclude_end?
left = Nodes::GreaterThanOrEqual.new(self, other.begin)
right = Nodes::LessThan.new(self, other.end)
Nodes::And.new [left, right]
@@ -54,7 +62,15 @@ def not_in other
when Arel::SelectManager
Arel::Nodes::NotIn.new(self, other.ast)
when Range
- if other.exclude_end?
+ if other.begin == -Float::INFINITY && other.end == Float::INFINITY
+ Nodes::In.new self, []
+ elsif other.end == Float::INFINITY
+ Nodes::LessThan.new(self, other.begin)
+ elsif other.begin == -Float::INFINITY && other.exclude_end?
+ Nodes::GreaterThanOrEqual.new(self, other.end)
+ elsif other.begin == -Float::INFINITY
+ Nodes::GreaterThan.new(self, other.end)
+ elsif other.exclude_end?
left = Nodes::LessThan.new(self, other.begin)
right = Nodes::GreaterThanOrEqual.new(self, other.end)
Nodes::Or.new left, right
View
34 test/visitors/test_to_sql.rb
@@ -230,6 +230,23 @@ def dispatch
}
end
+ it 'can handle ranges bounded by infinity' do
+ node = @attr.in 1..Float::INFINITY
+ @visitor.accept(node).must_be_like %{
+ "users"."id" >= 1
+ }
+ node = @attr.in(-Float::INFINITY..3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" <= 3
+ }
+ node = @attr.in(-Float::INFINITY...3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" < 3
+ }
+ node = @attr.in(-Float::INFINITY..Float::INFINITY)
+ @visitor.accept(node).must_be_like %{1=1}
+ end
+
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))
@@ -316,6 +333,23 @@ def quote value, column = nil
}
end
+ it 'can handle ranges bounded by infinity' do
+ node = @attr.not_in 1..Float::INFINITY
+ @visitor.accept(node).must_be_like %{
+ "users"."id" < 1
+ }
+ node = @attr.not_in(-Float::INFINITY..3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" > 3
+ }
+ node = @attr.not_in(-Float::INFINITY...3)
+ @visitor.accept(node).must_be_like %{
+ "users"."id" >= 3
+ }
+ node = @attr.not_in(-Float::INFINITY..Float::INFINITY)
+ @visitor.accept(node).must_be_like %{1=0}
+ end
+
it 'can handle subqueries' do
table = Table.new(:users)
subquery = table.project(:id).where(table[:name].eq('Aaron'))
Something went wrong with that request. Please try again.