Skip to main content

datafusion_functions_json/
json_contains.rs

1use std::any::Any;
2use std::sync::Arc;
3
4use datafusion::arrow::array::BooleanBuilder;
5use datafusion::arrow::datatypes::DataType;
6use datafusion::common::arrow::array::{ArrayRef, BooleanArray};
7use datafusion::common::{plan_err, Result, ScalarValue};
8use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
9
10use crate::common::{invoke, jiter_json_find, return_type_check, GetError, InvokeResult, JsonPath};
11use crate::common_macros::make_udf_function;
12
13make_udf_function!(
14    JsonContains,
15    json_contains,
16    json_data path,
17    r#"Does the key/index exist within the JSON value as the specified "path"?"#
18);
19
20#[derive(Debug, PartialEq, Eq, Hash)]
21pub(super) struct JsonContains {
22    signature: Signature,
23    aliases: [String; 1],
24}
25
26impl Default for JsonContains {
27    fn default() -> Self {
28        Self {
29            signature: Signature::variadic_any(Volatility::Immutable),
30            aliases: ["json_contains".to_string()],
31        }
32    }
33}
34
35impl ScalarUDFImpl for JsonContains {
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]) -> Result<DataType> {
49        if arg_types.len() < 2 {
50            plan_err!("The 'json_contains' function requires two or more arguments.")
51        } else {
52            return_type_check(arg_types, self.name(), DataType::Boolean).map(|_| DataType::Boolean)
53        }
54    }
55
56    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
57        invoke::<BooleanArray>(&args.args, jiter_json_contains)
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
83impl InvokeResult for BooleanArray {
84    type Item = bool;
85
86    type Builder = BooleanBuilder;
87
88    // Using boolean inside a dictionary is not an optimization!
89    const ACCEPT_DICT_RETURN: bool = false;
90
91    fn builder(capacity: usize) -> Self::Builder {
92        BooleanBuilder::with_capacity(capacity)
93    }
94
95    fn append_value(builder: &mut Self::Builder, value: Option<Self::Item>) {
96        builder.append_option(value);
97    }
98
99    fn finish(mut builder: Self::Builder) -> Result<ArrayRef> {
100        Ok(Arc::new(builder.finish()))
101    }
102
103    fn scalar(value: Option<Self::Item>) -> ScalarValue {
104        ScalarValue::Boolean(value)
105    }
106}
107
108#[allow(clippy::unnecessary_wraps)]
109fn jiter_json_contains(json_data: Option<&str>, path: &[JsonPath]) -> Result<bool, GetError> {
110    Ok(jiter_json_find(json_data, path).is_some())
111}