-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-48345][SQL] Check variable declarations #47404
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
Changes from all commits
2ee8e60
dc136ba
14f175d
881bacb
a58dbc0
a7b95e5
07df52c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,8 +48,7 @@ import org.apache.spark.sql.catalyst.util.DateTimeUtils.{convertSpecialDate, con | |
| import org.apache.spark.sql.connector.catalog.{CatalogV2Util, SupportsNamespaces, TableCatalog} | ||
| import org.apache.spark.sql.connector.catalog.TableChange.ColumnPosition | ||
| import org.apache.spark.sql.connector.expressions.{ApplyTransform, BucketTransform, DaysTransform, Expression => V2Expression, FieldReference, HoursTransform, IdentityTransform, LiteralValue, MonthsTransform, Transform, YearsTransform} | ||
| import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryParsingErrors, SqlScriptingErrors} | ||
| import org.apache.spark.sql.errors.DataTypeErrors.toSQLStmt | ||
| import org.apache.spark.sql.errors.{DataTypeErrorsBase, QueryCompilationErrors, QueryParsingErrors, SqlScriptingErrors} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.internal.SQLConf.LEGACY_BANG_EQUALS_NOT | ||
| import org.apache.spark.sql.types._ | ||
|
|
@@ -62,7 +61,8 @@ import org.apache.spark.util.random.RandomSampler | |
| * The AstBuilder converts an ANTLR4 ParseTree into a catalyst Expression, LogicalPlan or | ||
| * TableIdentifier. | ||
| */ | ||
| class AstBuilder extends DataTypeAstBuilder with SQLConfHelper with Logging { | ||
| class AstBuilder extends DataTypeAstBuilder with SQLConfHelper | ||
| with Logging with DataTypeErrorsBase { | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
| import ParserUtils._ | ||
|
|
||
|
|
@@ -133,12 +133,42 @@ class AstBuilder extends DataTypeAstBuilder with SQLConfHelper with Logging { | |
|
|
||
| private def visitCompoundBodyImpl( | ||
| ctx: CompoundBodyContext, | ||
| label: Option[String]): CompoundBody = { | ||
| label: Option[String], | ||
| allowVarDeclare: Boolean): CompoundBody = { | ||
| val buff = ListBuffer[CompoundPlanStatement]() | ||
| ctx.compoundStatements.forEach(compoundStatement => { | ||
| buff += visit(compoundStatement).asInstanceOf[CompoundPlanStatement] | ||
| }) | ||
|
|
||
| val compoundStatements = buff.toList | ||
|
|
||
| val candidates = if (allowVarDeclare) { | ||
| compoundStatements.dropWhile { | ||
| case SingleStatement(_: CreateVariable) => true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unrelated question: within SQL scripting, shall we only allow to create single-part-name variables? e.g. should
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreement with Serge is that we should be able to reference session variables at all times using Though, what I explained is a resolution part not the creation. For the creation, looking at the code and documentation, we should allow the same thing as well: I will check with Serge once again, just to confirm, but wouldn't block this PR on it because we are preparing a POC for variables anyways which is supposed to handle all local variable related stuff and we will resolve all such cases there, if that's fine by you!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just for my curiosity. If SQL scripting only allows creation of local variable, then we can even fail earlier in the parser if the CREATE VARIABLE uses more than one name part. |
||
| case _ => false | ||
| } | ||
| } else { | ||
| compoundStatements | ||
| } | ||
|
|
||
| val declareVarStatement = candidates.collectFirst { | ||
| case SingleStatement(c: CreateVariable) => c | ||
| } | ||
|
|
||
| declareVarStatement match { | ||
| case Some(c: CreateVariable) => | ||
| if (allowVarDeclare) { | ||
| throw SqlScriptingErrors.variableDeclarationOnlyAtBeginning( | ||
| toSQLId(c.name.asInstanceOf[UnresolvedIdentifier].nameParts), | ||
| c.origin.line.get.toString) | ||
| } else { | ||
| throw SqlScriptingErrors.variableDeclarationNotAllowedInScope( | ||
| toSQLId(c.name.asInstanceOf[UnresolvedIdentifier].nameParts), | ||
| c.origin.line.get.toString) | ||
| } | ||
| case _ => | ||
| } | ||
|
|
||
| CompoundBody(buff.toSeq, label) | ||
| } | ||
|
|
||
|
|
@@ -161,11 +191,11 @@ class AstBuilder extends DataTypeAstBuilder with SQLConfHelper with Logging { | |
| val labelText = beginLabelCtx. | ||
| map(_.multipartIdentifier().getText).getOrElse(java.util.UUID.randomUUID.toString). | ||
| toLowerCase(Locale.ROOT) | ||
| visitCompoundBodyImpl(ctx.compoundBody(), Some(labelText)) | ||
| visitCompoundBodyImpl(ctx.compoundBody(), Some(labelText), allowVarDeclare = true) | ||
| } | ||
|
|
||
| override def visitCompoundBody(ctx: CompoundBodyContext): CompoundBody = { | ||
| visitCompoundBodyImpl(ctx, None) | ||
| visitCompoundBodyImpl(ctx, None, allowVarDeclare = false) | ||
| } | ||
|
|
||
| override def visitCompoundStatement(ctx: CompoundStatementContext): CompoundPlanStatement = | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.