albertlockett commented on code in PR #7611:
URL: https://github.com/apache/arrow-rs/pull/7611#discussion_r2145389529


##########
arrow-array/src/builder/generic_bytes_dictionary_builder.rs:
##########
@@ -152,6 +155,65 @@ where
             values_builder,
         })
     }
+
+    /// Creates a new `GenericByteDictionaryBuilder` from the existing builder 
with the same
+    /// keys and values, but with a new data type for the keys.
+    ///
+    /// # example
+    /// ```
+    /// # use arrow_array::builder::StringDictionaryBuilder;
+    /// # use arrow_array::types::{UInt8Type, UInt16Type};
+    /// # use arrow_array::UInt16Array;;
+    ///
+    /// let mut u8_keyed_builder = StringDictionaryBuilder::<UInt8Type>::new();
+    /// u8_keyed_builder.append("def").unwrap();
+    /// u8_keyed_builder.append_null();
+    /// u8_keyed_builder.append("abc").unwrap();
+    ///
+    /// // for some reason, we decide we need to upgrade the key type
+    /// let mut u16_keyed_builder = 
StringDictionaryBuilder::<UInt16Type>::try_new_from_builder(u8_keyed_builder).unwrap();
+    /// let dictionary_array = u16_keyed_builder.finish();
+    /// let keys = dictionary_array.keys();
+    ///
+    /// assert_eq!(keys, &UInt16Array::from(vec![Some(0), None, Some(1)]));
+    /// ```
+    pub fn try_new_from_builder<K2>(
+        mut source: GenericByteDictionaryBuilder<K2, T>,
+    ) -> Result<Self, ArrowError>
+    where
+        K::Native: NumCast,
+        K2: ArrowDictionaryKeyType,
+        K2::Native: NumCast,
+    {
+        let state = source.state;
+        let dedup = source.dedup;
+        let values_builder = source.values_builder;
+
+        let source_keys = source.keys_builder.finish();
+        let new_keys: PrimitiveArray<K> = source_keys.try_unary(|value| {
+            num::cast::cast::<K2::Native, K::Native>(value).ok_or_else(|| {

Review Comment:
   Thanks Andrew!
   
   Created a follow up issue for this 
https://github.com/apache/arrow-rs/issues/7662 . You can assign this to me as 
well. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to