vertexclique commented on a change in pull request #7226:
URL: https://github.com/apache/arrow/pull/7226#discussion_r428057479
##########
File path: rust/arrow/src/array/builder.rs
##########
@@ -1334,6 +1334,58 @@ where
map: HashMap::new(),
}
}
+
+ /// Creates a new `StringDictionaryBuilder` from a keys builder and a
dictionary initialized with the given values.
+ /// The indices of those dictionary values are used as keys.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use arrow::datatypes::Int16Type;
+ /// use arrow::array::{StringArray, StringDictionaryBuilder,
PrimitiveBuilder};
+ /// use std::convert::TryFrom;
+ ///
+ /// let dictionary_values = StringArray::try_from(vec![None, Some("abc"),
Some("def")]).unwrap();
+ ///
+ /// let mut builder =
StringDictionaryBuilder::new_with_dictionary(PrimitiveBuilder::<Int16Type>::new(3),
&dictionary_values).unwrap();
+ /// builder.append("def").unwrap();
+ /// builder.append_null().unwrap();
+ /// builder.append("abc").unwrap();
+ ///
+ /// let dictionary_array = builder.finish();
+ ///
+ /// let keys: Vec<Option<i16>> = dictionary_array.keys().collect();
+ ///
+ /// assert_eq!(keys, vec![Some(2), None, Some(1)]);
Review comment:
assert is off by one space.
##########
File path: rust/arrow/src/array/builder.rs
##########
@@ -1334,6 +1334,58 @@ where
map: HashMap::new(),
}
}
+
+ /// Creates a new `StringDictionaryBuilder` from a keys builder and a
dictionary initialized with the given values.
+ /// The indices of those dictionary values are used as keys.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use arrow::datatypes::Int16Type;
+ /// use arrow::array::{StringArray, StringDictionaryBuilder,
PrimitiveBuilder};
+ /// use std::convert::TryFrom;
+ ///
+ /// let dictionary_values = StringArray::try_from(vec![None, Some("abc"),
Some("def")]).unwrap();
+ ///
+ /// let mut builder =
StringDictionaryBuilder::new_with_dictionary(PrimitiveBuilder::<Int16Type>::new(3),
&dictionary_values).unwrap();
+ /// builder.append("def").unwrap();
+ /// builder.append_null().unwrap();
+ /// builder.append("abc").unwrap();
+ ///
+ /// let dictionary_array = builder.finish();
+ ///
+ /// let keys: Vec<Option<i16>> = dictionary_array.keys().collect();
+ ///
+ /// assert_eq!(keys, vec![Some(2), None, Some(1)]);
+ /// ```
+ pub fn new_with_dictionary(
+ keys_builder: PrimitiveBuilder<K>,
+ dictionary_values: &StringArray,
+ ) -> Result<Self> {
+ let mut values_builder = StringBuilder::with_capacity(
+ dictionary_values.len(),
+ dictionary_values.value_data().len(),
+ );
+ let mut map: HashMap<Box<[u8]>, K::Native> = HashMap::new();
Review comment:
```suggestion
let mut map: HashMap<Box<[u8]>, K::Native> =
HashMap::with_capacity(dictionary_values.len());
```
##########
File path: rust/arrow/src/array/builder.rs
##########
@@ -1334,6 +1334,58 @@ where
map: HashMap::new(),
}
}
+
+ /// Creates a new `StringDictionaryBuilder` from a keys builder and a
dictionary initialized with the given values.
+ /// The indices of those dictionary values are used as keys.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use arrow::datatypes::Int16Type;
+ /// use arrow::array::{StringArray, StringDictionaryBuilder,
PrimitiveBuilder};
+ /// use std::convert::TryFrom;
+ ///
+ /// let dictionary_values = StringArray::try_from(vec![None, Some("abc"),
Some("def")]).unwrap();
+ ///
+ /// let mut builder =
StringDictionaryBuilder::new_with_dictionary(PrimitiveBuilder::<Int16Type>::new(3),
&dictionary_values).unwrap();
+ /// builder.append("def").unwrap();
+ /// builder.append_null().unwrap();
+ /// builder.append("abc").unwrap();
+ ///
+ /// let dictionary_array = builder.finish();
+ ///
+ /// let keys: Vec<Option<i16>> = dictionary_array.keys().collect();
+ ///
+ /// assert_eq!(keys, vec![Some(2), None, Some(1)]);
+ /// ```
+ pub fn new_with_dictionary(
+ keys_builder: PrimitiveBuilder<K>,
+ dictionary_values: &StringArray,
+ ) -> Result<Self> {
+ let mut values_builder = StringBuilder::with_capacity(
+ dictionary_values.len(),
+ dictionary_values.value_data().len(),
+ );
+ let mut map: HashMap<Box<[u8]>, K::Native> = HashMap::new();
+ for i in 0..dictionary_values.len() {
Review comment:
maybe better.
```suggestion
0..dictionary_values.len()
.for_each(|i| {
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]