-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcommon_union.rs
321 lines (292 loc) · 10.7 KB
/
common_union.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use std::collections::HashMap;
use std::sync::{Arc, OnceLock};
use datafusion::arrow::array::{
Array, ArrayRef, AsArray, BooleanArray, Float64Array, Int64Array, NullArray, StringArray, UnionArray,
};
use datafusion::arrow::buffer::{Buffer, ScalarBuffer};
use datafusion::arrow::datatypes::{DataType, Field, UnionFields, UnionMode};
use datafusion::arrow::error::ArrowError;
use datafusion::common::ScalarValue;
pub fn is_json_union(data_type: &DataType) -> bool {
match data_type {
DataType::Union(fields, UnionMode::Sparse) => fields == &union_fields(),
_ => false,
}
}
/// Extract nested JSON from a `JsonUnion` `UnionArray`
///
/// # Arguments
/// * `array` - The `UnionArray` to extract the nested JSON from
/// * `object_lookup` - If `true`, extract from the "object" member of the union,
/// otherwise extract from the "array" member
pub(crate) fn nested_json_array(array: &ArrayRef, object_lookup: bool) -> Option<&StringArray> {
nested_json_array_ref(array, object_lookup).map(AsArray::as_string)
}
pub(crate) fn nested_json_array_ref(array: &ArrayRef, object_lookup: bool) -> Option<&ArrayRef> {
let union_array: &UnionArray = array.as_any().downcast_ref::<UnionArray>()?;
let type_id = if object_lookup { TYPE_ID_OBJECT } else { TYPE_ID_ARRAY };
Some(union_array.child(type_id))
}
/// Extract a JSON string from a `JsonUnion` scalar
pub(crate) fn json_from_union_scalar<'a>(
type_id_value: Option<&'a (i8, Box<ScalarValue>)>,
fields: &UnionFields,
) -> Option<&'a str> {
if let Some((type_id, value)) = type_id_value {
// we only want to take the ScalarValue string if the type_id indicates the value represents nested JSON
if fields == &union_fields() && (*type_id == TYPE_ID_ARRAY || *type_id == TYPE_ID_OBJECT) {
if let ScalarValue::Utf8(s) = value.as_ref() {
return s.as_deref();
}
}
}
None
}
#[derive(Debug)]
pub(crate) struct JsonUnion {
bools: Vec<Option<bool>>,
ints: Vec<Option<i64>>,
floats: Vec<Option<f64>>,
strings: Vec<Option<String>>,
arrays: Vec<Option<String>>,
objects: Vec<Option<String>>,
type_ids: Vec<i8>,
index: usize,
length: usize,
}
impl JsonUnion {
pub fn new(length: usize) -> Self {
Self {
bools: vec![None; length],
ints: vec![None; length],
floats: vec![None; length],
strings: vec![None; length],
arrays: vec![None; length],
objects: vec![None; length],
type_ids: vec![TYPE_ID_NULL; length],
index: 0,
length,
}
}
pub fn data_type() -> DataType {
DataType::Union(union_fields(), UnionMode::Sparse)
}
pub fn push(&mut self, field: JsonUnionField) {
self.type_ids[self.index] = field.type_id();
match field {
JsonUnionField::JsonNull => (),
JsonUnionField::Bool(value) => self.bools[self.index] = Some(value),
JsonUnionField::Int(value) => self.ints[self.index] = Some(value),
JsonUnionField::Float(value) => self.floats[self.index] = Some(value),
JsonUnionField::Str(value) => self.strings[self.index] = Some(value),
JsonUnionField::Array(value) => self.arrays[self.index] = Some(value),
JsonUnionField::Object(value) => self.objects[self.index] = Some(value),
}
self.index += 1;
debug_assert!(self.index <= self.length);
}
pub fn push_none(&mut self) {
self.index += 1;
debug_assert!(self.index <= self.length);
}
}
/// So we can do `collect::<JsonUnion>()`
impl FromIterator<Option<JsonUnionField>> for JsonUnion {
fn from_iter<I: IntoIterator<Item = Option<JsonUnionField>>>(iter: I) -> Self {
let inner = iter.into_iter();
let (lower, upper) = inner.size_hint();
let mut union = Self::new(upper.unwrap_or(lower));
for opt_field in inner {
if let Some(union_field) = opt_field {
union.push(union_field);
} else {
union.push_none();
}
}
union
}
}
impl TryFrom<JsonUnion> for UnionArray {
type Error = ArrowError;
fn try_from(value: JsonUnion) -> Result<Self, Self::Error> {
let children: Vec<Arc<dyn Array>> = vec![
Arc::new(NullArray::new(value.length)),
Arc::new(BooleanArray::from(value.bools)),
Arc::new(Int64Array::from(value.ints)),
Arc::new(Float64Array::from(value.floats)),
Arc::new(StringArray::from(value.strings)),
Arc::new(StringArray::from(value.arrays)),
Arc::new(StringArray::from(value.objects)),
];
UnionArray::try_new(union_fields(), Buffer::from_vec(value.type_ids).into(), None, children)
}
}
#[derive(Debug)]
pub(crate) enum JsonUnionField {
JsonNull,
Bool(bool),
Int(i64),
Float(f64),
Str(String),
Array(String),
Object(String),
}
pub(crate) const TYPE_ID_NULL: i8 = 0;
const TYPE_ID_BOOL: i8 = 1;
const TYPE_ID_INT: i8 = 2;
const TYPE_ID_FLOAT: i8 = 3;
const TYPE_ID_STR: i8 = 4;
const TYPE_ID_ARRAY: i8 = 5;
const TYPE_ID_OBJECT: i8 = 6;
fn union_fields() -> UnionFields {
static FIELDS: OnceLock<UnionFields> = OnceLock::new();
FIELDS
.get_or_init(|| {
let json_metadata: HashMap<String, String> =
HashMap::from_iter(vec![("is_json".to_string(), "true".to_string())]);
UnionFields::from_iter([
(TYPE_ID_NULL, Arc::new(Field::new("null", DataType::Null, true))),
(TYPE_ID_BOOL, Arc::new(Field::new("bool", DataType::Boolean, false))),
(TYPE_ID_INT, Arc::new(Field::new("int", DataType::Int64, false))),
(TYPE_ID_FLOAT, Arc::new(Field::new("float", DataType::Float64, false))),
(TYPE_ID_STR, Arc::new(Field::new("str", DataType::Utf8, false))),
(
TYPE_ID_ARRAY,
Arc::new(Field::new("array", DataType::Utf8, false).with_metadata(json_metadata.clone())),
),
(
TYPE_ID_OBJECT,
Arc::new(Field::new("object", DataType::Utf8, false).with_metadata(json_metadata.clone())),
),
])
})
.clone()
}
impl JsonUnionField {
fn type_id(&self) -> i8 {
match self {
Self::JsonNull => TYPE_ID_NULL,
Self::Bool(_) => TYPE_ID_BOOL,
Self::Int(_) => TYPE_ID_INT,
Self::Float(_) => TYPE_ID_FLOAT,
Self::Str(_) => TYPE_ID_STR,
Self::Array(_) => TYPE_ID_ARRAY,
Self::Object(_) => TYPE_ID_OBJECT,
}
}
pub fn scalar_value(f: Option<Self>) -> ScalarValue {
ScalarValue::Union(
f.map(|f| (f.type_id(), Box::new(f.into()))),
union_fields(),
UnionMode::Sparse,
)
}
}
impl From<JsonUnionField> for ScalarValue {
fn from(value: JsonUnionField) -> Self {
match value {
JsonUnionField::JsonNull => Self::Null,
JsonUnionField::Bool(b) => Self::Boolean(Some(b)),
JsonUnionField::Int(i) => Self::Int64(Some(i)),
JsonUnionField::Float(f) => Self::Float64(Some(f)),
JsonUnionField::Str(s) | JsonUnionField::Array(s) | JsonUnionField::Object(s) => Self::Utf8(Some(s)),
}
}
}
pub struct JsonUnionEncoder {
boolean: BooleanArray,
int: Int64Array,
float: Float64Array,
string: StringArray,
array: StringArray,
object: StringArray,
type_ids: ScalarBuffer<i8>,
}
impl JsonUnionEncoder {
#[must_use]
pub fn from_union(union: UnionArray) -> Option<Self> {
if is_json_union(union.data_type()) {
let (_, type_ids, _, c) = union.into_parts();
Some(Self {
boolean: c[1].as_boolean().clone(),
int: c[2].as_primitive().clone(),
float: c[3].as_primitive().clone(),
string: c[4].as_string().clone(),
array: c[5].as_string().clone(),
object: c[6].as_string().clone(),
type_ids,
})
} else {
None
}
}
#[must_use]
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.type_ids.len()
}
/// Get the encodable value for a given index
///
/// # Panics
///
/// Panics if the idx is outside the union values or an invalid type id exists in the union.
#[must_use]
pub fn get_value(&self, idx: usize) -> JsonUnionValue {
let type_id = self.type_ids[idx];
match type_id {
TYPE_ID_NULL => JsonUnionValue::JsonNull,
TYPE_ID_BOOL => JsonUnionValue::Bool(self.boolean.value(idx)),
TYPE_ID_INT => JsonUnionValue::Int(self.int.value(idx)),
TYPE_ID_FLOAT => JsonUnionValue::Float(self.float.value(idx)),
TYPE_ID_STR => JsonUnionValue::Str(self.string.value(idx)),
TYPE_ID_ARRAY => JsonUnionValue::Array(self.array.value(idx)),
TYPE_ID_OBJECT => JsonUnionValue::Object(self.object.value(idx)),
_ => panic!("Invalid type_id: {type_id}, not a valid JSON type"),
}
}
}
#[derive(Debug, PartialEq)]
pub enum JsonUnionValue<'a> {
JsonNull,
Bool(bool),
Int(i64),
Float(f64),
Str(&'a str),
Array(&'a str),
Object(&'a str),
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_json_union() {
let json_union = JsonUnion::from_iter(vec![
Some(JsonUnionField::JsonNull),
Some(JsonUnionField::Bool(true)),
Some(JsonUnionField::Bool(false)),
Some(JsonUnionField::Int(42)),
Some(JsonUnionField::Float(42.0)),
Some(JsonUnionField::Str("foo".to_string())),
Some(JsonUnionField::Array("[42]".to_string())),
Some(JsonUnionField::Object(r#"{"foo": 42}"#.to_string())),
None,
]);
let union_array = UnionArray::try_from(json_union).unwrap();
let encoder = JsonUnionEncoder::from_union(union_array).unwrap();
let values_after: Vec<_> = (0..encoder.len()).map(|idx| encoder.get_value(idx)).collect();
assert_eq!(
values_after,
vec![
JsonUnionValue::JsonNull,
JsonUnionValue::Bool(true),
JsonUnionValue::Bool(false),
JsonUnionValue::Int(42),
JsonUnionValue::Float(42.0),
JsonUnionValue::Str("foo"),
JsonUnionValue::Array("[42]"),
JsonUnionValue::Object(r#"{"foo": 42}"#),
JsonUnionValue::JsonNull,
]
);
}
}