Search in sources :

Example 21 with DynamicClassLoader

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

the class MultimapAggregationFunction method generateAggregation.

private InternalAggregationFunction generateAggregation(Type keyType, Type valueType, Type outputType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(MultimapAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType, valueType);
    MultimapAggregationStateSerializer stateSerializer = new MultimapAggregationStateSerializer(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.bindTo(keyType).bindTo(valueType), ImmutableList.of(new AccumulatorStateDescriptor(MultimapAggregationState.class, stateSerializer, new MultimapAggregationStateFactory(keyType, valueType, groupMode))), 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) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) 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)

Example 22 with DynamicClassLoader

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

the class AbstractMinMaxAggregationFunction method generateAggregation.

protected InternalAggregationFunction generateAggregation(Type type, MethodHandle compareMethodHandle) {
    DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(type);
    MethodHandle inputFunction;
    MethodHandle combineFunction;
    MethodHandle outputFunction;
    Class<? extends AccumulatorState> stateInterface;
    AccumulatorStateSerializer<?> stateSerializer;
    if (type.getJavaType() == long.class) {
        stateInterface = NullableLongState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = LONG_INPUT_FUNCTION.bindTo(compareMethodHandle);
        combineFunction = LONG_COMBINE_FUNCTION.bindTo(compareMethodHandle);
        outputFunction = LONG_OUTPUT_FUNCTION.bindTo(type);
    } else if (type.getJavaType() == double.class) {
        stateInterface = NullableDoubleState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = DOUBLE_INPUT_FUNCTION.bindTo(compareMethodHandle);
        combineFunction = DOUBLE_COMBINE_FUNCTION.bindTo(compareMethodHandle);
        outputFunction = DOUBLE_OUTPUT_FUNCTION.bindTo(type);
    } else if (type.getJavaType() == boolean.class) {
        stateInterface = NullableBooleanState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = BOOLEAN_INPUT_FUNCTION.bindTo(compareMethodHandle);
        combineFunction = BOOLEAN_COMBINE_FUNCTION.bindTo(compareMethodHandle);
        outputFunction = BOOLEAN_OUTPUT_FUNCTION.bindTo(type);
    } else {
        // native container type is Slice or Block
        stateInterface = BlockPositionState.class;
        stateSerializer = new BlockPositionStateSerializer(type);
        inputFunction = min ? BLOCK_POSITION_MIN_INPUT_FUNCTION.bindTo(type) : BLOCK_POSITION_MAX_INPUT_FUNCTION.bindTo(type);
        combineFunction = min ? BLOCK_POSITION_MIN_COMBINE_FUNCTION.bindTo(type) : BLOCK_POSITION_MAX_COMBINE_FUNCTION.bindTo(type);
        outputFunction = BLOCK_POSITION_OUTPUT_FUNCTION.bindTo(type);
    }
    AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateInterface, classLoader);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getNameSuffix(), type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createParameterMetadata(type), inputFunction, combineFunction, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, stateFactory)), type);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getNameSuffix(), inputTypes, ImmutableList.of(intermediateType), type, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) BlockPositionStateSerializer(io.prestosql.operator.aggregation.state.BlockPositionStateSerializer) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) NullableDoubleState(io.prestosql.operator.aggregation.state.NullableDoubleState) MethodHandle(java.lang.invoke.MethodHandle)

Example 23 with DynamicClassLoader

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

the class AbstractMinMaxNAggregationFunction method generateAggregation.

protected InternalAggregationFunction generateAggregation(Type type) {
    DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxNAggregationFunction.class.getClassLoader());
    BlockComparator comparator = typeToComparator.apply(type);
    List<Type> inputTypes = ImmutableList.of(type, BIGINT);
    MinMaxNStateSerializer stateSerializer = new MinMaxNStateSerializer(comparator, type);
    Type intermediateType = stateSerializer.getSerializedType();
    ArrayType outputType = new ArrayType(type);
    List<ParameterMetadata> inputParameterMetadata = ImmutableList.of(new ParameterMetadata(STATE), new ParameterMetadata(BLOCK_INPUT_CHANNEL, type), new ParameterMetadata(INPUT_CHANNEL, BIGINT), new ParameterMetadata(BLOCK_INDEX));
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getNameSuffix(), type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, INPUT_FUNCTION.bindTo(comparator).bindTo(type), COMBINE_FUNCTION, OUTPUT_FUNCTION.bindTo(outputType), ImmutableList.of(new AccumulatorStateDescriptor(MinMaxNState.class, stateSerializer, new MinMaxNStateFactory())), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getNameSuffix(), inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) MinMaxNStateSerializer(io.prestosql.operator.aggregation.state.MinMaxNStateSerializer) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) 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) MinMaxNStateFactory(io.prestosql.operator.aggregation.state.MinMaxNStateFactory)

Example 24 with DynamicClassLoader

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

the class TestStateCompiler method testComplexStateEstimatedSize.

@Test(invocationCount = 100, successPercentage = 90)
public void testComplexStateEstimatedSize() {
    Map<String, Type> fieldMap = ImmutableMap.of("Block", new ArrayType(BIGINT), "AnotherBlock", mapType(BIGINT, VARCHAR));
    AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
    TestComplexState groupedState = factory.createGroupedState();
    long initialRetainedSize = getComplexStateRetainedSize(groupedState);
    assertEquals(groupedState.getEstimatedSize(), initialRetainedSize);
    // BlockBigArray or SliceBigArray has an internal map that can grow in size when getting more blocks
    // need to handle the map overhead separately
    initialRetainedSize -= getReferenceCountMapOverhead(groupedState);
    for (int i = 0; i < 1000; i++) {
        long retainedSize = 0;
        ((GroupedAccumulatorState) groupedState).setGroupId(i);
        groupedState.setBoolean(true);
        groupedState.setLong(1);
        groupedState.setDouble(2.0);
        groupedState.setByte((byte) 3);
        groupedState.setInt(4);
        Slice slice = utf8Slice("test");
        retainedSize += slice.getRetainedSize();
        groupedState.setSlice(slice);
        slice = wrappedDoubleArray(1.0, 2.0, 3.0);
        retainedSize += slice.getRetainedSize();
        groupedState.setAnotherSlice(slice);
        groupedState.setYetAnotherSlice(null);
        Block array = createLongsBlock(45);
        retainedSize += array.getRetainedSizeInBytes();
        groupedState.setBlock(array);
        BlockBuilder mapBlockBuilder = mapType(BIGINT, VARCHAR).createBlockBuilder(null, 1);
        BlockBuilder singleMapBlockWriter = mapBlockBuilder.beginBlockEntry();
        BIGINT.writeLong(singleMapBlockWriter, 123L);
        VARCHAR.writeSlice(singleMapBlockWriter, utf8Slice("testBlock"));
        mapBlockBuilder.closeEntry();
        Block map = mapBlockBuilder.build();
        retainedSize += map.getRetainedSizeInBytes();
        groupedState.setAnotherBlock(map);
        assertEquals(groupedState.getEstimatedSize(), initialRetainedSize + retainedSize * (i + 1) + getReferenceCountMapOverhead(groupedState));
    }
    for (int i = 0; i < 1000; i++) {
        long retainedSize = 0;
        ((GroupedAccumulatorState) groupedState).setGroupId(i);
        groupedState.setBoolean(true);
        groupedState.setLong(1);
        groupedState.setDouble(2.0);
        groupedState.setByte((byte) 3);
        groupedState.setInt(4);
        Slice slice = utf8Slice("test");
        retainedSize += slice.getRetainedSize();
        groupedState.setSlice(slice);
        slice = wrappedDoubleArray(1.0, 2.0, 3.0);
        retainedSize += slice.getRetainedSize();
        groupedState.setAnotherSlice(slice);
        groupedState.setYetAnotherSlice(null);
        Block array = createLongsBlock(45);
        retainedSize += array.getRetainedSizeInBytes();
        groupedState.setBlock(array);
        BlockBuilder mapBlockBuilder = mapType(BIGINT, VARCHAR).createBlockBuilder(null, 1);
        BlockBuilder singleMapBlockWriter = mapBlockBuilder.beginBlockEntry();
        BIGINT.writeLong(singleMapBlockWriter, 123L);
        VARCHAR.writeSlice(singleMapBlockWriter, utf8Slice("testBlock"));
        mapBlockBuilder.closeEntry();
        Block map = mapBlockBuilder.build();
        retainedSize += map.getRetainedSizeInBytes();
        groupedState.setAnotherBlock(map);
        assertEquals(groupedState.getEstimatedSize(), initialRetainedSize + retainedSize * 1000 + getReferenceCountMapOverhead(groupedState));
    }
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) ArrayType(io.prestosql.spi.type.ArrayType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) StructuralTestUtil.mapType(io.prestosql.util.StructuralTestUtil.mapType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Block(io.prestosql.spi.block.Block) BlockAssertions.createLongsBlock(io.prestosql.block.BlockAssertions.createLongsBlock) GroupedAccumulatorState(io.prestosql.spi.function.GroupedAccumulatorState) BlockBuilder(io.prestosql.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 25 with DynamicClassLoader

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

the class AbstractGreatestLeast method generate.

private Class<?> generate(List<Class<?>> javaTypes, Type type, MethodHandle compareMethod) {
    checkCondition(javaTypes.size() <= 127, NOT_SUPPORTED, "Too many arguments for function call %s()", getSignature().getName());
    String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(javaTypeName + "$" + getSignature().getName()), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    List<Parameter> parameters = IntStream.range(0, javaTypes.size()).mapToObj(i -> arg("arg" + i, javaTypes.get(i))).collect(toImmutableList());
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), getSignature().getNameSuffix(), type(javaTypes.get(0)), parameters);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    CallSiteBinder binder = new CallSiteBinder();
    if (type.getTypeSignature().getBase().equals(StandardTypes.DOUBLE)) {
        for (Parameter parameter : parameters) {
            body.append(parameter);
            body.append(invoke(binder.bind(CHECK_NOT_NAN.bindTo(getSignature().getName().toString())), "checkNotNaN"));
        }
    }
    Variable value = scope.declareVariable(javaTypes.get(0), "value");
    body.append(value.set(parameters.get(0)));
    for (int i = 1; i < javaTypes.size(); i++) {
        body.append(new IfStatement().condition(new BytecodeBlock().append(parameters.get(i)).append(value).append(invoke(binder.bind(compareMethod), "compare"))).ifTrue(value.set(parameters.get(i))));
    }
    body.append(value.ret());
    return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
Also used : BytecodeUtils.invoke(io.prestosql.sql.gen.BytecodeUtils.invoke) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) Scope(io.airlift.bytecode.Scope) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) BoundVariables(io.prestosql.metadata.BoundVariables) CompilerUtils.makeClassName(io.prestosql.util.CompilerUtils.makeClassName) Access.a(io.airlift.bytecode.Access.a) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) Parameter.arg(io.airlift.bytecode.Parameter.arg) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) CallSiteBinder(io.prestosql.sql.gen.CallSiteBinder) MethodDefinition(io.airlift.bytecode.MethodDefinition) PrestoException(io.prestosql.spi.PrestoException) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) PRIVATE(io.airlift.bytecode.Access.PRIVATE) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) ClassDefinition(io.airlift.bytecode.ClassDefinition) IntStream(java.util.stream.IntStream) ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle) FunctionKind(io.prestosql.spi.function.FunctionKind) StandardTypes(io.prestosql.spi.type.StandardTypes) Signature.orderableTypeParameter(io.prestosql.spi.function.Signature.orderableTypeParameter) TypeSignatureProvider(io.prestosql.sql.analyzer.TypeSignatureProvider) Parameter(io.airlift.bytecode.Parameter) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ImmutableList(com.google.common.collect.ImmutableList) Failures.checkCondition(io.prestosql.util.Failures.checkCondition) CompilerUtils.defineClass(io.prestosql.util.CompilerUtils.defineClass) Objects.requireNonNull(java.util.Objects.requireNonNull) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) Signature(io.prestosql.spi.function.Signature) FINAL(io.airlift.bytecode.Access.FINAL) STATIC(io.airlift.bytecode.Access.STATIC) IfStatement(io.airlift.bytecode.control.IfStatement) PUBLIC(io.airlift.bytecode.Access.PUBLIC) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Variable(io.airlift.bytecode.Variable) 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) Signature.orderableTypeParameter(io.prestosql.spi.function.Signature.orderableTypeParameter) Parameter(io.airlift.bytecode.Parameter)

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