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, PartialEq, Eq, Hash)]
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 fn placement(
59 &self,
60 args: &[datafusion::logical_expr::ExpressionPlacement],
61 ) -> datafusion::logical_expr::ExpressionPlacement {
62 if args.len() >= 2
65 && matches!(args[0], datafusion::logical_expr::ExpressionPlacement::Column)
66 && args[1..]
67 .iter()
68 .all(|arg| matches!(arg, datafusion::logical_expr::ExpressionPlacement::Literal))
69 {
70 datafusion::logical_expr::ExpressionPlacement::MoveTowardsLeafNodes
71 } else {
72 datafusion::logical_expr::ExpressionPlacement::KeepInPlace
73 }
74 }
75}
76
77fn jiter_json_get_json(opt_json: Option<&str>, path: &[JsonPath]) -> Result<String, GetError> {
78 if let Some((mut jiter, peek)) = jiter_json_find(opt_json, path) {
79 let start = jiter.current_index();
80 jiter.known_skip(peek)?;
81 let object_slice = jiter.slice_to_current(start);
82 let object_string = std::str::from_utf8(object_slice)?;
83 Ok(object_string.to_owned())
84 } else {
85 get_err!()
86 }
87}