Repository navigation
[Variant] Remove ceremony from iterator of variants into VariantArray - #8625
Conversation
|
Re: #8611 (comment) I'll throw in the other |
1722f43 to
ece0ffb
Compare
scovich
left a comment
There was a problem hiding this comment.
LGTM!
I'll throw in the other
impl Fromin the second commit
I'm not sure this is needed? VariantArray::from_iterator is pretty clear, and a VariantArray::from wouldn't be able to do anything smarter (can't just take ownership of the vec's data, for example)
| Variant::ShortString(ShortString::try_new("norm").unwrap()), | ||
| ]; | ||
|
|
||
| let variant_array = VariantArray::from_iter(v.into_iter()); |
There was a problem hiding this comment.
Shouldn't need that into_iter call, the trait takes IntoIterator as input?
There was a problem hiding this comment.
Yeah I was just testing something and forgot to revert that. Luckily clippy caught it
| impl<'m, 'v> FromIterator<Option<Variant<'m, 'v>>> for VariantArray { | ||
| fn from_iter<T: IntoIterator<Item = Option<Variant<'m, 'v>>>>(iter: T) -> Self { | ||
| let mut b = VariantArrayBuilder::new(0); | ||
| b.extend(iter); | ||
| b.build() | ||
| } | ||
| } | ||
|
|
||
| impl<'m, 'v> FromIterator<Variant<'m, 'v>> for VariantArray { | ||
| fn from_iter<T: IntoIterator<Item = Variant<'m, 'v>>>(iter: T) -> Self { | ||
| let mut b = VariantArrayBuilder::new(0); | ||
| b.extend(iter); | ||
| b.build() | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
I wonder if there's a way to get the iterator size without consuming the iterator...
But then again, I haven't ran a profile on the VariantArray code yet. I plan on looking at perf later on
There was a problem hiding this comment.
Most std lib code I've seen relies on the iterator's lower bound hint, with a fallback path if that turned out to be too low.
There's also the ExactSizeIterator trait, but given how rust treats potentially overlapping generic definitions as conflicts, I don't know how to provide implementations for both Iterator and ExactSizeIterator?
Yeah, I agree. I'll just remove the second impl From |
ece0ffb to
eda7457
Compare
eda7457 to
ea264c4
Compare
| } | ||
| } | ||
|
|
||
| impl<'m, 'v> FromIterator<Option<Variant<'m, 'v>>> for VariantArray { |
There was a problem hiding this comment.
If we do this:
| impl<'m, 'v> FromIterator<Option<Variant<'m, 'v>>> for VariantArray { | |
| impl<'m, 'v, V: Into<Variant<'m, 'v>> FromIterator<Option<V>> for VariantArray { |
then we can create a variant array from anything that converts cleanly to variant (e.g. i64 values or &str)?
There was a problem hiding this comment.
Yeah, I thought about this as well, but I figured we'd be creating 2 abstractions?
As in, I wonder if it's better to guide the user to do the Variant::from explicitly 🤔
There was a problem hiding this comment.
This is mostly going to be used by test code, and a homogenous set of e.g. i64 is a pretty common use case. But I don't have strong feelings either way.
There was a problem hiding this comment.
Filed a ticket to track this idea
…tArray` (apache#8627) # Which issue does this PR close? - Closes apache#8610 # Rationale for this change Since the fields of `VariantArray` impl `PartialEq`, this PR simply derives `PartialEq` for `VariantArray` out. Based off of apache#8625
- A follow up from #8625 # Rationale for this change While working on a separate task, I noticed `create_test_variant_array` was redundant. Since `VariantArray` can already be constructed directly from an iterator of Variants, this PR removes the now-unnecessary test helper.
) # Which issue does this PR close? Related-to: #8625 # Rationale for this change 63fe99a introduced the new unit test. I believe the index=3 is a typo. It should be =2. # What changes are included in this PR? Fixed the array index. # Are these changes tested? The fix is in a unit test. # Are there any user-facing changes? No. Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
…ble to Variant (#11053) ## Rationale Closes #8628. Previously `VariantArray` could be collected only from `Option<Variant>` items: ```rust let arr: VariantArray = values.iter().map(|v| Some(Variant::from(*v))).collect(); ``` With this change, any type that converts cleanly into `Variant` can be collected directly: ```rust let arr: VariantArray = vec![Some(1_i64), None, Some(42_i64)].into_iter().collect(); let arr: VariantArray = vec![Some("hello"), Some("a very long string exceeding the short string limit")].into_iter().collect(); ``` This follows the suggestion made in #8625 (comment). ## Changes - `parquet-variant-compute/src/variant_array.rs`: generalize `FromIterator<Option<Variant<'m, 'v>>>` to `impl<'m, 'v, V: Into<Variant<'m, 'v>>> FromIterator<Option<V>>`, converting each item through `Into::into`. Existing code that collects `Option<Variant>` items keeps working (identity `Into`). - Added regression tests: `i64` items with nulls, `&str` items covering both short-string (ShortString) and long-string (String) storage. ## Verification - `cargo test -p parquet-variant-compute --lib` — 366 tests passed (364 existing + 2 new), including the new `test_from_option_into_variants_into_variant_array` and `test_from_option_str_into_variant_array`. - `cargo clippy -p parquet-variant-compute --all-targets -- -D warnings` — clean. - `cargo fmt --all -- --check` — clean. Note: local verification used the `x86_64-pc-windows-gnu` target; upstream CI covers the full matrix. ## AI disclosure Per the Arrow project's AI-generated submissions guidance: this change was implemented with the assistance of an AI coding agent. The author reviewed the final diff, verified it compiles and passes tests/clippy/fmt locally, and takes full ownership of the change. The ASF ICLA may need to be on file before merge (asf-clabot will report the status).
Which issue does this PR close?
VarianttoVariantArray#8606Rationale for this change
Mapping from an iterator of
Variants into aVariantArraycurrently requires a lot of ceremony. This PR abstracts over this.