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());
}
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());
}
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);
}
}
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);
}
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);
}
}
Aggregations