Skip to content

Apache DataFusion SQL Query Engine

ARROW-10084: [Rust] [DataFusion] Added length of LargeStringArray and fixed undefined behavior.

Jorge C. Leitao66efa516b859
diff --git a/rust/datafusion/src/physical_plan/functions.rs b/rust/datafusion/src/physical_plan/functions.rs
index 0ffc083412..7b2fd59351 100644
--- a/rust/datafusion/src/physical_plan/functions.rs
+++ b/rust/datafusion/src/physical_plan/functions.rs
@@ -188,7 +188,16 @@ pub fn return_type(
// will be built-in functions whose return type depends on the
// incoming type.
match fun {
- BuiltinScalarFunction::Length => Ok(DataType::UInt32),
+ BuiltinScalarFunction::Length => Ok(match arg_types[0] {
+ DataType::LargeUtf8 => DataType::Int64,
+ DataType::Utf8 => DataType::Int32,
+ _ => {
+ // this error is internal as `data_types` should have captured this.
+ return Err(ExecutionError::InternalError(
+ "The length function can only accept strings.".to_string(),
+ ));
+ }
+ }),
BuiltinScalarFunction::Concat => Ok(DataType::Utf8),
BuiltinScalarFunction::ToTimestamp => {
Ok(DataType::Timestamp(TimeUnit::Nanosecond, None))
@@ -226,7 +235,7 @@ pub fn create_physical_expr(
BuiltinScalarFunction::Trunc => math_expressions::trunc,
BuiltinScalarFunction::Abs => math_expressions::abs,
BuiltinScalarFunction::Signum => math_expressions::signum,
- BuiltinScalarFunction::Length => |args| Ok(Arc::new(length(args[0].as_ref())?)),
+ BuiltinScalarFunction::Length => |args| Ok(length(args[0].as_ref())?),
BuiltinScalarFunction::Concat => {
|args| Ok(Arc::new(string_expressions::concatenate(args)?))
}
@@ -257,7 +266,9 @@ fn signature(fun: &BuiltinScalarFunction) -> Signature {
// for now, the list is small, as we do not have many built-in functions.
match fun {
- BuiltinScalarFunction::Length => Signature::Uniform(1, vec![DataType::Utf8]),
+ BuiltinScalarFunction::Length => {
+ Signature::Uniform(1, vec![DataType::Utf8, DataType::LargeUtf8])
+ }
BuiltinScalarFunction::Concat => Signature::Variadic(vec![DataType::Utf8]),
BuiltinScalarFunction::ToTimestamp => Signature::Uniform(1, vec![DataType::Utf8]),
BuiltinScalarFunction::Array => {
diff --git a/rust/datafusion/tests/sql.rs b/rust/datafusion/tests/sql.rs
index b4e07b644e..e158633b28 100644
--- a/rust/datafusion/tests/sql.rs
+++ b/rust/datafusion/tests/sql.rs
@@ -732,13 +732,14 @@ fn result_str(results: &[RecordBatch]) -> Vec<String> {
result
}
-#[tokio::test]
-async fn query_length() -> Result<()> {
- let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Utf8, false)]));
+async fn generic_query_length<T: 'static + Array + From<Vec<&'static str>>>(
+ datatype: DataType,
+) -> Result<()> {
+ let schema = Arc::new(Schema::new(vec![Field::new("c1", datatype, false)]));
let data = RecordBatch::try_new(
schema.clone(),
- vec![Arc::new(StringArray::from(vec!["", "a", "aa", "aaa"]))],
+ vec![Arc::new(T::from(vec!["", "a", "aa", "aaa"]))],
)?;
let table = MemTable::new(schema, vec![vec![data]])?;
@@ -752,6 +753,16 @@ async fn query_length() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn query_length() -> Result<()> {
+ generic_query_length::<StringArray>(DataType::Utf8).await
+}
+
+#[tokio::test]
+async fn query_large_length() -> Result<()> {
+ generic_query_length::<LargeStringArray>(DataType::LargeUtf8).await
+}
+
#[tokio::test]
async fn query_not() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));