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.byteCode.Access.FINAL;
 import static com.facebook.presto.byteCode.Access.PRIVATE;
 import static com.facebook.presto.byteCode.Access.PUBLIC;
 import static com.facebook.presto.byteCode.Access.STATIC;
 import static com.facebook.presto.byteCode.Access.a;
 import static com.facebook.presto.byteCode.NamedParameterDefinition.arg;
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.facebook.presto.metadata.Signature.typeParameter;
 import static com.facebook.presto.sql.gen.CompilerUtils.defineClass;
 import static com.facebook.presto.sql.relational.Signatures.arrayConstructorSignature;
 import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.lang.invoke.MethodHandles.lookup;
 
 public final class ArrayConstructor
         extends ParametricScalar
 {
     public static final ArrayConstructor ARRAY_CONSTRUCTOR = new ArrayConstructor();
     private static final Signature SIGNATURE = new Signature("array_constructor", ImmutableList.of(typeParameter("E")), "array<E>", ImmutableList.of("E""E"), truetrue);
 
     @Override
     public Signature getSignature()
     {
         return ;
     }
 
     @Override
     public boolean isHidden()
     {
         return true;
     }
 
     @Override
     public boolean isDeterministic()
     {
         return true;
     }
 
     @Override
     public String getDescription()
     {
         // Internal function, doesn't need a description
         return "";
     }
 
     @Override
     public FunctionInfo specialize(Map<StringTypetypesint arityTypeManager typeManagerFunctionRegistry functionRegistry)
     {
         checkArgument(types.size() == 1, "Can only construct arrays from exactly matching types");
         ImmutableList.Builder<Class<?>> builder = ImmutableList.builder();
         Type type = types.get("E");
         for (int i = 0; i < arityi++) {
             if (type.getJavaType().isPrimitive()) {
                builder.add(Primitives.wrap(type.getJavaType()));
            }
            else {
                builder.add(type.getJavaType());
            }
        }
        ImmutableList<Class<?>> stackTypes = builder.build();
        Class<?> clazz = generateArrayConstructor(stackTypestype);
        MethodHandle methodHandle;
        try {
            Method method = clazz.getMethod("arrayConstructor"stackTypes.toArray(new Class<?>[stackTypes.size()]));
            methodHandle = lookup().unreflect(method);
        }
        catch (ReflectiveOperationException e) {
            throw Throwables.propagate(e);
        }
        List<BooleannullableParameters = ImmutableList.copyOf(Collections.nCopies(stackTypes.size(), true));
        return new FunctionInfo(arrayConstructorSignature(parameterizedTypeName("array"type.getTypeSignature()), Collections.nCopies(aritytype.getTypeSignature())), "Constructs an array of the given elements"truemethodHandletruefalsenullableParameters);
    }
    public static Slice emptyArrayConstructor()
    {
        return ArrayType.toStackRepresentation(ImmutableList.of());
    }
    private static Class<?> generateArrayConstructor(List<Class<?>> stackTypesType elementType)
    {
        List<StringstackTypeNames = stackTypes.stream()
                .map(Class::getSimpleName)
                .collect(toImmutableList());
        ClassDefinition definition = new ClassDefinition(
                new CompilerContext(null),
                a(),
                CompilerUtils.makeClassName(Joiner.on("").join(stackTypeNames) + "ArrayConstructor"),
                type(Object.class));
        // Generate constructor
        definition.declareDefaultConstructor(a());
        // Generate arrayConstructor()
        ImmutableList.Builder<NamedParameterDefinitionparameters = ImmutableList.builder();
        for (int i = 0; i < stackTypes.size(); i++) {
            Class<?> stackType = stackTypes.get(i);
            parameters.add(arg("arg" + istackType));
        }
        CompilerContext context = new CompilerContext(null);
        Block body = definition.declareMethod(contexta(), "arrayConstructor"type(Slice.class), parameters.build())
                .getBody();
        Variable valuesVariable = context.declareVariable(List.class"values");
        body.comment("List<Object> values = new ArrayList();")
                .newObject(ArrayList.class)
                .dup()
                .invokeConstructor(ArrayList.class)
                .putVariable(valuesVariable);
        for (int i = 0; i < stackTypes.size(); i++) {
            body.comment("values.add(arg" + i + ");")
                    .getVariable(valuesVariable)
                    .getVariable("arg" + i);
            Class<?> stackType = stackTypes.get(i);
            if (stackType.isPrimitive()) {
                body.append(ByteCodeUtils.boxPrimitiveIfNecessary(contextstackType));
            }
            body.invokeInterface(List.class"add"boolean.classObject.class);
        }
        if (elementType instanceof ArrayType || elementType instanceof MapType || elementType instanceof RowType) {
            body.comment("return rawSlicesToStackRepresentation(values);")
                    .getVariable(valuesVariable)
                    .invokeStatic(ArrayType.class"rawSlicesToStackRepresentation"Slice.classList.class)
                    .retObject();
        }
        else {
            body.comment("return toStackRepresentation(values);")
                    .getVariable(valuesVariable)
                    .invokeStatic(ArrayType.class"toStackRepresentation"Slice.classList.class)
                    .retObject();
        }
        return defineClass(definitionObject.classnew DynamicClassLoader());
    }
New to GrepCode? Check out our FAQ X