/*
 * 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 io.prestosql.type;

import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.type.AbstractFixedWidthType;
import io.prestosql.spi.type.TypeSignature;

import static com.google.common.base.Preconditions.checkArgument;

public final class UnknownType
        extends AbstractFixedWidthType
{
    public static final UnknownType UNKNOWN = new UnknownType();
    public static final String NAME = "unknown";

    private UnknownType()
    {
        // We never access the native container for UNKNOWN because its null check is always true.
        // The actual native container type does not matter here.
        // We choose boolean to represent UNKNOWN because it's the smallest primitive type.
        super(new TypeSignature(NAME), boolean.class, 1);
    }

    @Override
    public boolean isComparable()
    {
        return true;
    }

    @Override
    public boolean isOrderable()
    {
        return true;
    }

    @Override
    public long hash(Block block, int position)
    {
        // Check that the position is valid
        checkArgument(block.isNull(position), "Expected NULL value for UnknownType");
        return 0;
    }

    @Override
    public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
    {
        // Check that the position is valid
        checkArgument(leftBlock.isNull(leftPosition), "Expected NULL value for UnknownType");
        checkArgument(rightBlock.isNull(rightPosition), "Expected NULL value for UnknownType");
        return true;
    }

    @Override
    public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
    {
        // Check that the position is valid
        checkArgument(leftBlock.isNull(leftPosition), "Expected NULL value for UnknownType");
        checkArgument(rightBlock.isNull(rightPosition), "Expected NULL value for UnknownType");
        return 0;
    }

    @Override
    public Object getObjectValue(ConnectorSession session, Block block, int position)
    {
        // call is null in case position is out of bounds
        checkArgument(block.isNull(position), "Expected NULL value for UnknownType");
        return null;
    }

    @Override
    public void appendTo(Block block, int position, BlockBuilder blockBuilder)
    {
        blockBuilder.appendNull();
    }

    @Override
    public boolean getBoolean(Block block, int position)
    {
        // Ideally, this function should never be invoked for unknown type.
        // However, some logic rely on having a default value before the null check.
        checkArgument(block.isNull(position));
        return false;
    }

    @Deprecated
    @Override
    public void writeBoolean(BlockBuilder blockBuilder, boolean value)
    {
        // Ideally, this function should never be invoked for unknown type.
        // However, some logic (e.g. AbstractMinMaxBy) rely on writing a default value before the null check.
        checkArgument(!value);
        blockBuilder.appendNull();
    }
}

@@ -664,16 +664,168 @@
 uilder;%0A
+import io.prestosql.spi.block.BlockBuilderStatus;%0Aimport io.prestosql.spi.block.ByteArrayBlockBuilder;%0Aimport io.prestosql.spi.block.PageBuilderStatus;%0A
 import i
@@ -897,32 +897,67 @@
 pi.type.Abstract
+Type;%0Aimport io.prestosql.spi.type.
 FixedWidthType;%0A
@@ -1123,16 +1123,40 @@
 Abstract
+Type%0A        implements 
 FixedWid
@@ -1624,11 +1624,1060 @@
 lass
-, 1
+);%0A    %7D%0A%0A    @Override%0A    public int getFixedSize()%0A    %7B%0A        return Byte.BYTES;%0A    %7D%0A%0A    @Override%0A    public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)%0A    %7B%0A        int maxBlockSizeInBytes;%0A        if (blockBuilderStatus == null) %7B%0A            maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;%0A        %7D%0A        else %7B%0A            maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();%0A        %7D%0A        return new ByteArrayBlockBuilder(%0A                blockBuilderStatus,%0A                Math.min(expectedEntries, maxBlockSizeInBytes / getFixedSize()));%0A    %7D%0A%0A    @Override%0A    public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries)%0A    %7B%0A        return createBlockBuilder(blockBuilderStatus, expectedEntries, getFixedSize());%0A    %7D%0A%0A    @Override%0A    public BlockBuilder createFixedSizeBlockBuilder(int positionCount)%0A    %7B%0A        return new ByteArrayBlockBuilder(null, positionCount
 );%0A 

Change UnknownType to use ByteArrayBlock