-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[SPARK-18389][SQL] Disallow cyclic view reference #17152
Conversation
Test build #73854 has finished for PR 17152 at commit
|
Based on our current impl of view, |
@gatorsmile Currently we don't perform recursive resolution over a temporary view, so perhaps that won't trigger a cyclic view reference. For example:
|
Yeah. The temporary view does not have such an issue, because we did not change it. My typo. What I mean is |
When do other databases report this error? During view creating/alter or during view resolution? |
Hive report the error during alter view:
|
sql("ALTER VIEW view1 AS SELECT * FROM view3 JOIN view2") | ||
}.getMessage | ||
assert(e2.contains("Recursive view `default`.`view1` detected (cycle: `default`.`view1` " + | ||
"-> `default`.`view3` -> `default`.`view2` -> `default`.`view1`)")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this test case?
sql("alter view v1 as select * from jt where exists (select 1 from v2)")
Should we get the same exception as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is missing in your code is whenever you hit a SubqueryExpression
, you need to traverse the plan
of that expression to detect cyclic references? See an example of the code in #16493.
Going back to @gatorsmile 's question, does this fix cover the scenario below?
If this is an existing problem and your PR does not cover it, would you intend to address it in this PR? |
@gatorsmile @nsyca Thank you for your comments! I've added the coverage for both |
Test build #74088 has finished for PR 17152 at commit
|
// Detect cyclic references from subqueries. | ||
plan.expressions.foreach { expr => | ||
if (expr.isInstanceOf[SubqueryExpression]) { | ||
checkCyclicViewReference(expr.asInstanceOf[SubqueryExpression].plan, path, viewIdent) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we use the pattern matching instead of isInstanceOf-asInstanceOf
? The logic in the code looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Test build #74169 has finished for PR 17152 at commit
|
thanks, merging to master! |
What changes were proposed in this pull request?
Disallow cyclic view references, a cyclic view reference may be created by the following queries:
In the above example, a reference cycle (testView -> testView2 -> testView) exsits.
We disallow cyclic view references by checking that in ALTER VIEW command, when the
analyzedPlan
contains the sameView
node with the altered view, we should prevent the behavior and throw an AnalysisException.How was this patch tested?
Test by
SQLViewSuite.test("correctly handle a cyclic view reference")
.