Repository navigation
[python] Read and write format table JSON files like Java - #11008
plusplusjiajia wants to merge 2 commits into
Conversation
089b8fc to
a8460de
Compare
e948e2d to
2df87f1
Compare
62cfd7b to
d6a7ed7
Compare
d6a7ed7 to
4c1d3e7
Compare
PyPaimon wrote format table JSON files with json.dumps of Python values, which fails for DECIMAL, DATE, TIMESTAMP and BINARY columns, and read each file with types inferred from its values. Java JsonFormatWriter writes every scalar as a JSON string, so PyPaimon read Java's files with string columns, or failed when files of one table were inferred differently. As in Java, the writer now writes scalar values as strings formatted with Java's casts, map keys as the string of their value, BINARY values as Base64 and nested values as arrays and objects, using json.line-delimiter. As in JsonFileReader, the reader converts the projected columns with the table's types, accepting JSON numbers and booleans as well as strings, keeps the last of map keys that convert to the same value, skips null lines and applies json.ignore-parse-errors. Both reject the types that Java's JSON format does not support. The files keep the partition columns, as PyPaimon writes them for the other formats: its partition paths lose null and binary values. Java reads columns by name and ignores them. The reader takes a partition column from the file when the file has it, and from the path otherwise. The Java value formatting of partition paths moves into a function that the JSON writer shares. It now also formats pandas timestamps of TIMESTAMP_LTZ columns, which have no astimezone() without an argument.
4c1d3e7 to
27cac1f
Compare
JingsongLi
left a comment
There was a problem hiding this comment.
I ran 221 Python tests and verified a real Java/Python round trip with DECIMAL, DATE, TIMESTAMP, BINARY, ARRAY, MAP, and ROW on ordinary partition paths. I reproduced two write-path issues with the public Format Table API.
|
|
||
| def _write_raw(self, raw: bytes, dir_path: str) -> str: | ||
| path = f"{dir_path}/{self._data_file_prefix}{uuid.uuid4().hex}{self._suffix}" | ||
| with self.table.file_io.new_output_stream(path) as out: |
There was a problem hiding this comment.
[P1] Remove the partially written file when an overwrite fails
A path is registered for failure cleanup only after write and close succeed. I seeded a real local JSON format table, then injected a short filesystem write during overwrite. The stream wrote {"id" and raised OSError. The old file survived, but the partial new data-*.json was still visible and an ordinary table read failed to parse it. Track the in-progress output before opening the stream or publish it from a temporary path after a successful close. Cover partial write and close failure in the overwrite cleanup test.
There was a problem hiding this comment.
Fixed. A file that fails to write or close is now deleted before the error is raised, with and without overwrite. test_failed_file_write_leaves_no_file covers a short write and a close failure in both cases, and checks that only the old file is left and is read.
| self._options = JsonOptions(options) | ||
|
|
||
| def write(self, data: pyarrow.Table) -> bytes: | ||
| columns = [data.column(field.name).to_pylist() for field in self._fields] |
There was a problem hiding this comment.
[P2] Keep accepting batches that omit nullable data columns
I created a JSON format table with id and a nullable note, then called write_arrow with just id. The same batch writes {"id": 7} on master. On this head data.column("note") raises Field "note" does not exist in schema. Producers can encounter this after a nullable column is added to a table they already write to. Preserve the missing nullable field behavior by omitting the key or materializing it as NULL, and add a public writer regression test.
There was a problem hiding this comment.
Fixed. The writer leaves out the key of a nullable column that the batch does not have, so your batch writes {"id":"7"} and reads note as null, as JsonFileReader reads a missing key. A batch without a NOT NULL column is refused, since its rows would read back as null. test_writes_batches_without_nullable_columns and test_refuses_batches_without_not_null_columns write through the public writer.
… batches without nullable columns A file that fails to write or close is deleted, so no partial file is left to read. The JSON writer leaves out the keys of nullable columns that the input does not have, which Java reads as null, and refuses input without a NOT NULL column. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Purpose
PyPaimon wrote format table JSON files with
json.dumpsof Python values, which fails for DECIMAL, DATE, TIMESTAMP and BINARY columns. It read each file with types inferred from its values.Java
JsonFormatWriterwrites every scalar as a JSON string. As a result, PyPaimon:Schema at index N was differentwhen the files of one table were inferred differently.As in Java, the writer now:
json.line-delimiter, or its fallback keylineSep;As in
JsonFileReader, the reader now:0.0and-0.0are different keys and all NaN keys are the same key;json.ignore-parse-errors;Float.parseFloatdoes, reads a lone surrogate as?, and decodes malformed UTF-8 as Java does;json.ignore-parse-errors, a VECTOR that cannot be built drops its map entry, as inJsonFileReader;Like Java, a blank line is a parse error unless
json.ignore-parse-errorsis set. Jackson reads a blank line as a missing node, whichJsonFileReadercannot convert.The files keep the partition columns, as PyPaimon writes them for the other formats. PyPaimon's partition paths lose null and BINARY values, so the columns cannot be left out yet. Java reads columns by name and ignores the extra keys. When a file has a partition column, the reader takes the value from the file. For files without it, such as Java's, the reader takes it from the path.
The reader and the writer reject the types that
JsonFileFormat.validateDataFieldsrejects, such as BLOB and VARIANT.Line splitting and Java's number parsing go into
format_table_text.py.A file that fails to write or close is deleted, for every format, so no partial file is left to read.
Overwrite, for every format: a partition's new files are written before its old ones are deleted, and a failure deletes the new files instead. For JSON,
write_arrowalso converts the whole input once before it deletes anything, so a value the writer refuses leaves the old rows. This converts the input twice. An I/O error, or a refused value in a laterwrite_arrow_batchcall, can still leave earlier partitions overwritten.The TIME pattern in
partition_spec.py, which partition values also use, no longer backtracks quadratically.A JSON number read into a STRING column is spelled as Java 19+
Double.toStringspells it, asjava_floating_stringdoes elsewhere in PyPaimon. Java 8 to 17 spell some numbers differently, such as1e23as9.999999999999999E22(#11164).Java's string formatting of partition values moves out of
_canonical_partitioninto a function that the writer shares. That function also accepts pandas timestamps for TIMESTAMP_LTZ: pandas'astimezone()requires an argument, so the old code failed on them.#11007 makes the same changes for CSV. The two conflict in
format_table_read.pyandformat_table_write.py, so #11007, which waits for #11018, will be rebased on this one.Tests
format_table_json_test.py, 60 tests:json.ignore-parse-errors;0.0,-0.0and NaN keys;Object of type Decimal is not JSON serializable.format_table_json_test.py,format_write_execution_test.py,rest_format_table_test.pyandcli_table_test.pypass: 227 tests. flake8 is clean.