|
17 | 17 |
|
18 | 18 | //! Support the coercion rule for aggregate function. |
19 | 19 |
|
20 | | -pub use datafusion_physical_expr::coercion_rule::aggregate_rule::{ |
21 | | - coerce_exprs, coerce_types, |
22 | | -}; |
23 | | - |
24 | | -#[cfg(test)] |
25 | | -mod tests { |
26 | | - use super::*; |
27 | | - use crate::physical_plan::aggregates; |
28 | | - use arrow::datatypes::DataType; |
29 | | - use datafusion_expr::AggregateFunction; |
30 | | - |
31 | | - #[test] |
32 | | - fn test_aggregate_coerce_types() { |
33 | | - // test input args with error number input types |
34 | | - let fun = AggregateFunction::Min; |
35 | | - let input_types = vec![DataType::Int64, DataType::Int32]; |
36 | | - let signature = aggregates::signature(&fun); |
37 | | - let result = coerce_types(&fun, &input_types, &signature); |
38 | | - assert_eq!("Error during planning: The function Min expects 1 arguments, but 2 were provided", result.unwrap_err().to_string()); |
39 | | - |
40 | | - // test input args is invalid data type for sum or avg |
41 | | - let fun = AggregateFunction::Sum; |
42 | | - let input_types = vec![DataType::Utf8]; |
43 | | - let signature = aggregates::signature(&fun); |
44 | | - let result = coerce_types(&fun, &input_types, &signature); |
45 | | - assert_eq!( |
46 | | - "Error during planning: The function Sum does not support inputs of type Utf8.", |
47 | | - result.unwrap_err().to_string() |
48 | | - ); |
49 | | - let fun = AggregateFunction::Avg; |
50 | | - let signature = aggregates::signature(&fun); |
51 | | - let result = coerce_types(&fun, &input_types, &signature); |
52 | | - assert_eq!( |
53 | | - "Error during planning: The function Avg does not support inputs of type Utf8.", |
54 | | - result.unwrap_err().to_string() |
55 | | - ); |
56 | | - |
57 | | - // test count, array_agg, approx_distinct, min, max. |
58 | | - // the coerced types is same with input types |
59 | | - let funs = vec![ |
60 | | - AggregateFunction::Count, |
61 | | - AggregateFunction::ArrayAgg, |
62 | | - AggregateFunction::ApproxDistinct, |
63 | | - AggregateFunction::Min, |
64 | | - AggregateFunction::Max, |
65 | | - ]; |
66 | | - let input_types = vec![ |
67 | | - vec![DataType::Int32], |
68 | | - // support the decimal data type for min/max agg |
69 | | - // vec![DataType::Decimal(10, 2)], |
70 | | - vec![DataType::Utf8], |
71 | | - ]; |
72 | | - for fun in funs { |
73 | | - for input_type in &input_types { |
74 | | - let signature = aggregates::signature(&fun); |
75 | | - let result = coerce_types(&fun, input_type, &signature); |
76 | | - assert_eq!(*input_type, result.unwrap()); |
77 | | - } |
78 | | - } |
79 | | - // test sum, avg |
80 | | - let funs = vec![AggregateFunction::Sum, AggregateFunction::Avg]; |
81 | | - let input_types = vec![ |
82 | | - vec![DataType::Int32], |
83 | | - vec![DataType::Float32], |
84 | | - vec![DataType::Decimal(20, 3)], |
85 | | - ]; |
86 | | - for fun in funs { |
87 | | - for input_type in &input_types { |
88 | | - let signature = aggregates::signature(&fun); |
89 | | - let result = coerce_types(&fun, input_type, &signature); |
90 | | - assert_eq!(*input_type, result.unwrap()); |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - // ApproxPercentileCont input types |
95 | | - let input_types = vec![ |
96 | | - vec![DataType::Int8, DataType::Float64], |
97 | | - vec![DataType::Int16, DataType::Float64], |
98 | | - vec![DataType::Int32, DataType::Float64], |
99 | | - vec![DataType::Int64, DataType::Float64], |
100 | | - vec![DataType::UInt8, DataType::Float64], |
101 | | - vec![DataType::UInt16, DataType::Float64], |
102 | | - vec![DataType::UInt32, DataType::Float64], |
103 | | - vec![DataType::UInt64, DataType::Float64], |
104 | | - vec![DataType::Float32, DataType::Float64], |
105 | | - vec![DataType::Float64, DataType::Float64], |
106 | | - ]; |
107 | | - for input_type in &input_types { |
108 | | - let signature = |
109 | | - aggregates::signature(&AggregateFunction::ApproxPercentileCont); |
110 | | - let result = coerce_types( |
111 | | - &AggregateFunction::ApproxPercentileCont, |
112 | | - input_type, |
113 | | - &signature, |
114 | | - ); |
115 | | - assert_eq!(*input_type, result.unwrap()); |
116 | | - } |
117 | | - } |
118 | | -} |
| 20 | +pub use datafusion_physical_expr::coercion_rule::aggregate_rule::coerce_exprs; |
0 commit comments