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

import com.facebook.presto.metadata.OperatorType;
import com.facebook.presto.operator.Description;
import com.facebook.presto.operator.aggregation.TypedSet;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.block.BlockBuilderStatus;
import com.facebook.presto.spi.type.StandardTypes;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.type.SqlType;

import java.lang.invoke.MethodHandle;

@ScalarFunction("array_distinct")
@Description("Remove duplicate values from the given array")
public final class ArrayDistinctFunction
{
    private ArrayDistinctFunction() {}

    @TypeParameter("E")
    @SqlType("array(E)")
    public static Block distinct(
            @OperatorDependency(operator = OperatorType.EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = {"E", "E"}) MethodHandle equals,
            @TypeParameter("E") Type type,
            @SqlType("array(E)") Block array)
    {
        if (array.getPositionCount() < 2) {
            return array;
        }

        if (array.getPositionCount() == 2) {
            if (type.equalTo(array, 0, array, 1)) {
                return array.getSingleValueBlock(0);
            }
            else {
                return array;
            }
        }

        TypedSet typedSet = new TypedSet(type, array.getPositionCount());
        BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), array.getPositionCount());
        for (int i = 0; i < array.getPositionCount(); i++) {
            if (!typedSet.contains(array, i)) {
                typedSet.add(array, i);
                type.appendTo(array, i, distinctElementBlockBuilder);
            }
        }

        return distinctElementBlockBuilder.build();
    }
}

@@ -1048,45 +1048,204 @@
 pe;%0A
-%0Aimport java.lang.invoke.MethodHandle
+import it.unimi.dsi.fastutil.longs.LongOpenHashSet;%0Aimport it.unimi.dsi.fastutil.longs.LongSet;%0A%0Aimport java.lang.invoke.MethodHandle;%0A%0Aimport static com.facebook.presto.spi.type.BigintType.BIGINT
 ;%0A%0A@
@@ -2504,32 +2504,952 @@
    %7D%0A        %7D%0A%0A
+        return distinctElementBlockBuilder.build();%0A    %7D%0A%0A    @SqlType(%22array(bigint)%22)%0A    public static Block bigintDistinct(@SqlType(%22array(bigint)%22) Block array)%0A    %7B%0A        if (array.getPositionCount() == 0) %7B%0A            return array;%0A        %7D%0A%0A        boolean containsNull = false;%0A        LongSet set = new LongOpenHashSet(array.getPositionCount());%0A        BlockBuilder distinctElementBlockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), array.getPositionCount());%0A        for (int i = 0; i %3C array.getPositionCount(); i++) %7B%0A            if (!containsNull && array.isNull(i)) %7B%0A                containsNull = true;%0A                distinctElementBlockBuilder.appendNull();%0A                continue;%0A            %7D%0A            long value = BIGINT.getLong(array, i);%0A            if (set.add(value)) %7B%0A                BIGINT.writeLong(distinctElementBlockBuilder, value);%0A            %7D%0A        %7D%0A%0A
         return d

Add optimized array_distinct for array<bigint>