use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.
the class AbstractTestHiveClient method alterBucketProperty.
private void alterBucketProperty(SchemaTableName schemaTableName, Optional<HiveBucketProperty> bucketProperty) {
try (Transaction transaction = newTransaction()) {
ConnectorSession session = newSession();
String tableOwner = session.getUser();
String schemaName = schemaTableName.getSchemaName();
String tableName = schemaTableName.getTableName();
MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), getMetastoreHeaders(session), false, DEFAULT_COLUMN_CONVERTER_PROVIDER);
Optional<Table> table = transaction.getMetastore().getTable(metastoreContext, schemaName, tableName);
Table.Builder tableBuilder = Table.builder(table.get());
tableBuilder.getStorageBuilder().setBucketProperty(bucketProperty);
PrincipalPrivileges principalPrivileges = testingPrincipalPrivilege(tableOwner, session.getUser());
// hack: replaceView can be used as replaceTable despite its name
transaction.getMetastore().replaceView(metastoreContext, schemaName, tableName, tableBuilder.build(), principalPrivileges);
transaction.commit();
}
}
use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.
the class MetastoreHiveStatisticsProvider method getPartitionsStatistics.
private static Map<String, PartitionStatistics> getPartitionsStatistics(ConnectorSession session, SemiTransactionalHiveMetastore metastore, SchemaTableName table, List<HivePartition> hivePartitions) {
if (hivePartitions.isEmpty()) {
return ImmutableMap.of();
}
boolean unpartitioned = hivePartitions.stream().anyMatch(partition -> partition.getPartitionId().equals(UNPARTITIONED_ID));
MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), getMetastoreHeaders(session), isUserDefinedTypeEncodingEnabled(session), metastore.getColumnConverterProvider());
if (unpartitioned) {
checkArgument(hivePartitions.size() == 1, "expected only one hive partition");
return ImmutableMap.of(UNPARTITIONED_ID, metastore.getTableStatistics(metastoreContext, table.getSchemaName(), table.getTableName()));
}
Set<String> partitionNames = hivePartitions.stream().map(HivePartition::getPartitionId).collect(toImmutableSet());
return metastore.getPartitionStatistics(metastoreContext, table.getSchemaName(), table.getTableName(), partitionNames);
}
use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.
the class AbstractTestHiveClient method testTableCreationIgnoreExisting.
@Test
public void testTableCreationIgnoreExisting() {
List<Column> columns = ImmutableList.of(new Column("dummy", HiveType.valueOf("uniontype<smallint,tinyint>"), Optional.empty(), Optional.empty()));
SchemaTableName schemaTableName = temporaryTable("create");
ConnectorSession session = newSession();
String schemaName = schemaTableName.getSchemaName();
String tableName = schemaTableName.getTableName();
PrincipalPrivileges privileges = testingPrincipalPrivilege(session);
Path targetPath;
try {
try (Transaction transaction = newTransaction()) {
LocationService locationService = getLocationService();
LocationHandle locationHandle = locationService.forNewTable(transaction.getMetastore(), session, schemaName, tableName, false);
targetPath = locationService.getQueryWriteInfo(locationHandle).getTargetPath();
Table table = createSimpleTable(schemaTableName, columns, session, targetPath, "q1");
transaction.getMetastore().createTable(session, table, privileges, Optional.empty(), false, EMPTY_TABLE_STATISTICS);
MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), getMetastoreHeaders(session), false, DEFAULT_COLUMN_CONVERTER_PROVIDER);
Optional<Table> tableHandle = transaction.getMetastore().getTable(metastoreContext, schemaName, tableName);
assertTrue(tableHandle.isPresent());
transaction.commit();
}
// try creating it again from another transaction with ignoreExisting=false
try (Transaction transaction = newTransaction()) {
Table table = createSimpleTable(schemaTableName, columns, session, targetPath.suffix("_2"), "q2");
transaction.getMetastore().createTable(session, table, privileges, Optional.empty(), false, EMPTY_TABLE_STATISTICS);
transaction.commit();
fail("Expected exception");
} catch (PrestoException e) {
assertInstanceOf(e, TableAlreadyExistsException.class);
}
// try creating it again from another transaction with ignoreExisting=true
try (Transaction transaction = newTransaction()) {
Table table = createSimpleTable(schemaTableName, columns, session, targetPath.suffix("_3"), "q3");
transaction.getMetastore().createTable(session, table, privileges, Optional.empty(), true, EMPTY_TABLE_STATISTICS);
transaction.commit();
}
// at this point the table should exist, now try creating the table again with a different table definition
columns = ImmutableList.of(new Column("new_column", HiveType.valueOf("string"), Optional.empty(), Optional.empty()));
try (Transaction transaction = newTransaction()) {
Table table = createSimpleTable(schemaTableName, columns, session, targetPath.suffix("_4"), "q4");
transaction.getMetastore().createTable(session, table, privileges, Optional.empty(), true, EMPTY_TABLE_STATISTICS);
transaction.commit();
fail("Expected exception");
} catch (PrestoException e) {
assertEquals(e.getErrorCode(), TRANSACTION_CONFLICT.toErrorCode());
assertEquals(e.getMessage(), format("Table already exists with a different schema: '%s'", schemaTableName.getTableName()));
}
} finally {
dropTable(schemaTableName);
}
}
use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.
the class AbstractTestHiveClient method doInsertIntoExistingPartitionEmptyStatistics.
private void doInsertIntoExistingPartitionEmptyStatistics(HiveStorageFormat storageFormat, SchemaTableName tableName) throws Exception {
doCreateEmptyTable(tableName, storageFormat, CREATE_TABLE_COLUMNS_PARTITIONED);
insertData(tableName, CREATE_TABLE_PARTITIONED_DATA);
eraseStatistics(tableName);
insertData(tableName, CREATE_TABLE_PARTITIONED_DATA);
ConnectorSession session = newSession();
try (Transaction transaction = newTransaction()) {
List<String> partitionNames = transaction.getMetastore().getPartitionNames(new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), getMetastoreHeaders(session), false, DEFAULT_COLUMN_CONVERTER_PROVIDER), tableName.getSchemaName(), tableName.getTableName()).orElseThrow(() -> new AssertionError("Table does not exist: " + tableName));
for (String partitionName : partitionNames) {
HiveBasicStatistics statistics = getBasicStatisticsForPartition(new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), getMetastoreHeaders(session), false, DEFAULT_COLUMN_CONVERTER_PROVIDER), transaction, tableName, partitionName);
assertThat(statistics.getRowCount()).isNotPresent();
assertThat(statistics.getInMemoryDataSizeInBytes()).isNotPresent();
// fileCount and rawSize statistics are computed on the fly by the metastore, thus cannot be erased
}
}
}
use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.
the class TestHiveLogicalPlanner method appendTableParameter.
private void appendTableParameter(ExtendedHiveMetastore metastore, String tableName, String parameterKey, String parameterValue) {
MetastoreContext metastoreContext = new MetastoreContext(getSession().getUser(), getSession().getQueryId().getId(), Optional.empty(), Optional.empty(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
Optional<Table> table = metastore.getTable(metastoreContext, getSession().getSchema().get(), tableName);
if (table.isPresent()) {
Table originalTable = table.get();
Table alteredTable = Table.builder(originalTable).setParameter(parameterKey, parameterValue).build();
metastore.dropTable(metastoreContext, originalTable.getDatabaseName(), originalTable.getTableName(), false);
metastore.createTable(metastoreContext, alteredTable, new PrincipalPrivileges(ImmutableMultimap.of(), ImmutableMultimap.of()));
}
}
Aggregations