use of io.trino.plugin.hive.HiveMetastoreClosure 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();
}
use of io.trino.plugin.hive.HiveMetastoreClosure in project trino by trinodb.
the class TestCachingHiveMetastore method testUpdatePartitionStatistics.
@Test
public void testUpdatePartitionStatistics() {
assertEquals(mockClient.getAccessCount(), 0);
HiveMetastoreClosure hiveMetastoreClosure = new HiveMetastoreClosure(metastore);
Table table = hiveMetastoreClosure.getTable(TEST_DATABASE, TEST_TABLE).get();
assertEquals(mockClient.getAccessCount(), 1);
hiveMetastoreClosure.updatePartitionStatistics(table.getDatabaseName(), table.getTableName(), TEST_PARTITION1, identity());
assertEquals(mockClient.getAccessCount(), 5);
}
use of io.trino.plugin.hive.HiveMetastoreClosure in project trino by trinodb.
the class TestHiveGlueMetastore method createDummyPartitionedTable.
private void createDummyPartitionedTable(SchemaTableName tableName, List<ColumnMetadata> columns, List<String> partitionColumnNames, List<PartitionValues> partitionValues) throws Exception {
doCreateEmptyTable(tableName, ORC, columns, partitionColumnNames);
HiveMetastoreClosure metastoreClient = new HiveMetastoreClosure(getMetastoreClient());
Table table = metastoreClient.getTable(tableName.getSchemaName(), tableName.getTableName()).orElseThrow(() -> new TableNotFoundException(tableName));
List<PartitionWithStatistics> partitions = new ArrayList<>();
List<String> partitionNames = new ArrayList<>();
partitionValues.stream().map(partitionValue -> makePartName(partitionColumnNames, partitionValue.values)).forEach(partitionName -> {
partitions.add(new PartitionWithStatistics(createDummyPartition(table, partitionName), partitionName, PartitionStatistics.empty()));
partitionNames.add(partitionName);
});
metastoreClient.addPartitions(tableName.getSchemaName(), tableName.getTableName(), partitions);
partitionNames.forEach(partitionName -> metastoreClient.updatePartitionStatistics(tableName.getSchemaName(), tableName.getTableName(), partitionName, currentStatistics -> EMPTY_TABLE_STATISTICS));
}
use of io.trino.plugin.hive.HiveMetastoreClosure in project trino by trinodb.
the class DropStatsProcedure method doDropStats.
private void doDropStats(ConnectorSession session, ConnectorAccessControl accessControl, String schema, String table, List<?> partitionValues) {
TransactionalMetadata hiveMetadata = hiveMetadataFactory.create(session.getIdentity(), true);
HiveTableHandle handle = (HiveTableHandle) hiveMetadata.getTableHandle(session, new SchemaTableName(schema, table));
if (handle == null) {
throw new TrinoException(INVALID_PROCEDURE_ARGUMENT, format("Table '%s' does not exist", new SchemaTableName(schema, table)));
}
accessControl.checkCanInsertIntoTable(null, new SchemaTableName(schema, table));
Map<String, ColumnHandle> columns = hiveMetadata.getColumnHandles(session, handle);
List<String> partitionColumns = columns.values().stream().map(HiveColumnHandle.class::cast).filter(HiveColumnHandle::isPartitionKey).map(HiveColumnHandle::getName).collect(toImmutableList());
HiveMetastoreClosure metastore = hiveMetadata.getMetastore().unsafeGetRawHiveMetastoreClosure();
if (partitionValues != null) {
// drop stats for specified partitions
List<List<String>> partitionStringValues = partitionValues.stream().map(DropStatsProcedure::validateParameterType).collect(toImmutableList());
validatePartitions(partitionStringValues, partitionColumns);
partitionStringValues.forEach(values -> metastore.updatePartitionStatistics(schema, table, makePartName(partitionColumns, values), stats -> PartitionStatistics.empty()));
} else {
// no partition specified, so drop stats for the entire table
if (partitionColumns.isEmpty()) {
// for non-partitioned tables, just wipe table stats
metastore.updateTableStatistics(schema, table, NO_ACID_TRANSACTION, stats -> PartitionStatistics.empty());
} else {
// the table is partitioned; remove stats for every partition
metastore.getPartitionNamesByFilter(handle.getSchemaName(), handle.getTableName(), partitionColumns, TupleDomain.all()).ifPresent(partitions -> partitions.forEach(partitionName -> metastore.updatePartitionStatistics(schema, table, partitionName, stats -> PartitionStatistics.empty())));
}
}
hiveMetadata.commit();
}
use of io.trino.plugin.hive.HiveMetastoreClosure in project trino by trinodb.
the class TestHiveGlueMetastore method setup.
@BeforeClass
public void setup() {
metastore = new HiveMetastoreClosure(metastoreClient);
glueClient = AWSGlueAsyncClientBuilder.defaultClient();
}
Aggregations