| 444 | } |
| 445 | |
| 446 | fn build_map_column( |
| 447 | records: &[Value], |
| 448 | name: &str, |
| 449 | map_type: &MapType, |
| 450 | num_rows: usize, |
| 451 | ) -> crate::Result<Arc<dyn arrow_array::Array>> { |
| 452 | let arrow_key_type = crate::arrow::paimon_type_to_arrow(map_type.key_type())?; |
| 453 | let arrow_value_type = crate::arrow::paimon_type_to_arrow(map_type.value_type())?; |
| 454 | |
| 455 | let idx = field_index(records, name); |
| 456 | let mut offsets = vec![0i32]; |
| 457 | let mut key_records: Vec<Value> = Vec::new(); |
| 458 | let mut value_records: Vec<Value> = Vec::new(); |
| 459 | |
| 460 | for record in records.iter().take(num_rows) { |
| 461 | match get_field_at(record, idx) { |
| 462 | Some(Value::Map(map)) => { |
| 463 | for (k, v) in map { |
| 464 | key_records.push(Value::Record(vec![( |
| 465 | "key".to_string(), |
| 466 | Value::String(k.clone()), |
| 467 | )])); |
| 468 | value_records.push(Value::Record(vec![("value".to_string(), v.clone())])); |
| 469 | } |
| 470 | offsets.push(offsets.last().unwrap() + map.len() as i32); |
| 471 | } |
| 472 | _ => { |
| 473 | offsets.push(*offsets.last().unwrap()); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | let key_col = build_column(&key_records, "key", map_type.key_type(), key_records.len())?; |
| 479 | let value_col = build_column( |
| 480 | &value_records, |
| 481 | "value", |
| 482 | map_type.value_type(), |
| 483 | value_records.len(), |
| 484 | )?; |
| 485 | |
| 486 | let struct_arr = StructArray::try_new( |
| 487 | vec![ |
| 488 | Arc::new(arrow_schema::Field::new("key", arrow_key_type, false)), |
| 489 | Arc::new(arrow_schema::Field::new( |
| 490 | "value", |
| 491 | arrow_value_type.clone(), |
| 492 | map_type.value_type().is_nullable(), |
| 493 | )), |
| 494 | ] |
| 495 | .into(), |
| 496 | vec![key_col, value_col], |
| 497 | None, |
| 498 | ) |
| 499 | .map_err(|e| Error::UnexpectedError { |
| 500 | message: format!("Failed to build map StructArray: {e}"), |
| 501 | source: Some(Box::new(e)), |
| 502 | })?; |
| 503 | |