Search in sources :

Example 1 with TableType

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);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Table(org.apache.hadoop.hive.metastore.api.Table) PrincipalPrivilegeSet(org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) File(java.io.File) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 2 with TableType

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());
    }
}
Also used : TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Collection(java.util.Collection) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName) File(java.io.File)

Example 3 with TableType

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;
}
Also used : Table(org.apache.hadoop.hive.ql.metadata.Table) TableType(org.apache.hadoop.hive.metastore.TableType) HiveTableScan(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveTableScan)

Example 4 with TableType

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();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Table(org.apache.hadoop.hive.metastore.api.Table) PrincipalPrivilegeSet(org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet) File(java.io.File) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 5 with TableType

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);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) LinkedHashSet(java.util.LinkedHashSet) Partition(com.facebook.presto.hive.metastore.Partition) Table(com.facebook.presto.hive.metastore.Table) TableType(org.apache.hadoop.hive.metastore.TableType) OutputStream(java.io.OutputStream) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TableType (org.apache.hadoop.hive.metastore.TableType)6 Path (org.apache.hadoop.fs.Path)4 TableAlreadyExistsException (com.facebook.presto.hive.TableAlreadyExistsException)3 SchemaTableName (com.facebook.presto.spi.SchemaTableName)3 File (java.io.File)3 PrestoException (com.facebook.presto.spi.PrestoException)2 IOException (java.io.IOException)2 PrincipalPrivilegeSet (org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet)2 Table (org.apache.hadoop.hive.metastore.api.Table)2 Partition (com.facebook.presto.hive.metastore.Partition)1 Table (com.facebook.presto.hive.metastore.Table)1 ImmutableList (com.google.common.collect.ImmutableList)1 DataOutputStream (java.io.DataOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1