/*
 * 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.Block;
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 javax.validation.constraints.NotNull;

import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.spi.type.StandardTypes.BIGINT;
import static com.facebook.presto.spi.type.StandardTypes.DOUBLE;
import static com.facebook.presto.util.Failures.checkCondition;

@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) {
            checkCondition(buckets >= 2, INVALID_FUNCTION_ARGUMENT, "numeric_histogram bucket count must be greater than one");
            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 {
            Block block = MapType.toStackRepresentation(state.get().getBuckets(), DoubleType.DOUBLE, DoubleType.DOUBLE);
            out.writeObject(block);
            out.closeEntry();
        }
    }
}

@@ -975,23 +975,32 @@
 spi.
-type.DoubleType
+block.BlockBuilderStatus
 ;%0Aim
@@ -1028,16 +1028,23 @@
 sto.
+spi.
 type.
-Map
+Double
 Type
@@ -1175,16 +1175,39 @@
 tNull;%0A%0A
+import java.util.Map;%0A%0A
 import s
@@ -3247,114 +3247,470 @@
     
-Block block = MapType.toStackRepresentation(state.get().getBuckets(), DoubleType.DOUBLE, DoubleType.DOUBLE
+Map%3CDouble, Double%3E value = state.get().getBuckets();%0A            BlockBuilder blockBuilder = DoubleType.DOUBLE.createBlockBuilder(new BlockBuilderStatus(), value.size() * 2);%0A            for (Map.Entry%3CDouble, Double%3E entry : value.entrySet()) %7B%0A                DoubleType.DOUBLE.writeDouble(blockBuilder, entry.getKey());%0A                DoubleType.DOUBLE.writeDouble(blockBuilder, entry.getValue());%0A            %7D%0A            Block block = blockBuilder.build(
 );%0A 

Optimize Block creation in numeric_histogram()