|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use arrow_schema::{DataType, Field, Schema}; |
| 4 | +use typed_arrow_dyn::{DynBuilders, DynCell, DynProjection, DynRow, DynSchema}; |
| 5 | + |
| 6 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 7 | + // schema: { id: Int64, profile: Struct{name: Utf8, age: Int32?}, tags: LargeList<Utf8?> } |
| 8 | + let profile_fields = vec![ |
| 9 | + Arc::new(Field::new("name", DataType::Utf8, false)), |
| 10 | + Arc::new(Field::new("age", DataType::Int32, true)), |
| 11 | + ]; |
| 12 | + let schema = Arc::new(Schema::new(vec![ |
| 13 | + Field::new("id", DataType::Int64, false), |
| 14 | + Field::new("profile", DataType::Struct(profile_fields.into()), true), |
| 15 | + Field::new( |
| 16 | + "tags", |
| 17 | + DataType::LargeList(Arc::new(Field::new("item", DataType::Utf8, true))), |
| 18 | + false, |
| 19 | + ), |
| 20 | + ])); |
| 21 | + |
| 22 | + // Build the batch using dynamic rows. |
| 23 | + let mut builders = DynBuilders::new(Arc::clone(&schema), 3); |
| 24 | + builders.append_option_row(Some(DynRow(vec![ |
| 25 | + Some(DynCell::I64(1)), |
| 26 | + Some(DynCell::Struct(vec![ |
| 27 | + Some(DynCell::Str("alice".into())), |
| 28 | + Some(DynCell::I32(34)), |
| 29 | + ])), |
| 30 | + Some(DynCell::List(vec![ |
| 31 | + Some(DynCell::Str("rust".into())), |
| 32 | + Some(DynCell::Str("arrow".into())), |
| 33 | + ])), |
| 34 | + ])))?; |
| 35 | + builders.append_option_row(Some(DynRow(vec![ |
| 36 | + Some(DynCell::I64(2)), |
| 37 | + None, |
| 38 | + Some(DynCell::List(vec![ |
| 39 | + Some(DynCell::Str("analytics".into())), |
| 40 | + None, |
| 41 | + ])), |
| 42 | + ])))?; |
| 43 | + builders.append_option_row(Some(DynRow(vec![ |
| 44 | + Some(DynCell::I64(3)), |
| 45 | + Some(DynCell::Struct(vec![ |
| 46 | + Some(DynCell::Str("carol".into())), |
| 47 | + None, |
| 48 | + ])), |
| 49 | + Some(DynCell::List(vec![])), |
| 50 | + ])))?; |
| 51 | + let batch = builders.try_finish_into_batch()?; |
| 52 | + |
| 53 | + // Iterate over borrowed views with zero-copy access. |
| 54 | + let dyn_schema = DynSchema::from_ref(Arc::clone(&schema)); |
| 55 | + for row in dyn_schema.iter_views(&batch)? { |
| 56 | + let row = row?; |
| 57 | + |
| 58 | + let id = row |
| 59 | + .get(0)? |
| 60 | + .and_then(|cell| cell.into_i64()) |
| 61 | + .expect("id column must be i64"); |
| 62 | + |
| 63 | + let name = row |
| 64 | + .get_by_name("profile") |
| 65 | + .and_then(|res| res.ok()) |
| 66 | + .and_then(|opt| opt) |
| 67 | + .and_then(|cell| cell.into_struct()) |
| 68 | + .and_then(|profile| { |
| 69 | + profile |
| 70 | + .get(0) |
| 71 | + .ok() |
| 72 | + .and_then(|opt| opt) |
| 73 | + .and_then(|cell| cell.into_str()) |
| 74 | + .map(str::to_owned) |
| 75 | + }) |
| 76 | + .unwrap_or_else(|| "<anonymous>".to_string()); |
| 77 | + |
| 78 | + let mut tags = Vec::new(); |
| 79 | + if let Some(list) = row |
| 80 | + .get_by_name("tags") |
| 81 | + .and_then(|res| res.ok()) |
| 82 | + .and_then(|opt| opt) |
| 83 | + .and_then(|cell| cell.into_list()) |
| 84 | + { |
| 85 | + for idx in 0..list.len() { |
| 86 | + let entry = list.get(idx)?; |
| 87 | + tags.push(entry.and_then(|cell| cell.into_str().map(str::to_owned))); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + println!("id={id} name={name} tags={tags:?}"); |
| 92 | + } |
| 93 | + |
| 94 | + // Project down to just `id` and `tags` and iterate lazily. |
| 95 | + let projection_schema = Schema::new(vec![ |
| 96 | + Field::new("id", DataType::Int64, false), |
| 97 | + Field::new( |
| 98 | + "tags", |
| 99 | + DataType::LargeList(Arc::new(Field::new("item", DataType::Utf8, true))), |
| 100 | + false, |
| 101 | + ), |
| 102 | + ]); |
| 103 | + let projection = DynProjection::from_schema(schema.as_ref(), &projection_schema)?; |
| 104 | + let mut projected = dyn_schema.iter_views(&batch)?.project(projection)?; |
| 105 | + |
| 106 | + println!("-- projected columns --"); |
| 107 | + while let Some(row) = projected.next() { |
| 108 | + let row = row?; |
| 109 | + let id = row |
| 110 | + .get(0)? |
| 111 | + .and_then(|cell| cell.into_i64()) |
| 112 | + .expect("projected id"); |
| 113 | + let tags = row |
| 114 | + .get(1)? |
| 115 | + .and_then(|cell| cell.into_list()) |
| 116 | + .map(|list| list.len()) |
| 117 | + .unwrap_or(0); |
| 118 | + println!("id={id} tag_count={tags}"); |
| 119 | + } |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
0 commit comments