1717
1818//! [`ScalarUDFImpl`] definitions for array_dims and array_ndims functions.
1919
20- use arrow:: array:: {
21- Array , ArrayRef , GenericListArray , ListArray , OffsetSizeTrait , UInt64Array ,
22- } ;
20+ use arrow:: array:: { Array , ArrayRef , ListArray , UInt64Array } ;
2321use arrow:: datatypes:: {
2422 DataType ,
25- DataType :: { LargeList , List , Null , UInt64 } ,
23+ DataType :: { FixedSizeList , LargeList , List , Null , UInt64 } ,
2624 UInt64Type ,
2725} ;
2826use std:: any:: Any ;
2927
30- use datafusion_common:: cast:: { as_large_list_array, as_list_array} ;
28+ use datafusion_common:: cast:: {
29+ as_fixed_size_list_array, as_large_list_array, as_list_array,
30+ } ;
3131use datafusion_common:: { exec_err, utils:: take_function_args, Result } ;
3232
3333use crate :: utils:: { compute_array_dims, make_scalar_function} ;
@@ -36,6 +36,7 @@ use datafusion_expr::{
3636 ColumnarValue , Documentation , ScalarUDFImpl , Signature , Volatility ,
3737} ;
3838use datafusion_macros:: user_doc;
39+ use itertools:: Itertools ;
3940use std:: sync:: Arc ;
4041
4142make_udf_expr_and_func ! (
@@ -78,7 +79,7 @@ impl Default for ArrayDims {
7879impl ArrayDims {
7980 pub fn new ( ) -> Self {
8081 Self {
81- signature : Signature :: array ( Volatility :: Immutable ) ,
82+ signature : Signature :: arrays ( 1 , None , Volatility :: Immutable ) ,
8283 aliases : vec ! [ "list_dims" . to_string( ) ] ,
8384 }
8485 }
@@ -150,7 +151,7 @@ pub(super) struct ArrayNdims {
150151impl ArrayNdims {
151152 pub fn new ( ) -> Self {
152153 Self {
153- signature : Signature :: array ( Volatility :: Immutable ) ,
154+ signature : Signature :: arrays ( 1 , None , Volatility :: Immutable ) ,
154155 aliases : vec ! [ String :: from( "list_ndims" ) ] ,
155156 }
156157 }
@@ -191,20 +192,21 @@ impl ScalarUDFImpl for ArrayNdims {
191192/// Array_dims SQL function
192193pub fn array_dims_inner ( args : & [ ArrayRef ] ) -> Result < ArrayRef > {
193194 let [ array] = take_function_args ( "array_dims" , args) ?;
194-
195- let data = match array. data_type ( ) {
195+ let data: Vec < _ > = match array. data_type ( ) {
196196 List ( _) => as_list_array ( & array) ?
197197 . iter ( )
198198 . map ( compute_array_dims)
199- . collect :: < Result < Vec < _ > > > ( ) ?,
199+ . try_collect ( ) ?,
200200 LargeList ( _) => as_large_list_array ( & array) ?
201201 . iter ( )
202202 . map ( compute_array_dims)
203- . collect :: < Result < Vec < _ > > > ( ) ?,
203+ . try_collect ( ) ?,
204+ FixedSizeList ( ..) => as_fixed_size_list_array ( & array) ?
205+ . iter ( )
206+ . map ( compute_array_dims)
207+ . try_collect ( ) ?,
204208 arg_type => {
205- return exec_err ! (
206- "array_dims does not support an argument of type {arg_type}"
207- ) ;
209+ return exec_err ! ( "array_dims does not support type {arg_type}" ) ;
208210 }
209211 } ;
210212
@@ -216,9 +218,7 @@ pub fn array_dims_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
216218pub fn array_ndims_inner ( args : & [ ArrayRef ] ) -> Result < ArrayRef > {
217219 let [ array] = take_function_args ( "array_ndims" , args) ?;
218220
219- fn general_list_ndims < O : OffsetSizeTrait > (
220- array : & GenericListArray < O > ,
221- ) -> Result < ArrayRef > {
221+ fn general_list_ndims ( array : & ArrayRef ) -> Result < ArrayRef > {
222222 let ndims = list_ndims ( array. data_type ( ) ) ;
223223 let data = vec ! [ ndims; array. len( ) ] ;
224224 let result = UInt64Array :: new ( data. into ( ) , array. nulls ( ) . cloned ( ) ) ;
@@ -227,16 +227,7 @@ pub fn array_ndims_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
227227
228228 match array. data_type ( ) {
229229 Null => Ok ( Arc :: new ( UInt64Array :: new_null ( array. len ( ) ) ) ) ,
230- List ( _) => {
231- let array = as_list_array ( array) ?;
232- general_list_ndims :: < i32 > ( array)
233- }
234- LargeList ( _) => {
235- let array = as_large_list_array ( array) ?;
236- general_list_ndims :: < i64 > ( array)
237- }
238- arg_type => {
239- exec_err ! ( "array_ndims does not support an argument of type type {arg_type}" )
240- }
230+ List ( _) | LargeList ( _) | FixedSizeList ( ..) => general_list_ndims ( array) ,
231+ arg_type => exec_err ! ( "array_ndims does not support type {arg_type}" ) ,
241232 }
242233}
0 commit comments