Skip to content

Commit f5a36ee

Browse files
committed
refactor(tests): move tests to bottom
1 parent f6bb7ec commit f5a36ee

File tree

1 file changed

+86
-85
lines changed

1 file changed

+86
-85
lines changed

datafusion/physical-expr/src/aggregate.rs

Lines changed: 86 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -25,91 +25,6 @@ pub(crate) mod groups_accumulator {
2525
};
2626
}
2727

28-
#[cfg(test)]
29-
mod tests {
30-
use super::*;
31-
use crate::expressions::Literal;
32-
use arrow::datatypes::{DataType, Field, Schema};
33-
use datafusion_common::ScalarValue;
34-
use datafusion_expr::{AggregateUDF, AggregateUDFImpl, Signature, Volatility};
35-
use std::{any::Any, sync::Arc};
36-
#[derive(Debug, PartialEq, Eq, Hash)]
37-
struct DummyUdf {
38-
signature: Signature,
39-
}
40-
41-
impl DummyUdf {
42-
fn new() -> Self {
43-
Self {
44-
signature: Signature::any(1, Volatility::Immutable),
45-
}
46-
}
47-
}
48-
49-
impl AggregateUDFImpl for DummyUdf {
50-
fn as_any(&self) -> &dyn Any {
51-
self
52-
}
53-
fn name(&self) -> &str {
54-
"dummy"
55-
}
56-
fn signature(&self) -> &Signature {
57-
&self.signature
58-
}
59-
fn return_type(&self, _args: &[DataType]) -> Result<DataType> {
60-
Ok(DataType::UInt64)
61-
}
62-
fn accumulator(&self, _args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
63-
unimplemented!()
64-
}
65-
fn state_fields(&self, _args: StateFieldsArgs) -> Result<Vec<FieldRef>> {
66-
unimplemented!()
67-
}
68-
fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool {
69-
true
70-
}
71-
fn create_groups_accumulator(
72-
&self,
73-
_args: AccumulatorArgs,
74-
) -> Result<Box<dyn GroupsAccumulator>> {
75-
unimplemented!()
76-
}
77-
}
78-
79-
#[test]
80-
fn test_args_schema_and_groups_path() {
81-
// literal-only: empty physical schema synthesizes schema from literal expr
82-
let udf = Arc::new(AggregateUDF::from(DummyUdf::new()));
83-
let lit_expr =
84-
Arc::new(Literal::new(ScalarValue::UInt32(Some(1)))) as Arc<dyn PhysicalExpr>;
85-
let agg =
86-
AggregateExprBuilder::new(Arc::clone(&udf), vec![Arc::clone(&lit_expr)])
87-
.alias("x")
88-
.schema(Arc::new(Schema::empty()))
89-
.build()
90-
.unwrap();
91-
match agg.args_schema() {
92-
Cow::Owned(s) => assert_eq!(s.field(0).name(), "lit"),
93-
_ => panic!("expected owned schema"),
94-
}
95-
assert!(agg.groups_accumulator_supported());
96-
97-
// non-empty physical schema should be borrowed
98-
let f = Field::new("b", DataType::Int32, false);
99-
let phys_schema = Schema::new(vec![f.clone()]);
100-
let col_expr = Arc::new(Column::new("b", 0)) as Arc<dyn PhysicalExpr>;
101-
let agg2 = AggregateExprBuilder::new(udf, vec![col_expr])
102-
.alias("x")
103-
.schema(Arc::new(phys_schema))
104-
.build()
105-
.unwrap();
106-
match agg2.args_schema() {
107-
Cow::Borrowed(s) => assert_eq!(s.field(0).name(), "b"),
108-
_ => panic!("expected borrowed schema"),
109-
}
110-
assert!(agg2.groups_accumulator_supported());
111-
}
112-
}
11328
pub(crate) mod stats {
11429
pub use datafusion_functions_aggregate_common::stats::StatsType;
11530
}
@@ -836,3 +751,89 @@ fn replace_order_by_clause(order_by: &mut String) {
836751
fn replace_fn_name_clause(aggr_name: &mut String, fn_name_old: &str, fn_name_new: &str) {
837752
*aggr_name = aggr_name.replace(fn_name_old, fn_name_new);
838753
}
754+
755+
#[cfg(test)]
756+
mod tests {
757+
use super::*;
758+
use crate::expressions::Literal;
759+
use arrow::datatypes::{DataType, Field, Schema};
760+
use datafusion_common::ScalarValue;
761+
use datafusion_expr::{AggregateUDF, AggregateUDFImpl, Signature, Volatility};
762+
use std::{any::Any, sync::Arc};
763+
#[derive(Debug, PartialEq, Eq, Hash)]
764+
struct DummyUdf {
765+
signature: Signature,
766+
}
767+
768+
impl DummyUdf {
769+
fn new() -> Self {
770+
Self {
771+
signature: Signature::any(1, Volatility::Immutable),
772+
}
773+
}
774+
}
775+
776+
impl AggregateUDFImpl for DummyUdf {
777+
fn as_any(&self) -> &dyn Any {
778+
self
779+
}
780+
fn name(&self) -> &str {
781+
"dummy"
782+
}
783+
fn signature(&self) -> &Signature {
784+
&self.signature
785+
}
786+
fn return_type(&self, _args: &[DataType]) -> Result<DataType> {
787+
Ok(DataType::UInt64)
788+
}
789+
fn accumulator(&self, _args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
790+
unimplemented!()
791+
}
792+
fn state_fields(&self, _args: StateFieldsArgs) -> Result<Vec<FieldRef>> {
793+
unimplemented!()
794+
}
795+
fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool {
796+
true
797+
}
798+
fn create_groups_accumulator(
799+
&self,
800+
_args: AccumulatorArgs,
801+
) -> Result<Box<dyn GroupsAccumulator>> {
802+
unimplemented!()
803+
}
804+
}
805+
806+
#[test]
807+
fn test_args_schema_and_groups_path() {
808+
// literal-only: empty physical schema synthesizes schema from literal expr
809+
let udf = Arc::new(AggregateUDF::from(DummyUdf::new()));
810+
let lit_expr =
811+
Arc::new(Literal::new(ScalarValue::UInt32(Some(1)))) as Arc<dyn PhysicalExpr>;
812+
let agg =
813+
AggregateExprBuilder::new(Arc::clone(&udf), vec![Arc::clone(&lit_expr)])
814+
.alias("x")
815+
.schema(Arc::new(Schema::empty()))
816+
.build()
817+
.unwrap();
818+
match agg.args_schema() {
819+
Cow::Owned(s) => assert_eq!(s.field(0).name(), "lit"),
820+
_ => panic!("expected owned schema"),
821+
}
822+
assert!(agg.groups_accumulator_supported());
823+
824+
// non-empty physical schema should be borrowed
825+
let f = Field::new("b", DataType::Int32, false);
826+
let phys_schema = Schema::new(vec![f.clone()]);
827+
let col_expr = Arc::new(Column::new("b", 0)) as Arc<dyn PhysicalExpr>;
828+
let agg2 = AggregateExprBuilder::new(udf, vec![col_expr])
829+
.alias("x")
830+
.schema(Arc::new(phys_schema))
831+
.build()
832+
.unwrap();
833+
match agg2.args_schema() {
834+
Cow::Borrowed(s) => assert_eq!(s.field(0).name(), "b"),
835+
_ => panic!("expected borrowed schema"),
836+
}
837+
assert!(agg2.groups_accumulator_supported());
838+
}
839+
}

0 commit comments

Comments
 (0)