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

io.trino.sql.planner.plan.StatisticAggregationsDescriptor Maven / Gradle / Ivy

There is a newer version: 465
Show 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.sql.planner.plan;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.DoNotCall;
import io.trino.spi.statistics.ColumnStatisticMetadata;
import io.trino.spi.statistics.TableStatisticType;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.util.Objects.requireNonNull;

public class StatisticAggregationsDescriptor
{
    private final Map grouping;
    private final Map tableStatistics;
    private final Map columnStatistics;

    public static  StatisticAggregationsDescriptor empty()
    {
        return StatisticAggregationsDescriptor.builder().build();
    }

    public StatisticAggregationsDescriptor(
            Map grouping,
            Map tableStatistics,
            Map columnStatistics)
    {
        this.grouping = ImmutableMap.copyOf(requireNonNull(grouping, "grouping is null"));
        this.tableStatistics = ImmutableMap.copyOf(requireNonNull(tableStatistics, "tableStatistics is null"));
        this.columnStatistics = ImmutableMap.copyOf(requireNonNull(columnStatistics, "columnStatistics is null"));
    }

    @JsonCreator
    @DoNotCall // for JSON serialization only
    public static  StatisticAggregationsDescriptor fromJson(
            @JsonProperty("grouping") Map grouping,
            @JsonProperty("tableStatistics") Map tableStatistics,
            @JsonProperty("columnStatisticsList") List> columnStatistics)
    {
        return new StatisticAggregationsDescriptor<>(
                grouping,
                tableStatistics,
                columnStatistics.stream()
                        .collect(toImmutableMap(ColumnStatisticAggregationsDescriptor::metadata, ColumnStatisticAggregationsDescriptor::input)));
    }

    @JsonProperty
    public Map getGrouping()
    {
        return grouping;
    }

    @JsonProperty
    public Map getTableStatistics()
    {
        return tableStatistics;
    }

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

    @JsonProperty
    @DoNotCall // for JSON serialization only
    public final List> getColumnStatisticsList()
    {
        return columnStatistics.entrySet().stream()
                .map(entry -> new ColumnStatisticAggregationsDescriptor(entry.getKey(), entry.getValue()))
                .collect(toImmutableList());
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        StatisticAggregationsDescriptor that = (StatisticAggregationsDescriptor) o;
        return Objects.equals(grouping, that.grouping) &&
                Objects.equals(tableStatistics, that.tableStatistics) &&
                Objects.equals(columnStatistics, that.columnStatistics);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(grouping, tableStatistics, columnStatistics);
    }

    @Override
    public String toString()
    {
        return toStringHelper(this)
                .add("grouping", grouping)
                .add("tableStatistics", tableStatistics)
                .add("columnStatistics", columnStatistics)
                .toString();
    }

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

    public  StatisticAggregationsDescriptor map(Function mapper)
    {
        return new StatisticAggregationsDescriptor<>(
                map(this.getGrouping(), mapper),
                map(this.getTableStatistics(), mapper),
                map(this.getColumnStatistics(), mapper));
    }

    private static  Map map(Map input, Function mapper)
    {
        return input.entrySet()
                .stream()
                .collect(toImmutableMap(Map.Entry::getKey, entry -> mapper.apply(entry.getValue())));
    }

    public static class Builder
    {
        private final ImmutableMap.Builder grouping = ImmutableMap.builder();
        private final ImmutableMap.Builder tableStatistics = ImmutableMap.builder();
        private final ImmutableMap.Builder columnStatistics = ImmutableMap.builder();

        public void addGrouping(String column, T key)
        {
            grouping.put(column, key);
        }

        public void addTableStatistic(TableStatisticType type, T key)
        {
            tableStatistics.put(type, key);
        }

        public void addColumnStatistic(ColumnStatisticMetadata statisticMetadata, T key)
        {
            columnStatistics.put(statisticMetadata, key);
        }

        public StatisticAggregationsDescriptor build()
        {
            return new StatisticAggregationsDescriptor<>(grouping.buildOrThrow(), tableStatistics.buildOrThrow(), columnStatistics.buildOrThrow());
        }
    }

    public record ColumnStatisticAggregationsDescriptor(ColumnStatisticMetadata metadata, T input) {}
}