Search in sources :

Example 1 with GenericAccumulatorFactoryBinder

use of io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder in project hetu-core by openlookeng.

the class ArrayAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type type, ArrayAggGroupImplementation groupMode) {
    DynamicClassLoader classLoader = new DynamicClassLoader(ArrayAggregationFunction.class.getClassLoader());
    AccumulatorStateSerializer<?> stateSerializer = new ArrayAggregationStateSerializer(type);
    AccumulatorStateFactory<?> stateFactory = new ArrayAggregationStateFactory(type, groupMode);
    List<Type> inputTypes = ImmutableList.of(type);
    Type outputType = new ArrayType(type);
    Type intermediateType = stateSerializer.getSerializedType();
    List<ParameterMetadata> inputParameterMetadata = createInputParameterMetadata(type);
    MethodHandle inputFunction = INPUT_FUNCTION.bindTo(type);
    MethodHandle combineFunction = COMBINE_FUNCTION.bindTo(type);
    MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(type);
    Class<? extends AccumulatorState> stateInterface = ArrayAggregationState.class;
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, inputFunction, combineFunction, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, stateFactory)), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) ParameterMetadata(io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata) ArrayType(io.prestosql.spi.type.ArrayType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with GenericAccumulatorFactoryBinder

use of io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder in project hetu-core by openlookeng.

the class Histogram method generateAggregation.

private static InternalAggregationFunction generateAggregation(String functionName, Type keyType, Type outputType, HistogramGroupImplementation groupMode) {
    DynamicClassLoader classLoader = new DynamicClassLoader(Histogram.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType);
    HistogramStateSerializer stateSerializer = new HistogramStateSerializer(keyType, outputType);
    Type intermediateType = stateSerializer.getSerializedType();
    MethodHandle inputFunction = INPUT_FUNCTION.bindTo(keyType);
    MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(outputType);
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(functionName, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType), inputFunction, COMBINE_FUNCTION, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(HistogramState.class, stateSerializer, new HistogramStateFactory(keyType, EXPECTED_SIZE_FOR_HASHING, groupMode))), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(functionName, inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Type(io.prestosql.spi.type.Type) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with GenericAccumulatorFactoryBinder

use of io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder in project hetu-core by openlookeng.

the class AbstractMinMaxBy method generateAggregation.

private InternalAggregationFunction generateAggregation(Type valueType, Type keyType, FunctionAndTypeManager functionAndTypeManager) {
    Class<?> stateClazz = getStateClass(keyType.getJavaType(), valueType.getJavaType());
    DynamicClassLoader classLoader = new DynamicClassLoader(getClass().getClassLoader());
    // Generate states and serializers:
    // For value that is a Block or Slice, we store them as Block/position combination
    // to avoid generating long-living objects through getSlice or getObject.
    // This can also help reducing cross-region reference in G1GC engine.
    // TODO: keys can have the same problem. But usually they are primitive types (given the nature of comparison).
    AccumulatorStateFactory<?> stateFactory;
    AccumulatorStateSerializer<?> stateSerializer;
    if (valueType.getJavaType().isPrimitive()) {
        Map<String, Type> stateFieldTypes = ImmutableMap.of("First", keyType, "Second", valueType);
        stateFactory = StateCompiler.generateStateFactory(stateClazz, stateFieldTypes, classLoader);
        stateSerializer = StateCompiler.generateStateSerializer(stateClazz, stateFieldTypes, classLoader);
    } else {
        // StateCompiler checks type compatibility.
        // Given "Second" in this case is always a Block, we only need to make sure the getter and setter of the Blocks are properly generated.
        // We deliberately make "SecondBlock" an array type so that the compiler will treat it as a block to workaround the sanity check.
        stateFactory = StateCompiler.generateStateFactory(stateClazz, ImmutableMap.of("First", keyType, "SecondBlock", new ArrayType(valueType)), classLoader);
        // States can be generated by StateCompiler given the they are simply classes with getters and setters.
        // However, serializers have logic in it. Creating serializers is better than generating them.
        stateSerializer = getStateSerializer(keyType, valueType);
    }
    Type intermediateType = stateSerializer.getSerializedType();
    List<Type> inputTypes = ImmutableList.of(valueType, keyType);
    CallSiteBinder binder = new CallSiteBinder();
    OperatorType operator = min ? LESS_THAN : GREATER_THAN;
    MethodHandle compareMethod = functionAndTypeManager.getBuiltInScalarFunctionImplementation(functionAndTypeManager.resolveOperatorFunctionHandle(operator, TypeSignatureProvider.fromTypes(keyType, keyType))).getMethodHandle();
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("processMaxOrMinBy"), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    generateInputMethod(definition, binder, compareMethod, keyType, valueType, stateClazz);
    generateCombineMethod(definition, binder, compareMethod, keyType, valueType, stateClazz);
    generateOutputMethod(definition, binder, valueType, stateClazz);
    Class<?> generatedClass = defineClass(definition, Object.class, binder.getBindings(), classLoader);
    MethodHandle inputMethod = methodHandle(generatedClass, "input", stateClazz, Block.class, Block.class, int.class);
    MethodHandle combineMethod = methodHandle(generatedClass, "combine", stateClazz, stateClazz);
    MethodHandle outputMethod = methodHandle(generatedClass, "output", stateClazz, BlockBuilder.class);
    AggregationMetadata aggregationMetadata = new AggregationMetadata(generateAggregationName(getSignature().getNameSuffix(), valueType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(valueType, keyType), inputMethod, combineMethod, outputMethod, ImmutableList.of(new AccumulatorStateDescriptor(stateClazz, stateSerializer, stateFactory)), valueType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(aggregationMetadata, classLoader);
    return new InternalAggregationFunction(getSignature().getNameSuffix(), inputTypes, ImmutableList.of(intermediateType), valueType, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) ClassDefinition(io.airlift.bytecode.ClassDefinition) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) OperatorType(io.prestosql.spi.function.OperatorType) ArrayType(io.prestosql.spi.type.ArrayType) SqlTypeBytecodeExpression.constantType(io.prestosql.sql.gen.SqlTypeBytecodeExpression.constantType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) CallSiteBinder(io.prestosql.sql.gen.CallSiteBinder) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata) MethodHandle(java.lang.invoke.MethodHandle)

Example 4 with GenericAccumulatorFactoryBinder

use of io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder in project hetu-core by openlookeng.

the class AbstractMinMaxByNAggregationFunction method generateAggregation.

protected InternalAggregationFunction generateAggregation(Type valueType, Type keyType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxNAggregationFunction.class.getClassLoader());
    BlockComparator comparator = typeToComparator.apply(keyType);
    List<Type> inputTypes = ImmutableList.of(valueType, keyType, BIGINT);
    MinMaxByNStateSerializer stateSerializer = new MinMaxByNStateSerializer(comparator, keyType, valueType);
    Type intermediateType = stateSerializer.getSerializedType();
    ArrayType outputType = new ArrayType(valueType);
    List<AggregationMetadata.ParameterMetadata> inputParameterMetadata = ImmutableList.of(new AggregationMetadata.ParameterMetadata(STATE), new AggregationMetadata.ParameterMetadata(NULLABLE_BLOCK_INPUT_CHANNEL, valueType), new AggregationMetadata.ParameterMetadata(BLOCK_INPUT_CHANNEL, keyType), new AggregationMetadata.ParameterMetadata(BLOCK_INDEX), new AggregationMetadata.ParameterMetadata(INPUT_CHANNEL, BIGINT));
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(name, valueType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, INPUT_FUNCTION.bindTo(comparator).bindTo(valueType).bindTo(keyType), COMBINE_FUNCTION, OUTPUT_FUNCTION.bindTo(outputType), ImmutableList.of(new AccumulatorStateDescriptor(MinMaxByNState.class, stateSerializer, new MinMaxByNStateFactory())), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(name, inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) AbstractMinMaxNAggregationFunction(io.prestosql.operator.aggregation.AbstractMinMaxNAggregationFunction) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) ArrayType(io.prestosql.spi.type.ArrayType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) BlockComparator(io.prestosql.operator.aggregation.BlockComparator) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata)

Example 5 with GenericAccumulatorFactoryBinder

use of io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder in project hetu-core by openlookeng.

the class MultimapAggregationFunction method generateAggregation.

private InternalAggregationFunction generateAggregation(Type keyType, Type valueType, Type outputType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(MultimapAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType, valueType);
    MultimapAggregationStateSerializer stateSerializer = new MultimapAggregationStateSerializer(keyType, valueType);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType, valueType), INPUT_FUNCTION, COMBINE_FUNCTION, OUTPUT_FUNCTION.bindTo(keyType).bindTo(valueType), ImmutableList.of(new AccumulatorStateDescriptor(MultimapAggregationState.class, stateSerializer, new MultimapAggregationStateFactory(keyType, valueType, groupMode))), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction)

Aggregations

DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)5 AggregationMetadata (io.prestosql.operator.aggregation.AggregationMetadata)5 AccumulatorStateDescriptor (io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)5 GenericAccumulatorFactoryBinder (io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder)5 InternalAggregationFunction (io.prestosql.operator.aggregation.InternalAggregationFunction)5 Type (io.prestosql.spi.type.Type)5 ArrayType (io.prestosql.spi.type.ArrayType)4 MethodHandle (java.lang.invoke.MethodHandle)3 ClassDefinition (io.airlift.bytecode.ClassDefinition)1 AbstractMinMaxNAggregationFunction (io.prestosql.operator.aggregation.AbstractMinMaxNAggregationFunction)1 ParameterMetadata (io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata)1 BlockComparator (io.prestosql.operator.aggregation.BlockComparator)1 OperatorType (io.prestosql.spi.function.OperatorType)1 CallSiteBinder (io.prestosql.sql.gen.CallSiteBinder)1 SqlTypeBytecodeExpression.constantType (io.prestosql.sql.gen.SqlTypeBytecodeExpression.constantType)1