forked from apache/iceberg
/
TestRewriteDataFilesProcedure.java
826 lines (717 loc) · 30.8 KB
/
TestRewriteDataFilesProcedure.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.iceberg.spark.extensions;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import org.apache.iceberg.AssertHelpers;
import org.apache.iceberg.SnapshotSummary;
import org.apache.iceberg.TableProperties;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.exceptions.ValidationException;
import org.apache.iceberg.expressions.NamedReference;
import org.apache.iceberg.expressions.Zorder;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.spark.ExtendedParser;
import org.apache.iceberg.spark.SparkCatalogConfig;
import org.apache.iceberg.spark.SparkTableCache;
import org.apache.iceberg.spark.source.ThreeColumnRecord;
import org.apache.spark.sql.AnalysisException;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.analysis.NoSuchProcedureException;
import org.apache.spark.sql.internal.SQLConf;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestRewriteDataFilesProcedure extends SparkExtensionsTestBase {
private static final String QUOTED_SPECIAL_CHARS_TABLE_NAME = "`table:with.special:chars`";
public TestRewriteDataFilesProcedure(
String catalogName, String implementation, Map<String, String> config) {
super(catalogName, implementation, config);
}
@BeforeClass
public static void setupSpark() {
// disable AQE as tests assume that writes generate a particular number of files
spark.conf().set(SQLConf.ADAPTIVE_EXECUTION_ENABLED().key(), "false");
}
@After
public void removeTable() {
sql("DROP TABLE IF EXISTS %s", tableName);
sql("DROP TABLE IF EXISTS %s", tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
}
@Test
public void testZOrderSortExpression() {
List<ExtendedParser.RawOrderField> order =
ExtendedParser.parseSortOrder(spark, "c1, zorder(c2, c3)");
Assert.assertEquals("Should parse 2 order fields", 2, order.size());
Assert.assertEquals(
"First field should be a ref", "c1", ((NamedReference<?>) order.get(0).term()).name());
Assert.assertTrue("Second field should be zorder", order.get(1).term() instanceof Zorder);
}
@Test
public void testRewriteDataFilesInEmptyTable() {
createTable();
List<Object[]> output = sql("CALL %s.system.rewrite_data_files('%s')", catalogName, tableIdent);
assertEquals("Procedure output must match", ImmutableList.of(row(0, 0, 0L, 0)), output);
}
@Test
public void testRewriteDataFilesOnPartitionTable() {
createPartitionTable();
// create 5 files for each partition (c2 = 'foo' and c2 = 'bar')
insertData(10);
List<Object[]> expectedRecords = currentData();
List<Object[]> output =
sql("CALL %s.system.rewrite_data_files(table => '%s')", catalogName, tableIdent);
assertEquals(
"Action should rewrite 10 data files and add 2 data files (one per partition) ",
row(10, 2),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesOnNonPartitionTable() {
createTable();
// create 10 files under non-partitioned table
insertData(10);
List<Object[]> expectedRecords = currentData();
List<Object[]> output =
sql("CALL %s.system.rewrite_data_files(table => '%s')", catalogName, tableIdent);
assertEquals(
"Action should rewrite 10 data files and add 1 data files",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithOptions() {
createTable();
// create 10 files under non-partitioned table
insertData(10);
List<Object[]> expectedRecords = currentData();
// set the min-input-files = 12, instead of default 5 to skip compacting the files.
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s', options => map('min-input-files','12'))",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 0 data files and add 0 data files",
ImmutableList.of(row(0, 0, 0L, 0)),
output);
List<Object[]> actualRecords = currentData();
assertEquals("Data should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithSortStrategy() {
createTable();
// create 10 files under non-partitioned table
insertData(10);
List<Object[]> expectedRecords = currentData();
// set sort_order = c1 DESC LAST
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s', "
+ "strategy => 'sort', sort_order => 'c1 DESC NULLS LAST')",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 10 data files and add 1 data files",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithSortStrategyAndMultipleShufflePartitionsPerFile() {
createTable();
insertData(10 /* file count */);
List<Object[]> expectedRecords = currentData();
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files("
+ " table => '%s', "
+ " strategy => 'sort', "
+ " sort_order => 'c1', "
+ " options => map('shuffle-partitions-per-file', '2'))",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 10 data files and add 1 data files",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithZOrder() {
createTable();
// create 10 files under non-partitioned table
insertData(10);
// set z_order = c1,c2
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s', "
+ "strategy => 'sort', sort_order => 'zorder(c1,c2)')",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 10 data files and add 1 data files",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
// Due to Z_order, the data written will be in the below order.
// As there is only one small output file, we can validate the query ordering (as it will not
// change).
ImmutableList<Object[]> expectedRows =
ImmutableList.of(
row(2, "bar", null),
row(2, "bar", null),
row(2, "bar", null),
row(2, "bar", null),
row(2, "bar", null),
row(1, "foo", null),
row(1, "foo", null),
row(1, "foo", null),
row(1, "foo", null),
row(1, "foo", null));
assertEquals("Should have expected rows", expectedRows, sql("SELECT * FROM %s", tableName));
}
@Test
public void testRewriteDataFilesWithZOrderAndMultipleShufflePartitionsPerFile() {
createTable();
insertData(10 /* file count */);
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files("
+ " table => '%s', "
+ "strategy => 'sort', "
+ " sort_order => 'zorder(c1, c2)', "
+ " options => map('shuffle-partitions-per-file', '2'))",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 10 data files and add 1 data files",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// Due to Z_order, the data written will be in the below order.
// As there is only one small output file, we can validate the query ordering (as it will not
// change).
ImmutableList<Object[]> expectedRows =
ImmutableList.of(
row(2, "bar", null),
row(2, "bar", null),
row(2, "bar", null),
row(2, "bar", null),
row(2, "bar", null),
row(1, "foo", null),
row(1, "foo", null),
row(1, "foo", null),
row(1, "foo", null),
row(1, "foo", null));
assertEquals("Should have expected rows", expectedRows, sql("SELECT * FROM %s", tableName));
}
@Test
public void testRewriteDataFilesWithFilter() {
createTable();
// create 10 files under non-partitioned table
insertData(10);
List<Object[]> expectedRecords = currentData();
// select only 5 files for compaction (files that may have c1 = 1)
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s',"
+ " where => 'c1 = 1 and c2 is not null')",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 5 data files (containing c1 = 1) and add 1 data files",
row(5, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithDeterministicTrueFilter() {
createTable();
// create 10 files under non-partitioned table
insertData(10);
List<Object[]> expectedRecords = currentData();
// select all 10 files for compaction
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s', where => '1=1')",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 10 data files and add 1 data files",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithDeterministicFalseFilter() {
createTable();
// create 10 files under non-partitioned table
insertData(10);
List<Object[]> expectedRecords = currentData();
// select no files for compaction
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s', where => '0=1')",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 0 data files and add 0 data files",
row(0, 0),
Arrays.copyOf(output.get(0), 2));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithFilterOnPartitionTable() {
createPartitionTable();
// create 5 files for each partition (c2 = 'foo' and c2 = 'bar')
insertData(10);
List<Object[]> expectedRecords = currentData();
// select only 5 files for compaction (files in the partition c2 = 'bar')
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c2 = \"bar\"')",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 5 data files from single matching partition"
+ "(containing c2 = bar) and add 1 data files",
row(5, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithInFilterOnPartitionTable() {
createPartitionTable();
// create 5 files for each partition (c2 = 'foo' and c2 = 'bar')
insertData(10);
List<Object[]> expectedRecords = currentData();
// select only 5 files for compaction (files in the partition c2 in ('bar'))
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c2 in (\"bar\")')",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 5 data files from single matching partition"
+ "(containing c2 = bar) and add 1 data files",
row(5, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
@Test
public void testRewriteDataFilesWithAllPossibleFilters() {
createPartitionTable();
// create 5 files for each partition (c2 = 'foo' and c2 = 'bar')
insertData(10);
// Pass the literal value which is not present in the data files.
// So that parsing can be tested on a same dataset without actually compacting the files.
// EqualTo
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 = 3')",
catalogName, tableIdent);
// GreaterThan
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 > 3')",
catalogName, tableIdent);
// GreaterThanOrEqual
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 >= 3')",
catalogName, tableIdent);
// LessThan
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 < 0')",
catalogName, tableIdent);
// LessThanOrEqual
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 <= 0')",
catalogName, tableIdent);
// In
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 in (3,4,5)')",
catalogName, tableIdent);
// IsNull
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 is null')",
catalogName, tableIdent);
// IsNotNull
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c3 is not null')",
catalogName, tableIdent);
// And
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 = 3 and c2 = \"bar\"')",
catalogName, tableIdent);
// Or
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 = 3 or c1 = 5')",
catalogName, tableIdent);
// Not
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c1 not in (1,2)')",
catalogName, tableIdent);
// StringStartsWith
sql(
"CALL %s.system.rewrite_data_files(table => '%s'," + " where => 'c2 like \"%s\"')",
catalogName, tableIdent, "car%");
// TODO: Enable when org.apache.iceberg.spark.SparkFilters have implementations for
// StringEndsWith & StringContains
// StringEndsWith
// sql("CALL %s.system.rewrite_data_files(table => '%s'," +
// " where => 'c2 like \"%s\"')", catalogName, tableIdent, "%car");
// StringContains
// sql("CALL %s.system.rewrite_data_files(table => '%s'," +
// " where => 'c2 like \"%s\"')", catalogName, tableIdent, "%car%");
}
@Test
public void testRewriteDataFilesWithInvalidInputs() {
createTable();
// create 2 files under non-partitioned table
insertData(2);
// Test for invalid strategy
AssertHelpers.assertThrows(
"Should reject calls with unsupported strategy error message",
IllegalArgumentException.class,
"unsupported strategy: temp. Only binpack or sort is supported",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', options => map('min-input-files','2'), "
+ "strategy => 'temp')",
catalogName, tableIdent));
// Test for sort_order with binpack strategy
AssertHelpers.assertThrows(
"Should reject calls with error message",
IllegalArgumentException.class,
"Must use only one rewriter type (bin-pack, sort, zorder)",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', strategy => 'binpack', "
+ "sort_order => 'c1 ASC NULLS FIRST')",
catalogName, tableIdent));
// Test for sort strategy without any (default/user defined) sort_order
AssertHelpers.assertThrows(
"Should reject calls with error message",
IllegalArgumentException.class,
"Cannot sort data without a valid sort order",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', strategy => 'sort')",
catalogName, tableIdent));
// Test for sort_order with invalid null order
AssertHelpers.assertThrows(
"Should reject calls with error message",
IllegalArgumentException.class,
"Unable to parse sortOrder:",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', strategy => 'sort', "
+ "sort_order => 'c1 ASC none')",
catalogName, tableIdent));
// Test for sort_order with invalid sort direction
AssertHelpers.assertThrows(
"Should reject calls with error message",
IllegalArgumentException.class,
"Unable to parse sortOrder:",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', strategy => 'sort', "
+ "sort_order => 'c1 none NULLS FIRST')",
catalogName, tableIdent));
// Test for sort_order with invalid column name
AssertHelpers.assertThrows(
"Should reject calls with error message",
ValidationException.class,
"Cannot find field 'col1' in struct:"
+ " struct<1: c1: optional int, 2: c2: optional string, 3: c3: optional string>",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', strategy => 'sort', "
+ "sort_order => 'col1 DESC NULLS FIRST')",
catalogName, tableIdent));
// Test with invalid filter column col1
AssertHelpers.assertThrows(
"Should reject calls with error message",
IllegalArgumentException.class,
"Cannot parse predicates in where option: col1 = 3",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', " + "where => 'col1 = 3')",
catalogName, tableIdent));
// Test for z_order with invalid column name
AssertHelpers.assertThrows(
"Should reject calls with error message",
IllegalArgumentException.class,
"Cannot find column 'col1' in table schema (case sensitive = false): "
+ "struct<1: c1: optional int, 2: c2: optional string, 3: c3: optional string>",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', strategy => 'sort', "
+ "sort_order => 'zorder(col1)')",
catalogName, tableIdent));
// Test for z_order with sort_order
AssertHelpers.assertThrows(
"Should reject calls with error message",
IllegalArgumentException.class,
"Cannot mix identity sort columns and a Zorder sort expression:" + " c1,zorder(c2,c3)",
() ->
sql(
"CALL %s.system.rewrite_data_files(table => '%s', strategy => 'sort', "
+ "sort_order => 'c1,zorder(c2,c3)')",
catalogName, tableIdent));
}
@Test
public void testInvalidCasesForRewriteDataFiles() {
AssertHelpers.assertThrows(
"Should not allow mixed args",
AnalysisException.class,
"Named and positional arguments cannot be mixed",
() -> sql("CALL %s.system.rewrite_data_files('n', table => 't')", catalogName));
AssertHelpers.assertThrows(
"Should not resolve procedures in arbitrary namespaces",
NoSuchProcedureException.class,
"not found",
() -> sql("CALL %s.custom.rewrite_data_files('n', 't')", catalogName));
AssertHelpers.assertThrows(
"Should reject calls without all required args",
AnalysisException.class,
"Missing required parameters",
() -> sql("CALL %s.system.rewrite_data_files()", catalogName));
AssertHelpers.assertThrows(
"Should reject duplicate arg names name",
AnalysisException.class,
"Duplicate procedure argument: table",
() -> sql("CALL %s.system.rewrite_data_files(table => 't', table => 't')", catalogName));
AssertHelpers.assertThrows(
"Should reject calls with empty table identifier",
IllegalArgumentException.class,
"Cannot handle an empty identifier",
() -> sql("CALL %s.system.rewrite_data_files('')", catalogName));
}
@Test
public void testBinPackTableWithSpecialChars() {
Assume.assumeTrue(catalogName.equals(SparkCatalogConfig.HADOOP.catalogName()));
TableIdentifier identifier =
TableIdentifier.of("default", QUOTED_SPECIAL_CHARS_TABLE_NAME.replaceAll("`", ""));
sql(
"CREATE TABLE %s (c1 int, c2 string, c3 string) USING iceberg",
tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
insertData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME), 10);
List<Object[]> expectedRecords = currentData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s', where => 'c2 is not null')",
catalogName, tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
assertEquals(
"Action should rewrite 10 data files and add 1 data file",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isEqualTo(
Long.valueOf(snapshotSummary(identifier).get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
Assert.assertEquals("Table cache must be empty", 0, SparkTableCache.get().size());
}
@Test
public void testSortTableWithSpecialChars() {
Assume.assumeTrue(catalogName.equals(SparkCatalogConfig.HADOOP.catalogName()));
TableIdentifier identifier =
TableIdentifier.of("default", QUOTED_SPECIAL_CHARS_TABLE_NAME.replaceAll("`", ""));
sql(
"CREATE TABLE %s (c1 int, c2 string, c3 string) USING iceberg",
tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
insertData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME), 10);
List<Object[]> expectedRecords = currentData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files("
+ " table => '%s',"
+ " strategy => 'sort',"
+ " sort_order => 'c1',"
+ " where => 'c2 is not null')",
catalogName, tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
assertEquals(
"Action should rewrite 10 data files and add 1 data file",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(
Long.valueOf(snapshotSummary(identifier).get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
Assert.assertEquals("Table cache must be empty", 0, SparkTableCache.get().size());
}
@Test
public void testZOrderTableWithSpecialChars() {
Assume.assumeTrue(catalogName.equals(SparkCatalogConfig.HADOOP.catalogName()));
TableIdentifier identifier =
TableIdentifier.of("default", QUOTED_SPECIAL_CHARS_TABLE_NAME.replaceAll("`", ""));
sql(
"CREATE TABLE %s (c1 int, c2 string, c3 string) USING iceberg",
tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
insertData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME), 10);
List<Object[]> expectedRecords = currentData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files("
+ " table => '%s',"
+ " strategy => 'sort',"
+ " sort_order => 'zorder(c1, c2)',"
+ " where => 'c2 is not null')",
catalogName, tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
assertEquals(
"Action should rewrite 10 data files and add 1 data file",
row(10, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(
Long.valueOf(snapshotSummary(identifier).get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData(tableName(QUOTED_SPECIAL_CHARS_TABLE_NAME));
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
Assert.assertEquals("Table cache must be empty", 0, SparkTableCache.get().size());
}
@Test
public void testDefaultSortOrder() {
createTable();
// add a default sort order for a table
sql("ALTER TABLE %s WRITE ORDERED BY c2", tableName);
// this creates 2 files under non-partitioned table due to sort order.
insertData(10);
List<Object[]> expectedRecords = currentData();
// When the strategy is set to 'sort' but the sort order is not specified,
// use table's default sort order.
List<Object[]> output =
sql(
"CALL %s.system.rewrite_data_files(table => '%s', "
+ "strategy => 'sort', "
+ "options => map('min-input-files','2'))",
catalogName, tableIdent);
assertEquals(
"Action should rewrite 2 data files and add 1 data files",
row(2, 1),
Arrays.copyOf(output.get(0), 2));
// verify rewritten bytes separately
assertThat(output.get(0)).hasSize(4);
assertThat(output.get(0)[2])
.isInstanceOf(Long.class)
.isEqualTo(Long.valueOf(snapshotSummary().get(SnapshotSummary.REMOVED_FILE_SIZE_PROP)));
List<Object[]> actualRecords = currentData();
assertEquals("Data after compaction should not change", expectedRecords, actualRecords);
}
private void createTable() {
sql("CREATE TABLE %s (c1 int, c2 string, c3 string) USING iceberg", tableName);
}
private void createPartitionTable() {
sql(
"CREATE TABLE %s (c1 int, c2 string, c3 string) "
+ "USING iceberg "
+ "PARTITIONED BY (c2) "
+ "TBLPROPERTIES ('%s' '%s')",
tableName,
TableProperties.WRITE_DISTRIBUTION_MODE,
TableProperties.WRITE_DISTRIBUTION_MODE_NONE);
}
private void insertData(int filesCount) {
insertData(tableName, filesCount);
}
private void insertData(String table, int filesCount) {
ThreeColumnRecord record1 = new ThreeColumnRecord(1, "foo", null);
ThreeColumnRecord record2 = new ThreeColumnRecord(2, "bar", null);
List<ThreeColumnRecord> records = Lists.newArrayList();
IntStream.range(0, filesCount / 2)
.forEach(
i -> {
records.add(record1);
records.add(record2);
});
Dataset<Row> df =
spark.createDataFrame(records, ThreeColumnRecord.class).repartition(filesCount);
try {
df.writeTo(table).append();
} catch (org.apache.spark.sql.catalyst.analysis.NoSuchTableException e) {
throw new RuntimeException(e);
}
}
private Map<String, String> snapshotSummary() {
return snapshotSummary(tableIdent);
}
private Map<String, String> snapshotSummary(TableIdentifier tableIdentifier) {
return validationCatalog.loadTable(tableIdentifier).currentSnapshot().summary();
}
private List<Object[]> currentData() {
return currentData(tableName);
}
private List<Object[]> currentData(String table) {
return rowsToJava(spark.sql("SELECT * FROM " + table + " order by c1, c2, c3").collectAsList());
}
}