This is an automated email from the ASF dual-hosted git repository.
jeffreyvo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 181007df05 Support Float16 for create_random_array (#9029)
181007df05 is described below
commit 181007df053e9004f7a211f7de086c4bbbd0a9e9
Author: niebayes <[email protected]>
AuthorDate: Wed Feb 4 09:53:19 2026 +0800
Support Float16 for create_random_array (#9029)
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
- Closes #9028
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Yes
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
Yes, users can now use `create_random_array` to generate random Float16
array.
---------
Co-authored-by: Jefffrey <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
---
arrow/Cargo.toml | 4 ++--
arrow/src/util/bench_util.rs | 2 +-
arrow/src/util/data_gen.rs | 7 ++-----
3 files changed, 5 insertions(+), 8 deletions(-)
diff --git a/arrow/Cargo.toml b/arrow/Cargo.toml
index fbb52318d3..137d785eee 100644
--- a/arrow/Cargo.toml
+++ b/arrow/Cargo.toml
@@ -55,7 +55,7 @@ arrow-select = { workspace = true }
arrow-string = { workspace = true }
rand = { version = "0.9", default-features = false, features = ["std",
"std_rng", "thread_rng"], optional = true }
-half = { version = "2.1", default-features = false, optional = true }
+half = { version = "2.1", default-features = false, features = ["rand_distr"],
optional = true }
[package.metadata.docs.rs]
all-features = true
@@ -86,7 +86,7 @@ canonical_extension_types =
["arrow-schema/canonical_extension_types"]
[dev-dependencies]
chrono = { workspace = true }
criterion = { workspace = true, default-features = false }
-half = { version = "2.1", default-features = false }
+half = { version = "2.1", default-features = false, features = ["rand_distr"] }
rand = { version = "0.9", default-features = false, features = ["std",
"std_rng", "thread_rng"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
# used in examples
diff --git a/arrow/src/util/bench_util.rs b/arrow/src/util/bench_util.rs
index bcf7a559e9..fefb9077b1 100644
--- a/arrow/src/util/bench_util.rs
+++ b/arrow/src/util/bench_util.rs
@@ -722,7 +722,7 @@ pub fn create_f16_array(size: usize, nan_density: f32) ->
Float16Array {
if rng.random::<f32>() < nan_density {
Some(f16::NAN)
} else {
- Some(f16::from_f32(rng.random()))
+ Some(rng.random())
}
})
.collect()
diff --git a/arrow/src/util/data_gen.rs b/arrow/src/util/data_gen.rs
index 023436e0a7..e54ab34994 100644
--- a/arrow/src/util/data_gen.rs
+++ b/arrow/src/util/data_gen.rs
@@ -92,11 +92,7 @@ pub fn create_random_array(
UInt16 => Arc::new(create_primitive_array::<UInt16Type>(size,
null_density)),
UInt32 => Arc::new(create_primitive_array::<UInt32Type>(size,
null_density)),
UInt64 => Arc::new(create_primitive_array::<UInt64Type>(size,
null_density)),
- Float16 => {
- return Err(ArrowError::NotYetImplemented(
- "Float16 is not implemented".to_string(),
- ));
- }
+ Float16 => Arc::new(create_primitive_array::<Float16Type>(size,
null_density)),
Float32 => Arc::new(create_primitive_array::<Float32Type>(size,
null_density)),
Float64 => Arc::new(create_primitive_array::<Float64Type>(size,
null_density)),
Timestamp(unit, tz) => match unit {
@@ -545,6 +541,7 @@ mod tests {
let size = 32;
let fields = vec![
Field::new("a", DataType::Int32, true),
+ Field::new("f16", DataType::Float16, true),
Field::new(
"timestamp_without_timezone",
DataType::Timestamp(TimeUnit::Nanosecond, None),