srielau opened a new pull request, #60724: URL: https://github.com/apache/spark/pull/60724
### What changes were proposed in this pull request? Parameter substitution rewrites the SQL text before parsing. Error positions for the parsed statement are computed on the substituted text, and the mapping back to the original SQL has gaps. This PR closes them: - `PositionMapper` looks up substitution ranges with binary search instead of a linear scan, and precomputes cumulative offsets. - Adds `PositionMapper.mapStopToOriginal`. An inclusive stop index inside a substituted value maps to the end of the original parameter marker. Previously start and stop both mapped to the start of the marker. - Adds line and column mapping helpers, so the `line` and `startPosition` of an `Origin` (and therefore of `AnalysisException`) are reported in original SQL coordinates. - `Origin.context` uses the stop-aware mapping. - `SparkParserUtils.positionAndText` and `SparkParserUtils.position` map line and column when a position mapper is present. The stored SQL text and the indexes now refer to the same string. - `SubstituteParamsParser` records the separator space it inserts before a replacement that follows a quote character, so the mapper's offsets match the substituted text. - `AbstractParser.parse` maps the current origin to original-text coordinates when it parses text other than the substituted statement, so nested parses do not apply the outer statement's mapping to unrelated text. - `SingleStatement.getText` and the SQL scripting execution nodes use `PositionMapper.originalFragment` when a mapper is present. Previously they sliced the original text with substituted indexes. - Replaces the position-mapping test helpers in `ParametersSuite` and `ParameterSubstitutionSuite`. Those helpers skipped their assertions when no query context was present. The new checks require the context and compare the exact SQL text, range, and fragment. ### Why are the changes needed? When a query contains parameter markers, the parser sees the substituted text. Errors raised after a marker therefore report line, column, and fragment values that depend on the length of the bound values, even when the submitted SQL is the same. Only changing a bound value can move the reported position. The stop-index mapping, the line and column reporting, and the scripting statement text all have the same root cause: the parser works in substituted coordinates, but the error context is reported against the original SQL. ### Does this PR introduce _any_ user-facing change? Yes. For queries with parameter markers: - Error positions (line, column, and query context fragment) for errors that occur after a marker refer to the original SQL text. For the same SQL, changing only a bound value no longer changes the reported position. - The fragment reported for an error that spans a marker now ends at the end of the marker, not its start. - Statement text returned for SQL scripting statements that contain parameter markers is the original statement text. Queries without parameter markers are not affected. ### How was this patch tested? - `build/sbt "catalyst/testOnly org.apache.spark.sql.catalyst.parser.ParameterSubstitutionSuite"`: 20 tests passed. This includes new tests for stop-index mapping inside a replacement, line and column mapping in multiline Unicode SQL, `ParseErrorListener` position mapping, and quote-adjacent separator spaces. - `build/sbt "sql/testOnly org.apache.spark.sql.ParametersSuite -- -z \"position mapping\""`: 17 tests passed. This includes analysis and `EXECUTE IMMEDIATE` position checks with bound values of different lengths. - Scalastyle and scalafmt checks passed (`./dev/lint-scala`). The bare parse-error position tests in `ParametersSuite` (for example `SELECT *** ?`) were removed. Those inputs produce no SQL query context, so the strict helper cannot assert on them. Parse-error mapping is covered by the `ParseErrorListener` unit test in `ParameterSubstitutionSuite` instead. Not yet run: the full `sql` and `catalyst` test suites, and the Connect and Thrift suites. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Cursor (Auto) -- 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]
