You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jayzhan211 (via GitHub)" <gi...@apache.org> on 2024/02/23 12:28:19 UTC

[PR] `eq` for struct [arrow-rs]

jayzhan211 opened a new pull request, #5423:
URL: https://github.com/apache/arrow-rs/pull/5423

   # 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 #.
   
   # 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?
   
   <!--
   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 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 `breaking 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@arrow.apache.org

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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "my-vegetable-has-exploded (via GitHub)" <gi...@apache.org>.
my-vegetable-has-exploded commented on PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#issuecomment-1998835193

   may also closes #5199 & #5217 ?


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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#discussion_r1501284691


##########
arrow-ord/src/cmp.rs:
##########
@@ -198,7 +198,38 @@ fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray,
     let r = r_v.map(|x| x.values().as_ref()).unwrap_or(r);
     let r_t = r.data_type();
 
-    if l_t != r_t || l_t.is_nested() {
+    if l_t != r_t {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Invalid comparison operation: {l_t} {op} {r_t}"
+        )));
+    }
+
+    if let Struct(_) = l_t {
+        let l = l.as_struct();
+        let r = r.as_struct();
+        assert_eq!(l.num_columns(), r.num_columns());
+        match op {
+            Op::Equal => {
+                let mut res = vec![true; len];
+                for i in 0..l.num_columns() {
+                    let col_l = l.column(i);
+                    let col_r = r.column(i);
+                    let eq_rows = compare_op(op, col_l, col_r)?;
+                    for (j, item) in res.iter_mut().enumerate().take(len) {

Review Comment:
   It will be faster to compute the bitwise and of the underlying buffers, than to do this element wise
   
   Something like
   
   ```
   let nulls = NullBuffer::union(l.nulls(), r.nulls());
   let vals = l.values () & r.values();
   BooleanArray::new(vals, nulls)
   ```
   
   In combination with `Iterator::fold`



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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#issuecomment-1962218897

   See https://github.com/apache/arrow-rs/pull/5408#issuecomment-1962218269 I think we should probably start by adding support to DynComparator. In the case of StructArray there is likely still merit to having a specialized impl as in this PR as it will be significantly faster, but I think we will always need the fallback of DynComparator in order to support ListArray and friends.


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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#discussion_r1501284691


##########
arrow-ord/src/cmp.rs:
##########
@@ -198,7 +198,38 @@ fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray,
     let r = r_v.map(|x| x.values().as_ref()).unwrap_or(r);
     let r_t = r.data_type();
 
-    if l_t != r_t || l_t.is_nested() {
+    if l_t != r_t {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Invalid comparison operation: {l_t} {op} {r_t}"
+        )));
+    }
+
+    if let Struct(_) = l_t {
+        let l = l.as_struct();
+        let r = r.as_struct();
+        assert_eq!(l.num_columns(), r.num_columns());
+        match op {
+            Op::Equal => {
+                let mut res = vec![true; len];
+                for i in 0..l.num_columns() {
+                    let col_l = l.column(i);
+                    let col_r = r.column(i);
+                    let eq_rows = compare_op(op, col_l, col_r)?;
+                    for (j, item) in res.iter_mut().enumerate().take(len) {

Review Comment:
   It will be faster to compute the bitwise and of the underlying buffers, than to do this element wise
   
   Something like
   
   ```
   let nulls = NullBuffer::union(l.nulls(), r.nulls());
   let vals = l.values () & r.values();
   BooleanArray::new(vals, nulls)
   ```



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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#discussion_r1501306029


##########
arrow-ord/src/cmp.rs:
##########
@@ -210,17 +210,19 @@ fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray,
         assert_eq!(l.num_columns(), r.num_columns());
         match op {
             Op::Equal => {
-                let mut res = BooleanArray::from(vec![true; len]);
-                for i in 0..l.num_columns() {
-                    let col_l = l.column(i);
-                    let col_r = r.column(i);
-                    let eq_rows = compare_op(op, col_l, col_r)?;
-
-                    let nulls = NullBuffer::union(res.nulls(), eq_rows.nulls());
-                    let vals = res.values() & eq_rows.values();
-                    res = BooleanArray::new(vals, nulls);
-                }
-                return Ok(res);
+                return (0..l.num_columns()).fold(
+                    Ok(BooleanArray::from(vec![true; len])),

Review Comment:
   ```suggestion
                       Ok(BooleanArray::new(BooleanBuffer::new_set(len), None)),
   ```
   
   This will be significantly faster



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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 closed pull request #5423: `eq` for struct
URL: https://github.com/apache/arrow-rs/pull/5423


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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#discussion_r1501310961


##########
arrow-ord/src/cmp.rs:
##########
@@ -198,7 +198,43 @@ fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray,
     let r = r_v.map(|x| x.values().as_ref()).unwrap_or(r);
     let r_t = r.data_type();
 
-    if l_t != r_t || l_t.is_nested() {
+    if l_t != r_t {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Invalid comparison operation: {l_t} {op} {r_t}"
+        )));
+    }
+
+    if let Struct(_) = l_t {
+        let l = l.as_struct();
+        let r = r.as_struct();
+        assert_eq!(l.num_columns(), r.num_columns());
+
+        #[allow(clippy::manual_try_fold)]
+        match op {
+            Op::Equal => {
+                return (0..l.num_columns()).fold(
+                    Ok(BooleanArray::new(BooleanBuffer::new_set(len), None)),
+                    |res, i| {
+                        let res = res?;
+                        let col_l = l.column(i);
+                        let col_r = r.column(i);
+                        let eq_rows = compare_op(op, col_l, col_r)?;
+
+                        let nulls = NullBuffer::union(res.nulls(), eq_rows.nulls());
+                        let vals = res.values() & eq_rows.values();
+                        Ok(BooleanArray::new(vals, nulls))
+                    },
+                )

Review Comment:
   ```suggestion
                   return (0..l.num_columns()).try_fold(
                       BooleanArray::new(BooleanBuffer::new_set(len), None),
                       |res, i| {
                           let col_l = l.column(i);
                           let col_r = r.column(i);
                           let eq_rows = compare_op(op, col_l, col_r)?;
   
                           let nulls = NullBuffer::union(res.nulls(), eq_rows.nulls());
                           let vals = res.values() & eq_rows.values();
                           Ok(BooleanArray::new(vals, nulls))
                       },
                   )
   ```



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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#discussion_r1501312449


##########
arrow-ord/src/cmp.rs:
##########
@@ -198,7 +198,43 @@ fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray,
     let r = r_v.map(|x| x.values().as_ref()).unwrap_or(r);
     let r_t = r.data_type();
 
-    if l_t != r_t || l_t.is_nested() {
+    if l_t != r_t {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Invalid comparison operation: {l_t} {op} {r_t}"
+        )));
+    }
+
+    if let Struct(_) = l_t {
+        let l = l.as_struct();
+        let r = r.as_struct();
+        assert_eq!(l.num_columns(), r.num_columns());
+
+        #[allow(clippy::manual_try_fold)]
+        match op {
+            Op::Equal => {
+                return (0..l.num_columns()).fold(
+                    Ok(BooleanArray::new(BooleanBuffer::new_set(len), None)),
+                    |res, i| {
+                        let res = res?;
+                        let col_l = l.column(i);
+                        let col_r = r.column(i);
+                        let eq_rows = compare_op(op, col_l, col_r)?;
+
+                        let nulls = NullBuffer::union(res.nulls(), eq_rows.nulls());
+                        let vals = res.values() & eq_rows.values();
+                        Ok(BooleanArray::new(vals, nulls))
+                    },
+                )

Review Comment:
   Oh, I did not return with Result `Ok(BooleanArray::new(vals, nulls))`, but `BooleanArray::new(vals, nulls))`



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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#discussion_r1501308440


##########
arrow-ord/src/cmp.rs:
##########
@@ -198,7 +198,38 @@ fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray,
     let r = r_v.map(|x| x.values().as_ref()).unwrap_or(r);
     let r_t = r.data_type();
 
-    if l_t != r_t || l_t.is_nested() {
+    if l_t != r_t {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Invalid comparison operation: {l_t} {op} {r_t}"
+        )));
+    }
+
+    if let Struct(_) = l_t {
+        let l = l.as_struct();
+        let r = r.as_struct();
+        assert_eq!(l.num_columns(), r.num_columns());
+        match op {
+            Op::Equal => {
+                let mut res = vec![true; len];
+                for i in 0..l.num_columns() {
+                    let col_l = l.column(i);
+                    let col_r = r.column(i);
+                    let eq_rows = compare_op(op, col_l, col_r)?;
+                    for (j, item) in res.iter_mut().enumerate().take(len) {

Review Comment:
   clippy told me to use try_fold instead, but it is so difficult to work with try_fold.



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


Re: [PR] `eq` for struct [arrow-rs]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #5423:
URL: https://github.com/apache/arrow-rs/pull/5423#issuecomment-1998894638

   > may also closes #5199 & #5217 ?
   
   yes, I didn't notice there is existing issue.


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