Validates compatibility between source and target struct fields for casting operations. This function implements comprehensive struct compatibility checking by examining: - Field name matching between source and target structs - Type castability for each matching field (including recursive struct validation) - Proper handling of missing fields (target fields not in source are allowed - filled wit
(
source_fields: &[FieldRef],
target_fields: &[FieldRef],
)
| 343 | /// ``` |
| 344 | /// |
| 345 | pub fn validate_struct_compatibility( |
| 346 | source_fields: &[FieldRef], |
| 347 | target_fields: &[FieldRef], |
| 348 | ) -> Result<()> { |
| 349 | let has_overlap = has_one_of_more_common_fields(source_fields, target_fields); |
| 350 | if !has_overlap { |
| 351 | return _plan_err!( |
| 352 | "Cannot cast struct with {} fields to {} fields because there is no field name overlap", |
| 353 | source_fields.len(), |
| 354 | target_fields.len() |
| 355 | ); |
| 356 | } |
| 357 | |
| 358 | // Check compatibility for each target field |
| 359 | for target_field in target_fields { |
| 360 | // Look for matching field in source by name |
| 361 | if let Some(source_field) = source_fields |
| 362 | .iter() |
| 363 | .find(|f| f.name() == target_field.name()) |
| 364 | { |
| 365 | validate_field_compatibility(source_field, target_field)?; |
| 366 | } else { |
| 367 | // Target field is missing from source |
| 368 | // If it's non-nullable, we cannot fill it with NULL |
| 369 | if !target_field.is_nullable() { |
| 370 | return _plan_err!( |
| 371 | "Cannot cast struct: target field '{}' is non-nullable but missing from source. \ |
| 372 | Cannot fill with NULL.", |
| 373 | target_field.name() |
| 374 | ); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Extra fields in source are OK - they'll be ignored |
| 380 | Ok(()) |
| 381 | } |
| 382 | |
| 383 | fn validate_field_compatibility( |
| 384 | source_field: &Field, |
searching dependent graphs…