Skip to content

Commit bb75d92

Browse files
fix: support parentheses for negative decimal formatting (#23718)
## Which issue does this PR close? - Closes #23717. ## Rationale for this change Spark and Java format negative numeric values with parentheses when the `(` flag is present. The decimal formatting path always emitted a minus sign because it ignored `negative_in_parentheses`, while the floating-point path already handled the flag. This made `format_string` inconsistent across numeric input types. ## What changes are included in this PR? - Add a closing suffix when a negative decimal uses parentheses formatting. - Include the suffix when calculating width, left alignment, and zero padding. - Add regression coverage for grouped negative decimals with and without an explicit width. ## Are these changes tested? Yes. The following checks pass locally: - `cargo fmt --all -- --check` - `cargo clippy --all-targets --all-features -- -D warnings` - `cargo test -p datafusion-spark --lib` - The full workspace test command from the contributor guide with `avro,json,backtrace,extended_tests,recursive_protection,parquet_encryption` enabled ## Are there any user-facing changes? Yes. Spark-compatible formatting of negative decimal values now honors the parentheses flag. There are no public API or breaking changes.
1 parent 0b07e58 commit bb75d92

1 file changed

Lines changed: 42 additions & 13 deletions

File tree

‎datafusion/spark/src/function/string/format_string.rs‎

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,7 @@ impl ConversionSpecifier {
19821982
self.validate_grouping_separator()?;
19831983

19841984
let mut prefix = String::new();
1985+
let mut suffix = String::new();
19851986
let upper = self.conversion_type.is_upper();
19861987

19871988
// Parse as BigDecimal
@@ -1991,15 +1992,16 @@ impl ConversionSpecifier {
19911992
let decimal = BigDecimal::from_bigint(decimal, scale);
19921993

19931994
// Handle sign
1994-
// TODO: `negative_in_parentheses` (the `(` flag) is not implemented here.
1995-
// Java/Spark wrap negative values in parentheses when this flag is set
1996-
// (e.g. `%(,.2f` with -1234.5 → "(1,234.50)"), but this path always
1997-
// uses a minus sign. See `format_float` for the correct implementation.
19981995
let is_negative = decimal.sign() == Sign::Minus;
19991996
let abs_decimal = decimal.abs();
20001997

20011998
if is_negative {
2002-
prefix.push('-');
1999+
if self.negative_in_parentheses {
2000+
prefix.push('(');
2001+
suffix.push(')');
2002+
} else {
2003+
prefix.push('-');
2004+
}
20032005
} else if self.space_sign {
20042006
prefix.push(' ');
20052007
} else if self.force_sign {
@@ -2078,23 +2080,25 @@ impl ConversionSpecifier {
20782080
let NumericParam::Literal(width) = self.width else {
20792081
writer.push_str(&prefix);
20802082
writer.push_str(&number);
2083+
writer.push_str(&suffix);
20812084
return Ok(());
20822085
};
20832086

20842087
if self.left_adj {
2085-
let mut full_num = prefix + &number;
2088+
let mut full_num = prefix + &number + &suffix;
20862089
while full_num.len() < width as usize {
20872090
full_num.push(' ');
20882091
}
20892092
writer.push_str(&full_num);
20902093
} else if self.zero_pad {
2091-
while prefix.len() + number.len() < width as usize {
2094+
while prefix.len() + number.len() + suffix.len() < width as usize {
20922095
prefix.push('0');
20932096
}
20942097
writer.push_str(&prefix);
20952098
writer.push_str(&number);
2099+
writer.push_str(&suffix);
20962100
} else {
2097-
let mut full_num = prefix + &number;
2101+
let mut full_num = prefix + &number + &suffix;
20982102
while full_num.len() < width as usize {
20992103
full_num = " ".to_owned() + &full_num;
21002104
}
@@ -2372,7 +2376,7 @@ mod tests {
23722376
use super::*;
23732377
use crate::function::utils::test::test_scalar_function;
23742378
use arrow::array::StringArray;
2375-
use arrow::datatypes::DataType::Utf8;
2379+
use arrow::datatypes::{DataType::Utf8, i256};
23762380

23772381
#[test]
23782382
fn test_format_string_nullability() -> Result<()> {
@@ -2896,17 +2900,42 @@ mod tests {
28962900

28972901
#[test]
28982902
fn test_grouping_separator_parentheses_decimal() -> Result<()> {
2899-
// %(,15.2f on negative decimal — format_decimal ignores negative_in_parentheses,
2900-
// always uses '-'. Check TODO in fn format_decimal
2903+
test_scalar_function!(
2904+
FormatStringFunc::new(),
2905+
vec![
2906+
ColumnarValue::Scalar(ScalarValue::Utf8(Some("%(,.2f".to_string()))),
2907+
ColumnarValue::Scalar(ScalarValue::Decimal128(Some(-123450), 10, 2)),
2908+
],
2909+
Ok(Some("(1,234.50)")),
2910+
&str,
2911+
Utf8,
2912+
StringArray
2913+
);
2914+
2915+
test_scalar_function!(
2916+
FormatStringFunc::new(),
2917+
vec![
2918+
ColumnarValue::Scalar(ScalarValue::Utf8(Some("%(,.2f".to_string()))),
2919+
ColumnarValue::Scalar(ScalarValue::Decimal256(
2920+
Some(i256::from(-123450)),
2921+
10,
2922+
2,
2923+
)),
2924+
],
2925+
Ok(Some("(1,234.50)")),
2926+
&str,
2927+
Utf8,
2928+
StringArray
2929+
);
2930+
29012931
// Java: String.format("%(,15.2f", -1234.5) → " (1,234.50)"
2902-
// Ours: " -1,234.50" (minus sign, no parens)
29032932
test_scalar_function!(
29042933
FormatStringFunc::new(),
29052934
vec![
29062935
ColumnarValue::Scalar(ScalarValue::Utf8(Some("%(,15.2f".to_string()))),
29072936
ColumnarValue::Scalar(ScalarValue::Decimal128(Some(-123450), 10, 2)),
29082937
],
2909-
Ok(Some(" -1,234.50")),
2938+
Ok(Some(" (1,234.50)")),
29102939
&str,
29112940
Utf8,
29122941
StringArray

0 commit comments

Comments
 (0)