[Variant] Make VariantArray iterable#8613
Conversation
a5da641 to
f8c109b
Compare
afa37a5 to
a254af8
Compare
|
For context this was removed in #8392 |
a254af8 to
3cdd28b
Compare
arrow::Array for VariantArrayVariantArray iterable
Thanks for clarifying |
e188b4b to
d65a5c3
Compare
alamb
left a comment
There was a problem hiding this comment.
Nice! this looks very useful to me -- thanks @friendlymatthew
| @@ -436,6 +441,99 @@ impl From<VariantArray> for ArrayRef { | |||
| } | |||
| } | |||
|
|
|||
| /// An iterator over [`VariantArray`] | |||
| /// | |||
| /// This iterator returns `Option<Option<Variant<'a, 'a>>>` where: | |||
| out | ||
| } | ||
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { |
There was a problem hiding this comment.
I double checked the definition of size_hint and this looks good:
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.size_hint
scovich
left a comment
There was a problem hiding this comment.
Very nice! One code cleanup to consider.
| if self.head_i == self.tail_i { | ||
| return None; | ||
| } | ||
|
|
||
| let out = if self.is_null(self.head_i) { | ||
| Some(None) | ||
| } else { | ||
| Some(Some(self.array.value(self.head_i))) | ||
| }; | ||
|
|
||
| self.head_i += 1; | ||
|
|
||
| out |
There was a problem hiding this comment.
| if self.head_i == self.tail_i { | |
| return None; | |
| } | |
| let out = if self.is_null(self.head_i) { | |
| Some(None) | |
| } else { | |
| Some(Some(self.array.value(self.head_i))) | |
| }; | |
| self.head_i += 1; | |
| out | |
| (self.head_i < self.tail_i) | |
| .then(|| self.value_opt(self.head_i)) | |
| .inspect(|_| self.head_i += 1) |
(same story for DoubleEndedIterator below)
There was a problem hiding this comment.
Hi hi, I think using combinators are great, but in this case we're 1) hurting readability and 2) using Option::inspect to mutate state
There was a problem hiding this comment.
Agree the inspect call was too clever, but why would this hurt readability?
(self.head_i < self.tail_i).then(|| {
let out = self.value_opt(self.head_i);
self.head_i += 1;
out
})There was a problem hiding this comment.
I think cases like the following are completely valid and very readable. It's simply inspecting the state
(self.head_i < self.tail_i).then(|| self.value_opt(self.head_i)); In the closure you posted above, I'm just a bit surprised as a reader that the closure is inspecting the state and mutating it. Plus, I think the following form is simpler to reason through
// base case
if self.head_i == self.tail_i {
return None;
}
// else
let out = self.value_opt(self.head_i);
self.head_i += 1;
Some(out)But then again, it's just personal preference. I don't think there's anything wrong with either approach
d65a5c3 to
3c5bcd4
Compare
|
Thanks @scovich and @friendlymatthew |
Which issue does this PR close?
VariantArray#8609VariantArrayiterable #8612This PR introduces an Iterator over
VariantArray. SinceVariantArraydoes notimpl Array, we can't make use ofArrayIter