Search in sources :

Example 26 with CallSiteBinder

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

the class LambdaBytecodeGenerator method compileLambdaProvider.

public static Class<? extends LambdaProvider> compileLambdaProvider(LambdaDefinitionExpression lambdaExpression, Metadata metadata, SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, Class lambdaInterface) {
    ClassDefinition lambdaProviderClassDefinition = new ClassDefinition(a(PUBLIC, Access.FINAL), makeClassName("LambdaProvider"), type(Object.class), type(LambdaProvider.class));
    FieldDefinition propertiesField = lambdaProviderClassDefinition.declareField(a(PRIVATE), "properties", SqlFunctionProperties.class);
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(lambdaProviderClassDefinition, callSiteBinder);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, lambdaExpression, metadata, sqlFunctionProperties, sessionFunctions);
    MethodDefinition method = lambdaProviderClassDefinition.declareMethod(a(PUBLIC), "getLambda", type(Object.class), ImmutableList.of());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    scope.declareVariable("wasNull", body, constantFalse());
    scope.declareVariable("properties", body, method.getThis().getField(propertiesField));
    RowExpressionCompiler rowExpressionCompiler = new RowExpressionCompiler(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, variableReferenceCompiler(ImmutableMap.of()), metadata, sqlFunctionProperties, sessionFunctions, compiledLambdaMap);
    BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, scope, callSiteBinder, cachedInstanceBinder, metadata.getFunctionAndTypeManager());
    body.append(generateLambda(generatorContext, ImmutableList.of(), compiledLambdaMap.get(lambdaExpression), lambdaInterface)).retObject();
    // constructor
    Parameter propertiesParameter = arg("properties", SqlFunctionProperties.class);
    MethodDefinition constructorDefinition = lambdaProviderClassDefinition.declareConstructor(a(PUBLIC), propertiesParameter);
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable constructorThisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(constructorThisVariable).invokeConstructor(Object.class).append(constructorThisVariable.setField(propertiesField, propertiesParameter));
    cachedInstanceBinder.generateInitializations(constructorThisVariable, constructorBody);
    constructorBody.ret();
    return defineClass(lambdaProviderClassDefinition, LambdaProvider.class, callSiteBinder.getBindings(), AccumulatorCompiler.class.getClassLoader());
}
Also used : Variable(com.facebook.presto.bytecode.Variable) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) LambdaProvider(com.facebook.presto.operator.aggregation.LambdaProvider) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder) Parameter(com.facebook.presto.bytecode.Parameter) AccumulatorCompiler(com.facebook.presto.operator.aggregation.AccumulatorCompiler) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Example 27 with CallSiteBinder

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

the class JoinCompiler method internalCompileHashStrategy.

private Class<? extends PagesHashStrategy> internalCompileHashStrategy(List<Type> types, List<Integer> outputChannels, List<Integer> joinChannels, Optional<Integer> sortChannel) {
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("PagesHashStrategy"), type(Object.class), type(PagesHashStrategy.class));
    FieldDefinition instanceSizeField = generateInstanceSize(classDefinition);
    FieldDefinition sizeField = classDefinition.declareField(a(PRIVATE, FINAL), "size", type(long.class));
    List<FieldDefinition> channelFields = new ArrayList<>();
    for (int i = 0; i < types.size(); i++) {
        FieldDefinition channelField = classDefinition.declareField(a(PRIVATE, FINAL), "channel_" + i, type(List.class, Block.class));
        channelFields.add(channelField);
    }
    List<Type> joinChannelTypes = new ArrayList<>();
    List<FieldDefinition> joinChannelFields = new ArrayList<>();
    for (int i = 0; i < joinChannels.size(); i++) {
        joinChannelTypes.add(types.get(joinChannels.get(i)));
        FieldDefinition channelField = classDefinition.declareField(a(PRIVATE, FINAL), "joinChannel_" + i, type(List.class, Block.class));
        joinChannelFields.add(channelField);
    }
    FieldDefinition hashChannelField = classDefinition.declareField(a(PRIVATE, FINAL), "hashChannel", type(List.class, Block.class));
    generateConstructor(classDefinition, joinChannels, sizeField, instanceSizeField, channelFields, joinChannelFields, hashChannelField);
    generateGetChannelCountMethod(classDefinition, outputChannels.size());
    generateGetSizeInBytesMethod(classDefinition, sizeField);
    generateAppendToMethod(classDefinition, callSiteBinder, types, outputChannels, channelFields);
    generateHashPositionMethod(classDefinition, callSiteBinder, joinChannelTypes, joinChannelFields, hashChannelField);
    generateHashRowMethod(classDefinition, callSiteBinder, joinChannelTypes);
    generateRowEqualsRowMethod(classDefinition, callSiteBinder, joinChannelTypes);
    generatePositionEqualsRowMethod(classDefinition, callSiteBinder, joinChannelTypes, joinChannelFields, true);
    generatePositionEqualsRowMethod(classDefinition, callSiteBinder, joinChannelTypes, joinChannelFields, false);
    generatePositionNotDistinctFromRowWithPageMethod(classDefinition, callSiteBinder, joinChannelTypes, joinChannelFields);
    generatePositionEqualsRowWithPageMethod(classDefinition, callSiteBinder, joinChannelTypes, joinChannelFields);
    generatePositionEqualsPositionMethod(classDefinition, callSiteBinder, joinChannelTypes, joinChannelFields, true);
    generatePositionEqualsPositionMethod(classDefinition, callSiteBinder, joinChannelTypes, joinChannelFields, false);
    generateIsPositionNull(classDefinition, joinChannelFields);
    generateCompareSortChannelPositionsMethod(classDefinition, callSiteBinder, types, channelFields, sortChannel);
    generateIsSortChannelPositionNull(classDefinition, channelFields, sortChannel);
    return defineClass(classDefinition, PagesHashStrategy.class, callSiteBinder.getBindings(), getClass().getClassLoader());
}
Also used : BigintType(com.facebook.presto.common.type.BigintType) Type(com.facebook.presto.common.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) OperatorType(com.facebook.presto.common.function.OperatorType) PagesHashStrategy(com.facebook.presto.operator.PagesHashStrategy) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) ArrayList(java.util.ArrayList) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Block(com.facebook.presto.common.block.Block) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition)

Example 28 with CallSiteBinder

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

the class OrderingCompiler method generatePageWithPositionComparatorClass.

private Class<? extends PageWithPositionComparator> generatePageWithPositionComparatorClass(List<Type> sortTypes, List<Integer> sortChannels, List<SortOrder> sortOrders) {
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("PageWithPositionComparator"), type(Object.class), type(PageWithPositionComparator.class));
    classDefinition.declareDefaultConstructor(a(PUBLIC));
    generateMergeSortCompareTo(classDefinition, callSiteBinder, sortTypes, sortChannels, sortOrders);
    return defineClass(classDefinition, PageWithPositionComparator.class, callSiteBinder.getBindings(), getClass().getClassLoader());
}
Also used : CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) SimplePageWithPositionComparator(com.facebook.presto.operator.SimplePageWithPositionComparator) PageWithPositionComparator(com.facebook.presto.operator.PageWithPositionComparator)

Example 29 with CallSiteBinder

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

the class StateCompiler method generateStateSerializer.

public static <T> AccumulatorStateSerializer<T> generateStateSerializer(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
    if (metadata != null && metadata.stateSerializerClass() != void.class) {
        try {
            return (AccumulatorStateSerializer<T>) metadata.stateSerializerClass().getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(clazz.getSimpleName() + "Serializer"), type(Object.class), type(AccumulatorStateSerializer.class));
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    // Generate constructor
    definition.declareDefaultConstructor(a(PUBLIC));
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    generateGetSerializedType(definition, fields, callSiteBinder);
    generateSerialize(definition, callSiteBinder, clazz, fields);
    generateDeserialize(definition, callSiteBinder, clazz, fields);
    Class<? extends AccumulatorStateSerializer> serializerClass = defineClass(definition, AccumulatorStateSerializer.class, callSiteBinder.getBindings(), classLoader);
    try {
        return (AccumulatorStateSerializer<T>) serializerClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : AccumulatorStateMetadata(com.facebook.presto.spi.function.AccumulatorStateMetadata) AccumulatorStateSerializer(com.facebook.presto.spi.function.AccumulatorStateSerializer) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder)

Aggregations

CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)29 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)27 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)19 Variable (com.facebook.presto.bytecode.Variable)17 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)16 Parameter (com.facebook.presto.bytecode.Parameter)16 Scope (com.facebook.presto.bytecode.Scope)15 ImmutableList (com.google.common.collect.ImmutableList)13 IfStatement (com.facebook.presto.bytecode.control.IfStatement)12 Block (com.facebook.presto.common.block.Block)12 Type (com.facebook.presto.common.type.Type)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)8 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)8 List (java.util.List)8 ForLoop (com.facebook.presto.bytecode.control.ForLoop)6 SqlTypeBytecodeExpression.constantType (com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType)6 PRIVATE (com.facebook.presto.bytecode.Access.PRIVATE)5 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)5 Access.a (com.facebook.presto.bytecode.Access.a)5