@@ -38,6 +38,7 @@ use crate::datatypes::{
38
38
use crate :: downcast_dictionary_array;
39
39
use crate :: error:: { ArrowError , Result } ;
40
40
use crate :: util:: bit_util;
41
+ use num:: ToPrimitive ;
41
42
use regex:: Regex ;
42
43
use std:: collections:: HashMap ;
43
44
@@ -1328,7 +1329,11 @@ macro_rules! dyn_compare_utf8_scalar {
1328
1329
}
1329
1330
1330
1331
/// Perform `left == right` operation on an array and a numeric scalar
1331
- /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
1332
+ /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values.
1333
+ ///
1334
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
1335
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
1336
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
1332
1337
pub fn eq_dyn_scalar < T > ( left : & dyn Array , right : T ) -> Result < BooleanArray >
1333
1338
where
1334
1339
T : num:: ToPrimitive + std:: fmt:: Debug ,
@@ -1342,7 +1347,11 @@ where
1342
1347
}
1343
1348
1344
1349
/// Perform `left < right` operation on an array and a numeric scalar
1345
- /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
1350
+ /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values.
1351
+ ///
1352
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
1353
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
1354
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
1346
1355
pub fn lt_dyn_scalar < T > ( left : & dyn Array , right : T ) -> Result < BooleanArray >
1347
1356
where
1348
1357
T : num:: ToPrimitive + std:: fmt:: Debug ,
@@ -1356,7 +1365,11 @@ where
1356
1365
}
1357
1366
1358
1367
/// Perform `left <= right` operation on an array and a numeric scalar
1359
- /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
1368
+ /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values.
1369
+ ///
1370
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
1371
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
1372
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
1360
1373
pub fn lt_eq_dyn_scalar < T > ( left : & dyn Array , right : T ) -> Result < BooleanArray >
1361
1374
where
1362
1375
T : num:: ToPrimitive + std:: fmt:: Debug ,
@@ -1370,7 +1383,11 @@ where
1370
1383
}
1371
1384
1372
1385
/// Perform `left > right` operation on an array and a numeric scalar
1373
- /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
1386
+ /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values.
1387
+ ///
1388
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
1389
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
1390
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
1374
1391
pub fn gt_dyn_scalar < T > ( left : & dyn Array , right : T ) -> Result < BooleanArray >
1375
1392
where
1376
1393
T : num:: ToPrimitive + std:: fmt:: Debug ,
@@ -1384,7 +1401,11 @@ where
1384
1401
}
1385
1402
1386
1403
/// Perform `left >= right` operation on an array and a numeric scalar
1387
- /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
1404
+ /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values.
1405
+ ///
1406
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
1407
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
1408
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
1388
1409
pub fn gt_eq_dyn_scalar < T > ( left : & dyn Array , right : T ) -> Result < BooleanArray >
1389
1410
where
1390
1411
T : num:: ToPrimitive + std:: fmt:: Debug ,
@@ -1398,7 +1419,11 @@ where
1398
1419
}
1399
1420
1400
1421
/// Perform `left != right` operation on an array and a numeric scalar
1401
- /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values
1422
+ /// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive values.
1423
+ ///
1424
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
1425
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
1426
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
1402
1427
pub fn neq_dyn_scalar < T > ( left : & dyn Array , right : T ) -> Result < BooleanArray >
1403
1428
where
1404
1429
T : num:: ToPrimitive + std:: fmt:: Debug ,
@@ -3019,14 +3044,31 @@ where
3019
3044
}
3020
3045
3021
3046
/// Perform `left == right` operation on a [`PrimitiveArray`] and a scalar value.
3047
+ ///
3048
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
3049
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
3050
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
3022
3051
pub fn eq_scalar < T > ( left : & PrimitiveArray < T > , right : T :: Native ) -> Result < BooleanArray >
3023
3052
where
3024
3053
T : ArrowNumericType ,
3054
+ T :: Native : num:: ToPrimitive + std:: fmt:: Debug ,
3025
3055
{
3026
3056
#[ cfg( feature = "simd" ) ]
3027
3057
return simd_compare_op_scalar ( left, right, T :: eq, |a, b| a == b) ;
3028
3058
#[ cfg( not( feature = "simd" ) ) ]
3029
- return compare_op_scalar ( left, |a| a == right) ;
3059
+ match left. data_type ( ) {
3060
+ DataType :: Float32 => {
3061
+ let left = as_primitive_array :: < Float32Type > ( left) ;
3062
+ let right = try_to_type ! ( right, to_f32) ?;
3063
+ compare_op_scalar ( left, |a : f32 | a. total_cmp ( & right) . is_eq ( ) )
3064
+ }
3065
+ DataType :: Float64 => {
3066
+ let left = as_primitive_array :: < Float64Type > ( left) ;
3067
+ let right = try_to_type ! ( right, to_f64) ?;
3068
+ compare_op_scalar ( left, |a : f64 | a. total_cmp ( & right) . is_eq ( ) )
3069
+ }
3070
+ _ => compare_op_scalar ( left, |a| a == right) ,
3071
+ }
3030
3072
}
3031
3073
3032
3074
/// Applies an unary and infallible comparison function to a primitive array.
@@ -3050,14 +3092,31 @@ where
3050
3092
}
3051
3093
3052
3094
/// Perform `left != right` operation on a [`PrimitiveArray`] and a scalar value.
3095
+ ///
3096
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
3097
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
3098
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
3053
3099
pub fn neq_scalar < T > ( left : & PrimitiveArray < T > , right : T :: Native ) -> Result < BooleanArray >
3054
3100
where
3055
3101
T : ArrowNumericType ,
3102
+ T :: Native : num:: ToPrimitive + std:: fmt:: Debug ,
3056
3103
{
3057
3104
#[ cfg( feature = "simd" ) ]
3058
3105
return simd_compare_op_scalar ( left, right, T :: ne, |a, b| a != b) ;
3059
3106
#[ cfg( not( feature = "simd" ) ) ]
3060
- return compare_op_scalar ( left, |a| a != right) ;
3107
+ match left. data_type ( ) {
3108
+ DataType :: Float32 => {
3109
+ let left = as_primitive_array :: < Float32Type > ( left) ;
3110
+ let right = try_to_type ! ( right, to_f32) ?;
3111
+ compare_op_scalar ( left, |a : f32 | a. total_cmp ( & right) . is_ne ( ) )
3112
+ }
3113
+ DataType :: Float64 => {
3114
+ let left = as_primitive_array :: < Float64Type > ( left) ;
3115
+ let right = try_to_type ! ( right, to_f64) ?;
3116
+ compare_op_scalar ( left, |a : f64 | a. total_cmp ( & right) . is_ne ( ) )
3117
+ }
3118
+ _ => compare_op_scalar ( left, |a| a != right) ,
3119
+ }
3061
3120
}
3062
3121
3063
3122
/// Perform `left < right` operation on two [`PrimitiveArray`]s. Null values are less than non-null
@@ -3074,14 +3133,31 @@ where
3074
3133
3075
3134
/// Perform `left < right` operation on a [`PrimitiveArray`] and a scalar value.
3076
3135
/// Null values are less than non-null values.
3136
+ ///
3137
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
3138
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
3139
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
3077
3140
pub fn lt_scalar < T > ( left : & PrimitiveArray < T > , right : T :: Native ) -> Result < BooleanArray >
3078
3141
where
3079
3142
T : ArrowNumericType ,
3143
+ T :: Native : num:: ToPrimitive + std:: fmt:: Debug ,
3080
3144
{
3081
3145
#[ cfg( feature = "simd" ) ]
3082
3146
return simd_compare_op_scalar ( left, right, T :: lt, |a, b| a < b) ;
3083
3147
#[ cfg( not( feature = "simd" ) ) ]
3084
- return compare_op_scalar ( left, |a| a < right) ;
3148
+ match left. data_type ( ) {
3149
+ DataType :: Float32 => {
3150
+ let left = as_primitive_array :: < Float32Type > ( left) ;
3151
+ let right = try_to_type ! ( right, to_f32) ?;
3152
+ compare_op_scalar ( left, |a : f32 | a. total_cmp ( & right) . is_lt ( ) )
3153
+ }
3154
+ DataType :: Float64 => {
3155
+ let left = as_primitive_array :: < Float64Type > ( left) ;
3156
+ let right = try_to_type ! ( right, to_f64) ?;
3157
+ compare_op_scalar ( left, |a : f64 | a. total_cmp ( & right) . is_lt ( ) )
3158
+ }
3159
+ _ => compare_op_scalar ( left, |a| a < right) ,
3160
+ }
3085
3161
}
3086
3162
3087
3163
/// Perform `left <= right` operation on two [`PrimitiveArray`]s. Null values are less than non-null
@@ -3101,14 +3177,31 @@ where
3101
3177
3102
3178
/// Perform `left <= right` operation on a [`PrimitiveArray`] and a scalar value.
3103
3179
/// Null values are less than non-null values.
3180
+ ///
3181
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
3182
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
3183
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
3104
3184
pub fn lt_eq_scalar < T > ( left : & PrimitiveArray < T > , right : T :: Native ) -> Result < BooleanArray >
3105
3185
where
3106
3186
T : ArrowNumericType ,
3187
+ T :: Native : num:: ToPrimitive + std:: fmt:: Debug ,
3107
3188
{
3108
3189
#[ cfg( feature = "simd" ) ]
3109
3190
return simd_compare_op_scalar ( left, right, T :: le, |a, b| a <= b) ;
3110
3191
#[ cfg( not( feature = "simd" ) ) ]
3111
- return compare_op_scalar ( left, |a| a <= right) ;
3192
+ match left. data_type ( ) {
3193
+ DataType :: Float32 => {
3194
+ let left = as_primitive_array :: < Float32Type > ( left) ;
3195
+ let right = try_to_type ! ( right, to_f32) ?;
3196
+ compare_op_scalar ( left, |a : f32 | a. total_cmp ( & right) . is_le ( ) )
3197
+ }
3198
+ DataType :: Float64 => {
3199
+ let left = as_primitive_array :: < Float64Type > ( left) ;
3200
+ let right = try_to_type ! ( right, to_f64) ?;
3201
+ compare_op_scalar ( left, |a : f64 | a. total_cmp ( & right) . is_le ( ) )
3202
+ }
3203
+ _ => compare_op_scalar ( left, |a| a <= right) ,
3204
+ }
3112
3205
}
3113
3206
3114
3207
/// Perform `left > right` operation on two [`PrimitiveArray`]s. Non-null values are greater than null
@@ -3125,14 +3218,31 @@ where
3125
3218
3126
3219
/// Perform `left > right` operation on a [`PrimitiveArray`] and a scalar value.
3127
3220
/// Non-null values are greater than null values.
3221
+ ///
3222
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
3223
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
3224
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
3128
3225
pub fn gt_scalar < T > ( left : & PrimitiveArray < T > , right : T :: Native ) -> Result < BooleanArray >
3129
3226
where
3130
3227
T : ArrowNumericType ,
3228
+ T :: Native : num:: ToPrimitive + std:: fmt:: Debug ,
3131
3229
{
3132
3230
#[ cfg( feature = "simd" ) ]
3133
3231
return simd_compare_op_scalar ( left, right, T :: gt, |a, b| a > b) ;
3134
3232
#[ cfg( not( feature = "simd" ) ) ]
3135
- return compare_op_scalar ( left, |a| a > right) ;
3233
+ match left. data_type ( ) {
3234
+ DataType :: Float32 => {
3235
+ let left = as_primitive_array :: < Float32Type > ( left) ;
3236
+ let right = try_to_type ! ( right, to_f32) ?;
3237
+ compare_op_scalar ( left, |a : f32 | a. total_cmp ( & right) . is_gt ( ) )
3238
+ }
3239
+ DataType :: Float64 => {
3240
+ let left = as_primitive_array :: < Float64Type > ( left) ;
3241
+ let right = try_to_type ! ( right, to_f64) ?;
3242
+ compare_op_scalar ( left, |a : f64 | a. total_cmp ( & right) . is_gt ( ) )
3243
+ }
3244
+ _ => compare_op_scalar ( left, |a| a > right) ,
3245
+ }
3136
3246
}
3137
3247
3138
3248
/// Perform `left >= right` operation on two [`PrimitiveArray`]s. Non-null values are greater than null
@@ -3152,14 +3262,31 @@ where
3152
3262
3153
3263
/// Perform `left >= right` operation on a [`PrimitiveArray`] and a scalar value.
3154
3264
/// Non-null values are greater than null values.
3265
+ ///
3266
+ /// For floating values like f32 and f64, this comparison produces an ordering in accordance to
3267
+ /// the totalOrder predicate as defined in the IEEE 754 (2008 revision) floating point standard.
3268
+ /// Please refer to `f32::total_cmp` and `f64::total_cmp`.
3155
3269
pub fn gt_eq_scalar < T > ( left : & PrimitiveArray < T > , right : T :: Native ) -> Result < BooleanArray >
3156
3270
where
3157
3271
T : ArrowNumericType ,
3272
+ T :: Native : num:: ToPrimitive + std:: fmt:: Debug ,
3158
3273
{
3159
3274
#[ cfg( feature = "simd" ) ]
3160
3275
return simd_compare_op_scalar ( left, right, T :: ge, |a, b| a >= b) ;
3161
3276
#[ cfg( not( feature = "simd" ) ) ]
3162
- return compare_op_scalar ( left, |a| a >= right) ;
3277
+ match left. data_type ( ) {
3278
+ DataType :: Float32 => {
3279
+ let left = as_primitive_array :: < Float32Type > ( left) ;
3280
+ let right = try_to_type ! ( right, to_f32) ?;
3281
+ compare_op_scalar ( left, |a : f32 | a. total_cmp ( & right) . is_ge ( ) )
3282
+ }
3283
+ DataType :: Float64 => {
3284
+ let left = as_primitive_array :: < Float64Type > ( left) ;
3285
+ let right = try_to_type ! ( right, to_f64) ?;
3286
+ compare_op_scalar ( left, |a : f64 | a. total_cmp ( & right) . is_ge ( ) )
3287
+ }
3288
+ _ => compare_op_scalar ( left, |a| a >= right) ,
3289
+ }
3163
3290
}
3164
3291
3165
3292
/// Checks if a [`GenericListArray`] contains a value in the [`PrimitiveArray`]
@@ -5852,12 +5979,12 @@ mod tests {
5852
5979
. map ( Some )
5853
5980
. collect ( ) ;
5854
5981
let expected = BooleanArray :: from (
5855
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
5982
+ vec ! [ Some ( true ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
5856
5983
) ;
5857
5984
assert_eq ! ( eq_dyn_scalar( & array, f32 :: NAN ) . unwrap( ) , expected) ;
5858
5985
5859
5986
let expected = BooleanArray :: from (
5860
- vec ! [ Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
5987
+ vec ! [ Some ( false ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
5861
5988
) ;
5862
5989
assert_eq ! ( neq_dyn_scalar( & array, f32 :: NAN ) . unwrap( ) , expected) ;
5863
5990
@@ -5866,12 +5993,12 @@ mod tests {
5866
5993
. map ( Some )
5867
5994
. collect ( ) ;
5868
5995
let expected = BooleanArray :: from (
5869
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
5996
+ vec ! [ Some ( true ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
5870
5997
) ;
5871
5998
assert_eq ! ( eq_dyn_scalar( & array, f64 :: NAN ) . unwrap( ) , expected) ;
5872
5999
5873
6000
let expected = BooleanArray :: from (
5874
- vec ! [ Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
6001
+ vec ! [ Some ( false ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
5875
6002
) ;
5876
6003
assert_eq ! ( neq_dyn_scalar( & array, f64 :: NAN ) . unwrap( ) , expected) ;
5877
6004
}
@@ -5883,12 +6010,12 @@ mod tests {
5883
6010
. map ( Some )
5884
6011
. collect ( ) ;
5885
6012
let expected = BooleanArray :: from (
5886
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
6013
+ vec ! [ Some ( false ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
5887
6014
) ;
5888
6015
assert_eq ! ( lt_dyn_scalar( & array, f32 :: NAN ) . unwrap( ) , expected) ;
5889
6016
5890
6017
let expected = BooleanArray :: from (
5891
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
6018
+ vec ! [ Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
5892
6019
) ;
5893
6020
assert_eq ! ( lt_eq_dyn_scalar( & array, f32 :: NAN ) . unwrap( ) , expected) ;
5894
6021
@@ -5897,12 +6024,12 @@ mod tests {
5897
6024
. map ( Some )
5898
6025
. collect ( ) ;
5899
6026
let expected = BooleanArray :: from (
5900
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
6027
+ vec ! [ Some ( false ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
5901
6028
) ;
5902
6029
assert_eq ! ( lt_dyn_scalar( & array, f64 :: NAN ) . unwrap( ) , expected) ;
5903
6030
5904
6031
let expected = BooleanArray :: from (
5905
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
6032
+ vec ! [ Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) , Some ( true ) ] ,
5906
6033
) ;
5907
6034
assert_eq ! ( lt_eq_dyn_scalar( & array, f64 :: NAN ) . unwrap( ) , expected) ;
5908
6035
}
@@ -5919,7 +6046,7 @@ mod tests {
5919
6046
assert_eq ! ( gt_dyn_scalar( & array, f32 :: NAN ) . unwrap( ) , expected) ;
5920
6047
5921
6048
let expected = BooleanArray :: from (
5922
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
6049
+ vec ! [ Some ( true ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
5923
6050
) ;
5924
6051
assert_eq ! ( gt_eq_dyn_scalar( & array, f32 :: NAN ) . unwrap( ) , expected) ;
5925
6052
@@ -5933,7 +6060,7 @@ mod tests {
5933
6060
assert_eq ! ( gt_dyn_scalar( & array, f64 :: NAN ) . unwrap( ) , expected) ;
5934
6061
5935
6062
let expected = BooleanArray :: from (
5936
- vec ! [ Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
6063
+ vec ! [ Some ( true ) , Some ( false ) , Some ( false ) , Some ( false ) , Some ( false ) ] ,
5937
6064
) ;
5938
6065
assert_eq ! ( gt_eq_dyn_scalar( & array, f64 :: NAN ) . unwrap( ) , expected) ;
5939
6066
}
0 commit comments