You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2026/01/30 09:46:35 UTC

[PR] perf: Optimize scalar path for chr function [datafusion]

kumarUjjawal opened a new pull request, #20073:
URL: https://github.com/apache/datafusion/pull/20073

   ## 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.
   -->
   
   - Part of https://github.com/apache/datafusion-comet/issues/2986.
   
   ## Rationale for this change
   
   `chr` currently routes scalar inputs through `make_scalar_function(chr, vec![])`, which performs a scalar → size-1 array → scalar roundtrip. This adds unnecessary overhead for constant folding / scalar evaluation.
   
   This PR adds a match-based scalar fast path and removes reliance on `make_scalar_function` for `chr`, while keeping the array behavior unchanged.
   
   <!--
    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.  
   -->
   
   ## What changes are included in this PR?
   
   - Refactor `ChrFunc::invoke_with_args` to:
   - Handle `ColumnarValue::Scalar(Int64)` directly (scalar fast path)
   - Handle `ColumnarValue::Array(Int64Array)` using the existing conversion logic
   - Add scalar benchmark to `benches/chr.rs` (`chr/scalar`) outside any size loop
   
   | Type | Before | After | Speedup |
   |------|--------|-------|---------|
   | `chr/scalar` | 342.05 ns | 87.339 ns | **3.92x** |
   
   <!--
   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.
   -->
   
   ## Are these changes tested?
   
   Yes
   
   <!--
   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)?
   -->
   
   ## Are there any user-facing changes?
   
   No
   
   <!--
   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.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] perf: Optimize scalar path for chr function [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #20073:
URL: https://github.com/apache/datafusion/pull/20073#discussion_r2749083598


##########
datafusion/functions/src/string/chr.rs:
##########
@@ -119,7 +119,47 @@ impl ScalarUDFImpl for ChrFunc {
     }
 
     fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
-        make_scalar_function(chr, vec![])(&args.args)
+        let return_type = args.return_field.data_type();
+        let [arg] = take_function_args(self.name(), args.args)?;
+
+        match arg {

Review Comment:
   Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] perf: Optimize scalar path for chr function [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #20073:
URL: https://github.com/apache/datafusion/pull/20073#discussion_r2745874658


##########
datafusion/functions/src/string/chr.rs:
##########
@@ -119,7 +119,47 @@ impl ScalarUDFImpl for ChrFunc {
     }
 
     fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
-        make_scalar_function(chr, vec![])(&args.args)
+        let return_type = args.return_field.data_type();
+        let [arg] = take_function_args(self.name(), args.args)?;
+
+        match arg {

Review Comment:
   ```rust
   match arg {
       ColumnarValue::Scalar(ScalarValue::Int64(Some(code_point))) => {
           if let Ok(u) = u32::try_from(code_point)
               && let Some(c) = core::char::from_u32(u)
           {
               Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(
                      c.to_string(),
                   ))))
               } else {
                   exec_err!("invalid Unicode scalar value: {code_point}")
               }
       }
       ColumnarValue::Scalar(ScalarValue::Int64(None)) => {
           Ok(ColumnarValue::Scalar(ScalarValue::Utf8(None)))
       }
       ColumnarValue::Array(array) => {
           Ok(ColumnarValue::Array(chr(&[array])?))
       }
       _ => internal_err!("..."),
   }
   ```
   
   - Easier to fold into match like this since we accept only one datatype; also unnecessary to double check the array datatype
   - Also worth looking into refactoring `chr()` function; no reason to pass in a slice when we can just pass the array



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] perf: Optimize scalar path for chr function [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #20073:
URL: https://github.com/apache/datafusion/pull/20073#issuecomment-3823003609

   <img width="989" height="170" alt="image" src="https://github.com/user-attachments/assets/de5a9ced-fa1f-4335-a6c7-f94ed57bcc07" />
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] perf: Optimize scalar path for chr function [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #20073:
URL: https://github.com/apache/datafusion/pull/20073#issuecomment-3827673434

   > LGTM!
   > 
   > It would be good to add some SLT tests though. There are only array based unit tests and there are no SLT tests. The Spark ones are empty - https://github.com/apache/datafusion/blob/f0de02fd664afcc4aad61fd8d13503533ed1e8d5/datafusion/sqllogictest/test_files/spark/string/chr.slt
   
   Thank you for the feedback.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] perf: Optimize scalar path for chr function [datafusion]

Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #20073:
URL: https://github.com/apache/datafusion/pull/20073#issuecomment-3823005692

   > <img alt="image" width="989" height="170" src="https://private-user-images.githubusercontent.com/10569915/542771641-de5a9ced-fa1f-4335-a6c7-f94ed57bcc07.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Njk3NjkzMzQsIm5iZiI6MTc2OTc2OTAzNCwicGF0aCI6Ii8xMDU2OTkxNS81NDI3NzE2NDEtZGU1YTljZWQtZmExZi00MzM1LWE2YzctZjk0ZWQ1N2JjYzA3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjAxMzAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwMTMwVDEwMzAzNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTYyMTYzNGJlYWUxMjg5OTlkYjlhMTI3MGUzZDkzZWQ4NTczODI1MTkyMmVmZGM4MGU5Mjg0MTE0OTZhYjc5NjUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.pGzLYba4o3yRakceZGH-_HejFnYPreGDAQbuuVZhlTA">
   
   Getting this in CI


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] perf: Optimize scalar path for chr function [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #20073:
URL: https://github.com/apache/datafusion/pull/20073#issuecomment-3830047498

   Thanks @kumarUjjawal & @martin-g 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] perf: Optimize scalar path for chr function [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #20073:
URL: https://github.com/apache/datafusion/pull/20073


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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