Commit f91aacc
authored
Fix SQLInterpolator treating ? in comments and literals as placeholders + shorten next_changelog (#1424)
## Summary
Fixes #1331 + some cosmetic change on next changelog
When `supportManyParameters=1`, `SQLInterpolator.interpolateSQL` split
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:
```sql
-- does this work?
select 'hello?', * from mytable where id = ?
```
This had 3 `?` characters but only 1 real placeholder, so the driver
rejected the perfectly valid binding of 1 parameter.
## What changed
- Added `SqlCommentParser.findPlaceholderPositions(sql)` which walks the
SQL with the existing comment/literal state machine and returns the
source indices of `?` characters that appear in `State.NORMAL` only.
- Reworked `SQLInterpolator.interpolateSQL` to 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.
- Removed the now-redundant `countPlaceholders` helper that scanned
every `?`.
`DatabricksParameterMetaData.countParameters` already used
`SqlCommentParser.forEachNonCommentChar` and is unaffected.
## Test plan
- [x] Existing `SQLInterpolatorTest` cases (basic interpolation, mixed
types, escaped quotes, mismatch errors, etc.) all pass.
- [x] Added unit tests for `SQLInterpolator` covering `?` inside line
comments, block comments, single-quoted strings, double-quoted
identifiers, backtick identifiers, and a combined case.
- [x] Added unit tests for `SqlCommentParser.findPlaceholderPositions`
covering null/empty input, basic positions, comments, literals, quoted
identifiers, and escaped quotes.
- [x] `mvn spotless:apply` clean.
---------
Signed-off-by: samikshya-chand_data <samikshya.chand@databricks.com>1 parent 547b38f commit f91aacc
5 files changed
Lines changed: 331 additions & 66 deletions
File tree
- src
- main/java/com/databricks/jdbc/common/util
- test/java/com/databricks/jdbc/common/util
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 24 | + | |
45 | 25 | | |
46 | 26 | | |
47 | 27 | | |
| |||
Lines changed: 30 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
| |||
65 | 63 | | |
66 | 64 | | |
67 | 65 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | 66 | | |
79 | 67 | | |
80 | 68 | | |
81 | | - | |
82 | | - | |
83 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
84 | 72 | | |
85 | 73 | | |
86 | 74 | | |
| |||
91 | 79 | | |
92 | 80 | | |
93 | 81 | | |
94 | | - | |
95 | | - | |
| 82 | + | |
| 83 | + | |
96 | 84 | | |
97 | 85 | | |
98 | 86 | | |
99 | 87 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
106 | 98 | | |
107 | 99 | | |
108 | 100 | | |
109 | 101 | | |
110 | 102 | | |
111 | | - | |
112 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
113 | 106 | | |
114 | 107 | | |
115 | 108 | | |
116 | 109 | | |
117 | 110 | | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
123 | 123 | | |
124 | | - | |
125 | 124 | | |
126 | 125 | | |
127 | 126 | | |
Lines changed: 58 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
| |||
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
20 | 33 | | |
21 | 34 | | |
22 | 35 | | |
| |||
26 | 39 | | |
27 | 40 | | |
28 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
29 | 51 | | |
30 | 52 | | |
31 | 53 | | |
| |||
48 | 70 | | |
49 | 71 | | |
50 | 72 | | |
51 | | - | |
| 73 | + | |
52 | 74 | | |
53 | 75 | | |
54 | | - | |
| 76 | + | |
55 | 77 | | |
56 | 78 | | |
57 | | - | |
| 79 | + | |
58 | 80 | | |
59 | | - | |
| 81 | + | |
60 | 82 | | |
61 | 83 | | |
62 | 84 | | |
63 | 85 | | |
64 | | - | |
| 86 | + | |
65 | 87 | | |
66 | | - | |
| 88 | + | |
67 | 89 | | |
68 | 90 | | |
69 | 91 | | |
70 | 92 | | |
71 | 93 | | |
72 | 94 | | |
73 | 95 | | |
74 | | - | |
| 96 | + | |
75 | 97 | | |
76 | | - | |
| 98 | + | |
77 | 99 | | |
78 | 100 | | |
79 | 101 | | |
80 | 102 | | |
81 | 103 | | |
82 | 104 | | |
83 | 105 | | |
84 | | - | |
| 106 | + | |
85 | 107 | | |
86 | | - | |
| 108 | + | |
87 | 109 | | |
88 | 110 | | |
89 | 111 | | |
| |||
93 | 115 | | |
94 | 116 | | |
95 | 117 | | |
96 | | - | |
| 118 | + | |
97 | 119 | | |
98 | 120 | | |
99 | 121 | | |
| |||
110 | 132 | | |
111 | 133 | | |
112 | 134 | | |
113 | | - | |
| 135 | + | |
114 | 136 | | |
115 | 137 | | |
116 | 138 | | |
| |||
119 | 141 | | |
120 | 142 | | |
121 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
122 | 168 | | |
123 | 169 | | |
124 | 170 | | |
| |||
Lines changed: 103 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
129 | 201 | | |
130 | 202 | | |
131 | 203 | | |
| |||
177 | 249 | | |
178 | 250 | | |
179 | 251 | | |
180 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
181 | 283 | | |
182 | 284 | | |
183 | 285 | | |
| |||
0 commit comments