Search in sources :

Example 16 with DynamicClassLoader

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);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) DumpBytecodeVisitor(com.facebook.presto.bytecode.DumpBytecodeVisitor) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) SmartClassWriter(com.facebook.presto.bytecode.SmartClassWriter) ClassWriter(org.objectweb.asm.ClassWriter) SmartClassWriter(com.facebook.presto.bytecode.SmartClassWriter)

Example 17 with DynamicClassLoader

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);
    }
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ConstantCallSite(java.lang.invoke.ConstantCallSite) MethodHandle(java.lang.invoke.MethodHandle)

Example 18 with DynamicClassLoader

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);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) LookupSource(com.facebook.presto.operator.LookupSource) SimpleJoinProbe(com.facebook.presto.operator.SimpleJoinProbe) Page(com.facebook.presto.spi.Page) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) JoinProbe(com.facebook.presto.operator.JoinProbe) SimpleJoinProbe(com.facebook.presto.operator.SimpleJoinProbe) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) JoinProbeFactory(com.facebook.presto.operator.JoinProbeFactory) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 19 with DynamicClassLoader

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);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) MultiKeyValuePairsStateFactory(com.facebook.presto.operator.aggregation.state.MultiKeyValuePairsStateFactory) MultiKeyValuePairStateSerializer(com.facebook.presto.operator.aggregation.state.MultiKeyValuePairStateSerializer) MapType(com.facebook.presto.type.MapType)

Example 20 with DynamicClassLoader

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);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) KeyValuePairStateSerializer(com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer) KeyValuePairsStateFactory(com.facebook.presto.operator.aggregation.state.KeyValuePairsStateFactory) MapType(com.facebook.presto.type.MapType)

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