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.Set;
 
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.STATE;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromAnnotations;
 import static com.facebook.presto.operator.aggregation.AggregationUtils.generateAggregationName;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class AggregationCompiler
 {
     private final TypeManager typeManager;
 
     public AggregationCompiler()
     {
         this(new TypeRegistry());
     }
 
     public AggregationCompiler(TypeManager typeManager)
     {
         this. = checkNotNull(typeManager"typeManager is null");
     }
 
     private static List<MethodfindPublicStaticMethodsWithAnnotation(Class<?> clazzClass<?> annotationClass)
     {
         ImmutableList.Builder<Methodmethods = ImmutableList.builder();
         for (Method method : clazz.getMethods()) {
             for (Annotation annotation : method.getAnnotations()) {
                 if (annotationClass.isInstance(annotation)) {
                     checkArgument(Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers()), "%s annotated with %s must be static and public"method.getName(), annotationClass.getSimpleName());
                     methods.add(method);
                 }
             }
         }
         return methods.build();
     }
 
     {
         List<InternalAggregationFunctionaggregations = generateAggregationFunctions(clazz);
         checkArgument(aggregations.size() == 1, "More than one aggregation function found");
         return aggregations.get(0);
     }
 
     public InternalAggregationFunction generateAggregationFunction(Class<?> clazzType returnTypeList<TypeargumentTypes)
     {
         checkNotNull(returnType"returnType is null");
         checkNotNull(argumentTypes"argumentTypes is null");
         for (InternalAggregationFunction aggregation : generateAggregationFunctions(clazz)) {
             if (aggregation.getFinalType().equals(returnType) && aggregation.getParameterTypes().equals(argumentTypes)) {
                 return aggregation;
             }
         }
         throw new IllegalArgumentException(String.format("No method with return type %s and arguments %s"returnTypeargumentTypes));
     }
 
     {
         AggregationFunction aggregationAnnotation = clazz.getAnnotation(AggregationFunction.class);
         checkNotNull(aggregationAnnotation"aggregationAnnotation is null");
 
         DynamicClassLoader classLoader = new DynamicClassLoader(clazz.getClassLoader());
        ImmutableList.Builder<InternalAggregationFunctionbuilder = ImmutableList.builder();
        for (Class<?> stateClass : getStateClasses(clazz)) {
            AccumulatorStateSerializer<?> stateSerializer = new StateCompiler().generateStateSerializer(stateClassclassLoader);
            Type intermediateType = stateSerializer.getSerializedType();
            Method intermediateInputFunction = getIntermediateInputFunction(clazzstateClass);
            Method combineFunction = getCombineFunction(clazzstateClass);
            AccumulatorStateFactory<?> stateFactory = new StateCompiler().generateStateFactory(stateClassclassLoader);
            for (Method outputFunction : getOutputFunctions(clazzstateClass)) {
                for (Method inputFunction : getInputFunctions(clazzstateClass)) {
                    for (String name : getNames(outputFunctionaggregationAnnotation)) {
                        List<TypeinputTypes = getInputTypes(inputFunction);
                        Type outputType = AggregationUtils.getOutputType(outputFunctionstateSerializer);
                        AggregationMetadata metadata = new AggregationMetadata(
                                generateAggregationName(nameoutputTypeinputTypes),
                                getParameterMetadata(inputFunctionaggregationAnnotation.approximate()),
                                inputFunction,
                                getParameterMetadata(intermediateInputFunctionfalse),
                                intermediateInputFunction,
                                combineFunction,
                                outputFunction,
                                stateClass,
                                stateSerializer,
                                stateFactory,
                                outputType,
                                aggregationAnnotation.approximate());
                        GenericAccumulatorFactoryBinder factory = new AccumulatorCompiler().generateAccumulatorFactoryBinder(metadataclassLoader);
                        builder.add(new InternalAggregationFunction(nameinputTypesintermediateTypeoutputTypeaggregationAnnotation.decomposable(), aggregationAnnotation.approximate(), factory));
                    }
                }
            }
        }
        return builder.build();
    }
    private List<ParameterMetadatagetParameterMetadata(@Nullable Method methodboolean sampleWeightAllowed)
    {
        if (method == null) {
            return null;
        }
        ImmutableList.Builder<ParameterMetadatabuilder = ImmutableList.builder();
        builder.add(new ParameterMetadata());
        Annotation[][] annotations = method.getParameterAnnotations();
        // Start at 1 because 0 is the STATE
        for (int i = 1; i < annotations.lengthi++) {
            builder.add(fromAnnotations(annotations[i], method.getDeclaringClass() + "." + method.getName(), ));
        }
        return builder.build();
    }
    private static List<StringgetNames(@Nullable Method outputFunctionAggregationFunction aggregationAnnotation)
    {
        List<StringdefaultNames = ImmutableList.<String>builder().add(aggregationAnnotation.value()).addAll(Arrays.asList(aggregationAnnotation.alias())).build();
        if (outputFunction == null) {
            return defaultNames;
        }
        AggregationFunction annotation = outputFunction.getAnnotation(AggregationFunction.class);
        if (annotation == null) {
            return defaultNames;
        }
        else {
            return ImmutableList.<String>builder().add(annotation.value()).addAll(Arrays.asList(annotation.alias())).build();
        }
    }
    private static Method getIntermediateInputFunction(Class<?> clazzClass<?> stateClass)
    {
        for (Method method : findPublicStaticMethodsWithAnnotation(clazzIntermediateInputFunction.class)) {
            if (method.getParameterTypes()[0] == stateClass) {
                return method;
            }
        }
        return null;
    }
    private static Method getCombineFunction(Class<?> clazzClass<?> stateClass)
    {
        for (Method method : findPublicStaticMethodsWithAnnotation(clazzCombineFunction.class)) {
            if (method.getParameterTypes()[0] == stateClass) {
                return method;
            }
        }
        return null;
    }
    private static List<MethodgetOutputFunctions(Class<?> clazzfinal Class<?> stateClass)
    {
        // Only include methods that match this state class
        List<Methodmethods = findPublicStaticMethodsWithAnnotation(clazzOutputFunction.class).stream()
                .filter(method -> method.getParameterTypes()[0] == stateClass)
                .collect(toImmutableList());
        if (methods.isEmpty()) {
            List<MethodnoOutputFunction = new ArrayList<>();
            noOutputFunction.add(null);
            return noOutputFunction;
        }
        return methods;
    }
    private static List<MethodgetInputFunctions(Class<?> clazzfinal Class<?> stateClass)
    {
        // Only include methods that match this state class
        List<MethodinputFunctions = findPublicStaticMethodsWithAnnotation(clazzInputFunction.class).stream()
                .filter(method -> method.getParameterTypes()[0] == stateClass)
                .collect(toImmutableList());
        checkArgument(!inputFunctions.isEmpty(), "Aggregation has no input functions");
        return inputFunctions;
    }
    private List<TypegetInputTypes(Method inputFunction)
    {
        ImmutableList.Builder<Typebuilder = ImmutableList.builder();
        Annotation[][] parameterAnnotations = inputFunction.getParameterAnnotations();
        for (Annotation[] annotations : parameterAnnotations) {
            for (Annotation annotation : annotations) {
                if (annotation instanceof SqlType) {
                    String typeName = ((SqlTypeannotation).value();
                    builder.add(.getType(parseTypeSignature(typeName)));
                }
            }
        }
        ImmutableList<Typetypes = builder.build();
        return types;
    }
    private static Set<Class<?>> getStateClasses(Class<?> clazz)
    {
        ImmutableSet.Builder<Class<?>> builder = ImmutableSet.builder();
        for (Method inputFunction : findPublicStaticMethodsWithAnnotation(clazzInputFunction.class)) {
            checkArgument(inputFunction.getParameterTypes().length > 0, "Input function has no parameters");
            Class<?> stateClass = inputFunction.getParameterTypes()[0];
            checkArgument(AccumulatorState.class.isAssignableFrom(stateClass), "stateClass is not a subclass of AccumulatorState");
            builder.add(stateClass);
        }
        ImmutableSet<Class<?>> stateClasses = builder.build();
        checkArgument(!stateClasses.isEmpty(), "No input functions found");
        return stateClasses;
    }
New to GrepCode? Check out our FAQ X