use std::any::Any;
use datafusion::arrow::array::StringArray;
use datafusion::arrow::datatypes::DataType;
use datafusion::common::Result as DataFusionResult;
use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
use crate::common::{get_err, invoke, jiter_json_find, return_type_check, GetError, JsonPath};
use crate::common_macros::make_udf_function;
make_udf_function!(
JsonGetJson,
json_get_json,
json_data path,
r#"Get a nested raw JSON string from a JSON string by its "path""#
);
#[derive(Debug, PartialEq, Eq, Hash)]
pub(super) struct JsonGetJson {
signature: Signature,
aliases: [String; 1],
}
impl Default for JsonGetJson {
fn default() -> Self {
Self {
signature: Signature::variadic_any(Volatility::Immutable),
aliases: ["json_get_json".to_string()],
}
}
}
impl ScalarUDFImpl for JsonGetJson {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
self.aliases[0].as_str()
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, arg_types: &[DataType]) -> DataFusionResult<DataType> {
return_type_check(arg_types, self.name(), DataType::Utf8)
}
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> DataFusionResult<ColumnarValue> {
invoke::<StringArray>(&args.args, jiter_json_get_json)
}
fn aliases(&self) -> &[String] {
&self.aliases
}
fn placement(
&self,
args: &[datafusion::logical_expr::ExpressionPlacement],
) -> datafusion::logical_expr::ExpressionPlacement {
if args.len() >= 2
&& matches!(args[0], datafusion::logical_expr::ExpressionPlacement::Column)
&& args[1..]
.iter()
.all(|arg| matches!(arg, datafusion::logical_expr::ExpressionPlacement::Literal))
{
datafusion::logical_expr::ExpressionPlacement::MoveTowardsLeafNodes
} else {
datafusion::logical_expr::ExpressionPlacement::KeepInPlace
}
}
}
fn jiter_json_get_json(opt_json: Option<&str>, path: &[JsonPath]) -> Result<String, GetError> {
if let Some((mut jiter, peek)) = jiter_json_find(opt_json, path) {
let start = jiter.current_index();
jiter.known_skip(peek)?;
let object_slice = jiter.slice_to_current(start);
let object_string = std::str::from_utf8(object_slice)?;
Ok(object_string.to_owned())
} else {
get_err!()
}
}