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

Function get_scale

datafusion/spark/src/function/math/round.rs:130–165  ·  view source on GitHub ↗

Extract the scale (decimal places) from the second argument. Returns `Some(0)` if no second argument is provided. Returns `None` if the scale argument is NULL (Spark returns NULL for `round(expr, NULL)`).

(args: &[ColumnarValue])

Source from the content-addressed store, hash-verified

128/// Returns `Some(0)` if no second argument is provided.
129/// Returns `None` if the scale argument is NULL (Spark returns NULL for `round(expr, NULL)`).
130fn get_scale(args: &[ColumnarValue]) -> Result<Option<i32>> {
131 if args.len() < 2 {
132 return Ok(Some(0));
133 }
134
135 match &args[1] {
136 ColumnarValue::Scalar(ScalarValue::Int8(Some(v))) => Ok(Some(i32::from(*v))),
137 ColumnarValue::Scalar(ScalarValue::Int16(Some(v))) => Ok(Some(i32::from(*v))),
138 ColumnarValue::Scalar(ScalarValue::Int32(Some(v))) => Ok(Some(*v)),
139 ColumnarValue::Scalar(ScalarValue::Int64(Some(v))) => {
140 i32::try_from(*v).map(Some).map_err(|_| {
141 (exec_err!("round scale {v} is out of supported i32 range")
142 as Result<(), _>)
143 .unwrap_err()
144 })
145 }
146 ColumnarValue::Scalar(ScalarValue::UInt8(Some(v))) => Ok(Some(i32::from(*v))),
147 ColumnarValue::Scalar(ScalarValue::UInt16(Some(v))) => Ok(Some(i32::from(*v))),
148 ColumnarValue::Scalar(ScalarValue::UInt32(Some(v))) => {
149 i32::try_from(*v).map(Some).map_err(|_| {
150 (exec_err!("round scale {v} is out of supported i32 range")
151 as Result<(), _>)
152 .unwrap_err()
153 })
154 }
155 ColumnarValue::Scalar(ScalarValue::UInt64(Some(v))) => {
156 i32::try_from(*v).map(Some).map_err(|_| {
157 (exec_err!("round scale {v} is out of supported i32 range")
158 as Result<(), _>)
159 .unwrap_err()
160 })
161 }
162 ColumnarValue::Scalar(sv) if sv.is_null() => Ok(None),
163 other => exec_err!("Unsupported type for round scale: {}", other.data_type()),
164 }
165}
166
167/// Round a floating-point value to the given number of decimal places using
168/// HALF_UP rounding mode (ties round away from zero).

Callers 1

spark_roundFunction · 0.85

Calls 4

unwrap_errMethod · 0.80
lenMethod · 0.45
mapMethod · 0.45
is_nullMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…