Search in sources :

Example 1 with HiveInsertTableHandle

use of io.trino.plugin.hive.HiveInsertTableHandle in project trino by trinodb.

the class CreateEmptyPartitionProcedure method doCreateEmptyPartition.

private void doCreateEmptyPartition(ConnectorSession session, ConnectorAccessControl accessControl, String schemaName, String tableName, List<String> partitionColumnNames, List<String> partitionValues) {
    TransactionalMetadata hiveMetadata = hiveMetadataFactory.create(session.getIdentity(), true);
    HiveTableHandle tableHandle = (HiveTableHandle) hiveMetadata.getTableHandle(session, new SchemaTableName(schemaName, tableName));
    if (tableHandle == null) {
        throw new TrinoException(INVALID_PROCEDURE_ARGUMENT, format("Table '%s' does not exist", new SchemaTableName(schemaName, tableName)));
    }
    accessControl.checkCanInsertIntoTable(null, new SchemaTableName(schemaName, tableName));
    List<String> actualPartitionColumnNames = tableHandle.getPartitionColumns().stream().map(HiveColumnHandle::getName).collect(toImmutableList());
    if (!Objects.equals(partitionColumnNames, actualPartitionColumnNames)) {
        throw new TrinoException(INVALID_PROCEDURE_ARGUMENT, "Provided partition column names do not match actual partition column names: " + actualPartitionColumnNames);
    }
    HiveMetastoreClosure metastore = hiveMetadata.getMetastore().unsafeGetRawHiveMetastoreClosure();
    if (metastore.getPartition(schemaName, tableName, partitionValues).isPresent()) {
        throw new TrinoException(ALREADY_EXISTS, "Partition already exists");
    }
    HiveInsertTableHandle hiveInsertTableHandle = (HiveInsertTableHandle) hiveMetadata.beginInsert(session, tableHandle, ImmutableList.of(), NO_RETRIES);
    String partitionName = FileUtils.makePartName(actualPartitionColumnNames, partitionValues);
    WriteInfo writeInfo = locationService.getPartitionWriteInfo(hiveInsertTableHandle.getLocationHandle(), Optional.empty(), partitionName);
    Slice serializedPartitionUpdate = Slices.wrappedBuffer(partitionUpdateJsonCodec.toJsonBytes(new PartitionUpdate(partitionName, UpdateMode.NEW, writeInfo.getWritePath(), writeInfo.getTargetPath(), ImmutableList.of(), 0, 0, 0)));
    hiveMetadata.finishInsert(session, hiveInsertTableHandle, ImmutableList.of(serializedPartitionUpdate), ImmutableList.of());
    hiveMetadata.commit();
}
Also used : HiveTableHandle(io.trino.plugin.hive.HiveTableHandle) Slice(io.airlift.slice.Slice) WriteInfo(io.trino.plugin.hive.LocationService.WriteInfo) TransactionalMetadata(io.trino.plugin.hive.TransactionalMetadata) TrinoException(io.trino.spi.TrinoException) HiveMetastoreClosure(io.trino.plugin.hive.HiveMetastoreClosure) SchemaTableName(io.trino.spi.connector.SchemaTableName) HiveInsertTableHandle(io.trino.plugin.hive.HiveInsertTableHandle) PartitionUpdate(io.trino.plugin.hive.PartitionUpdate)

Aggregations

Slice (io.airlift.slice.Slice)1 HiveInsertTableHandle (io.trino.plugin.hive.HiveInsertTableHandle)1 HiveMetastoreClosure (io.trino.plugin.hive.HiveMetastoreClosure)1 HiveTableHandle (io.trino.plugin.hive.HiveTableHandle)1 WriteInfo (io.trino.plugin.hive.LocationService.WriteInfo)1 PartitionUpdate (io.trino.plugin.hive.PartitionUpdate)1 TransactionalMetadata (io.trino.plugin.hive.TransactionalMetadata)1 TrinoException (io.trino.spi.TrinoException)1 SchemaTableName (io.trino.spi.connector.SchemaTableName)1