(
source_col: &ArrayRef,
source_key_type: &DataType,
target_key_type: &DataType,
target_value_type: &DataType,
cast_options: &CastOptions,
)
| 265 | } |
| 266 | |
| 267 | fn cast_dictionary_column( |
| 268 | source_col: &ArrayRef, |
| 269 | source_key_type: &DataType, |
| 270 | target_key_type: &DataType, |
| 271 | target_value_type: &DataType, |
| 272 | cast_options: &CastOptions, |
| 273 | ) -> Result<ArrayRef> { |
| 274 | // Dispatch on source key type to access keys/values, then recursively |
| 275 | // cast values. Rebuild with the source key type first. |
| 276 | macro_rules! cast_dict_values { |
| 277 | ($t:ty) => {{ |
| 278 | let source_dict = source_col |
| 279 | .as_any() |
| 280 | .downcast_ref::<DictionaryArray<$t>>() |
| 281 | .expect("downcast must succeed"); |
| 282 | let cast_values = |
| 283 | cast_column(source_dict.values(), target_value_type, cast_options)?; |
| 284 | Ok(Arc::new(DictionaryArray::<$t>::new( |
| 285 | source_dict.keys().clone(), |
| 286 | cast_values, |
| 287 | )) as ArrayRef) |
| 288 | }}; |
| 289 | } |
| 290 | |
| 291 | let result: Result<ArrayRef> = downcast_integer! { |
| 292 | source_key_type => (cast_dict_values), |
| 293 | k => _plan_err!("Unsupported dictionary key type: {k}") |
| 294 | }; |
| 295 | let result = result?; |
| 296 | |
| 297 | // If key types differ, delegate key casting to Arrow. |
| 298 | if source_key_type != target_key_type { |
| 299 | let target_dict_type = DataType::Dictionary( |
| 300 | Box::new(target_key_type.clone()), |
| 301 | Box::new(target_value_type.clone()), |
| 302 | ); |
| 303 | Ok(cast_with_options(&result, &target_dict_type, cast_options)?) |
| 304 | } else { |
| 305 | Ok(result) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /// Validates compatibility between source and target struct fields for casting operations. |
| 310 | /// |
no test coverage detected
searching dependent graphs…