Repository navigation
fix(params): keep an under-specified DECIMAL target lossless - #229
Conversation
A `TypedValue::Decimal` bound with a declared scale too small to
represent its value silently truncated on the server: a scale-0
target (`{ value: "123.45", precision: 10, scale: 0 }`) rode the
wire as `DECIMAL(10,0)` and the server rounded 123.45 to 123, and a
declared scale narrower than the value's own (`scale: 2` for
"123.4567") rounded to 123.46. The declared `(precision, scale)`
was emitted verbatim with no guard that the scale could hold the
supplied value.
The value string and the declared scale come from independent
sources on this path (a caller supplies both separately), so an
under-specified scale must never override the value's own lossless
form. When the value's own fractional-digit count exceeds the
declared scale, emit the value-derived `DECIMAL(p,s)` instead so no
digit is lost; otherwise the caller's declared shape rides verbatim.
Matches the reference drivers' SEA bind path: ODBC PR #167 keeps the
lossless value-derived type for a scale-less DECIMAL target and lets
the server infer, and JDBC's `getDecimalTypeString` derives the wire
`DECIMAL(p,s)` straight from the BigDecimal's own precision/scale.
Scientific-notation values keep the declared shape (the true scale
depends on exponent arithmetic the kernel deliberately avoids). The
Arrow bind path is unaffected — there the value is rendered from the
same Arrow scale that produces the type string, so they cannot
disagree.
Closes #210
Closes #215
Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Low
Looks good — the value-derived-scale fix is correct and well-tested (precision ≥ scale invariant holds, casts are range-guarded, prior behavior preserved for exact-fit/wider/integer cases). One low-severity gap: scientific-notation values (e.g. Python's str(Decimal("0.0000001")) → "1E-7") still silently truncate against an under-specified declared scale and diverge from the cited JDBC behavior — documented as intentional, flagged for a follow-up. Nit: the .max(scale) in decimal_value_precision_scale is a no-op since int_significant + scale >= scale always holds (harmless, documents intent)."
| /// before. | ||
| fn decimal_value_precision_scale(value: &str) -> Option<(u8, i8)> { | ||
| let mantissa = value.strip_prefix(['+', '-']).unwrap_or(value); | ||
| // Scientific notation: leave the declared shape untouched (see doc). |
There was a problem hiding this comment.
🔵 Low — Scientific-notation values still silently truncate against an under-specified declared scale — the exact bug class this PR fixes, left unhandled for a form Python commonly emits.
validate_decimal_literal accepts "1E-7" (Python's str(Decimal("0.0000001"))), and such values reach TypedValue::Decimal. Because decimal_value_precision_scale returns None for any mantissa containing e/E, the widen branch is skipped and the declared shape rides verbatim. So { value: "1E-7", precision: 10, scale: 2 } still emits DECIMAL(10,2) and the server rounds 0.0000001 to 0.00 — the same silent-truncation the PR eliminates for plain-decimal values.
This also diverges from the JDBC ground truth the PR cites: BigDecimal("1E-7").scale() is 7, so JDBC's getDecimalTypeString would send a lossless DECIMAL(7,7)-shaped type, whereas the kernel truncates.
The PR documents this as a deliberate scoping decision (avoiding exponent arithmetic), so this is non-blocking — but it's a real remaining lossy path for a common caller shape and worth a tracked follow-up rather than being closed out by this fix. At minimum, consider noting in the PARAMQUERY issues that the scientific-notation form is not covered.
Address review: a scientific-notation value (Python's
`str(Decimal("0.0000001"))` is `"1E-7"`) bound with an
under-specified declared scale still truncated, because
`decimal_value_precision_scale` bailed out (returned `None`) for any
exponent and the declared shape rode the wire unchanged. This path
is reachable — `validate_decimal_literal` accepts scientific
notation and the codec/pyo3 bind path forwards the value verbatim.
Normalise the exponent into the plain-decimal scale (`1E-7` -> scale
7, `1.5E-3` -> scale 4, `1E+2` -> scale 0) so sci-notation values
get the same lossless treatment as plain ones. The value still rides
the wire in its original notation, which the server accepts.
Also drop a no-op `.max(scale)` the reviewer flagged; the restructured
precision computation now genuinely needs the scale floor (a `1E-7`
mantissa has 1 significant digit but scale 7).
Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Low
Looks good — the fix correctly derives a lossless DECIMAL(p,s) only when the declared scale is too small, keeps the declared shape otherwise, and carries solid unit coverage; pre-existing decimal tests and the e2e bind (declared scale ≥ value scale) are unaffected. One Low: the decimal_value_precision_scale helper computes an under-sized precision for positive-exponent scientific notation ("1E+2" → (1,0)), which is latent/masked on the send path today but is baked into a test and would mislead future reuse.
| // requires `precision >= scale`, so floor precision at `scale` | ||
| // (mirrors JDBC's `precision < scale` guard). Also floor at 1. | ||
| let mantissa_significant = (int_part.trim_start_matches('0').len() + frac_part.len()) as i64; | ||
| let precision = mantissa_significant.max(scale).max(1); |
There was a problem hiding this comment.
🔵 Low — decimal_value_precision_scale computes precision from mantissa_significant (the digits written in the mantissa) but does not account for a positive exponent that shifts the decimal point right and inflates the integer magnitude. For "1E+2" (= 100) it returns Some((1, 0)) and for "1.5E+3" (= 1500) it returns Some((2, 0)) — a DECIMAL(1,0) cannot hold 100 and DECIMAL(2,0) cannot hold 1500 (they'd overflow the declared precision server-side). The decimal_value_precision_scale_derives_shape test enshrines these arithmetically-incorrect shapes.
This is currently masked on the send path: typed_value_to_sea_payload only uses the value-derived (val_p, val_s) when val_s > *scale, and a positive exponent floors scale at 0, so the value-derived branch is never taken for these inputs (the caller keeps the declared shape). So there is no wire-facing corruption today. But the helper is documented as a general "recover the value's own precision/scale" routine, and any future reuse (or a change that lets scale-0 value-derived shapes through) would emit a precision too small to represent the value. Consider either flooring precision at int_digits + max(0, -exponent-adjusted magnitude) (i.e. account for the exponent's effect on integer length) or explicitly returning None for positive-exponent inputs and documenting that they always keep the declared shape.
Summary
Fixes two conformance findings (PARAMQUERY-023 / #210 and PARAMQUERY-021 / #215) that are the same bug on the SEA parameter send path: a
TypedValue::Decimalbound with a declared scale too small to represent its value silently truncated on the server.{ value: "123.45", precision: 10, scale: 0 }rode the wire asDECIMAL(10,0)→ server rounded to 123.{ value: "123.4567", precision: 10, scale: 2 }rode asDECIMAL(10,2)→ server rounded to 123.46.typed_value_to_sea_payload(src/statement/params.rs) emitted the declared(precision, scale)verbatim with no guard that the scale could hold the supplied value.Root cause
On the
TypedValue::Decimalpath the value string and the declared(precision, scale)come from independent sources — a caller supplies both separately — so an under-specified declared scale can silently drop the value's fractional digits. (This is unique toTypedValue::Decimal; the Arrow bind path renders the value from the same Arrow scale that produces the type string, so the two cannot disagree, and theparam_rawC-ABI path forwards a host-declared type verbatim by design.)Fix
When the value's own fractional-digit count exceeds the declared scale, ride the wire with the value-derived
DECIMAL(p,s)so no digit is lost; otherwise the caller's declared shape is emitted verbatim (unchanged behavior). A new helperdecimal_value_precision_scalerecovers the value's own precision/scale from the canonical decimal string."123.45"(10, 0)DECIMAL(10,0)→ 123 ❌DECIMAL(5,2)→ 123.45 ✅"123.4567"(10, 2)DECIMAL(10,2)→ 123.46 ❌DECIMAL(7,4)→ 123.4567 ✅"123.45"(10, 2)DECIMAL(10,2)✅DECIMAL(10,2)(unchanged)"123"(10, 0)DECIMAL(10,0)✅DECIMAL(10,0)(unchanged)Scientific-notation values (
"1E-7") keep the declared shape — the true scale depends on exponent arithmetic the kernel deliberately avoids.Reference-driver ground truth
Per the conformance workflow, the intended behavior was confirmed against the reference drivers' SEA bind path (not taken from the issue's stated "expected" at face value):
ConvertBoundParameterapplies the declared DECIMAL type only whenscale > 0; a scale-less target keeps the lossless value-derived type and lets the server infer.DatabricksTypeUtil.getDecimalTypeString, used by the SEADatabricksSdkClient): derives the wireDECIMAL(p,s)straight from theBigDecimal's own precision/scale, ignoring any under-specified declared scale.Both converge on the same principle — an under-specified declared scale must never override the value's own lossless form. Because the kernel's
Decimalcarries the value as a decimal-form string (like JDBC'sBigDecimal, unlike ODBC's raw char buffer), the value-derivedDECIMAL(p,s)is the faithful port.Test plan
Unit tests in
src/statement/params.rs:typed_value_decimal_scale_less_target_keeps_value_scale— the [coverage] Conformance findings: PARAMQUERY-023 #210 reproduction:scale:0+"123.45"now emitsDECIMAL(5,2). Failed before the fix, passes after.typed_value_decimal_narrower_scale_widens_to_value_scale— the [coverage] Conformance findings: PARAMQUERY-021 #215 case:scale:2+"123.4567"now emitsDECIMAL(7,4).typed_value_decimal_declared_scale_sufficient_is_unchanged— regression guard: exact-fit, declared-wider, and integer-scale-0 all ride verbatim.decimal_value_precision_scale_derives_shape— helper edge cases: negatives, leading-zero fractions (0.05), no fractional part, scientific notation, and theprecision > 38clamp.Existing decimal tests (incl.
decimal_single_arg_defaults_scale_to_zeroand the scientific-notation cases) still pass. Fullcargo testgreen,cargo clippy --all-targets -- -D warningsclean,cargo fmtapplied.The reproducing xfail tripwires live in the coverage PR databricks/databricks-driver-test#1061 (
scale_less_decimal_target_stays_lossless) and #1117 (PARAMQUERY-021); they flip green once this merges.Closes #210
Closes #215
This pull request and its description were written by Isaac.