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.List;
 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.operator.scalar.JsonExtract.BooleanJsonExtractor;
 import static com.facebook.presto.operator.scalar.JsonExtract.DoubleJsonExtractor;
 import static com.facebook.presto.operator.scalar.JsonExtract.JsonExtractor;
 import static com.facebook.presto.operator.scalar.JsonExtract.JsonValueJsonExtractor;
 import static com.facebook.presto.operator.scalar.JsonExtract.LongJsonExtractor;
 import static com.facebook.presto.operator.scalar.JsonExtract.ScalarValueJsonExtractor;
 import static com.facebook.presto.operator.scalar.JsonExtract.generateExtractor;
 import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
 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;
 import static java.lang.String.format;
 
 public class ArraySubscriptOperator
         extends ParametricOperator
 {
     public static final ArraySubscriptOperator ARRAY_SUBSCRIPT = new ArraySubscriptOperator();
     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapperProvider().get().registerModule(new SimpleModule().addSerializer(Slice.classnew SliceSerializer()));
     private static final CollectionType COLLECTION_TYPE = .getTypeFactory().constructCollectionType(List.classObject.class);
 
     private static final Map<Class<?>, MethodHandleMETHOD_HANDLES = ImmutableMap.<Class<?>, MethodHandle>builder()
             .put(boolean.classmethodHandle(ArraySubscriptOperator.class"booleanSubscript"Slice.classlong.class))
             .put(long.classmethodHandle(ArraySubscriptOperator.class"longSubscript"Slice.classlong.class))
             .put(void.classmethodHandle(ArraySubscriptOperator.class"arrayWithUnknownType"Slice.classlong.class))
             .put(double.classmethodHandle(ArraySubscriptOperator.class"doubleSubscript"Slice.classlong.class))
             .put(Slice.classmethodHandle(ArraySubscriptOperator.class"sliceSubscript"Slice.classlong.class))
             .build();
     private static final MethodHandle STRUCTURAL_METHOD_HANDLE = methodHandle(ArraySubscriptOperator.class"structuralSubscript"Slice.classlong.class);
 
     private static final LoadingCache<CacheKeyJsonExtractor<?>> CACHE = CacheBuilder.newBuilder()
             .expireAfterAccess(1, .)
             .maximumSize(1000)
             .build(new CacheLoader<CacheKeyJsonExtractor<?>>()
             {
                 @Override
                 public JsonExtractor<?> load(CacheKey key)
                         throws Exception
                 {
                     return generateExtractor(format("$[%d]"key.getIndex() - 1), key.getType().getExtractor(), true);
                 }
             });
 
     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;
        if (elementType instanceof ArrayType || elementType instanceof MapType || elementType instanceof RowType) {
            methodHandle = ;
        }
        else {
            methodHandle = .get(elementType.getJavaType());
        }
        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(Slice jsonlong index)
    {
        try {
            List<Objectarray = .readValue(json.getInput(), );
            if (index <= 0 || index > array.size()) {
                throw new PrestoException("Index out of bounds");
            }
        }
        catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
    public static Long longSubscript(Slice arraylong index)
    {
        return subscript(arrayindex.);
    }
    public static Boolean booleanSubscript(Slice arraylong index)
    {
        return subscript(arrayindex.);
    }
    public static Double doubleSubscript(Slice arraylong index)
    {
        return subscript(arrayindex.);
    }
    public static Slice sliceSubscript(Slice arraylong index)
    {
        return subscript(arrayindex.);
    }
    public static Slice structuralSubscript(Slice arraylong index)
    {
        return subscript(arrayindex.);
    }
    @SuppressWarnings("unchecked")
    private static <T> T subscript(Slice arraylong indexExtractorType type)
    {
        if (index <= 0) {
            throw new PrestoException("Index out of bounds");
        }
        JsonExtractor<?> extractor = .getUnchecked(new CacheKey(indextype));
        return (T) JsonExtract.extract(arrayextractor);
    }
    private static class CacheKey
    {
        private final long index;
        private final ExtractorType type;
        private CacheKey(long indexExtractorType type)
        {
            this. = index;
            this. = type;
        }
        public long getIndex()
        {
            return ;
        }
        public ExtractorType getType()
        {
            return ;
        }
        @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            CacheKey cacheKey = (CacheKeyo;
            if ( != cacheKey.index) {
                return false;
            }
            if ( != cacheKey.type) {
                return false;
            }
            return true;
        }
        @Override
        public int hashCode()
        {
            int result = (int) ( ^ ( >>> 32));
            result = 31 * result + .hashCode();
            return result;
        }
    }
    private enum ExtractorType
    {
        LONG(new LongJsonExtractor()),
        BOOLEAN(new BooleanJsonExtractor()),
        DOUBLE(new DoubleJsonExtractor()),
        SLICE(new ScalarValueJsonExtractor()),
        STRUCTURAL(new JsonValueJsonExtractor());
        private final JsonExtractor<?> extractor;
        ExtractorType(JsonExtractor<?> extractor)
        {
            this. = extractor;
        }
        public JsonExtractor<?> getExtractor()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X