use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class BytecodeExpressionAssertions method execute.
public static Object execute(Function<Scope, BytecodeNode> nodeGenerator, ParameterizedType returnType, Optional<ClassLoader> parentClassLoader) throws Exception {
ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("Test"), type(Object.class));
MethodDefinition method = classDefinition.declareMethod(a(PUBLIC, STATIC), "test", returnType);
BytecodeNode node = nodeGenerator.apply(method.getScope());
method.getBody().append(node);
if (DUMP_BYTE_CODE_TREE) {
DumpBytecodeVisitor dumpBytecode = new DumpBytecodeVisitor(System.out);
dumpBytecode.visitClass(classDefinition);
}
DynamicClassLoader classLoader = new DynamicClassLoader(parentClassLoader.orElse(BytecodeExpressionAssertions.class.getClassLoader()));
ClassWriter cw = new SmartClassWriter(ClassInfoLoader.createClassInfoLoader(ImmutableList.of(classDefinition), classLoader));
classDefinition.visit(cw);
Class<?> clazz = classLoader.defineClass(classDefinition.getType().getJavaClassName(), cw.toByteArray());
return clazz.getMethod("test").invoke(null);
}
use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class Bootstrap method bootstrap.
public static CallSite bootstrap(MethodHandles.Lookup callerLookup, String name, MethodType type, long bindingId) {
try {
ClassLoader classLoader = callerLookup.lookupClass().getClassLoader();
checkArgument(classLoader instanceof DynamicClassLoader, "Expected %s's classloader to be of type %s", callerLookup.lookupClass().getName(), DynamicClassLoader.class.getName());
DynamicClassLoader dynamicClassLoader = (DynamicClassLoader) classLoader;
MethodHandle target = dynamicClassLoader.getCallSiteBindings().get(bindingId);
checkArgument(target != null, "Binding %s for function %s%s not found", bindingId, name, type.parameterList());
return new ConstantCallSite(target);
} catch (Throwable e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw Throwables.propagate(e);
}
}
use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class JoinProbeCompiler method internalCompileJoinOperatorFactory.
@VisibleForTesting
public HashJoinOperatorFactoryFactory internalCompileJoinOperatorFactory(List<Type> types, List<Integer> probeOutputChannels, List<Integer> probeJoinChannel, Optional<Integer> probeHashChannel) {
Class<? extends JoinProbe> joinProbeClass = compileJoinProbe(types, probeOutputChannels, probeJoinChannel, probeHashChannel);
ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("JoinProbeFactory"), type(Object.class), type(JoinProbeFactory.class));
classDefinition.declareDefaultConstructor(a(PUBLIC));
Parameter lookupSource = arg("lookupSource", LookupSource.class);
Parameter page = arg("page", Page.class);
MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "createJoinProbe", type(JoinProbe.class), lookupSource, page);
method.getBody().newObject(joinProbeClass).dup().append(lookupSource).append(page).invokeConstructor(joinProbeClass, LookupSource.class, Page.class).retObject();
DynamicClassLoader classLoader = new DynamicClassLoader(joinProbeClass.getClassLoader());
JoinProbeFactory joinProbeFactory;
if (probeJoinChannel.isEmpty()) {
// see comment in PagesIndex#createLookupSource
joinProbeFactory = new SimpleJoinProbe.SimpleJoinProbeFactory(types, probeOutputChannels, probeJoinChannel, probeHashChannel);
} else {
Class<? extends JoinProbeFactory> joinProbeFactoryClass = defineClass(classDefinition, JoinProbeFactory.class, classLoader);
try {
joinProbeFactory = joinProbeFactoryClass.newInstance();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
Class<? extends OperatorFactory> operatorFactoryClass = IsolatedClass.isolateClass(classLoader, OperatorFactory.class, LookupJoinOperatorFactory.class, LookupJoinOperator.class);
return new HashJoinOperatorFactoryFactory(joinProbeFactory, operatorFactoryClass);
}
use of com.facebook.presto.bytecode.DynamicClassLoader in project presto by prestodb.
the class MultimapAggregationFunction method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType) {
DynamicClassLoader classLoader = new DynamicClassLoader(MultimapAggregationFunction.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(keyType, valueType);
Type outputType = new MapType(keyType, new ArrayType(valueType));
MultiKeyValuePairStateSerializer stateSerializer = new MultiKeyValuePairStateSerializer(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, MultiKeyValuePairsState.class, stateSerializer, new MultiKeyValuePairsStateFactory(keyType, valueType), outputType);
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 MapAggregationFunction method generateAggregation.
private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType) {
DynamicClassLoader classLoader = new DynamicClassLoader(MapAggregationFunction.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(keyType, valueType);
Type outputType = new MapType(keyType, valueType);
KeyValuePairStateSerializer stateSerializer = new KeyValuePairStateSerializer(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.bindTo(keyType).bindTo(valueType), COMBINE_FUNCTION, OUTPUT_FUNCTION, KeyValuePairsState.class, stateSerializer, new KeyValuePairsStateFactory(keyType, valueType), outputType);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, intermediateType, outputType, true, factory);
}
Aggregations