Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 java.util.Map;
 
 import static com.facebook.presto.metadata.OperatorType.SUBSCRIPT;
 import static com.facebook.presto.metadata.Signature.typeParameter;
 import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
 import static com.facebook.presto.type.TypeUtils.readStructuralBlock;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
 import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
 import static com.facebook.presto.util.Reflection.methodHandle;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class ArraySubscriptOperator
         extends ParametricOperator
 {
     public static final ArraySubscriptOperator ARRAY_SUBSCRIPT = new ArraySubscriptOperator();
 
     private static final Map<Class<?>, MethodHandleMETHOD_HANDLES = ImmutableMap.<Class<?>, MethodHandle>builder()
             .put(boolean.classmethodHandle(ArraySubscriptOperator.class"booleanSubscript"Type.classSlice.classlong.class))
             .put(long.classmethodHandle(ArraySubscriptOperator.class"longSubscript"Type.classSlice.classlong.class))
             .put(void.classmethodHandle(ArraySubscriptOperator.class"arrayWithUnknownType"Type.classSlice.classlong.class))
             .put(double.classmethodHandle(ArraySubscriptOperator.class"doubleSubscript"Type.classSlice.classlong.class))
             .put(Slice.classmethodHandle(ArraySubscriptOperator.class"sliceSubscript"Type.classSlice.classlong.class))
             .build();
 
     protected ArraySubscriptOperator()
     {
         super(, ImmutableList.of(typeParameter("E")), "E", ImmutableList.of("array<E>""bigint"));
     }
 
     @Override
     public FunctionInfo specialize(Map<StringTypetypesint arityTypeManager typeManagerFunctionRegistry functionRegistry)
     {
         checkArgument(types.size() == 1, "Expected one type, got %s"types);
         Type elementType = types.get("E");
 
         MethodHandle methodHandle = .get(elementType.getJavaType());
         methodHandle = methodHandle.bindTo(elementType);
         checkNotNull(methodHandle"methodHandle is null");
         return new FunctionInfo(Signature.internalOperator(.name(), elementType.getTypeSignature(), parameterizedTypeName("array"elementType.getTypeSignature()), parseTypeSignature(.)), "Array subscript"truemethodHandletruetrue, ImmutableList.of(falsefalse));
     }
 
     public static void arrayWithUnknownType(Type elementTypeSlice arraylong index)
     {
         readBlockAndCheckIndex(arrayindex);
     }
 
     public static Long longSubscript(Type elementTypeSlice arraylong index)
     {
         Block block = readBlockAndCheckIndex(arrayindex);
         int position = Ints.checkedCast(index - 1);
         if (block.isNull(position)) {
             return null;
         }
 
         return elementType.getLong(blockposition);
     }
 
     public static Boolean booleanSubscript(Type elementTypeSlice arraylong index)
     {
         Block block = readBlockAndCheckIndex(arrayindex);
         int position = Ints.checkedCast(index - 1);
         if (block.isNull(position)) {
             return null;
         }
 
         return elementType.getBoolean(blockposition);
     }
 
    public static Double doubleSubscript(Type elementTypeSlice arraylong index)
    {
        Block block = readBlockAndCheckIndex(arrayindex);
        int position = Ints.checkedCast(index - 1);
        if (block.isNull(position)) {
            return null;
        }
        return elementType.getDouble(blockposition);
    }
    public static Slice sliceSubscript(Type elementTypeSlice arraylong index)
    {
        Block block = readBlockAndCheckIndex(arrayindex);
        int position = Ints.checkedCast(index - 1);
        if (block.isNull(position)) {
            return null;
        }
        return elementType.getSlice(blockposition);
    }
    public static Block readBlockAndCheckIndex(Slice arraylong index)
    {
        if (index <= 0) {
            throw new PrestoException("Index out of bounds");
        }
        Block block = readStructuralBlock(array);
        if (index > block.getPositionCount()) {
            throw new PrestoException("Index out of bounds");
        }
        return block;
    }
New to GrepCode? Check out our FAQ X