Repository navigation
[python] Read and write format table JSON files like Java - #11008
plusplusjiajia wants to merge 4 commits into
Conversation
089b8fc to
a8460de
Compare
e948e2d to
2df87f1
Compare
62cfd7b to
d6a7ed7
Compare
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.
1dbecf7 to
7094a73
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.
… 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>
…n columns that differ in nullability - The JSON writer refuses a column whose type is not the table's, a null in a NOT NULL column or field, and a column that the table does not have, as the input of other tables is refused, instead of writing values that read back as errors, nulls or other values. A misspelled column was written as a missing nullable one, whose values read as null. Nullability, metadata, the offset width of strings and fixed-size binaries may differ, as for other tables. - The JSON writer refuses a line that holds the line delimiter. - Every JSON write, not only an overwrite, converts the input before it writes any file, so a refused value no longer leaves the files of earlier partitions, which a retry would write again. - The JSON reader reads NaN and Infinity, which earlier PyPaimon versions wrote, and reuses one decoder. - A column is nullable in the result when it is in any file, so files that keep a partition column with the nullability of their input concatenate with files that leave it out. - A new file that fails to delete in an overwrite rollback no longer hides the write error or keeps the other new files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t table delete helper - _decode_java_utf8 decoded the rest of its input again after each malformed sequence, which took quadratic time on a JSON file with many of them: about 3.6 seconds for 1.8 MB with 16,000 malformed sequences. It now decodes in one pass with an error handler that replaces an encoded surrogate as one sequence, as BinaryString.toString does, and every other malformed sequence as before. - _delete_data_files_in_path has no caller since the overwrite writes the new files before it deletes the old ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7094a73 to
020eabe
Compare
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;NaNandInfinity, which earlier PyPaimon versions wrote and Jackson refuses;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.The JSON writer refuses a column whose type is not the table's, such as INT64 for an INT column or a zoned timestamp for TIMESTAMP, a null in a NOT NULL column or field, and a column that the table does not have, as
TableWriterefuses the input of other tables. Since a batch may leave out a nullable column, a misspelled column was otherwise written as a missing one, whose values read as null. Values of another type would read back as errors, nulls or other values. Nullability, metadata, the offset width of strings and fixed-size binaries may differ, as for other tables, andwrite_pandasconverts a DataFrame to the table's types. Earlier versions wrote such input withjson.dumps.A file that fails to write or close is deleted, for every format, so no partial file is left to read.
A column is nullable in the result when it is in any file. PyPaimon's Parquet, ORC and CSV files keep the partition columns with the nullability of the input, so they would otherwise not concatenate with Java's files, which leave them out and take the table's.
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, every write, not only an overwrite, also converts the whole input once before it writes or deletes anything, so a value the writer refuses leaves the old rows and writes no file that a retry would write again. This converts the input twice. An I/O error, or a refused value in a later
write_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, 52 tests:json.ignore-parse-errors;0.0,-0.0and NaN keys;NaNandInfinitywritten by earlier versions;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: 219 tests, with PyArrow 16.1.0, 22.0.0 and 24.0.0. flake8 is clean. The type and null checks behave the same with PyArrow 6.0.1 on Python 3.6.