ShortDecimalStatisticsBuilder.java

/*
 * 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.orc.metadata.statistics;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Optional;

public class ShortDecimalStatisticsBuilder
        implements LongValueStatisticsBuilder
{
    public static final long SHORT_DECIMAL_VALUE_BYTES = 8L;

    private final int scale;

    private long nonNullValueCount;
    private long storageSize;
    private long rawSize;
    private long minimum = Long.MAX_VALUE;
    private long maximum = Long.MIN_VALUE;

    public ShortDecimalStatisticsBuilder(int scale)
    {
        this.scale = scale;
    }

    @Override
    public void addValue(long value)
    {
        nonNullValueCount++;

        minimum = Math.min(value, minimum);
        maximum = Math.max(value, maximum);
    }

    private Optional<DecimalStatistics> buildDecimalStatistics()
    {
        if (nonNullValueCount == 0) {
            return Optional.empty();
        }
        return Optional.of(new DecimalStatistics(
                new BigDecimal(BigInteger.valueOf(minimum), scale),
                new BigDecimal(BigInteger.valueOf(maximum), scale),
                SHORT_DECIMAL_VALUE_BYTES));
    }

    @Override
    public ColumnStatistics buildColumnStatistics()
    {
        Optional<DecimalStatistics> decimalStatistics = buildDecimalStatistics();
        if (decimalStatistics.isPresent()) {
            return new DecimalColumnStatistics(nonNullValueCount, null, rawSize, storageSize, decimalStatistics.get());
        }
        return new ColumnStatistics(nonNullValueCount, null, rawSize, storageSize);
    }

    @Override
    public void incrementRawSize(long rawSize)
    {
        this.rawSize += rawSize;
    }

    @Override
    public void incrementSize(long storageSize)
    {
        this.storageSize += storageSize;
    }
}