@@ -19,11 +19,11 @@ use std::any::Any;
19
19
use std:: sync:: Arc ;
20
20
21
21
use arrow:: array:: {
22
- ArrayRef , ArrowPrimitiveType , GenericStringArray , OffsetSizeTrait , PrimitiveArray ,
22
+ ArrayAccessor , ArrayIter , ArrayRef , ArrowPrimitiveType , AsArray , OffsetSizeTrait ,
23
+ PrimitiveArray ,
23
24
} ;
24
25
use arrow:: datatypes:: { ArrowNativeType , DataType , Int32Type , Int64Type } ;
25
26
26
- use datafusion_common:: cast:: as_generic_string_array;
27
27
use datafusion_common:: { exec_err, Result } ;
28
28
use datafusion_expr:: TypeSignature :: Exact ;
29
29
use datafusion_expr:: { ColumnarValue , ScalarUDFImpl , Signature , Volatility } ;
@@ -46,7 +46,11 @@ impl FindInSetFunc {
46
46
use DataType :: * ;
47
47
Self {
48
48
signature : Signature :: one_of (
49
- vec ! [ Exact ( vec![ Utf8 , Utf8 ] ) , Exact ( vec![ LargeUtf8 , LargeUtf8 ] ) ] ,
49
+ vec ! [
50
+ Exact ( vec![ Utf8View , Utf8View ] ) ,
51
+ Exact ( vec![ Utf8 , Utf8 ] ) ,
52
+ Exact ( vec![ LargeUtf8 , LargeUtf8 ] ) ,
53
+ ] ,
50
54
Volatility :: Immutable ,
51
55
) ,
52
56
}
@@ -71,41 +75,52 @@ impl ScalarUDFImpl for FindInSetFunc {
71
75
}
72
76
73
77
fn invoke ( & self , args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
74
- match args[ 0 ] . data_type ( ) {
75
- DataType :: Utf8 => {
76
- make_scalar_function ( find_in_set :: < Int32Type > , vec ! [ ] ) ( args)
77
- }
78
- DataType :: LargeUtf8 => {
79
- make_scalar_function ( find_in_set :: < Int64Type > , vec ! [ ] ) ( args)
80
- }
81
- other => {
82
- exec_err ! ( "Unsupported data type {other:?} for function find_in_set" )
83
- }
84
- }
78
+ make_scalar_function ( find_in_set, vec ! [ ] ) ( args)
85
79
}
86
80
}
87
81
88
82
///Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings
89
83
///A string list is a string composed of substrings separated by , characters.
90
- pub fn find_in_set < T : ArrowPrimitiveType > ( args : & [ ArrayRef ] ) -> Result < ArrayRef >
91
- where
92
- T :: Native : OffsetSizeTrait ,
93
- {
84
+ fn find_in_set ( args : & [ ArrayRef ] ) -> Result < ArrayRef > {
94
85
if args. len ( ) != 2 {
95
86
return exec_err ! (
96
87
"find_in_set was called with {} arguments. It requires 2." ,
97
88
args. len( )
98
89
) ;
99
90
}
91
+ match args[ 0 ] . data_type ( ) {
92
+ DataType :: Utf8 => {
93
+ let string_array = args[ 0 ] . as_string :: < i32 > ( ) ;
94
+ let str_list_array = args[ 1 ] . as_string :: < i32 > ( ) ;
95
+ find_in_set_general :: < Int32Type , _ > ( string_array, str_list_array)
96
+ }
97
+ DataType :: LargeUtf8 => {
98
+ let string_array = args[ 0 ] . as_string :: < i64 > ( ) ;
99
+ let str_list_array = args[ 1 ] . as_string :: < i64 > ( ) ;
100
+ find_in_set_general :: < Int64Type , _ > ( string_array, str_list_array)
101
+ }
102
+ DataType :: Utf8View => {
103
+ let string_array = args[ 0 ] . as_string_view ( ) ;
104
+ let str_list_array = args[ 1 ] . as_string_view ( ) ;
105
+ find_in_set_general :: < Int32Type , _ > ( string_array, str_list_array)
106
+ }
107
+ other => {
108
+ exec_err ! ( "Unsupported data type {other:?} for function find_in_set" )
109
+ }
110
+ }
111
+ }
100
112
101
- let str_array: & GenericStringArray < T :: Native > =
102
- as_generic_string_array :: < T :: Native > ( & args[ 0 ] ) ?;
103
- let str_list_array: & GenericStringArray < T :: Native > =
104
- as_generic_string_array :: < T :: Native > ( & args[ 1 ] ) ?;
105
-
106
- let result = str_array
107
- . iter ( )
108
- . zip ( str_list_array. iter ( ) )
113
+ pub fn find_in_set_general < ' a , T : ArrowPrimitiveType , V : ArrayAccessor < Item = & ' a str > > (
114
+ string_array : V ,
115
+ str_list_array : V ,
116
+ ) -> Result < ArrayRef >
117
+ where
118
+ T :: Native : OffsetSizeTrait ,
119
+ {
120
+ let string_iter = ArrayIter :: new ( string_array) ;
121
+ let str_list_iter = ArrayIter :: new ( str_list_array) ;
122
+ let result = string_iter
123
+ . zip ( str_list_iter)
109
124
. map ( |( string, str_list) | match ( string, str_list) {
110
125
( Some ( string) , Some ( str_list) ) => {
111
126
let mut res = 0 ;
0 commit comments