@@ -129,9 +129,6 @@ pub fn cast_with_options(
129
129
if expr_type == cast_type {
130
130
Ok ( expr. clone ( ) )
131
131
} else if can_cast_types ( & expr_type, & cast_type) {
132
- // TODO
133
- // support numeric data type to decimal
134
- // support one type decimal to another type decimal
135
132
Ok ( Arc :: new ( CastExpr :: new ( expr, cast_type, cast_options) ) )
136
133
} else {
137
134
Err ( DataFusionError :: Internal ( format ! (
@@ -222,87 +219,120 @@ mod tests {
222
219
223
220
#[ test]
224
221
fn test_cast_numeric_to_decimal ( ) -> Result < ( ) > {
222
+ // int8
223
+ generic_test_cast ! (
224
+ Int8Array ,
225
+ DataType :: Int8 ,
226
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
227
+ DecimalArray ,
228
+ DataType :: Decimal ( 3 , 0 ) ,
229
+ vec![
230
+ Some ( Decimal128 ( Some ( 1 ) , 3 , 0 ) ) ,
231
+ Some ( Decimal128 ( Some ( 2 ) , 3 , 0 ) ) ,
232
+ Some ( Decimal128 ( Some ( 3 ) , 3 , 0 ) ) ,
233
+ Some ( Decimal128 ( Some ( 4 ) , 3 , 0 ) ) ,
234
+ Some ( Decimal128 ( Some ( 5 ) , 3 , 0 ) )
235
+ ] ,
236
+ DEFAULT_DATAFUSION_CAST_OPTIONS
237
+ ) ;
238
+
239
+ // int16
240
+ generic_test_cast ! (
241
+ Int16Array ,
242
+ DataType :: Int16 ,
243
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
244
+ DecimalArray ,
245
+ DataType :: Decimal ( 5 , 0 ) ,
246
+ vec![
247
+ Some ( Decimal128 ( Some ( 1 ) , 5 , 0 ) ) ,
248
+ Some ( Decimal128 ( Some ( 2 ) , 5 , 0 ) ) ,
249
+ Some ( Decimal128 ( Some ( 3 ) , 5 , 0 ) ) ,
250
+ Some ( Decimal128 ( Some ( 4 ) , 5 , 0 ) ) ,
251
+ Some ( Decimal128 ( Some ( 5 ) , 5 , 0 ) )
252
+ ] ,
253
+ DEFAULT_DATAFUSION_CAST_OPTIONS
254
+ ) ;
255
+
225
256
// int32
226
257
generic_test_cast ! (
227
258
Int32Array ,
228
259
DataType :: Int32 ,
229
260
vec![ 1 , 2 , 3 , 4 , 5 ] ,
230
- // TODO
231
- UInt32Array ,
232
- DataType :: UInt32 ,
261
+ DecimalArray ,
262
+ DataType :: Decimal ( 10 , 0 ) ,
233
263
vec![
234
- Some ( 1_u32 ) ,
235
- Some ( 2_u32 ) ,
236
- Some ( 3_u32 ) ,
237
- Some ( 4_u32 ) ,
238
- Some ( 5_u32 )
264
+ Some ( Decimal128 ( Some ( 1 ) , 10 , 0 ) ) ,
265
+ Some ( Decimal128 ( Some ( 2 ) , 10 , 0 ) ) ,
266
+ Some ( Decimal128 ( Some ( 3 ) , 10 , 0 ) ) ,
267
+ Some ( Decimal128 ( Some ( 4 ) , 10 , 0 ) ) ,
268
+ Some ( Decimal128 ( Some ( 5 ) , 10 , 0 ) )
239
269
] ,
240
270
DEFAULT_DATAFUSION_CAST_OPTIONS
241
271
) ;
272
+
242
273
// int64
243
274
generic_test_cast ! (
244
- Int32Array ,
275
+ Int64Array ,
245
276
DataType :: Int64 ,
246
277
vec![ 1 , 2 , 3 , 4 , 5 ] ,
247
- // TODO
248
- UInt32Array ,
249
- DataType :: UInt32 ,
278
+ DecimalArray ,
279
+ DataType :: Decimal ( 20 , 0 ) ,
250
280
vec![
251
- Some ( 1_u32 ) ,
252
- Some ( 2_u32 ) ,
253
- Some ( 3_u32 ) ,
254
- Some ( 4_u32 ) ,
255
- Some ( 5_u32 )
281
+ Some ( Decimal128 ( Some ( 1 ) , 20 , 0 ) ) ,
282
+ Some ( Decimal128 ( Some ( 2 ) , 20 , 0 ) ) ,
283
+ Some ( Decimal128 ( Some ( 3 ) , 20 , 0 ) ) ,
284
+ Some ( Decimal128 ( Some ( 4 ) , 20 , 0 ) ) ,
285
+ Some ( Decimal128 ( Some ( 5 ) , 20 , 0 ) )
256
286
] ,
257
287
DEFAULT_DATAFUSION_CAST_OPTIONS
258
288
) ;
259
- // float32
289
+
260
290
generic_test_cast ! (
261
- Int32Array ,
262
- DataType :: Float32 ,
291
+ Int64Array ,
292
+ DataType :: Int64 ,
263
293
vec![ 1 , 2 , 3 , 4 , 5 ] ,
264
- // TODO
265
- UInt32Array ,
266
- DataType :: UInt32 ,
294
+ DecimalArray ,
295
+ DataType :: Decimal ( 20 , 2 ) ,
267
296
vec![
268
- Some ( 1_u32 ) ,
269
- Some ( 2_u32 ) ,
270
- Some ( 3_u32 ) ,
271
- Some ( 4_u32 ) ,
272
- Some ( 5_u32 )
297
+ Some ( Decimal128 ( Some ( 100 ) , 20 , 2 ) ) ,
298
+ Some ( Decimal128 ( Some ( 200 ) , 20 , 2 ) ) ,
299
+ Some ( Decimal128 ( Some ( 300 ) , 20 , 2 ) ) ,
300
+ Some ( Decimal128 ( Some ( 400 ) , 20 , 2 ) ) ,
301
+ Some ( Decimal128 ( Some ( 500 ) , 20 , 2 ) )
273
302
] ,
274
303
DEFAULT_DATAFUSION_CAST_OPTIONS
275
304
) ;
276
- // float64
305
+
306
+ // float32
277
307
generic_test_cast ! (
278
- Int32Array ,
279
- DataType :: Float64 ,
280
- vec![ 1 , 2 , 3 , 4 , 5 ] ,
281
- // TODO
282
- UInt32Array ,
283
- DataType :: UInt32 ,
308
+ Float32Array ,
309
+ DataType :: Float32 ,
310
+ vec![ 1.5 , 2.5 , 3 , 1.123_456_8 , 5.50 ] ,
311
+ DecimalArray ,
312
+ DataType :: Decimal ( 10 , 2 ) ,
284
313
vec![
285
- Some ( 1_u32 ) ,
286
- Some ( 2_u32 ) ,
287
- Some ( 3_u32 ) ,
288
- Some ( 4_u32 ) ,
289
- Some ( 5_u32 )
314
+ Some ( Decimal128 ( Some ( 150 ) , 20 , 2 ) ) ,
315
+ Some ( Decimal128 ( Some ( 250 ) , 20 , 2 ) ) ,
316
+ Some ( Decimal128 ( Some ( 300 ) , 20 , 2 ) ) ,
317
+ Some ( Decimal128 ( Some ( 112 ) , 20 , 2 ) ) ,
318
+ Some ( Decimal128 ( Some ( 550 ) , 20 , 2 ) )
290
319
] ,
291
320
DEFAULT_DATAFUSION_CAST_OPTIONS
292
321
) ;
322
+
323
+ // float64
293
324
generic_test_cast ! (
294
- Int32Array ,
295
- DataType :: Decimal ( 10 , 4 ) ,
296
- vec![ 1 , 2 , 3 , 4 , 5 ] ,
297
- // TODO
298
- UInt32Array ,
299
- DataType :: UInt32 ,
325
+ Float64Array ,
326
+ DataType :: Float64 ,
327
+ vec![ 1.5 , 2.5 , 3 , 1.123_456_8 , 5.50 ] ,
328
+ DecimalArray ,
329
+ DataType :: Decimal ( 20 , 4 ) ,
300
330
vec![
301
- Some ( 1_u32 ) ,
302
- Some ( 2_u32 ) ,
303
- Some ( 3_u32 ) ,
304
- Some ( 4_u32 ) ,
305
- Some ( 5_u32 )
331
+ Some ( Decimal128 ( Some ( 15000 ) , 20 , 4 ) ) ,
332
+ Some ( Decimal128 ( Some ( 25000 ) , 20 , 4 ) ) ,
333
+ Some ( Decimal128 ( Some ( 30000 ) , 20 , 4 ) ) ,
334
+ Some ( Decimal128 ( Some ( 11234 ) , 20 , 4 ) ) ,
335
+ Some ( Decimal128 ( Some ( 55000 ) , 20 , 4 ) )
306
336
] ,
307
337
DEFAULT_DATAFUSION_CAST_OPTIONS
308
338
) ;
0 commit comments