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

io.trino.metastore.Table Maven / Gradle / Ivy

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.metastore;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.ColumnNotFoundException;
import io.trino.spi.connector.SchemaTableName;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import static com.google.common.base.MoreObjects.toStringHelper;
import static io.trino.metastore.MetastoreErrorCode.HIVE_INVALID_METADATA;
import static io.trino.spi.StandardErrorCode.ALREADY_EXISTS;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.security.PrincipalType.USER;
import static java.util.Objects.requireNonNull;

@Immutable
public class Table
{
    public static final String TABLE_COMMENT = "comment";

    private final String databaseName;
    private final String tableName;
    private final Optional owner;
    private final String tableType; // This is not an enum because some Hive implementations define additional table types.
    private final List dataColumns;
    private final List partitionColumns;
    private final Storage storage;
    private final Map parameters;
    private final Optional viewOriginalText;
    private final Optional viewExpandedText;
    private final OptionalLong writeId;

    @JsonCreator
    public Table(
            @JsonProperty("databaseName") String databaseName,
            @JsonProperty("tableName") String tableName,
            @JsonProperty("owner") Optional owner,
            @JsonProperty("tableType") String tableType,
            @JsonProperty("storage") Storage storage,
            @JsonProperty("dataColumns") List dataColumns,
            @JsonProperty("partitionColumns") List partitionColumns,
            @JsonProperty("parameters") Map parameters,
            @JsonProperty("viewOriginalText") Optional viewOriginalText,
            @JsonProperty("viewExpandedText") Optional viewExpandedText,
            @JsonProperty("writeId") OptionalLong writeId)
    {
        this.databaseName = requireNonNull(databaseName, "databaseName is null");
        this.tableName = requireNonNull(tableName, "tableName is null");
        this.owner = requireNonNull(owner, "owner is null");
        this.tableType = requireNonNull(tableType, "tableType is null");
        this.storage = requireNonNull(storage, "storage is null");
        this.dataColumns = ImmutableList.copyOf(requireNonNull(dataColumns, "dataColumns is null"));
        this.partitionColumns = ImmutableList.copyOf(requireNonNull(partitionColumns, "partitionColumns is null"));
        this.parameters = ImmutableMap.copyOf(requireNonNull(parameters, "parameters is null"));
        this.viewOriginalText = requireNonNull(viewOriginalText, "viewOriginalText is null");
        this.viewExpandedText = requireNonNull(viewExpandedText, "viewExpandedText is null");
        this.writeId = requireNonNull(writeId, "writeId is null");
    }

    @JsonProperty
    public String getDatabaseName()
    {
        return databaseName;
    }

    @JsonProperty
    public String getTableName()
    {
        return tableName;
    }

    @JsonIgnore
    public SchemaTableName getSchemaTableName()
    {
        return new SchemaTableName(databaseName, tableName);
    }

    @JsonProperty
    public Optional getOwner()
    {
        return owner;
    }

    @JsonProperty
    public String getTableType()
    {
        return tableType;
    }

    @JsonProperty
    public List getDataColumns()
    {
        return dataColumns;
    }

    @JsonProperty
    public List getPartitionColumns()
    {
        return partitionColumns;
    }

    public Optional getColumn(String name)
    {
        return Stream.concat(partitionColumns.stream(), dataColumns.stream())
                .filter(column -> column.getName().equals(name))
                .findFirst();
    }

    @JsonProperty
    public Storage getStorage()
    {
        return storage;
    }

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

    @JsonProperty
    public Optional getViewOriginalText()
    {
        return viewOriginalText;
    }

    @JsonProperty
    public Optional getViewExpandedText()
    {
        return viewExpandedText;
    }

    @JsonProperty
    public OptionalLong getWriteId()
    {
        return writeId;
    }

    public Table withComment(Optional comment)
    {
        Map parameters = new LinkedHashMap<>(this.parameters);
        comment.ifPresentOrElse(
                value -> parameters.put(TABLE_COMMENT, value),
                () -> parameters.remove(TABLE_COMMENT));

        return builder(this)
                .setParameters(parameters)
                .build();
    }

    public Table withOwner(HivePrincipal principal)
    {
        // TODO Add role support https://github.com/trinodb/trino/issues/5706
        if (principal.getType() != USER) {
            throw new TrinoException(NOT_SUPPORTED, "Setting table owner type as a role is not supported");
        }

        return builder(this)
                .setOwner(Optional.of(principal.getName()))
                .build();
    }

    public Table withColumnComment(String columnName, Optional columnComment)
    {
        boolean found = false;
        ImmutableList.Builder newDataColumns = ImmutableList.builder();
        for (Column dataColumn : dataColumns) {
            if (dataColumn.getName().equals(columnName)) {
                if (found) {
                    throw new TrinoException(HIVE_INVALID_METADATA, "Table %s.%s has multiple columns named %s".formatted(databaseName, tableName, columnName));
                }
                dataColumn = dataColumn.withComment(columnComment);
                found = true;
            }
            newDataColumns.add(dataColumn);
        }

        ImmutableList.Builder newPartitionColumns = ImmutableList.builder();
        for (Column partitionColumn : partitionColumns) {
            if (partitionColumn.getName().equals(columnName)) {
                if (found) {
                    throw new TrinoException(HIVE_INVALID_METADATA, "Table %s.%s has multiple columns named %s".formatted(databaseName, tableName, columnName));
                }
                partitionColumn = partitionColumn.withComment(columnComment);
                found = true;
            }
            newPartitionColumns.add(partitionColumn);
        }

        if (!found) {
            throw new ColumnNotFoundException(getSchemaTableName(), columnName);
        }

        return builder(this)
                .setDataColumns(newDataColumns.build())
                .setPartitionColumns(newPartitionColumns.build())
                .build();
    }

    public Table withAddColumn(Column newColumn)
    {
        if (dataColumns.stream().map(Column::getName).anyMatch(columnName -> columnName.equals(newColumn.getName())) ||
                partitionColumns.stream().map(Column::getName).anyMatch(columnName -> columnName.equals(newColumn.getName()))) {
            throw new TrinoException(ALREADY_EXISTS, "Table %s.%s already has a column named %s".formatted(databaseName, tableName, newColumn.getName()));
        }
        return builder(this)
                .addDataColumn(newColumn)
                .build();
    }

    public Table withDropColumn(String columnName)
    {
        if (partitionColumns.stream().map(Column::getName).anyMatch(columnName::equals)) {
            throw new TrinoException(NOT_SUPPORTED, "Cannot drop partition columns");
        }
        if (storage.getBucketProperty().stream().flatMap(bucket -> bucket.bucketedBy().stream()).anyMatch(columnName::equals) ||
                storage.getBucketProperty().stream().flatMap(bucket -> bucket.sortedBy().stream()).map(SortingColumn::columnName).anyMatch(columnName::equals)) {
            throw new TrinoException(NOT_SUPPORTED, "Cannot drop bucket column %s.%s.%s".formatted(databaseName, tableName, columnName));
        }

        boolean found = false;
        ImmutableList.Builder newDataColumns = ImmutableList.builder();
        for (Column dataColumn : dataColumns) {
            if (dataColumn.getName().equals(columnName)) {
                found = true;
            }
            else {
                newDataColumns.add(dataColumn);
            }
        }
        if (!found) {
            throw new ColumnNotFoundException(getSchemaTableName(), columnName);
        }
        List dataColumns = newDataColumns.build();
        if (dataColumns.isEmpty()) {
            throw new TrinoException(NOT_SUPPORTED, "Cannot drop the only non-partition column in a table");
        }
        return builder(this)
                .setDataColumns(dataColumns)
                .build();
    }

    public Table withRenameColumn(String oldColumnName, String newColumnName)
    {
        if (partitionColumns.stream().map(Column::getName).anyMatch(oldColumnName::equals)) {
            throw new TrinoException(NOT_SUPPORTED, "Renaming partition columns is not supported");
        }

        if (dataColumns.stream().map(Column::getName).anyMatch(newColumnName::equals) ||
                partitionColumns.stream().map(Column::getName).anyMatch(newColumnName::equals)) {
            throw new TrinoException(ALREADY_EXISTS, "Table %s.%s already has a column named %s".formatted(databaseName, tableName, newColumnName));
        }

        boolean found = false;
        ImmutableList.Builder newDataColumns = ImmutableList.builder();
        for (Column dataColumn : dataColumns) {
            if (dataColumn.getName().equals(oldColumnName)) {
                newDataColumns.add(dataColumn.withName(newColumnName));
                found = true;
            }
            else {
                newDataColumns.add(dataColumn);
            }
        }
        if (!found) {
            throw new ColumnNotFoundException(getSchemaTableName(), oldColumnName);
        }

        Builder builder = builder(this)
                .setDataColumns(newDataColumns.build());

        if (storage.getBucketProperty().isPresent()) {
            HiveBucketProperty bucketProperty = storage.getBucketProperty().get();

            ImmutableList.Builder newBucketColumns = ImmutableList.builder();
            for (String bucketColumn : bucketProperty.bucketedBy()) {
                if (bucketColumn.equals(oldColumnName)) {
                    newBucketColumns.add(newColumnName);
                }
                else {
                    newBucketColumns.add(bucketColumn);
                }
            }

            ImmutableList.Builder newSortedColumns = ImmutableList.builder();
            for (SortingColumn sortingColumn : bucketProperty.sortedBy()) {
                if (sortingColumn.columnName().equals(oldColumnName)) {
                    newSortedColumns.add(new SortingColumn(newColumnName, sortingColumn.order()));
                }
                else {
                    newSortedColumns.add(sortingColumn);
                }
            }

            builder.getStorageBuilder().setBucketProperty(new HiveBucketProperty(newBucketColumns.build(), bucketProperty.bucketCount(), newSortedColumns.build()));
        }
        return builder.build();
    }

    public Table withParameters(Map newParameters)
    {
        return builder(this)
                .setParameters(newParameters)
                .build();
    }

    public static Builder builder()
    {
        return new Builder();
    }

    public static Builder builder(Table table)
    {
        return new Builder(table);
    }

    @Override
    public String toString()
    {
        return toStringHelper(this)
                .add("databaseName", databaseName)
                .add("tableName", tableName)
                .add("owner", owner)
                .add("tableType", tableType)
                .add("dataColumns", dataColumns)
                .add("partitionColumns", partitionColumns)
                .add("storage", storage)
                .add("parameters", parameters)
                .add("viewOriginalText", viewOriginalText)
                .add("viewExpandedText", viewExpandedText)
                .toString();
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Table table = (Table) o;
        return Objects.equals(databaseName, table.databaseName) &&
                Objects.equals(tableName, table.tableName) &&
                Objects.equals(owner, table.owner) &&
                Objects.equals(tableType, table.tableType) &&
                Objects.equals(dataColumns, table.dataColumns) &&
                Objects.equals(partitionColumns, table.partitionColumns) &&
                Objects.equals(storage, table.storage) &&
                Objects.equals(parameters, table.parameters) &&
                Objects.equals(viewOriginalText, table.viewOriginalText) &&
                Objects.equals(viewExpandedText, table.viewExpandedText);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(
                databaseName,
                tableName,
                owner,
                tableType,
                dataColumns,
                partitionColumns,
                storage,
                parameters,
                viewOriginalText,
                viewExpandedText);
    }

    public static class Builder
    {
        private final Storage.Builder storageBuilder;
        private String databaseName;
        private String tableName;
        private Optional owner;
        private String tableType;
        private List dataColumns = new ArrayList<>();
        private List partitionColumns = new ArrayList<>();
        private Map parameters = new LinkedHashMap<>();
        private Optional viewOriginalText = Optional.empty();
        private Optional viewExpandedText = Optional.empty();
        private OptionalLong writeId = OptionalLong.empty();

        private Builder()
        {
            storageBuilder = Storage.builder();
        }

        private Builder(Table table)
        {
            databaseName = table.databaseName;
            tableName = table.tableName;
            owner = table.owner;
            tableType = table.tableType;
            storageBuilder = Storage.builder(table.getStorage());
            dataColumns = new ArrayList<>(table.dataColumns);
            partitionColumns = new ArrayList<>(table.partitionColumns);
            parameters = new LinkedHashMap<>(table.parameters);
            viewOriginalText = table.viewOriginalText;
            viewExpandedText = table.viewExpandedText;
            writeId = table.writeId;
        }

        public Builder setDatabaseName(String databaseName)
        {
            this.databaseName = databaseName;
            return this;
        }

        public Builder setTableName(String tableName)
        {
            this.tableName = tableName;
            return this;
        }

        public Builder setOwner(Optional owner)
        {
            this.owner = owner;
            return this;
        }

        public Builder setTableType(String tableType)
        {
            this.tableType = tableType;
            return this;
        }

        public Storage.Builder getStorageBuilder()
        {
            return storageBuilder;
        }

        public Builder setDataColumns(List dataColumns)
        {
            this.dataColumns = new ArrayList<>(dataColumns);
            return this;
        }

        public Builder addDataColumn(Column dataColumn)
        {
            this.dataColumns.add(dataColumn);
            return this;
        }

        public Builder setPartitionColumns(List partitionColumns)
        {
            this.partitionColumns = new ArrayList<>(partitionColumns);
            return this;
        }

        public Builder setParameters(Map parameters)
        {
            this.parameters = new LinkedHashMap<>(parameters);
            return this;
        }

        public Builder setParameter(String key, String value)
        {
            this.parameters.put(key, value);
            return this;
        }

        public Builder setParameter(String key, Optional value)
        {
            if (value.isEmpty()) {
                this.parameters.remove(key);
            }
            else {
                this.parameters.put(key, value.get());
            }
            return this;
        }

        public Builder setViewOriginalText(Optional viewOriginalText)
        {
            this.viewOriginalText = viewOriginalText;
            return this;
        }

        public Builder setViewExpandedText(Optional viewExpandedText)
        {
            this.viewExpandedText = viewExpandedText;
            return this;
        }

        public Builder setWriteId(OptionalLong writeId)
        {
            this.writeId = writeId;
            return this;
        }

        public Builder withStorage(Consumer consumer)
        {
            consumer.accept(storageBuilder);
            return this;
        }

        public Builder apply(Function function)
        {
            return function.apply(this);
        }

        public Table build()
        {
            return new Table(
                    databaseName,
                    tableName,
                    owner,
                    tableType,
                    storageBuilder.build(),
                    dataColumns,
                    partitionColumns,
                    parameters,
                    viewOriginalText,
                    viewExpandedText,
                    writeId);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy