-
Notifications
You must be signed in to change notification settings - Fork 881
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
Fix Null Mask Handling in ArrayData
, UnionArray
, and MapArray
#1589
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ecf753b
Fix ListArray and StructArray equality (#626)
tustvold 95b2ed3
Simplify null masking in equality comparisons
tustvold feda7b6
Miscellaneous fixes
tustvold 4aff5fe
Fix structure null equality
tustvold f67b650
Review feedback
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 |
---|---|---|
|
@@ -61,7 +61,6 @@ use std::any::Any; | |
/// type_id_buffer, | ||
/// Some(value_offsets_buffer), | ||
/// children, | ||
/// None, | ||
/// ).unwrap(); | ||
/// | ||
/// let value = array.value(0).as_any().downcast_ref::<Int32Array>().unwrap().value(0); | ||
|
@@ -94,7 +93,6 @@ use std::any::Any; | |
/// type_id_buffer, | ||
/// None, | ||
/// children, | ||
/// None, | ||
/// ).unwrap(); | ||
/// | ||
/// let value = array.value(0).as_any().downcast_ref::<Int32Array>().unwrap().value(0); | ||
|
@@ -140,7 +138,6 @@ impl UnionArray { | |
type_ids: Buffer, | ||
value_offsets: Option<Buffer>, | ||
child_arrays: Vec<(Field, ArrayRef)>, | ||
bitmap_data: Option<Buffer>, | ||
) -> Self { | ||
let (field_types, field_values): (Vec<_>, Vec<_>) = | ||
child_arrays.into_iter().unzip(); | ||
|
@@ -152,13 +149,11 @@ impl UnionArray { | |
UnionMode::Sparse | ||
}; | ||
|
||
let mut builder = ArrayData::builder(DataType::Union(field_types, mode)) | ||
let builder = ArrayData::builder(DataType::Union(field_types, mode)) | ||
.add_buffer(type_ids) | ||
.child_data(field_values.into_iter().map(|a| a.data().clone()).collect()) | ||
.len(len); | ||
if let Some(bitmap) = bitmap_data { | ||
builder = builder.null_bit_buffer(bitmap) | ||
} | ||
|
||
let data = match value_offsets { | ||
Some(b) => builder.add_buffer(b).build_unchecked(), | ||
None => builder.build_unchecked(), | ||
|
@@ -171,7 +166,6 @@ impl UnionArray { | |
type_ids: Buffer, | ||
value_offsets: Option<Buffer>, | ||
child_arrays: Vec<(Field, ArrayRef)>, | ||
bitmap: Option<Buffer>, | ||
) -> Result<Self> { | ||
if let Some(b) = &value_offsets { | ||
if ((type_ids.len()) * 4) != b.len() { | ||
|
@@ -216,7 +210,7 @@ impl UnionArray { | |
// Unsafe Justification: arguments were validated above (and | ||
// re-revalidated as part of data().validate() below) | ||
let new_self = | ||
unsafe { Self::new_unchecked(type_ids, value_offsets, child_arrays, bitmap) }; | ||
unsafe { Self::new_unchecked(type_ids, value_offsets, child_arrays) }; | ||
new_self.data().validate()?; | ||
|
||
Ok(new_self) | ||
|
@@ -512,7 +506,7 @@ mod tests { | |
builder.append::<Int32Type>("a", 1).unwrap(); | ||
builder.append::<Int64Type>("c", 3).unwrap(); | ||
builder.append::<Int32Type>("a", 10).unwrap(); | ||
builder.append_null().unwrap(); | ||
builder.append_null::<Int32Type>("a").unwrap(); | ||
builder.append::<Int32Type>("a", 6).unwrap(); | ||
let union = builder.build().unwrap(); | ||
|
||
|
@@ -522,29 +516,29 @@ mod tests { | |
match i { | ||
0 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(1_i32, value); | ||
} | ||
1 => { | ||
let slot = slot.as_any().downcast_ref::<Int64Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(3_i64, value); | ||
} | ||
2 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(10_i32, value); | ||
} | ||
3 => assert!(union.is_null(i)), | ||
3 => assert!(slot.is_null(0)), | ||
4 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(6_i32, value); | ||
|
@@ -560,7 +554,7 @@ mod tests { | |
builder.append::<Int32Type>("a", 1).unwrap(); | ||
builder.append::<Int64Type>("c", 3).unwrap(); | ||
builder.append::<Int32Type>("a", 10).unwrap(); | ||
builder.append_null().unwrap(); | ||
builder.append_null::<Int32Type>("a").unwrap(); | ||
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. it is strange to require a field name to append nulls -- I left a comment on how to improve things perhaps in the future |
||
builder.append::<Int32Type>("a", 6).unwrap(); | ||
let union = builder.build().unwrap(); | ||
|
||
|
@@ -573,15 +567,15 @@ mod tests { | |
match i { | ||
0 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(10_i32, value); | ||
} | ||
1 => assert!(new_union.is_null(i)), | ||
1 => assert!(slot.is_null(0)), | ||
2 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(6_i32, value); | ||
|
@@ -614,13 +608,9 @@ mod tests { | |
Arc::new(float_array), | ||
), | ||
]; | ||
let array = UnionArray::try_new( | ||
type_id_buffer, | ||
Some(value_offsets_buffer), | ||
children, | ||
None, | ||
) | ||
.unwrap(); | ||
let array = | ||
UnionArray::try_new(type_id_buffer, Some(value_offsets_buffer), children) | ||
.unwrap(); | ||
|
||
// Check type ids | ||
assert_eq!(Buffer::from_slice_ref(&type_ids), array.data().buffers()[0]); | ||
|
@@ -800,7 +790,7 @@ mod tests { | |
fn test_sparse_mixed_with_nulls() { | ||
let mut builder = UnionBuilder::new_sparse(5); | ||
builder.append::<Int32Type>("a", 1).unwrap(); | ||
builder.append_null().unwrap(); | ||
builder.append_null::<Int32Type>("a").unwrap(); | ||
builder.append::<Float64Type>("c", 3.0).unwrap(); | ||
builder.append::<Int32Type>("a", 4).unwrap(); | ||
let union = builder.build().unwrap(); | ||
|
@@ -824,22 +814,22 @@ mod tests { | |
match i { | ||
0 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(1_i32, value); | ||
} | ||
1 => assert!(union.is_null(i)), | ||
1 => assert!(slot.is_null(0)), | ||
2 => { | ||
let slot = slot.as_any().downcast_ref::<Float64Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(value, 3_f64); | ||
} | ||
3 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(4_i32, value); | ||
|
@@ -853,9 +843,9 @@ mod tests { | |
fn test_sparse_mixed_with_nulls_and_offset() { | ||
let mut builder = UnionBuilder::new_sparse(5); | ||
builder.append::<Int32Type>("a", 1).unwrap(); | ||
builder.append_null().unwrap(); | ||
builder.append_null::<Int32Type>("a").unwrap(); | ||
builder.append::<Float64Type>("c", 3.0).unwrap(); | ||
builder.append_null().unwrap(); | ||
builder.append_null::<Float64Type>("c").unwrap(); | ||
builder.append::<Int32Type>("a", 4).unwrap(); | ||
let union = builder.build().unwrap(); | ||
|
||
|
@@ -866,18 +856,18 @@ mod tests { | |
for i in 0..new_union.len() { | ||
let slot = new_union.value(i); | ||
match i { | ||
0 => assert!(new_union.is_null(i)), | ||
0 => assert!(slot.is_null(0)), | ||
1 => { | ||
let slot = slot.as_any().downcast_ref::<Float64Array>().unwrap(); | ||
assert!(!new_union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(value, 3_f64); | ||
} | ||
2 => assert!(new_union.is_null(i)), | ||
2 => assert!(slot.is_null(0)), | ||
3 => { | ||
let slot = slot.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
assert!(!new_union.is_null(i)); | ||
assert!(!slot.is_null(0)); | ||
assert_eq!(slot.len(), 1); | ||
let value = slot.value(0); | ||
assert_eq!(4_i32, value); | ||
|
@@ -886,4 +876,12 @@ mod tests { | |
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_type_check() { | ||
let mut builder = UnionBuilder::new_sparse(2); | ||
builder.append::<Float32Type>("a", 1.0).unwrap(); | ||
let err = builder.append::<Int32Type>("a", 1).unwrap_err().to_string(); | ||
assert!(err.contains("Attempt to write col \"a\" with type Int32 doesn't match existing type Float32"), "{}", err); | ||
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. 👍 |
||
} | ||
} |
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 is good to check null in the current slot, but does
union.is_null(index)
not work anymore? Can we check both?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.
The nullability is only a property of the slot, and not the Union itself, at least that is my interpretation of the specification
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.
https://arrow.apache.org/docs/format/Columnar.html#union-layout
Says
Which seems consistent 👍
However, it seems like
UnionArray
doesn't overrideis_null
to take this into account. Filed #1625 to track