@@ -77,6 +77,14 @@ impl PhysicalExpr for TryCastExpr {
77
77
78
78
fn evaluate ( & self , batch : & RecordBatch ) -> Result < ColumnarValue > {
79
79
let value = self . expr . evaluate ( batch) ?;
80
+ // TODO cast signed numeric to decimal
81
+ // INT32, INT64, FLOAT, DOUBLE
82
+ // private[sql] val IntDecimal = DecimalType(3, 0)
83
+ // private[sql] val IntDecimal = DecimalType(5, 0)
84
+ // private[sql] val IntDecimal = DecimalType(10, 0)
85
+ // private[sql] val LongDecimal = DecimalType(20, 0)
86
+ // private[sql] val FloatDecimal = DecimalType(14, 7)
87
+ // private[sql] val DoubleDecimal = DecimalType(30, 15)
80
88
match value {
81
89
ColumnarValue :: Array ( array) => Ok ( ColumnarValue :: Array ( kernels:: cast:: cast (
82
90
& array,
@@ -105,6 +113,11 @@ pub fn try_cast(
105
113
if expr_type == cast_type {
106
114
Ok ( expr. clone ( ) )
107
115
} else if can_cast_types ( & expr_type, & cast_type) {
116
+ // TODO
117
+ // support numeric data type to decimal
118
+ // support one type decimal to another type decimal
119
+ // Now we just support signed numeric type to decimal,
120
+ // and will implement decimal to signed numeric data type later.
108
121
Ok ( Arc :: new ( TryCastExpr :: new ( expr, cast_type) ) )
109
122
} else {
110
123
Err ( DataFusionError :: Internal ( format ! (
@@ -121,9 +134,10 @@ mod tests {
121
134
use crate :: physical_plan:: expressions:: col;
122
135
use arrow:: array:: { StringArray , Time64NanosecondArray } ;
123
136
use arrow:: {
124
- array:: { Array , Int32Array , Int64Array , TimestampNanosecondArray , UInt32Array } ,
137
+ array:: { Array , Int8Array , Int16Array , Int32Array , Int64Array , DecimalArray , TimestampNanosecondArray , UInt32Array } ,
125
138
datatypes:: * ,
126
139
} ;
140
+ use crate :: scalar:: ScalarValue :: Decimal128 ;
127
141
128
142
// runs an end-to-end test of physical type cast
129
143
// 1. construct a record batch with a column "a" of type A
@@ -175,6 +189,122 @@ mod tests {
175
189
} } ;
176
190
}
177
191
192
+ // #[test]
193
+ fn test_try_cast_numeric_to_decimal ( ) -> Result < ( ) > {
194
+ // int8
195
+ generic_test_cast ! (
196
+ Int8Array ,
197
+ DataType :: Int8 ,
198
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
199
+ // TODO
200
+ DecimalArray ,
201
+ DataType :: Decimal ( 3 , 0 ) ,
202
+ vec![
203
+ Some ( Decimal128 ( Some ( 1 ) , 3 , 0 ) ) ,
204
+ Some ( Decimal128 ( Some ( 2 ) , 3 , 0 ) ) ,
205
+ Some ( Decimal128 ( Some ( 3 ) , 3 , 0 ) ) ,
206
+ Some ( Decimal128 ( Some ( 4 ) , 3 , 0 ) ) ,
207
+ Some ( Decimal128 ( Some ( 5 ) , 3 , 0 ) )
208
+ ]
209
+ ) ;
210
+ // int16
211
+ generic_test_cast ! (
212
+ Int16Array ,
213
+ DataType :: Int16 ,
214
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
215
+ // TODO
216
+ DecimalArray ,
217
+ DataType :: Decimal ( 5 , 0 ) ,
218
+ vec![
219
+ Some ( Decimal128 ( Some ( 1 ) , 3 , 0 ) ) ,
220
+ Some ( Decimal128 ( Some ( 2 ) , 3 , 0 ) ) ,
221
+ Some ( Decimal128 ( Some ( 3 ) , 3 , 0 ) ) ,
222
+ Some ( Decimal128 ( Some ( 4 ) , 3 , 0 ) ) ,
223
+ Some ( Decimal128 ( Some ( 5 ) , 3 , 0 ) )
224
+ ]
225
+ ) ;
226
+ // int32
227
+ generic_test_cast ! (
228
+ Int32Array ,
229
+ DataType :: Int32 ,
230
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
231
+ // TODO
232
+ DecimalArray ,
233
+ DataType :: Decimal ( 10 , 0 ) ,
234
+ vec![
235
+ Some ( Decimal128 ( Some ( 1 ) , 3 , 0 ) ) ,
236
+ Some ( Decimal128 ( Some ( 2 ) , 3 , 0 ) ) ,
237
+ Some ( Decimal128 ( Some ( 3 ) , 3 , 0 ) ) ,
238
+ Some ( Decimal128 ( Some ( 4 ) , 3 , 0 ) ) ,
239
+ Some ( Decimal128 ( Some ( 5 ) , 3 , 0 ) )
240
+ ]
241
+ ) ;
242
+ // int64
243
+ generic_test_cast ! (
244
+ Int32Array ,
245
+ DataType :: Int64 ,
246
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
247
+ // TODO
248
+ UInt32Array ,
249
+ DataType :: UInt32 ,
250
+ vec![
251
+ Some ( 1_u32 ) ,
252
+ Some ( 2_u32 ) ,
253
+ Some ( 3_u32 ) ,
254
+ Some ( 4_u32 ) ,
255
+ Some ( 5_u32 )
256
+ ]
257
+ ) ;
258
+ // float32
259
+ generic_test_cast ! (
260
+ Int32Array ,
261
+ DataType :: Float32 ,
262
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
263
+ // TODO
264
+ UInt32Array ,
265
+ DataType :: UInt32 ,
266
+ vec![
267
+ Some ( 1_u32 ) ,
268
+ Some ( 2_u32 ) ,
269
+ Some ( 3_u32 ) ,
270
+ Some ( 4_u32 ) ,
271
+ Some ( 5_u32 )
272
+ ]
273
+ ) ;
274
+ // float64
275
+ generic_test_cast ! (
276
+ Int32Array ,
277
+ DataType :: Float64 ,
278
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
279
+ // TODO
280
+ UInt32Array ,
281
+ DataType :: UInt32 ,
282
+ vec![
283
+ Some ( 1_u32 ) ,
284
+ Some ( 2_u32 ) ,
285
+ Some ( 3_u32 ) ,
286
+ Some ( 4_u32 ) ,
287
+ Some ( 5_u32 )
288
+ ]
289
+ ) ;
290
+ generic_test_cast ! (
291
+ Int32Array ,
292
+ DataType :: Decimal ( 10 , 4 ) ,
293
+ vec![ 1 , 2 , 3 , 4 , 5 ] ,
294
+ // TODO
295
+ UInt32Array ,
296
+ DataType :: UInt32 ,
297
+ vec![
298
+ Some ( 1_u32 ) ,
299
+ Some ( 2_u32 ) ,
300
+ Some ( 3_u32 ) ,
301
+ Some ( 4_u32 ) ,
302
+ Some ( 5_u32 )
303
+ ]
304
+ ) ;
305
+ Ok ( ( ) )
306
+ }
307
+
178
308
#[ test]
179
309
fn test_cast_i32_u32 ( ) -> Result < ( ) > {
180
310
generic_test_cast ! (
0 commit comments