use of io.trino.sql.planner.LogicalPlanner.Stage.CREATED in project trino by trinodb.
the class TestRecursiveCte method testRecursiveQuery.
@Test
public void testRecursiveQuery() {
@Language("SQL") String sql = "WITH RECURSIVE t(n) AS (" + " SELECT 1" + " UNION ALL" + " SELECT n + 2 FROM t WHERE n < 6" + " )" + " SELECT * from t";
PlanMatchPattern pattern = anyTree(union(// base term
project(project(project(ImmutableMap.of("expr", expression("1")), values()))), // first recursion step
project(project(project(ImmutableMap.of("expr_0", expression("expr + 2")), filter("expr < 6", project(project(project(ImmutableMap.of("expr", expression("1")), values()))))))), // "post-recursion" step with convergence assertion
filter("IF((count >= BIGINT '0'), " + "CAST(fail(CAST('Recursion depth limit exceeded (1). Use ''max_recursion_depth'' session property to modify the limit.' AS varchar)) AS boolean), " + "true)", window(windowBuilder -> windowBuilder.addFunction("count", functionCall("count", ImmutableList.of())), project(project(project(ImmutableMap.of("expr_1", expression("expr + 2")), filter("expr < 6", project(ImmutableMap.of("expr", expression("expr_0")), project(project(project(ImmutableMap.of("expr_0", expression("expr + 2")), filter("expr < 6", project(project(project(ImmutableMap.of("expr", expression("1")), values()))))))))))))))));
assertPlan(sql, CREATED, pattern);
}
use of io.trino.sql.planner.LogicalPlanner.Stage.CREATED in project trino by trinodb.
the class TestWindowClause method testPreprojectExpression.
@Test
public void testPreprojectExpression() {
@Language("SQL") String sql = "SELECT max(b) OVER w FROM (VALUES (1, 1)) t(a, b) WINDOW w AS (PARTITION BY a + 1)";
PlanMatchPattern pattern = anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specification(ImmutableList.of("expr"), ImmutableList.of(), ImmutableMap.of())).addFunction("max_result", functionCall("max", ImmutableList.of("b"))), anyTree(project(ImmutableMap.of("expr", expression("a + 1")), anyTree(values("a", "b"))))));
assertPlan(sql, CREATED, pattern);
}
use of io.trino.sql.planner.LogicalPlanner.Stage.CREATED in project trino by trinodb.
the class TestWindowClause method testPreprojectExpressions.
@Test
public void testPreprojectExpressions() {
@Language("SQL") String sql = "SELECT max(b) OVER w3 FROM (VALUES (1, 1, 1)) t(a, b, c) WINDOW w1 AS (PARTITION BY a + 1), w2 AS (w1 ORDER BY b + 2), w3 AS (w2 RANGE c + 3 PRECEDING)";
PlanMatchPattern pattern = anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specification(ImmutableList.of("expr_a"), ImmutableList.of("expr_b"), ImmutableMap.of("expr_b", SortOrder.ASC_NULLS_LAST))).addFunction("max_result", functionCall("max", ImmutableList.of("b")), createTestMetadataManager().resolveFunction(TEST_SESSION, QualifiedName.of("max"), fromTypes(INTEGER)), windowFrame(RANGE, PRECEDING, Optional.of("frame_start"), Optional.of("expr_b"), CURRENT_ROW, Optional.empty(), Optional.empty())), project(ImmutableMap.of("frame_start", expression(new FunctionCall(QualifiedName.of("$operator$subtract"), ImmutableList.of(new SymbolReference("expr_b"), new SymbolReference("expr_c"))))), anyTree(project(ImmutableMap.of("expr_a", expression("a + 1"), "expr_b", expression("b + 2"), "expr_c", expression("c + 3")), anyTree(values("a", "b", "c")))))));
assertPlan(sql, CREATED, pattern);
}
use of io.trino.sql.planner.LogicalPlanner.Stage.CREATED in project trino by trinodb.
the class TestWindowClause method testWindowFunctionsInSelectAndOrderBy.
@Test
public void testWindowFunctionsInSelectAndOrderBy() {
@Language("SQL") String sql = "SELECT array_agg(a) OVER (w ORDER BY a + 1), -a a FROM (VALUES 1, 2, 3) t(a) WINDOW w AS () ORDER BY max(a) OVER (w ORDER BY a + 1)";
PlanMatchPattern pattern = anyTree(sort(ImmutableList.of(sort("max_result", ASCENDING, LAST)), any(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specification(ImmutableList.of(), ImmutableList.of("order_by_window_sortkey"), ImmutableMap.of("order_by_window_sortkey", SortOrder.ASC_NULLS_LAST))).addFunction("max_result", functionCall("max", ImmutableList.of("minus_a"))), any(project(ImmutableMap.of("order_by_window_sortkey", expression("minus_a + 1")), project(ImmutableMap.of("minus_a", expression("-a")), window(windowMatcherBuilder -> windowMatcherBuilder.specification(specification(ImmutableList.of(), ImmutableList.of("select_window_sortkey"), ImmutableMap.of("select_window_sortkey", SortOrder.ASC_NULLS_LAST))).addFunction("array_agg_result", functionCall("array_agg", ImmutableList.of("a"))), anyTree(project(ImmutableMap.of("select_window_sortkey", expression("a + 1")), anyTree(values("a"))))))))))));
assertPlan(sql, CREATED, pattern);
}
use of io.trino.sql.planner.LogicalPlanner.Stage.CREATED in project trino by trinodb.
the class TestWindowFrameRange method testFramePrecedingFollowingNoCoercions.
@Test
public void testFramePrecedingFollowingNoCoercions() {
@Language("SQL") String sql = "SELECT array_agg(key) OVER(ORDER BY key RANGE BETWEEN x PRECEDING AND y FOLLOWING) " + "FROM (VALUES (1, 1, 1), (2, 2, 2)) t(key, x, y)";
PlanMatchPattern pattern = anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specification(ImmutableList.of(), ImmutableList.of("key"), ImmutableMap.of("key", SortOrder.ASC_NULLS_LAST))).addFunction("array_agg_result", functionCall("array_agg", ImmutableList.of("key")), createTestMetadataManager().resolveFunction(TEST_SESSION, QualifiedName.of("array_agg"), fromTypes(INTEGER)), windowFrame(RANGE, PRECEDING, Optional.of("frame_start_value"), Optional.of("key"), FOLLOWING, Optional.of("frame_end_value"), Optional.of("key"))), project(ImmutableMap.of("frame_end_value", expression(new FunctionCall(QualifiedName.of("$operator$add"), ImmutableList.of(new SymbolReference("key"), new SymbolReference("y"))))), filter("IF((y >= CAST(0 AS INTEGER)), " + "true, " + "CAST(fail(CAST('Window frame offset value must not be negative or null' AS varchar)) AS boolean))", project(ImmutableMap.of("frame_start_value", expression(new FunctionCall(QualifiedName.of("$operator$subtract"), ImmutableList.of(new SymbolReference("key"), new SymbolReference("x"))))), filter("IF((x >= CAST(0 AS INTEGER)), " + "true, " + "CAST(fail(CAST('Window frame offset value must not be negative or null' AS varchar)) AS boolean))", anyTree(values(ImmutableList.of("key", "x", "y"), ImmutableList.of(ImmutableList.of(new LongLiteral("1"), new LongLiteral("1"), new LongLiteral("1")), ImmutableList.of(new LongLiteral("2"), new LongLiteral("2"), new LongLiteral("2")))))))))));
assertPlan(sql, CREATED, pattern);
}
Aggregations