com.facebook.presto.hive.HiveStatisticsUtil 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 com.facebook.presto.hive;
import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.hive.metastore.HiveColumnStatistics;
import com.facebook.presto.hive.metastore.PartitionStatistics;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.statistics.ColumnStatisticMetadata;
import com.facebook.presto.spi.statistics.ComputedStatistics;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableMap;
import org.joda.time.DateTimeZone;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.hive.metastore.Statistics.fromComputedStatistics;
import static com.facebook.presto.spi.statistics.TableStatisticType.ROW_COUNT;
import static com.google.common.base.Verify.verify;
public final class HiveStatisticsUtil
{
private HiveStatisticsUtil()
{
}
public static PartitionStatistics createPartitionStatistics(
ConnectorSession session,
HiveBasicStatistics basicStatistics,
Map columnTypes,
Map computedColumnStatistics,
DateTimeZone timeZone)
{
long rowCount = basicStatistics.getRowCount().orElseThrow(() -> new IllegalArgumentException("rowCount not present"));
Map columnStatistics = fromComputedStatistics(
session,
timeZone,
computedColumnStatistics,
columnTypes,
rowCount);
return new PartitionStatistics(basicStatistics, columnStatistics);
}
public static PartitionStatistics createPartitionStatistics(
ConnectorSession session,
Map columnTypes,
ComputedStatistics computedStatistics,
DateTimeZone timeZone)
{
Map computedColumnStatistics = computedStatistics.getColumnStatistics();
Block rowCountBlock = Optional.ofNullable(computedStatistics.getTableStatistics().get(ROW_COUNT))
.orElseThrow(() -> new VerifyException("rowCount not present"));
verify(!rowCountBlock.isNull(0), "rowCount must never be null");
long rowCount = BIGINT.getLong(rowCountBlock, 0);
HiveBasicStatistics rowCountOnlyBasicStatistics = new HiveBasicStatistics(OptionalLong.empty(), OptionalLong.of(rowCount), OptionalLong.empty(), OptionalLong.empty());
return createPartitionStatistics(session, rowCountOnlyBasicStatistics, columnTypes, computedColumnStatistics, timeZone);
}
public static Map getColumnStatistics(Map, ComputedStatistics> statistics, List partitionValues)
{
return Optional.ofNullable(statistics.get(partitionValues))
.map(ComputedStatistics::getColumnStatistics)
.orElse(ImmutableMap.of());
}
// TODO: Collect file count, on-disk size and in-memory size during ANALYZE
/**
* This method updates old {@link PartitionStatistics} with new statistics, only if the new
* partition stats are not empty. This method always overwrites each of the
* {@link HiveColumnStatistics} contained in the new partition statistics.
*
* @param oldPartitionStats old version of partition statistics
* @param newPartitionStats new version of partition statistics
* @return updated partition statistics
*/
public static PartitionStatistics updatePartitionStatistics(PartitionStatistics oldPartitionStats, PartitionStatistics newPartitionStats)
{
HiveBasicStatistics oldBasicStatistics = oldPartitionStats.getBasicStatistics();
HiveBasicStatistics newBasicStatistics = newPartitionStats.getBasicStatistics();
HiveBasicStatistics updatedBasicStatistics = new HiveBasicStatistics(
firstPresent(newBasicStatistics.getFileCount(), oldBasicStatistics.getFileCount()),
firstPresent(newBasicStatistics.getRowCount(), oldBasicStatistics.getRowCount()),
firstPresent(newBasicStatistics.getInMemoryDataSizeInBytes(), oldBasicStatistics.getInMemoryDataSizeInBytes()),
firstPresent(newBasicStatistics.getOnDiskDataSizeInBytes(), oldBasicStatistics.getOnDiskDataSizeInBytes()));
return new PartitionStatistics(updatedBasicStatistics, newPartitionStats.getColumnStatistics());
}
private static OptionalLong firstPresent(OptionalLong first, OptionalLong second)
{
return first.isPresent() ? first : second;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy