Skip to content

Apache DataFusion SQL Query Engine

Implement native support StringView for Octet Length (#11906)

Chojan Shang5251dc91ae18
diff --git a/datafusion/functions/src/string/octet_length.rs b/datafusion/functions/src/string/octet_length.rs
index 12980fab1f..f792914d86 100644
--- a/datafusion/functions/src/string/octet_length.rs
+++ b/datafusion/functions/src/string/octet_length.rs
@@ -43,7 +43,7 @@ impl OctetLengthFunc {
Self {
signature: Signature::uniform(
1,
- vec![Utf8, LargeUtf8],
+ vec![Utf8, LargeUtf8, Utf8View],
Volatility::Immutable,
),
}
@@ -84,6 +84,9 @@ impl ScalarUDFImpl for OctetLengthFunc {
ScalarValue::LargeUtf8(v) => Ok(ColumnarValue::Scalar(
ScalarValue::Int64(v.as_ref().map(|x| x.len() as i64)),
)),
+ ScalarValue::Utf8View(v) => Ok(ColumnarValue::Scalar(
+ ScalarValue::Int32(v.as_ref().map(|x| x.len() as i32)),
+ )),
_ => unreachable!(),
},
}
@@ -176,6 +179,36 @@ mod tests {
Int32,
Int32Array
);
+ test_function!(
+ OctetLengthFunc::new(),
+ &[ColumnarValue::Scalar(ScalarValue::Utf8View(Some(
+ String::from("joséjoséjoséjosé")
+ )))],
+ Ok(Some(20)),
+ i32,
+ Int32,
+ Int32Array
+ );
+ test_function!(
+ OctetLengthFunc::new(),
+ &[ColumnarValue::Scalar(ScalarValue::Utf8View(Some(
+ String::from("josé")
+ )))],
+ Ok(Some(5)),
+ i32,
+ Int32,
+ Int32Array
+ );
+ test_function!(
+ OctetLengthFunc::new(),
+ &[ColumnarValue::Scalar(ScalarValue::Utf8View(Some(
+ String::from("")
+ )))],
+ Ok(Some(0)),
+ i32,
+ Int32,
+ Int32Array
+ );
Ok(())
}
diff --git a/datafusion/sqllogictest/test_files/string_view.slt b/datafusion/sqllogictest/test_files/string_view.slt
index a61e3830fd..7ceccf1af0 100644
--- a/datafusion/sqllogictest/test_files/string_view.slt
+++ b/datafusion/sqllogictest/test_files/string_view.slt
@@ -678,14 +678,13 @@ logical_plan
## Ensure no casts for OCTET_LENGTH
-## TODO https://github.com/apache/datafusion/issues/11858
query TT
EXPLAIN SELECT
OCTET_LENGTH(column1_utf8view) as c1
FROM test;
----
logical_plan
-01)Projection: octet_length(CAST(test.column1_utf8view AS Utf8)) AS c1
+01)Projection: octet_length(test.column1_utf8view) AS c1
02)--TableScan: test projection=[column1_utf8view]
## Ensure no casts for OVERLAY