Skip to content

Read an integral JSON float as an integer in json_get_int - #147

Merged
adriangb merged 1 commit into
mainfrom
json-get-int-accepts-integral-floats
Sep 29, 2026
Merged

adriangb merged 1 commit into
mainfrom
json-get-int-accepts-integral-floats

Conversation

@adriangb

Copy link
Copy Markdown
Collaborator

json_get_int, and so CAST(j -> 'k' AS BIGINT) and ::int, returns NULL for a JSON number written as a float, even when its value is an integer:

select json_get_int('{"k": 1.0}', 'k'), json_get_int('{"k": 2e3}', 'k'), json_get_int('{"k": 1.5}', 'k');
-- before: NULL, NULL, NULL
-- after:  1, 2000, NULL

JSON has one number type, and many serializers write whole numbers as 1.0. Other engines convert 1.0 to 1.

With this change a JSON float with an integral value in the i64 range gives that integer, and -0.0 gives 0. These stay NULL:

  • a fractional value (1.5 is not rounded or truncated)
  • a value outside the i64 range (for example 9.223372036854776e18, which is 2^63, and 1e300)
  • NaN and infinity

Integers, big integer literals and numeric strings behave as before. The string "1.0" is still NULL.

Tests cover the range bounds, -0.0, exponents, fractional values, and the direct call, the CAST and the -> forms.

`json_get_int` was NULL for a JSON number written as a float, even with an
integral value such as `1.0` or `2e3`, and so was `CAST(j -> 'k' AS BIGINT)`.
A float with an integral value in the `i64` range now gives that integer.
A fractional value is not rounded or truncated, and a value outside the `i64`
range, NaN or infinity stays NULL. Integers and numeric strings are unchanged.
@codecov-commenter

codecov-commenter commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.12%. Comparing base (f4f0a6e) to head (4bceaca).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #147      +/-   ##
==========================================
+ Coverage   94.00%   94.12%   +0.12%     
==========================================
  Files          18       18              
  Lines        1902     1925      +23     
  Branches     1902     1925      +23     
==========================================
+ Hits         1788     1812      +24     
  Misses         64       64              
+ Partials       50       49       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adriangb
adriangb merged commit 2287f59 into main Sep 29, 2026
7 checks passed
@adriangb
adriangb deleted the json-get-int-accepts-integral-floats branch September 29, 2026 00:47
This was referenced Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants