io.trino.plugin.tpcds.statistics.TpcdsTableStatisticsFactory Maven / Gradle / Ivy
/*
* 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.tpcds.statistics;
import io.trino.plugin.tpcds.TpcdsColumnHandle;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.statistics.ColumnStatistics;
import io.trino.spi.statistics.DoubleRange;
import io.trino.spi.statistics.Estimate;
import io.trino.spi.statistics.TableStatistics;
import io.trino.spi.type.CharType;
import io.trino.spi.type.DecimalType;
import io.trino.spi.type.Decimals;
import io.trino.spi.type.Int128;
import io.trino.spi.type.TimeType;
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;
import io.trino.tpcds.Table;
import java.time.LocalDate;
import java.util.Map;
import java.util.Optional;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.DateType.DATE;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.spi.type.IntegerType.INTEGER;
import static java.lang.Double.parseDouble;
public class TpcdsTableStatisticsFactory
{
private final TableStatisticsDataRepository statisticsDataRepository = new TableStatisticsDataRepository();
public TableStatistics create(String schemaName, Table table, Map columnHandles)
{
Optional statisticsDataOptional = statisticsDataRepository.load(schemaName, table);
return statisticsDataOptional.map(statisticsData -> toTableStatistics(columnHandles, statisticsData))
.orElse(TableStatistics.empty());
}
private TableStatistics toTableStatistics(Map columnHandles, TableStatisticsData statisticsData)
{
long rowCount = statisticsData.rowCount();
TableStatistics.Builder tableStatistics = TableStatistics.builder()
.setRowCount(Estimate.of(rowCount));
if (rowCount > 0) {
Map columnsData = statisticsData.columns();
for (Map.Entry entry : columnHandles.entrySet()) {
TpcdsColumnHandle columnHandle = (TpcdsColumnHandle) entry.getValue();
tableStatistics.setColumnStatistics(entry.getValue(), toColumnStatistics(columnsData.get(entry.getKey()), columnHandle.type(), rowCount));
}
}
return tableStatistics.build();
}
private ColumnStatistics toColumnStatistics(ColumnStatisticsData columnStatisticsData, Type type, long rowCount)
{
ColumnStatistics.Builder columnStatistics = ColumnStatistics.builder();
long nullCount = columnStatisticsData.nullsCount();
columnStatistics.setNullsFraction(Estimate.of((double) nullCount / rowCount));
columnStatistics.setRange(toRange(columnStatisticsData.min(), columnStatisticsData.max(), type));
columnStatistics.setDistinctValuesCount(Estimate.of(columnStatisticsData.distinctValuesCount()));
columnStatistics.setDataSize(columnStatisticsData.dataSize().map(Estimate::of).orElse(Estimate.unknown()));
return columnStatistics.build();
}
private static Optional toRange(Optional