Construct a [`DictionaryArray`] from a partially typed JSON column
(
field: &Field,
json_col: ArrowJsonColumn,
dict_key: &DataType,
dict_value: &DataType,
dictionary: &ArrowJsonDictionaryBatch,
dictionaries: Option<&HashMap<i64, ArrowJsonDicti
| 1110 | |
| 1111 | /// Construct a [`DictionaryArray`] from a partially typed JSON column |
| 1112 | pub fn dictionary_array_from_json( |
| 1113 | field: &Field, |
| 1114 | json_col: ArrowJsonColumn, |
| 1115 | dict_key: &DataType, |
| 1116 | dict_value: &DataType, |
| 1117 | dictionary: &ArrowJsonDictionaryBatch, |
| 1118 | dictionaries: Option<&HashMap<i64, ArrowJsonDictionaryBatch>>, |
| 1119 | ) -> Result<ArrayRef> { |
| 1120 | match dict_key { |
| 1121 | DataType::Int8 |
| 1122 | | DataType::Int16 |
| 1123 | | DataType::Int32 |
| 1124 | | DataType::Int64 |
| 1125 | | DataType::UInt8 |
| 1126 | | DataType::UInt16 |
| 1127 | | DataType::UInt32 |
| 1128 | | DataType::UInt64 => { |
| 1129 | let null_buf = create_null_buf(&json_col); |
| 1130 | |
| 1131 | // build the key data into a buffer, then construct values separately |
| 1132 | #[allow(deprecated)] |
| 1133 | let key_field = Field::new_dict( |
| 1134 | "key", |
| 1135 | dict_key.clone(), |
| 1136 | field.is_nullable(), |
| 1137 | #[allow(deprecated)] |
| 1138 | field |
| 1139 | .dict_id() |
| 1140 | .expect("Dictionary fields must have a dict_id value"), |
| 1141 | field |
| 1142 | .dict_is_ordered() |
| 1143 | .expect("Dictionary fields must have a dict_is_ordered value"), |
| 1144 | ); |
| 1145 | let keys = array_from_json(&key_field, json_col, None)?; |
| 1146 | // note: not enough info on nullability of dictionary |
| 1147 | let value_field = Field::new("value", dict_value.clone(), true); |
| 1148 | let values = array_from_json( |
| 1149 | &value_field, |
| 1150 | dictionary.data.columns[0].clone(), |
| 1151 | dictionaries, |
| 1152 | )?; |
| 1153 | |
| 1154 | // convert key and value to dictionary data |
| 1155 | let dict_data = ArrayData::builder(field.data_type().clone()) |
| 1156 | .len(keys.len()) |
| 1157 | .add_buffer(keys.to_data().buffers()[0].clone()) |
| 1158 | .null_bit_buffer(Some(null_buf)) |
| 1159 | .add_child_data(values.into_data()) |
| 1160 | .build() |
| 1161 | .unwrap(); |
| 1162 | |
| 1163 | let array = match dict_key { |
| 1164 | DataType::Int8 => Arc::new(Int8DictionaryArray::from(dict_data)) as ArrayRef, |
| 1165 | DataType::Int16 => Arc::new(Int16DictionaryArray::from(dict_data)), |
| 1166 | DataType::Int32 => Arc::new(Int32DictionaryArray::from(dict_data)), |
| 1167 | DataType::Int64 => Arc::new(Int64DictionaryArray::from(dict_data)), |
| 1168 | DataType::UInt8 => Arc::new(UInt8DictionaryArray::from(dict_data)), |
| 1169 | DataType::UInt16 => Arc::new(UInt16DictionaryArray::from(dict_data)), |
no test coverage detected