You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/08/11 09:57:53 UTC

(datafusion) branch gh-readonly-queue/main/pr-24115-8cb7c84d6c3e4b0157985ef8d0b076cde7bf7e59 created (now d3100e2028)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-24115-8cb7c84d6c3e4b0157985ef8d0b076cde7bf7e59
in repository https://gitbox.apache.org/repos/asf/datafusion.git


      at d3100e2028 refactor: Refactor numeric sign and padding in Spark format_string (#24115)

This branch includes the following new commits:

     new d3100e2028 refactor: Refactor numeric sign and padding in Spark format_string (#24115)

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org


(datafusion) 01/01: refactor: Refactor numeric sign and padding in Spark format_string (#24115)

Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-24115-8cb7c84d6c3e4b0157985ef8d0b076cde7bf7e59
in repository https://gitbox.apache.org/repos/asf/datafusion.git

commit d3100e2028adfe8560024c40ec3719a2e0fca2ee
Author: Justin O'Dwyer <ju...@datadoghq.com>
AuthorDate: Tue Aug 11 11:57:29 2026 +0200

    refactor: Refactor numeric sign and padding in Spark format_string (#24115)
    
    ## Which issue does this PR close?
    
    <!--
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax. For example
    `Closes #123` indicates that this PR will close issue #123.
    -->
    
    - Closes https://github.com/apache/datafusion/issues/24058
    
    ## Rationale for this change
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    
    -->
    
    `format_decimal` and `format_float` had duplicated logic, and it'd be
    easy to modify one function and forget to change the other, causing
    bugs. This PR forces one source of truth for that logic.
    
    ## What changes are included in this PR?
    
    <!--
    There is no need to duplicate the description in the issue here, but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    Created a new helper.
    
    ## Are these changes tested?
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    -->
    
    Yes,
    
    ```
    cargo t -p datafusion-spark format_string --lib
    ```
    
    passes.
    
    ## Are there any user-facing changes?
    
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    
    If there are any breaking changes to public APIs, please add the `api
    change` label.
    -->
    
    No user facing-changes.
---
 .../spark/src/function/string/format_string.rs     | 144 +++++++++++++--------
 1 file changed, 88 insertions(+), 56 deletions(-)

diff --git a/datafusion/spark/src/function/string/format_string.rs b/datafusion/spark/src/function/string/format_string.rs
index 60b6d37e55..6a65164a18 100644
--- a/datafusion/spark/src/function/string/format_string.rs
+++ b/datafusion/spark/src/function/string/format_string.rs
@@ -1789,34 +1789,8 @@ impl ConversionSpecifier {
                 }
             }
         }
-        // Take care of padding
-        let NumericParam::Literal(width) = self.width else {
-            writer.push_str(&prefix);
-            writer.push_str(&number);
-            writer.push_str(&suffix);
-            return Ok(());
-        };
-        if self.left_adj {
-            let mut full_num = prefix + &number + &suffix;
-            while full_num.len() < width as usize {
-                full_num.push(' ');
-            }
-            writer.push_str(&full_num);
-        } else if self.zero_pad && value.is_finite() {
-            while prefix.len() + number.len() + suffix.len() < width as usize {
-                prefix.push('0');
-            }
-            writer.push_str(&prefix);
-            writer.push_str(&number);
-            writer.push_str(&suffix);
-        } else {
-            let mut full_num = prefix + &number + &suffix;
-            while full_num.len() < width as usize {
-                full_num = " ".to_owned() + &full_num;
-            }
-            writer.push_str(&full_num);
-        };
 
+        self.write_numeric_parts(writer, prefix, &number, &suffix, value.is_finite());
         Ok(())
     }
 
@@ -2076,35 +2050,7 @@ impl ConversionSpecifier {
             }
         };
 
-        // Handle padding
-        let NumericParam::Literal(width) = self.width else {
-            writer.push_str(&prefix);
-            writer.push_str(&number);
-            writer.push_str(&suffix);
-            return Ok(());
-        };
-
-        if self.left_adj {
-            let mut full_num = prefix + &number + &suffix;
-            while full_num.len() < width as usize {
-                full_num.push(' ');
-            }
-            writer.push_str(&full_num);
-        } else if self.zero_pad {
-            while prefix.len() + number.len() + suffix.len() < width as usize {
-                prefix.push('0');
-            }
-            writer.push_str(&prefix);
-            writer.push_str(&number);
-            writer.push_str(&suffix);
-        } else {
-            let mut full_num = prefix + &number + &suffix;
-            while full_num.len() < width as usize {
-                full_num = " ".to_owned() + &full_num;
-            }
-            writer.push_str(&full_num);
-        }
-
+        self.write_numeric_parts(writer, prefix, &number, &suffix, true);
         Ok(())
     }
 
@@ -2268,6 +2214,44 @@ impl ConversionSpecifier {
             TimeFormat::CLower => Ok(dt.format("%a %b %d %H:%M:%S UTC %Y").to_string()),
         }
     }
+
+    fn write_numeric_parts(
+        &self,
+        writer: &mut String,
+        mut prefix: String,
+        number: &str,
+        suffix: &str,
+        zero_pad_allowed: bool,
+    ) {
+        // Handle padding
+        let NumericParam::Literal(width) = self.width else {
+            writer.push_str(&prefix);
+            writer.push_str(number);
+            writer.push_str(suffix);
+            return;
+        };
+
+        if self.left_adj {
+            let mut full_num = prefix + number + suffix;
+            while full_num.len() < width as usize {
+                full_num.push(' ');
+            }
+            writer.push_str(&full_num);
+        } else if self.zero_pad && zero_pad_allowed {
+            while prefix.len() + number.len() + suffix.len() < width as usize {
+                prefix.push('0');
+            }
+            writer.push_str(&prefix);
+            writer.push_str(number);
+            writer.push_str(suffix);
+        } else {
+            let mut full_num = prefix + number + suffix;
+            while full_num.len() < width as usize {
+                full_num = " ".to_owned() + &full_num;
+            }
+            writer.push_str(&full_num);
+        }
+    }
 }
 
 trait FloatFormattable: std::fmt::Display {
@@ -2942,4 +2926,52 @@ mod tests {
         );
         Ok(())
     }
+
+    #[test]
+    fn test_grouping_separator_ignore_zero_padding_for_float_nan() -> Result<()> {
+        test_scalar_function!(
+            FormatStringFunc::new(),
+            vec![
+                ColumnarValue::Scalar(ScalarValue::Utf8(Some("%010.2f".to_string()))),
+                ColumnarValue::Scalar(ScalarValue::Float64(Some(f64::NAN))),
+            ],
+            Ok(Some("       NaN")),
+            &str,
+            Utf8,
+            StringArray
+        );
+        Ok(())
+    }
+
+    #[test]
+    fn test_grouping_separator_ignore_zero_padding_for_float_inf() -> Result<()> {
+        test_scalar_function!(
+            FormatStringFunc::new(),
+            vec![
+                ColumnarValue::Scalar(ScalarValue::Utf8(Some("%010.2f".to_string()))),
+                ColumnarValue::Scalar(ScalarValue::Float64(Some(f64::INFINITY))),
+            ],
+            Ok(Some("  Infinity")),
+            &str,
+            Utf8,
+            StringArray
+        );
+        Ok(())
+    }
+
+    #[test]
+    fn test_grouping_separator_parentheses_zero_padding_decimal() -> Result<()> {
+        test_scalar_function!(
+            FormatStringFunc::new(),
+            vec![
+                ColumnarValue::Scalar(ScalarValue::Utf8(Some("%(0,15.2f".to_string()))),
+                ColumnarValue::Scalar(ScalarValue::Decimal128(Some(-123450), 2, 2)),
+            ],
+            Ok(Some("(000001,234.50)")),
+            &str,
+            Utf8,
+            StringArray
+        );
+        Ok(())
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org