Search in sources :

Example 66 with MetastoreContext

use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.

the class FileHiveMetastore method getPartitionNames.

@Override
public synchronized Optional<List<String>> getPartitionNames(MetastoreContext metastoreContext, String databaseName, String tableName) {
    requireNonNull(databaseName, "databaseName is null");
    requireNonNull(tableName, "tableName is null");
    Optional<Table> tableReference = getTable(metastoreContext, databaseName, tableName);
    if (!tableReference.isPresent()) {
        return Optional.empty();
    }
    Table table = tableReference.get();
    Path tableMetadataDirectory = getTableMetadataDirectory(table);
    List<ArrayDeque<String>> partitions = listPartitions(tableMetadataDirectory, table.getPartitionColumns());
    List<String> partitionNames = partitions.stream().map(partitionValues -> makePartName(table.getPartitionColumns(), ImmutableList.copyOf(partitionValues))).collect(toList());
    return Optional.of(ImmutableList.copyOf(partitionNames));
}
Also used : Path(org.apache.hadoop.fs.Path) HdfsEnvironment(com.facebook.presto.hive.HdfsEnvironment) JsonCodec(com.facebook.airlift.json.JsonCodec) SchemaAlreadyExistsException(com.facebook.presto.hive.SchemaAlreadyExistsException) MetastoreUtil.makePartName(com.facebook.presto.hive.metastore.MetastoreUtil.makePartName) PartitionWithStatistics(com.facebook.presto.hive.metastore.PartitionWithStatistics) FileSystem(org.apache.hadoop.fs.FileSystem) PrestoPrincipal(com.facebook.presto.spi.security.PrestoPrincipal) PartitionNotFoundException(com.facebook.presto.hive.PartitionNotFoundException) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) FileStatus(org.apache.hadoop.fs.FileStatus) EXTERNAL_TABLE(com.facebook.presto.hive.metastore.PrestoTableType.EXTERNAL_TABLE) MATERIALIZED_VIEW(com.facebook.presto.hive.metastore.PrestoTableType.MATERIALIZED_VIEW) Duration(io.airlift.units.Duration) MetastoreUtil.getHiveBasicStatistics(com.facebook.presto.hive.metastore.MetastoreUtil.getHiveBasicStatistics) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) HIVE_METASTORE_ERROR(com.facebook.presto.hive.HiveErrorCode.HIVE_METASTORE_ERROR) ColumnNotFoundException(com.facebook.presto.spi.ColumnNotFoundException) ExtendedHiveMetastore(com.facebook.presto.hive.metastore.ExtendedHiveMetastore) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Locale(java.util.Locale) Map(java.util.Map) MANAGED_TABLE(com.facebook.presto.hive.metastore.PrestoTableType.MANAGED_TABLE) HiveTableName(com.facebook.presto.hive.metastore.HiveTableName) Path(org.apache.hadoop.fs.Path) HiveBasicStatistics(com.facebook.presto.hive.HiveBasicStatistics) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TEMPORARY_TABLE(com.facebook.presto.hive.metastore.PrestoTableType.TEMPORARY_TABLE) HdfsContext(com.facebook.presto.hive.HdfsContext) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) EnumSet(java.util.EnumSet) Collectors.toSet(java.util.stream.Collectors.toSet) BiMap(com.google.common.collect.BiMap) ImmutableSet(com.google.common.collect.ImmutableSet) PartitionNameWithVersion(com.facebook.presto.hive.metastore.PartitionNameWithVersion) ImmutableMap(com.google.common.collect.ImmutableMap) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Objects(java.util.Objects) List(java.util.List) ROLE(com.facebook.presto.spi.security.PrincipalType.ROLE) RoleGrant(com.facebook.presto.spi.security.RoleGrant) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) ByteStreams(com.google.common.io.ByteStreams) Entry(java.util.Map.Entry) MetastoreUtil.toPartitionValues(com.facebook.presto.hive.metastore.MetastoreUtil.toPartitionValues) Optional(java.util.Optional) HivePrivilegeInfo(com.facebook.presto.hive.metastore.HivePrivilegeInfo) FileUtils.unescapePathName(org.apache.hadoop.hive.common.FileUtils.unescapePathName) MetastoreUtil.verifyCanDropColumn(com.facebook.presto.hive.metastore.MetastoreUtil.verifyCanDropColumn) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Table(com.facebook.presto.hive.metastore.Table) Column(com.facebook.presto.hive.metastore.Column) Database(com.facebook.presto.hive.metastore.Database) HiveType(com.facebook.presto.hive.HiveType) HIVE_PARTITION_DROPPED_DURING_QUERY(com.facebook.presto.hive.HiveErrorCode.HIVE_PARTITION_DROPPED_DURING_QUERY) HashMap(java.util.HashMap) HiveColumnStatistics(com.facebook.presto.hive.metastore.HiveColumnStatistics) PrestoException(com.facebook.presto.spi.PrestoException) MetastoreUtil.convertPredicateToParts(com.facebook.presto.hive.metastore.MetastoreUtil.convertPredicateToParts) Function(java.util.function.Function) Partition(com.facebook.presto.hive.metastore.Partition) ArrayList(java.util.ArrayList) OWNERSHIP(com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) MetastoreUtil(com.facebook.presto.hive.metastore.MetastoreUtil) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) Objects.requireNonNull(java.util.Objects.requireNonNull) MetastoreUtil.updateStatisticsParameters(com.facebook.presto.hive.metastore.MetastoreUtil.updateStatisticsParameters) Type(com.facebook.presto.common.type.Type) LinkedHashSet(java.util.LinkedHashSet) OutputStream(java.io.OutputStream) USER(com.facebook.presto.spi.security.PrincipalType.USER) IOException(java.io.IOException) HiveTableName.hiveTableName(com.facebook.presto.hive.metastore.HiveTableName.hiveTableName) Domain(com.facebook.presto.common.predicate.Domain) File(java.io.File) VIRTUAL_VIEW(com.facebook.presto.hive.metastore.PrestoTableType.VIRTUAL_VIEW) ConnectorIdentity(com.facebook.presto.spi.security.ConnectorIdentity) HashBiMap(com.google.common.collect.HashBiMap) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnStatisticType(com.facebook.presto.spi.statistics.ColumnStatisticType) MetastoreUtil.extractPartitionValues(com.facebook.presto.hive.metastore.MetastoreUtil.extractPartitionValues) PartitionStatistics(com.facebook.presto.hive.metastore.PartitionStatistics) ArrayDeque(java.util.ArrayDeque) Table(com.facebook.presto.hive.metastore.Table) ArrayDeque(java.util.ArrayDeque)

Example 67 with MetastoreContext

use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.

the class IcebergHiveMetadata method getRawSystemTable.

private Optional<SystemTable> getRawSystemTable(ConnectorSession session, SchemaTableName tableName) {
    IcebergTableName name = IcebergTableName.from(tableName.getTableName());
    MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
    Optional<Table> hiveTable = metastore.getTable(metastoreContext, tableName.getSchemaName(), name.getTableName());
    if (!hiveTable.isPresent() || !isIcebergTable(hiveTable.get())) {
        return Optional.empty();
    }
    org.apache.iceberg.Table table = getHiveIcebergTable(metastore, hdfsEnvironment, session, new SchemaTableName(tableName.getSchemaName(), name.getTableName()));
    return getIcebergSystemTable(tableName, table);
}
Also used : IcebergUtil.isIcebergTable(com.facebook.presto.iceberg.IcebergUtil.isIcebergTable) IcebergUtil.getHiveIcebergTable(com.facebook.presto.iceberg.IcebergUtil.getHiveIcebergTable) SystemTable(com.facebook.presto.spi.SystemTable) Table(com.facebook.presto.hive.metastore.Table) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 68 with MetastoreContext

use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.

the class IcebergHiveMetadata method beginCreateTable.

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout) {
    SchemaTableName schemaTableName = tableMetadata.getTable();
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    Schema schema = toIcebergSchema(tableMetadata.getColumns());
    PartitionSpec partitionSpec = parsePartitionFields(schema, getPartitioning(tableMetadata.getProperties()));
    MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
    Database database = metastore.getDatabase(metastoreContext, schemaName).orElseThrow(() -> new SchemaNotFoundException(schemaName));
    HdfsContext hdfsContext = new HdfsContext(session, schemaName, tableName);
    String targetPath = getTableLocation(tableMetadata.getProperties());
    if (targetPath == null) {
        Optional<String> location = database.getLocation();
        if (!location.isPresent() || location.get().isEmpty()) {
            throw new PrestoException(NOT_SUPPORTED, "Database " + schemaName + " location is not set");
        }
        Path databasePath = new Path(location.get());
        Path resultPath = new Path(databasePath, tableName);
        targetPath = resultPath.toString();
    }
    TableOperations operations = new HiveTableOperations(metastore, new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER), hdfsEnvironment, hdfsContext, schemaName, tableName, session.getUser(), targetPath);
    if (operations.current() != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builderWithExpectedSize(2);
    FileFormat fileFormat = getFileFormat(tableMetadata.getProperties());
    propertiesBuilder.put(DEFAULT_FILE_FORMAT, fileFormat.toString());
    if (tableMetadata.getComment().isPresent()) {
        propertiesBuilder.put(TABLE_COMMENT, tableMetadata.getComment().get());
    }
    TableMetadata metadata = newTableMetadata(schema, partitionSpec, targetPath, propertiesBuilder.build());
    transaction = createTableTransaction(tableName, operations, metadata);
    return new IcebergWritableTableHandle(schemaName, tableName, SchemaParser.toJson(metadata.schema()), PartitionSpecParser.toJson(metadata.spec()), getColumns(metadata.schema(), typeManager), targetPath, fileFormat, metadata.properties());
}
Also used : Path(org.apache.hadoop.fs.Path) TableMetadata(org.apache.iceberg.TableMetadata) TableMetadata.newTableMetadata(org.apache.iceberg.TableMetadata.newTableMetadata) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) Schema(org.apache.iceberg.Schema) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) PrestoException(com.facebook.presto.spi.PrestoException) FileFormat(org.apache.iceberg.FileFormat) IcebergTableProperties.getFileFormat(com.facebook.presto.iceberg.IcebergTableProperties.getFileFormat) SchemaTableName(com.facebook.presto.spi.SchemaTableName) PartitionSpec(org.apache.iceberg.PartitionSpec) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) TableOperations(org.apache.iceberg.TableOperations) Database(com.facebook.presto.hive.metastore.Database) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) HdfsContext(com.facebook.presto.hive.HdfsContext)

Example 69 with MetastoreContext

use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.

the class IcebergHiveMetadata method dropTable.

@Override
public void dropTable(ConnectorSession session, ConnectorTableHandle tableHandle) {
    IcebergTableHandle handle = (IcebergTableHandle) tableHandle;
    // TODO: support path override in Iceberg table creation
    org.apache.iceberg.Table table = getHiveIcebergTable(metastore, hdfsEnvironment, session, handle.getSchemaTableName());
    if (table.properties().containsKey(OBJECT_STORE_PATH) || table.properties().containsKey(WRITE_NEW_DATA_LOCATION) || table.properties().containsKey(WRITE_METADATA_LOCATION)) {
        throw new PrestoException(NOT_SUPPORTED, "Table " + handle.getSchemaTableName() + " contains Iceberg path override properties and cannot be dropped from Presto");
    }
    MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
    metastore.dropTable(metastoreContext, handle.getSchemaName(), handle.getTableName(), true);
}
Also used : MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) PrestoException(com.facebook.presto.spi.PrestoException)

Example 70 with MetastoreContext

use of com.facebook.presto.hive.metastore.MetastoreContext in project presto by prestodb.

the class IcebergHiveMetadata method getTableMetadata.

@Override
protected ConnectorTableMetadata getTableMetadata(ConnectorSession session, SchemaTableName table) {
    MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
    if (!metastore.getTable(metastoreContext, table.getSchemaName(), table.getTableName()).isPresent()) {
        throw new TableNotFoundException(table);
    }
    org.apache.iceberg.Table icebergTable = getHiveIcebergTable(metastore, hdfsEnvironment, session, table);
    List<ColumnMetadata> columns = getColumnMetadatas(icebergTable);
    return new ConnectorTableMetadata(table, columns, createMetadataProperties(icebergTable), getTableComment(icebergTable));
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata)

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