use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class BindableAggregationFunction method specialize.
@Override
public InternalAggregationFunction specialize(BoundVariables variables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
// bind variables
Signature boundSignature = applyBoundVariables(getSignature(), variables, arity);
List<Type> inputTypes = boundSignature.getArgumentTypes().stream().map(x -> typeManager.getType(x)).collect(toImmutableList());
Type outputType = typeManager.getType(boundSignature.getReturnType());
AggregationFunction aggregationAnnotation = definitionClass.getAnnotation(AggregationFunction.class);
requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
DynamicClassLoader classLoader = new DynamicClassLoader(definitionClass.getClassLoader(), getClass().getClassLoader());
AggregationMetadata metadata;
AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClass, classLoader);
Type intermediateType = stateSerializer.getSerializedType();
Method combineFunction = AggregationCompiler.getCombineFunction(definitionClass, stateClass);
AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateClass, classLoader);
try {
MethodHandle inputHandle = lookup().unreflect(inputFunction);
MethodHandle combineHandle = lookup().unreflect(combineFunction);
MethodHandle outputHandle = outputFunction == null ? null : lookup().unreflect(outputFunction);
metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), outputType.getTypeSignature(), signaturesFromTypes(inputTypes)), getParameterMetadata(inputFunction, inputTypes), inputHandle, combineHandle, outputHandle, stateClass, stateSerializer, stateFactory, outputType);
} catch (IllegalAccessException e) {
throw Throwables.propagate(e);
}
AccumulatorFactoryBinder factory = new LazyAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, outputType, decomposable, factory);
}
use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class CountColumn method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type type) {
DynamicClassLoader classLoader = new DynamicClassLoader(CountColumn.class.getClassLoader());
AccumulatorStateSerializer<LongState> stateSerializer = StateCompiler.generateStateSerializer(LongState.class, classLoader);
AccumulatorStateFactory<LongState> stateFactory = StateCompiler.generateStateFactory(LongState.class, classLoader);
Type intermediateType = stateSerializer.getSerializedType();
List<Type> inputTypes = ImmutableList.of(type);
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, BIGINT.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), INPUT_FUNCTION, COMBINE_FUNCTION, OUTPUT_FUNCTION, LongState.class, stateSerializer, stateFactory, BIGINT);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, intermediateType, BIGINT, true, factory);
}
use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class DecimalAverageAggregation method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type type) {
checkArgument(type instanceof DecimalType, "type must be Decimal");
DynamicClassLoader classLoader = new DynamicClassLoader(DecimalAverageAggregation.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(type);
MethodHandle inputFunction;
MethodHandle outputFunction;
Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowAndLongState.class;
AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowAndLongStateSerializer();
if (((DecimalType) type).isShort()) {
inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
outputFunction = SHORT_DECIMAL_OUTPUT_FUNCTION;
} else {
inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
outputFunction = LONG_DECIMAL_OUTPUT_FUNCTION;
}
inputFunction = inputFunction.bindTo(type);
outputFunction = outputFunction.bindTo(type);
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), inputFunction, COMBINE_FUNCTION, outputFunction, stateInterface, stateSerializer, new LongDecimalWithOverflowAndLongStateFactory(), type);
Type intermediateType = stateSerializer.getSerializedType();
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, intermediateType, type, true, factory);
}
use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class DecimalSumAggregation method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type inputType, Type outputType) {
checkArgument(inputType instanceof DecimalType, "type must be Decimal");
DynamicClassLoader classLoader = new DynamicClassLoader(DecimalSumAggregation.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(inputType);
MethodHandle inputFunction;
Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowState.class;
AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowStateSerializer();
if (((DecimalType) inputType).isShort()) {
inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
} else {
inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
}
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(inputType), inputFunction.bindTo(inputType), COMBINE_FUNCTION, LONG_DECIMAL_OUTPUT_FUNCTION.bindTo(outputType), stateInterface, stateSerializer, new LongDecimalWithOverflowStateFactory(), outputType);
Type intermediateType = stateSerializer.getSerializedType();
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, intermediateType, outputType, true, factory);
}
use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class ArbitraryAggregationFunction method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type type) {
DynamicClassLoader classLoader = new DynamicClassLoader(ArbitraryAggregationFunction.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(type);
Type intermediateType = stateSerializer.getSerializedType();
List<ParameterMetadata> inputParameterMetadata = createInputParameterMetadata(type);
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, inputFunction, combineFunction, outputFunction.bindTo(type), stateInterface, stateSerializer, StateCompiler.generateStateFactory(stateInterface, classLoader), type);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, intermediateType, type, true, factory);
}
Aggregations