use of io.trino.sql.tree.LambdaExpression in project trino by trinodb.
the class QueryPlanner method planWindow.
private PlanBuilder planWindow(PlanBuilder subPlan, FunctionCall windowFunction, ResolvedWindow window, PlanAndMappings coercions, Optional<Symbol> frameStartSymbol, Optional<Symbol> sortKeyCoercedForFrameStartComparison, Optional<Symbol> frameEndSymbol, Optional<Symbol> sortKeyCoercedForFrameEndComparison) {
WindowFrame.Type frameType = WindowFrame.Type.RANGE;
FrameBound.Type frameStartType = FrameBound.Type.UNBOUNDED_PRECEDING;
FrameBound.Type frameEndType = FrameBound.Type.CURRENT_ROW;
Optional<Expression> frameStartExpression = Optional.empty();
Optional<Expression> frameEndExpression = Optional.empty();
if (window.getFrame().isPresent()) {
WindowFrame frame = window.getFrame().get();
frameType = frame.getType();
frameStartType = frame.getStart().getType();
frameStartExpression = frame.getStart().getValue();
if (frame.getEnd().isPresent()) {
frameEndType = frame.getEnd().get().getType();
frameEndExpression = frame.getEnd().get().getValue();
}
}
WindowNode.Specification specification = planWindowSpecification(window.getPartitionBy(), window.getOrderBy(), coercions::get);
// Rewrite frame bounds in terms of pre-projected inputs
WindowNode.Frame frame = new WindowNode.Frame(frameType, frameStartType, frameStartSymbol, sortKeyCoercedForFrameStartComparison, frameEndType, frameEndSymbol, sortKeyCoercedForFrameEndComparison, frameStartExpression, frameEndExpression);
Symbol newSymbol = symbolAllocator.newSymbol(windowFunction, analysis.getType(windowFunction));
NullTreatment nullTreatment = windowFunction.getNullTreatment().orElse(NullTreatment.RESPECT);
WindowNode.Function function = new WindowNode.Function(analysis.getResolvedFunction(windowFunction), windowFunction.getArguments().stream().map(argument -> {
if (argument instanceof LambdaExpression) {
return subPlan.rewrite(argument);
}
return coercions.get(argument).toSymbolReference();
}).collect(toImmutableList()), frame, nullTreatment == NullTreatment.IGNORE);
// create window node
return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(ImmutableMap.of(scopeAwareKey(windowFunction, analysis, subPlan.getScope()), newSymbol)), new WindowNode(idAllocator.getNextId(), subPlan.getRoot(), specification, ImmutableMap.of(newSymbol, function), Optional.empty(), ImmutableSet.of(), 0));
}
use of io.trino.sql.tree.LambdaExpression in project trino by trinodb.
the class QueryPlanner method planPatternRecognition.
private PlanBuilder planPatternRecognition(PlanBuilder subPlan, FunctionCall windowFunction, ResolvedWindow window, PlanAndMappings coercions, Optional<Symbol> frameEndSymbol) {
WindowNode.Specification specification = planWindowSpecification(window.getPartitionBy(), window.getOrderBy(), coercions::get);
// in window frame with pattern recognition, the frame extent is specified as `ROWS BETWEEN CURRENT ROW AND ... `
WindowFrame frame = window.getFrame().orElseThrow();
FrameBound frameEnd = frame.getEnd().orElseThrow();
WindowNode.Frame baseFrame = new WindowNode.Frame(WindowFrame.Type.ROWS, FrameBound.Type.CURRENT_ROW, Optional.empty(), Optional.empty(), frameEnd.getType(), frameEndSymbol, Optional.empty(), Optional.empty(), frameEnd.getValue());
Symbol newSymbol = symbolAllocator.newSymbol(windowFunction, analysis.getType(windowFunction));
NullTreatment nullTreatment = windowFunction.getNullTreatment().orElse(NullTreatment.RESPECT);
WindowNode.Function function = new WindowNode.Function(analysis.getResolvedFunction(windowFunction), windowFunction.getArguments().stream().map(argument -> {
if (argument instanceof LambdaExpression) {
return subPlan.rewrite(argument);
}
return coercions.get(argument).toSymbolReference();
}).collect(toImmutableList()), baseFrame, nullTreatment == NullTreatment.IGNORE);
PatternRecognitionComponents components = new RelationPlanner(analysis, symbolAllocator, idAllocator, lambdaDeclarationToSymbolMap, plannerContext, outerContext, session, recursiveSubqueries).planPatternRecognitionComponents(subPlan::rewrite, frame.getSubsets(), ImmutableList.of(), frame.getAfterMatchSkipTo(), frame.getPatternSearchMode(), frame.getPattern().orElseThrow(), frame.getVariableDefinitions());
// create pattern recognition node
return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(ImmutableMap.of(scopeAwareKey(windowFunction, analysis, subPlan.getScope()), newSymbol)), new PatternRecognitionNode(idAllocator.getNextId(), subPlan.getRoot(), specification, Optional.empty(), ImmutableSet.of(), 0, ImmutableMap.of(newSymbol, function), components.getMeasures(), Optional.of(baseFrame), RowsPerMatch.WINDOW, components.getSkipToLabel(), components.getSkipToPosition(), components.isInitial(), components.getPattern(), components.getSubsets(), components.getVariableDefinitions()));
}
use of io.trino.sql.tree.LambdaExpression in project trino by trinodb.
the class AstBuilder method visitLambda.
@Override
public Node visitLambda(SqlBaseParser.LambdaContext context) {
List<LambdaArgumentDeclaration> arguments = visit(context.identifier(), Identifier.class).stream().map(LambdaArgumentDeclaration::new).collect(toList());
Expression body = (Expression) visit(context.expression());
return new LambdaExpression(getLocation(context), arguments, body);
}
use of io.trino.sql.tree.LambdaExpression in project trino by trinodb.
the class TestSqlParser method testLambda.
@Test
public void testLambda() {
assertExpression("() -> x", new LambdaExpression(ImmutableList.of(), new Identifier("x")));
assertExpression("x -> sin(x)", new LambdaExpression(ImmutableList.of(new LambdaArgumentDeclaration(identifier("x"))), new FunctionCall(QualifiedName.of("sin"), ImmutableList.of(new Identifier("x")))));
assertExpression("(x, y) -> mod(x, y)", new LambdaExpression(ImmutableList.of(new LambdaArgumentDeclaration(identifier("x")), new LambdaArgumentDeclaration(identifier("y"))), new FunctionCall(QualifiedName.of("mod"), ImmutableList.of(new Identifier("x"), new Identifier("y")))));
}
use of io.trino.sql.tree.LambdaExpression in project trino by trinodb.
the class TestLambdaCaptureDesugaringRewriter method testRewriteBasicLambda.
@Test
public void testRewriteBasicLambda() {
Map<Symbol, Type> symbols = ImmutableMap.of(new Symbol("a"), BigintType.BIGINT);
SymbolAllocator allocator = new SymbolAllocator(symbols);
assertEquals(rewrite(expression("x -> a + x"), allocator.getTypes(), allocator), new BindExpression(ImmutableList.of(expression("a")), new LambdaExpression(Stream.of("a_0", "x").map(Identifier::new).map(LambdaArgumentDeclaration::new).collect(toList()), expression("a_0 + x"))));
}
Aggregations