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,
)
| 345 | /// - The value has a fractional part (floor always returns integers) |
| 346 | /// - Adding 1 would overflow |
| 347 | fn decimal_preimage_bounds<D: DecimalType>( |
| 348 | value: D::Native, |
| 349 | precision: u8, |
| 350 | scale: i8, |
| 351 | ) -> Option<(D::Native, D::Native)> |
| 352 | where |
| 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)] |
| 382 | mod tests { |
nothing calls this directly
no test coverage detected
searching dependent graphs…