Search in sources :

Example 81 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class SemiTransactionalHiveMetastore method declareIntentionToWrite.

public synchronized String declareIntentionToWrite(ConnectorSession session, WriteMode writeMode, Path stagingPathRoot, SchemaTableName schemaTableName) {
    setShared();
    if (writeMode == WriteMode.DIRECT_TO_TARGET_EXISTING_DIRECTORY) {
        Map<List<String>, Action<PartitionAndMore>> partitionActionsOfTable = partitionActions.get(schemaTableName);
        if (partitionActionsOfTable != null && !partitionActionsOfTable.isEmpty()) {
            throw new TrinoException(NOT_SUPPORTED, "Cannot insert into a table with a partition that has been modified in the same transaction when Trino is configured to skip temporary directories.");
        }
    }
    HdfsContext hdfsContext = new HdfsContext(session);
    String queryId = session.getQueryId();
    String declarationId = queryId + "_" + declaredIntentionsToWriteCounter;
    declaredIntentionsToWriteCounter++;
    declaredIntentionsToWrite.add(new DeclaredIntentionToWrite(declarationId, writeMode, hdfsContext, queryId, stagingPathRoot, schemaTableName));
    return declarationId;
}
Also used : TrinoException(io.trino.spi.TrinoException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) ValidTxnWriteIdList(org.apache.hadoop.hive.common.ValidTxnWriteIdList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext)

Example 82 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class SemiTransactionalHiveMetastore method dropPartition.

public synchronized void dropPartition(ConnectorSession session, String databaseName, String tableName, List<String> partitionValues, boolean deleteData) {
    setShared();
    Map<List<String>, Action<PartitionAndMore>> partitionActionsOfTable = partitionActions.computeIfAbsent(new SchemaTableName(databaseName, tableName), k -> new HashMap<>());
    Action<PartitionAndMore> oldPartitionAction = partitionActionsOfTable.get(partitionValues);
    if (oldPartitionAction == null) {
        HdfsContext hdfsContext = new HdfsContext(session);
        if (deleteData) {
            partitionActionsOfTable.put(partitionValues, new Action<>(ActionType.DROP, null, hdfsContext, session.getQueryId()));
        } else {
            partitionActionsOfTable.put(partitionValues, new Action<>(ActionType.DROP_PRESERVE_DATA, null, hdfsContext, session.getQueryId()));
        }
        return;
    }
    switch(oldPartitionAction.getType()) {
        case DROP:
        case DROP_PRESERVE_DATA:
            throw new PartitionNotFoundException(new SchemaTableName(databaseName, tableName), partitionValues);
        case ADD:
        case ALTER:
        case INSERT_EXISTING:
        case DELETE_ROWS:
        case UPDATE:
            throw new TrinoException(NOT_SUPPORTED, format("dropping a partition added in the same transaction is not supported: %s %s %s", databaseName, tableName, partitionValues));
    }
    throw new IllegalStateException("Unknown action type");
}
Also used : PartitionNotFoundException(io.trino.plugin.hive.PartitionNotFoundException) TrinoException(io.trino.spi.TrinoException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) ValidTxnWriteIdList(org.apache.hadoop.hive.common.ValidTxnWriteIdList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Example 83 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class SemiTransactionalHiveMetastore method truncateUnpartitionedTable.

public synchronized void truncateUnpartitionedTable(ConnectorSession session, String databaseName, String tableName) {
    checkReadable();
    Optional<Table> table = getTable(databaseName, tableName);
    SchemaTableName schemaTableName = new SchemaTableName(databaseName, tableName);
    if (table.isEmpty()) {
        throw new TableNotFoundException(schemaTableName);
    }
    if (!table.get().getTableType().equals(MANAGED_TABLE.toString())) {
        throw new TrinoException(NOT_SUPPORTED, "Cannot delete from non-managed Hive table");
    }
    if (!table.get().getPartitionColumns().isEmpty()) {
        throw new IllegalArgumentException("Table is partitioned");
    }
    Path path = new Path(table.get().getStorage().getLocation());
    HdfsContext context = new HdfsContext(session);
    setExclusive((delegate, hdfsEnvironment) -> {
        RecursiveDeleteResult recursiveDeleteResult = recursiveDeleteFiles(hdfsEnvironment, context, path, ImmutableSet.of(""), false);
        if (!recursiveDeleteResult.getNotDeletedEligibleItems().isEmpty()) {
            throw new TrinoException(HIVE_FILESYSTEM_ERROR, format("Error deleting from unpartitioned table %s. These items cannot be deleted: %s", schemaTableName, recursiveDeleteResult.getNotDeletedEligibleItems()));
        }
    });
}
Also used : Path(org.apache.hadoop.fs.Path) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) TrinoException(io.trino.spi.TrinoException) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Example 84 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class SemiTransactionalHiveMetastore method createTable.

/**
 * {@code currentLocation} needs to be supplied if a writePath exists for the table.
 */
public synchronized void createTable(ConnectorSession session, Table table, PrincipalPrivileges principalPrivileges, Optional<Path> currentPath, Optional<List<String>> files, boolean ignoreExisting, PartitionStatistics statistics, boolean cleanExtraOutputFilesOnCommit) {
    setShared();
    // When creating a table, it should never have partition actions. This is just a sanity check.
    checkNoPartitionAction(table.getDatabaseName(), table.getTableName());
    Action<TableAndMore> oldTableAction = tableActions.get(table.getSchemaTableName());
    TableAndMore tableAndMore = new TableAndMore(table, Optional.of(principalPrivileges), currentPath, files, ignoreExisting, statistics, statistics, cleanExtraOutputFilesOnCommit);
    if (oldTableAction == null) {
        HdfsContext hdfsContext = new HdfsContext(session);
        tableActions.put(table.getSchemaTableName(), new Action<>(ActionType.ADD, tableAndMore, hdfsContext, session.getQueryId()));
        return;
    }
    switch(oldTableAction.getType()) {
        case DROP:
            if (!oldTableAction.getHdfsContext().getIdentity().getUser().equals(session.getUser())) {
                throw new TrinoException(TRANSACTION_CONFLICT, "Operation on the same table with different user in the same transaction is not supported");
            }
            HdfsContext hdfsContext = new HdfsContext(session);
            tableActions.put(table.getSchemaTableName(), new Action<>(ActionType.ALTER, tableAndMore, hdfsContext, session.getQueryId()));
            return;
        case ADD:
        case ALTER:
        case INSERT_EXISTING:
        case DELETE_ROWS:
        case UPDATE:
            throw new TableAlreadyExistsException(table.getSchemaTableName());
        case DROP_PRESERVE_DATA:
            // TODO
            break;
    }
    throw new IllegalStateException("Unknown action type");
}
Also used : TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) TrinoException(io.trino.spi.TrinoException) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext)

Example 85 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class AlluxioHiveMetastore method getPartitionsByNames.

@Override
public Map<String, Optional<Partition>> getPartitionsByNames(Table table, List<String> partitionNames) {
    if (partitionNames.isEmpty()) {
        return Collections.emptyMap();
    }
    String databaseName = table.getDatabaseName();
    String tableName = table.getTableName();
    try {
        // Get all partitions
        List<PartitionInfo> partitionInfos = ProtoUtils.toPartitionInfoList(client.readTable(databaseName, tableName, Constraint.getDefaultInstance()));
        // Check that table name is correct
        // TODO also check for database name equality
        partitionInfos = partitionInfos.stream().filter(partition -> partition.getTableName().equals(tableName)).collect(Collectors.toList());
        Map<String, Optional<Partition>> result = partitionInfos.stream().filter(partitionName -> partitionNames.stream().anyMatch(partitionName.getPartitionName()::equals)).collect(Collectors.toMap(PartitionInfo::getPartitionName, partitionInfo -> Optional.of(ProtoUtils.fromProto(partitionInfo))));
        return Collections.unmodifiableMap(result);
    } catch (AlluxioStatusException e) {
        throw new TrinoException(HIVE_METASTORE_ERROR, e);
    }
}
Also used : ColumnStatisticsInfo(alluxio.grpc.table.ColumnStatisticsInfo) PartitionStatistics(io.trino.plugin.hive.PartitionStatistics) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) ThriftMetastoreUtil.getHiveBasicStatistics(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.getHiveBasicStatistics) MetastoreConfig(io.trino.plugin.hive.metastore.MetastoreConfig) Type(io.trino.spi.type.Type) Database(io.trino.plugin.hive.metastore.Database) Function(java.util.function.Function) ColumnStatisticType(io.trino.spi.statistics.ColumnStatisticType) ArrayList(java.util.ArrayList) HiveType(io.trino.plugin.hive.HiveType) OptionalLong(java.util.OptionalLong) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) HiveColumnStatistics(io.trino.plugin.hive.metastore.HiveColumnStatistics) ThriftMetastoreUtil(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) Column(io.trino.plugin.hive.metastore.Column) HIVE_METASTORE_ERROR(io.trino.plugin.hive.HiveErrorCode.HIVE_METASTORE_ERROR) TableMasterClient(alluxio.client.table.TableMasterClient) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Constraint(alluxio.grpc.table.Constraint) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) TableInfo(alluxio.grpc.table.TableInfo) PartitionWithStatistics(io.trino.plugin.hive.metastore.PartitionWithStatistics) HiveBasicStatistics(io.trino.plugin.hive.HiveBasicStatistics) AcidTransaction(io.trino.plugin.hive.acid.AcidTransaction) Table(io.trino.plugin.hive.metastore.Table) ImmutableMap(com.google.common.collect.ImmutableMap) FileUtils.makePartName(org.apache.hadoop.hive.common.FileUtils.makePartName) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) TupleDomain(io.trino.spi.predicate.TupleDomain) NotFoundException(alluxio.exception.status.NotFoundException) Collectors(java.util.stream.Collectors) RoleGrant(io.trino.spi.security.RoleGrant) PartitionInfo(alluxio.grpc.table.layout.hive.PartitionInfo) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Optional(java.util.Optional) HivePrivilegeInfo(io.trino.plugin.hive.metastore.HivePrivilegeInfo) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) Collections(java.util.Collections) HivePrivilege(io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege) Partition(io.trino.plugin.hive.metastore.Partition) Optional(java.util.Optional) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) TrinoException(io.trino.spi.TrinoException) PartitionInfo(alluxio.grpc.table.layout.hive.PartitionInfo)

Aggregations

TrinoException (io.trino.spi.TrinoException)623 IOException (java.io.IOException)151 ImmutableList (com.google.common.collect.ImmutableList)105 List (java.util.List)100 Type (io.trino.spi.type.Type)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)90 SchemaTableName (io.trino.spi.connector.SchemaTableName)83 Path (org.apache.hadoop.fs.Path)83 Optional (java.util.Optional)79 ArrayList (java.util.ArrayList)77 Map (java.util.Map)76 ImmutableMap (com.google.common.collect.ImmutableMap)70 Objects.requireNonNull (java.util.Objects.requireNonNull)69 ConnectorSession (io.trino.spi.connector.ConnectorSession)63 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)62 ImmutableSet (com.google.common.collect.ImmutableSet)56 VarcharType (io.trino.spi.type.VarcharType)54 Set (java.util.Set)54 Slice (io.airlift.slice.Slice)53 Table (io.trino.plugin.hive.metastore.Table)52