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

Function spark_round

datafusion/spark/src/function/math/round.rs:421–654  ·  view source on GitHub ↗
(args: &[ColumnarValue], enable_ansi_mode: bool)

Source from the content-addressed store, hash-verified

419// ---------------------------------------------------------------------------
420
421fn spark_round(args: &[ColumnarValue], enable_ansi_mode: bool) -> Result<ColumnarValue> {
422 if args.is_empty() || args.len() > 2 {
423 return exec_err!("round requires 1 or 2 arguments, got {}", args.len());
424 }
425
426 let scale = match get_scale(args)? {
427 Some(s) => s,
428 None => {
429 // NULL scale → return NULL with the same data type as the first argument
430 return Ok(ColumnarValue::Scalar(ScalarValue::try_from(
431 args[0].data_type(),
432 )?));
433 }
434 };
435
436 match &args[0] {
437 ColumnarValue::Array(array) => match array.data_type() {
438 DataType::Null => Ok(args[0].clone()),
439
440 // Integer types
441 DataType::Int8 => {
442 impl_integer_array_round!(array, Int8Type, scale, enable_ansi_mode)
443 }
444 DataType::Int16 => {
445 impl_integer_array_round!(array, Int16Type, scale, enable_ansi_mode)
446 }
447 DataType::Int32 => {
448 impl_integer_array_round!(array, Int32Type, scale, enable_ansi_mode)
449 }
450 DataType::Int64 => {
451 impl_integer_array_round!(array, Int64Type, scale, enable_ansi_mode)
452 }
453
454 // Unsigned integer types
455 DataType::UInt8 => {
456 impl_integer_array_round!(array, UInt8Type, scale, enable_ansi_mode)
457 }
458 DataType::UInt16 => {
459 impl_integer_array_round!(array, UInt16Type, scale, enable_ansi_mode)
460 }
461 DataType::UInt32 => {
462 impl_integer_array_round!(array, UInt32Type, scale, enable_ansi_mode)
463 }
464 DataType::UInt64 => {
465 let array = array.as_primitive::<UInt64Type>();
466 let result: PrimitiveArray<UInt64Type> = array.try_unary(|x| {
467 let v_i64 = i64::try_from(x).map_err(|_| {
468 (exec_err!(
469 "round: UInt64 value {x} exceeds i64::MAX and cannot be rounded"
470 ) as Result<(), _>)
471 .unwrap_err()
472 })?;
473 round_integer(v_i64, scale, enable_ansi_mode)
474 .map(|v| v as u64)
475 })?;
476 Ok(ColumnarValue::Array(Arc::new(result)))
477 }
478

Callers 1

invoke_with_argsMethod · 0.85

Calls 15

get_scaleFunction · 0.85
round_integerFunction · 0.85
newFunction · 0.85
unwrap_errMethod · 0.80
round_floatFunction · 0.70
round_decimalFunction · 0.70
Decimal32Class · 0.50
Decimal64Class · 0.50
Decimal128Class · 0.50
Decimal256Class · 0.50
is_emptyMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…