melihsozdinler commented on code in PR #40474:
URL: https://github.com/apache/spark/pull/40474#discussion_r1285909119


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala:
##########
@@ -614,6 +632,69 @@ class SessionCatalog(
     new Path(new Path(dbLocation), qualifiedIdent.table).toUri
   }
 
+  // --------------------------------------------------
+  // | Methods that interact with temp variables only |
+  // --------------------------------------------------
+
+  /**
+   * Create a temporary variable.
+   */
+  def createTempVariable(
+                      name: String,
+                      initialValue: Literal,
+                      variableDefault: String,
+                      overrideIfExists: Boolean): Unit = synchronized {
+    val tempVariable = VariableIdentifier(Seq(SYSTEM_CATALOG, 
SESSION_DATABASE, name))
+    if (variables.contains(tempVariable) && !overrideIfExists) {
+      throw new AnalysisException(errorClass = "VARIABLE_ALREADY_EXISTS",
+        messageParameters = Map("variableName"
+          -> quoteNameParts(UnresolvedAttribute.parseAttributeName(name))))
+    }
+    val structField = StructField(name, initialValue.dataType)
+      .withCurrentDefaultValue(variableDefault)
+    variables.put(tempVariable, (initialValue, structField))
+  }
+
+  /**
+   * Generate a [[Variable]] operator from the temporary variable stored.
+   */
+  def getVariable(variable: VariableIdentifier): Option[(Literal, 
StructField)] = synchronized {
+    val qualifiedVariable = VariableIdentifier(variable.variableName,
+      Some(variable.database.getOrElse(SESSION_DATABASE)),
+      Some(variable.catalog.getOrElse(SYSTEM_CATALOG)))
+    val isResolvingView = AnalysisContext.get.catalogAndNamespace.nonEmpty
+    val referredTempVariableNames = 
AnalysisContext.get.referredTempVariableNames
+    if (isResolvingView) {
+      // When resolving a view, only return a temp variable if it's referred 
by this view.
+      if (referredTempVariableNames.contains(variable.variableName)) {
+        variables.get(qualifiedVariable)
+      } else {
+        None
+      }
+    } else {
+      val result = variables.get(qualifiedVariable)
+      if (result.isDefined) {
+        // We are not resolving a view and the function is a temp one, add it 
to
+        // `AnalysisContext`, so during the view creation, we can save all 
referred temp
+        // variables to view metadata.
+        
AnalysisContext.get.referredTempVariableNames.add(variable.variableName)
+      }
+      result
+    }
+  }
+
+  /**
+   * Drop a temporary variable.
+   *4

Review Comment:
   This "4" is typo issue, right?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to