pan3793 commented on code in PR #54946:
URL: https://github.com/apache/spark/pull/54946#discussion_r3469321803
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala:
##########
@@ -103,6 +103,22 @@ class SparkSqlParser extends AbstractSqlParser {
parseInternal(command, None)(toResult)
}
+ /**
+ * Split a SQL string into individual statements after expanding any `${...}`
+ * variable references. Variable substitution has to happen *before*
splitting
+ * because a substituted value may itself contain `;`, comments, or
+ * `BEGIN ... END` structure that affect statement boundaries.
+ *
+ * Parameter substitution is intentionally NOT applied here: the splitter
+ * runs at the top level of an interactive session / batch input, where there
+ * is no parameter context bound. If a caller does have a parameter context,
+ * they should pre-substitute the input and call this with the result.
+ */
+ override def splitStatements(sqlText: String): SqlStatementSplitResult = {
+ val variableSubstituted = substitutor.substitute(sqlText)
Review Comment:
new algorithm:
**Placeholder-validation hybrid for `${...}` variable substitution** — Spark
SQL allows `SET key=value; SELECT '${key}'` so that `${key}` is substituted
using the value set by an earlier statement in the same batch. Substituting
`${...}` up-front (before splitting) breaks this ordering, and also breaks
block boundaries when the substituted value lands inside a `BEGIN ... END`
whose variable was set by a `SET` outside the block. Instead, the splitter
accepts an optional `validationPreprocess: String => String` callback that
`SparkSqlParser` uses to replace `${...}` with a constant placeholder
identifier _for the parser's validation only_. The emitted `SqlStatement` text
is always the **original** input, so the variable references survive into the
emitted statement and are substituted for real at execution time after earlier
`SET`s have run.
--
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]