Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13334 from gregolsen/postgresql_infinity

pg, fix Infinity and NaN values conversion.

Closes #13334.

* rebased
* removed guard
* inlined guard into case statement
  • Loading branch information...
commit 4320b77804e090ff8415dbbc79dde6c391f09c7c 2 parents f25f533 + aac40bc
@senny senny authored
View
15 activerecord/CHANGELOG.md
@@ -1,3 +1,18 @@
+* Fix `PostgreSQLAdapter::OID::Float#type_cast` to convert Infinity and
+ NaN PostgreSQL values into a native Ruby `Float::INFINITY` and `Float::NAN`
+
+ Example:
+
+ # Before
+ Point.create(value: 1.0/0)
+ Point.last.value # => 0.0
+
+ # After
+ Point.create(value: 1.0/0)
+ Point.last.value # => Infinity
+
+ *Innokenty Mikhailov*
+
* Allow the PostgreSQL adapter to handle bigserial pk types again.
Fixes #10410.
View
11 activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
@@ -249,9 +249,14 @@ class Float < Type
def type; :float end
def type_cast(value)
- return if value.nil?
-
- value.to_f
+ case value
+ when nil; nil
+ when 'Infinity'; ::Float::INFINITY
+ when '-Infinity'; -::Float::INFINITY
+ when 'NaN'; ::Float::NAN
+ else
+ value.to_f
+ end
end
end
View
7 activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -50,7 +50,11 @@ def setup
@second_money = PostgresqlMoney.find(2)
@connection.execute("INSERT INTO postgresql_numbers (id, single, double) VALUES (1, 123.456, 123456.789)")
+ @connection.execute("INSERT INTO postgresql_numbers (id, single, double) VALUES (2, '-Infinity', 'Infinity')")
+ @connection.execute("INSERT INTO postgresql_numbers (id, single, double) VALUES (3, 123.456, 'NaN')")
@first_number = PostgresqlNumber.find(1)
+ @second_number = PostgresqlNumber.find(2)
+ @third_number = PostgresqlNumber.find(3)
@connection.execute("INSERT INTO postgresql_times (id, time_interval, scaled_time_interval) VALUES (1, '1 year 2 days ago', '3 weeks ago')")
@first_time = PostgresqlTime.find(1)
@@ -154,6 +158,9 @@ def test_update_tsvector
def test_number_values
assert_equal 123.456, @first_number.single
assert_equal 123456.789, @first_number.double
+ assert_equal -::Float::INFINITY, @second_number.single
+ assert_equal ::Float::INFINITY, @second_number.double
+ assert_same ::Float::NAN, @third_number.double
end
def test_time_values
Please sign in to comment.
Something went wrong with that request. Please try again.