19
19
20
20
package org .apache .iceberg .data ;
21
21
22
+ import java .io .File ;
22
23
import java .io .IOException ;
23
24
import java .util .List ;
24
25
import java .util .Set ;
44
45
import org .junit .Test ;
45
46
import org .junit .rules .TemporaryFolder ;
46
47
48
+ import static org .assertj .core .api .Assertions .assertThat ;
49
+
47
50
public abstract class DeleteReadTests {
48
51
// Schema passed to create tables
49
52
public static final Schema SCHEMA = new Schema (
@@ -64,6 +67,12 @@ public abstract class DeleteReadTests {
64
67
private List <Record > records = null ;
65
68
private DataFile dataFile = null ;
66
69
70
+ protected final int formatVersion ;
71
+
72
+ public DeleteReadTests (int formatVersion ) {
73
+ this .formatVersion = formatVersion ;
74
+ }
75
+
67
76
@ Before
68
77
public void writeTestDataFile () throws IOException {
69
78
this .tableName = "test" ;
@@ -102,6 +111,27 @@ protected boolean expectPruned() {
102
111
return true ;
103
112
}
104
113
114
+ protected boolean countDeletes () {
115
+ return false ;
116
+ }
117
+
118
+ /**
119
+ * This will only be called after calling rowSet(String, Table, String...), and only if
120
+ * countDeletes() is true.
121
+ */
122
+ protected long deleteCount () {
123
+ return 0L ;
124
+ }
125
+
126
+ protected void checkDeleteCount (long expectedDeletes ) {
127
+ if (countDeletes ()) {
128
+ long actualDeletes = deleteCount ();
129
+ assertThat (actualDeletes )
130
+ .as ("Table should contain expected number of deletes" )
131
+ .isEqualTo (expectedDeletes );
132
+ }
133
+ }
134
+
105
135
@ Test
106
136
public void testEqualityDeletes () throws IOException {
107
137
Schema deleteRowSchema = table .schema ().select ("data" );
@@ -119,7 +149,7 @@ public void testEqualityDeletes() throws IOException {
119
149
.addDeletes (eqDeletes )
120
150
.commit ();
121
151
122
- StructLikeSet expected = rowSetWithoutIds (29 , 89 , 122 );
152
+ StructLikeSet expected = rowSetWithoutIds (table , records , 29 , 89 , 122 );
123
153
StructLikeSet actual = rowSet (tableName , table , "*" );
124
154
125
155
Assert .assertEquals ("Table should contain expected rows" , expected , actual );
@@ -142,7 +172,7 @@ public void testEqualityDeletesWithRequiredEqColumn() throws IOException {
142
172
.addDeletes (eqDeletes )
143
173
.commit ();
144
174
145
- StructLikeSet expected = selectColumns (rowSetWithoutIds (29 , 89 , 122 ), "id" );
175
+ StructLikeSet expected = selectColumns (rowSetWithoutIds (table , records , 29 , 89 , 122 ), "id" );
146
176
StructLikeSet actual = rowSet (tableName , table , "id" );
147
177
148
178
if (expectPruned ()) {
@@ -180,7 +210,7 @@ public void testEqualityDeletesSpanningMultipleDataFiles() throws IOException {
180
210
.addDeletes (eqDeletes )
181
211
.commit ();
182
212
183
- StructLikeSet expected = rowSetWithoutIds (29 , 89 , 122 , 144 );
213
+ StructLikeSet expected = rowSetWithoutIds (table , records , 29 , 89 , 122 , 144 );
184
214
StructLikeSet actual = rowSet (tableName , table , "*" );
185
215
186
216
Assert .assertEquals ("Table should contain expected rows" , expected , actual );
@@ -194,15 +224,20 @@ public void testPositionDeletes() throws IOException {
194
224
Pair .of (dataFile .path (), 6L ) // id = 122
195
225
);
196
226
197
- Pair <DeleteFile , CharSequenceSet > posDeletes = FileHelpers .writeDeleteFile (
198
- table , Files .localOutput (temp .newFile ()), Row .of (0 ), deletes );
227
+ Pair <DeleteFile , CharSequenceSet > posDeletes =
228
+ FileHelpers .writeDeleteFile (
229
+ table ,
230
+ Files .localOutput (File .createTempFile ("junit" , null , temp .getRoot ())),
231
+ Row .of (0 ),
232
+ deletes ,
233
+ formatVersion );
199
234
200
235
table .newRowDelta ()
201
236
.addDeletes (posDeletes .first ())
202
237
.validateDataFilesExist (posDeletes .second ())
203
238
.commit ();
204
239
205
- StructLikeSet expected = rowSetWithoutIds (29 , 89 , 122 );
240
+ StructLikeSet expected = rowSetWithoutIds (table , records , 29 , 89 , 122 );
206
241
StructLikeSet actual = rowSet (tableName , table , "*" );
207
242
208
243
Assert .assertEquals ("Table should contain expected rows" , expected , actual );
@@ -212,33 +247,47 @@ public void testPositionDeletes() throws IOException {
212
247
public void testMixedPositionAndEqualityDeletes () throws IOException {
213
248
Schema dataSchema = table .schema ().select ("data" );
214
249
Record dataDelete = GenericRecord .create (dataSchema );
215
- List <Record > dataDeletes = Lists .newArrayList (
216
- dataDelete .copy ("data" , "a" ), // id = 29
217
- dataDelete .copy ("data" , "d" ), // id = 89
218
- dataDelete .copy ("data" , "g" ) // id = 122
219
- );
220
-
221
- DeleteFile eqDeletes = FileHelpers .writeDeleteFile (
222
- table , Files .localOutput (temp .newFile ()), Row .of (0 ), dataDeletes , dataSchema );
223
-
224
- List <Pair <CharSequence , Long >> deletes = Lists .newArrayList (
225
- Pair .of (dataFile .path (), 3L ), // id = 89
226
- Pair .of (dataFile .path (), 5L ) // id = 121
227
- );
228
-
229
- Pair <DeleteFile , CharSequenceSet > posDeletes = FileHelpers .writeDeleteFile (
230
- table , Files .localOutput (temp .newFile ()), Row .of (0 ), deletes );
231
-
232
- table .newRowDelta ()
233
- .addDeletes (eqDeletes )
234
- .addDeletes (posDeletes .first ())
235
- .validateDataFilesExist (posDeletes .second ())
236
- .commit ();
237
-
238
- StructLikeSet expected = rowSetWithoutIds (29 , 89 , 121 , 122 );
250
+ List <Record > dataDeletes =
251
+ Lists .newArrayList (
252
+ dataDelete .copy ("data" , "a" ), // id = 29
253
+ dataDelete .copy ("data" , "d" ), // id = 89
254
+ dataDelete .copy ("data" , "g" ) // id = 122
255
+ );
256
+
257
+ DeleteFile eqDeletes =
258
+ FileHelpers .writeDeleteFile (
259
+ table ,
260
+ Files .localOutput (File .createTempFile ("junit" , null , temp .getRoot ())),
261
+ Row .of (0 ),
262
+ dataDeletes ,
263
+ dataSchema );
264
+
265
+ List <Pair <CharSequence , Long >> deletes =
266
+ Lists .newArrayList (
267
+ Pair .of (dataFile .location (), 3L ), // id = 89
268
+ Pair .of (dataFile .location (), 5L ) // id = 121
269
+ );
270
+
271
+ Pair <DeleteFile , CharSequenceSet > posDeletes =
272
+ FileHelpers .writeDeleteFile (
273
+ table ,
274
+ Files .localOutput (File .createTempFile ("junit" , null , temp .getRoot ())),
275
+ Row .of (0 ),
276
+ deletes ,
277
+ formatVersion );
278
+
279
+ table
280
+ .newRowDelta ()
281
+ .addDeletes (eqDeletes )
282
+ .addDeletes (posDeletes .first ())
283
+ .validateDataFilesExist (posDeletes .second ())
284
+ .commit ();
285
+
286
+ StructLikeSet expected = rowSetWithoutIds (table , records , 29 , 89 , 121 , 122 );
239
287
StructLikeSet actual = rowSet (tableName , table , "*" );
240
288
241
- Assert .assertEquals ("Table should contain expected rows" , expected , actual );
289
+ assertThat (actual ).as ("Table should contain expected rows" ).isEqualTo (expected );
290
+ checkDeleteCount (4L );
242
291
}
243
292
244
293
@ Test
@@ -269,7 +318,7 @@ public void testMultipleEqualityDeleteSchemas() throws IOException {
269
318
.addDeletes (idEqDeletes )
270
319
.commit ();
271
320
272
- StructLikeSet expected = rowSetWithoutIds (29 , 89 , 121 , 122 );
321
+ StructLikeSet expected = rowSetWithoutIds (table , records , 29 , 89 , 121 , 122 );
273
322
StructLikeSet actual = rowSet (tableName , table , "*" );
274
323
275
324
Assert .assertEquals ("Table should contain expected rows" , expected , actual );
@@ -306,7 +355,7 @@ public void testEqualityDeleteByNull() throws IOException {
306
355
.addDeletes (eqDeletes )
307
356
.commit ();
308
357
309
- StructLikeSet expected = rowSetWithoutIds (131 );
358
+ StructLikeSet expected = rowSetWithoutIds (table , records , 131 );
310
359
StructLikeSet actual = rowSet (tableName , table , "*" );
311
360
312
361
Assert .assertEquals ("Table should contain expected rows" , expected , actual );
@@ -321,12 +370,14 @@ private StructLikeSet selectColumns(StructLikeSet rows, String... columns) {
321
370
return set ;
322
371
}
323
372
324
- private StructLikeSet rowSetWithoutIds (int ... idsToRemove ) {
373
+ protected static StructLikeSet rowSetWithoutIds (
374
+ Table table , List <Record > recordList , int ... idsToRemove ) {
325
375
Set <Integer > deletedIds = Sets .newHashSet (ArrayUtil .toIntList (idsToRemove ));
326
376
StructLikeSet set = StructLikeSet .create (table .schema ().asStruct ());
327
- records .stream ()
328
- .filter (row -> !deletedIds .contains (row .getField ("id" )))
329
- .forEach (set ::add );
377
+ recordList .stream ()
378
+ .filter (row -> !deletedIds .contains (row .getField ("id" )))
379
+ .map (record -> new InternalRecordWrapper (table .schema ().asStruct ()).wrap (record ))
380
+ .forEach (set ::add );
330
381
return set ;
331
382
}
332
383
}
0 commit comments