|
| 1 | +use std::any::Any; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use datafusion::arrow::array::{ArrayRef, ListArray, ListBuilder, StringBuilder}; |
| 5 | +use datafusion::arrow::datatypes::{DataType, Field}; |
| 6 | +use datafusion::common::{Result as DataFusionResult, ScalarValue}; |
| 7 | +use datafusion::logical_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; |
| 8 | +use jiter::Peek; |
| 9 | + |
| 10 | +use crate::common::{get_err, invoke, jiter_json_find, return_type_check, GetError, JsonPath}; |
| 11 | +use crate::common_macros::make_udf_function; |
| 12 | + |
| 13 | +make_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)] |
| 21 | +pub(super) struct JsonObjectKeys { |
| 22 | + signature: Signature, |
| 23 | + aliases: [String; 2], |
| 24 | +} |
| 25 | + |
| 26 | +impl 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 | + |
| 35 | +impl 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(&self, args: &[ColumnarValue]) -> DataFusionResult<ColumnarValue> { |
| 57 | + invoke::<ListArrayWrapper, Vec<String>>( |
| 58 | + args, |
| 59 | + jiter_json_object_keys, |
| 60 | + |w| Ok(Arc::new(w.0) as ArrayRef), |
| 61 | + keys_to_scalar, |
| 62 | + true, |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + fn aliases(&self) -> &[String] { |
| 67 | + &self.aliases |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Wrapper for a `ListArray` that allows us to implement `FromIterator<Option<Vec<String>>>` as required. |
| 72 | +#[derive(Debug)] |
| 73 | +struct ListArrayWrapper(ListArray); |
| 74 | + |
| 75 | +impl FromIterator<Option<Vec<String>>> for ListArrayWrapper { |
| 76 | + fn from_iter<I: IntoIterator<Item = Option<Vec<String>>>>(iter: I) -> Self { |
| 77 | + let values_builder = StringBuilder::new(); |
| 78 | + let mut builder = ListBuilder::new(values_builder); |
| 79 | + for opt_keys in iter { |
| 80 | + if let Some(keys) = opt_keys { |
| 81 | + for value in keys { |
| 82 | + builder.values().append_value(value); |
| 83 | + } |
| 84 | + builder.append(true); |
| 85 | + } else { |
| 86 | + builder.append(false); |
| 87 | + } |
| 88 | + } |
| 89 | + Self(builder.finish()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn keys_to_scalar(opt_keys: Option<Vec<String>>) -> ScalarValue { |
| 94 | + let values_builder = StringBuilder::new(); |
| 95 | + let mut builder = ListBuilder::new(values_builder); |
| 96 | + if let Some(keys) = opt_keys { |
| 97 | + for value in keys { |
| 98 | + builder.values().append_value(value); |
| 99 | + } |
| 100 | + builder.append(true); |
| 101 | + } else { |
| 102 | + builder.append(false); |
| 103 | + } |
| 104 | + let array = builder.finish(); |
| 105 | + ScalarValue::List(Arc::new(array)) |
| 106 | +} |
| 107 | + |
| 108 | +fn jiter_json_object_keys(opt_json: Option<&str>, path: &[JsonPath]) -> Result<Vec<String>, GetError> { |
| 109 | + if let Some((mut jiter, peek)) = jiter_json_find(opt_json, path) { |
| 110 | + match peek { |
| 111 | + Peek::Object => { |
| 112 | + let mut opt_key = jiter.known_object()?; |
| 113 | + |
| 114 | + let mut keys = Vec::new(); |
| 115 | + while let Some(key) = opt_key { |
| 116 | + keys.push(key.to_string()); |
| 117 | + jiter.next_skip()?; |
| 118 | + opt_key = jiter.next_key()?; |
| 119 | + } |
| 120 | + Ok(keys) |
| 121 | + } |
| 122 | + _ => get_err!(), |
| 123 | + } |
| 124 | + } else { |
| 125 | + get_err!() |
| 126 | + } |
| 127 | +} |
0 commit comments