Repository navigation
Commit 672f6f0
authored
refactor(jdbc): extract numeric text parsing and collapse numeric getters (#218)
## Summary
Extracts SQLite's text-to-number coercion from `LibsqlResultSet` into a
package-private `SqliteNumericText`, then collapses the duplicated
`getInt`/`getFloat` switch ladders onto the existing `toLong`/`toDouble`
switches.
- New `SqliteNumericText` holds `parseLong(String)`,
`parseDouble(String)`, and `blobAsText(byte[])` plus the private
scanners `skipSqliteWhitespace`, `isSqliteWhitespace`, `isAsciiDigit`.
The scanner bodies are byte-identical to the deleted ones; only
`parseSqliteLong`/`parseSqliteDouble` were renamed to
`parseLong`/`parseDouble`.
- `toLong(Value)` and `toDouble(Value)` remain the single `Value`
switches and now delegate the text and BLOB cases.
- `getInt` is `(int) toLong(getCurrentValue(columnIndex))`; `getFloat`
is `(float) toDouble(getCurrentValue(columnIndex))`.
- No parser rule, message, saturation bound, date/time code, LOB class,
or POM changed. A follow-up commit adds characterization tests for the
saturation bound and all six SQLite whitespace characters.
`LibsqlResultSet.java` is now 912 lines (was 1029), under the 950-line
acceptance bound.
## Equivalence of the collapsed getters
The pre-change switch arms, the `toLong`/`toDouble` arms, and the new
casts are compared arm by arm:
| `Value` variant | Pre-change `getInt` arm | `(int) toLong(v)` arm |
Equal |
|---|---|---|---|
| `Null` | `0` | `(int) 0L` = `0` | yes |
| `Integer` | `(int) i.value()` | `(int) i.value()` | yes |
| `Float` | `(int) (long) f.value()` | `(int) (long) f.value()` | yes —
truncate to int64, then low-32-bit wrap |
| `Text` | `(int) parseSqliteLong(t.value())` | `(int)
SqliteNumericText.parseLong(t.value())` | yes — same body |
| `Blob` | `(int) parseSqliteLong(blobAsText(b))` | `(int)
SqliteNumericText.parseLong(SqliteNumericText.blobAsText(b.bytes()))` |
yes — same body and UTF-8 decode |
| `Value` variant | Pre-change `getFloat` arm | `(float) toDouble(v)`
arm | Equal |
|---|---|---|---|
| `Null` | `0.0F` | `(float) 0.0` = `0.0F` | yes |
| `Integer` | `(float) (double) i.value()` | `(float) (double)
i.value()` | yes — rounds through double, not direct long→float |
| `Float` | `(float) f.value()` | `(float) f.value()` | yes |
| `Text` | `(float) parseSqliteDouble(t.value())` | `(float)
SqliteNumericText.parseDouble(t.value())` | yes — same body |
| `Blob` | `(float) parseSqliteDouble(blobAsText(b))` | `(float)
SqliteNumericText.parseDouble(SqliteNumericText.blobAsText(b.bytes()))`
| yes |
A scratch differential probe (not committed) ran both ladders over the
five variants: `Null`; boundary and wrapping `Integer` values
(`Long.MIN/MAX_VALUE`, `Integer.MIN/MAX_VALUE`, `1L << 32`,
`3_000_000_000L`); `Float` values including `NaN`, both infinities,
`-0.0`, `3.9e9`, `Long.MAX_VALUE` as double, and `Math.nextDown(1.0f)`;
30 text and 30 BLOB inputs covering empty, whitespace-only, sign-only,
numeric prefixes, trailing garbage, `3.5e` / `3.5e+` (no exponent
digit), `3.5e-2x`, `.5`, and int64 overflow both ways; plus 40,000
random longs and random-bits doubles.
Result: **60,092 values, 0 mismatches** (int compared by value, float by
raw bits).
`getBoolean` still routes through `getInt`, so REAL `0.5` stays false,
and `LibsqlResultSetTest` / `LibsqlResultSetGetBooleanTest` pass
unedited.
## Follow-up: characterization tests
Commit `373b875` adds caller-observable tests to `LibsqlResultSetTest`
without touching production code.
| Mutant | Test that kills it | Observed failure |
|---|---|---|
| Halve the saturation bound in `parseLong` |
`largeIntegerTextBelowLongMaxIsReturnedUnchanged` | `expected:
6000000000000000000L` / `but was: 9223372036854775807L` |
| Drop `'\t'` from the whitespace set |
`everySqliteWhitespaceCharacterIsSkippedBeforeTheNumericPrefix(String)[2]`
(the tab invocation only) | `expected: 42` / `but was: 0` |
New coverage:
- Large valid integer `6000000000000000000` (between `Long.MAX_VALUE /
2` and `Long.MAX_VALUE`) read through `getLong` and `getBigDecimal`,
unchanged.
- Text just past `Long.MAX_VALUE` saturates to `Long.MAX_VALUE`; text
just past `Long.MIN_VALUE` saturates to `Long.MIN_VALUE`.
- Space, tab, newline, carriage return, vertical tab, and form feed each
skipped before a numeric prefix, as one parameterized test.
`LibsqlResultSetTest` is 45 tests (was 36): 4 new methods, one
parameterized with 6 invocations. The JDBC module is 392 tests (was
383).
## Diff stat
```text
.../magnusp/libsql/jdbc/LibsqlResultSet.java | 133 ++-------------------
.../magnusp/libsql/jdbc/SqliteNumericText.java | 121 +++++++++++++++++++
.../magnusp/libsql/jdbc/LibsqlResultSetTest.java | 36 ++++++
3 files changed, 165 insertions(+), 125 deletions(-)
```
## Lane results
| Lane | Result |
|---|---|
| `./mvnw -pl libsql-jdbc -am test` | green — 392 tests, 0 failures, 0
errors, 0 skipped |
| `./mvnw spotless:apply` then `./mvnw spotless:check` | green — BUILD
SUCCESS, no reformat needed |
| `./mvnw -pl libsql-conformance -am verify` | green — 272 tests, 0
failures, 0 errors, 7 skipped |
Fixes #2001 parent 850f437 commit 672f6f0
3 files changed
Lines changed: 165 additions & 125 deletions
File tree
- libsql-jdbc/src
- main/java/com/github/magnusp/libsql/jdbc
- test/java/com/github/magnusp/libsql/jdbc
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
| 205 | + | |
213 | 206 | | |
214 | 207 | | |
215 | 208 | | |
| |||
219 | 212 | | |
220 | 213 | | |
221 | 214 | | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
| 215 | + | |
230 | 216 | | |
231 | 217 | | |
232 | 218 | | |
| |||
239 | 225 | | |
240 | 226 | | |
241 | 227 | | |
242 | | - | |
243 | | - | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
244 | 231 | | |
245 | 232 | | |
246 | 233 | | |
| |||
249 | 236 | | |
250 | 237 | | |
251 | 238 | | |
252 | | - | |
253 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
254 | 242 | | |
255 | 243 | | |
256 | 244 | | |
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 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | 245 | | |
363 | 246 | | |
364 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| 14 | + | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
| |||
138 | 141 | | |
139 | 142 | | |
140 | 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 | + | |
141 | 177 | | |
142 | 178 | | |
143 | 179 | | |
| |||
0 commit comments