-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
87 lines (67 loc) · 2.35 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::io::Read;
use std::{collections::HashMap, fs::File};
use arrow2::{
array::{Array, StructArray},
chunk::Chunk,
datatypes::Schema,
io::ipc::write::WriteOptions,
};
use arrow2_convert::{serialize::TryIntoArrow, ArrowField};
use serde::Deserialize;
// Types for which I want to use arrow2-convert
pub type AssignmentValue = f64;
#[derive(Debug, Clone, ArrowField, Deserialize)]
pub struct Assignment {
pub name: String,
pub value: AssignmentValue,
}
#[derive(Debug, Clone, ArrowField, Deserialize)]
pub struct Action {
pub target: Assignment,
pub assignments: Vec<Assignment>,
}
pub type Plan = Vec<Action>;
pub type State = Vec<Assignment>;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// read into Plan
let mut file = File::open("data/plan.yaml")?;
let mut contents = vec![];
file.read_to_end(&mut contents).unwrap();
let plan: Plan = serde_yaml::from_slice(&contents)?;
// infer schema by construting default Plan
let default = Plan::default();
let default_arrow_array: Box<dyn Array> = default.try_into_arrow()?;
let datatype = default_arrow_array.data_type();
let fields = StructArray::get_fields(&datatype);
let schema = Schema::from(fields.to_vec());
// into arrow
let plan: Box<dyn Array> = plan.try_into_arrow()?;
// downcast to StructArray
let struct_plan = plan
.as_any()
.downcast_ref::<arrow2::array::StructArray>()
.unwrap();
// unsure how to get rid of this...
let struct_plan = struct_plan.clone();
let chunk = Chunk::new(vec![struct_plan.arced()]);
let options = WriteOptions::default();
// Serialization
let serialized_schema = arrow2::io::flight::serialize_schema(&schema, None);
let (_, serialized_batch) = arrow2::io::flight::serialize_batch(&chunk, &[], &options);
// ... network ...
// Deserialization
let (schema, ipc_schema) =
arrow2::io::flight::deserialize_schemas(&serialized_schema.data_header)?;
let dictionaries_by_field = HashMap::new();
let chunk = match arrow2::io::flight::deserialize_batch(
&serialized_batch,
&schema.fields,
&ipc_schema,
&dictionaries_by_field,
) {
Ok(chunk) => chunk,
Err(e) => panic!("not able to deserialize message: {:#?}", e),
};
println!("chunk: :{:#?}", chunk);
Ok(())
}