@@ -23,13 +23,12 @@ use arrow::datatypes::{DataType, Field};
23
23
use arrow_array:: types:: { Date32Type , IntervalMonthDayNanoType } ;
24
24
use arrow_array:: NullArray ;
25
25
use arrow_buffer:: { BooleanBufferBuilder , NullBuffer , OffsetBuffer } ;
26
- use arrow_schema:: DataType :: { Date32 , Int64 , Interval , List } ;
26
+ use arrow_schema:: DataType :: * ;
27
27
use arrow_schema:: IntervalUnit :: MonthDayNano ;
28
28
use datafusion_common:: cast:: { as_date32_array, as_int64_array, as_interval_mdn_array} ;
29
29
use datafusion_common:: { exec_err, not_impl_datafusion_err, Result } ;
30
- use datafusion_expr:: {
31
- ColumnarValue , ScalarUDFImpl , Signature , TypeSignature , Volatility ,
32
- } ;
30
+ use datafusion_expr:: { ColumnarValue , ScalarUDFImpl , Signature , Volatility } ;
31
+ use itertools:: Itertools ;
33
32
use std:: any:: Any ;
34
33
use std:: iter:: from_fn;
35
34
use std:: sync:: Arc ;
@@ -49,16 +48,7 @@ pub(super) struct Range {
49
48
impl Range {
50
49
pub fn new ( ) -> Self {
51
50
Self {
52
- signature : Signature :: one_of (
53
- vec ! [
54
- TypeSignature :: Exact ( vec![ Int64 ] ) ,
55
- TypeSignature :: Exact ( vec![ Int64 , Int64 ] ) ,
56
- TypeSignature :: Exact ( vec![ Int64 , Int64 , Int64 ] ) ,
57
- TypeSignature :: Exact ( vec![ Date32 , Date32 , Interval ( MonthDayNano ) ] ) ,
58
- TypeSignature :: Any ( 3 ) ,
59
- ] ,
60
- Volatility :: Immutable ,
61
- ) ,
51
+ signature : Signature :: user_defined ( Volatility :: Immutable ) ,
62
52
aliases : vec ! [ ] ,
63
53
}
64
54
}
@@ -75,9 +65,34 @@ impl ScalarUDFImpl for Range {
75
65
& self . signature
76
66
}
77
67
68
+ fn coerce_types ( & self , arg_types : & [ DataType ] ) -> Result < Vec < DataType > > {
69
+ arg_types
70
+ . iter ( )
71
+ . map ( |arg_type| match arg_type {
72
+ Null => Ok ( Null ) ,
73
+ Int8 => Ok ( Int64 ) ,
74
+ Int16 => Ok ( Int64 ) ,
75
+ Int32 => Ok ( Int64 ) ,
76
+ Int64 => Ok ( Int64 ) ,
77
+ UInt8 => Ok ( Int64 ) ,
78
+ UInt16 => Ok ( Int64 ) ,
79
+ UInt32 => Ok ( Int64 ) ,
80
+ UInt64 => Ok ( Int64 ) ,
81
+ Timestamp ( _, _) => Ok ( Date32 ) ,
82
+ Date32 => Ok ( Date32 ) ,
83
+ Date64 => Ok ( Date32 ) ,
84
+ Utf8 => Ok ( Date32 ) ,
85
+ LargeUtf8 => Ok ( Date32 ) ,
86
+ Utf8View => Ok ( Date32 ) ,
87
+ Interval ( _) => Ok ( Interval ( MonthDayNano ) ) ,
88
+ _ => exec_err ! ( "Unsupported DataType" ) ,
89
+ } )
90
+ . try_collect ( )
91
+ }
92
+
78
93
fn return_type ( & self , arg_types : & [ DataType ] ) -> Result < DataType > {
79
- if arg_types. iter ( ) . any ( |t| t. eq ( & DataType :: Null ) ) {
80
- Ok ( DataType :: Null )
94
+ if arg_types. iter ( ) . any ( |t| t. is_null ( ) ) {
95
+ Ok ( Null )
81
96
} else {
82
97
Ok ( List ( Arc :: new ( Field :: new (
83
98
"item" ,
@@ -88,7 +103,7 @@ impl ScalarUDFImpl for Range {
88
103
}
89
104
90
105
fn invoke ( & self , args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
91
- if args. iter ( ) . any ( |arg| arg. data_type ( ) == DataType :: Null ) {
106
+ if args. iter ( ) . any ( |arg| arg. data_type ( ) . is_null ( ) ) {
92
107
return Ok ( ColumnarValue :: Array ( Arc :: new ( NullArray :: new ( 1 ) ) ) ) ;
93
108
}
94
109
match args[ 0 ] . data_type ( ) {
@@ -120,16 +135,7 @@ pub(super) struct GenSeries {
120
135
impl GenSeries {
121
136
pub fn new ( ) -> Self {
122
137
Self {
123
- signature : Signature :: one_of (
124
- vec ! [
125
- TypeSignature :: Exact ( vec![ Int64 ] ) ,
126
- TypeSignature :: Exact ( vec![ Int64 , Int64 ] ) ,
127
- TypeSignature :: Exact ( vec![ Int64 , Int64 , Int64 ] ) ,
128
- TypeSignature :: Exact ( vec![ Date32 , Date32 , Interval ( MonthDayNano ) ] ) ,
129
- TypeSignature :: Any ( 3 ) ,
130
- ] ,
131
- Volatility :: Immutable ,
132
- ) ,
138
+ signature : Signature :: user_defined ( Volatility :: Immutable ) ,
133
139
aliases : vec ! [ ] ,
134
140
}
135
141
}
@@ -146,9 +152,34 @@ impl ScalarUDFImpl for GenSeries {
146
152
& self . signature
147
153
}
148
154
155
+ fn coerce_types ( & self , _arg_types : & [ DataType ] ) -> Result < Vec < DataType > > {
156
+ _arg_types
157
+ . iter ( )
158
+ . map ( |arg_type| match arg_type {
159
+ Null => Ok ( Null ) ,
160
+ Int8 => Ok ( Int64 ) ,
161
+ Int16 => Ok ( Int64 ) ,
162
+ Int32 => Ok ( Int64 ) ,
163
+ Int64 => Ok ( Int64 ) ,
164
+ UInt8 => Ok ( Int64 ) ,
165
+ UInt16 => Ok ( Int64 ) ,
166
+ UInt32 => Ok ( Int64 ) ,
167
+ UInt64 => Ok ( Int64 ) ,
168
+ Timestamp ( _, _) => Ok ( Date32 ) ,
169
+ Date32 => Ok ( Date32 ) ,
170
+ Date64 => Ok ( Date32 ) ,
171
+ Utf8 => Ok ( Date32 ) ,
172
+ LargeUtf8 => Ok ( Date32 ) ,
173
+ Utf8View => Ok ( Date32 ) ,
174
+ Interval ( _) => Ok ( Interval ( MonthDayNano ) ) ,
175
+ _ => exec_err ! ( "Unsupported DataType" ) ,
176
+ } )
177
+ . try_collect ( )
178
+ }
179
+
149
180
fn return_type ( & self , arg_types : & [ DataType ] ) -> Result < DataType > {
150
- if arg_types. iter ( ) . any ( |t| t. eq ( & DataType :: Null ) ) {
151
- Ok ( DataType :: Null )
181
+ if arg_types. iter ( ) . any ( |t| t. is_null ( ) ) {
182
+ Ok ( Null )
152
183
} else {
153
184
Ok ( List ( Arc :: new ( Field :: new (
154
185
"item" ,
@@ -159,15 +190,15 @@ impl ScalarUDFImpl for GenSeries {
159
190
}
160
191
161
192
fn invoke ( & self , args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
162
- if args. iter ( ) . any ( |arg| arg. data_type ( ) == DataType :: Null ) {
193
+ if args. iter ( ) . any ( |arg| arg. data_type ( ) . is_null ( ) ) {
163
194
return Ok ( ColumnarValue :: Array ( Arc :: new ( NullArray :: new ( 1 ) ) ) ) ;
164
195
}
165
196
match args[ 0 ] . data_type ( ) {
166
197
Int64 => make_scalar_function ( |args| gen_range_inner ( args, true ) ) ( args) ,
167
198
Date32 => make_scalar_function ( |args| gen_range_date ( args, true ) ) ( args) ,
168
199
dt => {
169
200
exec_err ! (
170
- "unsupported type for range . Expected Int64 or Date32, got: {}" ,
201
+ "unsupported type for gen_series . Expected Int64 or Date32, got: {}" ,
171
202
dt
172
203
)
173
204
}
0 commit comments