Search in sources :

Example 41 with ClassDefinition

use of com.facebook.presto.bytecode.ClassDefinition 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 42 with ClassDefinition

use of com.facebook.presto.bytecode.ClassDefinition 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 43 with ClassDefinition

use of com.facebook.presto.bytecode.ClassDefinition 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)

Example 44 with ClassDefinition

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

the class StateCompiler method generateSingleStateClass.

private static <T> Class<? extends T> generateSingleStateClass(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("Single" + clazz.getSimpleName()), type(Object.class), type(clazz));
    FieldDefinition instanceSize = generateInstanceSize(definition);
    // Add getter for class size
    definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)).getBody().getStaticField(instanceSize).retLong();
    // Generate constructor
    MethodDefinition constructor = definition.declareConstructor(a(PUBLIC));
    constructor.getBody().append(constructor.getThis()).invokeConstructor(Object.class);
    // Generate fields
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    for (StateField field : fields) {
        generateField(definition, constructor, field);
    }
    constructor.getBody().ret();
    return defineClass(definition, clazz, classLoader);
}
Also used : MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition)

Example 45 with ClassDefinition

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

the class StateCompiler method generateStateFactory.

public static <T> AccumulatorStateFactory<T> generateStateFactory(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
    if (metadata != null && metadata.stateFactoryClass() != void.class) {
        try {
            return (AccumulatorStateFactory<T>) metadata.stateFactoryClass().getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    Class<? extends T> singleStateClass = generateSingleStateClass(clazz, fieldTypes, classLoader);
    Class<? extends T> groupedStateClass = generateGroupedStateClass(clazz, fieldTypes, classLoader);
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(clazz.getSimpleName() + "Factory"), type(Object.class), type(AccumulatorStateFactory.class));
    // Generate constructor
    definition.declareDefaultConstructor(a(PUBLIC));
    // Generate single state creation method
    definition.declareMethod(a(PUBLIC), "createSingleState", type(Object.class)).getBody().newObject(singleStateClass).dup().invokeConstructor(singleStateClass).retObject();
    // Generate grouped state creation method
    definition.declareMethod(a(PUBLIC), "createGroupedState", type(Object.class)).getBody().newObject(groupedStateClass).dup().invokeConstructor(groupedStateClass).retObject();
    // Generate getters for state class
    definition.declareMethod(a(PUBLIC), "getSingleStateClass", type(Class.class, singleStateClass)).getBody().push(singleStateClass).retObject();
    definition.declareMethod(a(PUBLIC), "getGroupedStateClass", type(Class.class, groupedStateClass)).getBody().push(groupedStateClass).retObject();
    Class<? extends AccumulatorStateFactory> factoryClass = defineClass(definition, AccumulatorStateFactory.class, classLoader);
    try {
        return (AccumulatorStateFactory<T>) factoryClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : AccumulatorStateMetadata(com.facebook.presto.spi.function.AccumulatorStateMetadata) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) AccumulatorStateFactory(com.facebook.presto.spi.function.AccumulatorStateFactory)

Aggregations

ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)45 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)27 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)26 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)25 Parameter (com.facebook.presto.bytecode.Parameter)23 Variable (com.facebook.presto.bytecode.Variable)21 Scope (com.facebook.presto.bytecode.Scope)19 ImmutableList (com.google.common.collect.ImmutableList)18 List (java.util.List)14 IfStatement (com.facebook.presto.bytecode.control.IfStatement)13 Block (com.facebook.presto.common.block.Block)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)11 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)10 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)10 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)9 Type (com.facebook.presto.common.type.Type)9 PrestoException (com.facebook.presto.spi.PrestoException)9 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)8 Access.a (com.facebook.presto.bytecode.Access.a)8 ParameterizedType.type (com.facebook.presto.bytecode.ParameterizedType.type)8