Decode NUMERIC columns as Decimal without precision loss - #835
Open
aminghadersohi wants to merge 1 commit into
Open
aminghadersohi wants to merge 1 commit into
aminghadersohi wants to merge 1 commit into
Conversation
orjson decodes every JSON number to a float, so NUMERIC values lost the digits beyond double precision that CrateDB sends. When a response declares a NUMERIC column, or an array of them, decode the body a second time with Decimal floats and replace only those columns. Other columns keep their float values, and responses without NUMERIC columns are decoded once, as before. Fixes crate#826
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #826.
Problem
CrateDB's HTTP endpoint sends every digit of a
NUMERICvalue, but_json_from_responsedecodes the body withorjson.loads, which turns every JSON number into a float.SELECT non aNUMERIC(38, 18)column holding12345678901234567890.123456789012345678returns1.2345678901234567e+19. Writes are already exact, becauseDecimalis serialized as a string.Change
After the orjson decode, check the response's
col_types. If any column isNUMERIC(22), or an array ofNUMERIC([100, 22]), decode the body a second time withjson.loads(..., parse_float=Decimal)and replace only those columns' cells withDecimalvalues. Integers in those columns also becomeDecimal.DOUBLEstaysfloat, and integers stayint.NUMERICcolumn (and any bulk or rowless response) is decoded once, as before. orjson's speed is unaffected for them.NUMERICcolumn always reads asDecimal, the typeDecimalwrites already use.Tests
test_numeric_columns_decode_without_precision_losscovers aNUMERICvalue beyond double precision, aDOUBLEnext to it, aNUMERICarray with an int and a null, a nullNUMERIC, and integer cells.test_responses_without_numeric_columns_unchangedcovers the fast path.tests/client: 137 passed.ruff checkandmypy srcare clean.NUMERIC(38, 18)column: before the change, the value read back as a float with the fractional digits lost. After it,Decimal('12345678901234567890.123456789012345678').I left the pre-existing
ruff formatdifferences in files this PR doesn't touch as they are.