Repository navigation
[1015] Read Paimon column stats by the schema each data file was written with - #1016
Indian-boult wants to merge 5 commits into
Conversation
…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) { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
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 aDROPthenADD, 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
TINYINTandSMALLINTcolumns with no schema change. Both map toINT, so their stats were read withgetInt, and aTINYINTcolumn holding -5 and 7 reported a min of 251.Brief change log
PaimonDataFileExtractorbuilds 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.PaimonStatsExtractormatches the file's columns to the snapshot schema by field id, and takes the column names from the file's schema whenvalueStatsCols()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 coversINTtoBIGINT,FLOATtoDOUBLE, 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.TestPaimonTablegetsalterTable, andwriteRowsWithoutCompaction, which returns the data files it wrote.TestPaimonStatsExtractorcoverTINYINTandSMALLINTstats 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:
Mismatch between column stats names and values arity, the two drop and add tests report stats for the new column from the dropped one, theTINYINTtest reports a min of 251 for -5, the widening test throwsNegativeArraySizeExceptionreading aDECIMAL(5,2)value asDECIMAL(20,4), and the narrowing test reports stats for every changed column../mvnw -pl xtable-core testruns 604 tests with 0 failures, 0 errors and 3 skipped../mvnw -pl xtable-core -DskipUTs -Dit.test='ITConversionController#testVariousOperations' verifyruns 28 tests with 0 failures and 0 errors.c IS NULLreturned 0 rows on the drop and add table. After the fix all syncs succeed andc IS NULLreturns 2 rows.