MCPcopy Create free account
hub / github.com/apache/datafusion / decimal_preimage_bounds

Function decimal_preimage_bounds

datafusion/functions/src/math/floor.rs:347–379  ·  view source on GitHub ↗

Compute preimage bounds for floor function on decimal types. For floor(x) = n, the preimage is [n, n+1). Returns None if: - The value has a fractional part (floor always returns integers) - Adding 1 would overflow

(
    value: D::Native,
    precision: u8,
    scale: i8,
)

Source from the content-addressed store, hash-verified

345/// - The value has a fractional part (floor always returns integers)
346/// - Adding 1 would overflow
347fn decimal_preimage_bounds<D: DecimalType>(
348 value: D::Native,
349 precision: u8,
350 scale: i8,
351) -> Option<(D::Native, D::Native)>
352where
353 D::Native: DecimalCast + ArrowNativeTypeOp + std::ops::Rem<Output = D::Native>,
354{
355 // Use rescale_decimal to compute "1" at target scale (avoids manual pow)
356 // Convert integer 1 (scale=0) to the target scale
357 let one_scaled: D::Native = rescale_decimal::<D, D>(
358 D::Native::ONE, // value = 1
359 1, // input_precision = 1
360 0, // input_scale = 0 (integer)
361 precision, // output_precision
362 scale, // output_scale
363 )?;
364
365 // floor always returns an integer, so if value has a fractional part, there's no solution
366 // Check: value % one_scaled != 0 means fractional part exists
367 if scale > 0 && value % one_scaled != D::Native::ZERO {
368 return None;
369 }
370
371 // Compute upper bound using checked addition
372 // Before preimage stage, the internal i128/i256(value) is validated based on the precision and scale.
373 // MAX_DECIMAL128_FOR_EACH_PRECISION and MAX_DECIMAL256_FOR_EACH_PRECISION are used to validate the internal i128/i256.
374 // Any invalid i128/i256 will not reach here.
375 // Therefore, the add_checked will always succeed if tested via SQL/SLT path.
376 let upper = value.add_checked(one_scaled).ok()?;
377
378 Some((value, upper))
379}
380
381#[cfg(test)]
382mod tests {

Callers

nothing calls this directly

Calls 1

add_checkedMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…