Search in sources :

Example 1 with DynamicClassLoader

use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.

the class AbstractMinMaxAggregationFunction method generateAggregation.

protected InternalAggregationFunction generateAggregation(Type type, MethodHandle compareMethodHandle) {
    DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(type);
    MethodHandle inputFunction;
    MethodHandle combineFunction;
    MethodHandle outputFunction;
    Class<? extends AccumulatorState> stateInterface;
    AccumulatorStateSerializer<?> stateSerializer;
    if (type.getJavaType() == long.class) {
        stateInterface = NullableLongState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = LONG_INPUT_FUNCTION;
        combineFunction = LONG_COMBINE_FUNCTION;
        outputFunction = LONG_OUTPUT_FUNCTION;
    } else if (type.getJavaType() == double.class) {
        stateInterface = NullableDoubleState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = DOUBLE_INPUT_FUNCTION;
        combineFunction = DOUBLE_COMBINE_FUNCTION;
        outputFunction = DOUBLE_OUTPUT_FUNCTION;
    } else if (type.getJavaType() == Slice.class) {
        stateInterface = SliceState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = SLICE_INPUT_FUNCTION;
        combineFunction = SLICE_COMBINE_FUNCTION;
        outputFunction = SLICE_OUTPUT_FUNCTION;
    } else if (type.getJavaType() == boolean.class) {
        stateInterface = NullableBooleanState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = BOOLEAN_INPUT_FUNCTION;
        combineFunction = BOOLEAN_COMBINE_FUNCTION;
        outputFunction = BOOLEAN_OUTPUT_FUNCTION;
    } else {
        stateInterface = BlockState.class;
        stateSerializer = new BlockStateSerializer(type);
        inputFunction = BLOCK_INPUT_FUNCTION;
        combineFunction = BLOCK_COMBINE_FUNCTION;
        outputFunction = BLOCK_OUTPUT_FUNCTION;
    }
    inputFunction = inputFunction.bindTo(compareMethodHandle);
    combineFunction = combineFunction.bindTo(compareMethodHandle);
    outputFunction = outputFunction.bindTo(type);
    AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateInterface, classLoader);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createParameterMetadata(type), inputFunction, combineFunction, outputFunction, stateInterface, stateSerializer, stateFactory, type);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, type, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) BlockStateSerializer(com.facebook.presto.operator.aggregation.state.BlockStateSerializer) NullableBooleanState(com.facebook.presto.operator.aggregation.state.NullableBooleanState) Type(com.facebook.presto.spi.type.Type) OperatorType(com.facebook.presto.spi.function.OperatorType) BlockState(com.facebook.presto.operator.aggregation.state.BlockState) NullableDoubleState(com.facebook.presto.operator.aggregation.state.NullableDoubleState) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with DynamicClassLoader

use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.

the class AbstractMinMaxBy method generateAggregation.

private InternalAggregationFunction generateAggregation(Type valueType, Type keyType, FunctionRegistry functionRegistry) {
    Class<?> stateClazz = getStateClass(keyType.getJavaType(), valueType.getJavaType());
    Map<String, Type> stateFieldTypes = ImmutableMap.of("First", keyType, "Second", valueType);
    DynamicClassLoader classLoader = new DynamicClassLoader(getClass().getClassLoader());
    AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateClazz, stateFieldTypes, classLoader);
    AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClazz, stateFieldTypes, classLoader);
    Type intermediateType = stateSerializer.getSerializedType();
    List<Type> inputTypes = ImmutableList.of(valueType, keyType);
    CallSiteBinder binder = new CallSiteBinder();
    OperatorType operator = min ? LESS_THAN : GREATER_THAN;
    MethodHandle compareMethod = functionRegistry.getScalarFunctionImplementation(functionRegistry.resolveOperator(operator, ImmutableList.of(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 metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), valueType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(valueType, keyType), inputMethod, combineMethod, outputMethod, stateClazz, stateSerializer, stateFactory, valueType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, valueType, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) OperatorType(com.facebook.presto.spi.function.OperatorType) OperatorType(com.facebook.presto.spi.function.OperatorType) Type(com.facebook.presto.spi.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with DynamicClassLoader

use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.

the class AbstractMinMaxNAggregationFunction method generateAggregation.

protected InternalAggregationFunction generateAggregation(Type type) {
    DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxNAggregationFunction.class.getClassLoader());
    BlockComparator comparator = typeToComparator.apply(type);
    List<Type> inputTypes = ImmutableList.of(type, BIGINT);
    MinMaxNStateSerializer stateSerializer = new MinMaxNStateSerializer(comparator, type);
    Type intermediateType = stateSerializer.getSerializedType();
    ArrayType outputType = new ArrayType(type);
    List<ParameterMetadata> inputParameterMetadata = ImmutableList.of(new ParameterMetadata(STATE), new ParameterMetadata(BLOCK_INPUT_CHANNEL, type), new ParameterMetadata(INPUT_CHANNEL, BIGINT), new ParameterMetadata(BLOCK_INDEX));
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, INPUT_FUNCTION.bindTo(comparator).bindTo(type), COMBINE_FUNCTION, OUTPUT_FUNCTION.bindTo(outputType), MinMaxNState.class, stateSerializer, new MinMaxNStateFactory(), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, outputType, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) MinMaxNStateSerializer(com.facebook.presto.operator.aggregation.state.MinMaxNStateSerializer) ParameterMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) Type(com.facebook.presto.spi.type.Type) MinMaxNStateFactory(com.facebook.presto.operator.aggregation.state.MinMaxNStateFactory)

Example 4 with DynamicClassLoader

use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.

the class ChecksumAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type type) {
    DynamicClassLoader classLoader = new DynamicClassLoader(ChecksumAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(type);
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), INPUT_FUNCTION.bindTo(type), COMBINE_FUNCTION, OUTPUT_FUNCTION, NullableLongState.class, StateCompiler.generateStateSerializer(NullableLongState.class, classLoader), StateCompiler.generateStateFactory(NullableLongState.class, classLoader), VARBINARY);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, BIGINT, VARBINARY, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) NullableLongState(com.facebook.presto.operator.aggregation.state.NullableLongState) Type(com.facebook.presto.spi.type.Type)

Example 5 with DynamicClassLoader

use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.

the class AggregationCompiler method generateBindableAggregationFunctions.

public static List<BindableAggregationFunction> generateBindableAggregationFunctions(Class<?> aggregationDefinition) {
    AggregationFunction aggregationAnnotation = aggregationDefinition.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    DynamicClassLoader classLoader = new DynamicClassLoader(aggregationDefinition.getClassLoader());
    ImmutableList.Builder<BindableAggregationFunction> builder = ImmutableList.builder();
    for (Class<?> stateClass : getStateClasses(aggregationDefinition)) {
        AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClass, classLoader);
        for (Method outputFunction : getOutputFunctions(aggregationDefinition, stateClass)) {
            for (Method inputFunction : getInputFunctions(aggregationDefinition, stateClass)) {
                List<LongVariableConstraint> longVariableConstraints = parseLongVariableConstraints(inputFunction);
                for (String name : getNames(outputFunction, aggregationAnnotation)) {
                    List<TypeSignature> inputTypes = getInputTypesSignatures(inputFunction);
                    TypeSignature outputType = TypeSignature.parseTypeSignature(outputFunction.getAnnotation(OutputFunction.class).value());
                    builder.add(new BindableAggregationFunction(new Signature(name, FunctionKind.AGGREGATE, // TODO parse constrains from annotations
                    ImmutableList.of(), longVariableConstraints, outputType, inputTypes, false), getDescription(aggregationDefinition, outputFunction), aggregationAnnotation.decomposable(), aggregationDefinition, stateClass, inputFunction, outputFunction));
                }
            }
        }
    }
    return builder.build();
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Method(java.lang.reflect.Method) AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) LongVariableConstraint(com.facebook.presto.metadata.LongVariableConstraint) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Aggregations

DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)26 Type (com.facebook.presto.spi.type.Type)18 MethodHandle (java.lang.invoke.MethodHandle)11 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)7 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)6 ArrayType (com.facebook.presto.type.ArrayType)6 MapType (com.facebook.presto.type.MapType)6 Parameter (com.facebook.presto.bytecode.Parameter)5 ImmutableList (com.google.common.collect.ImmutableList)5 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)4 Variable (com.facebook.presto.bytecode.Variable)4 Signature (com.facebook.presto.metadata.Signature)4 ParameterMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)4 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)4 CompilerUtils.defineClass (com.facebook.presto.bytecode.CompilerUtils.defineClass)3 Scope (com.facebook.presto.bytecode.Scope)3 BoundVariables (com.facebook.presto.metadata.BoundVariables)3 FunctionRegistry (com.facebook.presto.metadata.FunctionRegistry)3 Block (com.facebook.presto.spi.block.Block)3 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)3