Search in sources :

Example 6 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class MetastoreUtil method fromMetastoreApiTable.

public static Table fromMetastoreApiTable(org.apache.hadoop.hive.metastore.api.Table table) {
    StorageDescriptor storageDescriptor = table.getSd();
    if (storageDescriptor == null) {
        throw new PrestoException(HIVE_INVALID_METADATA, "Table is missing storage descriptor");
    }
    Table.Builder tableBuilder = Table.builder().setDatabaseName(table.getDbName()).setTableName(table.getTableName()).setOwner(nullToEmpty(table.getOwner())).setTableType(table.getTableType()).setDataColumns(storageDescriptor.getCols().stream().map(MetastoreUtil::fromMetastoreApiFieldSchema).collect(toList())).setPartitionColumns(table.getPartitionKeys().stream().map(MetastoreUtil::fromMetastoreApiFieldSchema).collect(toList())).setParameters(table.getParameters() == null ? ImmutableMap.of() : table.getParameters()).setViewOriginalText(Optional.ofNullable(emptyToNull(table.getViewOriginalText()))).setViewExpandedText(Optional.ofNullable(emptyToNull(table.getViewExpandedText())));
    fromMetastoreApiStorageDescriptor(storageDescriptor, tableBuilder.getStorageBuilder(), table.getTableName());
    return tableBuilder.build();
}
Also used : StorageDescriptor(org.apache.hadoop.hive.metastore.api.StorageDescriptor) PrestoException(com.facebook.presto.spi.PrestoException)

Example 7 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class FileHiveMetastore method renameTable.

@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);
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException)

Example 8 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class SemiTransactionalHiveMetastore method getTablePrivileges.

public synchronized Set<HivePrivilegeInfo> getTablePrivileges(String user, String databaseName, String tableName) {
    checkReadable();
    SchemaTableName schemaTableName = new SchemaTableName(databaseName, tableName);
    Action<TableAndMore> tableAction = tableActions.get(schemaTableName);
    if (tableAction == null) {
        return delegate.getTablePrivileges(user, databaseName, tableName);
    }
    switch(tableAction.getType()) {
        case ADD:
        case ALTER:
            {
                if (!user.equals(tableAction.getData().getTable().getOwner())) {
                    throw new PrestoException(NOT_SUPPORTED, "Cannot access a table newly created in the transaction with a different user");
                }
                Collection<HivePrivilegeInfo> privileges = tableAction.getData().getPrincipalPrivileges().getUserPrivileges().get(user);
                return ImmutableSet.<HivePrivilegeInfo>builder().addAll(privileges).add(new HivePrivilegeInfo(OWNERSHIP, true)).build();
            }
        case INSERT_EXISTING:
            return delegate.getTablePrivileges(user, databaseName, tableName);
        case DROP:
            throw new TableNotFoundException(schemaTableName);
        default:
            throw new IllegalStateException("Unknown action type");
    }
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) Collection(java.util.Collection) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 9 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class FileHiveMetastore method renameDatabase.

@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);
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException)

Example 10 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class TestingHiveMetastore method createDatabase.

@Override
public synchronized void createDatabase(Database database) {
    requireNonNull(database, "database is null");
    if (!database.getLocation().isPresent()) {
        File location = new File(baseDirectory, database.getDatabaseName() + ".db");
        database = Database.builder(database).setLocation(Optional.of(location.getAbsoluteFile().toURI().toString())).build();
    }
    File directory = new File(URI.create(database.getLocation().get()));
    if (directory.exists()) {
        throw new PrestoException(HIVE_METASTORE_ERROR, "Database directory already exists");
    }
    if (!isParentDir(directory, baseDirectory)) {
        throw new PrestoException(HIVE_METASTORE_ERROR, "Database directory must be inside of the metastore base directory");
    }
    if (!directory.mkdirs()) {
        throw new PrestoException(HIVE_METASTORE_ERROR, "Could not create database directory");
    }
    if (databases.putIfAbsent(database.getDatabaseName(), database) != null) {
        throw new PrestoException(ALREADY_EXISTS, "Database " + database.getDatabaseName() + " already exists");
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) File(java.io.File)

Aggregations

PrestoException (com.facebook.presto.spi.PrestoException)747 IOException (java.io.IOException)161 ImmutableList (com.google.common.collect.ImmutableList)106 Type (com.facebook.presto.common.type.Type)95 SchemaTableName (com.facebook.presto.spi.SchemaTableName)88 List (java.util.List)83 ArrayList (java.util.ArrayList)79 Path (org.apache.hadoop.fs.Path)78 Optional (java.util.Optional)68 ImmutableMap (com.google.common.collect.ImmutableMap)64 Map (java.util.Map)64 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)58 Block (com.facebook.presto.common.block.Block)55 Table (com.facebook.presto.hive.metastore.Table)55 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)55 Objects.requireNonNull (java.util.Objects.requireNonNull)55 Slice (io.airlift.slice.Slice)50 SqlType (com.facebook.presto.spi.function.SqlType)49 Test (org.testng.annotations.Test)49 ConnectorSession (com.facebook.presto.spi.ConnectorSession)46