Indian-boult commented on code in PR #1016:
URL: https://github.com/apache/incubator-xtable/pull/1016#discussion_r4727665494
##########
xtable-core/src/test/java/org/apache/xtable/paimon/TestPaimonStatsExtractor.java:
##########
@@ -561,6 +706,43 @@ private void createPartitionedTable() {
paimonTable = testTable.getPaimonTable();
}
+ private Catalog createCatalog() {
+ return TestPaimonTable.createFilesystemCatalog(tempDir.toString(), new
Configuration());
+ }
+
+ private static FileStoreTable alterTable(
+ Catalog catalog, FileStoreTable table, SchemaChange change) throws
Exception {
+ Identifier identifier = Identifier.create("test_db", table.name());
+ catalog.alterTable(identifier, change, false);
+ return (FileStoreTable) catalog.getTable(identifier);
+ }
+
+ /** Writes one data file per call. Compaction would rewrite older files with
the new schema. */
+ private static void writeWithoutCompaction(FileStoreTable table,
List<GenericRow> rows)
+ throws Exception {
+ BatchWriteBuilder builder = table.newBatchWriteBuilder();
+ try (BatchTableWrite writer = builder.newWrite();
+ BatchTableCommit commit = builder.newCommit()) {
+ for (GenericRow row : rows) {
+ writer.write(row);
+ }
+ commit.commit(writer.prepareCommit());
+ }
+ }
Review Comment:
Moved in 1712b69. `TestPaimonTable` now has `alterTable` and
`writeRowsWithoutCompaction`, next to `createTable` and `writeRows`.
##########
xtable-core/src/test/java/org/apache/xtable/paimon/TestPaimonStatsExtractor.java:
##########
@@ -561,6 +706,43 @@ private void createPartitionedTable() {
paimonTable = testTable.getPaimonTable();
}
+ private Catalog createCatalog() {
+ return TestPaimonTable.createFilesystemCatalog(tempDir.toString(), new
Configuration());
+ }
+
+ private static FileStoreTable alterTable(
+ Catalog catalog, FileStoreTable table, SchemaChange change) throws
Exception {
+ Identifier identifier = Identifier.create("test_db", table.name());
+ catalog.alterTable(identifier, change, false);
+ return (FileStoreTable) catalog.getTable(identifier);
+ }
+
+ /** Writes one data file per call. Compaction would rewrite older files with
the new schema. */
+ private static void writeWithoutCompaction(FileStoreTable table,
List<GenericRow> rows)
+ throws Exception {
+ BatchWriteBuilder builder = table.newBatchWriteBuilder();
+ try (BatchTableWrite writer = builder.newWrite();
+ BatchTableCommit commit = builder.newCommit()) {
+ for (GenericRow row : rows) {
+ writer.write(row);
+ }
+ commit.commit(writer.prepareCommit());
+ }
+ }
+
+ private Map<Long, List<ColumnStat>> getStatsByRowCount(FileStoreTable table)
{
Review Comment:
Changed in 1712b69. `writeRowsWithoutCompaction` returns the data files it
added, so each test keeps the name of the file it wrote, like `oldFile` and
`newFile`, and looks its stats up with `getStatsByFileName`. Files with the
same row count no longer collide.
##########
xtable-core/src/main/java/org/apache/xtable/paimon/PaimonStatsExtractor.java:
##########
@@ -83,6 +94,36 @@ public List<ColumnStat> extractColumnStats(DataFileMeta
file, InternalSchema int
return columnStats;
}
+ /**
+ * Maps the column names of the file's schema to the fields of the schema
the stats are reported
+ * in. Columns are matched by field id, since a column can be renamed,
dropped or added after the
+ * file was written. A column whose type changed is left out, since the file
stores its stats in
+ * the old type.
+ */
+ private static Map<String, InternalField> getFieldsByFileColumnName(
+ InternalSchema fileSchema, InternalSchema internalSchema) {
+ if (fileSchema.equals(internalSchema)) {
+ return internalSchema.getFields().stream()
+ .collect(Collectors.toMap(InternalField::getName,
Function.identity()));
+ }
+ Map<Integer, InternalField> fieldsById =
+ internalSchema.getFields().stream()
+ .collect(Collectors.toMap(InternalField::getFieldId,
Function.identity()));
+ Map<String, InternalField> fieldsByFileColumnName = new HashMap<>();
+ for (InternalField fileField : fileSchema.getFields()) {
+ InternalField field = fieldsById.get(fileField.getFieldId());
+ if (field != null && hasSameType(fileField.getSchema(),
field.getSchema())) {
+ fieldsByFileColumnName.put(fileField.getName(), field);
+ }
+ }
+ return fieldsByFileColumnName;
+ }
+
+ private static boolean hasSameType(InternalSchema fileSchema, InternalSchema
schema) {
Review 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]