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.aggregation;
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.metadata.Signature.typeParameter;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INDEX;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INPUT_CHANNEL;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.STATE;
 import static com.facebook.presto.operator.aggregation.AggregationUtils.generateAggregationName;
 import static com.facebook.presto.spi.type.VarbinaryType.VARBINARY;
 import static com.facebook.presto.type.TypeUtils.buildStructuralSlice;
 import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
 import static com.facebook.presto.util.Reflection.methodHandle;
 
 public class ArrayAggregation
         extends ParametricAggregation
 {
     public static final ArrayAggregation ARRAY_AGGREGATION = new ArrayAggregation();
     private static final String NAME = "array_agg";
     private static final MethodHandle INPUT_FUNCTION = methodHandle(ArrayAggregation.class"input"Type.classArrayAggregationState.classBlock.classint.class);
     private static final MethodHandle COMBINE_FUNCTION = methodHandle(ArrayAggregation.class"combine"Type.classArrayAggregationState.classArrayAggregationState.class);
     private static final MethodHandle OUTPUT_FUNCTION = methodHandle(ArrayAggregation.class"output"ArrayAggregationState.classBlockBuilder.class);
     private static final Signature SIGNATURE = new Signature(, ImmutableList.of(typeParameter("T")), "array<T>", ImmutableList.of("T"), falsefalse);
 
     @Override
     public Signature getSignature()
     {
         return ;
     }
 
     @Override
     public String getDescription()
     {
         return "return an array of values";
     }
 
     @Override
     public FunctionInfo specialize(Map<StringTypetypesint arityTypeManager typeManagerFunctionRegistry functionRegistry)
     {
         Type type = types.get("T");
         Signature signature = new Signature(,
                 parameterizedTypeName("array"type.getTypeSignature()),
                 type.getTypeSignature());
         InternalAggregationFunction aggregation = generateAggregation(type);
         return new FunctionInfo(signaturegetDescription(), aggregation);
     }
 
     {
         DynamicClassLoader classLoader = new DynamicClassLoader(ArrayAggregation.class.getClassLoader());
 
         AccumulatorStateSerializer<?> stateSerializer = new ArrayAggregationStateSerializer(type);
         AccumulatorStateFactory<?> stateFactory = new ArrayAggregationStateFactory();
         MethodHandle inputFunction = .bindTo(type);
         MethodHandle combineFunction = .bindTo(type);
         MethodHandle outputFunction = ;
         Class<? extends AccumulatorStatestateInterface = ArrayAggregationState.class;
 
         List<TypeinputTypes = ImmutableList.of(type);
         Type outputType = new ArrayType(type);
         Type intermediateType = stateSerializer.getSerializedType();
         List<ParameterMetadatainputParameterMetadata = createInputParameterMetadata(type);
         AggregationMetadata metadata = new AggregationMetadata(
                generateAggregationName(typeinputTypes),
                inputParameterMetadata,
                inputFunction,
                null,
                null,
                combineFunction,
                outputFunction,
                stateInterface,
                stateSerializer,
                stateFactory,
                outputType,
                false);
        GenericAccumulatorFactoryBinder factory = new AccumulatorCompiler().generateAccumulatorFactoryBinder(metadataclassLoader);
        return new InternalAggregationFunction(inputTypesintermediateTypeoutputTypetruefalsefactory);
    }
    private static List<ParameterMetadatacreateInputParameterMetadata(Type value)
    {
        return ImmutableList.of(new ParameterMetadata(), new ParameterMetadata(value), new ParameterMetadata());
    }
    public static void input(Type typeArrayAggregationState stateBlock valueint position)
    {
        BlockBuilder blockBuilder = state.getBlockBuilder();
        if (blockBuilder == null) {
            blockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus()); // not using type.createBlockBuilder because fixedWidth ones can't be serialized
            state.setBlockBuilder(blockBuilder);
        }
        long startSize = blockBuilder.getSizeInBytes();
        type.appendTo(valuepositionblockBuilder);
        state.addMemoryUsage(blockBuilder.getSizeInBytes() - startSize);
    }
    public static void combine(Type typeArrayAggregationState stateArrayAggregationState otherState)
    {
        BlockBuilder stateBlockBuilder = state.getBlockBuilder();
        BlockBuilder otherStateBlockBuilder = otherState.getBlockBuilder();
        if (otherStateBlockBuilder == null) {
            return;
        }
        if (stateBlockBuilder == null) {
            state.setBlockBuilder(otherStateBlockBuilder);
            return;
        }
        int otherPositionCount = otherStateBlockBuilder.getPositionCount();
        long startSize = stateBlockBuilder.getSizeInBytes();
        for (int i = 0; i < otherPositionCounti++) {
            type.appendTo(otherStateBlockBuilderistateBlockBuilder);
        }
        state.addMemoryUsage(stateBlockBuilder.getSizeInBytes() - startSize);
    }
    public static void output(ArrayAggregationState stateBlockBuilder out)
    {
        BlockBuilder stateBlockBuilder = state.getBlockBuilder();
        if (stateBlockBuilder == null || stateBlockBuilder.getPositionCount() == 0) {
            out.appendNull();
        }
        else {
            .writeSlice(outbuildStructuralSlice(stateBlockBuilder));
        }
    }
New to GrepCode? Check out our FAQ X