Skip to content

Commit 9807bc4

Browse files
committed
add column structs
1 parent 531e9ca commit 9807bc4

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/json_get_array.rs

+37-33
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,6 @@ use jiter::Peek;
1111
use crate::common::{check_args, get_err, invoke, jiter_json_find, GetError, JsonPath};
1212
use crate::common_macros::make_udf_function;
1313

14-
struct StrArrayColumn {
15-
rows: GenericListArray<i32>,
16-
}
17-
18-
impl FromIterator<Option<Vec<String>>> for StrArrayColumn {
19-
fn from_iter<T: IntoIterator<Item = Option<Vec<String>>>>(iter: T) -> Self {
20-
let string_builder = StringBuilder::new();
21-
let mut list_builder = ListBuilder::new(string_builder);
22-
23-
for row in iter {
24-
if let Some(row) = row {
25-
for elem in row {
26-
list_builder.values().append_value(elem);
27-
}
28-
29-
list_builder.append(true);
30-
} else {
31-
list_builder.append(false);
32-
}
33-
}
34-
35-
Self {
36-
rows: list_builder.finish(),
37-
}
38-
}
39-
}
40-
4114
make_udf_function!(
4215
JsonGetArray,
4316
json_get_array,
@@ -78,7 +51,7 @@ impl ScalarUDFImpl for JsonGetArray {
7851
}
7952

8053
fn invoke(&self, args: &[ColumnarValue]) -> DataFusionResult<ColumnarValue> {
81-
invoke::<StrArrayColumn, Vec<String>>(
54+
invoke::<JsonArray, JsonArrayField>(
8255
args,
8356
jiter_json_get_array,
8457
|c| Ok(Arc::new(c.rows) as ArrayRef),
@@ -87,7 +60,7 @@ impl ScalarUDFImpl for JsonGetArray {
8760
let mut list_builder = ListBuilder::new(string_builder);
8861

8962
if let Some(row) = i {
90-
for elem in row {
63+
for elem in row.elements {
9164
list_builder.values().append_value(elem);
9265
}
9366
}
@@ -102,25 +75,56 @@ impl ScalarUDFImpl for JsonGetArray {
10275
}
10376
}
10477

105-
fn jiter_json_get_array(json_data: Option<&str>, path: &[JsonPath]) -> Result<Vec<String>, GetError> {
78+
struct JsonArray {
79+
rows: GenericListArray<i32>,
80+
}
81+
82+
struct JsonArrayField {
83+
elements: Vec<String>,
84+
}
85+
86+
impl FromIterator<Option<JsonArrayField>> for JsonArray {
87+
fn from_iter<T: IntoIterator<Item = Option<JsonArrayField>>>(iter: T) -> Self {
88+
let string_builder = StringBuilder::new();
89+
let mut list_builder = ListBuilder::new(string_builder);
90+
91+
for row in iter {
92+
if let Some(row) = row {
93+
for elem in row.elements {
94+
list_builder.values().append_value(elem);
95+
}
96+
97+
list_builder.append(true);
98+
} else {
99+
list_builder.append(false);
100+
}
101+
}
102+
103+
Self {
104+
rows: list_builder.finish(),
105+
}
106+
}
107+
}
108+
109+
fn jiter_json_get_array(json_data: Option<&str>, path: &[JsonPath]) -> Result<JsonArrayField, GetError> {
106110
if let Some((mut jiter, peek)) = jiter_json_find(json_data, path) {
107111
match peek {
108112
Peek::Array => {
109113
let mut peek_opt = jiter.known_array()?;
110-
let mut array_values = Vec::new();
114+
let mut elements = Vec::new();
111115

112116
while let Some(peek) = peek_opt {
113117
let start = jiter.current_index();
114118
jiter.known_skip(peek)?;
115119
let object_slice = jiter.slice_to_current(start);
116120
let object_string = std::str::from_utf8(object_slice)?;
117121

118-
array_values.push(object_string.to_owned());
122+
elements.push(object_string.to_owned());
119123

120124
peek_opt = jiter.array_step()?;
121125
}
122126

123-
Ok(array_values)
127+
Ok(JsonArrayField { elements })
124128
}
125129
_ => get_err!(),
126130
}

0 commit comments

Comments
 (0)