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

import com.facebook.presto.byteCode.ByteCodeNode;
import com.facebook.presto.byteCode.MethodGenerationContext;
import com.facebook.presto.byteCode.expression.ByteCodeExpression;
import com.facebook.presto.byteCode.instruction.InvokeInstruction;
import com.facebook.presto.spi.type.Type;
import com.google.common.collect.ImmutableList;

import java.lang.reflect.Method;
import java.util.List;

import static com.facebook.presto.byteCode.ParameterizedType.type;
import static com.facebook.presto.sql.gen.Bootstrap.BOOTSTRAP_METHOD;
import static com.google.common.base.Preconditions.checkNotNull;

public class SqlTypeByteCodeExpression
        extends ByteCodeExpression
{
    public static ByteCodeExpression constantType(CallSiteBinder callSiteBinder, Type type)
    {
        checkNotNull(callSiteBinder, "callSiteBinder is null");
        checkNotNull(type, "type is null");

        Binding binding = callSiteBinder.bind(type, Type.class);
        return new SqlTypeByteCodeExpression(type, binding, BOOTSTRAP_METHOD);
    }

    private final Type type;
    private final Binding binding;
    private final Method bootstrapMethod;

    private SqlTypeByteCodeExpression(Type type, Binding binding, Method bootstrapMethod)
    {
        super(type(Type.class));

        this.type = checkNotNull(type, "type is null");
        this.binding = checkNotNull(binding, "binding is null");
        this.bootstrapMethod = checkNotNull(bootstrapMethod, "bootstrapMethod is null");
    }

    @Override
    public ByteCodeNode getByteCode(MethodGenerationContext generationContext)
    {
        return InvokeInstruction.invokeDynamic(type.getTypeSignature().toString().replaceAll("\\W+", "_"), binding.getType(), bootstrapMethod, binding.getBindingId());
    }

    @Override
    public List<ByteCodeNode> getChildNodes()
    {
        return ImmutableList.of();
    }

    @Override
    protected String formatOneLine()
    {
        return type.getTypeSignature().toString();
    }
}

@@ -834,24 +834,72 @@
 nstruction;%0A
+import com.facebook.presto.spi.PrestoException;%0A
 import com.f
@@ -924,24 +924,24 @@
 .type.Type;%0A
-
 import com.g
@@ -971,24 +971,55 @@
 mutableList;
+%0Aimport io.airlift.slice.Slice;
 %0A%0Aimport jav
@@ -1128,24 +1128,107 @@
 dType.type;%0A
+import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;%0A
 import stati
@@ -1349,16 +1349,55 @@
 NotNull;
+%0Aimport static java.lang.String.format;
 %0A%0Apublic
@@ -1484,16 +1484,23 @@
  static 
+SqlType
 ByteCode
@@ -2716,32 +2716,32 @@
 OneLine()%0A    %7B%0A
-
         return t
@@ -2780,11 +2780,1659 @@
 );%0A    %7D
+%0A%0A    public ByteCodeExpression getValue(ByteCodeExpression block, ByteCodeExpression position)%0A    %7B%0A        Class%3C?%3E fromJavaElementType = type.getJavaType();%0A%0A        if (fromJavaElementType == boolean.class) %7B%0A            return invoke(%22getBoolean%22, boolean.class, block, position);%0A        %7D%0A        if (fromJavaElementType == long.class) %7B%0A            return invoke(%22getLong%22, long.class, block, position);%0A        %7D%0A        if (fromJavaElementType == double.class) %7B%0A            return invoke(%22getDouble%22, double.class, block, position);%0A        %7D%0A        if (fromJavaElementType == Slice.class) %7B%0A            return invoke(%22getSlice%22, Slice.class, block, position);%0A        %7D%0A        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format(%22Unexpected type %25s%22, fromJavaElementType.getName()));%0A    %7D%0A%0A    public ByteCodeExpression writeValue(ByteCodeExpression blockBuilder, ByteCodeExpression value)%0A    %7B%0A        Class%3C?%3E fromJavaElementType = type.getJavaType();%0A%0A        if (fromJavaElementType == boolean.class) %7B%0A            return invoke(%22writeBoolean%22, void.class, blockBuilder, value);%0A        %7D%0A        if (fromJavaElementType == long.class) %7B%0A            return invoke(%22writeLong%22, void.class, blockBuilder, value);%0A        %7D%0A        if (fromJavaElementType == double.class) %7B%0A            return invoke(%22writeDouble%22, void.class, blockBuilder, value);%0A        %7D%0A        if (fromJavaElementType == Slice.class) %7B%0A            return invoke(%22writeSlice%22, void.class, blockBuilder, value);%0A        %7D%0A        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format(%22Unexpected type %25s%22, fromJavaElementType.getName()));%0A    %7D
 %0A%7D%0A

Add SqlTypeByteCodeExpression getValue and writeValue methods