Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/17152#discussion_r104743057
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala ---
@@ -358,4 +366,51 @@ object ViewHelper {
generateViewDefaultDatabase(viewDefaultDatabase) ++
generateQueryColumnNames(queryOutput)
}
+
+ /**
+ * Recursively search the logical plan to detect cyclic view references,
throw an
+ * AnalysisException if cycle detected.
+ *
+ * A cyclic view reference is a cycle of reference dependencies, for
example, if the following
+ * statements are executed:
+ * CREATE VIEW testView AS SELECT id FROM tbl
+ * CREATE VIEW testView2 AS SELECT id FROM testView
+ * ALTER VIEW testView AS SELECT * FROM testView2
+ * The view `testView` references `testView2`, and `testView2` also
references `testView`,
+ * therefore a reference cycle (testView -> testView2 -> testView)
exists.
+ *
+ * @param plan the logical plan we detect cyclic view references from.
+ * @param path the path between the altered view and current node.
+ * @param viewIdent the table identifier of the altered view, we compare
two views by the
+ * `desc.identifier`.
+ */
+ def checkCyclicViewReference(
+ plan: LogicalPlan,
+ path: Seq[TableIdentifier],
+ viewIdent: TableIdentifier): Unit = {
+ plan match {
+ case v: View =>
+ val ident = v.desc.identifier
+ val newPath = path :+ ident
+ // If the table identifier equals to the `viewIdent`, current view
node is the same with
+ // the altered view. We detect a view reference cycle, should
throw an AnalysisException.
+ if (ident == viewIdent) {
+ throw new AnalysisException(s"Recursive view $viewIdent detected
" +
+ s"(cycle: ${newPath.mkString(" -> ")})")
+ } else {
+ v.children.foreach { child =>
+ checkCyclicViewReference(child, newPath, viewIdent)
+ }
+ }
+ case _ =>
+ plan.children.foreach(child => checkCyclicViewReference(child,
path, viewIdent))
+ }
+
+ // Detect cyclic references from subqueries.
+ plan.expressions.foreach { expr =>
+ if (expr.isInstanceOf[SubqueryExpression]) {
+
checkCyclicViewReference(expr.asInstanceOf[SubqueryExpression].plan, path,
viewIdent)
--- End diff --
+1
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]