forked from datafusion-contrib/datafusion-functions-json
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_get_array.rs
More file actions
150 lines (123 loc) · 4.58 KB
/
json_get_array.rs
File metadata and controls
150 lines (123 loc) · 4.58 KB
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
use std::any::Any;
use std::sync::Arc;
use datafusion::arrow::array::{ArrayRef, ListBuilder, StringBuilder};
use datafusion::arrow::datatypes::{DataType, Field};
use datafusion::common::{Result as DataFusionResult, ScalarValue};
use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
use jiter::Peek;
use crate::common::{
get_err, invoke, is_json_metadata, jiter_json_find, return_type_check, GetError, InvokeResult, JsonPath,
};
use crate::common_macros::make_udf_function;
fn list_item_field() -> Field {
Field::new("item", DataType::Utf8, true).with_metadata(is_json_metadata())
}
make_udf_function!(
JsonGetArray,
json_get_array,
json_data path,
r#"Get an arrow array from a JSON string by its "path""#
);
#[derive(Debug, PartialEq, Eq, Hash)]
pub(super) struct JsonGetArray {
signature: Signature,
aliases: [String; 1],
}
impl Default for JsonGetArray {
fn default() -> Self {
Self {
signature: Signature::variadic_any(Volatility::Immutable),
aliases: ["json_get_array".to_string()],
}
}
}
impl ScalarUDFImpl for JsonGetArray {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
self.aliases[0].as_str()
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, arg_types: &[DataType]) -> DataFusionResult<DataType> {
return_type_check(arg_types, self.name(), DataType::List(Arc::new(list_item_field())))
}
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> DataFusionResult<ColumnarValue> {
invoke::<BuildArrayList>(&args.args, jiter_json_get_array)
}
fn aliases(&self) -> &[String] {
&self.aliases
}
fn placement(
&self,
args: &[datafusion::logical_expr::ExpressionPlacement],
) -> datafusion::logical_expr::ExpressionPlacement {
// If the first argument is a column and the remaining arguments are literals (a path)
// then we can push this UDF down to the leaf nodes.
if args.len() >= 2
&& matches!(args[0], datafusion::logical_expr::ExpressionPlacement::Column)
&& args[1..]
.iter()
.all(|arg| matches!(arg, datafusion::logical_expr::ExpressionPlacement::Literal))
{
datafusion::logical_expr::ExpressionPlacement::MoveTowardsLeafNodes
} else {
datafusion::logical_expr::ExpressionPlacement::KeepInPlace
}
}
}
#[derive(Debug)]
struct BuildArrayList;
impl InvokeResult for BuildArrayList {
type Item = Vec<String>;
type Builder = ListBuilder<StringBuilder>;
const ACCEPT_DICT_RETURN: bool = false;
fn builder(capacity: usize) -> Self::Builder {
let values_builder = StringBuilder::new();
ListBuilder::with_capacity(values_builder, capacity).with_field(list_item_field())
}
fn append_value(builder: &mut Self::Builder, value: Option<Self::Item>) {
builder.append_option(value.map(|v| v.into_iter().map(Some)));
}
fn finish(mut builder: Self::Builder) -> DataFusionResult<ArrayRef> {
Ok(Arc::new(builder.finish()))
}
fn scalar(value: Option<Self::Item>) -> ScalarValue {
let mut builder = ListBuilder::new(StringBuilder::new()).with_field(list_item_field());
if let Some(array_items) = value {
for item in array_items {
builder.values().append_value(item);
}
builder.append(true);
} else {
builder.append(false);
}
let array = builder.finish();
ScalarValue::List(Arc::new(array))
}
}
fn jiter_json_get_array(opt_json: Option<&str>, path: &[JsonPath]) -> Result<Vec<String>, GetError> {
if let Some((mut jiter, peek)) = jiter_json_find(opt_json, path) {
match peek {
Peek::Array => {
let mut peek_opt = jiter.known_array()?;
let mut array_items: Vec<String> = Vec::new();
while let Some(element_peek) = peek_opt {
// Get the raw JSON slice for each array element
let start = jiter.current_index();
jiter.known_skip(element_peek)?;
let slice = jiter.slice_to_current(start);
let element_str = std::str::from_utf8(slice)?.to_string();
array_items.push(element_str);
peek_opt = jiter.array_step()?;
}
Ok(array_items)
}
_ => get_err!(),
}
} else {
get_err!()
}
}