/
TestHiveCatalog.java
757 lines (661 loc) · 29.2 KB
/
TestHiveCatalog.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
/*
* 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.hive;
import static org.apache.iceberg.NullOrder.NULLS_FIRST;
import static org.apache.iceberg.SortDirection.ASC;
import static org.apache.iceberg.TableProperties.CURRENT_SCHEMA;
import static org.apache.iceberg.TableProperties.CURRENT_SNAPSHOT_ID;
import static org.apache.iceberg.TableProperties.CURRENT_SNAPSHOT_SUMMARY;
import static org.apache.iceberg.TableProperties.CURRENT_SNAPSHOT_TIMESTAMP;
import static org.apache.iceberg.TableProperties.DEFAULT_PARTITION_SPEC;
import static org.apache.iceberg.TableProperties.DEFAULT_SORT_ORDER;
import static org.apache.iceberg.expressions.Expressions.bucket;
import static org.apache.iceberg.types.Types.NestedField.required;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.iceberg.AssertHelpers;
import org.apache.iceberg.CachingCatalog;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.CatalogUtil;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DataFiles;
import org.apache.iceberg.FileFormat;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.PartitionSpecParser;
import org.apache.iceberg.Schema;
import org.apache.iceberg.SchemaParser;
import org.apache.iceberg.Snapshot;
import org.apache.iceberg.SortOrder;
import org.apache.iceberg.SortOrderParser;
import org.apache.iceberg.Table;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.TableProperties;
import org.apache.iceberg.Transaction;
import org.apache.iceberg.UpdateSchema;
import org.apache.iceberg.catalog.Catalog;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.exceptions.AlreadyExistsException;
import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
import org.apache.iceberg.exceptions.NoSuchNamespaceException;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.transforms.Transform;
import org.apache.iceberg.transforms.Transforms;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.util.JsonUtil;
import org.apache.thrift.TException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.rules.TemporaryFolder;
public class TestHiveCatalog extends HiveMetastoreTest {
private static ImmutableMap meta =
ImmutableMap.of(
"owner", "apache",
"group", "iceberg",
"comment", "iceberg hiveCatalog test");
@Rule public TemporaryFolder temp = new TemporaryFolder();
@Test
public void testCreateTableBuilder() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("data", 16).build();
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
String location = temp.newFolder("tbl").toString();
try {
Table table =
catalog
.buildTable(tableIdent, schema)
.withPartitionSpec(spec)
.withLocation(location)
.withProperty("key1", "value1")
.withProperty("key2", "value2")
.create();
Assert.assertEquals(location, table.location());
Assert.assertEquals(2, table.schema().columns().size());
Assert.assertEquals(1, table.spec().fields().size());
Assert.assertEquals("value1", table.properties().get("key1"));
Assert.assertEquals("value2", table.properties().get("key2"));
} finally {
catalog.dropTable(tableIdent);
}
}
@Test
public void testCreateTableWithCaching() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("data", 16).build();
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
String location = temp.newFolder("tbl").toString();
ImmutableMap<String, String> properties = ImmutableMap.of("key1", "value1", "key2", "value2");
Catalog cachingCatalog = CachingCatalog.wrap(catalog);
try {
Table table = cachingCatalog.createTable(tableIdent, schema, spec, location, properties);
Assert.assertEquals(location, table.location());
Assert.assertEquals(2, table.schema().columns().size());
Assert.assertEquals(1, table.spec().fields().size());
Assert.assertEquals("value1", table.properties().get("key1"));
Assert.assertEquals("value2", table.properties().get("key2"));
} finally {
cachingCatalog.dropTable(tableIdent);
}
}
@Test
public void testInitialize() {
Assertions.assertDoesNotThrow(
() -> {
HiveCatalog catalog = new HiveCatalog();
catalog.initialize("hive", Maps.newHashMap());
});
}
@Test
public void testToStringWithoutSetConf() {
Assertions.assertDoesNotThrow(
() -> {
HiveCatalog catalog = new HiveCatalog();
catalog.toString();
});
}
@Test
public void testInitializeCatalogWithProperties() {
Map<String, String> properties = Maps.newHashMap();
properties.put("uri", "thrift://examplehost:9083");
properties.put("warehouse", "/user/hive/testwarehouse");
HiveCatalog catalog = new HiveCatalog();
catalog.initialize("hive", properties);
Assert.assertEquals(catalog.getConf().get("hive.metastore.uris"), "thrift://examplehost:9083");
Assert.assertEquals(
catalog.getConf().get("hive.metastore.warehouse.dir"), "/user/hive/testwarehouse");
}
@Test
public void testCreateTableTxnBuilder() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
String location = temp.newFolder("tbl").toString();
try {
Transaction txn =
catalog.buildTable(tableIdent, schema).withLocation(location).createTransaction();
txn.commitTransaction();
Table table = catalog.loadTable(tableIdent);
Assert.assertEquals(location, table.location());
Assert.assertEquals(2, table.schema().columns().size());
Assert.assertTrue(table.spec().isUnpartitioned());
} finally {
catalog.dropTable(tableIdent);
}
}
@Test
public void testReplaceTxnBuilder() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("data", 16).build();
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
String location = temp.newFolder("tbl").toString();
try {
Transaction createTxn =
catalog
.buildTable(tableIdent, schema)
.withPartitionSpec(spec)
.withLocation(location)
.withProperty("key1", "value1")
.createOrReplaceTransaction();
createTxn.commitTransaction();
Table table = catalog.loadTable(tableIdent);
Assert.assertEquals(1, table.spec().fields().size());
String newLocation = temp.newFolder("tbl-2").toString();
Transaction replaceTxn =
catalog
.buildTable(tableIdent, schema)
.withProperty("key2", "value2")
.withLocation(newLocation)
.replaceTransaction();
replaceTxn.commitTransaction();
table = catalog.loadTable(tableIdent);
Assert.assertEquals(newLocation, table.location());
Assert.assertNull(table.currentSnapshot());
PartitionSpec v1Expected =
PartitionSpec.builderFor(table.schema())
.alwaysNull("data", "data_bucket")
.withSpecId(1)
.build();
Assert.assertEquals("Table should have a spec with one void field", v1Expected, table.spec());
Assert.assertEquals("value1", table.properties().get("key1"));
Assert.assertEquals("value2", table.properties().get("key2"));
} finally {
catalog.dropTable(tableIdent);
}
}
@Test
public void testCreateTableDefaultSortOrder() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("data", 16).build();
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
try {
Table table = catalog.createTable(tableIdent, schema, spec);
Assert.assertEquals("Order ID must match", 0, table.sortOrder().orderId());
Assert.assertTrue("Order must unsorted", table.sortOrder().isUnsorted());
Assert.assertFalse(
"Must not have default sort order in catalog",
hmsTableParameters().containsKey(DEFAULT_SORT_ORDER));
} finally {
catalog.dropTable(tableIdent);
}
}
@Test
public void testCreateTableCustomSortOrder() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("data", 16).build();
SortOrder order = SortOrder.builderFor(schema).asc("id", NULLS_FIRST).build();
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
try {
Table table =
catalog
.buildTable(tableIdent, schema)
.withPartitionSpec(spec)
.withSortOrder(order)
.create();
SortOrder sortOrder = table.sortOrder();
Assert.assertEquals("Order ID must match", 1, sortOrder.orderId());
Assert.assertEquals("Order must have 1 field", 1, sortOrder.fields().size());
Assert.assertEquals("Direction must match ", ASC, sortOrder.fields().get(0).direction());
Assert.assertEquals(
"Null order must match ", NULLS_FIRST, sortOrder.fields().get(0).nullOrder());
Transform<?, ?> transform = Transforms.identity(Types.IntegerType.get());
Assert.assertEquals("Transform must match", transform, sortOrder.fields().get(0).transform());
Assert.assertEquals(
SortOrderParser.toJson(table.sortOrder()), hmsTableParameters().get(DEFAULT_SORT_ORDER));
} finally {
catalog.dropTable(tableIdent);
}
}
@Test
public void testCreateNamespace() throws Exception {
Namespace namespace1 = Namespace.of("noLocation");
catalog.createNamespace(namespace1, meta);
Database database1 = metastoreClient.getDatabase(namespace1.toString());
Assert.assertTrue(database1.getParameters().get("owner").equals("apache"));
Assert.assertTrue(database1.getParameters().get("group").equals("iceberg"));
Assert.assertEquals(
"There no same location for db and namespace",
database1.getLocationUri(),
defaultUri(namespace1));
AssertHelpers.assertThrows(
"Should fail to create when namespace already exist " + namespace1,
AlreadyExistsException.class,
"Namespace '" + namespace1 + "' already exists!",
() -> {
catalog.createNamespace(namespace1);
});
String hiveLocalDir = temp.newFolder().toURI().toString();
// remove the trailing slash of the URI
hiveLocalDir = hiveLocalDir.substring(0, hiveLocalDir.length() - 1);
ImmutableMap newMeta =
ImmutableMap.<String, String>builder().putAll(meta).put("location", hiveLocalDir).build();
Namespace namespace2 = Namespace.of("haveLocation");
catalog.createNamespace(namespace2, newMeta);
Database database2 = metastoreClient.getDatabase(namespace2.toString());
Assert.assertEquals(
"There no same location for db and namespace", database2.getLocationUri(), hiveLocalDir);
}
@Test
public void testListNamespace() throws TException {
List<Namespace> namespaces;
Namespace namespace1 = Namespace.of("dbname1");
catalog.createNamespace(namespace1, meta);
namespaces = catalog.listNamespaces(namespace1);
Assert.assertTrue("Hive db not hive the namespace 'dbname1'", namespaces.isEmpty());
Namespace namespace2 = Namespace.of("dbname2");
catalog.createNamespace(namespace2, meta);
namespaces = catalog.listNamespaces();
Assert.assertTrue("Hive db not hive the namespace 'dbname2'", namespaces.contains(namespace2));
}
@Test
public void testLoadNamespaceMeta() throws TException {
Namespace namespace = Namespace.of("dbname_load");
catalog.createNamespace(namespace, meta);
Map<String, String> nameMata = catalog.loadNamespaceMetadata(namespace);
Assert.assertTrue(nameMata.get("owner").equals("apache"));
Assert.assertTrue(nameMata.get("group").equals("iceberg"));
Assert.assertEquals(
"There no same location for db and namespace",
nameMata.get("location"),
catalog.convertToDatabase(namespace, meta).getLocationUri());
}
@Test
public void testNamespaceExists() throws TException {
Namespace namespace = Namespace.of("dbname_exists");
catalog.createNamespace(namespace, meta);
Assert.assertTrue("Should true to namespace exist", catalog.namespaceExists(namespace));
Assert.assertTrue(
"Should false to namespace doesn't exist",
!catalog.namespaceExists(Namespace.of("db2", "db2", "ns2")));
}
@Test
public void testSetNamespaceProperties() throws TException {
Namespace namespace = Namespace.of("dbname_set");
catalog.createNamespace(namespace, meta);
catalog.setProperties(
namespace,
ImmutableMap.of(
"owner", "alter_apache",
"test", "test",
"location", "file:/data/tmp",
"comment", "iceberg test"));
Database database = metastoreClient.getDatabase(namespace.level(0));
Assert.assertEquals(database.getParameters().get("owner"), "alter_apache");
Assert.assertEquals(database.getParameters().get("test"), "test");
Assert.assertEquals(database.getParameters().get("group"), "iceberg");
AssertHelpers.assertThrows(
"Should fail to namespace not exist" + namespace,
NoSuchNamespaceException.class,
"Namespace does not exist: ",
() -> {
catalog.setProperties(Namespace.of("db2", "db2", "ns2"), meta);
});
}
@Test
public void testRemoveNamespaceProperties() throws TException {
Namespace namespace = Namespace.of("dbname_remove");
catalog.createNamespace(namespace, meta);
catalog.removeProperties(namespace, ImmutableSet.of("comment", "owner"));
Database database = metastoreClient.getDatabase(namespace.level(0));
Assert.assertEquals(database.getParameters().get("owner"), null);
Assert.assertEquals(database.getParameters().get("group"), "iceberg");
AssertHelpers.assertThrows(
"Should fail to namespace not exist" + namespace,
NoSuchNamespaceException.class,
"Namespace does not exist: ",
() -> {
catalog.removeProperties(
Namespace.of("db2", "db2", "ns2"), ImmutableSet.of("comment", "owner"));
});
}
@Test
public void testDropNamespace() throws TException {
Namespace namespace = Namespace.of("dbname_drop");
TableIdentifier identifier = TableIdentifier.of(namespace, "table");
Schema schema =
new Schema(Types.StructType.of(required(1, "id", Types.LongType.get())).fields());
catalog.createNamespace(namespace, meta);
catalog.createTable(identifier, schema);
Map<String, String> nameMata = catalog.loadNamespaceMetadata(namespace);
Assert.assertTrue(nameMata.get("owner").equals("apache"));
Assert.assertTrue(nameMata.get("group").equals("iceberg"));
AssertHelpers.assertThrows(
"Should fail to drop namespace is not empty" + namespace,
NamespaceNotEmptyException.class,
"Namespace dbname_drop is not empty. One or more tables exist.",
() -> {
catalog.dropNamespace(namespace);
});
Assert.assertTrue(catalog.dropTable(identifier, true));
Assert.assertTrue(
"Should fail to drop namespace if it is not empty", catalog.dropNamespace(namespace));
Assert.assertFalse(
"Should fail to drop when namespace doesn't exist",
catalog.dropNamespace(Namespace.of("db.ns1")));
AssertHelpers.assertThrows(
"Should fail to drop namespace exist" + namespace,
NoSuchNamespaceException.class,
"Namespace does not exist: ",
() -> {
catalog.loadNamespaceMetadata(namespace);
});
}
@Test
public void testTableName() {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("data", 16).build();
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
try {
catalog.buildTable(tableIdent, schema).withPartitionSpec(spec).create();
Table table = catalog.loadTable(tableIdent);
Assert.assertEquals("Name must match", "hive.hivedb.tbl", table.name());
TableIdentifier snapshotsTableIdent = TableIdentifier.of(DB_NAME, "tbl", "snapshots");
Table snapshotsTable = catalog.loadTable(snapshotsTableIdent);
Assert.assertEquals("Name must match", "hive.hivedb.tbl.snapshots", snapshotsTable.name());
} finally {
catalog.dropTable(tableIdent);
}
}
private String defaultUri(Namespace namespace) throws TException {
return metastoreClient.getConfigValue("hive.metastore.warehouse.dir", "")
+ "/"
+ namespace.level(0)
+ ".db";
}
@Test
public void testUUIDinTableProperties() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
TableIdentifier tableIdentifier = TableIdentifier.of(DB_NAME, "tbl");
String location = temp.newFolder("tbl").toString();
try {
catalog.buildTable(tableIdentifier, schema).withLocation(location).create();
Assert.assertNotNull(hmsTableParameters().get(TableProperties.UUID));
} finally {
catalog.dropTable(tableIdentifier);
}
}
@Test
public void testSnapshotStatsTableProperties() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
TableIdentifier tableIdentifier = TableIdentifier.of(DB_NAME, "tbl");
String location = temp.newFolder("tbl").toString();
try {
catalog.buildTable(tableIdentifier, schema).withLocation(location).create();
// check whether parameters are in expected state
Map<String, String> parameters = hmsTableParameters();
Assert.assertEquals("0", parameters.get(TableProperties.SNAPSHOT_COUNT));
Assert.assertNull(parameters.get(CURRENT_SNAPSHOT_SUMMARY));
Assert.assertNull(parameters.get(CURRENT_SNAPSHOT_ID));
Assert.assertNull(parameters.get(CURRENT_SNAPSHOT_TIMESTAMP));
// create a snapshot
Table icebergTable = catalog.loadTable(tableIdentifier);
String fileName = UUID.randomUUID().toString();
DataFile file =
DataFiles.builder(icebergTable.spec())
.withPath(FileFormat.PARQUET.addExtension(fileName))
.withRecordCount(2)
.withFileSizeInBytes(0)
.build();
icebergTable.newFastAppend().appendFile(file).commit();
// check whether parameters are in expected state
parameters = hmsTableParameters();
Assert.assertEquals("1", parameters.get(TableProperties.SNAPSHOT_COUNT));
String summary =
JsonUtil.mapper().writeValueAsString(icebergTable.currentSnapshot().summary());
Assert.assertEquals(summary, parameters.get(CURRENT_SNAPSHOT_SUMMARY));
long snapshotId = icebergTable.currentSnapshot().snapshotId();
Assert.assertEquals(String.valueOf(snapshotId), parameters.get(CURRENT_SNAPSHOT_ID));
Assert.assertEquals(
String.valueOf(icebergTable.currentSnapshot().timestampMillis()),
parameters.get(CURRENT_SNAPSHOT_TIMESTAMP));
} finally {
catalog.dropTable(tableIdentifier);
}
}
@Test
public void testSetSnapshotSummary() throws Exception {
Configuration conf = new Configuration();
conf.set("iceberg.hive.table-property-max-size", "4000");
HiveTableOperations ops =
new HiveTableOperations(conf, null, null, catalog.name(), DB_NAME, "tbl");
Snapshot snapshot = mock(Snapshot.class);
Map<String, String> summary = Maps.newHashMap();
when(snapshot.summary()).thenReturn(summary);
// create a snapshot summary whose json string size is less than the limit
for (int i = 0; i < 100; i++) {
summary.put(String.valueOf(i), "value");
}
Assert.assertTrue(JsonUtil.mapper().writeValueAsString(summary).length() < 4000);
Map<String, String> parameters = Maps.newHashMap();
ops.setSnapshotSummary(parameters, snapshot);
Assert.assertEquals("The snapshot summary must be in parameters", 1, parameters.size());
// create a snapshot summary whose json string size exceeds the limit
for (int i = 0; i < 1000; i++) {
summary.put(String.valueOf(i), "value");
}
long summarySize = JsonUtil.mapper().writeValueAsString(summary).length();
// the limit has been updated to 4000 instead of the default value(32672)
Assert.assertTrue(summarySize > 4000 && summarySize < 32672);
parameters.remove(CURRENT_SNAPSHOT_SUMMARY);
ops.setSnapshotSummary(parameters, snapshot);
Assert.assertEquals(
"The snapshot summary must not be in parameters due to the size limit",
0,
parameters.size());
}
@Test
public void testNotExposeTableProperties() {
Configuration conf = new Configuration();
conf.set("iceberg.hive.table-property-max-size", "0");
HiveTableOperations ops =
new HiveTableOperations(conf, null, null, catalog.name(), DB_NAME, "tbl");
TableMetadata metadata = mock(TableMetadata.class);
Map<String, String> parameters = Maps.newHashMap();
parameters.put(CURRENT_SNAPSHOT_SUMMARY, "summary");
parameters.put(CURRENT_SNAPSHOT_ID, "snapshotId");
parameters.put(CURRENT_SNAPSHOT_TIMESTAMP, "timestamp");
parameters.put(CURRENT_SCHEMA, "schema");
parameters.put(DEFAULT_PARTITION_SPEC, "partitionSpec");
parameters.put(DEFAULT_SORT_ORDER, "sortOrder");
ops.setSnapshotStats(metadata, parameters);
Assert.assertNull(parameters.get(CURRENT_SNAPSHOT_SUMMARY));
Assert.assertNull(parameters.get(CURRENT_SNAPSHOT_ID));
Assert.assertNull(parameters.get(CURRENT_SNAPSHOT_TIMESTAMP));
ops.setSchema(metadata, parameters);
Assert.assertNull(parameters.get(CURRENT_SCHEMA));
ops.setPartitionSpec(metadata, parameters);
Assert.assertNull(parameters.get(DEFAULT_PARTITION_SPEC));
ops.setSortOrder(metadata, parameters);
Assert.assertNull(parameters.get(DEFAULT_SORT_ORDER));
}
@Test
public void testSetDefaultPartitionSpec() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
try {
Table table = catalog.buildTable(tableIdent, schema).create();
Assert.assertFalse(
"Must not have default partition spec",
hmsTableParameters().containsKey(TableProperties.DEFAULT_PARTITION_SPEC));
table.updateSpec().addField(bucket("data", 16)).commit();
Assert.assertEquals(
PartitionSpecParser.toJson(table.spec()),
hmsTableParameters().get(TableProperties.DEFAULT_PARTITION_SPEC));
} finally {
catalog.dropTable(tableIdent);
}
}
@Test
public void testSetCurrentSchema() throws Exception {
Schema schema =
new Schema(
required(1, "id", Types.IntegerType.get(), "unique ID"),
required(2, "data", Types.StringType.get()));
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
try {
Table table = catalog.buildTable(tableIdent, schema).create();
Assert.assertEquals(
SchemaParser.toJson(table.schema()), hmsTableParameters().get(CURRENT_SCHEMA));
// add many new fields to make the schema json string exceed the limit
UpdateSchema updateSchema = table.updateSchema();
for (int i = 0; i < 600; i++) {
updateSchema.addColumn("new_col_" + i, Types.StringType.get());
}
updateSchema.commit();
Assert.assertTrue(SchemaParser.toJson(table.schema()).length() > 32672);
Assert.assertNull(hmsTableParameters().get(CURRENT_SCHEMA));
} finally {
catalog.dropTable(tableIdent);
}
}
private Map<String, String> hmsTableParameters() throws TException {
org.apache.hadoop.hive.metastore.api.Table hmsTable = metastoreClient.getTable(DB_NAME, "tbl");
return hmsTable.getParameters();
}
@Test
public void testConstructorWarehousePathWithEndSlash() {
HiveCatalog catalogWithSlash = new HiveCatalog();
String wareHousePath = "s3://bucket/db/tbl";
catalogWithSlash.initialize(
"hive_catalog", ImmutableMap.of(CatalogProperties.WAREHOUSE_LOCATION, wareHousePath + "/"));
Assert.assertEquals(
"Should have trailing slash stripped",
wareHousePath,
catalogWithSlash.getConf().get(HiveConf.ConfVars.METASTOREWAREHOUSE.varname));
}
@Test
public void testTablePropsDefinedAtCatalogLevel() {
Schema schema = new Schema(required(1, "id", Types.IntegerType.get(), "unique ID"));
TableIdentifier tableIdent = TableIdentifier.of(DB_NAME, "tbl");
ImmutableMap<String, String> catalogProps =
ImmutableMap.of(
"table-default.key1", "catalog-default-key1",
"table-default.key2", "catalog-default-key2",
"table-default.key3", "catalog-default-key3",
"table-override.key3", "catalog-override-key3",
"table-override.key4", "catalog-override-key4");
Catalog hiveCatalog =
CatalogUtil.loadCatalog(
HiveCatalog.class.getName(),
CatalogUtil.ICEBERG_CATALOG_TYPE_HIVE,
catalogProps,
hiveConf);
try {
Table table =
hiveCatalog
.buildTable(tableIdent, schema)
.withProperty("key2", "table-key2")
.withProperty("key3", "table-key3")
.withProperty("key5", "table-key5")
.create();
Assert.assertEquals(
"Table defaults set for the catalog must be added to the table properties.",
"catalog-default-key1",
table.properties().get("key1"));
Assert.assertEquals(
"Table property must override table default properties set at catalog level.",
"table-key2",
table.properties().get("key2"));
Assert.assertEquals(
"Table property override set at catalog level must override table default"
+ " properties set at catalog level and table property specified.",
"catalog-override-key3",
table.properties().get("key3"));
Assert.assertEquals(
"Table override not in table props or defaults should be added to table properties",
"catalog-override-key4",
table.properties().get("key4"));
Assert.assertEquals(
"Table properties without any catalog level default or override should be added to table"
+ " properties.",
"table-key5",
table.properties().get("key5"));
} finally {
hiveCatalog.dropTable(tableIdent);
}
}
@Test
public void testDatabaseLocationWithSlashInWarehouseDir() {
Configuration conf = new Configuration();
// With a trailing slash
conf.set("hive.metastore.warehouse.dir", "s3://bucket/");
HiveCatalog catalog = new HiveCatalog();
catalog.setConf(conf);
Database database = catalog.convertToDatabase(Namespace.of("database"), ImmutableMap.of());
Assert.assertEquals("s3://bucket/database.db", database.getLocationUri());
}
}