1818use std:: any:: Any ;
1919use std:: sync:: Arc ;
2020
21- use arrow:: array:: { ArrayRef , OffsetSizeTrait } ;
21+ use arrow:: array:: ArrayRef ;
2222use 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 } ;
2725use datafusion_expr:: TypeSignature :: * ;
2826use datafusion_expr:: { ColumnarValue , Volatility } ;
2927use datafusion_expr:: { ScalarUDFImpl , Signature } ;
@@ -43,14 +41,15 @@ impl Default for EndsWithFunc {
4341
4442impl 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}
0 commit comments