This is an automated email from the ASF dual-hosted git repository.
viirya pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 5edc954a9 Creates PrimitiveDictionaryBuilder from provided keys and
values builders (#3777)
5edc954a9 is described below
commit 5edc954a939c129e1386c2ac19add45e6fcdc9cb
Author: Liang-Chi Hsieh <[email protected]>
AuthorDate: Wed Mar 1 00:07:25 2023 -0800
Creates PrimitiveDictionaryBuilder from provided keys and values builders
(#3777)
* Creates PrimitiveDictionaryBuilder from provided keys and values builders
* Panics the function if provided builder is not empty
---
.../src/builder/primitive_dictionary_builder.rs | 37 +++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
diff --git a/arrow-array/src/builder/primitive_dictionary_builder.rs
b/arrow-array/src/builder/primitive_dictionary_builder.rs
index 00187cdde..742c09d8c 100644
--- a/arrow-array/src/builder/primitive_dictionary_builder.rs
+++ b/arrow-array/src/builder/primitive_dictionary_builder.rs
@@ -113,6 +113,26 @@ where
}
}
+ /// Creates a new `PrimitiveDictionaryBuilder` from the provided keys and
values builders.
+ ///
+ /// # Panics
+ ///
+ /// This method panics if `keys_builder` or `values_builder` is not empty.
+ pub fn new_from_builders(
+ keys_builder: PrimitiveBuilder<K>,
+ values_builder: PrimitiveBuilder<V>,
+ ) -> Self {
+ assert!(
+ keys_builder.is_empty() && values_builder.is_empty(),
+ "keys and values builders must be empty"
+ );
+ Self {
+ keys_builder,
+ values_builder,
+ map: HashMap::new(),
+ }
+ }
+
/// Creates a new `PrimitiveDictionaryBuilder` with the provided capacities
///
/// `keys_capacity`: the number of keys, i.e. length of array to build
@@ -276,7 +296,8 @@ mod tests {
use crate::array::Array;
use crate::array::UInt32Array;
use crate::array::UInt8Array;
- use crate::types::{Int32Type, UInt32Type, UInt8Type};
+ use crate::builder::Decimal128Builder;
+ use crate::types::{Decimal128Type, Int32Type, UInt32Type, UInt8Type};
#[test]
fn test_primitive_dictionary_builder() {
@@ -329,4 +350,18 @@ mod tests {
// Special error if the key overflows (256th entry)
builder.append(1257).unwrap();
}
+
+ #[test]
+ fn test_primitive_dictionary_with_builders() {
+ let keys_builder = PrimitiveBuilder::<Int32Type>::new();
+ let values_builder =
+ Decimal128Builder::new().with_data_type(DataType::Decimal128(1,
2));
+ let mut builder =
+ PrimitiveDictionaryBuilder::<Int32Type,
Decimal128Type>::new_from_builders(
+ keys_builder,
+ values_builder,
+ );
+ let dict_array = builder.finish();
+ assert_eq!(dict_array.value_type(), DataType::Decimal128(1, 2));
+ }
}