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

Function output_scale_for_decimal

datafusion/functions/src/math/round.rs:42–60  ·  view source on GitHub ↗
(precision: u8, input_scale: i8, decimal_places: i32)

Source from the content-addressed store, hash-verified

40use std::sync::Arc;
41
42fn output_scale_for_decimal(precision: u8, input_scale: i8, decimal_places: i32) -> i8 {
43 // `decimal_places` controls the maximum output scale, but scale cannot exceed the input scale.
44 //
45 // For negative-scale decimals, allow further scale reduction to match negative `decimal_places`
46 // (e.g. scale -2 rounded to -3 becomes scale -3). This preserves fixed precision by
47 // representing the rounded result at a coarser scale.
48 if input_scale < 0 {
49 // Decimal scales must be within [-precision, precision] and fit in i8. For negative-scale
50 // decimals, allow rounding to move the output scale further negative, but cap it at
51 // `-precision` (beyond that, the rounded result is always 0).
52 let min_scale = -i32::from(precision);
53 let new_scale = i32::from(input_scale).min(decimal_places).max(min_scale);
54 return new_scale as i8;
55 }
56
57 // The `min` ensures the result is always within i8 range because `input_scale` is i8.
58 let decimal_places = decimal_places.max(0);
59 i32::from(input_scale).min(decimal_places) as i8
60}
61
62fn normalize_decimal_places_for_decimal(
63 decimal_places: i32,

Callers 1

Calls 2

maxMethod · 0.45
minMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…