Skip to content

[1015] Read Paimon column stats by the schema each data file was written with - #1016

Open
Indian-boult wants to merge 5 commits into
apache:mainfrom
Indian-boult:fix/paimon-schema-evolution-stats
Open

Indian-boult wants to merge 5 commits into
apache:mainfrom
Indian-boult:fix/paimon-schema-evolution-stats

Conversation

@Indian-boult

@Indian-boult Indian-boult commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1015

What is the purpose of the pull request

The Paimon source read each data file's stats by position and type in the snapshot schema, but Paimon stores them by position and type in the schema the file was written with. After a schema change this failed the sync on ADD COLUMN, or gave a column the stats of another column after a DROP then ADD, or misread a column after a type change. This PR reads the stats by the file's schema, matches columns by field id, and reads each value with the file's Paimon type.

Reading with the file's Paimon type also fixes TINYINT and SMALLINT columns with no schema change. Both map to INT, so their stats were read with getInt, and a TINYINT column holding -5 and 7 reported a min of 251.

Brief change log

  • PaimonDataFileExtractor builds the stats layout of each schema id of the data files once per sync call, and passes it to the stats extractor with each file.
  • PaimonStatsExtractor matches the file's columns to the snapshot schema by field id, and takes the column names from the file's schema when valueStatsCols() is null. It reads each value with the file's Paimon type, and keeps a column's stats when its type is unchanged or widened so that the new type holds every old value, which covers INT to BIGINT, FLOAT to DOUBLE, a decimal with at least as many digits before and after the point, and a finer timestamp precision. It skips a column that was dropped, or changed to a type that cannot hold its old values or sorts them differently.
  • TestPaimonTable gets alterTable, and writeRowsWithoutCompaction, which returns the data files it wrote.
  • New tests in TestPaimonStatsExtractor cover TINYINT and SMALLINT stats with negative values, adding a column on append and primary key tables, rename then add, drop then add, drop then add with the same name, and widening and narrowing type changes. Each schema test writes a file before the change and checks its stats after.

Verify this pull request

This change added tests and can be verified as follows:

  • Before the fix, 8 of the new tests fail on main. The three add column tests fail with Mismatch between column stats names and values arity, the two drop and add tests report stats for the new column from the dropped one, the TINYINT test reports a min of 251 for -5, the widening test throws NegativeArraySizeException reading a DECIMAL(5,2) value as DECIMAL(20,4), and the narrowing test reports stats for every changed column.
  • After the fix, they pass.
  • ./mvnw -pl xtable-core test runs 604 tests with 0 failures, 0 errors and 3 skipped.
  • ./mvnw -pl xtable-core -DskipUTs -Dit.test='ITConversionController#testVariousOperations' verify runs 28 tests with 0 failures and 0 errors.
  • Manually synced the tables from the issue to Iceberg, Delta and Hudi. Before the fix the add column table failed to sync and c IS NULL returned 0 rows on the drop and add table. After the fix all syncs succeed and c IS NULL returns 2 rows.

…ten with

Paimon stores a data file's value stats by position in the schema the file
was written with (DataFileMeta#schemaId). When valueStatsCols is null, the
Paimon source used the positions of the snapshot schema instead. After a
schema change, files written before it were read with the wrong layout:

- After ADD COLUMN, the sync failed with "Mismatch between column stats
  names and values arity".
- After DROP COLUMN b and ADD COLUMN c, c got the stats of b. Iceberg then
  skipped those files for c IS NULL, although c is null in all their rows.
- After a column type change, the old values were read with the new type.

Resolve the file's schema by schemaId, match its columns to the snapshot
schema by field id, and skip a column whose type changed.

Fixes apache#1015
…schemas

The file schemas are passed as a map filled as files are read. When a file was written with the schema the stats are reported in, its columns map by name without the match by field id. Adds a test for a column dropped and added again under the same name.
return fieldsByFileColumnName;
}

private static boolean hasSameType(InternalSchema fileSchema, InternalSchema schema) {

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.

Should we use the original Paimon types here? Paimon is a unique case where there are multiple types for integers like tinyint and smallint.

Similarly, do we need to account for cases like widening decimals? Ex, DECIMAL(5,2) to DECIMAL(10,2)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes for reading the values. The stats are stored in the type the file was written with, and TINYINT and SMALLINT both map to INT, so the old code read a byte or a short with getInt. That is wrong on main too, with no schema change at all. A TINYINT column holding -5 and 7 reported a min of 251. 1712b69 reads each value with the file's Paimon type through InternalRowUtils.get, and testStatsOfTinyIntAndSmallIntColumns covers it.

To decide whether the stats still apply after a type change, I kept the comparison on the internal types, since that is the type the target reads the file with. The stats are now kept when the new type holds every value of the old one. That covers TINYINT, SMALLINT and INT to a wider integer, FLOAT to DOUBLE, a decimal with at least as many digits before and after the point like DECIMAL(5,2) to DECIMAL(10,2), and a finer timestamp precision. A decimal is rescaled to the new scale. Changes like BIGINT to INT, DOUBLE to FLOAT, a decimal with fewer digits or INT to STRING still drop the stats, because the old values would not fit the new type or would sort differently, and Paimon allows those casts by default. Narrowing among TINYINT, SMALLINT and INT keeps them, since the target sees INT either way and the file still holds the old values. testStatsAfterColumnTypeWidening and testStatsAfterColumnTypeNarrowing cover both sides.

The mapping is now built once for each schema id in a sync and reused for every file written with it, so the shortcut for equal schemas is no longer needed.

…widening

Read each stats value with the Paimon type of the schema the file was
written with. TINYINT and SMALLINT map to INT, so reading them with
getInt got negative values wrong even without a schema change.

Keep a column's stats after a type change when the new type holds every
old value: INT to BIGINT, FLOAT to DOUBLE, a decimal with at least as
many digits before and after the point, and a finer timestamp precision.

Build the stats layout once for each schema id of the files. Move the
alter and write helpers to TestPaimonTable and look up test stats by
the name of the file each write returns.

This branch has not been deployed

No deployments
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.

Paimon source fails or reports wrong column stats for files written before a schema change

2 participants