|
| 1 | +use std::any::Any; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use arrow::array::{GenericListArray, ListBuilder, StringBuilder}; |
| 5 | +use arrow_schema::{DataType, Field}; |
| 6 | +use datafusion_common::arrow::array::ArrayRef; |
| 7 | +use datafusion_common::{Result as DataFusionResult, ScalarValue}; |
| 8 | +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; |
| 9 | +use jiter::Peek; |
| 10 | + |
| 11 | +use crate::common::{check_args, get_err, invoke, jiter_json_find, GetError, JsonPath}; |
| 12 | +use crate::common_macros::make_udf_function; |
| 13 | + |
| 14 | +struct StrArrayColumn { |
| 15 | + rows: GenericListArray<i32>, |
| 16 | +} |
| 17 | + |
| 18 | +impl FromIterator<Option<Vec<String>>> for StrArrayColumn { |
| 19 | + fn from_iter<T: IntoIterator<Item = Option<Vec<String>>>>(iter: T) -> Self { |
| 20 | + let string_builder = StringBuilder::new(); |
| 21 | + let mut list_builder = ListBuilder::new(string_builder); |
| 22 | + |
| 23 | + for row in iter { |
| 24 | + if let Some(row) = row { |
| 25 | + for elem in row { |
| 26 | + list_builder.values().append_value(elem); |
| 27 | + } |
| 28 | + |
| 29 | + list_builder.append(true); |
| 30 | + } else { |
| 31 | + list_builder.append(false); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + Self { |
| 36 | + rows: list_builder.finish(), |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +make_udf_function!( |
| 42 | + JsonGetArray, |
| 43 | + json_get_array, |
| 44 | + json_data path, |
| 45 | + r#"Get an arrow array value from a JSON string by its "path""# |
| 46 | +); |
| 47 | + |
| 48 | +#[derive(Debug)] |
| 49 | +pub(super) struct JsonGetArray { |
| 50 | + signature: Signature, |
| 51 | + aliases: [String; 1], |
| 52 | +} |
| 53 | + |
| 54 | +impl Default for JsonGetArray { |
| 55 | + fn default() -> Self { |
| 56 | + Self { |
| 57 | + signature: Signature::variadic_any(Volatility::Immutable), |
| 58 | + aliases: ["json_get_array".to_string()], |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl ScalarUDFImpl for JsonGetArray { |
| 64 | + fn as_any(&self) -> &dyn Any { |
| 65 | + self |
| 66 | + } |
| 67 | + |
| 68 | + fn name(&self) -> &str { |
| 69 | + self.aliases[0].as_str() |
| 70 | + } |
| 71 | + |
| 72 | + fn signature(&self) -> &Signature { |
| 73 | + &self.signature |
| 74 | + } |
| 75 | + |
| 76 | + fn return_type(&self, arg_types: &[DataType]) -> DataFusionResult<DataType> { |
| 77 | + check_args(arg_types, self.name()).map(|()| DataType::List(Field::new("item", DataType::Utf8, true).into())) |
| 78 | + } |
| 79 | + |
| 80 | + fn invoke(&self, args: &[ColumnarValue]) -> DataFusionResult<ColumnarValue> { |
| 81 | + invoke::<StrArrayColumn, Vec<String>>( |
| 82 | + args, |
| 83 | + jiter_json_get_array, |
| 84 | + |c| Ok(Arc::new(c.rows) as ArrayRef), |
| 85 | + |i| { |
| 86 | + let string_builder = StringBuilder::new(); |
| 87 | + let mut list_builder = ListBuilder::new(string_builder); |
| 88 | + |
| 89 | + if let Some(row) = i { |
| 90 | + for elem in row { |
| 91 | + list_builder.values().append_value(elem); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + ScalarValue::List(list_builder.finish().into()) |
| 96 | + }, |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + fn aliases(&self) -> &[String] { |
| 101 | + &self.aliases |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fn jiter_json_get_array(json_data: Option<&str>, path: &[JsonPath]) -> Result<Vec<String>, GetError> { |
| 106 | + if let Some((mut jiter, peek)) = jiter_json_find(json_data, path) { |
| 107 | + match peek { |
| 108 | + Peek::Array => { |
| 109 | + let mut peek_opt = jiter.known_array()?; |
| 110 | + let mut array_values = Vec::new(); |
| 111 | + |
| 112 | + while let Some(peek) = peek_opt { |
| 113 | + let start = jiter.current_index(); |
| 114 | + jiter.known_skip(peek)?; |
| 115 | + let object_slice = jiter.slice_to_current(start); |
| 116 | + let object_string = std::str::from_utf8(object_slice)?; |
| 117 | + |
| 118 | + array_values.push(object_string.to_owned()); |
| 119 | + |
| 120 | + peek_opt = jiter.array_step()?; |
| 121 | + } |
| 122 | + |
| 123 | + Ok(array_values) |
| 124 | + } |
| 125 | + _ => get_err!(), |
| 126 | + } |
| 127 | + } else { |
| 128 | + get_err!() |
| 129 | + } |
| 130 | +} |
0 commit comments