@@ -52,6 +52,13 @@ public void testPartitionStatsOnEmptyTable() throws Exception {
52
52
() -> PartitionStatsUtil .computeStats (testTable , testTable .currentSnapshot ()))
53
53
.isInstanceOf (IllegalArgumentException .class )
54
54
.hasMessage ("snapshot cannot be null" );
55
+
56
+ assertThatThrownBy (
57
+ () ->
58
+ PartitionStatsUtil .computeStatsIncremental (
59
+ testTable , testTable .currentSnapshot (), testTable .currentSnapshot ()))
60
+ .isInstanceOf (IllegalArgumentException .class )
61
+ .hasMessage ("Current snapshot cannot be null" );
55
62
}
56
63
57
64
@ Test
@@ -73,6 +80,55 @@ public void testPartitionStatsOnUnPartitionedTable() throws Exception {
73
80
() -> PartitionStatsUtil .computeStats (testTable , testTable .currentSnapshot ()))
74
81
.isInstanceOf (IllegalArgumentException .class )
75
82
.hasMessage ("table must be partitioned" );
83
+
84
+ assertThatThrownBy (
85
+ () ->
86
+ PartitionStatsUtil .computeStatsIncremental (
87
+ testTable , testTable .currentSnapshot (), testTable .currentSnapshot ()))
88
+ .isInstanceOf (IllegalArgumentException .class )
89
+ .hasMessage ("Table must be partitioned" );
90
+ }
91
+
92
+ @ Test
93
+ public void testNonAncestorSnapshot () throws Exception {
94
+ Table testTable =
95
+ TestTables .create (tempDir ("invalid_ancestor" ), "invalid_ancestor" , SCHEMA , SPEC , 2 );
96
+
97
+ List <DataFile > files = prepareDataFiles (testTable );
98
+ AppendFiles appendFiles = testTable .newAppend ();
99
+ files .forEach (appendFiles ::appendFile );
100
+ appendFiles .commit ();
101
+ Snapshot snapshot1 = testTable .currentSnapshot ();
102
+
103
+ appendFiles = testTable .newAppend ();
104
+ files .forEach (appendFiles ::appendFile );
105
+ appendFiles .commit ();
106
+ Snapshot snapshot2 = testTable .currentSnapshot ();
107
+
108
+ assertThatThrownBy (
109
+ () -> PartitionStatsUtil .computeStatsIncremental (testTable , snapshot2 , snapshot1 ))
110
+ .isInstanceOf (IllegalArgumentException .class )
111
+ .hasMessage (
112
+ String .format (
113
+ "Starting snapshot %s is not an ancestor of current snapshot %s" ,
114
+ snapshot2 .snapshotId (), snapshot1 .snapshotId ()));
115
+ }
116
+
117
+ @ Test
118
+ public void testSameSnapshots () throws Exception {
119
+ Table testTable = TestTables .create (tempDir ("same_snapshot" ), "same_snapshot" , SCHEMA , SPEC , 2 );
120
+
121
+ List <DataFile > files = prepareDataFiles (testTable );
122
+ AppendFiles appendFiles = testTable .newAppend ();
123
+ files .forEach (appendFiles ::appendFile );
124
+ appendFiles .commit ();
125
+
126
+ assertThatThrownBy (
127
+ () ->
128
+ PartitionStatsUtil .computeStatsIncremental (
129
+ testTable , testTable .currentSnapshot (), testTable .currentSnapshot ()))
130
+ .isInstanceOf (IllegalArgumentException .class )
131
+ .hasMessage ("Both the snapshots are same" );
76
132
}
77
133
78
134
@ Test
@@ -212,6 +268,88 @@ public void testPartitionStats() throws Exception {
212
268
snapshot3 .snapshotId ()));
213
269
}
214
270
271
+ @ Test
272
+ public void testPartitionStatsIncrementalCompute () throws Exception {
273
+ Table testTable =
274
+ TestTables .create (tempDir ("compute_incremental" ), "compute_incremental" , SCHEMA , SPEC , 2 );
275
+
276
+ List <DataFile > files = prepareDataFiles (testTable );
277
+ for (int i = 0 ; i < 3 ; i ++) {
278
+ // insert same set of records thrice to have a new manifest files
279
+ AppendFiles appendFiles = testTable .newAppend ();
280
+ files .forEach (appendFiles ::appendFile );
281
+ appendFiles .commit ();
282
+ }
283
+
284
+ Snapshot snapshotFrom = testTable .currentSnapshot ();
285
+
286
+ AppendFiles appendFiles = testTable .newAppend ();
287
+ files .forEach (appendFiles ::appendFile );
288
+ appendFiles .commit ();
289
+
290
+ Snapshot currentSnapshot = testTable .currentSnapshot ();
291
+ Types .StructType partitionType = Partitioning .partitionType (testTable );
292
+ Collection <PartitionStats > result =
293
+ PartitionStatsUtil .computeStatsIncremental (
294
+ testTable , snapshotFrom , testTable .currentSnapshot ())
295
+ .values ();
296
+ // should only contain stats from last append (one data file per partition instead of total 4)
297
+ validateStats (
298
+ result ,
299
+ Tuple .tuple (
300
+ partitionData (partitionType , "foo" , "A" ),
301
+ 0 ,
302
+ files .get (0 ).recordCount (),
303
+ 1 ,
304
+ files .get (0 ).fileSizeInBytes (),
305
+ 0L ,
306
+ 0 ,
307
+ 0L ,
308
+ 0 ,
309
+ null ,
310
+ currentSnapshot .timestampMillis (),
311
+ currentSnapshot .snapshotId ()),
312
+ Tuple .tuple (
313
+ partitionData (partitionType , "foo" , "B" ),
314
+ 0 ,
315
+ files .get (1 ).recordCount (),
316
+ 1 ,
317
+ files .get (1 ).fileSizeInBytes (),
318
+ 0L ,
319
+ 0 ,
320
+ 0L ,
321
+ 0 ,
322
+ null ,
323
+ currentSnapshot .timestampMillis (),
324
+ currentSnapshot .snapshotId ()),
325
+ Tuple .tuple (
326
+ partitionData (partitionType , "bar" , "A" ),
327
+ 0 ,
328
+ files .get (2 ).recordCount (),
329
+ 1 ,
330
+ files .get (2 ).fileSizeInBytes (),
331
+ 0L ,
332
+ 0 ,
333
+ 0L ,
334
+ 0 ,
335
+ null ,
336
+ currentSnapshot .timestampMillis (),
337
+ currentSnapshot .snapshotId ()),
338
+ Tuple .tuple (
339
+ partitionData (partitionType , "bar" , "B" ),
340
+ 0 ,
341
+ files .get (3 ).recordCount (),
342
+ 1 ,
343
+ files .get (3 ).fileSizeInBytes (),
344
+ 0L ,
345
+ 0 ,
346
+ 0L ,
347
+ 0 ,
348
+ null ,
349
+ currentSnapshot .timestampMillis (),
350
+ currentSnapshot .snapshotId ()));
351
+ }
352
+
215
353
@ Test
216
354
@ SuppressWarnings ("MethodLength" )
217
355
public void testPartitionStatsWithSchemaEvolution () throws Exception {
@@ -561,6 +699,10 @@ private static void computeAndValidatePartitionStats(Table testTable, Tuple... e
561
699
Collection <PartitionStats > result =
562
700
PartitionStatsUtil .computeStats (testTable , testTable .currentSnapshot ());
563
701
702
+ validateStats (result , expectedValues );
703
+ }
704
+
705
+ private static void validateStats (Collection <PartitionStats > result , Tuple ... expectedValues ) {
564
706
assertThat (result )
565
707
.extracting (
566
708
PartitionStats ::partition ,
0 commit comments