1
- use arrow:: array:: PrimitiveArray ;
1
+ use arrow:: array:: Array ;
2
+ use arrow:: array:: { Int64Array , PrimitiveArray } ;
2
3
use arrow:: array_ops;
3
4
use arrow:: datatypes:: ArrowNumericType ;
5
+ use arrow:: datatypes:: ArrowPrimitiveType ;
6
+ use arrow:: datatypes:: Int64Type ;
4
7
use std:: ops:: Add ;
5
8
6
9
struct AggregateFunctions ;
7
10
8
11
impl AggregateFunctions {
9
- pub fn max < T > ( array : & PrimitiveArray < T > ) -> Option < T :: Native >
12
+ pub fn max < T > ( arrays : Vec < & PrimitiveArray < T > > ) -> Option < T :: Native >
10
13
where
11
14
T : ArrowNumericType ,
15
+ T :: Native : std:: cmp:: Ord ,
12
16
{
13
- array_ops:: max ( array)
17
+ arrays . iter ( ) . map ( |array| array_ops:: max ( array) . unwrap ( ) ) . max ( )
14
18
}
15
- pub fn min < T > ( array : & PrimitiveArray < T > ) -> Option < T :: Native >
19
+ pub fn min < T > ( arrays : Vec < & PrimitiveArray < T > > ) -> Option < T :: Native >
16
20
where
17
21
T : ArrowNumericType ,
22
+ T :: Native : std:: cmp:: Ord ,
18
23
{
19
- array_ops:: min ( array)
24
+ arrays . iter ( ) . map ( |array| array_ops:: max ( array) . unwrap ( ) ) . max ( )
20
25
}
21
- pub fn avg ( ) { }
22
- pub fn count ( ) { }
23
- pub fn sum < T > ( array : & PrimitiveArray < T > ) -> Option < T :: Native >
26
+ // pub fn avg<T>(array: &PrimitiveArray<T>) -> Option<f64>
27
+ // where
28
+ // T: ArrowNumericType
29
+ // {
30
+ // let sum = array_ops::sum(array);
31
+ // match sum {
32
+ // None => None,
33
+ // Some(sum) => {
34
+ // let count = AggregateFunctions::count(array).unwrap();
35
+ // let sum = sum as f64;
36
+ // Some(sum / count as f64)
37
+ // }
38
+ // }
39
+ // }
40
+
41
+ /// Count returns the number of non-null values in the array/column.
42
+ ///
43
+ /// For the number of all values, use `len()`
44
+ pub fn count < T > ( arrays : Vec < & PrimitiveArray < T > > ) -> Option < i64 >
45
+ where
46
+ T : ArrowPrimitiveType ,
47
+ {
48
+ let mut sum = 0 ;
49
+ arrays. iter ( ) . for_each ( |array| sum += ( array. len ( ) - array. null_count ( ) ) as i64 ) ;
50
+
51
+ Some ( sum)
52
+ }
53
+ fn count_distinct ( ) { }
54
+ pub fn sum < T > ( arrays : Vec < & PrimitiveArray < T > > ) -> Option < T :: Native >
24
55
where
25
56
T : ArrowNumericType ,
26
57
T :: Native : Add < Output = T :: Native > ,
27
58
{
28
- array_ops:: sum ( array)
59
+ let mut sum = T :: default_value ( ) ;
60
+ arrays. iter ( ) . for_each ( |array| sum = sum + array_ops:: sum ( array) . unwrap_or ( T :: default_value ( ) ) ) ;
61
+
62
+ Some ( sum)
63
+
29
64
}
30
65
pub fn first ( ) { }
31
66
pub fn kurtosis ( ) { }
@@ -37,3 +72,28 @@ impl AggregateFunctions {
37
72
pub fn variance ( ) { }
38
73
// TODO population and sample variances
39
74
}
75
+
76
+ #[ cfg( test) ]
77
+ mod tests {
78
+ use super :: * ;
79
+ use arrow:: array:: { Float64Array , Int32Array } ;
80
+ use arrow:: datatypes:: Int32Type ;
81
+
82
+ #[ test]
83
+ fn testit ( ) {
84
+ let a = Int32Array :: from ( vec ! [ 6 , 7 , 8 , 9 , 10 ] ) ;
85
+ let b = Int32Array :: from ( vec ! [ 7 , 6 , 8 , 9 , 10 ] ) ;
86
+ let c = b. value ( 0 ) ;
87
+ let d = Int32Array :: from ( vec ! [ c] ) ;
88
+ // assert_eq!(7.0, c);
89
+ assert_eq ! ( a. value( 0 ) , b. value( 1 ) ) ;
90
+ assert_eq ! ( b. value( 0 ) , d. value( 0 ) ) ;
91
+ }
92
+
93
+ #[ test]
94
+ fn test_aggregate_count ( ) {
95
+ let a = Int32Array :: from ( vec ! [ 5 , 6 , 7 , 8 , 9 ] ) ;
96
+ let c = AggregateFunctions :: count ( vec ! [ & a] ) . unwrap ( ) ;
97
+ assert_eq ! ( 5 , c) ;
98
+ }
99
+ }
0 commit comments