Skip to content
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

fix: detect non-recursive CTEs in the recursive WITH clause #9836

Merged
merged 5 commits into from
Apr 1, 2024

Conversation

jonahgao
Copy link
Member

Which issue does this PR close?

Closes #9804.

Rationale for this change

If the recursive term of a CTE does not reference the CTE itself, then it should be compiled to a non-recursive CTE, otherwise it may lead to incorrect query results.

This PR traverses the LogicalPlan of the recursive term, looking for a table scan of the work table to determine if there is a self-reference.

What changes are included in this PR?

  • Move the CTE-related code to a new mod named cte.
  • Detect non-recursive CTEs in the recursive WITH clause.

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Mar 28, 2024
let recursive_plan =
self.set_expr_to_plan(*right_expr, &mut planner_context.clone())?;

// Check if the recursive term references the CTE itself,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the main changes.

}
}

pub(super) fn set_operation_to_plan(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is reused by CTE.

@jonahgao jonahgao marked this pull request as draft March 28, 2024 16:18
@@ -2992,16 +2992,6 @@ fn join_with_aliases() {
quick_test(sql, expected);
}

#[test]
fn cte_use_same_name_multiple_times() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is moved to slt.

@jonahgao jonahgao marked this pull request as ready for review March 28, 2024 16:40
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jonahgao -- this PR looks really nice 🏆 -- I especially like how you have refactored the code into smaller and easier to understand functions

I apologize for the delay in reviewing. 🚀

cc @matthewgapp

@jonahgao
Copy link
Member Author

jonahgao commented Apr 1, 2024

No worries about the delay :) And thank you for the review @alamb !

@alamb alamb merged commit f300168 into apache:main Apr 1, 2024
23 checks passed
@jonahgao jonahgao deleted the detect_non_recursive branch April 2, 2024 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WITH RECURSIVE clause may define non-recursive CTEs
2 participants