18
18
use std:: any:: Any ;
19
19
use std:: sync:: Arc ;
20
20
21
- use arrow:: array:: { ArrayRef , OffsetSizeTrait } ;
21
+ use arrow:: array:: ArrayRef ;
22
22
use arrow:: datatypes:: DataType ;
23
- use arrow:: datatypes:: DataType :: Boolean ;
24
23
25
- use datafusion_common:: cast:: as_generic_string_array;
26
- use datafusion_common:: { exec_err, Result } ;
24
+ use datafusion_common:: { internal_err, Result } ;
27
25
use datafusion_expr:: TypeSignature :: * ;
28
26
use datafusion_expr:: { ColumnarValue , Volatility } ;
29
27
use datafusion_expr:: { ScalarUDFImpl , Signature } ;
@@ -43,14 +41,15 @@ impl Default for EndsWithFunc {
43
41
44
42
impl EndsWithFunc {
45
43
pub fn new ( ) -> Self {
46
- use DataType :: * ;
47
44
Self {
48
45
signature : Signature :: one_of (
49
46
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 ] ) ,
54
53
] ,
55
54
Volatility :: Immutable ,
56
55
) ,
@@ -72,27 +71,25 @@ impl ScalarUDFImpl for EndsWithFunc {
72
71
}
73
72
74
73
fn return_type ( & self , _arg_types : & [ DataType ] ) -> Result < DataType > {
75
- Ok ( Boolean )
74
+ Ok ( DataType :: Boolean )
76
75
}
77
76
78
77
fn invoke ( & self , args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
79
78
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
+ }
82
82
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" ) ?
84
84
}
85
85
}
86
86
}
87
87
}
88
88
89
89
/// Returns true if string ends with suffix.
90
90
/// 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 ] ) ?;
96
93
97
94
Ok ( Arc :: new ( result) as ArrayRef )
98
95
}
0 commit comments