Skip to main content

datafusion_functions_json/
json_object_keys.rs

1use std::any::Any;
2use std::sync::Arc;
3
4use datafusion::arrow::array::{ArrayRef, ListBuilder, StringBuilder};
5use datafusion::arrow::datatypes::{DataType, Field};
6use datafusion::common::{Result as DataFusionResult, ScalarValue};
7use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
8use jiter::Peek;
9
10use crate::common::{get_err, invoke, jiter_json_find, return_type_check, GetError, InvokeResult, JsonPath};
11use crate::common_macros::make_udf_function;
12
13make_udf_function!(
14    JsonObjectKeys,
15    json_object_keys,
16    json_data path,
17    r"Get the keys of a JSON object as an array."
18);
19
20#[derive(Debug, PartialEq, Eq, Hash)]
21pub(super) struct JsonObjectKeys {
22    signature: Signature,
23    aliases: [String; 2],
24}
25
26impl Default for JsonObjectKeys {
27    fn default() -> Self {
28        Self {
29            signature: Signature::variadic_any(Volatility::Immutable),
30            aliases: ["json_object_keys".to_string(), "json_keys".to_string()],
31        }
32    }
33}
34
35impl ScalarUDFImpl for JsonObjectKeys {
36    fn as_any(&self) -> &dyn Any {
37        self
38    }
39
40    fn name(&self) -> &str {
41        self.aliases[0].as_str()
42    }
43
44    fn signature(&self) -> &Signature {
45        &self.signature
46    }
47
48    fn return_type(&self, arg_types: &[DataType]) -> DataFusionResult<DataType> {
49        return_type_check(
50            arg_types,
51            self.name(),
52            DataType::List(Arc::new(Field::new("item", DataType::Utf8, true))),
53        )
54    }
55
56    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> DataFusionResult<ColumnarValue> {
57        invoke::<BuildListArray>(&args.args, jiter_json_object_keys)
58    }
59
60    fn aliases(&self) -> &[String] {
61        &self.aliases
62    }
63
64    fn placement(
65        &self,
66        args: &[datafusion::logical_expr::ExpressionPlacement],
67    ) -> datafusion::logical_expr::ExpressionPlacement {
68        // If the first argument is a column and the remaining arguments are literals (a path)
69        // then we can push this UDF down to the leaf nodes.
70        if args.len() >= 2
71            && matches!(args[0], datafusion::logical_expr::ExpressionPlacement::Column)
72            && args[1..]
73                .iter()
74                .all(|arg| matches!(arg, datafusion::logical_expr::ExpressionPlacement::Literal))
75        {
76            datafusion::logical_expr::ExpressionPlacement::MoveTowardsLeafNodes
77        } else {
78            datafusion::logical_expr::ExpressionPlacement::KeepInPlace
79        }
80    }
81}
82
83/// Struct used to build a `ListArray` from the result of `jiter_json_object_keys`.
84#[derive(Debug)]
85struct BuildListArray;
86
87impl InvokeResult for BuildListArray {
88    type Item = Vec<String>;
89
90    type Builder = ListBuilder<StringBuilder>;
91
92    const ACCEPT_DICT_RETURN: bool = true;
93
94    fn builder(capacity: usize) -> Self::Builder {
95        let values_builder = StringBuilder::new();
96        ListBuilder::with_capacity(values_builder, capacity)
97    }
98
99    fn append_value(builder: &mut Self::Builder, value: Option<Self::Item>) {
100        builder.append_option(value.map(|v| v.into_iter().map(Some)));
101    }
102
103    fn finish(mut builder: Self::Builder) -> DataFusionResult<ArrayRef> {
104        Ok(Arc::new(builder.finish()))
105    }
106
107    fn scalar(value: Option<Self::Item>) -> ScalarValue {
108        keys_to_scalar(value)
109    }
110}
111
112fn keys_to_scalar(opt_keys: Option<Vec<String>>) -> ScalarValue {
113    let values_builder = StringBuilder::new();
114    let mut builder = ListBuilder::new(values_builder);
115    if let Some(keys) = opt_keys {
116        for value in keys {
117            builder.values().append_value(value);
118        }
119        builder.append(true);
120    } else {
121        builder.append(false);
122    }
123    let array = builder.finish();
124    ScalarValue::List(Arc::new(array))
125}
126
127fn jiter_json_object_keys(opt_json: Option<&str>, path: &[JsonPath]) -> Result<Vec<String>, GetError> {
128    if let Some((mut jiter, peek)) = jiter_json_find(opt_json, path) {
129        match peek {
130            Peek::Object => {
131                let mut opt_key = jiter.known_object()?;
132
133                let mut keys = Vec::new();
134                while let Some(key) = opt_key {
135                    keys.push(key.to_string());
136                    jiter.next_skip()?;
137                    opt_key = jiter.next_key()?;
138                }
139                Ok(keys)
140            }
141            _ => get_err!(),
142        }
143    } else {
144        get_err!()
145    }
146}