Guosmilesmile commented on code in PR #15906:
URL: https://github.com/apache/iceberg/pull/15906#discussion_r3130753459
##########
data/src/test/java/org/apache/iceberg/data/BaseFormatModelTests.java:
##########
@@ -719,4 +1029,247 @@ private static String splitSizeProperty(FileFormat
fileFormat) {
"No split size property defined for format: " + fileFormat);
};
}
+
+ private static void assertCounts(
+ FileFormat fileFormat, Schema schema, List<Record> genericRecords,
ContentFile<?> file) {
+ if (!supportsFeature(fileFormat, FEATURE_COLUMN_LEVEL_METRICS)) {
+ return;
+ }
+
+ Map<Integer, Long> valueCounts = file.valueCounts();
+ Map<Integer, Long> nullValueCounts = file.nullValueCounts();
+ 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 static void assertBounds(
+ FileFormat fileFormat, Schema schema, List<Record> genericRecords,
ContentFile<?> file) {
+ if (!supportsFeature(fileFormat, FEATURE_COLUMN_LEVEL_METRICS)) {
+ return;
+ }
+
+ Map<Integer, ByteBuffer> lowerBounds = file.lowerBounds();
+ Map<Integer, ByteBuffer> upperBounds = file.upperBounds();
+ 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 static void assertBoundsNull(Schema schema, ContentFile<?> file) {
+ Map<Integer, ByteBuffer> lowerBounds = file.lowerBounds();
+ Map<Integer, ByteBuffer> upperBounds = file.upperBounds();
+ for (Types.NestedField field : schema.columns()) {
+ if (field.type().isPrimitiveType()) {
+ assertThat(lowerBounds == null || lowerBounds.get(field.fieldId()) ==
null).isTrue();
+ assertThat(upperBounds == null || upperBounds.get(field.fieldId()) ==
null).isTrue();
+ }
+ }
+ }
+
+ private static void assertColumnSize(FileFormat fileFormat, ContentFile<?>
file) {
+ if (!supportsFeature(fileFormat, FEATURE_COLUMN_LEVEL_METRICS)) {
+ return;
+ }
+
+ assertThat(file.columnSizes()).isNotNull().isNotEmpty();
+ }
+
+ private static void assertColumnSizeEmpty(FileFormat fileFormat,
ContentFile<?> file) {
+ if (!supportsFeature(fileFormat, FEATURE_COLUMN_LEVEL_METRICS)) {
+ return;
+ }
+
+ assertThat(file.columnSizes()).isEmpty();
+ }
+
+ private static void assertCountsNull(Schema schema, ContentFile<?> file) {
+ Map<Integer, Long> valueCounts = file.valueCounts();
+ Map<Integer, Long> nullValueCounts = file.nullValueCounts();
+ for (Types.NestedField field : schema.columns()) {
+ if (field.type().isPrimitiveType()) {
+ assertThat(valueCounts == null || valueCounts.get(field.fieldId()) ==
null).isTrue();
+ assertThat(nullValueCounts == null ||
nullValueCounts.get(field.fieldId()) == null)
+ .isTrue();
+ }
+ }
+ }
+
+ private static void assertNanCounts(
+ FileFormat fileFormat, Schema schema, List<Record> records,
ContentFile<?> file) {
+ if (!supportsFeature(fileFormat, FEATURE_COLUMN_LEVEL_METRICS)) {
+ return;
+ }
+
+ Map<Integer, Long> nanValueCounts = file.nanValueCounts();
+ assertThat(nanValueCounts).isNotNull();
+
+ for (Types.NestedField field : schema.columns()) {
+ if (field.type().typeId() == Type.TypeID.FLOAT
+ || field.type().typeId() == Type.TypeID.DOUBLE) {
+ long expectedNanCount =
+ records.stream()
+ .map(r -> r.getField(field.name()))
+ .filter(
+ v ->
+ (v instanceof Float && ((Float) v).isNaN())
+ || (v instanceof Double && ((Double) v).isNaN()))
+ .count();
+
assertThat(nanValueCounts.get(field.fieldId())).isEqualTo(expectedNanCount);
+ }
+ }
+ }
+
+ private void writePositionDeletesAndAssertMetrics(
Review Comment:
After some thought, let's first separate the writing and validation. As for
whether to further split the validation, it depends on future usage — if there
are other reuse scenarios, we can split it further at that point.
--
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]