Skip to content

fix(params): keep an under-specified DECIMAL target lossless - #229

Merged
vikrantpuppala merged 2 commits into
mainfrom
fix/210-215-decimal-scale-less-truncation
Aug 10, 2026
Merged

vikrantpuppala merged 2 commits into
mainfrom
fix/210-215-decimal-scale-less-truncation

Conversation

@vikrantpuppala

Copy link
Copy Markdown
Collaborator

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::Decimal bound 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 as DECIMAL(10,0) → server rounded to 123.
  • { value: "123.4567", precision: 10, scale: 2 } rode as DECIMAL(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::Decimal path 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 to TypedValue::Decimal; the Arrow bind path renders the value from the same Arrow scale that produces the type string, so the two cannot disagree, and the param_raw C-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 helper decimal_value_precision_scale recovers the value's own precision/scale from the canonical decimal string.

bound value declared before after
"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):

  • ODBC (seeding reference, PR #167): ConvertBoundParameter applies the declared DECIMAL type only when scale > 0; a scale-less target keeps the lossless value-derived type and lets the server infer.
  • JDBC (DatabricksTypeUtil.getDecimalTypeString, used by the SEA DatabricksSdkClient): derives the wire DECIMAL(p,s) straight from the BigDecimal'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 Decimal carries the value as a decimal-form string (like JDBC's BigDecimal, unlike ODBC's raw char buffer), the value-derived DECIMAL(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 emits DECIMAL(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 emits DECIMAL(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 the precision > 38 clamp.

Existing decimal tests (incl. decimal_single_arg_defaults_scale_to_zero and the scientific-notation cases) still pass. Full cargo test green, cargo clippy --all-targets -- -D warnings clean, cargo fmt applied.

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.

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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)."

Comment thread src/statement/params.rs Outdated
/// 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).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/statement/params.rs
// 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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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.

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Aug 10, 2026
Merged via the queue into main with commit 94550a0 Aug 10, 2026
20 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — b396f43b Deployed Aug 10, 2026 by oss-reviewer-bot[bot] via followup #240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[coverage] Conformance findings: PARAMQUERY-021 [coverage] Conformance findings: PARAMQUERY-023

1 participant