-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the CONCAT scalar function to support Utf8View #12224
Changes from 14 commits
723ceb7
f7abdd5
503d5b9
b30330d
76d6b5f
9798ed3
91d04ff
6d28927
769d99d
ac30a83
7ea6e0a
dd3ad39
504459c
e081934
0069c1a
0929a4b
f16de44
d0bf3ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,13 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow::array::{as_largestring_array, Array}; | ||
use arrow::datatypes::DataType; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use arrow::datatypes::DataType; | ||
use arrow::datatypes::DataType::Utf8; | ||
|
||
use datafusion_common::cast::as_string_array; | ||
use datafusion_common::{internal_err, Result, ScalarValue}; | ||
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::simplify::{ExprSimplifyResult, SimplifyInfo}; | ||
use datafusion_expr::{lit, ColumnarValue, Expr, Volatility}; | ||
|
@@ -46,7 +45,10 @@ impl ConcatFunc { | |
pub fn new() -> Self { | ||
use DataType::*; | ||
Self { | ||
signature: Signature::variadic(vec![Utf8], Volatility::Immutable), | ||
signature: Signature::variadic( | ||
vec![Utf8, Utf8View, LargeUtf8], | ||
Volatility::Immutable, | ||
), | ||
} | ||
} | ||
} | ||
|
@@ -64,13 +66,18 @@ impl ScalarUDFImpl for ConcatFunc { | |
&self.signature | ||
} | ||
|
||
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(Utf8) | ||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
use DataType::*; | ||
Ok(match &arg_types[0] { | ||
LargeUtf8 => LargeUtf8, | ||
_ => Utf8, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. currently Utf8View isn't declared as an expected return type, cc @comphead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added test cases for Utf8View and LargeUtf8 return types. Have gone ahead and adjusted this. |
||
} | ||
|
||
/// Concatenates the text representations of all the arguments. NULL arguments are ignored. | ||
/// concat('abcde', 2, NULL, 22) = 'abcde222' | ||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
let first_arg_datatype = args[0].data_type(); | ||
let array_len = args | ||
.iter() | ||
.filter_map(|x| match x { | ||
|
@@ -87,7 +94,21 @@ impl ScalarUDFImpl for ConcatFunc { | |
result.push_str(v); | ||
} | ||
} | ||
return Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(result)))); | ||
|
||
return match first_arg_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}") | ||
} | ||
}; | ||
} | ||
|
||
// Array | ||
|
@@ -103,28 +124,83 @@ impl ScalarUDFImpl for ConcatFunc { | |
columns.push(ColumnarValueRef::Scalar(s.as_bytes())); | ||
} | ||
} | ||
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) => { | ||
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) | ||
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") | ||
} | ||
}; | ||
columns.push(column); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
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(); | ||
match first_arg_datatype { | ||
DataType::Utf8 | DataType::Utf8View => { | ||
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::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!(), | ||
} | ||
Ok(ColumnarValue::Array(Arc::new(builder.finish(None)))) | ||
} | ||
|
||
/// Simplify the `concat` function by | ||
|
@@ -151,11 +227,11 @@ pub fn simplify_concat(args: Vec<Expr>) -> Result<ExprSimplifyResult> { | |
for arg in args.clone() { | ||
match arg { | ||
// filter out `null` args | ||
Expr::Literal(ScalarValue::Utf8(None) | ScalarValue::LargeUtf8(None)) => {} | ||
Expr::Literal(ScalarValue::Utf8(None) | ScalarValue::LargeUtf8(None) | ScalarValue::Utf8View(None)) => {} | ||
// All literals have been converted to Utf8 or LargeUtf8 in type_coercion. | ||
// Concatenate it with the `contiguous_scalar`. | ||
Expr::Literal( | ||
ScalarValue::Utf8(Some(v)) | ScalarValue::LargeUtf8(Some(v)), | ||
ScalarValue::Utf8(Some(v)) | ScalarValue::LargeUtf8(Some(v)) | ScalarValue::Utf8View(Some(v)), | ||
) => contiguous_scalar += &v, | ||
Expr::Literal(x) => { | ||
return internal_err!( | ||
|
@@ -197,6 +273,7 @@ mod tests { | |
use crate::utils::test::test_function; | ||
use arrow::array::Array; | ||
use arrow::array::{ArrayRef, StringArray}; | ||
use DataType::*; | ||
|
||
#[test] | ||
fn test_functions() -> Result<()> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to make the already existing
StringArrayBuilder
generic but was having issues 😢