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.FunctionRegistry.mangleFieldAccessor;
 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.type.RowType.RowField;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static java.lang.String.format;
 
 public class RowFieldAccessor
         extends ParametricScalar
 {
     private static final Map<StringMethodHandleMETHOD_HANDLE_MAP;
 
     private final Signature signature;
     private final MethodHandle methodHandle;
 
     static {
         ImmutableMap.Builder<StringMethodHandlebuilder = ImmutableMap.builder();
         builder.put("long", Reflection.methodHandle(RowFieldAccessor.class"longAccessor"JsonExtractor.classSlice.class));
         builder.put("double", Reflection.methodHandle(RowFieldAccessor.class"doubleAccessor"JsonExtractor.classSlice.class));
         builder.put("boolean", Reflection.methodHandle(RowFieldAccessor.class"booleanAccessor"JsonExtractor.classSlice.class));
         builder.put("slice", Reflection.methodHandle(RowFieldAccessor.class"sliceAccessor"JsonExtractor.classSlice.class));
          = builder.build();
     }
 
     public RowFieldAccessor(RowType typeString fieldName)
     {
         Type returnType = null;
         int index = 0;
         for (RowField field : type.getFields()) {
             if (field.getName().equals(Optional.of(fieldName))) {
                 returnType = field.getType();
                 break;
             }
             index++;
         }
         checkNotNull(returnType"%s not found in row type %s"fieldNametype);
          = new Signature(mangleFieldAccessor(fieldName), returnType.getTypeSignature(), type.getTypeSignature());
 
         JsonExtractor<?> extractor;
         if (returnType instanceof ArrayType || returnType instanceof MapType || returnType instanceof RowType) {
             extractor = new JsonValueJsonExtractor();
         }
         else if (returnType.getJavaType() == boolean.class) {
             extractor = new BooleanJsonExtractor();
         }
         else if (returnType.getJavaType() == long.class) {
             extractor = new LongJsonExtractor();
         }
         else if (returnType.getJavaType() == double.class) {
             extractor = new DoubleJsonExtractor();
         }
         else if (returnType.getJavaType() == Slice.class) {
             extractor = new ScalarValueJsonExtractor();
         }
         else {
             throw new IllegalArgumentException("Unsupported stack type: " + returnType.getJavaType());
         }
         extractor = generateExtractor(format("$[%d]"index), extractortrue);
         String stackType = returnType.getJavaType().getSimpleName().toLowerCase();
         checkState(.containsKey(stackType), "method handle missing for %s stack type"stackType);
         = .get(stackType).bindTo(extractor);
    }
    @Override
    public Signature getSignature()
    {
        return ;
    }
    @Override
    public boolean isHidden()
    {
        return true;
    }
    @Override
    public boolean isDeterministic()
    {
        return true;
    }
    @Override
    public String getDescription()
    {
        return null;
    }
    @Override
    public FunctionInfo specialize(Map<StringTypetypesint arityTypeManager typeManagerFunctionRegistry functionRegistry)
    {
        checkNotNull("methodHandle is null");
        return new FunctionInfo(getDescription(), isHidden(), isDeterministic(), true, ImmutableList.of(false));
    }
    public static Long longAccessor(JsonExtractor<LongextractorSlice row)
    {
        return JsonExtract.extract(rowextractor);
    }
    public static Boolean booleanAccessor(JsonExtractor<BooleanextractorSlice row)
    {
        return JsonExtract.extract(rowextractor);
    }
    public static Double doubleAccessor(JsonExtractor<DoubleextractorSlice row)
    {
        return JsonExtract.extract(rowextractor);
    }
    public static Slice sliceAccessor(JsonExtractor<SliceextractorSlice row)
    {
        return JsonExtract.extract(rowextractor);
    }
New to GrepCode? Check out our FAQ X