@@ -19,7 +19,7 @@ use std::any::Any;
19
19
use std:: sync:: Arc ;
20
20
21
21
use arrow:: array:: cast:: AsArray ;
22
- use arrow:: array:: { Array , ArrayRef , StringArray } ;
22
+ use arrow:: array:: { new_null_array , Array , ArrayRef , StringArray } ;
23
23
use arrow:: datatypes:: DataType ;
24
24
use arrow:: datatypes:: DataType :: {
25
25
Date32 , Date64 , Duration , Time32 , Time64 , Timestamp , Utf8 ,
@@ -109,7 +109,6 @@ impl ScalarUDFImpl for ToCharFunc {
109
109
}
110
110
111
111
match & args[ 1 ] {
112
- // null format, use default formats
113
112
ColumnarValue :: Scalar ( ScalarValue :: Utf8 ( None ) )
114
113
| ColumnarValue :: Scalar ( ScalarValue :: Null ) => {
115
114
_to_char_scalar ( args[ 0 ] . clone ( ) , None )
@@ -175,6 +174,18 @@ fn _to_char_scalar(
175
174
let data_type = & expression. data_type ( ) ;
176
175
let is_scalar_expression = matches ! ( & expression, ColumnarValue :: Scalar ( _) ) ;
177
176
let array = expression. into_array ( 1 ) ?;
177
+
178
+ if format. is_none ( ) {
179
+ if is_scalar_expression {
180
+ return Ok ( ColumnarValue :: Scalar ( ScalarValue :: Utf8 ( None ) ) ) ;
181
+ } else {
182
+ return Ok ( ColumnarValue :: Array ( new_null_array (
183
+ & DataType :: Utf8 ,
184
+ array. len ( ) ,
185
+ ) ) ) ;
186
+ }
187
+ }
188
+
178
189
let format_options = match _build_format_options ( data_type, format) {
179
190
Ok ( value) => value,
180
191
Err ( value) => return value,
@@ -202,7 +213,7 @@ fn _to_char_scalar(
202
213
203
214
fn _to_char_array ( args : & [ ColumnarValue ] ) -> Result < ColumnarValue > {
204
215
let arrays = ColumnarValue :: values_to_arrays ( args) ?;
205
- let mut results: Vec < String > = vec ! [ ] ;
216
+ let mut results: Vec < Option < String > > = vec ! [ ] ;
206
217
let format_array = arrays[ 1 ] . as_string :: < i32 > ( ) ;
207
218
let data_type = arrays[ 0 ] . data_type ( ) ;
208
219
@@ -212,6 +223,10 @@ fn _to_char_array(args: &[ColumnarValue]) -> Result<ColumnarValue> {
212
223
} else {
213
224
Some ( format_array. value ( idx) )
214
225
} ;
226
+ if format. is_none ( ) {
227
+ results. push ( None ) ;
228
+ continue ;
229
+ }
215
230
let format_options = match _build_format_options ( data_type, format) {
216
231
Ok ( value) => value,
217
232
Err ( value) => return value,
@@ -221,7 +236,7 @@ fn _to_char_array(args: &[ColumnarValue]) -> Result<ColumnarValue> {
221
236
let formatter = ArrayFormatter :: try_new ( arrays[ 0 ] . as_ref ( ) , & format_options) ?;
222
237
let result = formatter. value ( idx) . try_to_string ( ) ;
223
238
match result {
224
- Ok ( value) => results. push ( value) ,
239
+ Ok ( value) => results. push ( Some ( value) ) ,
225
240
Err ( e) => return exec_err ! ( "{}" , e) ,
226
241
}
227
242
}
@@ -230,9 +245,12 @@ fn _to_char_array(args: &[ColumnarValue]) -> Result<ColumnarValue> {
230
245
ColumnarValue :: Array ( _) => Ok ( ColumnarValue :: Array ( Arc :: new ( StringArray :: from (
231
246
results,
232
247
) ) as ArrayRef ) ) ,
233
- ColumnarValue :: Scalar ( _) => Ok ( ColumnarValue :: Scalar ( ScalarValue :: Utf8 ( Some (
234
- results. first ( ) . unwrap ( ) . to_string ( ) ,
235
- ) ) ) ) ,
248
+ ColumnarValue :: Scalar ( _) => match results. first ( ) . unwrap ( ) {
249
+ Some ( value) => Ok ( ColumnarValue :: Scalar ( ScalarValue :: Utf8 ( Some (
250
+ value. to_string ( ) ,
251
+ ) ) ) ) ,
252
+ None => Ok ( ColumnarValue :: Scalar ( ScalarValue :: Utf8 ( None ) ) ) ,
253
+ } ,
236
254
}
237
255
}
238
256
0 commit comments