Extends builder with dictionary This is the same as [`Self::extend`] but is faster as it translates the dictionary values once rather than doing a lookup for each item in the iterator when dictionary values are null (the actual mapped values) the keys are null
(
&mut self,
dictionary: &TypedDictionaryArray<K, PrimitiveArray<V>>,
)
| 381 | /// when dictionary values are null (the actual mapped values) the keys are null |
| 382 | /// |
| 383 | pub fn extend_dictionary( |
| 384 | &mut self, |
| 385 | dictionary: &TypedDictionaryArray<K, PrimitiveArray<V>>, |
| 386 | ) -> Result<(), ArrowError> { |
| 387 | let values = dictionary.values(); |
| 388 | |
| 389 | let v_len = values.len(); |
| 390 | let k_len = dictionary.keys().len(); |
| 391 | if v_len == 0 && k_len == 0 { |
| 392 | return Ok(()); |
| 393 | } |
| 394 | |
| 395 | // All nulls |
| 396 | if v_len == 0 { |
| 397 | self.append_nulls(k_len); |
| 398 | return Ok(()); |
| 399 | } |
| 400 | |
| 401 | if k_len == 0 { |
| 402 | return Err(ArrowError::InvalidArgumentError( |
| 403 | "Dictionary keys should not be empty when values are not empty".to_string(), |
| 404 | )); |
| 405 | } |
| 406 | |
| 407 | // Orphan values will be carried over to the new dictionary |
| 408 | let mapped_values = values |
| 409 | .iter() |
| 410 | // Dictionary values can technically be null, so we need to handle that |
| 411 | .map(|dict_value| { |
| 412 | dict_value |
| 413 | .map(|dict_value| self.get_or_insert_key(dict_value)) |
| 414 | .transpose() |
| 415 | }) |
| 416 | .collect::<Result<Vec<_>, _>>()?; |
| 417 | |
| 418 | // Just insert the keys without additional lookups |
| 419 | dictionary.keys().iter().for_each(|key| match key { |
| 420 | None => self.append_null(), |
| 421 | Some(original_dict_index) => { |
| 422 | let index = original_dict_index.as_usize().min(v_len - 1); |
| 423 | match mapped_values[index] { |
| 424 | None => self.append_null(), |
| 425 | Some(mapped_value) => self.keys_builder.append_value(mapped_value), |
| 426 | } |
| 427 | } |
| 428 | }); |
| 429 | |
| 430 | Ok(()) |
| 431 | } |
| 432 | |
| 433 | /// Builds the `DictionaryArray` and reset this builder. |
| 434 | pub fn finish(&mut self) -> DictionaryArray<K> { |