use of org.apache.hadoop.hive.metastore.TableType in project presto by prestodb.
the class InMemoryHiveMetastore method createTable.
@Override
public synchronized void createTable(Table table) {
TableType tableType = TableType.valueOf(table.getTableType());
checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
if (tableType == VIRTUAL_VIEW) {
checkArgument(table.getSd().getLocation() == null, "Storage location for view must be null");
} else {
File directory = new File(new Path(table.getSd().getLocation()).toUri());
checkArgument(directory.exists(), "Table directory does not exist");
if (tableType == MANAGED_TABLE) {
checkArgument(isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory");
}
}
SchemaTableName schemaTableName = new SchemaTableName(table.getDbName(), table.getTableName());
Table tableCopy = table.deepCopy();
if (relations.putIfAbsent(schemaTableName, tableCopy) != null) {
throw new TableAlreadyExistsException(schemaTableName);
}
if (tableType == VIRTUAL_VIEW) {
views.put(schemaTableName, tableCopy);
}
PrincipalPrivilegeSet privileges = table.getPrivileges();
if (privileges != null) {
for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getUserPrivileges().entrySet()) {
String user = entry.getKey();
Set<HivePrivilegeInfo> userPrivileges = entry.getValue().stream().map(HivePrivilegeInfo::parsePrivilege).flatMap(Collection::stream).collect(toImmutableSet());
setTablePrivileges(user, USER, table.getDbName(), table.getTableName(), userPrivileges);
}
for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getRolePrivileges().entrySet()) {
String role = entry.getKey();
Set<HivePrivilegeInfo> rolePrivileges = entry.getValue().stream().map(HivePrivilegeInfo::parsePrivilege).flatMap(Collection::stream).collect(toImmutableSet());
setTablePrivileges(role, ROLE, table.getDbName(), table.getTableName(), rolePrivileges);
}
}
}
use of org.apache.hadoop.hive.metastore.TableType in project presto by prestodb.
the class TestingHiveMetastore method createTable.
@Override
public synchronized void createTable(Table table, PrincipalPrivileges principalPrivileges) {
TableType tableType = TableType.valueOf(table.getTableType());
checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
SchemaTableName schemaTableName = new SchemaTableName(table.getDatabaseName(), table.getTableName());
if (tableType == VIRTUAL_VIEW) {
checkArgument(table.getStorage().getLocation().isEmpty(), "Storage location for view must be empty");
} else {
File directory = new File(URI.create(table.getStorage().getLocation()));
if (!directory.exists()) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Table directory does not exist");
}
if (tableType == MANAGED_TABLE && !isParentDir(directory, baseDirectory)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Table directory must be inside of the metastore base directory");
}
}
if (relations.putIfAbsent(schemaTableName, table) != null) {
throw new TableAlreadyExistsException(schemaTableName);
}
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());
}
}
use of org.apache.hadoop.hive.metastore.TableType in project hive by apache.
the class HiveRelOpMaterializationValidator method visit.
@Override
public RelNode visit(TableScan scan) {
if (scan instanceof HiveTableScan) {
HiveTableScan hiveScan = (HiveTableScan) scan;
RelOptHiveTable relOptHiveTable = (RelOptHiveTable) hiveScan.getTable();
Table tab = relOptHiveTable.getHiveTableMD();
if (tab.isTemporary()) {
fail(tab.getTableName() + " is a temporary table");
}
TableType tt = tab.getTableType();
if (tab.getTableType() == TableType.EXTERNAL_TABLE) {
fail(tab.getFullyQualifiedName() + " is an external table");
}
return scan;
}
// TableScan of a non-Hive table - don't support for materializations.
fail(scan.getTable().getQualifiedName() + " is a table scan of a non-Hive table.");
return scan;
}
use of org.apache.hadoop.hive.metastore.TableType in project presto by prestodb.
the class InMemoryHiveMetastore method createTable.
@Override
public synchronized void createTable(MetastoreContext metastoreContext, Table table) {
TableType tableType = TableType.valueOf(table.getTableType());
checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
if (tableType == VIRTUAL_VIEW) {
checkArgument(table.getSd().getLocation() == null, "Storage location for view must be null");
} else {
File directory = new File(new Path(table.getSd().getLocation()).toUri());
checkArgument(directory.exists(), "Table directory does not exist: %s", directory);
if (tableType == MANAGED_TABLE) {
checkArgument(isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory");
}
}
SchemaTableName schemaTableName = new SchemaTableName(table.getDbName(), table.getTableName());
Table tableCopy = table.deepCopy();
if (relations.putIfAbsent(schemaTableName, tableCopy) != null) {
throw new TableAlreadyExistsException(schemaTableName);
}
if (tableType == VIRTUAL_VIEW) {
views.put(schemaTableName, tableCopy);
}
PrincipalPrivilegeSet privileges = table.getPrivileges();
if (privileges != null) {
throw new UnsupportedOperationException();
}
}
use of org.apache.hadoop.hive.metastore.TableType in project presto by prestodb.
the class FileHiveMetastore method addPartitions.
@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);
}
}
Aggregations