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"))));
}
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"))));
}
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"))));
}
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);
}
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")))));
}
Aggregations