/
FileHiveMetastore.java
1043 lines (901 loc) · 44 KB
/
FileHiveMetastore.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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed 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 com.facebook.presto.hive.metastore.file;
import com.facebook.presto.hive.HdfsConfiguration;
import com.facebook.presto.hive.HdfsConfigurationUpdater;
import com.facebook.presto.hive.HdfsEnvironment;
import com.facebook.presto.hive.HdfsEnvironment.HdfsContext;
import com.facebook.presto.hive.HiveClientConfig;
import com.facebook.presto.hive.HiveHdfsConfiguration;
import com.facebook.presto.hive.HiveType;
import com.facebook.presto.hive.PartitionNotFoundException;
import com.facebook.presto.hive.SchemaAlreadyExistsException;
import com.facebook.presto.hive.TableAlreadyExistsException;
import com.facebook.presto.hive.authentication.NoHdfsAuthentication;
import com.facebook.presto.hive.metastore.Column;
import com.facebook.presto.hive.metastore.Database;
import com.facebook.presto.hive.metastore.ExtendedHiveMetastore;
import com.facebook.presto.hive.metastore.HiveColumnStatistics;
import com.facebook.presto.hive.metastore.HivePrivilegeInfo;
import com.facebook.presto.hive.metastore.Partition;
import com.facebook.presto.hive.metastore.PrincipalPrivileges;
import com.facebook.presto.hive.metastore.PrincipalType;
import com.facebook.presto.hive.metastore.Table;
import com.facebook.presto.spi.ColumnNotFoundException;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.SchemaNotFoundException;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.TableNotFoundException;
import com.facebook.presto.spi.security.Identity;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import io.airlift.json.JsonCodec;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.metastore.TableType;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_METASTORE_ERROR;
import static com.facebook.presto.hive.HiveUtil.toPartitionValues;
import static com.facebook.presto.hive.metastore.Database.DEFAULT_DATABASE_NAME;
import static com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP;
import static com.facebook.presto.hive.metastore.MetastoreUtil.makePartName;
import static com.facebook.presto.hive.metastore.MetastoreUtil.verifyCanDropColumn;
import static com.facebook.presto.hive.metastore.PrincipalType.ROLE;
import static com.facebook.presto.hive.metastore.PrincipalType.USER;
import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static org.apache.hadoop.hive.common.FileUtils.unescapePathName;
import static org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE;
import static org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE;
import static org.apache.hadoop.hive.metastore.TableType.VIRTUAL_VIEW;
@ThreadSafe
public class FileHiveMetastore
implements ExtendedHiveMetastore
{
private static final String PUBLIC_ROLE_NAME = "public";
private static final String PRESTO_SCHEMA_FILE_NAME = ".prestoSchema";
private static final String PRESTO_PERMISSIONS_DIRECTORY_NAME = ".prestoPermissions";
private final HdfsEnvironment hdfsEnvironment;
private final Path catalogDirectory;
private final HdfsContext hdfsContext;
private final FileSystem metadataFileSystem;
private final JsonCodec<DatabaseMetadata> databaseCodec = JsonCodec.jsonCodec(DatabaseMetadata.class);
private final JsonCodec<TableMetadata> tableCodec = JsonCodec.jsonCodec(TableMetadata.class);
private final JsonCodec<PartitionMetadata> partitionCodec = JsonCodec.jsonCodec(PartitionMetadata.class);
private final JsonCodec<List<PermissionMetadata>> permissionsCodec = JsonCodec.listJsonCodec(PermissionMetadata.class);
public static FileHiveMetastore createTestingFileHiveMetastore(File catalogDirectory)
{
HiveClientConfig hiveClientConfig = new HiveClientConfig();
HdfsConfiguration hdfsConfiguration = new HiveHdfsConfiguration(new HdfsConfigurationUpdater(hiveClientConfig));
HdfsEnvironment hdfsEnvironment = new HdfsEnvironment(hdfsConfiguration, hiveClientConfig, new NoHdfsAuthentication());
return new FileHiveMetastore(hdfsEnvironment, catalogDirectory.toURI().toString(), "test");
}
@Inject
public FileHiveMetastore(HdfsEnvironment hdfsEnvironment, FileHiveMetastoreConfig config)
{
this(hdfsEnvironment, config.getCatalogDirectory(), config.getMetastoreUser());
}
public FileHiveMetastore(HdfsEnvironment hdfsEnvironment, String catalogDirectory, String metastoreUser)
{
this.hdfsEnvironment = requireNonNull(hdfsEnvironment, "hdfsEnvironment is null");
this.catalogDirectory = new Path(requireNonNull(catalogDirectory, "baseDirectory is null"));
this.hdfsContext = new HdfsContext(new Identity(metastoreUser, Optional.empty()));
try {
metadataFileSystem = hdfsEnvironment.getFileSystem(hdfsContext, this.catalogDirectory);
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
@Override
public synchronized void createDatabase(Database database)
{
requireNonNull(database, "database is null");
if (database.getLocation().isPresent()) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Database can not be created with a location set");
}
verifyDatabaseNotExists(database.getDatabaseName());
Path databaseMetadataDirectory = getDatabaseMetadataDirectory(database.getDatabaseName());
writeSchemaFile("database", databaseMetadataDirectory, databaseCodec, new DatabaseMetadata(database), false);
}
@Override
public synchronized void dropDatabase(String databaseName)
{
requireNonNull(databaseName, "databaseName is null");
getRequiredDatabase(databaseName);
if (!getAllTables(databaseName).orElse(ImmutableList.of()).isEmpty()) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Database " + databaseName + " is not empty");
}
deleteMetadataDirectory(getDatabaseMetadataDirectory(databaseName));
}
@Override
public synchronized void renameDatabase(String databaseName, String newDatabaseName)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(newDatabaseName, "newDatabaseName is null");
getRequiredDatabase(databaseName);
verifyDatabaseNotExists(newDatabaseName);
try {
if (!metadataFileSystem.rename(getDatabaseMetadataDirectory(databaseName), getDatabaseMetadataDirectory(newDatabaseName))) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not rename database metadata directory");
}
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
@Override
public synchronized Optional<Database> getDatabase(String databaseName)
{
requireNonNull(databaseName, "databaseName is null");
Path databaseMetadataDirectory = getDatabaseMetadataDirectory(databaseName);
return readSchemaFile("database", databaseMetadataDirectory, databaseCodec)
.map(databaseMetadata -> databaseMetadata.toDatabase(databaseName, databaseMetadataDirectory.toString()));
}
private Database getRequiredDatabase(String databaseName)
{
return getDatabase(databaseName)
.orElseThrow(() -> new SchemaNotFoundException(databaseName));
}
private void verifyDatabaseNotExists(String databaseName)
{
if (getDatabase(databaseName).isPresent()) {
throw new SchemaAlreadyExistsException(databaseName);
}
}
@Override
public synchronized List<String> getAllDatabases()
{
List<String> databases = getChildSchemaDirectories(catalogDirectory).stream()
.map(Path::getName)
.collect(toList());
return ImmutableList.copyOf(databases);
}
@Override
public synchronized void createTable(Table table, PrincipalPrivileges principalPrivileges)
{
verifyTableNotExists(table.getDatabaseName(), table.getTableName());
Path tableMetadataDirectory = getTableMetadataDirectory(table);
// validate table location
if (table.getTableType().equals(VIRTUAL_VIEW.name())) {
checkArgument(table.getStorage().getLocation().isEmpty(), "Storage location for view must be empty");
}
else if (table.getTableType().equals(MANAGED_TABLE.name())) {
if (!tableMetadataDirectory.equals(new Path(table.getStorage().getLocation()))) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Table directory must be " + tableMetadataDirectory);
}
}
else if (table.getTableType().equals(EXTERNAL_TABLE.name())) {
try {
Path externalLocation = new Path(table.getStorage().getLocation());
FileSystem externalFileSystem = hdfsEnvironment.getFileSystem(hdfsContext, externalLocation);
if (!externalFileSystem.isDirectory(externalLocation)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "External table location does not exist");
}
if (isChildDirectory(catalogDirectory, externalLocation)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "External table location can not be inside the system metadata directory");
}
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not validate external location", e);
}
}
else {
throw new PrestoException(NOT_SUPPORTED, "Table type not supported: " + table.getTableType());
}
writeSchemaFile("table", tableMetadataDirectory, tableCodec, new TableMetadata(table), false);
for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getUserPrivileges().asMap().entrySet()) {
setTablePrivileges(entry.getKey(), USER, table.getDatabaseName(), table.getTableName(), entry.getValue());
}
for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getRolePrivileges().asMap().entrySet()) {
setTablePrivileges(entry.getKey(), ROLE, table.getDatabaseName(), table.getTableName(), entry.getValue());
}
}
@Override
public synchronized Optional<Table> getTable(String databaseName, String tableName)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName);
return readSchemaFile("table", tableMetadataDirectory, tableCodec)
.map(tableMetadata -> tableMetadata.toTable(databaseName, tableName, tableMetadataDirectory.toString()));
}
@Override
public Map<String, HiveColumnStatistics> getTableColumnStatistics(String databaseName, String tableName)
{
return ImmutableMap.of();
}
@Override
public Map<String, Map<String, HiveColumnStatistics>> getPartitionColumnStatistics(String databaseName, String tableName, Set<String> partitionNames)
{
return ImmutableMap.of();
}
private Table getRequiredTable(String databaseName, String tableName)
{
return getTable(databaseName, tableName)
.orElseThrow(() -> new TableNotFoundException(new SchemaTableName(databaseName, tableName)));
}
private void verifyTableNotExists(String newDatabaseName, String newTableName)
{
if (getTable(newDatabaseName, newTableName).isPresent()) {
throw new TableAlreadyExistsException(new SchemaTableName(newDatabaseName, newTableName));
}
}
@Override
public synchronized Optional<List<String>> getAllTables(String databaseName)
{
requireNonNull(databaseName, "databaseName is null");
Optional<Database> database = getDatabase(databaseName);
if (!database.isPresent()) {
return Optional.empty();
}
Path databaseMetadataDirectory = getDatabaseMetadataDirectory(databaseName);
List<String> tables = getChildSchemaDirectories(databaseMetadataDirectory).stream()
.map(Path::getName)
.collect(toList());
return Optional.of(ImmutableList.copyOf(tables));
}
@Override
public synchronized Optional<List<String>> getAllViews(String databaseName)
{
Optional<List<String>> tables = getAllTables(databaseName);
if (!tables.isPresent()) {
return Optional.empty();
}
List<String> views = tables.get().stream()
.map(tableName -> getTable(databaseName, tableName))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(table -> table.getTableType().equals(VIRTUAL_VIEW.name()))
.map(Table::getTableName)
.collect(toList());
return Optional.of(ImmutableList.copyOf(views));
}
@Override
public synchronized void dropTable(String databaseName, String tableName, boolean deleteData)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
Table table = getRequiredTable(databaseName, tableName);
Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName);
// It is safe to delete the whole meta directory for external tables and views
if (!table.getTableType().equals(MANAGED_TABLE.name()) || deleteData) {
deleteMetadataDirectory(tableMetadataDirectory);
}
else {
// in this case we only wan to delete the metadata of a managed table
deleteSchemaFile("table", tableMetadataDirectory);
deleteTablePrivileges(table);
}
}
@Override
public synchronized void replaceTable(String databaseName, String tableName, Table newTable, PrincipalPrivileges principalPrivileges)
{
Table table = getRequiredTable(databaseName, tableName);
if (!table.getTableType().equals(VIRTUAL_VIEW.name()) || !newTable.getTableType().equals(VIRTUAL_VIEW.name())) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Only views can be updated with replaceTable");
}
if (!table.getDatabaseName().equals(databaseName) || !table.getTableName().equals(tableName)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Replacement table must have same name");
}
Path tableMetadataDirectory = getTableMetadataDirectory(table);
writeSchemaFile("table", tableMetadataDirectory, tableCodec, new TableMetadata(newTable), true);
// replace existing permissions
deleteTablePrivileges(table);
for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getUserPrivileges().asMap().entrySet()) {
setTablePrivileges(entry.getKey(), USER, table.getDatabaseName(), table.getTableName(), entry.getValue());
}
for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getRolePrivileges().asMap().entrySet()) {
setTablePrivileges(entry.getKey(), ROLE, table.getDatabaseName(), table.getTableName(), entry.getValue());
}
}
@Override
public synchronized void renameTable(String databaseName, String tableName, String newDatabaseName, String newTableName)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(newDatabaseName, "newDatabaseName is null");
requireNonNull(newTableName, "newTableName is null");
getRequiredTable(databaseName, tableName);
getRequiredDatabase(newDatabaseName);
// verify new table does not exist
verifyTableNotExists(newDatabaseName, newTableName);
try {
if (!metadataFileSystem.rename(getTableMetadataDirectory(databaseName, tableName), getTableMetadataDirectory(newDatabaseName, newTableName))) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not rename table directory");
}
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
@Override
public synchronized void updateTableParameters(String databaseName, String tableName, Function<Map<String, String>, Map<String, String>> update)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(update, "update is null");
Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName);
TableMetadata table = readSchemaFile("table", tableMetadataDirectory, tableCodec)
.orElseThrow(() -> new TableNotFoundException(new SchemaTableName(databaseName, tableName)));
Map<String, String> parameters = table.getParameters();
Map<String, String> updatedParameters = requireNonNull(update.apply(parameters), "updatedParameters is null");
if (!parameters.equals(updatedParameters)) {
writeSchemaFile("table", tableMetadataDirectory, tableCodec, table.withParameters(updatedParameters), true);
}
}
@Override
public synchronized void addColumn(String databaseName, String tableName, String columnName, HiveType columnType, String columnComment)
{
alterTable(databaseName, tableName, oldTable -> {
if (oldTable.getColumn(columnName).isPresent()) {
throw new PrestoException(ALREADY_EXISTS, "Column already exists: " + columnName);
}
return oldTable.withDataColumns(ImmutableList.<Column>builder()
.addAll(oldTable.getDataColumns())
.add(new Column(columnName, columnType, Optional.ofNullable(columnComment)))
.build());
});
}
@Override
public synchronized void renameColumn(String databaseName, String tableName, String oldColumnName, String newColumnName)
{
alterTable(databaseName, tableName, oldTable -> {
if (oldTable.getColumn(newColumnName).isPresent()) {
throw new PrestoException(ALREADY_EXISTS, "Column already exists: " + newColumnName);
}
if (!oldTable.getColumn(oldColumnName).isPresent()) {
SchemaTableName name = new SchemaTableName(databaseName, tableName);
throw new ColumnNotFoundException(name, oldColumnName);
}
for (Column column : oldTable.getPartitionColumns()) {
if (column.getName().equals(oldColumnName)) {
throw new PrestoException(NOT_SUPPORTED, "Renaming partition columns is not supported");
}
}
ImmutableList.Builder<Column> newDataColumns = ImmutableList.builder();
for (Column fieldSchema : oldTable.getDataColumns()) {
if (fieldSchema.getName().equals(oldColumnName)) {
newDataColumns.add(new Column(newColumnName, fieldSchema.getType(), fieldSchema.getComment()));
}
else {
newDataColumns.add(fieldSchema);
}
}
return oldTable.withDataColumns(newDataColumns.build());
});
}
@Override
public synchronized void dropColumn(String databaseName, String tableName, String columnName)
{
alterTable(databaseName, tableName, oldTable -> {
verifyCanDropColumn(this, databaseName, tableName, columnName);
if (!oldTable.getColumn(columnName).isPresent()) {
SchemaTableName name = new SchemaTableName(databaseName, tableName);
throw new ColumnNotFoundException(name, columnName);
}
ImmutableList.Builder<Column> newDataColumns = ImmutableList.builder();
for (Column fieldSchema : oldTable.getDataColumns()) {
if (!fieldSchema.getName().equals(columnName)) {
newDataColumns.add(fieldSchema);
}
}
return oldTable.withDataColumns(newDataColumns.build());
});
}
private void alterTable(String databaseName, String tableName, Function<TableMetadata, TableMetadata> alterFunction)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName);
TableMetadata oldTableSchema = readSchemaFile("table", tableMetadataDirectory, tableCodec)
.orElseThrow(() -> new TableNotFoundException(new SchemaTableName(databaseName, tableName)));
TableMetadata newTableSchema = alterFunction.apply(oldTableSchema);
if (oldTableSchema == newTableSchema) {
return;
}
writeSchemaFile("table", tableMetadataDirectory, tableCodec, newTableSchema, true);
}
@Override
public synchronized void addPartitions(String databaseName, String tableName, List<Partition> partitions)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(partitions, "partitions is null");
Table table = getRequiredTable(databaseName, tableName);
TableType tableType = TableType.valueOf(table.getTableType());
checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE).contains(tableType), "Invalid table type: %s", tableType);
try {
Map<Path, byte[]> schemaFiles = new LinkedHashMap<>();
for (Partition partition : partitions) {
verifiedPartition(table, partition);
Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
Path schemaPath = new Path(partitionMetadataDirectory, PRESTO_SCHEMA_FILE_NAME);
if (metadataFileSystem.exists(schemaPath)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Partition already exists");
}
byte[] schemaJson = partitionCodec.toJsonBytes(new PartitionMetadata(table, partition));
schemaFiles.put(schemaPath, schemaJson);
}
Set<Path> createdFiles = new LinkedHashSet<>();
try {
for (Entry<Path, byte[]> entry : schemaFiles.entrySet()) {
try (OutputStream outputStream = metadataFileSystem.create(entry.getKey())) {
createdFiles.add(entry.getKey());
outputStream.write(entry.getValue());
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not write partition schema", e);
}
}
}
catch (Throwable e) {
for (Path createdFile : createdFiles) {
try {
metadataFileSystem.delete(createdFile, false);
}
catch (IOException ignored) {
}
}
throw e;
}
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
private void verifiedPartition(Table table, Partition partition)
{
Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
if (table.getTableType().equals(MANAGED_TABLE.name())) {
if (!partitionMetadataDirectory.equals(new Path(partition.getStorage().getLocation()))) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Partition directory must be " + partitionMetadataDirectory);
}
}
else if (table.getTableType().equals(EXTERNAL_TABLE.name())) {
try {
Path externalLocation = new Path(partition.getStorage().getLocation());
FileSystem externalFileSystem = hdfsEnvironment.getFileSystem(hdfsContext, externalLocation);
if (!externalFileSystem.isDirectory(externalLocation)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "External partition location does not exist");
}
if (isChildDirectory(catalogDirectory, externalLocation)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "External partition location can not be inside the system metadata directory");
}
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not validate external partition location", e);
}
}
else {
throw new PrestoException(NOT_SUPPORTED, "Partitions can not be added to " + table.getTableType());
}
}
@Override
public synchronized void dropPartition(String databaseName, String tableName, List<String> partitionValues, boolean deleteData)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(partitionValues, "partitionValues is null");
Optional<Table> tableReference = getTable(databaseName, tableName);
if (!tableReference.isPresent()) {
return;
}
Table table = tableReference.get();
Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partitionValues);
if (deleteData) {
deleteMetadataDirectory(partitionMetadataDirectory);
}
else {
deleteSchemaFile("partition", partitionMetadataDirectory);
}
}
@Override
public synchronized void alterPartition(String databaseName, String tableName, Partition partition)
{
Table table = getRequiredTable(databaseName, tableName);
verifiedPartition(table, partition);
Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
writeSchemaFile("partition", partitionMetadataDirectory, partitionCodec, new PartitionMetadata(table, partition), true);
}
@Override
public synchronized void updatePartitionParameters(String databaseName, String tableName, List<String> partitionValues, Function<Map<String, String>, Map<String, String>> update)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(partitionValues, "partitionValues is null");
requireNonNull(update, "update is null");
Table table = getRequiredTable(databaseName, tableName);
Path partitionDirectory = getPartitionMetadataDirectory(table, partitionValues);
PartitionMetadata partition = readSchemaFile("partition", partitionDirectory, partitionCodec)
.orElseThrow(() -> new PartitionNotFoundException(new SchemaTableName(databaseName, tableName), partitionValues));
Map<String, String> parameters = partition.getParameters();
Map<String, String> updatedParameters = requireNonNull(update.apply(parameters), "updatedParameters is null");
if (!parameters.equals(updatedParameters)) {
writeSchemaFile("partition", partitionDirectory, partitionCodec, partition.withParameters(updatedParameters), true);
}
}
@Override
public synchronized Optional<List<String>> getPartitionNames(String databaseName, String tableName)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
Optional<Table> tableReference = getTable(databaseName, tableName);
if (!tableReference.isPresent()) {
return Optional.empty();
}
Table table = tableReference.get();
Path tableMetadataDirectory = getTableMetadataDirectory(table);
List<ArrayDeque<String>> partitions = listPartitions(tableMetadataDirectory, table.getPartitionColumns());
List<String> partitionNames = partitions.stream()
.map(partitionValues -> makePartName(table.getPartitionColumns(), ImmutableList.copyOf(partitionValues)))
.collect(toList());
return Optional.of(ImmutableList.copyOf(partitionNames));
}
private List<ArrayDeque<String>> listPartitions(Path director, List<Column> partitionColumns)
{
if (partitionColumns.isEmpty()) {
return ImmutableList.of();
}
try {
String directoryPrefix = partitionColumns.get(0).getName() + '=';
List<ArrayDeque<String>> partitionValues = new ArrayList<>();
for (FileStatus fileStatus : metadataFileSystem.listStatus(director)) {
if (!fileStatus.isDirectory()) {
continue;
}
if (!fileStatus.getPath().getName().startsWith(directoryPrefix)) {
continue;
}
List<ArrayDeque<String>> childPartitionValues;
if (partitionColumns.size() == 1) {
childPartitionValues = ImmutableList.of(new ArrayDeque<>());
}
else {
childPartitionValues = listPartitions(fileStatus.getPath(), partitionColumns.subList(1, partitionColumns.size()));
}
String value = unescapePathName(fileStatus.getPath().getName().substring(directoryPrefix.length()));
for (ArrayDeque<String> childPartition : childPartitionValues) {
childPartition.addFirst(value);
partitionValues.add(childPartition);
}
}
return partitionValues;
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Error listing partition directories", e);
}
}
@Override
public synchronized Optional<Partition> getPartition(String databaseName, String tableName, List<String> partitionValues)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(partitionValues, "partitionValues is null");
Optional<Table> tableReference = getTable(databaseName, tableName);
if (!tableReference.isPresent()) {
return Optional.empty();
}
Table table = tableReference.get();
Path partitionDirectory = getPartitionMetadataDirectory(table, partitionValues);
return readSchemaFile("partition", partitionDirectory, partitionCodec)
.map(partitionMetadata -> partitionMetadata.toPartition(databaseName, tableName, partitionValues, partitionDirectory.toString()));
}
@Override
public synchronized Optional<List<String>> getPartitionNamesByParts(String databaseName, String tableName, List<String> parts)
{
// todo this should be more efficient by selectively walking the directory tree
return getPartitionNames(databaseName, tableName).map(partitionNames -> partitionNames.stream()
.filter(partitionName -> partitionMatches(partitionName, parts))
.collect(toList()));
}
private static boolean partitionMatches(String partitionName, List<String> parts)
{
List<String> values = toPartitionValues(partitionName);
if (values.size() != parts.size()) {
return false;
}
for (int i = 0; i < values.size(); i++) {
String part = parts.get(i);
if (!part.isEmpty() && !values.get(i).equals(part)) {
return false;
}
}
return true;
}
@Override
public synchronized Map<String, Optional<Partition>> getPartitionsByNames(String databaseName, String tableName, List<String> partitionNames)
{
ImmutableMap.Builder<String, Optional<Partition>> builder = ImmutableMap.builder();
for (String partitionName : partitionNames) {
List<String> partitionValues = toPartitionValues(partitionName);
builder.put(partitionName, getPartition(databaseName, tableName, partitionValues));
}
return builder.build();
}
@Override
public synchronized Set<String> getRoles(String user)
{
return ImmutableSet.<String>builder()
.add(PUBLIC_ROLE_NAME)
.add("admin") // todo there should be a way to manage the admins list
.build();
}
@Override
public synchronized Set<HivePrivilegeInfo> getDatabasePrivileges(String user, String databaseName)
{
Set<HivePrivilegeInfo> privileges = new HashSet<>();
if (isDatabaseOwner(user, databaseName)) {
privileges.add(new HivePrivilegeInfo(OWNERSHIP, true));
}
return privileges;
}
@Override
public synchronized Set<HivePrivilegeInfo> getTablePrivileges(String user, String databaseName, String tableName)
{
Table table = getRequiredTable(databaseName, tableName);
Set<HivePrivilegeInfo> privileges = new HashSet<>();
if (user.equals(table.getOwner())) {
privileges.add(new HivePrivilegeInfo(OWNERSHIP, true));
}
Path permissionsDirectory = getPermissionsDirectory(table);
privileges.addAll(getTablePrivileges(permissionsDirectory, user, USER));
for (String role : getRoles(user)) {
privileges.addAll(getTablePrivileges(permissionsDirectory, role, ROLE));
}
return privileges;
}
private synchronized Collection<HivePrivilegeInfo> getTablePrivileges(
Path permissionsDirectory,
String principalName,
PrincipalType principalType)
{
Path permissionFilePath = getPermissionsPath(permissionsDirectory, principalName, principalType);
return readFile("permissions", permissionFilePath, permissionsCodec).orElse(ImmutableList.of()).stream()
.map(PermissionMetadata::toHivePrivilegeInfo)
.collect(toList());
}
@Override
public synchronized void grantTablePrivileges(String databaseName, String tableName, String grantee, Set<HivePrivilegeInfo> privileges)
{
setTablePrivileges(grantee, USER, databaseName, tableName, privileges);
}
@Override
public synchronized void revokeTablePrivileges(String databaseName, String tableName, String grantee, Set<HivePrivilegeInfo> privileges)
{
Set<HivePrivilegeInfo> currentPrivileges = getTablePrivileges(grantee, databaseName, tableName);
currentPrivileges.removeAll(privileges);
setTablePrivileges(grantee, USER, databaseName, tableName, currentPrivileges);
}
private synchronized void setTablePrivileges(
String principalName,
PrincipalType principalType,
String databaseName,
String tableName,
Collection<HivePrivilegeInfo> privileges)
{
requireNonNull(principalName, "principalName is null");
requireNonNull(principalType, "principalType is null");
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(privileges, "privileges is null");
try {
Table table = getRequiredTable(databaseName, tableName);
Path permissionsDirectory = getPermissionsDirectory(table);
metadataFileSystem.mkdirs(permissionsDirectory);
if (!metadataFileSystem.isDirectory(permissionsDirectory)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not create permissions directory");
}
Path permissionFilePath = getPermissionsPath(permissionsDirectory, principalName, principalType);
List<PermissionMetadata> permissions = privileges.stream()
.map(PermissionMetadata::new)
.collect(toList());
writeFile("permissions", permissionFilePath, permissionsCodec, permissions, true);
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
private synchronized void deleteTablePrivileges(Table table)
{
try {
Path permissionsDirectory = getPermissionsDirectory(table);
metadataFileSystem.delete(permissionsDirectory, true);
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not delete table permissions", e);
}
}
private boolean isDatabaseOwner(String user, String databaseName)
{
// all users are "owners" of the default database
if (DEFAULT_DATABASE_NAME.equalsIgnoreCase(databaseName)) {
return true;
}
Optional<Database> databaseMetadata = getDatabase(databaseName);
if (!databaseMetadata.isPresent()) {
return false;
}
Database database = databaseMetadata.get();
// a database can be owned by a user or role
if (database.getOwnerType() == USER && user.equals(database.getOwnerName())) {
return true;
}
if (database.getOwnerType() == ROLE && getRoles(user).contains(database.getOwnerName())) {
return true;
}
return false;
}
private Path getDatabaseMetadataDirectory(String databaseName)
{
return new Path(catalogDirectory, databaseName);
}
private Path getTableMetadataDirectory(Table table)
{
return getTableMetadataDirectory(table.getDatabaseName(), table.getTableName());
}
private Path getTableMetadataDirectory(String databaseName, String tableName)
{
return new Path(getDatabaseMetadataDirectory(databaseName), tableName);
}
private Path getPartitionMetadataDirectory(Table table, List<String> values)
{
String partitionName = makePartName(table.getPartitionColumns(), values);
return getPartitionMetadataDirectory(table, partitionName);
}
private Path getPartitionMetadataDirectory(Table table, String partitionName)
{
Path tableMetadataDirectory = getTableMetadataDirectory(table);
return new Path(tableMetadataDirectory, partitionName);
}
private Path getPermissionsDirectory(Table table)
{
return new Path(getTableMetadataDirectory(table), PRESTO_PERMISSIONS_DIRECTORY_NAME);
}
private static Path getPermissionsPath(Path permissionsDirectory, String principalName, PrincipalType principalType)
{
return new Path(permissionsDirectory, principalType.name().toLowerCase(Locale.US) + "_" + principalName);
}
private List<Path> getChildSchemaDirectories(Path metadataDirectory)
{
try {
if (!metadataFileSystem.isDirectory(metadataDirectory)) {
return ImmutableList.of();
}
ImmutableList.Builder<Path> childSchemaDirectories = ImmutableList.builder();
for (FileStatus child : metadataFileSystem.listStatus(metadataDirectory)) {
if (!child.isDirectory()) {
continue;
}
Path childPath = child.getPath();
if (childPath.getName().startsWith(".")) {
continue;
}
if (metadataFileSystem.isFile(new Path(childPath, PRESTO_SCHEMA_FILE_NAME))) {
childSchemaDirectories.add(childPath);
}
}
return childSchemaDirectories.build();
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
private void deleteMetadataDirectory(Path metadataDirectory)
{
try {
Path schemaPath = new Path(metadataDirectory, PRESTO_SCHEMA_FILE_NAME);
if (!metadataFileSystem.isFile(schemaPath)) {
// if there is no schema file, assume this is not a database, partition or table
return;
}
if (!metadataFileSystem.delete(metadataDirectory, true)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not delete metadata directory");
}
}
catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
private <T> Optional<T> readSchemaFile(String type, Path metadataDirectory, JsonCodec<T> codec)
{
Path schemaPath = new Path(metadataDirectory, PRESTO_SCHEMA_FILE_NAME);
return readFile(type + " schema", schemaPath, codec);
}
private <T> Optional<T> readFile(String type, Path path, JsonCodec<T> codec)
{
try {
if (!metadataFileSystem.isFile(path)) {
return Optional.empty();
}
try (FSDataInputStream inputStream = metadataFileSystem.open(path)) {
byte[] json = ByteStreams.toByteArray(inputStream);
return Optional.of(codec.fromJson(json));
}
}
catch (Exception e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not read " + type, e);
}
}
private <T> void writeSchemaFile(String type, Path directory, JsonCodec<T> codec, T value, boolean overwrite)
{
Path schemaPath = new Path(directory, PRESTO_SCHEMA_FILE_NAME);
writeFile(type + " schema", schemaPath, codec, value, overwrite);
}
private <T> void writeFile(String type, Path path, JsonCodec<T> codec, T value, boolean overwrite)
{
try {