You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/08/11 13:07:01 UTC
[GitHub] [arrow-rs] psvri opened a new pull request, #2413: Add validation logic for StructBuilder::finish
psvri opened a new pull request, #2413:
URL: https://github.com/apache/arrow-rs/pull/2413
# Which issue does this PR close?
Closes #2252.
# Rationale for this change
```StructBuilder::finish``` calls ```ArrayDataBuilder::build_unchecked``` without verifying the length of the child arrays. This allows forming invalid ```ArrayData```
# What changes are included in this PR?
I have added a new private function ```StructBuilder::validate_content``` which is called first before running the finish logic. This validates contents in the struct to ensure that fields and field_builders are of equal length and the number of items in individual field_builders are equal to self.len
# Are there any user-facing changes?
No, Since I have used panics to handle the errors.
--
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
[GitHub] [arrow-rs] psvri commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
psvri commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r943819781
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +234,17 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Validates contents in the struct to ensure that fields and field_builders are of equal
+ /// length and the number of items in individual field_builders are equal to self.len
+ fn validate_content(&self) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("Field_builders are of unequal lengths.")
+ }
Review Comment:
I have added this logic in [29ff8a6](https://github.com/apache/arrow-rs/pull/2413/commits/29ff8a65226856ffb90c9b8f9e9a4b8f129a5114)
--
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
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r943577472
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +234,17 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Validates contents in the struct to ensure that fields and field_builders are of equal
+ /// length and the number of items in individual field_builders are equal to self.len
+ fn validate_content(&self) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("Field_builders are of unequal lengths.")
+ }
Review Comment:
This should also verify the length of the null buffer
--
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
[GitHub] [arrow-rs] liukun4515 commented on pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
liukun4515 commented on PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#issuecomment-1212023657
I think the validation should be done in each `push` operation for Builder or not the `finish`
--
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
[GitHub] [arrow-rs] HaoYang670 commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944436453
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +236,28 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of items in individual field_builders are equal to self.null_buffer_builder.len()
+ fn validate_content(&self) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+ if !self
+ .field_builders
+ .iter()
+ .all(|x| x.len() == self.null_buffer_builder.len())
Review Comment:
As we have checked
```
self.field_builders.iter().all(|x| x.len() == self.len)
```
before, we don't need to check
```
self .field_builders.iter().all(|x| x.len() == self.null_buffer_builder.len())
```
Based on the transitivity of integer equality, checking
```
self.len == self.null_buffer_builder.len()
```
is enough.
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -411,4 +436,44 @@ mod tests {
let mut builder = StructBuilder::new(fields, field_builders);
assert!(builder.field_builder::<BinaryBuilder>(0).is_none());
}
+
+ #[test]
+ #[should_panic(expected = "StructBuilder and field_builders are of unequal lengths.")]
+ fn test_struct_array_builder_unequal_field_builders_lengths() {
+ let mut int_builder = Int32Builder::new(10);
+ let mut bool_builder = BooleanBuilder::new(10);
+
+ int_builder.append_value(1);
+ int_builder.append_value(2);
+ bool_builder.append_value(true);
+
+ let mut fields = Vec::new();
+ let mut field_builders = Vec::new();
+ fields.push(Field::new("f1", DataType::Int32, false));
+ field_builders.push(Box::new(int_builder) as Box<dyn ArrayBuilder>);
+ fields.push(Field::new("f2", DataType::Boolean, false));
+ field_builders.push(Box::new(bool_builder) as Box<dyn ArrayBuilder>);
+
+ let mut builder = StructBuilder::new(fields, field_builders);
+ builder.append(true);
+ builder.append(true);
+ builder.finish();
+ }
+
+ #[test]
+ #[should_panic(
+ expected = "Number of fields is not equal to the number of field_builders."
+ )]
+ fn test_struct_array_builder_unequal_field_field_builders() {
+ let int_builder = Int32Builder::new(10);
+
+ let mut fields = Vec::new();
+ let mut field_builders = Vec::new();
+ fields.push(Field::new("f1", DataType::Int32, false));
+ field_builders.push(Box::new(int_builder) as Box<dyn ArrayBuilder>);
+ fields.push(Field::new("f2", DataType::Boolean, false));
+
+ let mut builder = StructBuilder::new(fields, field_builders);
+ builder.finish();
+ }
Review Comment:
We also need tests to check the length of null buffer builder.
--
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
[GitHub] [arrow-rs] ursabot commented on pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#issuecomment-1214159050
Benchmark runs are scheduled for baseline = 98eeb01bf8c52052ee0e6ce12543f6e3b4f6ca27 and contender = f0d7d0b8d41e5a13005f15b41c8f09f01e09ebed. f0d7d0b8d41e5a13005f15b41c8f09f01e09ebed is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/a0bc2ed5d40949ddbf86848a0e6d0382...c0ec351edd1d49849d7fd61faf28b041/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/47967480579e460e8a590bd544c4d392...14505a4f6120420e99847ec092ddbe1f/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/b99425054df24d07a145b16b3eb27f9c...bb89fa40edda4c6d8d12361acf1b1367/)
[Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/4848687d0c184c15b6bcafaecf9e79d7...624351bc25004ffbb52f389216d20f78/)
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
--
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
[GitHub] [arrow-rs] psvri commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
psvri commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944168104
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
In array::data::tests::test_try_new_sliced_struct , there was this below code and thats why I had understood it that way.
```
// struct = { a: int, b: bool }
// struct[1] = null
builder
.field_builder::<Int32Builder>(0)
.unwrap()
.append_option(None);
builder
.field_builder::<BooleanBuilder>(1)
.unwrap()
.append_option(None);
builder.append(false);
```
So I added this logic. My bad,I had understood it wrong. I shall remove it.
--
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
[GitHub] [arrow-rs] HaoYang670 commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944440652
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +236,28 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of items in individual field_builders are equal to self.null_buffer_builder.len()
+ fn validate_content(&self) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+ if !self
+ .field_builders
+ .iter()
+ .all(|x| x.len() == self.null_buffer_builder.len())
Review Comment:
Oh, wait! We don't need to check the null buffer length at all. As we can make sure that the length of null buffer builder is always equal to `self.len`. @tustvold
--
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
[GitHub] [arrow-rs] psvri commented on pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
psvri commented on PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#issuecomment-1212049501
In the linked issue the expected behavior was for ```finish``` to panic if its children are not all the same length. Hence I made the code this way.
--
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
[GitHub] [arrow-rs] psvri commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
psvri commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944369634
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
Can you check if the latest commit looks good ?
--
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
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944211616
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
The check should be that the bit length of null_buffer_builder is the same as the length of the inner builders, the null counts can and likely will differ
--
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
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944442511
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +236,28 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of items in individual field_builders are equal to self.null_buffer_builder.len()
+ fn validate_content(&self) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+ if !self
+ .field_builders
+ .iter()
+ .all(|x| x.len() == self.null_buffer_builder.len())
Review Comment:
Aah yes, does beg the question why we even have `self.len` as a separate variable...
--
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
[GitHub] [arrow-rs] HaoYang670 commented on pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#issuecomment-1214147938
Could we merge this? And then we can do some clean-up.
--
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
[GitHub] [arrow-rs] psvri commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
psvri commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944139812
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
When we say a struct is null, its child elements should be null. Hence they should have same null counts.
--
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
[GitHub] [arrow-rs] HaoYang670 commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944446372
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +236,28 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of items in individual field_builders are equal to self.null_buffer_builder.len()
+ fn validate_content(&self) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+ if !self
+ .field_builders
+ .iter()
+ .all(|x| x.len() == self.null_buffer_builder.len())
Review Comment:
Filed https://github.com/apache/arrow-rs/issues/2429 to track this.
--
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
[GitHub] [arrow-rs] HaoYang670 commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944150450
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
> When we say a struct is null, its child elements should be null.
Sorry, I'm afraid this is not the truth. Each array has its own null buffer, which means the validation of elements in different arrays are independent.
For example, let's say we have a struct array `["name": String, "age": Int]`. We could have an element which the fields could be `None`:
```
[
["Name": Some("Bob"), "age": Some(32)],
["Name": Some("Andy"), "age": None],
["Name": None, "age": None],
]
```
Also, we could have a `null` field in the Struct array:
```
[
Some(["Name": Some("Bob"), "age": Some(32)]),
None, // the underlying "Name" and "age" fields could be anything. Because they are masked by the null buffer.
Some(["Name": None, "age": None]),
]
```
--
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
[GitHub] [arrow-rs] HaoYang670 commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944071176
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
Review Comment:
Could we check this when constructing the struct builder, such as moving this check to `StructBuilder::new()`
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
Why can't struct builder and child builders have different null counts?
--
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
[GitHub] [arrow-rs] tustvold merged pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
tustvold merged PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413
--
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
[GitHub] [arrow-rs] HaoYang670 commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944150450
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
> When we say a struct is null, its child elements should be null.
Sorry, I'm afraid this is not the truth. Each array has its own null buffer, which means the validation of elements in different array are independent.
For example, let's say we have a struct array `["name": String, "age": Int]`. We could have an element which the fields could be `None`:
```
[
["Name": Some("Bob"), "age": Some(32)],
["Name": Some("Andy"), "age": None],
["Name": None, "age": None],
]
```
Also, we could have a `null` field in the Struct array:
```
[
Some(["Name": Some("Bob"), "age": Some(32)]),
None, // the underlying "Name" and "age" fields could be anything. Because they are masked by the null buffer.
Some(["Name": None, "age": None]),
]
```
--
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
[GitHub] [arrow-rs] HaoYang670 commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
HaoYang670 commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944150450
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
+ }
+ if !self.field_builders.iter().all(|x| x.len() == self.len) {
+ panic!("StructBuilder and field_builders are of unequal lengths.");
+ }
+
+ let mut child_data = Vec::with_capacity(self.field_builders.len());
+ for f in &mut self.field_builders {
+ let arr = f.finish();
+ child_data.push(arr.into_data());
+ }
+
+ let null_bit_buffer = self.null_buffer_builder.finish();
+
+ if !child_data.is_empty() {
+ let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, self.len);
+
+ let mut child_null_count = 0;
+
+ //child_null_count is incremented when all child elements are null
+ for index in 0..child_data[0].len() {
+ let mut is_null = true;
+ for data in &child_data {
+ if data.is_valid(index) {
+ is_null = false;
+ break;
+ }
+ }
+ if is_null {
+ child_null_count += 1;
+ }
+ }
+
+ if child_null_count != self_null_count {
+ panic!("StructBuilder and field_builders contain unequal null counts.");
Review Comment:
> When we say a struct is null, its child elements should be null.
Sorry, I'm afraid this is not the truth. Each array has its own null buffer, which means the validation of elements in different arrays are independent.
For example, let's say we have a struct array `["name": String, "age": Int]`. We could have elements whose fields could be `None`:
```
[
["Name": Some("Bob"), "age": Some(32)],
["Name": Some("Andy"), "age": None],
["Name": None, "age": None],
]
```
Also, we could have a `null` field in the Struct array:
```
[
Some(["Name": Some("Bob"), "age": Some(32)]),
None, // the underlying "Name" and "age" fields could be anything. Because they are masked by the null buffer.
Some(["Name": None, "age": None]),
]
```
--
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
[GitHub] [arrow-rs] psvri commented on a diff in pull request #2413: Add validation logic for StructBuilder::finish
Posted by GitBox <gi...@apache.org>.
psvri commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944138637
##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
let array_data = unsafe { builder.build_unchecked() };
StructArray::from(array_data)
}
+
+ /// Constructs and validates contents in the builder to ensure that
+ /// - fields and field_builders are of equal length
+ /// - the number of items in individual field_builders are equal to self.len
+ /// - the number of null items in individual field_builders are equal to null_count in self.null_buffer_builder.
+ fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+ if self.fields.len() != self.field_builders.len() {
+ panic!("Number of fields is not equal to the number of field_builders.");
Review Comment:
I too was thinking the same. But I made my code according to issues expected behavior. If the team needs it, I shall move this check to new function.
--
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