Search in sources :

Example 21 with BoundSignature

use of io.trino.metadata.BoundSignature in project trino by trinodb.

the class SimplifyCountOverConstant method isCountOverConstant.

private boolean isCountOverConstant(Session session, AggregationNode.Aggregation aggregation, Assignments inputs) {
    BoundSignature signature = aggregation.getResolvedFunction().getSignature();
    if (!signature.getName().equals("count") || signature.getArgumentTypes().size() != 1) {
        return false;
    }
    Expression argument = aggregation.getArguments().get(0);
    if (argument instanceof SymbolReference) {
        argument = inputs.get(Symbol.from(argument));
    }
    if (isEffectivelyLiteral(plannerContext, session, argument)) {
        Object value = evaluateConstantExpression(argument, ImmutableMap.of(), ImmutableSet.of(), plannerContext, session, new AllowAllAccessControl(), ImmutableSet.of(), ImmutableMap.of());
        verify(!(value instanceof Expression));
        return value != null;
    }
    return false;
}
Also used : ExpressionInterpreter.evaluateConstantExpression(io.trino.sql.planner.ExpressionInterpreter.evaluateConstantExpression) Expression(io.trino.sql.tree.Expression) SymbolReference(io.trino.sql.tree.SymbolReference) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) BoundSignature(io.trino.metadata.BoundSignature)

Example 22 with BoundSignature

use of io.trino.metadata.BoundSignature in project trino by trinodb.

the class AbstractGreatestLeast method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    Type type = boundSignature.getReturnType();
    checkArgument(type.isOrderable(), "Type must be orderable");
    MethodHandle compareMethod = getMinMaxCompare(functionDependencies, type, simpleConvention(FAIL_ON_NULL, NEVER_NULL, NEVER_NULL), min);
    List<Class<?>> javaTypes = IntStream.range(0, boundSignature.getArity()).mapToObj(i -> wrap(type.getJavaType())).collect(toImmutableList());
    Class<?> clazz = generate(javaTypes, compareMethod);
    MethodHandle methodHandle = methodHandle(clazz, getFunctionMetadata().getSignature().getName(), javaTypes.toArray(new Class<?>[0]));
    return new ChoicesScalarFunctionImplementation(boundSignature, NULLABLE_RETURN, nCopies(javaTypes.size(), BOXED_NULLABLE), methodHandle);
}
Also used : FunctionDependencies(io.trino.metadata.FunctionDependencies) SCALAR(io.trino.metadata.FunctionKind.SCALAR) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) FunctionNullability(io.trino.metadata.FunctionNullability) Scope(io.airlift.bytecode.Scope) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) Access.a(io.airlift.bytecode.Access.a) BOOTSTRAP_METHOD(io.trino.sql.gen.Bootstrap.BOOTSTRAP_METHOD) Parameter.arg(io.airlift.bytecode.Parameter.arg) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CompilerUtils.makeClassName(io.trino.util.CompilerUtils.makeClassName) MinMaxCompare.getMinMaxCompare(io.trino.util.MinMaxCompare.getMinMaxCompare) BytecodeExpressions.constantNull(io.airlift.bytecode.expression.BytecodeExpressions.constantNull) FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) MethodDefinition(io.airlift.bytecode.MethodDefinition) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) Collectors.joining(java.util.stream.Collectors.joining) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass) Signature.orderableTypeParameter(io.trino.metadata.Signature.orderableTypeParameter) List(java.util.List) PRIVATE(io.airlift.bytecode.Access.PRIVATE) BytecodeExpressions.or(io.airlift.bytecode.expression.BytecodeExpressions.or) Failures.checkCondition(io.trino.util.Failures.checkCondition) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) ClassDefinition(io.airlift.bytecode.ClassDefinition) IntStream(java.util.stream.IntStream) ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) MethodHandle(java.lang.invoke.MethodHandle) Type(io.trino.spi.type.Type) Parameter(io.airlift.bytecode.Parameter) ImmutableList(com.google.common.collect.ImmutableList) LabelNode(io.airlift.bytecode.instruction.LabelNode) Signature(io.trino.metadata.Signature) NULLABLE_RETURN(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN) FINAL(io.airlift.bytecode.Access.FINAL) STATIC(io.airlift.bytecode.Access.STATIC) MethodType.methodType(java.lang.invoke.MethodType.methodType) BytecodeExpressions.invokeDynamic(io.airlift.bytecode.expression.BytecodeExpressions.invokeDynamic) BOXED_NULLABLE(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BOXED_NULLABLE) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) IfStatement(io.airlift.bytecode.control.IfStatement) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) PUBLIC(io.airlift.bytecode.Access.PUBLIC) MinMaxCompare.getMinMaxCompareFunctionDependencies(io.trino.util.MinMaxCompare.getMinMaxCompareFunctionDependencies) BoundSignature(io.trino.metadata.BoundSignature) BytecodeExpressions.isNull(io.airlift.bytecode.expression.BytecodeExpressions.isNull) Primitives.wrap(com.google.common.primitives.Primitives.wrap) Reflection.methodHandle(io.trino.util.Reflection.methodHandle) Type(io.trino.spi.type.Type) MethodType.methodType(java.lang.invoke.MethodType.methodType) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass) MethodHandle(java.lang.invoke.MethodHandle)

Example 23 with BoundSignature

use of io.trino.metadata.BoundSignature in project trino by trinodb.

the class ParametricAggregation method specialize.

@Override
public AggregationMetadata specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    // Find implementation matching arguments
    AggregationImplementation concreteImplementation = findMatchingImplementation(boundSignature);
    // Build state factory and serializer
    AccumulatorStateDescriptor<?> accumulatorStateDescriptor = generateAccumulatorStateDescriptor(stateClass);
    // Bind provided dependencies to aggregation method handlers
    FunctionMetadata metadata = getFunctionMetadata();
    FunctionBinding functionBinding = SignatureBinder.bindFunction(metadata.getFunctionId(), metadata.getSignature(), boundSignature);
    MethodHandle inputHandle = bindDependencies(concreteImplementation.getInputFunction(), concreteImplementation.getInputDependencies(), functionBinding, functionDependencies);
    Optional<MethodHandle> removeInputHandle = concreteImplementation.getRemoveInputFunction().map(removeInputFunction -> bindDependencies(removeInputFunction, concreteImplementation.getRemoveInputDependencies(), functionBinding, functionDependencies));
    Optional<MethodHandle> combineHandle = concreteImplementation.getCombineFunction();
    if (getAggregationMetadata().isDecomposable()) {
        checkArgument(combineHandle.isPresent(), "Decomposable method %s does not have a combine method", boundSignature.getName());
        combineHandle = combineHandle.map(combineFunction -> bindDependencies(combineFunction, concreteImplementation.getCombineDependencies(), functionBinding, functionDependencies));
    } else {
        checkArgument(concreteImplementation.getCombineFunction().isEmpty(), "Decomposable method %s does not have a combine method", boundSignature.getName());
    }
    MethodHandle outputHandle = bindDependencies(concreteImplementation.getOutputFunction(), concreteImplementation.getOutputDependencies(), functionBinding, functionDependencies);
    List<AggregationParameterKind> inputParameterKinds = concreteImplementation.getInputParameterKinds();
    inputHandle = normalizeInputMethod(inputHandle, boundSignature, inputParameterKinds);
    removeInputHandle = removeInputHandle.map(function -> normalizeInputMethod(function, boundSignature, inputParameterKinds));
    return new AggregationMetadata(inputHandle, removeInputHandle, combineHandle, outputHandle, ImmutableList.of(accumulatorStateDescriptor));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) FunctionDependencies(io.trino.metadata.FunctionDependencies) FunctionDependencyDeclarationBuilder(io.trino.metadata.FunctionDependencyDeclaration.FunctionDependencyDeclarationBuilder) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) ImplementationDependency(io.trino.operator.annotations.ImplementationDependency) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature(io.trino.metadata.Signature) FunctionMetadata(io.trino.metadata.FunctionMetadata) StateCompiler.getSerializedType(io.trino.operator.aggregation.state.StateCompiler.getSerializedType) AggregationFunctionAdapter.normalizeInputMethod(io.trino.operator.aggregation.AggregationFunctionAdapter.normalizeInputMethod) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) ParametricImplementationsGroup(io.trino.operator.ParametricImplementationsGroup) ParametricFunctionHelpers.bindDependencies(io.trino.operator.ParametricFunctionHelpers.bindDependencies) Collection(java.util.Collection) TrinoException(io.trino.spi.TrinoException) StateCompiler.generateStateFactory(io.trino.operator.aggregation.state.StateCompiler.generateStateFactory) FunctionBinding(io.trino.metadata.FunctionBinding) StateCompiler.generateStateSerializer(io.trino.operator.aggregation.state.StateCompiler.generateStateSerializer) AGGREGATE(io.trino.metadata.FunctionKind.AGGREGATE) AccumulatorState(io.trino.spi.function.AccumulatorState) String.format(java.lang.String.format) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) AMBIGUOUS_FUNCTION_CALL(io.trino.spi.StandardErrorCode.AMBIGUOUS_FUNCTION_CALL) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) List(java.util.List) SignatureBinder(io.trino.metadata.SignatureBinder) BoundSignature(io.trino.metadata.BoundSignature) StringJoiner(java.util.StringJoiner) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FUNCTION_IMPLEMENTATION_MISSING(io.trino.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_MISSING) SqlAggregationFunction(io.trino.metadata.SqlAggregationFunction) FunctionMetadata(io.trino.metadata.FunctionMetadata) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) FunctionBinding(io.trino.metadata.FunctionBinding) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) MethodHandle(java.lang.invoke.MethodHandle)

Example 24 with BoundSignature

use of io.trino.metadata.BoundSignature in project trino by trinodb.

the class QuantileDigestAggregationFunction method specialize.

@Override
public AggregationMetadata specialize(BoundSignature boundSignature) {
    QuantileDigestType outputType = (QuantileDigestType) boundSignature.getReturnType();
    Type valueType = outputType.getValueType();
    int arity = boundSignature.getArity();
    QuantileDigestStateSerializer stateSerializer = new QuantileDigestStateSerializer(valueType);
    MethodHandle inputFunction = getMethodHandle(valueType, arity);
    inputFunction = normalizeInputMethod(inputFunction, boundSignature, ImmutableList.<AggregationParameterKind>builder().add(STATE).addAll(getInputTypes(valueType, arity).stream().map(ignored -> INPUT_CHANNEL).collect(Collectors.toList())).build());
    return new AggregationMetadata(inputFunction, Optional.empty(), Optional.of(COMBINE_FUNCTION), OUTPUT_FUNCTION.bindTo(stateSerializer), ImmutableList.of(new AccumulatorStateDescriptor<>(QuantileDigestState.class, stateSerializer, new QuantileDigestStateFactory())));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) QuantileDigest(io.airlift.stats.QuantileDigest) Signature.comparableTypeParameter(io.trino.metadata.Signature.comparableTypeParameter) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) MethodHandles.insertArguments(java.lang.invoke.MethodHandles.insertArguments) FunctionNullability(io.trino.metadata.FunctionNullability) Type(io.trino.spi.type.Type) DEFAULT_WEIGHT(io.trino.operator.scalar.QuantileDigestFunctions.DEFAULT_WEIGHT) QuantileDigestStateFactory(io.trino.operator.aggregation.state.QuantileDigestStateFactory) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) QDIGEST(io.trino.spi.type.StandardTypes.QDIGEST) ImmutableList(com.google.common.collect.ImmutableList) TypeSignature.parametricType(io.trino.spi.type.TypeSignature.parametricType) FloatingPointBitsConverterUtil.floatToSortableInt(io.trino.operator.aggregation.FloatingPointBitsConverterUtil.floatToSortableInt) QuantileDigestStateSerializer(io.trino.operator.aggregation.state.QuantileDigestStateSerializer) Signature(io.trino.metadata.Signature) FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) AggregationFunctionAdapter.normalizeInputMethod(io.trino.operator.aggregation.AggregationFunctionAdapter.normalizeInputMethod) QuantileDigestFunctions.verifyWeight(io.trino.operator.scalar.QuantileDigestFunctions.verifyWeight) STATE(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind.STATE) INPUT_CHANNEL(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind.INPUT_CHANNEL) Collections.nCopies(java.util.Collections.nCopies) QuantileDigestType(io.trino.spi.type.QuantileDigestType) StandardTypes(io.trino.spi.type.StandardTypes) QuantileDigestState(io.trino.operator.aggregation.state.QuantileDigestState) FloatingPointBitsConverterUtil.doubleToSortableLong(io.trino.operator.aggregation.FloatingPointBitsConverterUtil.doubleToSortableLong) AGGREGATE(io.trino.metadata.FunctionKind.AGGREGATE) Collectors(java.util.stream.Collectors) DEFAULT_ACCURACY(io.trino.operator.scalar.QuantileDigestFunctions.DEFAULT_ACCURACY) String.format(java.lang.String.format) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) List(java.util.List) QuantileDigestFunctions.verifyAccuracy(io.trino.operator.scalar.QuantileDigestFunctions.verifyAccuracy) BIGINT(io.trino.spi.type.BigintType.BIGINT) BoundSignature(io.trino.metadata.BoundSignature) Optional(java.util.Optional) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlAggregationFunction(io.trino.metadata.SqlAggregationFunction) Reflection.methodHandle(io.trino.util.Reflection.methodHandle) Type(io.trino.spi.type.Type) TypeSignature.parametricType(io.trino.spi.type.TypeSignature.parametricType) QuantileDigestType(io.trino.spi.type.QuantileDigestType) QuantileDigestStateSerializer(io.trino.operator.aggregation.state.QuantileDigestStateSerializer) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) QuantileDigestStateFactory(io.trino.operator.aggregation.state.QuantileDigestStateFactory) QuantileDigestType(io.trino.spi.type.QuantileDigestType) MethodHandle(java.lang.invoke.MethodHandle)

Example 25 with BoundSignature

use of io.trino.metadata.BoundSignature in project trino by trinodb.

the class TestAnnotationEngineForAggregates method testSimpleBlockInputAggregationParse.

@Test
public void testSimpleBlockInputAggregationParse() {
    Signature expectedSignature = new Signature("block_input_aggregate", DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()));
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(BlockInputAggregationFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with @BlockPosition usage");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertImplementationCount(implementations, 1, 0, 0);
    AggregationImplementation implementation = getOnlyElement(implementations.getExactImplementations().values());
    assertEquals(implementation.getDefinitionClass(), BlockInputAggregationFunction.class);
    assertDependencyCount(implementation, 0, 0, 0);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, BLOCK_INPUT_CHANNEL, BLOCK_INDEX));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Test(org.testng.annotations.Test)

Aggregations

BoundSignature (io.trino.metadata.BoundSignature)27 Signature (io.trino.metadata.Signature)20 TypeSignature (io.trino.spi.type.TypeSignature)18 Test (org.testng.annotations.Test)17 ParametricAggregation (io.trino.operator.aggregation.ParametricAggregation)13 AggregationFunctionMetadata (io.trino.metadata.AggregationFunctionMetadata)12 AggregationImplementation (io.trino.operator.aggregation.AggregationImplementation)12 MethodHandle (java.lang.invoke.MethodHandle)8 FunctionMetadata (io.trino.metadata.FunctionMetadata)7 ImmutableList (com.google.common.collect.ImmutableList)6 FunctionDependencies (io.trino.metadata.FunctionDependencies)6 List (java.util.List)6 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 FunctionNullability (io.trino.metadata.FunctionNullability)4 Type (io.trino.spi.type.Type)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 FunctionDependencyDeclaration (io.trino.metadata.FunctionDependencyDeclaration)3 ChoicesScalarFunctionImplementation (io.trino.operator.scalar.ChoicesScalarFunctionImplementation)3 TrinoException (io.trino.spi.TrinoException)3