Skip to content

Commit 8deba02

Browse files
authored
Implement native support StringView for Ends With (#11924)
Signed-off-by: Chojan Shang <[email protected]>
1 parent 032b9c9 commit 8deba02

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

datafusion/functions/src/string/ends_with.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
use std::any::Any;
1919
use std::sync::Arc;
2020

21-
use arrow::array::{ArrayRef, OffsetSizeTrait};
21+
use arrow::array::ArrayRef;
2222
use arrow::datatypes::DataType;
23-
use arrow::datatypes::DataType::Boolean;
2423

25-
use datafusion_common::cast::as_generic_string_array;
26-
use datafusion_common::{exec_err, Result};
24+
use datafusion_common::{internal_err, Result};
2725
use datafusion_expr::TypeSignature::*;
2826
use datafusion_expr::{ColumnarValue, Volatility};
2927
use datafusion_expr::{ScalarUDFImpl, Signature};
@@ -43,14 +41,15 @@ impl Default for EndsWithFunc {
4341

4442
impl EndsWithFunc {
4543
pub fn new() -> Self {
46-
use DataType::*;
4744
Self {
4845
signature: Signature::one_of(
4946
vec![
50-
Exact(vec![Utf8, Utf8]),
51-
Exact(vec![Utf8, LargeUtf8]),
52-
Exact(vec![LargeUtf8, Utf8]),
53-
Exact(vec![LargeUtf8, LargeUtf8]),
47+
// Planner attempts coercion to the target type starting with the most preferred candidate.
48+
// For example, given input `(Utf8View, Utf8)`, it first tries coercing to `(Utf8View, Utf8View)`.
49+
// If that fails, it proceeds to `(Utf8, Utf8)`.
50+
Exact(vec![DataType::Utf8View, DataType::Utf8View]),
51+
Exact(vec![DataType::Utf8, DataType::Utf8]),
52+
Exact(vec![DataType::LargeUtf8, DataType::LargeUtf8]),
5453
],
5554
Volatility::Immutable,
5655
),
@@ -72,27 +71,25 @@ impl ScalarUDFImpl for EndsWithFunc {
7271
}
7372

7473
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
75-
Ok(Boolean)
74+
Ok(DataType::Boolean)
7675
}
7776

7877
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
7978
match args[0].data_type() {
80-
DataType::Utf8 => make_scalar_function(ends_with::<i32>, vec![])(args),
81-
DataType::LargeUtf8 => make_scalar_function(ends_with::<i64>, vec![])(args),
79+
DataType::Utf8View | DataType::Utf8 | DataType::LargeUtf8 => {
80+
make_scalar_function(ends_with, vec![])(args)
81+
}
8282
other => {
83-
exec_err!("Unsupported data type {other:?} for function ends_with")
83+
internal_err!("Unsupported data type {other:?} for function ends_with. Expected Utf8, LargeUtf8 or Utf8View")?
8484
}
8585
}
8686
}
8787
}
8888

8989
/// Returns true if string ends with suffix.
9090
/// ends_with('alphabet', 'abet') = 't'
91-
pub fn ends_with<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
92-
let left = as_generic_string_array::<T>(&args[0])?;
93-
let right = as_generic_string_array::<T>(&args[1])?;
94-
95-
let result = arrow::compute::kernels::comparison::ends_with(left, right)?;
91+
pub fn ends_with(args: &[ArrayRef]) -> Result<ArrayRef> {
92+
let result = arrow::compute::kernels::comparison::ends_with(&args[0], &args[1])?;
9693

9794
Ok(Arc::new(result) as ArrayRef)
9895
}

datafusion/sqllogictest/test_files/string_view.slt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,17 +618,15 @@ logical_plan
618618
03)----TableScan: test projection=[column1_utf8view, column2_utf8view]
619619

620620
## Ensure no casts for ENDS_WITH
621-
## TODO https://github.com/apache/datafusion/issues/11852
622621
query TT
623622
EXPLAIN SELECT
624623
ENDS_WITH(column1_utf8view, 'foo') as c1,
625624
ENDS_WITH(column2_utf8view, column2_utf8view) as c2
626625
FROM test;
627626
----
628627
logical_plan
629-
01)Projection: ends_with(CAST(test.column1_utf8view AS Utf8), Utf8("foo")) AS c1, ends_with(__common_expr_1, __common_expr_1) AS c2
630-
02)--Projection: CAST(test.column2_utf8view AS Utf8) AS __common_expr_1, test.column1_utf8view
631-
03)----TableScan: test projection=[column1_utf8view, column2_utf8view]
628+
01)Projection: ends_with(test.column1_utf8view, Utf8View("foo")) AS c1, ends_with(test.column2_utf8view, test.column2_utf8view) AS c2
629+
02)--TableScan: test projection=[column1_utf8view, column2_utf8view]
632630

633631
## Ensure no casts for LEVENSHTEIN
634632
## TODO https://github.com/apache/datafusion/issues/11854

0 commit comments

Comments
 (0)