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

arrow-ord: add support for nested types to partition #7131

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions arrow-ord/src/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use std::ops::Range;

use arrow_array::{Array, ArrayRef};
use arrow_buffer::BooleanBuffer;
use arrow_schema::ArrowError;
use arrow_schema::{ArrowError, SortOptions};

use crate::cmp::distinct;
use crate::ord::make_comparator;

/// A computed set of partitions, see [`partition`]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -156,18 +157,24 @@ fn find_boundaries(v: &dyn Array) -> Result<BooleanBuffer, ArrowError> {
let slice_len = v.len() - 1;
let v1 = v.slice(0, slice_len);
let v2 = v.slice(1, slice_len);
Ok(distinct(&v1, &v2)?.values().clone())

if !v.data_type().is_nested() {
return Ok(distinct(&v1, &v2)?.values().clone());
}
// Given that we're only comparing values, null ordering in the input or
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if using eq would be faster 🤔

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean for non-nested types? eq doesn't support nested types similarly to distinct and given they both shell out to compare_op I don't think there should be much of a perf difference between distinct and eq + mapping nulls to booleans (which would be necessary).

// sort options do not matter.
let cmp = make_comparator(&v1, &v2, SortOptions::default())?;
Ok((0..slice_len).map(|i| !cmp(i, i).is_eq()).collect())
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use super::*;
use arrow_array::*;
use arrow_schema::DataType;

use super::*;

#[test]
fn test_partition_empty() {
let err = partition(&[]).unwrap_err();
Expand Down Expand Up @@ -298,4 +305,31 @@ mod tests {
vec![(0..1), (1..2), (2..4), (4..5), (5..7), (7..8), (8..9)],
);
}

#[test]
fn test_partition_nested() {
let input = vec![
Arc::new(
StructArray::try_from(vec![(
"f1",
Arc::new(Int64Array::from(vec![
None,
None,
Some(1),
Some(2),
Some(2),
Some(2),
Some(3),
Some(4),
])) as _,
)])
.unwrap(),
) as _,
Arc::new(Int64Array::from(vec![1, 1, 1, 2, 3, 3, 3, 4])) as _,
];
assert_eq!(
partition(&input).unwrap().ranges(),
vec![0..2, 2..3, 3..4, 4..6, 6..7, 7..8]
)
}
}
Loading