Search in sources :

Example 16 with ResolvedFunction

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

the class TestPushdownLimitIntoWindow method assertWindowNotOrdered.

private void assertWindowNotOrdered(String rankingFunctionName) {
    ResolvedFunction ranking = tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of(rankingFunctionName), fromTypes());
    tester().assertThat(new PushdownLimitIntoWindow()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol rowNumberSymbol = p.symbol("row_number_1");
        return p.limit(3, p.window(new WindowNode.Specification(ImmutableList.of(a), Optional.empty()), ImmutableMap.of(rowNumberSymbol, newWindowNodeFunction(ranking, a)), p.values(a)));
    }).doesNotFire();
}
Also used : Symbol(io.trino.sql.planner.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) PlanMatchPattern.topNRanking(io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking) ResolvedFunction(io.trino.metadata.ResolvedFunction) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) DEFAULT_FRAME(io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME) SortOrder(io.trino.spi.connector.SortOrder) QualifiedName(io.trino.sql.tree.QualifiedName) PlanMatchPattern.limit(io.trino.sql.planner.assertions.PlanMatchPattern.limit) ImmutableList(com.google.common.collect.ImmutableList) Optional(java.util.Optional) WindowNode(io.trino.sql.planner.plan.WindowNode) WindowNode(io.trino.sql.planner.plan.WindowNode) ResolvedFunction(io.trino.metadata.ResolvedFunction) Symbol(io.trino.sql.planner.Symbol)

Example 17 with ResolvedFunction

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

the class LogicalPlanner method noTruncationCast.

/*
    According to the standard, for the purpose of store assignment (INSERT),
    no non-space characters of a character string, and no non-zero octets
    of a binary string must be lost when the inserted value is truncated to
    fit in the target column type.
    The following method returns a cast from source type to target type
    with a guarantee of no illegal truncation.
    TODO Once BINARY and parametric VARBINARY types are supported, they should be handled here.
    TODO This workaround is insufficient to handle structural types
     */
private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
    if (fromType instanceof UnknownType || (!(toType instanceof VarcharType) && !(toType instanceof CharType))) {
        return new Cast(expression, toSqlType(toType));
    }
    int targetLength;
    if (toType instanceof VarcharType) {
        if (((VarcharType) toType).isUnbounded()) {
            return new Cast(expression, toSqlType(toType));
        }
        targetLength = ((VarcharType) toType).getBoundedLength();
    } else {
        targetLength = ((CharType) toType).getLength();
    }
    checkState(fromType instanceof VarcharType || fromType instanceof CharType, "inserting non-character value to column of character type");
    ResolvedFunction spaceTrimmedLength = metadata.resolveFunction(session, QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
    ResolvedFunction fail = metadata.resolveFunction(session, QualifiedName.of("fail"), fromTypes(VARCHAR));
    return new IfExpression(// check if the trimmed value fits in the target type
    new ComparisonExpression(GREATER_THAN_OR_EQUAL, new GenericLiteral("BIGINT", Integer.toString(targetLength)), new CoalesceExpression(new FunctionCall(spaceTrimmedLength.toQualifiedName(), ImmutableList.of(new Cast(expression, toSqlType(VARCHAR)))), new GenericLiteral("BIGINT", "0"))), new Cast(expression, toSqlType(toType)), new Cast(new FunctionCall(fail.toQualifiedName(), ImmutableList.of(new Cast(new StringLiteral(format("Cannot truncate non-space characters when casting from %s to %s on INSERT", fromType.getDisplayName(), toType.getDisplayName())), toSqlType(VARCHAR)))), toSqlType(toType)));
}
Also used : UnknownType(io.trino.type.UnknownType) Cast(io.trino.sql.tree.Cast) IfExpression(io.trino.sql.tree.IfExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) StringLiteral(io.trino.sql.tree.StringLiteral) VarcharType(io.trino.spi.type.VarcharType) ResolvedFunction(io.trino.metadata.ResolvedFunction) CharType(io.trino.spi.type.CharType) FunctionCall(io.trino.sql.tree.FunctionCall) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) GenericLiteral(io.trino.sql.tree.GenericLiteral)

Example 18 with ResolvedFunction

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

the class StatisticsAggregationPlanner method createAggregation.

private ColumnStatisticsAggregation createAggregation(QualifiedName functionName, SymbolReference input, Type inputType, Type outputType) {
    ResolvedFunction resolvedFunction = metadata.resolveFunction(session, functionName, fromTypes(inputType));
    Type resolvedType = getOnlyElement(resolvedFunction.getSignature().getArgumentTypes());
    verify(resolvedType.equals(inputType), "resolved function input type does not match the input type: %s != %s", resolvedType, inputType);
    return new ColumnStatisticsAggregation(new AggregationNode.Aggregation(resolvedFunction, ImmutableList.of(input), false, Optional.empty(), Optional.empty(), Optional.empty()), outputType);
}
Also used : Type(io.trino.spi.type.Type) ColumnStatisticType(io.trino.spi.statistics.ColumnStatisticType) TableStatisticType(io.trino.spi.statistics.TableStatisticType) ResolvedFunction(io.trino.metadata.ResolvedFunction) AggregationNode(io.trino.sql.planner.plan.AggregationNode)

Example 19 with ResolvedFunction

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

the class TestDeterminismEvaluator method testDeterminismEvaluator.

@Test
public void testDeterminismEvaluator() {
    TestingFunctionResolution functionResolution = new TestingFunctionResolution();
    CallExpression random = new CallExpression(functionResolution.resolveFunction(QualifiedName.of("random"), fromTypes(BIGINT)), singletonList(constant(10L, BIGINT)));
    assertFalse(isDeterministic(random));
    InputReferenceExpression col0 = field(0, BIGINT);
    ResolvedFunction lessThan = functionResolution.resolveOperator(LESS_THAN, ImmutableList.of(BIGINT, BIGINT));
    CallExpression lessThanExpression = new CallExpression(lessThan, ImmutableList.of(col0, constant(10L, BIGINT)));
    assertTrue(isDeterministic(lessThanExpression));
    CallExpression lessThanRandomExpression = new CallExpression(lessThan, ImmutableList.of(col0, random));
    assertFalse(isDeterministic(lessThanRandomExpression));
}
Also used : TestingFunctionResolution(io.trino.metadata.TestingFunctionResolution) ResolvedFunction(io.trino.metadata.ResolvedFunction) Test(org.testng.annotations.Test)

Example 20 with ResolvedFunction

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

the class StatisticAggregations method createPartialAggregations.

public Parts createPartialAggregations(SymbolAllocator symbolAllocator, PlannerContext plannerContext) {
    ImmutableMap.Builder<Symbol, Aggregation> partialAggregation = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, Aggregation> finalAggregation = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, Symbol> mappings = ImmutableMap.builder();
    for (Map.Entry<Symbol, Aggregation> entry : aggregations.entrySet()) {
        Aggregation originalAggregation = entry.getValue();
        ResolvedFunction resolvedFunction = originalAggregation.getResolvedFunction();
        AggregationFunctionMetadata functionMetadata = plannerContext.getMetadata().getAggregationFunctionMetadata(resolvedFunction);
        List<Type> intermediateTypes = functionMetadata.getIntermediateTypes().stream().map(plannerContext.getTypeManager()::getType).collect(toImmutableList());
        Type intermediateType = intermediateTypes.size() == 1 ? intermediateTypes.get(0) : RowType.anonymous(intermediateTypes);
        Symbol partialSymbol = symbolAllocator.newSymbol(resolvedFunction.getSignature().getName(), intermediateType);
        mappings.put(entry.getKey(), partialSymbol);
        partialAggregation.put(partialSymbol, new Aggregation(resolvedFunction, originalAggregation.getArguments(), originalAggregation.isDistinct(), originalAggregation.getFilter(), originalAggregation.getOrderingScheme(), originalAggregation.getMask()));
        finalAggregation.put(entry.getKey(), new Aggregation(resolvedFunction, ImmutableList.of(partialSymbol.toSymbolReference()), false, Optional.empty(), Optional.empty(), Optional.empty()));
    }
    groupingSymbols.forEach(symbol -> mappings.put(symbol, symbol));
    return new Parts(new StatisticAggregations(partialAggregation.buildOrThrow(), groupingSymbols), new StatisticAggregations(finalAggregation.buildOrThrow(), groupingSymbols), mappings.buildOrThrow());
}
Also used : Symbol(io.trino.sql.planner.Symbol) ResolvedFunction(io.trino.metadata.ResolvedFunction) ImmutableMap(com.google.common.collect.ImmutableMap) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) RowType(io.trino.spi.type.RowType) Type(io.trino.spi.type.Type) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

ResolvedFunction (io.trino.metadata.ResolvedFunction)51 Test (org.testng.annotations.Test)31 WindowNode (io.trino.sql.planner.plan.WindowNode)21 ImmutableList (com.google.common.collect.ImmutableList)20 ImmutableMap (com.google.common.collect.ImmutableMap)20 Symbol (io.trino.sql.planner.Symbol)20 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)18 DEFAULT_FRAME (io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME)18 QualifiedName (io.trino.sql.tree.QualifiedName)18 Optional (java.util.Optional)18 OrderingScheme (io.trino.sql.planner.OrderingScheme)17 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)17 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)17 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)15 FunctionCall (io.trino.sql.tree.FunctionCall)14 SortOrder (io.trino.spi.connector.SortOrder)12 Type (io.trino.spi.type.Type)12 Expression (io.trino.sql.tree.Expression)11 BIGINT (io.trino.spi.type.BigintType.BIGINT)10 PlanMatchPattern.topNRanking (io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking)9