You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "tlm365 (via GitHub)" <gi...@apache.org> on 2024/12/13 04:13:26 UTC

[PR] Support unicode character for `initcap` function [datafusion]

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

   ## 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 #13711.
   
   ## 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.  
   -->
   
   ## What changes are included in this PR?
   - Support UTF-8 for `initcap` function.
   - Moved `initcap` from `string` module to `unicode` module.
   - Update docs.
   
   <!--
   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, unit tests are included.
   <!--
   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?
   Yes. `api change`.
   <!--
   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] Support unicode character for `initcap` function [datafusion]

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


##########
datafusion/functions/src/unicode/initcap.rs:
##########
@@ -74,7 +76,7 @@ impl ScalarUDFImpl for InitcapFunc {
             DataType::LargeUtf8 => make_scalar_function(initcap::<i64>, vec![])(args),
             DataType::Utf8View => make_scalar_function(initcap_utf8view, vec![])(args),
             other => {
-                exec_err!("Unsupported data type {other:?} for function initcap")
+                exec_err!("Unsupported data type {other:?} for function `initcap`")

Review Comment:
   ```suggestion
                   internal_err!("Unsupported data type {other:?} for function `initcap`")
   ```
   Incompatible arg should be checked during planning before, thus here is unreachable, and we can use `internal_err` to indicate a potential bug if it's executed



##########
datafusion/sqllogictest/test_files/string/string_query.slt.part:
##########
@@ -460,7 +460,7 @@ Andrew Datafusion📊🔥
 Xiangpeng Datafusion数据融合
 Raphael Datafusionдатафусион
 Under_Score Un Iść Core
-Percent Pan Tadeusz Ma Iść W KąT
+Percent Pan Tadeusz Ma Iść W Kąt

Review Comment:
   I've checked and this result is compatible with PostgreSQL



-- 
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] Support unicode character for `initcap` function [datafusion]

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


##########
datafusion/functions/src/unicode/initcap.rs:
##########
@@ -213,7 +243,7 @@ mod tests {
             Ok(Some("Hi Thomas")),
             &str,
             Utf8,
-            StringArray
+            StringViewArray

Review Comment:
   👍 



##########
datafusion/functions/src/unicode/initcap.rs:
##########
@@ -123,24 +124,36 @@ fn get_initcap_doc() -> &'static Documentation {
 fn initcap<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
     let string_array = as_generic_string_array::<T>(&args[0])?;
 
-    // first map is the iterator, second is for the `Option<_>`
-    let result = string_array
-        .iter()
-        .map(initcap_string)
-        .collect::<GenericStringArray<T>>();
+    let mut builder = GenericStringBuilder::<T>::with_capacity(
+        string_array.len(),
+        string_array.value_data().len(),
+    );
 
-    Ok(Arc::new(result) as ArrayRef)
+    string_array.iter().for_each(|str| match str {
+        Some(s) => {
+            let initcap_str = initcap_string(Some(s)).unwrap();
+            builder.append_value(initcap_str);
+        }
+        None => builder.append_null(),
+    });
+
+    Ok(Arc::new(builder.finish()) as ArrayRef)
 }
 
 fn initcap_utf8view(args: &[ArrayRef]) -> Result<ArrayRef> {
     let string_view_array = as_string_view_array(&args[0])?;
 
-    let result = string_view_array
-        .iter()
-        .map(initcap_string)
-        .collect::<StringArray>();
+    let mut builder = StringViewBuilder::with_capacity(string_view_array.len());
+
+    string_view_array.iter().for_each(|str| match str {
+        Some(s) => {
+            let initcap_str = initcap_string(Some(s)).unwrap();
+            builder.append_value(initcap_str);
+        }
+        None => builder.append_null(),
+    });
 
-    Ok(Arc::new(result) as ArrayRef)
+    Ok(Arc::new(builder.finish()) as ArrayRef)
 }
 
 fn initcap_string(input: Option<&str>) -> Option<String> {

Review Comment:
   ```suggestion
   fn initcap_string(input: &str) -> String {
   ```
   It's only used by L135 and L150 and never be `None`. I think we can remove the `Option`.



##########
datafusion/functions/src/unicode/initcap.rs:
##########
@@ -149,13 +162,16 @@ fn initcap_string(input: Option<&str>) -> Option<String> {
         let mut prev_is_alphanumeric = false;
 
         for c in s.chars() {
-            let transformed = if prev_is_alphanumeric {
-                c.to_ascii_lowercase()
+            if prev_is_alphanumeric {
+                for lc in c.to_lowercase() {
+                    result.push(lc);
+                }
             } else {
-                c.to_ascii_uppercase()
-            };
-            result.push(transformed);
-            prev_is_alphanumeric = c.is_ascii_alphanumeric();
+                for uc in c.to_uppercase() {
+                    result.push(uc);
+                }
+            }

Review Comment:
   Does this change slow down the ASCII-only case? If yes, maybe we can consider keeping a faster path for ASCII-only cases. (like https://github.com/apache/datafusion/issues/12306)



-- 
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] Support unicode character for `initcap` function [datafusion]

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

   Thanks @tlm365 and @2010YOUY01 for reviewing


-- 
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] Support unicode character for `initcap` function [datafusion]

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


##########
datafusion/functions/src/unicode/initcap.rs:
##########
@@ -74,7 +76,7 @@ impl ScalarUDFImpl for InitcapFunc {
             DataType::LargeUtf8 => make_scalar_function(initcap::<i64>, vec![])(args),
             DataType::Utf8View => make_scalar_function(initcap_utf8view, vec![])(args),
             other => {
-                exec_err!("Unsupported data type {other:?} for function initcap")
+                exec_err!("Unsupported data type {other:?} for function `initcap`")

Review Comment:
   @2010YOUY01 Thanks for reviewing,
   
   > Incompatible arg should be checked during planning before, thus here is unreachable, and we can use internal_err to indicate a potential bug if it's executed
   
   Make sense to me👍



-- 
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] Support unicode character for `initcap` function [datafusion]

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


##########
datafusion/functions/src/unicode/initcap.rs:
##########
@@ -149,13 +162,16 @@ fn initcap_string(input: Option<&str>) -> Option<String> {
         let mut prev_is_alphanumeric = false;
 
         for c in s.chars() {
-            let transformed = if prev_is_alphanumeric {
-                c.to_ascii_lowercase()
+            if prev_is_alphanumeric {
+                for lc in c.to_lowercase() {
+                    result.push(lc);
+                }
             } else {
-                c.to_ascii_uppercase()
-            };
-            result.push(transformed);
-            prev_is_alphanumeric = c.is_ascii_alphanumeric();
+                for uc in c.to_uppercase() {
+                    result.push(uc);
+                }
+            }

Review Comment:
   @goldmedal thanks for reviewing
   
   > Does this change slow down the ASCII-only case? If yes, maybe we can consider keeping a faster path for ASCII-only cases. (like https://github.com/apache/datafusion/issues/12306)
   
   Seems like keeping a fast path for ASCII-only cases is beneficial. I will check and update it soon.



-- 
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] Support unicode character for `initcap` function [datafusion]

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


-- 
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