20
20
use arrow:: datatypes:: DataType ;
21
21
22
22
/// Determine if a DataType is signed numeric or not
23
- pub fn is_signed_numeric ( dt : & DataType ) -> bool {
23
+ pub ( crate ) fn is_signed_numeric ( dt : & DataType ) -> bool {
24
24
matches ! (
25
25
dt,
26
26
DataType :: Int8
@@ -29,12 +29,13 @@ pub fn is_signed_numeric(dt: &DataType) -> bool {
29
29
| DataType :: Int64
30
30
| DataType :: Float16
31
31
| DataType :: Float32
32
- | DataType :: Float64
32
+ | DataType :: Float64 // TODO liukun4515
33
+ // | DataType::Decimal(_,_)
33
34
)
34
35
}
35
36
36
37
/// Determine if a DataType is numeric or not
37
- pub fn is_numeric ( dt : & DataType ) -> bool {
38
+ fn is_numeric ( dt : & DataType ) -> bool {
38
39
is_signed_numeric ( dt)
39
40
|| match dt {
40
41
DataType :: UInt8 | DataType :: UInt16 | DataType :: UInt32 | DataType :: UInt64 => {
@@ -125,6 +126,11 @@ pub fn numerical_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<Da
125
126
return Some ( lhs_type. clone ( ) ) ;
126
127
}
127
128
129
+ // TODO liukun4515
130
+ // In the decimal data type, what to do if the metadata of decimal type is diff.
131
+ // add decimal data type, diff operator we should have diff rule to do coercion.
132
+ // first step, we can just support decimal type in case which left and right datatype are the same
133
+
128
134
// these are ordered from most informative to least informative so
129
135
// that the coercion removes the least amount of information
130
136
match ( lhs_type, rhs_type) {
@@ -170,6 +176,8 @@ pub fn order_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataTy
170
176
#[ cfg( test) ]
171
177
mod tests {
172
178
use super :: * ;
179
+ use crate :: arrow:: datatypes:: DataType :: Int8 ;
180
+ use arrow:: datatypes:: DataType :: { Float32 , Float64 , Int16 , Int32 , Int64 } ;
173
181
174
182
#[ test]
175
183
fn test_dictionary_type_coersion ( ) {
@@ -192,4 +200,75 @@ mod tests {
192
200
let rhs_type = Dictionary ( Box :: new ( Int8 ) , Box :: new ( Utf8 ) ) ;
193
201
assert_eq ! ( dictionary_coercion( & lhs_type, & rhs_type) , Some ( Utf8 ) ) ;
194
202
}
203
+
204
+ #[ test]
205
+ fn test_is_signed_numeric ( ) {
206
+ assert ! ( is_signed_numeric( & DataType :: Int8 ) ) ;
207
+ assert ! ( is_signed_numeric( & DataType :: Int16 ) ) ;
208
+ assert ! ( is_signed_numeric( & DataType :: Int32 ) ) ;
209
+ assert ! ( is_signed_numeric( & DataType :: Int64 ) ) ;
210
+ assert ! ( is_signed_numeric( & DataType :: Float16 ) ) ;
211
+ assert ! ( is_signed_numeric( & DataType :: Float32 ) ) ;
212
+ assert ! ( is_signed_numeric( & DataType :: Float64 ) ) ;
213
+
214
+ // decimal data type
215
+ assert ! ( is_signed_numeric( & DataType :: Decimal ( 12 , 2 ) ) ) ;
216
+ assert ! ( is_signed_numeric( & DataType :: Decimal ( 14 , 10 ) ) ) ;
217
+
218
+ // negative test
219
+ assert ! ( !is_signed_numeric( & DataType :: UInt64 ) ) ;
220
+ assert ! ( !is_signed_numeric( & DataType :: UInt16 ) ) ;
221
+ }
222
+
223
+ #[ test]
224
+ fn test_is_numeric ( ) {
225
+ assert ! ( is_numeric( & DataType :: Int8 ) ) ;
226
+ assert ! ( is_numeric( & DataType :: Int16 ) ) ;
227
+ assert ! ( is_numeric( & DataType :: Int32 ) ) ;
228
+ assert ! ( is_numeric( & DataType :: Int64 ) ) ;
229
+ assert ! ( is_numeric( & DataType :: Float16 ) ) ;
230
+ assert ! ( is_numeric( & DataType :: Float32 ) ) ;
231
+ assert ! ( is_numeric( & DataType :: Float64 ) ) ;
232
+
233
+ // decimal data type
234
+ assert ! ( is_numeric( & DataType :: Decimal ( 12 , 2 ) ) ) ;
235
+ assert ! ( is_numeric( & DataType :: Decimal ( 14 , 10 ) ) ) ;
236
+
237
+ // unsigned test
238
+ assert ! ( is_numeric( & DataType :: UInt8 ) ) ;
239
+ assert ! ( is_numeric( & DataType :: UInt16 ) ) ;
240
+ assert ! ( is_numeric( & DataType :: UInt32 ) ) ;
241
+ assert ! ( is_numeric( & DataType :: UInt64 ) ) ;
242
+
243
+ // negative test
244
+ assert ! ( !is_numeric( & DataType :: Boolean ) ) ;
245
+ assert ! ( !is_numeric( & DataType :: Date32 ) ) ;
246
+ }
247
+
248
+ #[ test]
249
+ fn test_numerical_coercion ( ) {
250
+ // negative test
251
+ assert_eq ! (
252
+ None ,
253
+ numerical_coercion( & DataType :: Float64 , & DataType :: Binary )
254
+ ) ;
255
+ assert_eq ! (
256
+ None ,
257
+ numerical_coercion( & DataType :: Float64 , & DataType :: Utf8 )
258
+ ) ;
259
+
260
+ // positive test
261
+ let test_types = vec ! [ Int8 , Int16 , Int32 , Int64 , Float32 , Float64 ] ;
262
+ let mut index = test_types. len ( ) ;
263
+ while index > 0 {
264
+ let this_type = & test_types[ index - 1 ] ;
265
+ for i in 0 ..index {
266
+ assert_eq ! (
267
+ Some ( this_type. clone( ) ) ,
268
+ numerical_coercion( this_type, & test_types[ i] )
269
+ ) ;
270
+ }
271
+ index -= 1 ;
272
+ }
273
+ }
195
274
}
0 commit comments