Search in sources :

Example 6 with MetastoreContext

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();
    }
}
Also used : Table(com.facebook.presto.hive.metastore.Table) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) ConnectorSession(com.facebook.presto.spi.ConnectorSession)

Example 7 with MetastoreContext

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);
}
Also used : MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext)

Example 8 with MetastoreContext

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);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Table(com.facebook.presto.hive.metastore.Table) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Column(com.facebook.presto.hive.metastore.Column) SortingColumn(com.facebook.presto.hive.metastore.SortingColumn) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) ConnectorSession(com.facebook.presto.spi.ConnectorSession) Test(org.testng.annotations.Test)

Example 9 with MetastoreContext

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
        }
    }
}
Also used : MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) ConnectorSession(com.facebook.presto.spi.ConnectorSession)

Example 10 with MetastoreContext

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()));
    }
}
Also used : Table(com.facebook.presto.hive.metastore.Table) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext)

Aggregations

MetastoreContext (com.facebook.presto.hive.metastore.MetastoreContext)99 Table (com.facebook.presto.hive.metastore.Table)59 PrestoException (com.facebook.presto.spi.PrestoException)53 SchemaTableName (com.facebook.presto.spi.SchemaTableName)52 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)49 Column (com.facebook.presto.hive.metastore.Column)42 ImmutableList (com.google.common.collect.ImmutableList)41 ImmutableMap (com.google.common.collect.ImmutableMap)41 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)40 Map (java.util.Map)38 Optional (java.util.Optional)38 PrestoPrincipal (com.facebook.presto.spi.security.PrestoPrincipal)37 List (java.util.List)37 Set (java.util.Set)37 PartitionStatistics (com.facebook.presto.hive.metastore.PartitionStatistics)35 Objects.requireNonNull (java.util.Objects.requireNonNull)35 Type (com.facebook.presto.common.type.Type)34 HivePrivilegeInfo (com.facebook.presto.hive.metastore.HivePrivilegeInfo)34 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)34 Domain (com.facebook.presto.common.predicate.Domain)33