Repository navigation
Load mixed-type JSON fields declared as strings without quoting them - #9149
Open
behroozazarkhalili wants to merge 2 commits into
Open
behroozazarkhalili wants to merge 2 commits into
behroozazarkhalili wants to merge 2 commits into
Conversation
When a field has mixed types, the JSON loader re-encodes it as JSON text and loads it as Json(). If the user declared the field as a string in `features`, that JSON text was cast to the string column as is, so a string value "x" came back as '"x"'. For the fields declared as strings, only the values that are not strings are now replaced by their JSON text before Arrow parses the batch, and the strings are kept. This also works with on_mixed_types=None, and the field is encoded in the next batches without parsing them twice. Fixes huggingface#7496
ujson keeps 10 significant digits, so a float like 0.1 + 0.2 in a mixed-type field declared as a string became "0.3", and the other float columns of the batch were rounded too when the batch was re-encoded. Encode the declared string fields with the standard json module (compact separators, like the Json() fields), and only re-encode the Json() fields with ujson when there are some.
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 #7496
Since the JSON loader falls back to
Json()for mixed-type fields, the Arrow error from the issue doesn't happen anymore, but the requested feature type is still applied after the field was converted. If a field has mixed types and is declared as a string infeatures, the loader encodes it as JSON text and then casts that text to the string column, so string values come back quoted:As suggested by @lhoestq I first tried to pass an Arrow schema to
paj.read_json, but withexplicit_schemaa string field still fails on numbers (Column(/a) changed from string to number), so the values have to be converted before parsing.For the fields declared as strings (
Value("string")orValue("large_string"), including in structs and lists), when Arrow reports that their type changes, the values that are not strings are replaced by their JSON text and the strings are kept (string_encode_fieldinutils/json.py). This uses the standardjsonmodule (with compact separators, likeJson()fields), since ujson keeps only 10 significant digits:0.1 + 0.2stays"0.30000000000000004"and big integers keep all their digits. The other mixed fields still useJson(). Like theJson()fallback, this only happens for a field once Arrow reports a type change, and the next batches are encoded directly. It uses the features given by the user (self.config.features), and also works withon_mixed_types=None.Tests (new functions in
tests/packaged_modules/test_json.py): int/float/bool/object/null values mixed with strings, a string that is valid JSON text, a list of strings, a field in a struct,.jsonland.json,on_mixed_types="use_json"andNone; a declared string field next to aJson()field; all the digits of floats and big integers, without changing the other columns (JSON Lines); and a file with many batches, where only the first batch with mixed types is parsed twice. Onmain25 of the 27 new tests fail (the 2 that pass are the "valid JSON text" case, which already worked).tests/packaged_modules/test_json.py,tests/io/test_json.py,tests/featuresandtests/test_load.pyon Python 3.12: 734 passed; the only failure,test_load_dataset_builder_then_edit_then_load_again, also fails onmainon our Lustre file system. On Python 3.10 the new tests fail with thexPatherror ofmainthat #9119 fixes, like the other tests that load local JSON files.Known differences, not changed here: a float that ends with
.0is"1"in a batch where the field only has numbers (Arrow's cast) and"1.0"in a batch where it is mixed with strings; and in.jsonfiles (not JSON Lines) the whole file is first parsed and re-encoded with ujson, which already rounds floats to 10 digits onmain(e.g.0.30000000000000004is loaded as0.3, in any column) and doesn't support integers above 2**64.This conflicts with #8928 (strings that are valid JSON in
Json()fields) in two places ofjson.py, where both PRs change how a batch is re-encoded. I merged the two branches locally: keeping both changes resolves the conflicts, and the JSON tests of both PRs pass on the result, so whichever is merged second only needs that merge.