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

Function round_columnar

datafusion/functions/src/math/round.rs:459–631  ·  view source on GitHub ↗
(
    value: &ColumnarValue,
    decimal_places: &ColumnarValue,
    number_rows: usize,
    return_type: &DataType,
)

Source from the content-addressed store, hash-verified

457}
458
459fn round_columnar(
460 value: &ColumnarValue,
461 decimal_places: &ColumnarValue,
462 number_rows: usize,
463 return_type: &DataType,
464) -> Result<ColumnarValue> {
465 let value_array = value.to_array(number_rows)?;
466 let both_scalars = matches!(value, ColumnarValue::Scalar(_))
467 && matches!(decimal_places, ColumnarValue::Scalar(_));
468 let decimal_places_is_array = matches!(decimal_places, ColumnarValue::Array(_));
469
470 let arr: ArrayRef = match (value_array.data_type(), return_type) {
471 (Float64, _) => {
472 let result = calculate_binary_math::<Float64Type, Int32Type, Float64Type, _>(
473 value_array.as_ref(),
474 decimal_places,
475 round_float::<f64>,
476 )?;
477 result as _
478 }
479 (Float32, _) => {
480 let result = calculate_binary_math::<Float32Type, Int32Type, Float32Type, _>(
481 value_array.as_ref(),
482 decimal_places,
483 round_float::<f32>,
484 )?;
485 result as _
486 }
487 (Decimal32(input_precision, scale), Decimal32(precision, new_scale)) => {
488 // reduce scale to reclaim integer precision
489 let result = calculate_binary_decimal_math::<
490 Decimal32Type,
491 Int32Type,
492 Decimal32Type,
493 _,
494 >(
495 value_array.as_ref(),
496 decimal_places,
497 |v, dp| {
498 let rounded = round_decimal_or_zero(
499 v,
500 *input_precision,
501 *scale,
502 *new_scale,
503 dp,
504 )?;
505 if *precision == Decimal32Type::MAX_PRECISION
506 && (decimal_places_is_array || (*scale == 0 && dp < 0))
507 {
508 // If we're already at max precision, we can't widen the result type. For
509 // dp arrays, or for scale == 0 with negative dp, rounding can overflow the
510 // fixed-precision type. Validate per-row and return an error instead of
511 // producing an invalid decimal that Arrow may display incorrectly.
512 validate_decimal_precision::<Decimal32Type>(
513 rounded, *precision, *new_scale,
514 )
515 } else {
516 Ok(rounded)

Callers 2

invoke_with_argsMethod · 0.85
round_arraysFunction · 0.85

Calls 5

round_decimal_or_zeroFunction · 0.85
to_arrayMethod · 0.45
data_typeMethod · 0.45
as_refMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…