datafusion_functions/string/
concat.rsuse arrow::array::{as_largestring_array, Array};
use arrow::datatypes::DataType;
use std::any::Any;
use std::sync::{Arc, OnceLock};
use crate::string::concat;
use crate::strings::{
ColumnarValueRef, LargeStringArrayBuilder, StringArrayBuilder, StringViewArrayBuilder,
};
use datafusion_common::cast::{as_string_array, as_string_view_array};
use datafusion_common::{internal_err, plan_err, Result, ScalarValue};
use datafusion_expr::expr::ScalarFunction;
use datafusion_expr::scalar_doc_sections::DOC_SECTION_STRING;
use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};
use datafusion_expr::{lit, ColumnarValue, Documentation, Expr, Volatility};
use datafusion_expr::{ScalarUDFImpl, Signature};
#[derive(Debug)]
pub struct ConcatFunc {
signature: Signature,
}
impl Default for ConcatFunc {
fn default() -> Self {
ConcatFunc::new()
}
}
impl ConcatFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::variadic(
vec![Utf8, Utf8View, LargeUtf8],
Volatility::Immutable,
),
}
}
}
impl ScalarUDFImpl for ConcatFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"concat"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
use DataType::*;
let mut dt = &Utf8;
arg_types.iter().for_each(|data_type| {
if data_type == &Utf8View {
dt = data_type;
}
if data_type == &LargeUtf8 && dt != &Utf8View {
dt = data_type;
}
});
Ok(dt.to_owned())
}
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
let mut return_datatype = DataType::Utf8;
args.iter().for_each(|col| {
if col.data_type() == DataType::Utf8View {
return_datatype = col.data_type();
}
if col.data_type() == DataType::LargeUtf8
&& return_datatype != DataType::Utf8View
{
return_datatype = col.data_type();
}
});
let array_len = args
.iter()
.filter_map(|x| match x {
ColumnarValue::Array(array) => Some(array.len()),
_ => None,
})
.next();
if array_len.is_none() {
let mut result = String::new();
for arg in args {
if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(v))) = arg {
result.push_str(v);
}
}
return match return_datatype {
DataType::Utf8View => {
Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(Some(result))))
}
DataType::Utf8 => {
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(result))))
}
DataType::LargeUtf8 => {
Ok(ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(result))))
}
other => {
plan_err!("Concat function does not support datatype of {other}")
}
};
}
let len = array_len.unwrap();
let mut data_size = 0;
let mut columns = Vec::with_capacity(args.len());
for arg in args {
match arg {
ColumnarValue::Scalar(ScalarValue::Utf8(maybe_value))
| ColumnarValue::Scalar(ScalarValue::LargeUtf8(maybe_value))
| ColumnarValue::Scalar(ScalarValue::Utf8View(maybe_value)) => {
if let Some(s) = maybe_value {
data_size += s.len() * len;
columns.push(ColumnarValueRef::Scalar(s.as_bytes()));
}
}
ColumnarValue::Array(array) => {
match array.data_type() {
DataType::Utf8 => {
let string_array = as_string_array(array)?;
data_size += string_array.values().len();
let column = if array.is_nullable() {
ColumnarValueRef::NullableArray(string_array)
} else {
ColumnarValueRef::NonNullableArray(string_array)
};
columns.push(column);
},
DataType::LargeUtf8 => {
let string_array = as_largestring_array(array);
data_size += string_array.values().len();
let column = if array.is_nullable() {
ColumnarValueRef::NullableLargeStringArray(string_array)
} else {
ColumnarValueRef::NonNullableLargeStringArray(string_array)
};
columns.push(column);
},
DataType::Utf8View => {
let string_array = as_string_view_array(array)?;
data_size += string_array.len();
let column = if array.is_nullable() {
ColumnarValueRef::NullableStringViewArray(string_array)
} else {
ColumnarValueRef::NonNullableStringViewArray(string_array)
};
columns.push(column);
},
other => {
return plan_err!("Input was {other} which is not a supported datatype for concat function")
}
};
}
_ => unreachable!(),
}
}
match return_datatype {
DataType::Utf8 => {
let mut builder = StringArrayBuilder::with_capacity(len, data_size);
for i in 0..len {
columns
.iter()
.for_each(|column| builder.write::<true>(column, i));
builder.append_offset();
}
let string_array = builder.finish(None);
Ok(ColumnarValue::Array(Arc::new(string_array)))
}
DataType::Utf8View => {
let mut builder = StringViewArrayBuilder::with_capacity(len, data_size);
for i in 0..len {
columns
.iter()
.for_each(|column| builder.write::<true>(column, i));
builder.append_offset();
}
let string_array = builder.finish();
Ok(ColumnarValue::Array(Arc::new(string_array)))
}
DataType::LargeUtf8 => {
let mut builder = LargeStringArrayBuilder::with_capacity(len, data_size);
for i in 0..len {
columns
.iter()
.for_each(|column| builder.write::<true>(column, i));
builder.append_offset();
}
let string_array = builder.finish(None);
Ok(ColumnarValue::Array(Arc::new(string_array)))
}
_ => unreachable!(),
}
}
fn simplify(
&self,
args: Vec<Expr>,
_info: &dyn SimplifyInfo,
) -> Result<ExprSimplifyResult> {
simplify_concat(args)
}
fn documentation(&self) -> Option<&Documentation> {
Some(get_concat_doc())
}
}
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
fn get_concat_doc() -> &'static Documentation {
DOCUMENTATION.get_or_init(|| {
Documentation::builder()
.with_doc_section(DOC_SECTION_STRING)
.with_description("Concatenates multiple strings together.")
.with_syntax_example("concat(str[, ..., str_n])")
.with_sql_example(
r#"```sql
> select concat('data', 'f', 'us', 'ion');
+-------------------------------------------------------+
| concat(Utf8("data"),Utf8("f"),Utf8("us"),Utf8("ion")) |
+-------------------------------------------------------+
| datafusion |
+-------------------------------------------------------+
```"#,
)
.with_standard_argument("str", Some("String"))
.with_argument("str_n", "Subsequent string expressions to concatenate.")
.with_related_udf("concat_ws")
.build()
.unwrap()
})
}
pub fn simplify_concat(args: Vec<Expr>) -> Result<ExprSimplifyResult> {
let mut new_args = Vec::with_capacity(args.len());
let mut contiguous_scalar = "".to_string();
for arg in args.clone() {
match arg {
Expr::Literal(ScalarValue::Utf8(None) | ScalarValue::LargeUtf8(None) | ScalarValue::Utf8View(None)) => {}
Expr::Literal(
ScalarValue::Utf8(Some(v)) | ScalarValue::LargeUtf8(Some(v)) | ScalarValue::Utf8View(Some(v)),
) => contiguous_scalar += &v,
Expr::Literal(x) => {
return internal_err!(
"The scalar {x} should be casted to string type during the type coercion."
)
}
arg => {
if !contiguous_scalar.is_empty() {
new_args.push(lit(contiguous_scalar));
contiguous_scalar = "".to_string();
}
new_args.push(arg);
}
}
}
if !contiguous_scalar.is_empty() {
new_args.push(lit(contiguous_scalar));
}
if !args.eq(&new_args) {
Ok(ExprSimplifyResult::Simplified(Expr::ScalarFunction(
ScalarFunction {
func: concat(),
args: new_args,
},
)))
} else {
Ok(ExprSimplifyResult::Original(args))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::test::test_function;
use arrow::array::{Array, LargeStringArray, StringViewArray};
use arrow::array::{ArrayRef, StringArray};
use DataType::*;
#[test]
fn test_functions() -> Result<()> {
test_function!(
ConcatFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::from("aa")),
ColumnarValue::Scalar(ScalarValue::from("bb")),
ColumnarValue::Scalar(ScalarValue::from("cc")),
],
Ok(Some("aabbcc")),
&str,
Utf8,
StringArray
);
test_function!(
ConcatFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::from("aa")),
ColumnarValue::Scalar(ScalarValue::Utf8(None)),
ColumnarValue::Scalar(ScalarValue::from("cc")),
],
Ok(Some("aacc")),
&str,
Utf8,
StringArray
);
test_function!(
ConcatFunc::new(),
&[ColumnarValue::Scalar(ScalarValue::Utf8(None))],
Ok(Some("")),
&str,
Utf8,
StringArray
);
test_function!(
ConcatFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::from("aa")),
ColumnarValue::Scalar(ScalarValue::Utf8View(None)),
ColumnarValue::Scalar(ScalarValue::LargeUtf8(None)),
ColumnarValue::Scalar(ScalarValue::from("cc")),
],
Ok(Some("aacc")),
&str,
Utf8View,
StringViewArray
);
test_function!(
ConcatFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::from("aa")),
ColumnarValue::Scalar(ScalarValue::LargeUtf8(None)),
ColumnarValue::Scalar(ScalarValue::from("cc")),
],
Ok(Some("aacc")),
&str,
LargeUtf8,
LargeStringArray
);
Ok(())
}
#[test]
fn concat() -> Result<()> {
let c0 =
ColumnarValue::Array(Arc::new(StringArray::from(vec!["foo", "bar", "baz"])));
let c1 = ColumnarValue::Scalar(ScalarValue::Utf8(Some(",".to_string())));
let c2 = ColumnarValue::Array(Arc::new(StringArray::from(vec![
Some("x"),
None,
Some("z"),
])));
let args = &[c0, c1, c2];
#[allow(deprecated)] let result = ConcatFunc::new().invoke(args)?;
let expected =
Arc::new(StringArray::from(vec!["foo,x", "bar,", "baz,z"])) as ArrayRef;
match &result {
ColumnarValue::Array(array) => {
assert_eq!(&expected, array);
}
_ => panic!(),
}
Ok(())
}
}