Search in sources :

Example 6 with DynamicClassLoader

use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.

the class ArrayAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type type, ArrayAggGroupImplementation groupMode) {
    DynamicClassLoader classLoader = new DynamicClassLoader(ArrayAggregationFunction.class.getClassLoader());
    AccumulatorStateSerializer<?> stateSerializer = new ArrayAggregationStateSerializer(type);
    AccumulatorStateFactory<?> stateFactory = new ArrayAggregationStateFactory(type, groupMode);
    List<Type> inputTypes = ImmutableList.of(type);
    Type outputType = new ArrayType(type);
    Type intermediateType = stateSerializer.getSerializedType();
    List<ParameterMetadata> inputParameterMetadata = createInputParameterMetadata(type);
    MethodHandle inputFunction = INPUT_FUNCTION.bindTo(type);
    MethodHandle combineFunction = COMBINE_FUNCTION.bindTo(type);
    MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(type);
    Class<? extends AccumulatorState> stateInterface = ArrayAggregationState.class;
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, inputFunction, combineFunction, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, stateFactory)), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) ParameterMetadata(io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata) ArrayType(io.prestosql.spi.type.ArrayType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata) MethodHandle(java.lang.invoke.MethodHandle)

Example 7 with DynamicClassLoader

use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.

the class MapAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType, MapType outputType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(MapAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType, valueType);
    KeyValuePairStateSerializer stateSerializer = new KeyValuePairStateSerializer(outputType);
    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, ImmutableList.of(new AccumulatorStateDescriptor(KeyValuePairsState.class, stateSerializer, new KeyValuePairsStateFactory(keyType, valueType))), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) KeyValuePairStateSerializer(io.prestosql.operator.aggregation.state.KeyValuePairStateSerializer) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) KeyValuePairsStateFactory(io.prestosql.operator.aggregation.state.KeyValuePairsStateFactory)

Example 8 with DynamicClassLoader

use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.

the class Histogram method generateAggregation.

private static InternalAggregationFunction generateAggregation(String functionName, Type keyType, Type outputType, HistogramGroupImplementation groupMode) {
    DynamicClassLoader classLoader = new DynamicClassLoader(Histogram.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType);
    HistogramStateSerializer stateSerializer = new HistogramStateSerializer(keyType, outputType);
    Type intermediateType = stateSerializer.getSerializedType();
    MethodHandle inputFunction = INPUT_FUNCTION.bindTo(keyType);
    MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(outputType);
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(functionName, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType), inputFunction, COMBINE_FUNCTION, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(HistogramState.class, stateSerializer, new HistogramStateFactory(keyType, EXPECTED_SIZE_FOR_HASHING, groupMode))), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(functionName, inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Type(io.prestosql.spi.type.Type) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) MethodHandle(java.lang.invoke.MethodHandle)

Example 9 with DynamicClassLoader

use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.

the class VarArgsToMapAdapterGenerator method generateVarArgsToMapAdapter.

/**
 * Generate byte code that
 * <p><ul>
 * <li>takes a specified number of variables as arguments (types of the arguments are provided in {@code javaTypes})
 * <li>put the variables in a map (keys of the map are provided in {@code names})
 * <li>invoke the provided {@code function} with the map
 * <li>return with the result of the function call (type must match {@code returnType})
 * </ul></p>
 */
public static MethodHandle generateVarArgsToMapAdapter(Class<?> returnType, List<Class<?>> javaTypes, List<String> names, Function<Map<String, Object>, Object> function) {
    checkCondition(javaTypes.size() <= 254, NOT_SUPPORTED, "Too many arguments for vararg function");
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("VarArgsToMapAdapter"), type(Object.class));
    ImmutableList.Builder<Parameter> parameterListBuilder = ImmutableList.builder();
    for (int i = 0; i < javaTypes.size(); i++) {
        Class<?> javaType = javaTypes.get(i);
        parameterListBuilder.add(arg("input_" + i, javaType));
    }
    ImmutableList<Parameter> parameterList = parameterListBuilder.build();
    MethodDefinition methodDefinition = classDefinition.declareMethod(a(PUBLIC, STATIC), "varArgsToMap", type(returnType), parameterList);
    BytecodeBlock body = methodDefinition.getBody();
    // ImmutableMap.Builder can not be used here because it doesn't allow nulls.
    Variable map = methodDefinition.getScope().declareVariable("map", methodDefinition.getBody(), invokeStatic(Maps.class, "newHashMapWithExpectedSize", HashMap.class, constantInt(javaTypes.size())));
    for (int i = 0; i < javaTypes.size(); i++) {
        body.append(map.invoke("put", Object.class, constantString(names.get(i)).cast(Object.class), parameterList.get(i).cast(Object.class)));
    }
    body.append(loadConstant(callSiteBinder, function, Function.class).invoke("apply", Object.class, map.cast(Object.class)).cast(returnType).ret());
    Class<?> generatedClass = defineClass(classDefinition, Object.class, callSiteBinder.getBindings(), new DynamicClassLoader(VarArgsToMapAdapterGenerator.class.getClassLoader()));
    return Reflection.methodHandle(generatedClass, "varArgsToMap", javaTypes.toArray(new Class<?>[javaTypes.size()]));
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Variable(io.airlift.bytecode.Variable) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) Maps(com.google.common.collect.Maps) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) CompilerUtils.defineClass(io.prestosql.util.CompilerUtils.defineClass)

Example 10 with DynamicClassLoader

use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.

the class ArrayConstructor method generateArrayConstructor.

private static Class<?> generateArrayConstructor(List<Class<?>> stackTypes, Type elementType) {
    checkCondition(stackTypes.size() <= 254, NOT_SUPPORTED, "Too many arguments for array constructor");
    List<String> stackTypeNames = stackTypes.stream().map(Class::getSimpleName).collect(toImmutableList());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(Joiner.on("").join(stackTypeNames) + "ArrayConstructor"), type(Object.class));
    // Generate constructor
    definition.declareDefaultConstructor(a(PRIVATE));
    // Generate arrayConstructor()
    ImmutableList.Builder<Parameter> parameters = ImmutableList.builder();
    for (int i = 0; i < stackTypes.size(); i++) {
        Class<?> stackType = stackTypes.get(i);
        parameters.add(arg("arg" + i, stackType));
    }
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "arrayConstructor", type(Block.class), parameters.build());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable blockBuilderVariable = scope.declareVariable(BlockBuilder.class, "blockBuilder");
    CallSiteBinder binder = new CallSiteBinder();
    BytecodeExpression createBlockBuilder = blockBuilderVariable.set(constantType(binder, elementType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), constantInt(stackTypes.size())));
    body.append(createBlockBuilder);
    for (int i = 0; i < stackTypes.size(); i++) {
        Variable argument = scope.getVariable("arg" + i);
        IfStatement ifStatement = new IfStatement().condition(equal(argument, constantNull(stackTypes.get(i)))).ifTrue(blockBuilderVariable.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, elementType).writeValue(blockBuilderVariable, argument.cast(elementType.getJavaType())));
        body.append(ifStatement);
    }
    body.append(blockBuilderVariable.invoke("build", Block.class).ret());
    return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(ArrayConstructor.class.getClassLoader()));
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Variable(io.airlift.bytecode.Variable) Signature.typeVariable(io.prestosql.spi.function.Signature.typeVariable) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) IfStatement(io.airlift.bytecode.control.IfStatement) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) CallSiteBinder(io.prestosql.sql.gen.CallSiteBinder) Parameter(io.airlift.bytecode.Parameter) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Block(io.prestosql.spi.block.Block) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Aggregations

DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)38 Type (io.prestosql.spi.type.Type)20 AccumulatorStateDescriptor (io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)19 MethodHandle (java.lang.invoke.MethodHandle)14 ClassDefinition (io.airlift.bytecode.ClassDefinition)11 ImmutableList (com.google.common.collect.ImmutableList)7 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)7 MethodDefinition (io.airlift.bytecode.MethodDefinition)7 Parameter (io.airlift.bytecode.Parameter)7 Variable (io.airlift.bytecode.Variable)7 ArrayType (io.prestosql.spi.type.ArrayType)7 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 Scope (io.airlift.bytecode.Scope)5 AggregationMetadata (io.prestosql.operator.aggregation.AggregationMetadata)5 ParameterMetadata (io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata)5 GenericAccumulatorFactoryBinder (io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder)5 InternalAggregationFunction (io.prestosql.operator.aggregation.InternalAggregationFunction)5 IfStatement (io.airlift.bytecode.control.IfStatement)4 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)4 CallSiteBinder (io.trino.sql.gen.CallSiteBinder)4