All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.plugin.hive.metastore.file.PartitionMetadata Maven / Gradle / Ivy

Go to download

This is a Databricks build of Trino's Hive plugin which includes support for HTTP based transport for it's Hive metastore thrift interface.

The newest version!
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.plugin.hive.metastore.file;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.trino.plugin.hive.HiveBucketProperty;
import io.trino.plugin.hive.HiveStorageFormat;
import io.trino.plugin.hive.PartitionStatistics;
import io.trino.plugin.hive.metastore.Column;
import io.trino.plugin.hive.metastore.HiveColumnStatistics;
import io.trino.plugin.hive.metastore.Partition;
import io.trino.plugin.hive.metastore.PartitionWithStatistics;
import io.trino.plugin.hive.metastore.Storage;
import io.trino.plugin.hive.metastore.StorageFormat;
import io.trino.plugin.hive.metastore.Table;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.trino.plugin.hive.TableType.EXTERNAL_TABLE;
import static io.trino.plugin.hive.metastore.StorageFormat.VIEW_STORAGE_FORMAT;
import static io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.updateStatisticsParameters;
import static java.util.Objects.requireNonNull;

public class PartitionMetadata
{
    private final List columns;
    private final Map parameters;

    private final Optional storageFormat;
    private final Optional bucketProperty;
    private final Map serdeParameters;

    private final Optional externalLocation;

    private final Map columnStatistics;

    @JsonCreator
    public PartitionMetadata(
            @JsonProperty("columns") List columns,
            @JsonProperty("parameters") Map parameters,
            @JsonProperty("storageFormat") Optional storageFormat,
            @JsonProperty("bucketProperty") Optional bucketProperty,
            @JsonProperty("serdeParameters") Map serdeParameters,
            @JsonProperty("externalLocation") Optional externalLocation,
            @JsonProperty("columnStatistics") Map columnStatistics)
    {
        this.columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null"));
        this.parameters = ImmutableMap.copyOf(requireNonNull(parameters, "parameters is null"));

        this.storageFormat = requireNonNull(storageFormat, "storageFormat is null");
        this.bucketProperty = requireNonNull(bucketProperty, "bucketProperty is null");
        this.serdeParameters = requireNonNull(serdeParameters, "serdeParameters is null");

        this.externalLocation = requireNonNull(externalLocation, "externalLocation is null");
        this.columnStatistics = ImmutableMap.copyOf(requireNonNull(columnStatistics, "columnStatistics is null"));
    }

    public PartitionMetadata(Table table, PartitionWithStatistics partitionWithStatistics)
    {
        Partition partition = partitionWithStatistics.getPartition();
        PartitionStatistics statistics = partitionWithStatistics.getStatistics();

        this.columns = partition.getColumns();
        this.parameters = updateStatisticsParameters(partition.getParameters(), statistics.getBasicStatistics());

        StorageFormat tableFormat = partition.getStorage().getStorageFormat();
        storageFormat = Arrays.stream(HiveStorageFormat.values())
                .filter(format -> tableFormat.equals(StorageFormat.fromHiveStorageFormat(format)))
                .findFirst();

        if (table.getTableType().equals(EXTERNAL_TABLE.name())) {
            externalLocation = Optional.of(partition.getStorage().getLocation());
        }
        else {
            externalLocation = Optional.empty();
        }

        bucketProperty = partition.getStorage().getBucketProperty();
        serdeParameters = partition.getStorage().getSerdeParameters();
        columnStatistics = ImmutableMap.copyOf(statistics.getColumnStatistics());
    }

    @JsonProperty
    public List getColumns()
    {
        return columns;
    }

    @JsonProperty
    public Map getParameters()
    {
        return parameters;
    }

    @JsonProperty
    public Optional getStorageFormat()
    {
        return storageFormat;
    }

    @JsonProperty
    public Optional getBucketProperty()
    {
        return bucketProperty;
    }

    @JsonProperty
    public Map getSerdeParameters()
    {
        return serdeParameters;
    }

    @JsonProperty
    public Optional getExternalLocation()
    {
        return externalLocation;
    }

    @JsonProperty
    public Map getColumnStatistics()
    {
        return columnStatistics;
    }

    public PartitionMetadata withParameters(Map parameters)
    {
        return new PartitionMetadata(columns, parameters, storageFormat, bucketProperty, serdeParameters, externalLocation, columnStatistics);
    }

    public PartitionMetadata withColumnStatistics(Map columnStatistics)
    {
        return new PartitionMetadata(columns, parameters, storageFormat, bucketProperty, serdeParameters, externalLocation, columnStatistics);
    }

    public Partition toPartition(String databaseName, String tableName, List values, String location)
    {
        return new Partition(
                databaseName,
                tableName,
                values,
                Storage.builder()
                        .setLocation(externalLocation.orElse(location))
                        .setStorageFormat(storageFormat.map(StorageFormat::fromHiveStorageFormat).orElse(VIEW_STORAGE_FORMAT))
                        .setBucketProperty(bucketProperty)
                        .setSerdeParameters(serdeParameters)
                        .build(),
                columns,
                parameters);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy