@@ -48,7 +48,7 @@ fn bit_length_impl<P: ArrowPrimitiveType>(
48
48
/// For list array, length is the number of elements in each list.
49
49
/// For string array and binary array, length is the number of bytes of each value.
50
50
///
51
- /// * this only accepts ListArray/LargeListArray, StringArray/LargeStringArray, BinaryArray/LargeBinaryArray, and FixedSizeListArray,
51
+ /// * this only accepts ListArray/LargeListArray, StringArray/LargeStringArray/StringViewArray , BinaryArray/LargeBinaryArray, and FixedSizeListArray,
52
52
/// or DictionaryArray with above Arrays as values
53
53
/// * length of null is null.
54
54
pub fn length ( array : & dyn Array ) -> Result < ArrayRef , ArrowError > {
@@ -74,6 +74,14 @@ pub fn length(array: &dyn Array) -> Result<ArrayRef, ArrowError> {
74
74
let list = array. as_string :: < i64 > ( ) ;
75
75
Ok ( length_impl :: < Int64Type > ( list. offsets ( ) , list. nulls ( ) ) )
76
76
}
77
+ DataType :: Utf8View => {
78
+ let list = array. as_string_view ( ) ;
79
+ let v = list. views ( ) . iter ( ) . map ( |v| * v as i32 ) . collect :: < Vec < _ > > ( ) ;
80
+ Ok ( Arc :: new ( PrimitiveArray :: < Int32Type > :: new (
81
+ v. into ( ) ,
82
+ list. nulls ( ) . cloned ( ) ,
83
+ ) ) )
84
+ }
77
85
DataType :: Binary => {
78
86
let list = array. as_binary :: < i32 > ( ) ;
79
87
Ok ( length_impl :: < Int32Type > ( list. offsets ( ) , list. nulls ( ) ) )
@@ -147,9 +155,15 @@ mod tests {
147
155
148
156
fn length_cases_string ( ) -> Vec < ( Vec < & ' static str > , usize , Vec < i32 > ) > {
149
157
// a large array
150
- let values = [ "one" , "on" , "o" , "" ] ;
158
+ let values = [
159
+ "one" ,
160
+ "on" ,
161
+ "o" ,
162
+ "" ,
163
+ "this is a longer string to test string array with" ,
164
+ ] ;
151
165
let values = values. into_iter ( ) . cycle ( ) . take ( 4096 ) . collect ( ) ;
152
- let expected = [ 3 , 2 , 1 , 0 ] . into_iter ( ) . cycle ( ) . take ( 4096 ) . collect ( ) ;
166
+ let expected = [ 3 , 2 , 1 , 0 , 49 ] . into_iter ( ) . cycle ( ) . take ( 4096 ) . collect ( ) ;
153
167
154
168
vec ! [
155
169
( vec![ "hello" , " " , "world" ] , 3 , vec![ 5 , 1 , 5 ] ) ,
@@ -210,6 +224,21 @@ mod tests {
210
224
} )
211
225
}
212
226
227
+ #[ test]
228
+ fn length_test_string_view ( ) {
229
+ length_cases_string ( )
230
+ . into_iter ( )
231
+ . for_each ( |( input, len, expected) | {
232
+ let array = StringViewArray :: from ( input) ;
233
+ let result = length ( & array) . unwrap ( ) ;
234
+ assert_eq ! ( len, result. len( ) ) ;
235
+ let result = result. as_any ( ) . downcast_ref :: < Int32Array > ( ) . unwrap ( ) ;
236
+ expected. iter ( ) . enumerate ( ) . for_each ( |( i, value) | {
237
+ assert_eq ! ( * value, result. value( i) ) ;
238
+ } ) ;
239
+ } )
240
+ }
241
+
213
242
#[ test]
214
243
fn length_test_binary ( ) {
215
244
let value: Vec < & [ u8 ] > = vec ! [ b"zero" , b"one" , & [ 0xff , 0xf8 ] ] ;
0 commit comments