cloud-fan commented on code in PR #50325:
URL: https://github.com/apache/spark/pull/50325#discussion_r2010310507


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveIdentifierClause.scala:
##########
@@ -34,17 +39,94 @@ class ResolveIdentifierClause(earlyBatches: 
Seq[RuleExecutor[LogicalPlan]#Batch]
     override def batches: Seq[Batch] = earlyBatches.asInstanceOf[Seq[Batch]]
   }
 
-  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.resolveOperatorsUpWithPruning(
-    _.containsPattern(UNRESOLVED_IDENTIFIER)) {
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    val referredTempVars = new mutable.ArrayBuffer[Seq[String]]
+
+    plan match {
+      case createView: CreateView =>
+        analyzeCreateView(createView, referredTempVars)
+      case _ => apply0(plan)
+    }
+  }
+
+  private def apply0(
+      plan: LogicalPlan,
+      referredTempVars: Option[mutable.ArrayBuffer[Seq[String]]] = None): 
LogicalPlan =
+    
plan.resolveOperatorsUpWithPruning(_.containsPattern(UNRESOLVED_IDENTIFIER)) {
     case p: PlanWithUnresolvedIdentifier if p.identifierExpr.resolved && 
p.childrenResolved =>
+
+      if (referredTempVars.isDefined) {
+        referredTempVars.get ++= collectTemporaryVariablesInLogicalPlan(p)
+      }
+
       
executor.execute(p.planBuilder.apply(evalIdentifierExpr(p.identifierExpr), 
p.children))
     case other =>
       
other.transformExpressionsWithPruning(_.containsAnyPattern(UNRESOLVED_IDENTIFIER))
 {
         case e: ExpressionWithUnresolvedIdentifier if 
e.identifierExpr.resolved =>
+
+          if (referredTempVars.isDefined) {
+            referredTempVars.get ++= 
collectTemporaryVariablesInExpressionTree(e)
+          }
+
           e.exprBuilder.apply(evalIdentifierExpr(e.identifierExpr), 
e.otherExprs)
       }
   }
 
+  private def analyzeCreateView(
+      createView: CreateView,
+      referredTempVars: mutable.ArrayBuffer[Seq[String]]) = {
+    val analyzedChild = apply0(createView.child)
+    val analyzedQuery = apply0(createView.query, Some(referredTempVars))
+    val tableIdentifier = ResolvedIdentifierToTableIdentifier(analyzedChild)
+    if (referredTempVars.nonEmpty && tableIdentifier.isDefined) {

Review Comment:
   I think the implementation is overly complicated to have more details in the 
error message. We should make it simpler:
   1. rename the previous `def apply` to `def apply0` and add an extra 
ArrayBuffer parameter to collect visited session variables by the IDENTIFIER 
clause.
   2. if the root node is `CreateView`, fail if the collected session variables 
is not empty. There is no need to mention the view name here as this is a 
single CREATE VIERW command.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to