Skip to content

[Variant] Remove ceremony from iterator of variants into VariantArray - #8625

Merged
alamb merged 2 commits into
apache:mainfrom
pydantic:friendlymatthew/impl-into-iter-variant-array
Oct 16, 2025
Merged

alamb merged 2 commits into
apache:mainfrom
pydantic:friendlymatthew/impl-into-iter-variant-array

Conversation

@friendlymatthew

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Mapping from an iterator of Variants into a VariantArray currently requires a lot of ceremony. This PR abstracts over this.

@github-actions github-actions Bot added the parquet-variant parquet-variant* crates label Oct 15, 2025
@friendlymatthew

Copy link
Copy Markdown
Contributor Author

cc @alamb @scovich

Re: #8611 (comment)

I'll throw in the other impl From in the second commit

@friendlymatthew
friendlymatthew force-pushed the friendlymatthew/impl-into-iter-variant-array branch from 1722f43 to ece0ffb Compare October 15, 2025 14:26

@scovich scovich left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

I'll throw in the other impl From in 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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need that into_iter call, the trait takes IntoIterator as input?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was just testing something and forgot to revert that. Luckily clippy caught it

Comment on lines +443 to +458
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()
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@friendlymatthew

Copy link
Copy Markdown
Contributor Author

LGTM!

I'll throw in the other impl From in 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)

Yeah, I agree. I'll just remove the second impl From

@friendlymatthew
friendlymatthew force-pushed the friendlymatthew/impl-into-iter-variant-array branch from ece0ffb to eda7457 Compare October 15, 2025 14:31
@friendlymatthew
friendlymatthew force-pushed the friendlymatthew/impl-into-iter-variant-array branch from eda7457 to ea264c4 Compare October 15, 2025 14:31
}
}

impl<'m, 'v> FromIterator<Option<Variant<'m, 'v>>> for VariantArray {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this:

Suggested change
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)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alamb
alamb merged commit 63fe99a into apache:main Oct 16, 2025
alamb pushed a commit that referenced this pull request Oct 17, 2025
…tArray` (#8627)

# Which issue does this PR close?

- Closes #8610

# Rationale for this change

Since the fields of `VariantArray` impl `PartialEq`, this PR simply
derives `PartialEq` for `VariantArray`
out.

Based off of #8625
samueleresca pushed a commit to samueleresca/arrow-rs that referenced this pull request Oct 18, 2025
…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
alamb pushed a commit that referenced this pull request Oct 24, 2025
- 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.
alamb pushed a commit that referenced this pull request Oct 28, 2025
)

# 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]>
alamb pushed a commit that referenced this pull request Sep 28, 2026
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Variant] Remove ceremony of going from list of Variant to VariantArray

4 participants