/*
 * 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.operator.aggregation;

import com.facebook.presto.operator.aggregation.state.AccumulatorState;
import com.facebook.presto.operator.aggregation.state.AccumulatorStateMetadata;
import com.facebook.presto.operator.aggregation.state.NumericHistogramStateSerializer;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.type.DoubleType;
import com.facebook.presto.type.MapType;
import com.facebook.presto.type.SqlType;
import com.google.common.primitives.Ints;
import io.airlift.slice.Slice;

import javax.validation.constraints.NotNull;

import static com.facebook.presto.spi.type.StandardTypes.BIGINT;
import static com.facebook.presto.spi.type.StandardTypes.DOUBLE;

@AggregationFunction("numeric_histogram")
public class NumericHistogramAggregation
{
    public static final int ENTRY_BUFFER_SIZE = 100;

    private NumericHistogramAggregation()
    {
    }

    @AccumulatorStateMetadata(stateSerializerClass = NumericHistogramStateSerializer.class, stateFactoryClass = NumericHistogramStateFactory.class)
    public interface State
            extends AccumulatorState
    {
        @NotNull
        NumericHistogram get();
        void set(NumericHistogram value);
    }

    @InputFunction
    public static void add(State state, @SqlType(BIGINT) long buckets, @SqlType(DOUBLE) double value, @SqlType(DOUBLE) double weight)
    {
        NumericHistogram histogram = state.get();
        if (histogram == null) {
            histogram = new NumericHistogram(Ints.checkedCast(buckets), ENTRY_BUFFER_SIZE);
            state.set(histogram);
        }

        histogram.add(value, weight);
    }

    @InputFunction
    public static void add(State state, @SqlType(BIGINT) long buckets, @SqlType(DOUBLE) double value)
    {
        add(state, buckets, value, 1);
    }

    @CombineFunction
    public static void merge(State state, State other)
    {
        NumericHistogram input = other.get();
        NumericHistogram previous = state.get();

        if (previous == null) {
            state.set(input);
        }
        else {
            previous.mergeWith(input);
        }
    }

    @OutputFunction("map<double,double>")
    public static void output(State state, BlockBuilder out)
    {
        if (state.get() == null) {
            out.appendNull();
        }
        else {
            Slice slice = MapType.toStackRepresentation(state.get().getBuckets(), DoubleType.DOUBLE, DoubleType.DOUBLE);
            out.writeBytes(slice, 0, slice.length());
            out.closeEntry();
        }
    }
}

@@ -1146,16 +1146,99 @@
 tNull;%0A%0A
+import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;%0A
 import s
@@ -1358,16 +1358,80 @@
 .DOUBLE;
+%0Aimport static com.facebook.presto.util.Failures.checkCondition;
 %0A%0A@Aggre
@@ -2168,32 +2168,160 @@
 gram == null) %7B%0A
+            checkCondition(buckets %3E= 2, INVALID_FUNCTION_ARGUMENT, %22numeric_histogram bucket count must be greater than one%22);%0A
             hist

Categorize errors in numeric_histogram