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.comparableTypeParameter;
 import static com.facebook.presto.metadata.Signature.typeParameter;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INDEX;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.NULLABLE_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.util.Reflection.method;
 
 public class MapAggregation
         extends ParametricAggregation
 {
     public static final MapAggregation MAP_AGG = new MapAggregation();
     public static final String NAME = "map_agg";
     private static final Method OUTPUT_FUNCTION = method(MapAggregation.class"output"KeyValuePairsState.classBlockBuilder.class);
     private static final Method INPUT_FUNCTION = method(MapAggregation.class"input"KeyValuePairsState.classBlock.classBlock.classint.class);
     private static final Method COMBINE_FUNCTION = method(MapAggregation.class"combine"KeyValuePairsState.classKeyValuePairsState.class);
 
     private static final Signature SIGNATURE = new Signature(, ImmutableList.of(comparableTypeParameter("K"), typeParameter("V")),
                                                                    "map<K,V>", ImmutableList.of("K""V"), falsefalse);
 
     @Override
     public Signature getSignature()
     {
         return ;
     }
 
     @Override
     public String getDescription()
     {
         return "Aggregates all the rows (key/value pairs) into a single map";
     }
 
     @Override
     public FunctionInfo specialize(Map<StringTypetypesint arityTypeManager typeManagerFunctionRegistry functionRegistry)
     {
         Type keyType = types.get("K");
         Type valueType = types.get("V");
         Signature signature = new Signature(new MapType(keyTypevalueType).getTypeSignature(), keyType.getTypeSignature(), valueType.getTypeSignature());
         InternalAggregationFunction aggregation = generateAggregation(keyTypevalueType);
         return new FunctionInfo(signaturegetDescription(), aggregation.getIntermediateType().getTypeSignature(), aggregationfalse);
     }
 
     private static InternalAggregationFunction generateAggregation(Type keyTypeType valueType)
     {
         DynamicClassLoader classLoader = new DynamicClassLoader(MapAggregation.class.getClassLoader());
         List<TypeinputTypes = ImmutableList.of(keyTypevalueType);
         Type outputType = new MapType(keyTypevalueType);
         KeyValuePairStateSerializer stateSerializer = new KeyValuePairStateSerializer();
         Type intermediateType = stateSerializer.getSerializedType();
 
         AggregationMetadata metadata = new AggregationMetadata(
                 generateAggregationName(outputTypeinputTypes),
                 createInputParameterMetadata(keyTypevalueType),
                 ,
                 null,
                 null,
                 ,
                 ,
                 KeyValuePairsState.class,
                 stateSerializer,
                 new KeyValuePairsStateFactory(keyTypevalueType),
                 outputType,
                false);
        GenericAccumulatorFactoryBinder factory = new AccumulatorCompiler().generateAccumulatorFactoryBinder(metadataclassLoader);
        return new InternalAggregationFunction(inputTypesintermediateTypeoutputTypetruefalsefactory);
    }
    private static List<ParameterMetadatacreateInputParameterMetadata(Type keyTypeType valueType)
    {
        return ImmutableList.of(new ParameterMetadata(),
                                new ParameterMetadata(keyType),
                                new ParameterMetadata(valueType),
                                new ParameterMetadata());
    }
    public static void input(KeyValuePairsState stateBlock keyBlock valueint position)
    {
        KeyValuePairs pairs = state.get();
        if (pairs == null) {
            pairs = new KeyValuePairs(state.getKeyType(), state.getValueType());
            state.set(pairs);
        }
        long startSize = pairs.estimatedInMemorySize();
        pairs.add(keyvalueposition);
        state.addMemoryUsage(pairs.estimatedInMemorySize() - startSize);
    }
    public static void combine(KeyValuePairsState stateKeyValuePairsState otherState)
    {
        if (state.get() != null && otherState.get() != null) {
            Block keys = otherState.get().getKeys();
            Block values = otherState.get().getValues();
            for (int i = 0; i < keys.getPositionCount(); i++) {
                state.get().add(keysvaluesi);
            }
        }
        else if (state.get() == null) {
            state.set(otherState.get());
        }
    }
    public static void output(KeyValuePairsState stateBlockBuilder out)
    {
        KeyValuePairs pairs = state.get();
        if (pairs == null) {
            out.appendNull();
        }
        else {
            Slice slice = pairs.serialize();
            out.writeBytes(slice, 0, slice.length());
            out.closeEntry();
        }
    }
New to GrepCode? Check out our FAQ X