Create a `DictionaryArray` from the provided values array. Each element gets a unique key (`0..N-1`), without deduplication. Useful for wrapping arrays in dictionary form. # Input ["alice", "bob", "alice", null, "carol"] # Output `DictionaryArray ` { keys: [0, 1, 2, 3, 4], values: ["alice", "bob", "alice", null, "carol"] }
(
values_array: ArrayRef,
)
| 1092 | /// values: ["alice", "bob", "alice", null, "carol"] |
| 1093 | /// } |
| 1094 | pub fn dict_from_values<K: ArrowDictionaryKeyType>( |
| 1095 | values_array: ArrayRef, |
| 1096 | ) -> Result<ArrayRef> { |
| 1097 | // Create a key array with `size` elements of 0..array_len for all |
| 1098 | // non-null value elements |
| 1099 | let key_array: PrimitiveArray<K> = (0..values_array.len()) |
| 1100 | .map(|index| { |
| 1101 | if values_array.is_valid(index) { |
| 1102 | let native_index = K::Native::from_usize(index).ok_or_else(|| { |
| 1103 | _internal_datafusion_err!( |
| 1104 | "Can not create index of type {} from value {index}", |
| 1105 | K::DATA_TYPE |
| 1106 | ) |
| 1107 | })?; |
| 1108 | Ok(Some(native_index)) |
| 1109 | } else { |
| 1110 | Ok(None) |
| 1111 | } |
| 1112 | }) |
| 1113 | .collect::<Result<Vec<_>>>()? |
| 1114 | .into_iter() |
| 1115 | .collect(); |
| 1116 | |
| 1117 | // create a new DictionaryArray |
| 1118 | // |
| 1119 | // Note: this path could be made faster by using the ArrayData |
| 1120 | // APIs and skipping validation, if it every comes up in |
| 1121 | // performance traces. |
| 1122 | let dict_array = DictionaryArray::<K>::try_new(key_array, values_array)?; |
| 1123 | Ok(Arc::new(dict_array)) |
| 1124 | } |
| 1125 | |
| 1126 | macro_rules! typed_cast_tz { |
| 1127 | ($array:expr, $index:expr, $array_cast:ident, $SCALAR:ident, $TZ:expr) => {{ |