Skip to content

[python] Read and write format table JSON files like Java - #11008

Open
plusplusjiajia wants to merge 2 commits into
apache:masterfrom
plusplusjiajia:fix/python-format-table-json
Open

plusplusjiajia wants to merge 2 commits into
apache:masterfrom
plusplusjiajia:fix/python-format-table-json

Conversation

@plusplusjiajia

@plusplusjiajia plusplusjiajia commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Purpose

PyPaimon wrote format table JSON files with json.dumps of Python values, which fails for DECIMAL, DATE, TIMESTAMP and BINARY columns. It read each file with types inferred from its values.

Java JsonFormatWriter writes every scalar as a JSON string. As a result, PyPaimon:

  • read Java's files with string columns;
  • failed with Schema at index N was different when the files of one table were inferred differently.

As in Java, the writer now:

  • writes scalar values as strings formatted with Java's casts;
  • writes map keys as the string of their value;
  • writes BINARY values as Base64;
  • writes nested values as arrays and objects;
  • uses json.line-delimiter, or its fallback key lineSep;
  • 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;
  • refuses a VECTOR with a null element and a MAP with a BINARY or nested key, which nothing reads back.

As in JsonFileReader, the reader now:

  • converts the projected columns with the table's types;
  • accepts JSON numbers and booleans as well as strings;
  • reads floating-point numbers as decimals when a projected column is DECIMAL;
  • keeps the last of the map keys that convert to the same value. FLOAT and DOUBLE keys compare as in Java, so 0.0 and -0.0 are different keys and all NaN keys are the same key;
  • skips null lines;
  • converts a file in batches of rows;
  • applies json.ignore-parse-errors;
  • rounds FLOAT text once, as Float.parseFloat does, reads a lone surrogate as ?, and decodes malformed UTF-8 as Java does;
  • treats a line it cannot parse, such as one with an exponent past Decimal's range, as a parse error. With json.ignore-parse-errors, a VECTOR that cannot be built drops its map entry, as in JsonFileReader;
  • returns the columns in table order.

Like Java, a blank line is a parse error unless json.ignore-parse-errors is set. Jackson reads a blank line as a missing node, which JsonFileReader cannot 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.validateDataFields rejects, 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_arrow also 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 later write_arrow_batch call, 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.toString spells it, as java_floating_string does elsewhere in PyPaimon. Java 8 to 17 spell some numbers differently, such as 1e23 as 9.999999999999999E22 (#11164).

Java's string formatting of partition values moves out of _canonical_partition into 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.py and format_table_write.py, so #11007, which waits for #11018, will be rebased on this one.

Tests

  • format_table_json_test.py, 60 tests:
    • the writer's output for scalar, binary, map and nested values, compared with what Java writes;
    • a round trip of every type;
    • native JSON values read with the table's types;
    • parsing only the projected columns;
    • parse errors with and without json.ignore-parse-errors;
    • map key conversion and duplicates, including 0.0, -0.0 and NaN keys;
    • VECTOR lengths;
    • type validation;
    • both line delimiter keys;
    • a partitioned table written and read back, with and without an empty projection;
    • null and BINARY partition values;
    • a file without partition columns, as Java writes it;
    • a table that has only partition columns;
    • input without nullable or NOT NULL columns;
    • files that fail to write or close, with and without overwrite;
    • refused and failed overwrites, malformed lines, Base64 padding, long numbers, TIME partitions and NOT NULL partitions read with Java's files.
  • The table-level test fails without this change: writing a DECIMAL column raises Object of type Decimal is not JSON serializable.
  • Cross-client check against a DLF catalog. This was run with an earlier version of this change, which left the partition columns out of the files and did not have the later parsing and overwrite changes, together with the partition path fix. Spark with Paimon 2.1.0, PyPaimon and pypaimon-rust 0.4.0 each wrote JSON tables with DECIMAL, DATE, TIMESTAMP and BINARY values, and every client read every table back with the same rows.
  • format_table_json_test.py, format_write_execution_test.py, rest_format_table_test.py and cli_table_test.py pass: 227 tests. flake8 is clean.

@plusplusjiajia
plusplusjiajia force-pushed the fix/python-format-table-json branch 2 times, most recently from 089b8fc to a8460de Compare November 6, 2026 02:16
@plusplusjiajia
plusplusjiajia force-pushed the fix/python-format-table-json branch 2 times, most recently from e948e2d to 2df87f1 Compare November 16, 2026 01:39
@plusplusjiajia
plusplusjiajia force-pushed the fix/python-format-table-json branch 10 times, most recently from 62cfd7b to d6a7ed7 Compare November 24, 2026 14:57
@plusplusjiajia
plusplusjiajia marked this pull request as ready for review November 24, 2026 15:15
@plusplusjiajia
plusplusjiajia force-pushed the fix/python-format-table-json branch from d6a7ed7 to 4c1d3e7 Compare November 24, 2026 15:24
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.
@plusplusjiajia
plusplusjiajia force-pushed the fix/python-format-table-json branch from 4c1d3e7 to 27cac1f Compare November 27, 2026 11:00

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
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