From 91896593e90e65068379e91eb5f75388b4bf8391 Mon Sep 17 00:00:00 2001 From: Y Ethan Guo Date: Fri, 20 Nov 2026 10:31:51 -0800 Subject: [PATCH 1/3] Parse DATE and TIMESTAMP partition values that Hudi writes as dates and times Hudi writes the partition path of a DATE column as an ISO date such as 2024-02-29. Its SQL key generator writes a TIMESTAMP column as a date and time in the default time zone of the writer's JVM with the colons escaped, such as 2024-02-29 10%3A30%3A00. The path based extractor read both as numbers, so the first commit of a table partitioned by such a column failed with a NumberFormatException. A DATE value that is not a number is now parsed as an ISO date into days since epoch. A TIMESTAMP value that is not a number is unescaped and parsed as a date and time with an optional offset, into the precision of the field, or micros when the field declares none. A value without an offset is read in the JVM's default time zone for TIMESTAMP and in UTC for TIMESTAMP_NTZ. Numbers are read as before. The tests parse twelve paths: dates before and after the epoch as dates and as numbers, and timestamps that are escaped, carry a fraction or an offset, are numbers, or have millis, micros or no declared precision, with and without a time zone. Another test expects an invalid date and a timestamp without a time to fail with a PartitionValuesExtractorException. --- .../PathBasedPartitionValuesExtractor.java | 93 ++++++++++++++++++- .../TestHudiPartitionValuesExtractor.java | 93 +++++++++++++++++++ 2 files changed, 181 insertions(+), 5 deletions(-) diff --git a/xtable-core/src/main/java/org/apache/xtable/hudi/PathBasedPartitionValuesExtractor.java b/xtable-core/src/main/java/org/apache/xtable/hudi/PathBasedPartitionValuesExtractor.java index c5ca0bdc1..facd3b70f 100644 --- a/xtable-core/src/main/java/org/apache/xtable/hudi/PathBasedPartitionValuesExtractor.java +++ b/xtable-core/src/main/java/org/apache/xtable/hudi/PathBasedPartitionValuesExtractor.java @@ -22,18 +22,33 @@ import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalAccessor; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.TimeZone; +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; import lombok.AllArgsConstructor; import lombok.NonNull; import lombok.Value; +import org.apache.hudi.common.util.PartitionPathEncodeUtils; + import org.apache.xtable.exception.PartitionValuesExtractorException; import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; import org.apache.xtable.model.schema.InternalType; import org.apache.xtable.model.stat.PartitionValue; import org.apache.xtable.model.stat.Range; @@ -42,6 +57,16 @@ @AllArgsConstructor public class PathBasedPartitionValuesExtractor { private static final String HIVE_DEFAULT_PARTITION = "__HIVE_DEFAULT_PARTITION__"; + private static final Pattern INTEGRAL_VALUE = Pattern.compile("-?\\d+"); + private static final DateTimeFormatter TIMESTAMP_FORMATTER = + new DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .appendPattern("[' ']['T']") + .append(DateTimeFormatter.ISO_LOCAL_TIME) + .optionalStart() + .appendOffsetId() + .optionalEnd() + .toFormatter(); @NonNull protected final Map pathToPartitionFieldFormat; public List extractPartitionValues( @@ -95,8 +120,7 @@ private PartialResult parsePartitionPath( // this case is possible if user is directly relying on directly _hoodie_partition_path due // to custom partitioning logic boolean isSlashDelimited = totalNumberOfPartitions > 1; - return parseValue( - remainingPath, field.getSourceField().getSchema().getDataType(), isSlashDelimited); + return parseValue(remainingPath, field.getSourceField().getSchema(), isSlashDelimited); default: throw new IllegalArgumentException( "Unexpected partition type: " + field.getTransformType()); @@ -119,7 +143,7 @@ protected static PartialResult parseDate(String remainingPath, String format) { } protected static PartialResult parseValue( - String remainingPath, InternalType sourceFieldType, boolean isSlashDelimited) { + String remainingPath, InternalSchema sourceFieldSchema, boolean isSlashDelimited) { if (remainingPath.isEmpty()) { throw new PartitionValuesExtractorException("Missing partition value"); } @@ -134,19 +158,24 @@ protected static PartialResult parseValue( String unParsedPath = remainingPath.substring(Math.min(endCharIndex + 1, remainingPath.length())); Object parsedValue; + InternalType sourceFieldType = sourceFieldSchema.getDataType(); switch (sourceFieldType) { case STRING: case ENUM: parsedValue = valueAsString; break; case INT: - case DATE: parsedValue = Integer.parseInt(valueAsString); break; + case DATE: + parsedValue = parseDateValue(valueAsString); + break; case LONG: + parsedValue = Long.parseLong(valueAsString); + break; case TIMESTAMP: case TIMESTAMP_NTZ: - parsedValue = Long.parseLong(valueAsString); + parsedValue = parseTimestampValue(valueAsString, sourceFieldSchema); break; case DOUBLE: parsedValue = Double.parseDouble(valueAsString); @@ -172,6 +201,60 @@ protected static PartialResult parseValue( return new PartialResult(parsedValue, unParsedPath); } + /** Days since epoch, from either a number of days or an ISO date such as 2024-02-29. */ + private static int parseDateValue(String value) { + if (INTEGRAL_VALUE.matcher(value).matches()) { + return Integer.parseInt(value); + } + try { + return (int) LocalDate.parse(value).toEpochDay(); + } catch (DateTimeParseException ex) { + throw new PartitionValuesExtractorException("Unable to parse date from path: " + value, ex); + } + } + + /** + * Time since epoch in the field's timestamp precision, from either a number in that precision or + * a date and time such as 2024-02-29 10:30:00, which may be path escaped and carry an offset. + */ + private static long parseTimestampValue(String value, InternalSchema fieldSchema) { + if (INTEGRAL_VALUE.matcher(value).matches()) { + return Long.parseLong(value); + } + String unescaped = PartitionPathEncodeUtils.unescapePathName(value); + Instant instant; + try { + TemporalAccessor parsed = + TIMESTAMP_FORMATTER.parseBest(unescaped, OffsetDateTime::from, LocalDateTime::from); + if (parsed instanceof OffsetDateTime) { + instant = ((OffsetDateTime) parsed).toInstant(); + } else { + // Hudi's SQL key generator writes a timestamp as wall-clock time in the writer JVM's + // default time zone, while a timestamp without time zone has no zone to apply. + ZoneId zone = + fieldSchema.getDataType() == InternalType.TIMESTAMP_NTZ + ? ZoneOffset.UTC + : ZoneId.systemDefault(); + instant = ((LocalDateTime) parsed).atZone(zone).toInstant(); + } + } catch (DateTimeParseException ex) { + throw new PartitionValuesExtractorException( + "Unable to parse timestamp from path: " + value, ex); + } + Object precision = + fieldSchema.getMetadata() == null + ? null + : fieldSchema.getMetadata().get(InternalSchema.MetadataKey.TIMESTAMP_PRECISION); + if (precision == InternalSchema.MetadataValue.MILLIS) { + return instant.toEpochMilli(); + } + if (precision == InternalSchema.MetadataValue.NANOS) { + return TimeUnit.SECONDS.toNanos(instant.getEpochSecond()) + instant.getNano(); + } + return TimeUnit.SECONDS.toMicros(instant.getEpochSecond()) + + TimeUnit.NANOSECONDS.toMicros(instant.getNano()); + } + @Value protected static class PartialResult { Object value; diff --git a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiPartitionValuesExtractor.java b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiPartitionValuesExtractor.java index 4d3faa50c..e035503d2 100644 --- a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiPartitionValuesExtractor.java +++ b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiPartitionValuesExtractor.java @@ -19,11 +19,15 @@ package org.apache.xtable.hudi; import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -569,6 +573,95 @@ void testNestedColumnPartitioning( Assertions.assertEquals(expected, actual); } + private static InternalSchema timestampSchema( + InternalType type, InternalSchema.MetadataValue precision) { + return InternalSchema.builder() + .name("timestamp") + .dataType(type) + .metadata( + Collections.singletonMap(InternalSchema.MetadataKey.TIMESTAMP_PRECISION, precision)) + .build(); + } + + private static long epochMicros(String instant) { + Instant parsed = Instant.parse(instant); + return TimeUnit.SECONDS.toMicros(parsed.getEpochSecond()) + + TimeUnit.NANOSECONDS.toMicros(parsed.getNano()); + } + + static Stream dateAndTimestampValues() { + InternalSchema date = InternalSchema.builder().name("date").dataType(InternalType.DATE).build(); + InternalSchema micros = + timestampSchema(InternalType.TIMESTAMP, InternalSchema.MetadataValue.MICROS); + InternalSchema millis = + timestampSchema(InternalType.TIMESTAMP, InternalSchema.MetadataValue.MILLIS); + InternalSchema ntzMicros = + timestampSchema(InternalType.TIMESTAMP_NTZ, InternalSchema.MetadataValue.MICROS); + long localMicros = + TimeUnit.SECONDS.toMicros( + LocalDateTime.parse("2024-02-29T10:30:00") + .atZone(ZoneId.systemDefault()) + .toEpochSecond()); + return Stream.of( + Arguments.of(date, "p=2024-02-29", (int) LocalDate.parse("2024-02-29").toEpochDay()), + Arguments.of(date, "p=1969-12-31", -1), + Arguments.of(date, "p=19782", 19782), + Arguments.of(date, "p=-1", -1), + // Hudi's SQL writer escapes the colons of a timestamp in the path + Arguments.of(micros, "p=2024-02-29 10%3A30%3A00", localMicros), + Arguments.of( + micros, "p=2024-02-29 10:30:00.123456Z", epochMicros("2024-02-29T10:30:00.123456Z")), + Arguments.of(micros, "p=2024-02-29T10:30:00+01:00", epochMicros("2024-02-29T09:30:00Z")), + Arguments.of(micros, "p=1709202600123456", 1709202600123456L), + Arguments.of( + millis, + "p=2024-02-29T10:30:00.123Z", + Instant.parse("2024-02-29T10:30:00.123Z").toEpochMilli()), + Arguments.of(millis, "p=1709202600123", 1709202600123L), + Arguments.of(ntzMicros, "p=2024-02-29 10:30:00", epochMicros("2024-02-29T10:30:00Z")), + Arguments.of( + InternalSchema.builder().name("timestamp").dataType(InternalType.TIMESTAMP).build(), + "p=1969-12-31T23:59:59.999999Z", + -1L)); + } + + @ParameterizedTest + @MethodSource("dateAndTimestampValues") + void datesAndTimestampsParseFromNumbersAndStrings( + InternalSchema schema, String partitionPath, Object expectedValue) { + InternalPartitionField column = createSimplePartitionField("p", null, schema); + List actual = + new PathBasedPartitionValuesExtractor(Collections.emptyMap()) + .extractPartitionValues(Collections.singletonList(column), partitionPath); + Assertions.assertEquals( + Collections.singletonList( + PartitionValue.builder() + .partitionField(column) + .range(Range.scalar(expectedValue)) + .build()), + actual); + } + + @Test + void invalidDateAndTimestampValuesFail() { + PathBasedPartitionValuesExtractor extractor = + new PathBasedPartitionValuesExtractor(Collections.emptyMap()); + InternalPartitionField date = + createSimplePartitionField( + "p", null, InternalSchema.builder().name("date").dataType(InternalType.DATE).build()); + InternalPartitionField timestamp = + createSimplePartitionField( + "p", + null, + timestampSchema(InternalType.TIMESTAMP, InternalSchema.MetadataValue.MICROS)); + Assertions.assertThrows( + PartitionValuesExtractorException.class, + () -> extractor.extractPartitionValues(Collections.singletonList(date), "2024-02-30")); + Assertions.assertThrows( + PartitionValuesExtractorException.class, + () -> extractor.extractPartitionValues(Collections.singletonList(timestamp), "2024-02-29")); + } + private static InternalPartitionField createSimplePartitionField( String name, String parentPath, InternalSchema schema) { InternalField.InternalFieldBuilder sourceFieldBuilder = From 31e6e94c31dd89d258922b217ed76f8a2161bf4a Mon Sep 17 00:00:00 2001 From: Y Ethan Guo Date: Fri, 20 Nov 2026 10:31:52 -0800 Subject: [PATCH 2/3] Refuse a data file whose TIMESTAMP partition column differs from its path value Hudi's SQL key generator truncates the partition path of a TIMESTAMP column to whole seconds, so rows whose values differ below the second share a path and a file group. The value parsed from the path is then not the value of every row of the file. A target that fills an identity partition column from the partition value, as Iceberg does, returned the truncated value for every row and pruned filters on the column by it. No single partition value is right for a file that holds several, and a value taken from the column stats would differ from the one the file is later removed under, which is parsed from the path again. The data file extractor therefore keeps the path value and checks every file it adds stats to: for an identity partition on a TIMESTAMP or TIMESTAMP_NTZ column with a non-null value, the min and the max of the column's stats have to equal the partition value. Otherwise it throws a PartitionValuesExtractorException that names the file, the range of the column and the path value, and suggests partitioning by a column of whole seconds or a coarser value. A file without stats for the column is refused as well, since nothing shows which values it holds. The tests keep a file whose only value is the path value, for TIMESTAMP, TIMESTAMP_NTZ and a null partition, and expect the refusal for a single value with fractional seconds, for two values of one second, and for a file without stats. --- .../xtable/hudi/HudiDataFileExtractor.java | 103 ++++++++++++++++-- .../hudi/TestHudiDataFileExtractor.java | 94 ++++++++++++++++ 2 files changed, 186 insertions(+), 11 deletions(-) diff --git a/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java b/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java index 2596ba946..511a0ebc2 100644 --- a/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java +++ b/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java @@ -69,10 +69,16 @@ import org.apache.xtable.collectors.CustomCollectors; import org.apache.xtable.exception.NotSupportedException; +import org.apache.xtable.exception.PartitionValuesExtractorException; import org.apache.xtable.exception.ReadException; import org.apache.xtable.model.InternalTable; import org.apache.xtable.model.exception.ParseException; +import org.apache.xtable.model.schema.InternalField; import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.schema.InternalType; +import org.apache.xtable.model.schema.PartitionTransformType; +import org.apache.xtable.model.stat.ColumnStat; import org.apache.xtable.model.stat.PartitionValue; import org.apache.xtable.model.storage.FileFormat; import org.apache.xtable.model.storage.InternalDataFile; @@ -166,9 +172,7 @@ public InternalFilesDiff getDiffForCommit( Stream filesAddedWithoutStats = allInfo.getAdded().stream(); List filesAdded = - fileStatsExtractor - .addStatsToFiles(tableMetadata, filesAddedWithoutStats, table.getReadSchema()) - .collect(Collectors.toList()); + withStats(filesAddedWithoutStats, table.getReadSchema()).collect(Collectors.toList()); List filesRemoved = allInfo.getRemoved(); return InternalFilesDiff.builder().filesAdded(filesAdded).filesRemoved(filesRemoved).build(); @@ -223,8 +227,7 @@ public InternalFilesDiff getDiffFromCommitMetadata( } }); List filesAdded = - fileStatsExtractor - .addStatsToFiles(tableMetadata, filesAddedWithoutStats.stream(), table.getReadSchema()) + withStats(filesAddedWithoutStats.stream(), table.getReadSchema()) .collect(Collectors.toList()); return InternalFilesDiff.builder().filesAdded(filesAdded).filesRemoved(filesToRemove).build(); } @@ -293,8 +296,7 @@ public InternalFilesDiff getDiffFromRollbackMetadata( filesAddedWithoutStats.add(buildFileWithoutStats(partitionValues, baseFile))); }); List filesAdded = - fileStatsExtractor - .addStatsToFiles(tableMetadata, filesAddedWithoutStats.stream(), table.getReadSchema()) + withStats(filesAddedWithoutStats.stream(), table.getReadSchema()) .collect(Collectors.toList()); return InternalFilesDiff.builder().filesAdded(filesAdded).filesRemoved(filesToRemove).build(); } @@ -425,8 +427,7 @@ public InternalFilesDiff getDiffFromReplaceCommitMetadata( .collect(Collectors.toList())); }); List filesAdded = - fileStatsExtractor - .addStatsToFiles(tableMetadata, filesAddedWithoutStats.stream(), table.getReadSchema()) + withStats(filesAddedWithoutStats.stream(), table.getReadSchema()) .collect(Collectors.toList()); return InternalFilesDiff.builder().filesAdded(filesAdded).filesRemoved(filesToRemove).build(); } @@ -724,8 +725,7 @@ private List getInternalDataFilesForPartitions( .getLatestBaseFiles(partitionPath) .map(baseFile -> buildFileWithoutStats(partitionValues, baseFile)); }); - Stream files = - fileStatsExtractor.addStatsToFiles(tableMetadata, filesWithoutStats, table.getReadSchema()); + Stream files = withStats(filesWithoutStats, table.getReadSchema()); return PartitionFileGroup.fromFiles(files); } @@ -750,6 +750,87 @@ private static class AddedAndRemovedFiles { List removed; } + private Stream withStats( + Stream files, InternalSchema schema) { + return fileStatsExtractor + .addStatsToFiles(tableMetadata, files, schema) + .map(HudiDataFileExtractor::requireTimestampPartitionValuesMatchStats); + } + + /** + * A partition value parsed from the path is the value of every row of the file only when the path + * holds the value itself. Hudi's SQL key generator writes a TIMESTAMP partition path truncated to + * whole seconds, so there the path is a one-second bucket: rows whose values differ below the + * second share a path and a file group. A target that fills an identity partition column from the + * partition value, as Iceberg and Delta readers do, would read every row of the file as the + * truncated value, and prune filters on the column by it. + * + *

No single partition value is right for a file holding several values, and a value taken from + * the column stats would differ from the one the file is later removed under, which is parsed + * from the path again. So the path value is kept, and a file whose column stats show any other + * value for a timestamp partition column is refused. + * + * @throws PartitionValuesExtractorException when a timestamp partition column of the file holds a + * value other than its partition value, or has no stats to show which values it holds + */ + static InternalDataFile requireTimestampPartitionValuesMatchStats(InternalDataFile file) { + for (PartitionValue partitionValue : file.getPartitionValues()) { + InternalPartitionField partitionField = partitionValue.getPartitionField(); + InternalField sourceField = partitionField.getSourceField(); + InternalType type = sourceField.getSchema().getDataType(); + Object value = partitionValue.getRange().getMinValue(); + if (partitionField.getTransformType() != PartitionTransformType.VALUE + || (type != InternalType.TIMESTAMP && type != InternalType.TIMESTAMP_NTZ) + || value == null) { + continue; + } + Optional stat = + file.getColumnStats().stream() + .filter( + s -> s.getField() != null && sourceField.getPath().equals(s.getField().getPath())) + .findFirst(); + Object min = stat.map(s -> s.getRange().getMinValue()).orElse(null); + Object max = stat.map(s -> s.getRange().getMaxValue()).orElse(null); + if (!value.equals(min) || !value.equals(max)) { + throw new PartitionValuesExtractorException( + String.format( + "Data file %s holds values of TIMESTAMP partition column %s from %s to %s, but its" + + " partition path records %s. Hudi writes a timestamp partition path" + + " truncated to whole seconds, so values with fractional seconds, or" + + " several values in one file, cannot be published as a partition value." + + " Partition by a column holding whole seconds or a coarser value instead", + file.getPhysicalPath(), + sourceField.getPath(), + stat.isPresent() ? render(min, sourceField) : "unknown (no column stats)", + stat.isPresent() ? render(max, sourceField) : "unknown (no column stats)", + render(value, sourceField))); + } + } + return file; + } + + private static String render(Object timestamp, InternalField field) { + if (!(timestamp instanceof Long)) { + return String.valueOf(timestamp); + } + long value = (Long) timestamp; + Object precision = + field.getSchema().getMetadata() == null + ? null + : field.getSchema().getMetadata().get(InternalSchema.MetadataKey.TIMESTAMP_PRECISION); + java.time.Instant instant = + precision == InternalSchema.MetadataValue.MILLIS + ? java.time.Instant.ofEpochMilli(value) + : precision == InternalSchema.MetadataValue.NANOS + ? java.time.Instant.ofEpochSecond(0, value) + : java.time.Instant.ofEpochSecond( + Math.floorDiv(value, 1_000_000L), Math.floorMod(value, 1_000_000L) * 1_000L); + String rendered = instant.toString(); + return field.getSchema().getDataType() == InternalType.TIMESTAMP_NTZ + ? rendered.substring(0, rendered.length() - 1) + : rendered; + } + /** * Builds a {@link InternalDataFile} without any statistics or rowCount value set. * diff --git a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java index 883bc0b41..2b55ed7aa 100644 --- a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java +++ b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java @@ -20,6 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -43,6 +45,15 @@ import org.apache.hudi.storage.StoragePath; import org.apache.hudi.storage.hadoop.HadoopStorageConfiguration; +import org.apache.xtable.exception.PartitionValuesExtractorException; +import org.apache.xtable.model.schema.InternalField; +import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.schema.InternalType; +import org.apache.xtable.model.schema.PartitionTransformType; +import org.apache.xtable.model.stat.ColumnStat; +import org.apache.xtable.model.stat.PartitionValue; +import org.apache.xtable.model.stat.Range; import org.apache.xtable.model.storage.InternalDataFile; class TestHudiDataFileExtractor { @@ -167,4 +178,87 @@ private static HudiDataFileExtractor buildExtractorWithBasePath(String basePathS when(metaClient.getBasePath()).thenReturn(new StoragePath(basePathStr)); return new HudiDataFileExtractor(metaClient, null, null); } + + private static final long TEN_THIRTY_MICROS = 1709202600_000000L; + + private static InternalDataFile timestampPartitionedFile( + InternalType type, Long partitionValue, Range stats) { + InternalField field = + InternalField.builder() + .name("ts") + .schema( + InternalSchema.builder() + .name("ts") + .dataType(type) + .metadata( + Collections.singletonMap( + InternalSchema.MetadataKey.TIMESTAMP_PRECISION, + InternalSchema.MetadataValue.MICROS)) + .build()) + .build(); + InternalPartitionField partitionField = + InternalPartitionField.builder() + .sourceField(field) + .transformType(PartitionTransformType.VALUE) + .build(); + return InternalDataFile.builder() + .physicalPath("file:/table/ts=2024-02-29 10%3A30%3A00/f.parquet") + .partitionValues( + Collections.singletonList( + PartitionValue.builder() + .partitionField(partitionField) + .range(Range.scalar(partitionValue)) + .build())) + .columnStats( + stats == null + ? Collections.emptyList() + : Collections.singletonList(ColumnStat.builder().field(field).range(stats).build())) + .build(); + } + + @Test + void timestampPartitionValueMatchingTheFilesOnlyValueIsKept() { + InternalDataFile file = + timestampPartitionedFile( + InternalType.TIMESTAMP, TEN_THIRTY_MICROS, Range.scalar(TEN_THIRTY_MICROS)); + assertSame(file, HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(file)); + InternalDataFile ntz = + timestampPartitionedFile( + InternalType.TIMESTAMP_NTZ, TEN_THIRTY_MICROS, Range.scalar(TEN_THIRTY_MICROS)); + assertSame(ntz, HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(ntz)); + InternalDataFile nullPartition = + timestampPartitionedFile(InternalType.TIMESTAMP, null, Range.scalar(null)); + assertSame( + nullPartition, + HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(nullPartition)); + } + + @Test + void timestampPartitionValueTruncatedBelowTheSecondIsRefused() { + // One value with fractional seconds: the path holds it truncated to the second. + InternalDataFile single = + timestampPartitionedFile( + InternalType.TIMESTAMP, TEN_THIRTY_MICROS, Range.scalar(TEN_THIRTY_MICROS + 123456)); + PartitionValuesExtractorException e = + assertThrows( + PartitionValuesExtractorException.class, + () -> HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(single)); + assertTrue(e.getMessage().contains("2024-02-29T10:30:00.123456Z"), e.getMessage()); + assertTrue(e.getMessage().contains("fractional seconds"), e.getMessage()); + // Two values of the same second share the path and the file group. + InternalDataFile shared = + timestampPartitionedFile( + InternalType.TIMESTAMP, + TEN_THIRTY_MICROS, + Range.vector(TEN_THIRTY_MICROS + 123456, TEN_THIRTY_MICROS + 654321)); + assertThrows( + PartitionValuesExtractorException.class, + () -> HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(shared)); + // Without stats nothing shows the file holds only the partition value. + InternalDataFile noStats = + timestampPartitionedFile(InternalType.TIMESTAMP, TEN_THIRTY_MICROS, null); + assertThrows( + PartitionValuesExtractorException.class, + () -> HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(noStats)); + } } From 32d970dffbb72e0333beacbff6c3316a9918fabd Mon Sep 17 00:00:00 2001 From: Y Ethan Guo Date: Mon, 23 Nov 2026 23:59:17 -0800 Subject: [PATCH 3/3] Keep a file without stats for its TIMESTAMP partition column The check refused a file whose TIMESTAMP partition column had no column stats. A Hudi table indexes only some columns in the metadata table, 32 by default, and a table written with drop.partition.columns stores no footer stats for the column, so every sync of such a table failed, including tables with numeric partition paths that synced before. A file without stats for the column is now kept, and the check refuses only a file whose stats show a value other than the partition value. --- .../apache/xtable/hudi/HudiDataFileExtractor.java | 14 ++++++++++---- .../xtable/hudi/TestHudiDataFileExtractor.java | 10 ++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java b/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java index 511a0ebc2..5634066ea 100644 --- a/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java +++ b/xtable-core/src/main/java/org/apache/xtable/hudi/HudiDataFileExtractor.java @@ -770,8 +770,10 @@ private Stream withStats( * from the path again. So the path value is kept, and a file whose column stats show any other * value for a timestamp partition column is refused. * - * @throws PartitionValuesExtractorException when a timestamp partition column of the file holds a - * value other than its partition value, or has no stats to show which values it holds + *

A file without stats for the column is kept, since nothing shows it holds another value. + * + * @throws PartitionValuesExtractorException when the column stats of a timestamp partition column + * of the file show a value other than its partition value */ static InternalDataFile requireTimestampPartitionValuesMatchStats(InternalDataFile file) { for (PartitionValue partitionValue : file.getPartitionValues()) { @@ -791,6 +793,10 @@ static InternalDataFile requireTimestampPartitionValuesMatchStats(InternalDataFi .findFirst(); Object min = stat.map(s -> s.getRange().getMinValue()).orElse(null); Object max = stat.map(s -> s.getRange().getMaxValue()).orElse(null); + if (min == null && max == null) { + // The column is outside the indexed columns, or the file stores no stats for it. + continue; + } if (!value.equals(min) || !value.equals(max)) { throw new PartitionValuesExtractorException( String.format( @@ -801,8 +807,8 @@ static InternalDataFile requireTimestampPartitionValuesMatchStats(InternalDataFi + " Partition by a column holding whole seconds or a coarser value instead", file.getPhysicalPath(), sourceField.getPath(), - stat.isPresent() ? render(min, sourceField) : "unknown (no column stats)", - stat.isPresent() ? render(max, sourceField) : "unknown (no column stats)", + render(min, sourceField), + render(max, sourceField), render(value, sourceField))); } } diff --git a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java index 2b55ed7aa..7ec4f4169 100644 --- a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java +++ b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiDataFileExtractor.java @@ -231,6 +231,10 @@ void timestampPartitionValueMatchingTheFilesOnlyValueIsKept() { assertSame( nullPartition, HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(nullPartition)); + // A column outside the indexed columns has no stats, and nothing shows another value. + InternalDataFile noStats = + timestampPartitionedFile(InternalType.TIMESTAMP, TEN_THIRTY_MICROS, null); + assertSame(noStats, HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(noStats)); } @Test @@ -254,11 +258,5 @@ void timestampPartitionValueTruncatedBelowTheSecondIsRefused() { assertThrows( PartitionValuesExtractorException.class, () -> HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(shared)); - // Without stats nothing shows the file holds only the partition value. - InternalDataFile noStats = - timestampPartitionedFile(InternalType.TIMESTAMP, TEN_THIRTY_MICROS, null); - assertThrows( - PartitionValuesExtractorException.class, - () -> HudiDataFileExtractor.requireTimestampPartitionValuesMatchStats(noStats)); } }