Search in sources :

Example 26 with Table

use of io.trino.plugin.hive.metastore.Table in project trino by trinodb.

the class CachingHiveMetastore method loadPartitionsByNames.

private Map<HivePartitionName, Optional<Partition>> loadPartitionsByNames(Iterable<? extends HivePartitionName> partitionNames) {
    requireNonNull(partitionNames, "partitionNames is null");
    checkArgument(!Iterables.isEmpty(partitionNames), "partitionNames is empty");
    HivePartitionName firstPartition = Iterables.get(partitionNames, 0);
    HiveTableName hiveTableName = firstPartition.getHiveTableName();
    Optional<Table> table = getTable(hiveTableName.getDatabaseName(), hiveTableName.getTableName());
    if (table.isEmpty()) {
        return stream(partitionNames).collect(toImmutableMap(name -> name, name -> Optional.empty()));
    }
    List<String> partitionsToFetch = new ArrayList<>();
    for (HivePartitionName partitionName : partitionNames) {
        checkArgument(partitionName.getHiveTableName().equals(hiveTableName), "Expected table name %s but got %s", hiveTableName, partitionName.getHiveTableName());
        partitionsToFetch.add(partitionName.getPartitionName().orElseThrow());
    }
    ImmutableMap.Builder<HivePartitionName, Optional<Partition>> partitions = ImmutableMap.builder();
    Map<String, Optional<Partition>> partitionsByNames = delegate.getPartitionsByNames(table.get(), partitionsToFetch);
    for (HivePartitionName partitionName : partitionNames) {
        partitions.put(partitionName, partitionsByNames.getOrDefault(partitionName.getPartitionName().orElseThrow(), Optional.empty()));
    }
    return partitions.buildOrThrow();
}
Also used : CacheStatsMBean(io.airlift.jmx.CacheStatsMBean) HivePartitionManager.extractPartitionValues(io.trino.plugin.hive.HivePartitionManager.extractPartitionValues) LoadingCache(com.google.common.cache.LoadingCache) MetastoreUtil.makePartitionName(io.trino.plugin.hive.metastore.MetastoreUtil.makePartitionName) Database(io.trino.plugin.hive.metastore.Database) Throwables.throwIfUnchecked(com.google.common.base.Throwables.throwIfUnchecked) AcidOperation(io.trino.plugin.hive.acid.AcidOperation) Duration(io.airlift.units.Duration) AcidTransactionOwner(io.trino.plugin.hive.metastore.AcidTransactionOwner) ColumnStatisticType(io.trino.spi.statistics.ColumnStatisticType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) EvictableCacheBuilder(io.trino.collect.cache.EvictableCacheBuilder) PartitionFilter(io.trino.plugin.hive.metastore.PartitionFilter) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Maps.immutableEntry(com.google.common.collect.Maps.immutableEntry) Map(java.util.Map) HiveTableName.hiveTableName(io.trino.plugin.hive.metastore.HiveTableName.hiveTableName) PartitionWithStatistics(io.trino.plugin.hive.metastore.PartitionWithStatistics) PartitionFilter.partitionFilter(io.trino.plugin.hive.metastore.PartitionFilter.partitionFilter) AcidTransaction(io.trino.plugin.hive.acid.AcidTransaction) Functions.identity(com.google.common.base.Functions.identity) Table(io.trino.plugin.hive.metastore.Table) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) HivePartition(io.trino.plugin.hive.HivePartition) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) HivePartitionName.hivePartitionName(io.trino.plugin.hive.metastore.HivePartitionName.hivePartitionName) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) ThreadSafe(javax.annotation.concurrent.ThreadSafe) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) SchemaTableName(io.trino.spi.connector.SchemaTableName) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Entry(java.util.Map.Entry) Optional(java.util.Optional) HivePrivilegeInfo(io.trino.plugin.hive.metastore.HivePrivilegeInfo) HiveTableName(io.trino.plugin.hive.metastore.HiveTableName) Partition(io.trino.plugin.hive.metastore.Partition) PartitionStatistics(io.trino.plugin.hive.PartitionStatistics) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) Iterables(com.google.common.collect.Iterables) Nested(org.weakref.jmx.Nested) ImmutableSetMultimap.toImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap) HivePartitionName(io.trino.plugin.hive.metastore.HivePartitionName) Type(io.trino.spi.type.Type) PartitionNotFoundException(io.trino.plugin.hive.PartitionNotFoundException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DataOperationType(org.apache.hadoop.hive.metastore.api.DataOperationType) HiveType(io.trino.plugin.hive.HiveType) OptionalLong(java.util.OptionalLong) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) ImmutableList(com.google.common.collect.ImmutableList) Managed(org.weakref.jmx.Managed) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Objects.requireNonNull(java.util.Objects.requireNonNull) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) UserTableKey(io.trino.plugin.hive.metastore.UserTableKey) Executor(java.util.concurrent.Executor) FileUtils.makePartName(org.apache.hadoop.hive.common.FileUtils.makePartName) TupleDomain(io.trino.spi.predicate.TupleDomain) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) SetMultimap(com.google.common.collect.SetMultimap) RoleGrant(io.trino.spi.security.RoleGrant) ExecutionException(java.util.concurrent.ExecutionException) Streams.stream(com.google.common.collect.Streams.stream) TablesWithParameterCacheKey(io.trino.plugin.hive.metastore.TablesWithParameterCacheKey) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) CacheLoader.asyncReloading(com.google.common.cache.CacheLoader.asyncReloading) HivePrivilege(io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege) Table(io.trino.plugin.hive.metastore.Table) Optional(java.util.Optional) HivePartitionName(io.trino.plugin.hive.metastore.HivePartitionName) ArrayList(java.util.ArrayList) HiveTableName(io.trino.plugin.hive.metastore.HiveTableName) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 27 with Table

use of io.trino.plugin.hive.metastore.Table in project trino by trinodb.

the class FileHiveMetastore method listTablePrivileges.

@Override
public synchronized Set<HivePrivilegeInfo> listTablePrivileges(String databaseName, String tableName, Optional<String> tableOwner, Optional<HivePrincipal> principal) {
    Table table = getRequiredTable(databaseName, tableName);
    Path permissionsDirectory = getPermissionsDirectory(table);
    if (principal.isEmpty()) {
        Builder<HivePrivilegeInfo> privileges = ImmutableSet.<HivePrivilegeInfo>builder().addAll(readAllPermissions(permissionsDirectory));
        tableOwner.ifPresent(owner -> privileges.add(new HivePrivilegeInfo(OWNERSHIP, true, new HivePrincipal(USER, owner), new HivePrincipal(USER, owner))));
        return privileges.build();
    }
    ImmutableSet.Builder<HivePrivilegeInfo> result = ImmutableSet.builder();
    if (principal.get().getType() == USER && table.getOwner().orElseThrow().equals(principal.get().getName())) {
        result.add(new HivePrivilegeInfo(OWNERSHIP, true, principal.get(), principal.get()));
    }
    result.addAll(readPermissionsFile(getPermissionsPath(permissionsDirectory, principal.get())));
    return result.build();
}
Also used : Path(org.apache.hadoop.fs.Path) HivePrivilegeInfo(io.trino.plugin.hive.metastore.HivePrivilegeInfo) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal)

Example 28 with Table

use of io.trino.plugin.hive.metastore.Table in project trino by trinodb.

the class FileHiveMetastore method addPartitions.

@Override
public synchronized void addPartitions(String databaseName, String tableName, List<PartitionWithStatistics> 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 (PartitionWithStatistics partitionWithStatistics : partitions) {
            Partition partition = partitionWithStatistics.getPartition();
            verifiedPartition(table, partition);
            Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
            Path schemaPath = getSchemaPath(PARTITION, partitionMetadataDirectory);
            if (metadataFileSystem.exists(schemaPath)) {
                throw new TrinoException(HIVE_METASTORE_ERROR, "Partition already exists");
            }
            byte[] schemaJson = partitionCodec.toJsonBytes(new PartitionMetadata(table, partitionWithStatistics));
            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 TrinoException(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 TrinoException(HIVE_METASTORE_ERROR, e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) LinkedHashSet(java.util.LinkedHashSet) Partition(io.trino.plugin.hive.metastore.Partition) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) TableType(org.apache.hadoop.hive.metastore.TableType) OutputStream(java.io.OutputStream) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) PartitionWithStatistics(io.trino.plugin.hive.metastore.PartitionWithStatistics) TrinoException(io.trino.spi.TrinoException)

Example 29 with Table

use of io.trino.plugin.hive.metastore.Table in project trino by trinodb.

the class FileHiveMetastore method setTablePrivileges.

private synchronized void setTablePrivileges(HivePrincipal grantee, String databaseName, String tableName, Collection<HivePrivilegeInfo> privileges) {
    requireNonNull(grantee, "grantee 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);
        boolean created = metadataFileSystem.mkdirs(permissionsDirectory);
        if (!created && !metadataFileSystem.isDirectory(permissionsDirectory)) {
            throw new TrinoException(HIVE_METASTORE_ERROR, "Could not create permissions directory");
        }
        Path permissionFilePath = getPermissionsPath(permissionsDirectory, grantee);
        List<PermissionMetadata> permissions = privileges.stream().map(hivePrivilegeInfo -> new PermissionMetadata(hivePrivilegeInfo.getHivePrivilege(), hivePrivilegeInfo.isGrantOption(), grantee)).collect(toList());
        writeFile("permissions", permissionFilePath, permissionsCodec, permissions, true);
    } catch (IOException e) {
        throw new TrinoException(HIVE_METASTORE_ERROR, e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ThriftMetastoreUtil.updateStatisticsParameters(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.updateStatisticsParameters) Arrays(java.util.Arrays) FileSystem(org.apache.hadoop.fs.FileSystem) USER(io.trino.spi.security.PrincipalType.USER) FileStatus(org.apache.hadoop.fs.FileStatus) ColumnStatisticType(io.trino.spi.statistics.ColumnStatisticType) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) DATABASE(io.trino.plugin.hive.metastore.file.FileHiveMetastore.SchemaType.DATABASE) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Column(io.trino.plugin.hive.metastore.Column) Map(java.util.Map) PartitionWithStatistics(io.trino.plugin.hive.metastore.PartitionWithStatistics) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) EnumSet(java.util.EnumSet) TABLE_COMMENT(io.trino.plugin.hive.HiveMetadata.TABLE_COMMENT) AcidTransaction(io.trino.plugin.hive.acid.AcidTransaction) HdfsEnvironment(io.trino.plugin.hive.HdfsEnvironment) Table(io.trino.plugin.hive.metastore.Table) ConnectorIdentity(io.trino.spi.security.ConnectorIdentity) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) GuardedBy(javax.annotation.concurrent.GuardedBy) MANAGED_TABLE(org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE) SchemaTableName(io.trino.spi.connector.SchemaTableName) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) OWNERSHIP(io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP) HdfsConfig(io.trino.plugin.hive.HdfsConfig) ByteStreams(com.google.common.io.ByteStreams) HdfsConfigurationInitializer(io.trino.plugin.hive.HdfsConfigurationInitializer) Partition(io.trino.plugin.hive.metastore.Partition) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) PartitionStatistics(io.trino.plugin.hive.PartitionStatistics) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) PartitionNotFoundException(io.trino.plugin.hive.PartitionNotFoundException) ColumnNotFoundException(io.trino.spi.connector.ColumnNotFoundException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HiveType(io.trino.plugin.hive.HiveType) ThriftMetastoreUtil(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) NodeVersion(io.trino.plugin.hive.NodeVersion) SchemaAlreadyExistsException(io.trino.plugin.hive.SchemaAlreadyExistsException) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) LinkedHashSet(java.util.LinkedHashSet) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) SPARK_TABLE_PROVIDER_KEY(io.trino.plugin.hive.util.HiveUtil.SPARK_TABLE_PROVIDER_KEY) IOException(java.io.IOException) HdfsConfiguration(io.trino.plugin.hive.HdfsConfiguration) RoleGrant(io.trino.spi.security.RoleGrant) File(java.io.File) TableType(org.apache.hadoop.hive.metastore.TableType) ArrayDeque(java.util.ArrayDeque) HivePrivilege(io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege) HivePartitionManager.extractPartitionValues(io.trino.plugin.hive.HivePartitionManager.extractPartitionValues) ThriftMetastoreUtil.getHiveBasicStatistics(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.getHiveBasicStatistics) MetastoreUtil.makePartitionName(io.trino.plugin.hive.metastore.MetastoreUtil.makePartitionName) HiveUtil.toPartitionValues(io.trino.plugin.hive.util.HiveUtil.toPartitionValues) EXTERNAL_TABLE(org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE) Database(io.trino.plugin.hive.metastore.Database) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) NoHdfsAuthentication(io.trino.plugin.hive.authentication.NoHdfsAuthentication) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) MATERIALIZED_VIEW(org.apache.hadoop.hive.metastore.TableType.MATERIALIZED_VIEW) Locale(java.util.Locale) ALREADY_EXISTS(io.trino.spi.StandardErrorCode.ALREADY_EXISTS) Path(org.apache.hadoop.fs.Path) HiveHdfsConfiguration(io.trino.plugin.hive.HiveHdfsConfiguration) Collectors.toSet(java.util.stream.Collectors.toSet) HiveBasicStatistics(io.trino.plugin.hive.HiveBasicStatistics) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) TrinoException(io.trino.spi.TrinoException) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) Objects(java.util.Objects) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext) List(java.util.List) PARTITION(io.trino.plugin.hive.metastore.file.FileHiveMetastore.SchemaType.PARTITION) Entry(java.util.Map.Entry) Optional(java.util.Optional) HivePrivilegeInfo(io.trino.plugin.hive.metastore.HivePrivilegeInfo) FileUtils.unescapePathName(org.apache.hadoop.hive.common.FileUtils.unescapePathName) JsonCodec(io.airlift.json.JsonCodec) VERSION_COMPATIBILITY_CONFIG(io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig.VERSION_COMPATIBILITY_CONFIG) MetastoreConfig(io.trino.plugin.hive.metastore.MetastoreConfig) Type(io.trino.spi.type.Type) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) Builder(com.google.common.collect.ImmutableSet.Builder) HiveColumnStatistics(io.trino.plugin.hive.metastore.HiveColumnStatistics) ImmutableList(com.google.common.collect.ImmutableList) HIVE_METASTORE_ERROR(io.trino.plugin.hive.HiveErrorCode.HIVE_METASTORE_ERROR) UNSAFE_ASSUME_COMPATIBILITY(io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig.VersionCompatibility.UNSAFE_ASSUME_COMPATIBILITY) Objects.requireNonNull(java.util.Objects.requireNonNull) VIRTUAL_VIEW(org.apache.hadoop.hive.metastore.TableType.VIRTUAL_VIEW) VersionCompatibility(io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig.VersionCompatibility) OutputStream(java.io.OutputStream) DELTA_LAKE_PROVIDER(io.trino.plugin.hive.util.HiveUtil.DELTA_LAKE_PROVIDER) TupleDomain(io.trino.spi.predicate.TupleDomain) ROLE(io.trino.spi.security.PrincipalType.ROLE) Collectors.toList(java.util.stream.Collectors.toList) MetastoreUtil.verifyCanDropColumn(io.trino.plugin.hive.metastore.MetastoreUtil.verifyCanDropColumn) TABLE(io.trino.plugin.hive.metastore.file.FileHiveMetastore.SchemaType.TABLE) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException)

Example 30 with Table

use of io.trino.plugin.hive.metastore.Table in project trino by trinodb.

the class FileHiveMetastore method dropPartition.

@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.isEmpty()) {
        return;
    }
    Table table = tableReference.get();
    Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partitionValues);
    if (deleteData) {
        deleteDirectoryAndSchema(PARTITION, partitionMetadataDirectory);
    } else {
        deleteSchemaFile(PARTITION, partitionMetadataDirectory);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable)

Aggregations

Table (io.trino.plugin.hive.metastore.Table)123 TrinoException (io.trino.spi.TrinoException)69 SchemaTableName (io.trino.spi.connector.SchemaTableName)64 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)57 Path (org.apache.hadoop.fs.Path)56 Column (io.trino.plugin.hive.metastore.Column)54 Optional (java.util.Optional)54 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)50 Partition (io.trino.plugin.hive.metastore.Partition)49 List (java.util.List)48 Map (java.util.Map)47 ImmutableMap (com.google.common.collect.ImmutableMap)45 PrincipalPrivileges (io.trino.plugin.hive.metastore.PrincipalPrivileges)45 ConnectorSession (io.trino.spi.connector.ConnectorSession)45 ImmutableList (com.google.common.collect.ImmutableList)43 Set (java.util.Set)43 Objects.requireNonNull (java.util.Objects.requireNonNull)40 HdfsContext (io.trino.plugin.hive.HdfsEnvironment.HdfsContext)39 TupleDomain (io.trino.spi.predicate.TupleDomain)38 Type (io.trino.spi.type.Type)38