You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/01/24 07:42:19 UTC

[GitHub] [arrow-datafusion] ovr opened a new pull request #1660: fix: substr - correct behaivour with negative start pos

ovr opened a new pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660


   # Which issue does this PR close?
   
   In https://github.com/apache/arrow-datafusion/pull/1621 PR, @xudong963 found an incorrect behavior in `substr` function with negative starting position.
   
   Closes #.
   
   # Rationale for this change
   
   Use the same behavior as PostgreSQL provides.
   
   # What changes are included in this PR?
   
   Fix
   
   # Are there any user-facing changes?
   
   It's a fix, not a breaking change, because DF uses PostgreSQL dialect.


-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] ovr commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
ovr commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r790733421



##########
File path: datafusion/src/physical_plan/functions.rs
##########
@@ -3680,6 +3704,60 @@ mod tests {
             StringArray
         );
         #[cfg(feature = "unicode_expressions")]
+        test_function!(

Review comment:
       You are right, i did another iteration and looks like I found a way how to get similar behaviour
   
   PG:
   
   
   ```
   select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
   
   [
     {
       "s1": "alph",
       "s2": "alph",
       "s3": ""
     }
   ]
   ```
   
   DF:
   
   ```
   ❯ select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
   +------+------+----+
   | s1   | s2   | s3 |
   +------+------+----+
   | alph | alph |    |
   +------+------+----+
   1 row in set. Query took 0.016 seconds.
   
   ```




-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] xudong963 commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
xudong963 commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r790493092



##########
File path: datafusion/src/physical_plan/functions.rs
##########
@@ -3680,6 +3704,60 @@ mod tests {
             StringArray
         );
         #[cfg(feature = "unicode_expressions")]
+        test_function!(

Review comment:
       
   ```sql
   postgres=# select a from t;
       a
   ----------
    alphabet
   (1 rows)
   
   postgres=# select substring(a, 0, 5) from t;
    substring
   -----------
    alph
   (1 rows)
   
   ```

##########
File path: datafusion/src/physical_plan/functions.rs
##########
@@ -3582,6 +3582,30 @@ mod tests {
             StringArray
         );
         #[cfg(feature = "unicode_expressions")]
+        test_function!(

Review comment:
       duplicate with 3561~3571




-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] alamb merged pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
alamb merged pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660


   


-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] Jimexist commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
Jimexist commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r791868982



##########
File path: datafusion/src/physical_plan/unicode_expressions.rs
##########
@@ -457,21 +457,29 @@ pub fn substr<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
                                 start,
                                 count
                             )))
-                        } else if start <= 0 {
-                            Ok(Some(string.to_string()))
                         } else {
                             let graphemes = string.graphemes(true).collect::<Vec<&str>>();
-                            let start_pos = start as usize - 1;
-                            let count_usize = count as usize;
-                            if graphemes.len() < start_pos {
+                            let (start_pos, end_pos) = if start <= 0 {
+                                let end_pos = start + count - 1;
+                                (
+                                    0_usize,
+                                    if end_pos < 0 {
+                                        // we use 0 as workaround for usize to return empty string
+                                        0
+                                    } else {
+                                        end_pos as usize
+                                    },

Review comment:
       ```suggestion
                                       0_usize.max(end_pos as usize),
   ```




-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] ovr commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
ovr commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r790733421



##########
File path: datafusion/src/physical_plan/functions.rs
##########
@@ -3680,6 +3704,60 @@ mod tests {
             StringArray
         );
         #[cfg(feature = "unicode_expressions")]
+        test_function!(

Review comment:
       You are right, i did another iteration and looks like I found a way how to get similar behaviour
   
   PG:
   
   
   ```
   select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
   | s1.    | s2     | s3   |
   | :---- | :---- | :--- |
   | alph  | alph |         |
   ```
   
   DF:
   
   ```
   ❯ select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
   +------+------+----+
   | s1   | s2   | s3 |
   +------+------+----+
   | alph | alph |    |
   +------+------+----+
   1 row in set. Query took 0.016 seconds.
   
   ```
   
   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@arrow.apache.org

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



[GitHub] [arrow-datafusion] ovr commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
ovr commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r790733421



##########
File path: datafusion/src/physical_plan/functions.rs
##########
@@ -3680,6 +3704,60 @@ mod tests {
             StringArray
         );
         #[cfg(feature = "unicode_expressions")]
+        test_function!(

Review comment:
       You are right, i did another iteration and looks like I found a way how to get similar behaviour
   
   PG:
   
   
   ```
   select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
   | s1 | s2 | s3 |
   | :--- | :--- | :--- |
   | alph | alph |  |
   ```
   
   DF:
   
   ```
   ❯ select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
   +------+------+----+
   | s1   | s2   | s3 |
   +------+------+----+
   | alph | alph |    |
   +------+------+----+
   1 row in set. Query took 0.016 seconds.
   
   ```
   
   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@arrow.apache.org

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



[GitHub] [arrow-datafusion] ovr commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
ovr commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r796034327



##########
File path: datafusion/src/physical_plan/unicode_expressions.rs
##########
@@ -457,21 +457,29 @@ pub fn substr<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
                                 start,
                                 count
                             )))
-                        } else if start <= 0 {
-                            Ok(Some(string.to_string()))
                         } else {
                             let graphemes = string.graphemes(true).collect::<Vec<&str>>();
-                            let start_pos = start as usize - 1;
-                            let count_usize = count as usize;
-                            if graphemes.len() < start_pos {
+                            let (start_pos, end_pos) = if start <= 0 {
+                                let end_pos = start + count - 1;
+                                (
+                                    0_usize,
+                                    if end_pos < 0 {
+                                        // we use 0 as workaround for usize to return empty string
+                                        0
+                                    } else {
+                                        end_pos as usize
+                                    },

Review comment:
       @Jimexist It doesn't work because it doesn't reflect comparing with less than 0. `-1 (i32)` with `as usize` will produce a maximum number that can be stored in `usize`.
   
   ![image](https://user-images.githubusercontent.com/572096/151866702-28829160-8521-4129-a816-668196d4564a.png)
   
   @alamb It doesnt work.




-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] Jimexist commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
Jimexist commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r791868982



##########
File path: datafusion/src/physical_plan/unicode_expressions.rs
##########
@@ -457,21 +457,29 @@ pub fn substr<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
                                 start,
                                 count
                             )))
-                        } else if start <= 0 {
-                            Ok(Some(string.to_string()))
                         } else {
                             let graphemes = string.graphemes(true).collect::<Vec<&str>>();
-                            let start_pos = start as usize - 1;
-                            let count_usize = count as usize;
-                            if graphemes.len() < start_pos {
+                            let (start_pos, end_pos) = if start <= 0 {
+                                let end_pos = start + count - 1;
+                                (
+                                    0_usize,
+                                    if end_pos < 0 {
+                                        // we use 0 as workaround for usize to return empty string
+                                        0
+                                    } else {
+                                        end_pos as usize
+                                    },

Review comment:
       ```suggestion
                                       0_usize.max(end_pos as usize),
   ```




-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] xudong963 commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
xudong963 commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r790493746



##########
File path: datafusion/src/physical_plan/functions.rs
##########
@@ -3680,6 +3704,60 @@ mod tests {
             StringArray
         );
         #[cfg(feature = "unicode_expressions")]
+        test_function!(

Review comment:
       Hi @ovr, Sorry, I think there are still some problems.




-- 
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@arrow.apache.org

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



[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1660: fix: substr - correct behaivour with negative start pos

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1660:
URL: https://github.com/apache/arrow-datafusion/pull/1660#discussion_r796026830



##########
File path: datafusion/src/physical_plan/unicode_expressions.rs
##########
@@ -457,21 +457,29 @@ pub fn substr<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
                                 start,
                                 count
                             )))
-                        } else if start <= 0 {
-                            Ok(Some(string.to_string()))
                         } else {
                             let graphemes = string.graphemes(true).collect::<Vec<&str>>();
-                            let start_pos = start as usize - 1;
-                            let count_usize = count as usize;
-                            if graphemes.len() < start_pos {
+                            let (start_pos, end_pos) = if start <= 0 {
+                                let end_pos = start + count - 1;
+                                (
+                                    0_usize,
+                                    if end_pos < 0 {
+                                        // we use 0 as workaround for usize to return empty string
+                                        0
+                                    } else {
+                                        end_pos as usize
+                                    },

Review comment:
       @ovr  would you like to make this change so we can merge the PR? I can find some time to do so too if that would help.




-- 
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@arrow.apache.org

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