Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor: improve zip kernel docs, add examples #6928

Merged
merged 2 commits into from
Jan 4, 2025
Merged

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Jan 1, 2025

Which issue does this PR close?

Rationale for this change

While reviewing apache/datafusion#13953 from @aweltsch I learned about the zip kernel but found its documentation somewhat confusing

https://docs.rs/arrow/latest/arrow/compute/kernels/zip/fn.zip.html

What changes are included in this PR?

Changes

  1. Add a worked example
  2. Improve wording

Are there any user-facing changes?

Docs improved. No functional changes
Screenshot 2025-01-01 at 8 53 11 AM

@alamb alamb added the documentation Improvements or additions to documentation label Jan 1, 2025
@alamb alamb changed the title Minor: improve zip kernel docs` Minor: improve zip kernel docs Jan 1, 2025
Copy link
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor comment 👍

On a side note, I noticed here that scalars have their length checked:

if truthy_is_scalar && truthy.len() != 1 {
return Err(ArrowError::InvalidArgumentError(
"scalar arrays must have 1 element".into(),
));
}

Which seems odd that this check is being done this 'late' so to speak; one would think being able to create a Scalar should enforce this invariant such that we don't have to check it for downstream kernels each time (though I think zip is the only one doing this anyway?)

Comment on lines 25 to 54
/// Zip two arrays by some boolean mask.
///
/// - Where `mask` is `true`, values of `truthy` are taken
/// - Where `mask` is `false` or `NULL`, values of `falsy` are taken
///
/// # Example
/// ```
/// # use std::sync::Arc;
/// # use arrow_array::{ArrayRef, BooleanArray, Int32Array};
/// # use arrow_select::zip::zip;
/// // mask: [true, true, false, NULL, true]
/// let mask = BooleanArray::from(vec![
/// Some(true), Some(true), Some(false), None, Some(true)
/// ]);
/// // truthy array: [1, NULL, 3, 4, 5]
/// let truthy = Int32Array::from(vec![
/// Some(1), None, Some(3), Some(4), Some(5)
/// ]);
/// // falsy array: [10, 20, 30, 40, 50]
/// let falsy = Int32Array::from(vec![
/// Some(10), Some(20), Some(30), Some(40), Some(50)
/// ]);
/// // zip with this mask select the first, second and last value from `truthy`
/// // and the third and fourth value from `falsy`
/// let result = zip(&mask, &truthy, &falsy).unwrap();
/// // Expected: [1, NULL, 30, 40, 5]
/// let expected: ArrayRef = Arc::new(Int32Array::from(vec![
/// Some(1), None, Some(30), Some(40), Some(5)
/// ]));
/// assert_eq!(&result, &expected);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be necessary to add details on how Scalars interact with this kernel, or is that unnecessary as that should be implied via knowledge of how Scalars behave in general?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will research this later likely tomorrow and respond

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be necessary to add details on how Scalars interact with this kernel, or is that unnecessary as that should be implied via knowledge of how Scalars behave in general?

I think in theory it should be implied but I also think it might not be obvious how it works / scalar is not as widely used / supported so I also added a new example use of scalars (to fill in certain elements with a constant)

Copy link
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Forgot to select Approve)

@github-actions github-actions bot added the arrow Changes to the arrow crate label Jan 4, 2025
@alamb alamb changed the title Minor: improve zip kernel docs Minor: improve zip kernel docs, add examples Jan 4, 2025
@alamb
Copy link
Contributor Author

alamb commented Jan 4, 2025

Thank you (very much!) for the review @Jefffrey ❤️

@alamb alamb merged commit 24a6bff into apache:main Jan 4, 2025
27 checks passed
@alamb
Copy link
Contributor Author

alamb commented Jan 4, 2025

Thanks again @Jefffrey -- I am also happy to add more examples, or updates as a follow on PR

@alamb alamb deleted the alamb/zip_docs branch January 4, 2025 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants