use std::any::Any;
use std::sync::Arc;
use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait};
use arrow::datatypes::DataType;
use datafusion_common::cast::{as_generic_string_array, as_int64_array};
use datafusion_common::{exec_err, Result};
use datafusion_expr::TypeSignature::*;
use datafusion_expr::{ColumnarValue, Volatility};
use datafusion_expr::{ScalarUDFImpl, Signature};
use crate::utils::{make_scalar_function, utf8_to_str_type};
#[derive(Debug)]
pub struct SplitPartFunc {
signature: Signature,
}
impl Default for SplitPartFunc {
fn default() -> Self {
Self::new()
}
}
impl SplitPartFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::one_of(
vec![
Exact(vec![Utf8, Utf8, Int64]),
Exact(vec![LargeUtf8, Utf8, Int64]),
Exact(vec![Utf8, LargeUtf8, Int64]),
Exact(vec![LargeUtf8, LargeUtf8, Int64]),
],
Volatility::Immutable,
),
}
}
}
impl ScalarUDFImpl for SplitPartFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"split_part"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
utf8_to_str_type(&arg_types[0], "split_part")
}
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
match args[0].data_type() {
DataType::Utf8 => make_scalar_function(split_part::<i32>, vec![])(args),
DataType::LargeUtf8 => make_scalar_function(split_part::<i64>, vec![])(args),
other => {
exec_err!("Unsupported data type {other:?} for function split_part")
}
}
}
}
fn split_part<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
let string_array = as_generic_string_array::<T>(&args[0])?;
let delimiter_array = as_generic_string_array::<T>(&args[1])?;
let n_array = as_int64_array(&args[2])?;
let result = string_array
.iter()
.zip(delimiter_array.iter())
.zip(n_array.iter())
.map(|((string, delimiter), n)| match (string, delimiter, n) {
(Some(string), Some(delimiter), Some(n)) => {
let split_string: Vec<&str> = string.split(delimiter).collect();
let len = split_string.len();
let index = match n.cmp(&0) {
std::cmp::Ordering::Less => len as i64 + n,
std::cmp::Ordering::Equal => {
return exec_err!("field position must not be zero");
}
std::cmp::Ordering::Greater => n - 1,
} as usize;
if index < len {
Ok(Some(split_string[index]))
} else {
Ok(Some(""))
}
}
_ => Ok(None),
})
.collect::<Result<GenericStringArray<T>>>()?;
Ok(Arc::new(result) as ArrayRef)
}
#[cfg(test)]
mod tests {
use arrow::array::{Array, StringArray};
use arrow::datatypes::DataType::Utf8;
use datafusion_common::ScalarValue;
use datafusion_common::{exec_err, Result};
use datafusion_expr::{ColumnarValue, ScalarUDFImpl};
use crate::string::split_part::SplitPartFunc;
use crate::utils::test::test_function;
#[test]
fn test_functions() -> Result<()> {
test_function!(
SplitPartFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from(
"abc~@~def~@~ghi"
)))),
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("~@~")))),
ColumnarValue::Scalar(ScalarValue::Int64(Some(2))),
],
Ok(Some("def")),
&str,
Utf8,
StringArray
);
test_function!(
SplitPartFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from(
"abc~@~def~@~ghi"
)))),
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("~@~")))),
ColumnarValue::Scalar(ScalarValue::Int64(Some(20))),
],
Ok(Some("")),
&str,
Utf8,
StringArray
);
test_function!(
SplitPartFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from(
"abc~@~def~@~ghi"
)))),
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("~@~")))),
ColumnarValue::Scalar(ScalarValue::Int64(Some(-1))),
],
Ok(Some("ghi")),
&str,
Utf8,
StringArray
);
test_function!(
SplitPartFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from(
"abc~@~def~@~ghi"
)))),
ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("~@~")))),
ColumnarValue::Scalar(ScalarValue::Int64(Some(0))),
],
exec_err!("field position must not be zero"),
&str,
Utf8,
StringArray
);
Ok(())
}
}