dtenedor commented on code in PR #60367:
URL: https://github.com/apache/spark/pull/60367#discussion_r4649107690
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/SubstituteParamsParser.scala:
##########
@@ -134,11 +200,16 @@ class SubstituteParamsParser extends Logging {
case Some(value) =>
// Substitute all occurrences of this parameter
locationList.foreach { location =>
- substitutions += Substitution(location, value)
+ substitutions += Substitution(location, namedReplacement(sqlText,
location, value))
+ }
+ case None if locationList.forall(_.optional) =>
+ // An optional named parameter marker (`:name?`) without a value is
NULL.
+ locationList.foreach { location =>
+ substitutions += Substitution(location, namedReplacement(sqlText,
location, "NULL"))
Review Comment:
Thanks, fixed. `SubstituteParamsParser` now leaves an unbound `:name?` in
the text. The main parser then builds an optional `NamedParameter`, which the
analysis resolves to the tagged NULL of a missing reference, the same as
without a parameter context and in the legacy binding mode. `SELECT CAST(:p?:a
AS STRING) AS a, :q AS q` with `q` bound returns (NULL, 1) through `spark.sql`
and `EXECUTE IMMEDIATE`, in both binding modes (new `ParametersSuite` test).
This also closes the MERGE-source gap that was documented as not covered, so
the TODO and that note in the PR description are gone. A DataFrame source built
as `spark.sql("SELECT pk, :p? AS archived FROM source WHERE pk = :q", Map("q"
-> 1))` now fails the MERGE with IN_DML at `:p?` (new `MergeIntoDataFrameSuite`
test).
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/SubstituteParamsParser.scala:
##########
@@ -73,12 +108,42 @@ class SubstituteParamsParser extends Logging {
// Use shared parser configuration to ensure consistency with main parser.
AbstractParser.configureParser(parser, sqlText, tokenStream, SQLConf.get)
- val astBuilder = new SubstituteParmsAstBuilder()
-
// Use shared two-stage parsing strategy for consistent error handling.
- val ctx = AbstractParser.executeWithTwoStageStrategy(parser, tokenStream,
- _.compoundOrSingleStatement())
- val parameterLocations = astBuilder.extractParameterLocations(ctx)
+ def parseParameterLocations(): (ParserRuleContext, ParameterLocationInfo)
= {
+ val ctx = AbstractParser.executeWithTwoStageStrategy(parser, tokenStream,
+ _.compoundOrSingleStatement())
+ (ctx, new SubstituteParmsAstBuilder().extractParameterLocations(ctx))
+ }
+ val (ctx, parameterLocations) = parseParameterLocations() match {
+ // A rule that accepts any tokens, such as `SET .*?`, parses a statement
such as
+ // `SET VAR v = :p?`, which is not valid without optional references.
Only the parser that
+ // builds the plan can tell, so parse a statement with a `?` that is not
a parameter marker
+ // again with optional references.
+ case (_, locations)
+ if parser.optional_references_enabled &&
+ AbstractParser.questions(tokenStream).length >
Review Comment:
Thanks, fixed. The re-parse condition now compares against
`AbstractParser.positionalParameterMarkerCount`, the number of markers the
grammar actually parsed, which includes those of DECLARE CURSOR queries. The
main parser already uses that count.
I couldn't reproduce a wrong result end to end. `spark.sql` rejects
positional parameters for a script
(UNSUPPORTED_FEATURE.SQL_SCRIPTING_WITH_POSITIONAL_PARAMETERS), so the cursor
query's `?` is the only one in the script. `DeclareCursor` keeps its query as
text and parses it again at OPEN, so the main parse running in optional mode
didn't change it. The reported parse mode was still wrong, and the next
construct that keeps a parsed `?` would have been misread.
A new `OptionalReferencesParserSuite` test checks that a named-parameter
script with a cursor `?` substitutes `:x` and reports
`parsedWithOptionalReferences = false`; it fails with the old count. A
`ParametersSuite` test runs such a script end to end, with the cursor query
``SELECT first(DISTINCT ?) AS f FROM VALUES (2) AS t(`distinct`)`` opened
`USING 1`, and gets (1, 3).
--
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]