Skip to content

Commit e60cbdf

Browse files
committed
add one level of nesting so we can have nulls in the map properties
1 parent 974a5ac commit e60cbdf

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

raphtory-api/src/core/entities/properties/prop/prop_enum.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use crate::core::{
55
},
66
storage::arc_str::ArcStr,
77
};
8+
use arrow_array::cast::AsArray;
89
#[cfg(feature = "arrow")]
910
use arrow_array::StructArray;
10-
use arrow_schema::DataType;
11+
use arrow_schema::{DataType, Field, FieldRef};
1112
use bigdecimal::{num_bigint::BigInt, BigDecimal};
1213
use chrono::{DateTime, NaiveDateTime, Utc};
1314
use itertools::Itertools;
@@ -161,11 +162,16 @@ impl PartialOrd for Prop {
161162
}
162163

163164
pub struct SerdeProp<'a>(pub &'a Prop);
164-
pub struct SedeList<'a>(pub &'a Vec<Prop>);
165+
pub struct SerdeList<'a>(pub &'a Vec<Prop>);
165166
#[derive(Clone, Copy)]
166167
pub struct SerdeMap<'a>(pub &'a HashMap<ArcStr, Prop, FxBuildHasher>);
167168

168-
impl<'a> Serialize for SedeList<'a> {
169+
#[derive(Clone, Copy, Serialize)]
170+
pub struct SerdeRow<'a> {
171+
value: Option<SerdeMap<'a>>,
172+
}
173+
174+
impl<'a> Serialize for SerdeList<'a> {
169175
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
170176
where
171177
S: serde::Serializer,
@@ -209,7 +215,7 @@ impl<'a> Serialize for SerdeProp<'a> {
209215
Prop::Bool(b) => serializer.serialize_bool(*b),
210216
Prop::DTime(dt) => serializer.serialize_i64(dt.timestamp_millis()),
211217
Prop::NDTime(dt) => serializer.serialize_i64(dt.and_utc().timestamp_millis()),
212-
Prop::List(l) => SedeList(l).serialize(serializer),
218+
Prop::List(l) => SerdeList(l).serialize(serializer),
213219
Prop::Map(m) => SerdeMap(m).serialize(serializer),
214220
Prop::Decimal(dec) => serializer.serialize_str(&dec.to_string()),
215221
_ => {
@@ -355,31 +361,23 @@ pub fn struct_array_from_props<P>(
355361
) -> StructArray {
356362
use serde_arrow::ArrayBuilder;
357363

358-
let fields = match dt {
359-
DataType::Struct(fields) => fields,
360-
_ => panic!("Expected DataType::Struct, got {:?}", dt),
361-
};
364+
let fields = [FieldRef::new(Field::new("value", dt.clone(), true))];
362365

363-
let mut builder = ArrayBuilder::from_arrow(fields)
366+
let mut builder = ArrayBuilder::from_arrow(&fields)
364367
.unwrap_or_else(|e| panic!("Failed to make array builder {e}"));
365368

366-
let empty_map = FxHashMap::default();
367-
368369
for p in props {
369-
match p.as_ref().map(as_serde_map) {
370-
Some(map) => builder
371-
.push(map)
372-
.unwrap_or_else(|e| panic!("Failed to push map to array builder {e}")),
373-
_ => builder
374-
.push(SerdeMap(&empty_map))
375-
.unwrap_or_else(|e| panic!("Failed to push empty map to array builder {e}")),
376-
}
370+
builder
371+
.push(SerdeRow {
372+
value: p.as_ref().map(as_serde_map),
373+
})
374+
.unwrap_or_else(|e| panic!("Failed to push map to array builder {e}"))
377375
}
378376

379377
let arrays = builder
380378
.to_arrow()
381379
.unwrap_or_else(|e| panic!("Failed to convert to arrow array {e}"));
382-
StructArray::new(fields.clone(), arrays, None)
380+
arrays.first().unwrap().as_struct().clone()
383381
}
384382

385383
impl Display for Prop {

raphtory-api/src/core/entities/properties/prop/prop_ref_enum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hash::FxHashMap;
99
#[cfg(feature = "arrow")]
1010
use crate::core::entities::properties::prop::PropArray;
1111
use crate::core::{
12-
entities::properties::prop::{ArrowRow, Prop, SedeList, SerdeMap},
12+
entities::properties::prop::{ArrowRow, Prop, SerdeList, SerdeMap},
1313
storage::arc_str::ArcStr,
1414
};
1515

@@ -197,7 +197,7 @@ impl<'a> Serialize for PropRef<'a> {
197197
PropNum::F64(v) => serializer.serialize_f64(*v),
198198
},
199199
PropRef::Bool(b) => serializer.serialize_bool(*b),
200-
PropRef::List(lst) => SedeList(lst).serialize(serializer),
200+
PropRef::List(lst) => SerdeList(lst).serialize(serializer),
201201
PropRef::Map(map_ref) => map_ref.serialize(serializer),
202202
PropRef::NDTime(dt) => serializer.serialize_i64(dt.and_utc().timestamp_millis()),
203203
PropRef::DTime(dt) => serializer.serialize_i64(dt.timestamp_millis()),

0 commit comments

Comments
 (0)