-
Notifications
You must be signed in to change notification settings - Fork 807
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
Push SortOptions into DynComparator Allowing Nested Comparisons (#5426) #5792
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10ce321
Push SortOptions into DynComparator (#5426)
tustvold caa891e
Clippy
tustvold fc7956d
Merge remote-tracking branch 'upstream/master' into sort-options-comp…
tustvold e7b32c8
Review feedback
tustvold 90bd86e
Tweak print_row
tustvold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,6 +259,8 @@ pub fn build_compare(left: &dyn Array, right: &dyn Array) -> Result<DynComparato | |
/// Returns a comparison function that compares two values at two different positions | ||
/// between the two arrays. | ||
/// | ||
/// For comparing arrays element-wise, see also the vectorised kernels in [`crate::cmp`]. | ||
/// | ||
/// If `nulls_first` is true `NULL` values will be considered less than any non-null value, | ||
/// otherwise they will be considered greater. | ||
/// | ||
|
@@ -289,10 +291,21 @@ pub fn build_compare(left: &dyn Array, right: &dyn Array) -> Result<DynComparato | |
/// | ||
/// # Postgres-compatible Nested Comparison | ||
/// | ||
/// Whilst SQL prescribes ternary logic for nulls, that is comparing a value against a null yields | ||
/// a NULL, many systems, including postgres, instead apply a total ordering to comparison | ||
/// of nested nulls. That is nulls within nested types are either greater than any value, | ||
/// or less than any value (Spark). This could be implemented as below | ||
/// Whilst SQL prescribes ternary logic for nulls, that is comparing a value against a NULL yields | ||
/// a NULL, many systems, including postgres, instead apply a total ordering to comparison of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
/// nested nulls. That is nulls within nested types are either greater than any value (postgres), | ||
/// or less than any value (Spark). | ||
/// | ||
/// In particular | ||
/// | ||
/// ```ignore | ||
/// { a: 1, b: null } == { a: 1, b: null } => true | ||
/// { a: 1, b: null } == { a: 1, b: 1 } => false | ||
/// { a: 1, b: null } == null => null | ||
/// null == null => null | ||
/// ``` | ||
/// | ||
/// This could be implemented as below | ||
/// | ||
/// ``` | ||
/// # use arrow_array::{Array, BooleanArray}; | ||
|
@@ -366,7 +379,9 @@ pub fn make_comparator( | |
#[cfg(test)] | ||
pub mod tests { | ||
use super::*; | ||
use arrow_array::builder::{Int32Builder, ListBuilder}; | ||
use arrow_buffer::{i256, IntervalDayTime, OffsetBuffer}; | ||
use arrow_schema::{DataType, Field, Fields}; | ||
use half::f16; | ||
use std::sync::Arc; | ||
|
||
|
@@ -736,4 +751,157 @@ pub mod tests { | |
test_bytes_impl::<BinaryType>(); | ||
test_bytes_impl::<LargeBinaryType>(); | ||
} | ||
|
||
#[test] | ||
fn test_lists() { | ||
let mut a = ListBuilder::new(ListBuilder::new(Int32Builder::new())); | ||
a.extend([ | ||
Some(vec![Some(vec![Some(1), Some(2), None]), Some(vec![None])]), | ||
Some(vec![ | ||
Some(vec![Some(1), Some(2), Some(3)]), | ||
Some(vec![Some(1)]), | ||
]), | ||
Some(vec![]), | ||
]); | ||
let a = a.finish(); | ||
let mut b = ListBuilder::new(ListBuilder::new(Int32Builder::new())); | ||
b.extend([ | ||
Some(vec![Some(vec![Some(1), Some(2), None]), Some(vec![None])]), | ||
Some(vec![ | ||
Some(vec![Some(1), Some(2), None]), | ||
Some(vec![Some(1)]), | ||
]), | ||
Some(vec![ | ||
Some(vec![Some(1), Some(2), Some(3), Some(4)]), | ||
Some(vec![Some(1)]), | ||
]), | ||
None, | ||
]); | ||
let b = b.finish(); | ||
|
||
let opts = SortOptions { | ||
descending: false, | ||
nulls_first: true, | ||
}; | ||
let cmp = make_comparator(&a, &b, opts).unwrap(); | ||
assert_eq!(cmp(0, 0), Ordering::Equal); | ||
assert_eq!(cmp(0, 1), Ordering::Less); | ||
assert_eq!(cmp(0, 2), Ordering::Less); | ||
assert_eq!(cmp(1, 2), Ordering::Less); | ||
assert_eq!(cmp(1, 3), Ordering::Greater); | ||
assert_eq!(cmp(2, 0), Ordering::Less); | ||
|
||
let opts = SortOptions { | ||
descending: true, | ||
nulls_first: true, | ||
}; | ||
let cmp = make_comparator(&a, &b, opts).unwrap(); | ||
assert_eq!(cmp(0, 0), Ordering::Equal); | ||
assert_eq!(cmp(0, 1), Ordering::Less); | ||
assert_eq!(cmp(0, 2), Ordering::Less); | ||
assert_eq!(cmp(1, 2), Ordering::Greater); | ||
assert_eq!(cmp(1, 3), Ordering::Greater); | ||
assert_eq!(cmp(2, 0), Ordering::Greater); | ||
|
||
let opts = SortOptions { | ||
descending: true, | ||
nulls_first: false, | ||
}; | ||
let cmp = make_comparator(&a, &b, opts).unwrap(); | ||
assert_eq!(cmp(0, 0), Ordering::Equal); | ||
assert_eq!(cmp(0, 1), Ordering::Greater); | ||
assert_eq!(cmp(0, 2), Ordering::Greater); | ||
assert_eq!(cmp(1, 2), Ordering::Greater); | ||
assert_eq!(cmp(1, 3), Ordering::Less); | ||
assert_eq!(cmp(2, 0), Ordering::Greater); | ||
|
||
let opts = SortOptions { | ||
descending: false, | ||
nulls_first: false, | ||
}; | ||
let cmp = make_comparator(&a, &b, opts).unwrap(); | ||
assert_eq!(cmp(0, 0), Ordering::Equal); | ||
assert_eq!(cmp(0, 1), Ordering::Greater); | ||
assert_eq!(cmp(0, 2), Ordering::Greater); | ||
assert_eq!(cmp(1, 2), Ordering::Less); | ||
assert_eq!(cmp(1, 3), Ordering::Less); | ||
assert_eq!(cmp(2, 0), Ordering::Less); | ||
} | ||
|
||
#[test] | ||
fn test_struct() { | ||
let fields = Fields::from(vec![ | ||
Field::new("a", DataType::Int32, true), | ||
Field::new_list("b", Field::new("item", DataType::Int32, true), true), | ||
]); | ||
|
||
let a = Int32Array::from(vec![Some(1), Some(2), None, None]); | ||
let mut b = ListBuilder::new(Int32Builder::new()); | ||
b.extend([Some(vec![Some(1), Some(2)]), Some(vec![None]), None, None]); | ||
let b = b.finish(); | ||
|
||
let nulls = Some(NullBuffer::from_iter([true, true, true, false])); | ||
let values = vec![Arc::new(a) as _, Arc::new(b) as _]; | ||
let s1 = StructArray::new(fields.clone(), values, nulls); | ||
|
||
let a = Int32Array::from(vec![None, Some(2), None]); | ||
let mut b = ListBuilder::new(Int32Builder::new()); | ||
b.extend([None, None, Some(vec![])]); | ||
let b = b.finish(); | ||
|
||
let values = vec![Arc::new(a) as _, Arc::new(b) as _]; | ||
let s2 = StructArray::new(fields.clone(), values, None); | ||
|
||
let opts = SortOptions { | ||
descending: false, | ||
nulls_first: true, | ||
}; | ||
let cmp = make_comparator(&s1, &s2, opts).unwrap(); | ||
assert_eq!(cmp(0, 1), Ordering::Less); // (1, [1, 2]) cmp (2, None) | ||
assert_eq!(cmp(0, 0), Ordering::Greater); // (1, [1, 2]) cmp (None, None) | ||
assert_eq!(cmp(1, 1), Ordering::Greater); // (2, [None]) cmp (2, None) | ||
assert_eq!(cmp(2, 2), Ordering::Less); // (None, None) cmp (None, []) | ||
assert_eq!(cmp(3, 0), Ordering::Less); // None cmp (None, []) | ||
assert_eq!(cmp(2, 0), Ordering::Equal); // (None, None) cmp (None, None) | ||
assert_eq!(cmp(3, 0), Ordering::Less); // None cmp (None, None) | ||
|
||
let opts = SortOptions { | ||
descending: true, | ||
nulls_first: true, | ||
}; | ||
let cmp = make_comparator(&s1, &s2, opts).unwrap(); | ||
assert_eq!(cmp(0, 1), Ordering::Greater); // (1, [1, 2]) cmp (2, None) | ||
assert_eq!(cmp(0, 0), Ordering::Greater); // (1, [1, 2]) cmp (None, None) | ||
assert_eq!(cmp(1, 1), Ordering::Greater); // (2, [None]) cmp (2, None) | ||
assert_eq!(cmp(2, 2), Ordering::Less); // (None, None) cmp (None, []) | ||
assert_eq!(cmp(3, 0), Ordering::Less); // None cmp (None, []) | ||
assert_eq!(cmp(2, 0), Ordering::Equal); // (None, None) cmp (None, None) | ||
assert_eq!(cmp(3, 0), Ordering::Less); // None cmp (None, None) | ||
|
||
let opts = SortOptions { | ||
descending: true, | ||
nulls_first: false, | ||
}; | ||
let cmp = make_comparator(&s1, &s2, opts).unwrap(); | ||
assert_eq!(cmp(0, 1), Ordering::Greater); // (1, [1, 2]) cmp (2, None) | ||
assert_eq!(cmp(0, 0), Ordering::Less); // (1, [1, 2]) cmp (None, None) | ||
assert_eq!(cmp(1, 1), Ordering::Less); // (2, [None]) cmp (2, None) | ||
assert_eq!(cmp(2, 2), Ordering::Greater); // (None, None) cmp (None, []) | ||
assert_eq!(cmp(3, 0), Ordering::Greater); // None cmp (None, []) | ||
assert_eq!(cmp(2, 0), Ordering::Equal); // (None, None) cmp (None, None) | ||
assert_eq!(cmp(3, 0), Ordering::Greater); // None cmp (None, None) | ||
|
||
let opts = SortOptions { | ||
descending: false, | ||
nulls_first: false, | ||
}; | ||
let cmp = make_comparator(&s1, &s2, opts).unwrap(); | ||
assert_eq!(cmp(0, 1), Ordering::Less); // (1, [1, 2]) cmp (2, None) | ||
assert_eq!(cmp(0, 0), Ordering::Less); // (1, [1, 2]) cmp (None, None) | ||
assert_eq!(cmp(1, 1), Ordering::Less); // (2, [None]) cmp (2, None) | ||
assert_eq!(cmp(2, 2), Ordering::Greater); // (None, None) cmp (None, []) | ||
assert_eq!(cmp(3, 0), Ordering::Greater); // None cmp (None, []) | ||
assert_eq!(cmp(2, 0), Ordering::Equal); // (None, None) cmp (None, None) | ||
assert_eq!(cmp(3, 0), Ordering::Greater); // None cmp (None, None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might help to point people at the faster kernels too
like "see kernels in
ord::cmp
for fast kernels