Search in sources :

Example 26 with FunctionCall

use of io.trino.sql.tree.FunctionCall in project trino by trinodb.

the class TestSqlParser method testAggregationWithOrderBy.

@Test
public void testAggregationWithOrderBy() {
    assertExpression("array_agg(x ORDER BY x DESC)", new FunctionCall(Optional.empty(), QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(identifier("x"), DESCENDING, UNDEFINED)))), false, Optional.empty(), Optional.empty(), ImmutableList.of(identifier("x"))));
    assertStatement("SELECT array_agg(x ORDER BY t.y) FROM t", simpleQuery(selectList(new FunctionCall(Optional.empty(), QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new DereferenceExpression(new Identifier("t"), identifier("y")), ASCENDING, UNDEFINED)))), false, Optional.empty(), Optional.empty(), ImmutableList.of(new Identifier("x")))), table(QualifiedName.of("t"))));
}
Also used : OrderBy(io.trino.sql.tree.OrderBy) SortItem(io.trino.sql.tree.SortItem) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.junit.jupiter.api.Test)

Example 27 with FunctionCall

use of io.trino.sql.tree.FunctionCall in project trino by trinodb.

the class TestSqlParser method testNullTreatment.

@Test
public void testNullTreatment() {
    assertExpression("lead(x, 1) ignore nulls over()", new FunctionCall(Optional.empty(), QualifiedName.of("lead"), Optional.of(new WindowSpecification(Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty())), Optional.empty(), Optional.empty(), false, Optional.of(NullTreatment.IGNORE), Optional.empty(), ImmutableList.of(new Identifier("x"), new LongLiteral("1"))));
    assertExpression("lead(x, 1) respect nulls over()", new FunctionCall(Optional.empty(), QualifiedName.of("lead"), Optional.of(new WindowSpecification(Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty())), Optional.empty(), Optional.empty(), false, Optional.of(NullTreatment.RESPECT), Optional.empty(), ImmutableList.of(new Identifier("x"), new LongLiteral("1"))));
}
Also used : QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) LongLiteral(io.trino.sql.tree.LongLiteral) WindowSpecification(io.trino.sql.tree.WindowSpecification) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.junit.jupiter.api.Test)

Example 28 with FunctionCall

use of io.trino.sql.tree.FunctionCall in project trino by trinodb.

the class TestDomainTranslator method testUnsupportedFunctions.

@Test
public void testUnsupportedFunctions() {
    assertUnsupportedPredicate(new FunctionCall(QualifiedName.of("LENGTH"), ImmutableList.of(C_VARCHAR.toSymbolReference())));
    assertUnsupportedPredicate(new FunctionCall(QualifiedName.of("REPLACE"), ImmutableList.of(C_VARCHAR.toSymbolReference(), stringLiteral("abc"))));
}
Also used : FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 29 with FunctionCall

use of io.trino.sql.tree.FunctionCall in project trino by trinodb.

the class ExpressionTestUtils method resolveFunctionCalls.

public static Expression resolveFunctionCalls(PlannerContext plannerContext, Session session, TypeProvider typeProvider, Expression expression, Scope scope) {
    ExpressionAnalyzer analyzer = ExpressionAnalyzer.createWithoutSubqueries(plannerContext, new AllowAllAccessControl(), session, typeProvider, ImmutableMap.of(), node -> semanticException(EXPRESSION_NOT_CONSTANT, node, "Constant expression cannot contain a subquery"), WarningCollector.NOOP, false);
    analyzer.analyze(expression, scope);
    return ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<>() {

        @Override
        public Expression rewriteFunctionCall(FunctionCall node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
            ResolvedFunction resolvedFunction = analyzer.getResolvedFunctions().get(NodeRef.of(node));
            checkArgument(resolvedFunction != null, "Function has not been analyzed: %s", node);
            FunctionCall rewritten = treeRewriter.defaultRewrite(node, context);
            FunctionCall newFunctionCall = new FunctionCall(rewritten.getLocation(), resolvedFunction.toQualifiedName(), rewritten.getWindow(), rewritten.getFilter(), rewritten.getOrderBy(), rewritten.isDistinct(), rewritten.getNullTreatment(), rewritten.getProcessingMode(), rewritten.getArguments());
            return coerceIfNecessary(node, newFunctionCall);
        }

        @Override
        protected Expression rewriteExpression(Expression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
            Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);
            rewrittenExpression = coerceIfNecessary(node, rewrittenExpression);
            return rewrittenExpression;
        }

        private Expression coerceIfNecessary(Expression originalExpression, Expression rewrittenExpression) {
            // cast expression if coercion is registered
            Type coercion = analyzer.getExpressionCoercions().get(NodeRef.of(originalExpression));
            if (coercion != null) {
                rewrittenExpression = new Cast(rewrittenExpression, toSqlType(coercion), false, analyzer.getTypeOnlyCoercions().contains(NodeRef.of(originalExpression)));
            }
            return rewrittenExpression;
        }
    }, expression);
}
Also used : Cast(io.trino.sql.tree.Cast) Type(io.trino.spi.type.Type) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Expression(io.trino.sql.tree.Expression) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) ResolvedFunction(io.trino.metadata.ResolvedFunction) ExpressionAnalyzer(io.trino.sql.analyzer.ExpressionAnalyzer) FunctionCall(io.trino.sql.tree.FunctionCall)

Example 30 with FunctionCall

use of io.trino.sql.tree.FunctionCall in project trino by trinodb.

the class TestPrunePattenRecognitionColumns method testDoNotPruneVariableDefinitionSources.

@Test
public void testDoNotPruneVariableDefinitionSources() {
    // input symbol "a" is used only by the variable definition
    tester().assertThat(new PrunePattenRecognitionColumns()).on(p -> p.project(Assignments.of(), p.patternRecognition(builder -> builder.addMeasure(p.symbol("measure"), "1", BIGINT).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "LAST(X.a) > 0").source(p.values(p.symbol("a"), p.symbol("b")))))).matches(strictProject(ImmutableMap.of(), patternRecognition(builder -> builder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "LAST(X.a) > 0"), strictProject(ImmutableMap.of("a", expression("a")), values("a", "b")))));
    // inputs "a", "b" are used as aggregation arguments
    QualifiedName maxBy = tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("max_by"), fromTypes(BIGINT, BIGINT)).toQualifiedName();
    tester().assertThat(new PrunePattenRecognitionColumns()).on(p -> p.project(Assignments.of(), p.patternRecognition(builder -> builder.addMeasure(p.symbol("measure"), "1", BIGINT).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), new ComparisonExpression(GREATER_THAN, new FunctionCall(maxBy, ImmutableList.of(PlanBuilder.expression("a"), PlanBuilder.expression("b"))), PlanBuilder.expression("5"))).source(p.values(p.symbol("a"), p.symbol("b"), p.symbol("c")))))).matches(strictProject(ImmutableMap.of(), patternRecognition(builder -> builder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), new ComparisonExpression(GREATER_THAN, new FunctionCall(maxBy, ImmutableList.of(PlanBuilder.expression("a"), PlanBuilder.expression("b"))), PlanBuilder.expression("5"))), strictProject(ImmutableMap.of("a", expression("a"), "b", expression("b")), values("a", "b", "c")))));
}
Also used : CURRENT_ROW(io.trino.sql.tree.FrameBound.Type.CURRENT_ROW) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) PlanMatchPattern.patternRecognition(io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition) FOLLOWING(io.trino.sql.tree.FrameBound.Type.FOLLOWING) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) ALL_WITH_UNMATCHED(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_WITH_UNMATCHED) ALL_SHOW_EMPTY(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY) ImmutableList(com.google.common.collect.ImmutableList) PlanBuilder(io.trino.sql.planner.iterative.rule.test.PlanBuilder) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) WINDOW(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.WINDOW) NEXT(io.trino.sql.tree.SkipTo.Position.NEXT) FunctionCall(io.trino.sql.tree.FunctionCall) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ResolvedFunction(io.trino.metadata.ResolvedFunction) Assignments(io.trino.sql.planner.plan.Assignments) PAST_LAST(io.trino.sql.tree.SkipTo.Position.PAST_LAST) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) DEFAULT_FRAME(io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) PlanMatchPattern.windowFrame(io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame) QualifiedName(io.trino.sql.tree.QualifiedName) ASC_NULLS_LAST(io.trino.spi.connector.SortOrder.ASC_NULLS_LAST) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) BIGINT(io.trino.spi.type.BigintType.BIGINT) MetadataManager.createTestMetadataManager(io.trino.metadata.MetadataManager.createTestMetadataManager) Optional(java.util.Optional) ROWS(io.trino.sql.tree.WindowFrame.Type.ROWS) WindowNode(io.trino.sql.planner.plan.WindowNode) UNBOUNDED_FOLLOWING(io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QualifiedName(io.trino.sql.tree.QualifiedName) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Aggregations

FunctionCall (io.trino.sql.tree.FunctionCall)69 Test (org.testng.annotations.Test)32 StringLiteral (io.trino.sql.tree.StringLiteral)31 ImmutableList (com.google.common.collect.ImmutableList)29 QualifiedName (io.trino.sql.tree.QualifiedName)29 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)25 Expression (io.trino.sql.tree.Expression)25 LongLiteral (io.trino.sql.tree.LongLiteral)25 Optional (java.util.Optional)22 ImmutableMap (com.google.common.collect.ImmutableMap)21 Identifier (io.trino.sql.tree.Identifier)19 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)18 ResolvedFunction (io.trino.metadata.ResolvedFunction)17 Assignments (io.trino.sql.planner.plan.Assignments)16 Cast (io.trino.sql.tree.Cast)15 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)14 BIGINT (io.trino.spi.type.BigintType.BIGINT)13 PlanMatchPattern.project (io.trino.sql.planner.assertions.PlanMatchPattern.project)13 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)13 MetadataManager.createTestMetadataManager (io.trino.metadata.MetadataManager.createTestMetadataManager)12