Skip to content

Commit

Permalink
Fix literalization of infinite and NaN float values in PostgreSQL arr…
Browse files Browse the repository at this point in the history
…ay bound variables
  • Loading branch information
jeremyevans committed Aug 8, 2023
1 parent 3ad4147 commit c33ec61
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
=== master

* Fix literalization of infinite and NaN float values in PostgreSQL array bound variables (jeremyevans)

=== 5.71.0 (2023-08-01)

* Support ILIKE ANY on PostgreSQL by not forcing the use of ESCAPE for ILIKE (gilesbowkett) (#2066)
Expand Down
8 changes: 8 additions & 0 deletions lib/sequel/extensions/pg_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ def bound_variable_array(a)
a
when String
bound_variable_array_string(a)
when Float
if a.infinite?
a > 0 ? '"Infinity"' : '"-Infinity"'
elsif a.nan?
'"NaN"'
else
literal(a)
end
else
if (s = bound_variable_arg(a, nil)).is_a?(String)
bound_variable_array_string(s)
Expand Down
10 changes: 10 additions & 0 deletions spec/extensions/pg_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ def quote_identifiers?; false end
c.call('{{{1,2},{3,4}},{{5,6},{7,8}}}').to_a.must_equal [[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]
end

it "should parse infinite and NaN values in arrays" do
c = @converter[1022]
i, ni, nan = a = c.call('{"Infinity","-Infinity","NaN"}').to_a
a.all?{|x| Float === x}.must_equal true
i.infinite?.must_equal 1
ni.infinite?.must_equal(-1)
nan.nan?.must_equal true
end

it "should parse single dimensional decimal arrays" do
c = @converter[1231]
c.call("{}").to_a.must_equal []
Expand Down Expand Up @@ -200,6 +209,7 @@ def quote_identifiers?; false end
@db.bound_variable_arg([1,2], nil).must_equal '{1,2}'
@db.bound_variable_arg([[1,2]], nil).must_equal '{{1,2}}'
@db.bound_variable_arg([1.0,2.0], nil).must_equal '{1.0,2.0}'
@db.bound_variable_arg([1.0/0.0, -1.0/0.0, 0.0/0.0], nil).must_equal '{"Infinity","-Infinity","NaN"}'
@db.bound_variable_arg([Sequel.lit('a'), Sequel.blob("a\0'\"")], nil).must_equal '{a,"a\\\\000\\\\047\\""}'
@db.bound_variable_arg(["\\ \"", 'NULL', nil], nil).must_equal '{"\\\\ \\"","NULL",NULL}'
end
Expand Down

0 comments on commit c33ec61

Please sign in to comment.