Guosmilesmile commented on code in PR #15906:
URL: https://github.com/apache/iceberg/pull/15906#discussion_r3101714073
##########
data/src/test/java/org/apache/iceberg/data/BaseFormatModelTests.java:
##########
@@ -719,4 +981,120 @@ private static String splitSizeProperty(FileFormat
fileFormat) {
"No split size property defined for format: " + fileFormat);
};
}
+
+ private void assertValueAndNullCounts(
+ FileFormat fileFormat,
+ Schema schema,
+ List<Record> genericRecords,
+ Map<Integer, Long> valueCounts,
+ Map<Integer, Long> nullValueCounts) {
+ assumeSupports(fileFormat, FEATURE_COLUMN_LEVEL_METRICS);
+ for (Types.NestedField field : schema.columns()) {
+ if (field.type().isPrimitiveType()) {
+ assertThat(valueCounts).containsKey(field.fieldId());
+ assertThat(nullValueCounts).containsKey(field.fieldId());
+
+ long nullCount =
+ genericRecords.stream().filter(r -> r.getField(field.name()) ==
null).count();
+
+
assertThat(valueCounts.get(field.fieldId())).isEqualTo(genericRecords.size());
+ assertThat(nullValueCounts.get(field.fieldId())).isEqualTo(nullCount);
+ }
+ }
+ }
+
+ private void assertBounds(
+ FileFormat fileFormat,
+ Schema schema,
+ List<Record> genericRecords,
+ Map<Integer, ByteBuffer> lowerBounds,
+ Map<Integer, ByteBuffer> upperBounds) {
+ assumeSupports(fileFormat, FEATURE_COLUMN_LEVEL_METRICS);
+ for (Types.NestedField field : schema.columns()) {
+ if (field.type().isPrimitiveType()) {
+ assertThat(lowerBounds).containsKey(field.fieldId());
+ assertThat(upperBounds).containsKey(field.fieldId());
+
+ ByteBuffer lowerBuffer = lowerBounds.get(field.fieldId());
+ ByteBuffer upperBuffer = upperBounds.get(field.fieldId());
+
+ Comparator<Object> cmp =
Comparators.forType(field.type().asPrimitiveType());
+
+ Object[] minMax = computeMinMax(genericRecords, field, cmp);
+ Object expectedMin = minMax[0];
+ Object expectedMax = minMax[1];
+
+ if (expectedMin != null) {
+ assertThat(lowerBuffer).isNotNull();
+ Object actualLower = Conversions.fromByteBuffer(field.type(),
lowerBuffer);
+ assertThat(cmp.compare(actualLower, expectedMin)).isEqualTo(0);
+ }
+
+ if (expectedMax != null) {
+ assertThat(upperBuffer).isNotNull();
+ Object actualUpper = Conversions.fromByteBuffer(field.type(),
upperBuffer);
+ assertThat(cmp.compare(actualUpper, expectedMax)).isEqualTo(0);
+ }
+ }
+ }
+ }
+
+ private static Object[] computeMinMax(
+ List<Record> records, Types.NestedField field, Comparator<Object> cmp) {
+ Object min = null;
+ Object max = null;
+ for (Record record : records) {
+ Object value = record.getField(field.name());
+ if (value == null) {
+ continue;
+ }
+
+ if (value instanceof Float && ((Float) value).isNaN()) {
+ continue;
+ }
+
+ if (value instanceof Double && ((Double) value).isNaN()) {
+ continue;
+ }
+
+ if (min == null || cmp.compare(value, min) < 0) {
+ min = value;
+ }
+
+ if (max == null || cmp.compare(value, max) > 0) {
+ max = value;
+ }
+ }
+
+ return new Object[] {min, max};
+ }
+
+ private void assertBoundsNull(
+ FileFormat fileFormat,
+ Schema schema,
+ Map<Integer, ByteBuffer> lowerBounds,
+ Map<Integer, ByteBuffer> upperBounds) {
+ assumeSupports(fileFormat, FEATURE_COLUMN_LEVEL_METRICS);
Review Comment:
Change to return instead of skipping all the test
##########
data/src/test/java/org/apache/iceberg/data/BaseFormatModelTests.java:
##########
@@ -628,6 +644,239 @@ void
testReaderBuilderRecordsPerBatchNotSupported(FileFormat fileFormat) throws
.isInstanceOf(UnsupportedOperationException.class);
}
+ @ParameterizedTest
+ @FieldSource("FILE_FORMATS")
+ void testDataWriterMetricsCollection(FileFormat fileFormat) throws
IOException {
+ DataGenerator dataGenerator = new DataGenerators.DefaultSchema();
+ Schema schema = dataGenerator.schema();
+
+ List<Record> genericRecords = dataGenerator.generateRecords();
+ DataFile dataFile = writeGenericRecords(fileFormat, schema,
genericRecords);
+
+ assertThat(dataFile).isNotNull();
+ assertThat(dataFile.recordCount()).isEqualTo(genericRecords.size());
+
+ assertValueAndNullCounts(
+ fileFormat, schema, genericRecords, dataFile.valueCounts(),
dataFile.nullValueCounts());
+ assertBounds(
+ fileFormat, schema, genericRecords, dataFile.lowerBounds(),
dataFile.upperBounds());
+
+ assertThat(dataFile.columnSizes()).isNotNull().isNotEmpty();
+ }
+
+ @ParameterizedTest
+ @FieldSource("FILE_FORMATS")
+ void testDataWriterMetricsWithNoneMode(FileFormat fileFormat) throws
IOException {
+ DataGenerator dataGenerator = new DataGenerators.DefaultSchema();
+ Schema schema = dataGenerator.schema();
+
+ TestTables.TestTable table =
+ TestTables.create(
+ tableDir,
+ "test",
+ schema,
+ PartitionSpec.unpartitioned(),
+ 3,
+ ImmutableMap.of(TableProperties.DEFAULT_WRITE_METRICS_MODE,
"none"));
+
+ MetricsConfig noneConfig = MetricsConfig.forTable(table);
+ List<Record> genericRecords = dataGenerator.generateRecords();
+ DataFile dataFile = writeGenericRecords(fileFormat, schema,
genericRecords, noneConfig);
+
+ assertThat(dataFile).isNotNull();
+ assertThat(dataFile.recordCount()).isEqualTo(genericRecords.size());
+
+ assertCountsNull(fileFormat, schema, dataFile.valueCounts(),
dataFile.nullValueCounts());
+ assertBoundsNull(fileFormat, schema, dataFile.lowerBounds(),
dataFile.upperBounds());
+ }
+
+ @ParameterizedTest
+ @FieldSource("FILE_FORMATS")
+ void testDataWriterMetricsWithCountsMode(FileFormat fileFormat) throws
IOException {
+ DataGenerator dataGenerator = new DataGenerators.DefaultSchema();
+ Schema schema = dataGenerator.schema();
+ TestTables.TestTable table =
+ TestTables.create(
+ tableDir,
+ "test",
+ schema,
+ PartitionSpec.unpartitioned(),
+ 3,
+ ImmutableMap.of(TableProperties.DEFAULT_WRITE_METRICS_MODE,
"counts"));
+
+ MetricsConfig countsConfig = MetricsConfig.forTable(table);
+
+ List<Record> genericRecords = dataGenerator.generateRecords();
+ DataFile dataFile = writeGenericRecords(fileFormat, schema,
genericRecords, countsConfig);
+
+ assertThat(dataFile).isNotNull();
+ assertThat(dataFile.recordCount()).isEqualTo(genericRecords.size());
+
+ // In the counts mode, valueCounts and nullValueCounts should be present,
while lowerBounds and
+ // upperBounds should be null.
+ assertValueAndNullCounts(
+ fileFormat, schema, genericRecords, dataFile.valueCounts(),
dataFile.nullValueCounts());
+ assertBoundsNull(fileFormat, schema, dataFile.lowerBounds(),
dataFile.upperBounds());
+ }
+
+ @ParameterizedTest
+ @FieldSource("FILE_FORMATS")
+ void testEqualityDeleteWriterMetricsCollection(FileFormat fileFormat) throws
IOException {
+ DataGenerator dataGenerator = new DataGenerators.DefaultSchema();
+ Schema schema = dataGenerator.schema();
+ FileWriterBuilder<EqualityDeleteWriter<Record>, Object> writerBuilder =
+ FormatModelRegistry.equalityDeleteWriteBuilder(fileFormat,
Record.class, encryptedFile);
+
+ EqualityDeleteWriter<Record> writer =
+ writerBuilder
+ .schema(schema)
+ .spec(PartitionSpec.unpartitioned())
+ .equalityFieldIds(1)
+ .build();
+
+ List<Record> genericRecords = dataGenerator.generateRecords();
+
+ try (writer) {
+ genericRecords.forEach(writer::write);
+ }
+
+ DeleteFile deleteFile = writer.toDeleteFile();
+
+ assertThat(deleteFile).isNotNull();
+ assertThat(deleteFile.recordCount()).isEqualTo(genericRecords.size());
+
+ assertValueAndNullCounts(
+ fileFormat, schema, genericRecords, deleteFile.valueCounts(),
deleteFile.nullValueCounts());
+ assertBounds(
+ fileFormat, schema, genericRecords, deleteFile.lowerBounds(),
deleteFile.upperBounds());
+ }
+
+ @ParameterizedTest
+ @FieldSource("FILE_FORMATS")
+ void testPositionDeleteWriterMetricsSingleFile(FileFormat fileFormat) throws
IOException {
+ // Single file reference: counts are removed but bounds are preserved.
+ List<PositionDelete<T>> deletes =
+ ImmutableList.of(
+ PositionDelete.<T>create().set("d-file-1.parquet", 0L),
+ PositionDelete.<T>create().set("d-file-1.parquet", 5L),
+ PositionDelete.<T>create().set("d-file-1.parquet", 3L));
+
+ writePositionDeletesAndAssertMetrics(fileFormat, deletes, true);
+ }
+
+ @ParameterizedTest
+ @FieldSource("FILE_FORMATS")
+ void testPositionDeleteWriterMetricsMultipleFiles(FileFormat fileFormat)
throws IOException {
+ // Multiple file references: both counts and bounds are removed.
+ List<PositionDelete<T>> deletes =
+ ImmutableList.of(
+ PositionDelete.<T>create().set("d-file-1.parquet", 0L),
+ PositionDelete.<T>create().set("d-file-1.parquet", 5L),
+ PositionDelete.<T>create().set("d-file-2.parquet", 3L));
+
+ writePositionDeletesAndAssertMetrics(fileFormat, deletes, false);
+ }
+
+ private void writePositionDeletesAndAssertMetrics(
Review Comment:
Get it.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]