Skip to content

[SQL][Minor] Disable Recursive CTE self-references from window functions #51178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ object ResolveWithCTE extends Rule[LogicalPlan] {
checkIfSelfReferenceIsPlacedCorrectly(right, cteId, allowRecursiveRef = false)
case Aggregate(_, _, child, _) =>
checkIfSelfReferenceIsPlacedCorrectly(child, cteId, allowRecursiveRef = false)
case Window(_, _, _, child, _) =>
checkIfSelfReferenceIsPlacedCorrectly(child, cteId, allowRecursiveRef = false)
case r: UnionLoopRef if !allowRecursiveRef && r.loopId == cteId =>
throw new AnalysisException(
errorClass = "INVALID_RECURSIVE_REFERENCE.PLACE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2011,3 +2011,19 @@ WithCTE
+- Project [n#x]
+- SubqueryAlias t1
+- CTERelationRef xxxx, true, [n#x], false, false


-- !query
WITH RECURSIVE win(id, val) AS (
SELECT 1, CAST(10 AS BIGINT)
UNION ALL
SELECT id + 1, SUM(val) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
FROM win WHERE id < 3
)
SELECT * FROM win
-- !query analysis
org.apache.spark.sql.catalyst.ExtendedAnalysisException
{
"errorClass" : "INVALID_RECURSIVE_REFERENCE.PLACE",
"sqlState" : "42836"
}
11 changes: 10 additions & 1 deletion sql/core/src/test/resources/sql-tests/inputs/cte-recursion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,13 @@ WITH RECURSIVE t1(n) AS (
UNION ALL
SELECT n + 1 FROM t1
)
((SELECT n FROM t1) UNION ALL (SELECT n FROM t1)) LIMIT 20
((SELECT n FROM t1) UNION ALL (SELECT n FROM t1)) LIMIT 20;

-- Recursive CTE with self reference inside Window function
WITH RECURSIVE win(id, val) AS (
SELECT 1, CAST(10 AS BIGINT)
UNION ALL
SELECT id + 1, SUM(val) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
FROM win WHERE id < 3
)
SELECT * FROM win;
Original file line number Diff line number Diff line change
Expand Up @@ -1804,3 +1804,21 @@ struct<n:int>
7
8
9


-- !query
WITH RECURSIVE win(id, val) AS (
SELECT 1, CAST(10 AS BIGINT)
UNION ALL
SELECT id + 1, SUM(val) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
FROM win WHERE id < 3
)
SELECT * FROM win
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.catalyst.ExtendedAnalysisException
{
"errorClass" : "INVALID_RECURSIVE_REFERENCE.PLACE",
"sqlState" : "42836"
}