Skip to content

Conversation

@superserious-dev
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

Adds Float16 conversion to the cast_to_variant kernel

What changes are included in this PR?

  • a macro to make converting array type that require a cast simpler
  • conversion of DataType::Float16 => Variant::Float

Are these changes tested?

Yes, additional unit tests have been added.

Are there any user-facing changes?

Yes, adds new type conversion to kernel


/// Convert the input array to a `VariantArray` row by row,
/// transforming each element with `cast_fn`
macro_rules! cast_conversion {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This macro applies cast_fn to each element before converting it to a Variant. Some of the remaining types will require casting before being accepted by Variant::from.

We could also add the cast_fn argument to primitive_conversion! macro and not add this macro. It would require passing something like |v| v (ie. a no-op function) to the existing primitive conversions that don't require casts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm working on a couple of these issues in parallel and made some additional tweaks to the macro here: https://github.com/apache/arrow-rs/pull/8074/files.

[dependencies]
arrow = { workspace = true }
arrow-schema = { workspace = true }
half = { version = "2.1", default-features = false }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needed to reference f16 in the code and in the tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah -- we should probably directly export he f16 type from the arrow crate (pub use) to avoid having users explicitly have to use half . Maybe as a follow on PR

primitive_conversion!(UInt64Type, input, builder);
}
DataType::Float16 => {
cast_conversion!(Float16Type, |v: f16| -> f32 { v.into() }, input, builder);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Casted f16 to f32 so that the value can be wrapped by Variant::Float.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense to me.

In general, getting a macro that knows how to convert various Arrow types to Variant I think is an important building block

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @superserious-dev

Given the helpfulness of this macro I am going to merge this PR in now to avoid conflicts / hopefully others can use it

[dependencies]
arrow = { workspace = true }
arrow-schema = { workspace = true }
half = { version = "2.1", default-features = false }
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah -- we should probably directly export he f16 type from the arrow crate (pub use) to avoid having users explicitly have to use half . Maybe as a follow on PR

primitive_conversion!(UInt64Type, input, builder);
}
DataType::Float16 => {
cast_conversion!(Float16Type, |v: f16| -> f32 { v.into() }, input, builder);
Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense to me.

In general, getting a macro that knows how to convert various Arrow types to Variant I think is an important building block

@alamb alamb merged commit 554cafa into apache:main Aug 7, 2025
13 checks passed
@alamb alamb changed the title Implement DataType::Float16 => Variant::Float [Variant] Implement DataType::Float16 => Variant::Float Aug 7, 2025
@superserious-dev superserious-dev deleted the float16-cast-to-variant branch August 7, 2025 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Variant]: Implement DataType::Float16 support for cast_to_variant kernel

2 participants