Search in sources :

Example 1 with FrameBound

use of io.trino.sql.tree.FrameBound 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()));
}
Also used : WindowNode(io.trino.sql.planner.plan.WindowNode) WindowFrame(io.trino.sql.tree.WindowFrame) FrameBound(io.trino.sql.tree.FrameBound) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) NullTreatment(io.trino.sql.tree.FunctionCall.NullTreatment) ResolvedFunction(io.trino.metadata.ResolvedFunction) Function(java.util.function.Function) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) WindowFrame(io.trino.sql.tree.WindowFrame) LambdaExpression(io.trino.sql.tree.LambdaExpression) PatternRecognitionComponents(io.trino.sql.planner.RelationPlanner.PatternRecognitionComponents)

Example 2 with FrameBound

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

the class QueryPlanner method planPatternRecognition.

private PlanBuilder planPatternRecognition(PlanBuilder subPlan, WindowOperation windowMeasure, ResolvedWindow window, Optional<Symbol> frameEndSymbol) {
    WindowNode.Specification specification = planWindowSpecification(window.getPartitionBy(), window.getOrderBy(), subPlan::translate);
    // 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());
    PatternRecognitionComponents components = new RelationPlanner(analysis, symbolAllocator, idAllocator, lambdaDeclarationToSymbolMap, plannerContext, outerContext, session, recursiveSubqueries).planPatternRecognitionComponents(subPlan::rewrite, frame.getSubsets(), ImmutableList.of(analysis.getMeasureDefinition(windowMeasure)), frame.getAfterMatchSkipTo(), frame.getPatternSearchMode(), frame.getPattern().orElseThrow(), frame.getVariableDefinitions());
    Symbol measureSymbol = getOnlyElement(components.getMeasures().keySet());
    // create pattern recognition node
    return new PlanBuilder(subPlan.getTranslations().withAdditionalMappings(ImmutableMap.of(scopeAwareKey(windowMeasure, analysis, subPlan.getScope()), measureSymbol)), new PatternRecognitionNode(idAllocator.getNextId(), subPlan.getRoot(), specification, Optional.empty(), ImmutableSet.of(), 0, ImmutableMap.of(), components.getMeasures(), Optional.of(baseFrame), RowsPerMatch.WINDOW, components.getSkipToLabel(), components.getSkipToPosition(), components.isInitial(), components.getPattern(), components.getSubsets(), components.getVariableDefinitions()));
}
Also used : WindowNode(io.trino.sql.planner.plan.WindowNode) WindowFrame(io.trino.sql.tree.WindowFrame) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) WindowFrame(io.trino.sql.tree.WindowFrame) FrameBound(io.trino.sql.tree.FrameBound) PatternRecognitionComponents(io.trino.sql.planner.RelationPlanner.PatternRecognitionComponents) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder)

Example 3 with FrameBound

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

the class TestSqlParser method testWindowSpecification.

@Test
public void testWindowSpecification() {
    assertExpression("rank() OVER someWindow", new FunctionCall(Optional.empty(), QualifiedName.of("rank"), Optional.of(new WindowReference(new Identifier("someWindow"))), Optional.empty(), Optional.empty(), false, Optional.empty(), Optional.empty(), ImmutableList.of()));
    assertExpression("rank() OVER (someWindow PARTITION BY x ORDER BY y ROWS CURRENT ROW)", new FunctionCall(Optional.empty(), QualifiedName.of("rank"), Optional.of(new WindowSpecification(Optional.of(new Identifier("someWindow")), ImmutableList.of(new Identifier("x")), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("y"), ASCENDING, UNDEFINED)))), Optional.of(new WindowFrame(ROWS, new FrameBound(CURRENT_ROW), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), ImmutableList.of())))), Optional.empty(), Optional.empty(), false, Optional.empty(), Optional.empty(), ImmutableList.of()));
    assertExpression("rank() OVER (PARTITION BY x ORDER BY y ROWS CURRENT ROW)", new FunctionCall(Optional.empty(), QualifiedName.of("rank"), Optional.of(new WindowSpecification(Optional.empty(), ImmutableList.of(new Identifier("x")), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("y"), ASCENDING, UNDEFINED)))), Optional.of(new WindowFrame(ROWS, new FrameBound(CURRENT_ROW), Optional.empty(), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), ImmutableList.of())))), Optional.empty(), Optional.empty(), false, Optional.empty(), Optional.empty(), ImmutableList.of()));
}
Also used : OrderBy(io.trino.sql.tree.OrderBy) SortItem(io.trino.sql.tree.SortItem) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) WindowFrame(io.trino.sql.tree.WindowFrame) FrameBound(io.trino.sql.tree.FrameBound) WindowSpecification(io.trino.sql.tree.WindowSpecification) FunctionCall(io.trino.sql.tree.FunctionCall) WindowReference(io.trino.sql.tree.WindowReference) Test(org.junit.jupiter.api.Test)

Example 4 with FrameBound

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

the class TestMergeWindows method testMergeDifferentFramesWithDefault.

@Test
public void testMergeDifferentFramesWithDefault() {
    Optional<WindowFrame> frameD = Optional.of(new WindowFrame(WindowFrame.Type.ROWS, new FrameBound(FrameBound.Type.CURRENT_ROW), Optional.of(new FrameBound(FrameBound.Type.UNBOUNDED_FOLLOWING)), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), ImmutableList.of()));
    ExpectedValueProvider<WindowNode.Specification> specificationD = specification(ImmutableList.of(SUPPKEY_ALIAS), ImmutableList.of(ORDERKEY_ALIAS), ImmutableMap.of(ORDERKEY_ALIAS, SortOrder.ASC_NULLS_LAST));
    @Language("SQL") String sql = "SELECT " + "SUM(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey) sum_quantity_C, " + "AVG(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) avg_quantity_D, " + "SUM(discount) OVER (PARTITION BY suppkey ORDER BY orderkey) sum_discount_C " + "FROM lineitem";
    assertUnitPlan(sql, anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationD).addFunction(functionCall("avg", frameD, ImmutableList.of(QUANTITY_ALIAS))).addFunction(functionCall("sum", UNSPECIFIED_FRAME, ImmutableList.of(DISCOUNT_ALIAS))).addFunction(functionCall("sum", UNSPECIFIED_FRAME, ImmutableList.of(QUANTITY_ALIAS))), LINEITEM_TABLESCAN_DOQS)));
}
Also used : WindowFrame(io.trino.sql.tree.WindowFrame) Language(org.intellij.lang.annotations.Language) FrameBound(io.trino.sql.tree.FrameBound) Test(org.testng.annotations.Test) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest)

Example 5 with FrameBound

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

the class TestMergeWindows method testMergeDifferentFrames.

@Test
public void testMergeDifferentFrames() {
    Optional<WindowFrame> frameC = Optional.of(new WindowFrame(WindowFrame.Type.ROWS, new FrameBound(FrameBound.Type.UNBOUNDED_PRECEDING), Optional.of(new FrameBound(FrameBound.Type.CURRENT_ROW)), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), ImmutableList.of()));
    ExpectedValueProvider<WindowNode.Specification> specificationC = specification(ImmutableList.of(SUPPKEY_ALIAS), ImmutableList.of(ORDERKEY_ALIAS), ImmutableMap.of(ORDERKEY_ALIAS, SortOrder.ASC_NULLS_LAST));
    Optional<WindowFrame> frameD = Optional.of(new WindowFrame(WindowFrame.Type.ROWS, new FrameBound(FrameBound.Type.CURRENT_ROW), Optional.of(new FrameBound(FrameBound.Type.UNBOUNDED_FOLLOWING)), ImmutableList.of(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(), ImmutableList.of()));
    @Language("SQL") String sql = "SELECT " + "SUM(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) sum_quantity_C, " + "AVG(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) avg_quantity_D, " + "SUM(discount) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) sum_discount_C " + "FROM lineitem";
    assertUnitPlan(sql, anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationC).addFunction(functionCall("avg", frameD, ImmutableList.of(QUANTITY_ALIAS))).addFunction(functionCall("sum", frameC, ImmutableList.of(DISCOUNT_ALIAS))).addFunction(functionCall("sum", frameC, ImmutableList.of(QUANTITY_ALIAS))), LINEITEM_TABLESCAN_DOQS)));
}
Also used : WindowFrame(io.trino.sql.tree.WindowFrame) Language(org.intellij.lang.annotations.Language) FrameBound(io.trino.sql.tree.FrameBound) Test(org.testng.annotations.Test) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest)

Aggregations

FrameBound (io.trino.sql.tree.FrameBound)6 WindowFrame (io.trino.sql.tree.WindowFrame)6 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)2 PatternRecognitionComponents (io.trino.sql.planner.RelationPlanner.PatternRecognitionComponents)2 BasePlanTest (io.trino.sql.planner.assertions.BasePlanTest)2 PatternRecognitionNode (io.trino.sql.planner.plan.PatternRecognitionNode)2 WindowNode (io.trino.sql.planner.plan.WindowNode)2 FunctionCall (io.trino.sql.tree.FunctionCall)2 LambdaExpression (io.trino.sql.tree.LambdaExpression)2 SortItem (io.trino.sql.tree.SortItem)2 Language (org.intellij.lang.annotations.Language)2 Test (org.testng.annotations.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ResolvedFunction (io.trino.metadata.ResolvedFunction)1 DecimalType (io.trino.spi.type.DecimalType)1 Type (io.trino.spi.type.Type)1 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)1 ResolvedWindow (io.trino.sql.analyzer.Analysis.ResolvedWindow)1 SelectExpression (io.trino.sql.analyzer.Analysis.SelectExpression)1