Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle infinity value in PostgreSQL range type #31617

Merged
merged 1 commit into from Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -60,7 +60,7 @@ def type_cast_single(value)
end

def type_cast_single_for_database(value)
infinity?(value) ? "" : @subtype.serialize(value)
infinity?(value) ? value : @subtype.serialize(value)
end

def extract_bounds(value)
Expand Down
Expand Up @@ -138,7 +138,7 @@ def encode_array(array_data)
end

def encode_range(range)
"[#{type_cast(range.first)},#{type_cast(range.last)}#{range.exclude_end? ? ')' : ']'}"
"[#{type_cast_range_value(range.first)},#{type_cast_range_value(range.last)}#{range.exclude_end? ? ')' : ']'}"
end

def determine_encoding_of_strings_in_array(value)
Expand All @@ -154,6 +154,14 @@ def type_cast_array(values)
else _type_cast(values)
end
end

def type_cast_range_value(value)
infinity?(value) ? "" : type_cast(value)
end

def infinity?(value)
value.respond_to?(:infinite?) && value.infinite?
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/adapters/postgresql/range_test.rb
Expand Up @@ -358,6 +358,18 @@ def test_ranges_correctly_escape_input
end
end

def test_infinity_values
PostgresqlRange.create!(int4_range: 1..Float::INFINITY,
int8_range: -Float::INFINITY..0,
float_range: -Float::INFINITY..Float::INFINITY)

record = PostgresqlRange.first

assert_equal(1...Float::INFINITY, record.int4_range)
assert_equal(-Float::INFINITY...1, record.int8_range)
assert_equal(-Float::INFINITY...Float::INFINITY, record.float_range)
end

private
def assert_equal_round_trip(range, attribute, value)
round_trip(range, attribute, value)
Expand Down