melihsozdinler commented on code in PR #40474:
URL: https://github.com/apache/spark/pull/40474#discussion_r1285903634
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala:
##########
@@ -567,6 +568,135 @@ class SparkSqlAstBuilder extends AstBuilder {
}
}
+ /**
+ * Create a [[CreateVariableCommand]].
+ *
+ * For example:
+ * {{{
+ * DECLARE [OR REPLACE] [VARIABLE] [db_name.]variable_name
+ * [dataType] [defaultExpression];
+ * }}}
+ *
+ * We will ad CREATE VARIABLE for persisted variable definitions to this,
hence the name...
+ */
+ override def visitCreateVariable(ctx: CreateVariableContext): LogicalPlan =
withOrigin(ctx) {
+
+ def format(name: String): String = {
+ if (conf.caseSensitiveAnalysis) name else name.toLowerCase(Locale.ROOT)
+ }
+
+ val multipartIdentifier = visitMultipartIdentifier(ctx.multipartIdentifier)
+ val defaultExpression = if (ctx.variableDefaultExpression() == null) {
+ "null"
+ } else {
+ visitVariableDefaultExpression(ctx.variableDefaultExpression())
+ }
+ val dataTypeStr: Option[String] =
+ if (Option(ctx.dataType).nonEmpty) {
+ Option(source(Option(ctx.dataType).get))
+ } else {
+ Option(null)
+ }
+
+ if (multipartIdentifier.length > 3) {
+ throw QueryParsingErrors.unsupportedVariableNameError(
+ multipartIdentifier, ctx.multipartIdentifier)
+ }
+
+ val schemaQualifiedName = if (multipartIdentifier.length < 2) {
+ SESSION_DATABASE +: multipartIdentifier
+ } else {
+ multipartIdentifier
+ }
+
+ val catalogQualifiedName = if (schemaQualifiedName.length < 3) {
+ SYSTEM_CATALOG +: schemaQualifiedName
+ } else {
+ schemaQualifiedName
+ }
+ val variableIdentifier = VariableIdentifier(catalogQualifiedName)
+
+ if (variableIdentifier != VariableIdentifier(
+ Seq(SYSTEM_CATALOG, SESSION_DATABASE,
format(catalogQualifiedName.last)))) {
+ throw QueryParsingErrors.unsupportedVariableNameError(
+ multipartIdentifier, ctx.multipartIdentifier)
+ }
+
+ CreateVariableCommand(variableIdentifier, dataTypeStr, defaultExpression,
ctx.REPLACE != null)
+ }
+
+ /**
+ * Create a DROP VARIABLE statement.
+ *
+ * For example:
+ * {{{
+ * DROP TEMPORARY VARIABLE [IF EXISTS] variable;
+ * }}}
+ */
+ override def visitDropVariable(ctx: DropVariableContext): LogicalPlan =
withOrigin(ctx) {
+ val variableName = visitMultipartIdentifier(ctx.multipartIdentifier)
+ if (variableName.length > 3) {
+ throw QueryParsingErrors.unsupportedVariableNameError(variableName, ctx)
+ }
+
+ val variableIdentifier = VariableIdentifier(variableName)
+
+ if (ctx.TEMPORARY() != null) {
+ if (variableIdentifier.database.getOrElse(SESSION_DATABASE) !=
SESSION_DATABASE ||
+ variableIdentifier.catalog.getOrElse(SYSTEM_CATALOG) !=
SYSTEM_CATALOG) {
+ throw
QueryParsingErrors.unsupportedVariableNameError(variableIdentifier.nameParts,
ctx)
+ }
+
+ DropVariableCommand(
+ identifier = VariableIdentifier(Seq(SYSTEM_CATALOG, SESSION_DATABASE,
+ variableIdentifier.variableName)),
+ ifExists = ctx.EXISTS != null)
+ } else {
+ DropVariableCommand(
+ identifier = variableIdentifier,
+ ifExists = ctx.EXISTS != null)
+ }
+ }
+
+ override def visitSetVariable(ctx: SetVariableContext): LogicalPlan =
withOrigin(ctx) {
+
+ if (ctx.query() != null) {
+ /**
Review Comment:
We may use the correct one-liner comment based on this information here:
https://github.com/databricks/scala-style-guide#documentation-style
--
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]