Skip to content

Commit e6c5014

Browse files
committed
Add tests
1 parent 91637db commit e6c5014

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.iceberg;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
23+
24+
import org.apache.iceberg.types.Types;
25+
import org.junit.jupiter.api.Test;
26+
27+
public class TestPartitionStats {
28+
29+
private static final PartitionData PARTITION =
30+
new PartitionData(
31+
Types.StructType.of(Types.NestedField.required(1, "foo", Types.IntegerType.get())));
32+
33+
@Test
34+
public void testAppendWithAllValues() {
35+
PartitionStats stats1 =
36+
createStats(100L, 15, 1000L, 2L, 500, 1L, 200, 15L, 1625077800000L, 12345L);
37+
PartitionStats stats2 = createStats(200L, 7, 500L, 1L, 100, 0L, 50, 7L, 1625077900000L, 12346L);
38+
39+
stats1.appendStats(stats2);
40+
41+
validateStats(stats1, 300L, 22, 1500L, 3L, 600, 1L, 250, 22L, 1625077900000L, 12346L);
42+
}
43+
44+
@Test
45+
public void testAppendWithThisNullOptionalField() {
46+
PartitionStats stats1 = createStats(100L, 15, 1000L, 2L, 500, 1L, 200, null, null, null);
47+
PartitionStats stats2 = createStats(100L, 7, 500L, 1L, 100, 0L, 50, 7L, 1625077900000L, 12346L);
48+
49+
stats1.appendStats(stats2);
50+
51+
validateStats(stats1, 200L, 22, 1500L, 3L, 600, 1L, 250, 7L, 1625077900000L, 12346L);
52+
}
53+
54+
@Test
55+
public void testAppendWithBothNullOptionalFields() {
56+
PartitionStats stats1 = createStats(100L, 15, 1000L, 2L, 500, 1L, 200, null, null, null);
57+
PartitionStats stats2 = createStats(100L, 7, 500L, 1L, 100, 0L, 50, null, null, null);
58+
59+
stats1.appendStats(stats2);
60+
61+
validateStats(stats1, 200L, 22, 1500L, 3L, 600, 1L, 250, null, null, null);
62+
}
63+
64+
@Test
65+
public void testAppendWithOtherNullOptionalFields() {
66+
PartitionStats stats1 =
67+
createStats(100L, 15, 1000L, 2L, 500, 1L, 200, 15L, 1625077900000L, 12346L);
68+
PartitionStats stats2 = createStats(100L, 7, 500L, 1L, 100, 0L, 50, null, null, null);
69+
70+
stats1.appendStats(stats2);
71+
72+
validateStats(stats1, 200L, 22, 1500L, 3L, 600, 1L, 250, 15L, 1625077900000L, 12346L);
73+
}
74+
75+
@Test
76+
public void testAppendWithDifferentSpec() {
77+
PartitionStats stats1 = new PartitionStats(PARTITION, 1);
78+
PartitionStats stats2 = new PartitionStats(PARTITION, 2);
79+
80+
assertThatThrownBy(() -> stats1.appendStats(stats2))
81+
.isInstanceOf(IllegalArgumentException.class)
82+
.hasMessage("Spec IDs must match");
83+
}
84+
85+
private PartitionStats createStats(
86+
long dataRecordCount,
87+
int dataFileCount,
88+
long totalDataFileSizeInBytes,
89+
long positionDeleteRecordCount,
90+
int positionDeleteFileCount,
91+
long equalityDeleteRecordCount,
92+
int equalityDeleteFileCount,
93+
Long totalRecordCount,
94+
Long lastUpdatedAt,
95+
Long lastUpdatedSnapshotId) {
96+
97+
PartitionStats stats = new PartitionStats(PARTITION, 1);
98+
stats.set(2, dataRecordCount);
99+
stats.set(3, dataFileCount);
100+
stats.set(4, totalDataFileSizeInBytes);
101+
stats.set(5, positionDeleteRecordCount);
102+
stats.set(6, positionDeleteFileCount);
103+
stats.set(7, equalityDeleteRecordCount);
104+
stats.set(8, equalityDeleteFileCount);
105+
stats.set(9, totalRecordCount);
106+
stats.set(10, lastUpdatedAt);
107+
stats.set(11, lastUpdatedSnapshotId);
108+
109+
return stats;
110+
}
111+
112+
private void validateStats(PartitionStats stats, Object... expectedValues) {
113+
// Spec id and partition data should be unchanged
114+
assertThat(stats.get(0, PartitionData.class)).isEqualTo(PARTITION);
115+
assertThat(stats.get(1, Integer.class)).isEqualTo(1);
116+
117+
for (int i = 0; i < expectedValues.length; i++) {
118+
if (expectedValues[i] == null) {
119+
assertThat(stats.get(i + 2, Object.class)).isNull();
120+
} else {
121+
assertThat(stats.get(i + 2, Object.class)).isEqualTo(expectedValues[i]);
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)