datafusion_functions_json/
json_get_json.rs1use std::any::Any;
2
3use datafusion::arrow::array::StringArray;
4use datafusion::arrow::datatypes::DataType;
5use datafusion::common::Result as DataFusionResult;
6use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
7
8use crate::common::{get_err, invoke, jiter_json_find, return_type_check, GetError, JsonPath};
9use crate::common_macros::make_udf_function;
10
11make_udf_function!(
12 JsonGetJson,
13 json_get_json,
14 json_data path,
15 r#"Get a nested raw JSON string from a JSON string by its "path""#
16);
17
18#[derive(Debug)]
19pub(super) struct JsonGetJson {
20 signature: Signature,
21 aliases: [String; 1],
22}
23
24impl Default for JsonGetJson {
25 fn default() -> Self {
26 Self {
27 signature: Signature::variadic_any(Volatility::Immutable),
28 aliases: ["json_get_json".to_string()],
29 }
30 }
31}
32
33impl ScalarUDFImpl for JsonGetJson {
34 fn as_any(&self) -> &dyn Any {
35 self
36 }
37
38 fn name(&self) -> &str {
39 self.aliases[0].as_str()
40 }
41
42 fn signature(&self) -> &Signature {
43 &self.signature
44 }
45
46 fn return_type(&self, arg_types: &[DataType]) -> DataFusionResult<DataType> {
47 return_type_check(arg_types, self.name(), DataType::Utf8)
48 }
49
50 fn invoke_with_args(&self, args: ScalarFunctionArgs) -> DataFusionResult<ColumnarValue> {
51 invoke::<StringArray>(&args.args, jiter_json_get_json)
52 }
53
54 fn aliases(&self) -> &[String] {
55 &self.aliases
56 }
57}
58
59fn jiter_json_get_json(opt_json: Option<&str>, path: &[JsonPath]) -> Result<String, GetError> {
60 if let Some((mut jiter, peek)) = jiter_json_find(opt_json, path) {
61 let start = jiter.current_index();
62 jiter.known_skip(peek)?;
63 let object_slice = jiter.slice_to_current(start);
64 let object_string = std::str::from_utf8(object_slice)?;
65 Ok(object_string.to_owned())
66 } else {
67 get_err!()
68 }
69}