Fix SQLInterpolator treating ? in comments and literals as placeholders#1424
Open
samikshya-db wants to merge 5 commits intodatabricks:mainfrom
Open
Fix SQLInterpolator treating ? in comments and literals as placeholders#1424samikshya-db wants to merge 5 commits intodatabricks:mainfrom
samikshya-db wants to merge 5 commits intodatabricks:mainfrom
Conversation
When supportManyParameters=1, SQLInterpolator split the SQL on every '?', so question marks inside line/block comments, string literals, and quoted identifiers were counted as parameter placeholders. This caused "Parameter count does not match" errors for queries like: -- does this work? select 'hello?', * from mytable where id = ? Add SqlCommentParser.findPlaceholderPositions to locate only real '?' markers (state == NORMAL) and use it from SQLInterpolator. Fixes databricks#1331. Signed-off-by: samikshya-chand_data <samikshya.chand@databricks.com>
- Add IndexedSqlCharConsumer overload to forEachNonCommentChar so callers can recover the source-string index of each emitted character. Refactor findPlaceholderPositions to delegate to it, removing ~70 lines of duplicated state-machine logic. - Apply the same comment/literal-aware fix to surroundPlaceholdersWithQuotes, which previously used a regex that ignored comments, double-quoted identifiers, and backtick identifiers. A '?' inside any of those is now preserved instead of being wrapped in single quotes. - Drop the now-unused Matcher/Pattern imports from SQLInterpolator. - Add tests for CRLF line comments, nested block comments containing '?', adjacent placeholders, leading/trailing placeholders, escaped backticks, and the new surroundPlaceholdersWithQuotes cases. Signed-off-by: samikshya-chand_data <samikshya.chand@databricks.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1331.
When
supportManyParameters=1,SQLInterpolator.interpolateSQLsplit the SQL on every?and counted every occurrence as a placeholder. Question marks inside line/block comments, single-quoted strings, double-quoted identifiers, and backtick-quoted identifiers were all counted, producing spurious"Parameter count does not match"errors for queries such as:This had 3
?characters but only 1 real placeholder, so the driver rejected the perfectly valid binding of 1 parameter.What changed
SqlCommentParser.findPlaceholderPositions(sql)which walks the SQL with the existing comment/literal state machine and returns the source indices of?characters that appear inState.NORMALonly.SQLInterpolator.interpolateSQLto use those positions: a single pass that slices the original SQL between placeholders and substitutes formatted parameter values. Comments, string literals, and quoted identifiers are preserved verbatim in the output.countPlaceholdershelper that scanned every?.DatabricksParameterMetaData.countParametersalready usedSqlCommentParser.forEachNonCommentCharand is unaffected.Test plan
SQLInterpolatorTestcases (basic interpolation, mixed types, escaped quotes, mismatch errors, etc.) all pass.SQLInterpolatorcovering?inside line comments, block comments, single-quoted strings, double-quoted identifiers, backtick identifiers, and a combined case.SqlCommentParser.findPlaceholderPositionscovering null/empty input, basic positions, comments, literals, quoted identifiers, and escaped quotes.mvn spotless:applyclean.