MCPcopy Create free account
hub / github.com/apache/datafusion / aggregate

Function aggregate

datafusion/optimizer/src/common_subexpr_eliminate.rs:941–1079  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

939
940 #[test]
941 fn aggregate() -> Result<()> {
942 let table_scan = test_table_scan()?;
943
944 let return_type = DataType::UInt32;
945 let accumulator: AccumulatorFactoryFunction = Arc::new(|_| unimplemented!());
946 let udf_agg = |inner: Expr| {
947 Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf(
948 Arc::new(AggregateUDF::from(SimpleAggregateUDF::new_with_signature(
949 "my_agg",
950 Signature::exact(vec![DataType::UInt32], Volatility::Stable),
951 return_type.clone(),
952 Arc::clone(&accumulator),
953 vec![Field::new("value", DataType::UInt32, true).into()],
954 ))),
955 vec![inner],
956 false,
957 None,
958 vec![],
959 None,
960 ))
961 };
962
963 // test: common aggregates
964 let plan = LogicalPlanBuilder::from(table_scan.clone())
965 .aggregate(
966 iter::empty::<Expr>(),
967 vec![
968 // common: avg(col("a"))
969 avg(col("a")).alias("col1"),
970 avg(col("a")).alias("col2"),
971 // no common
972 avg(col("b")).alias("col3"),
973 avg(col("c")),
974 // common: udf_agg(col("a"))
975 udf_agg(col("a")).alias("col4"),
976 udf_agg(col("a")).alias("col5"),
977 // no common
978 udf_agg(col("b")).alias("col6"),
979 udf_agg(col("c")),
980 ],
981 )?
982 .build()?;
983
984 assert_optimized_plan_equal!(
985 plan,
986 @ r"
987 Projection: __common_expr_1 AS col1, __common_expr_1 AS col2, col3, __common_expr_3 AS avg(test.c), __common_expr_2 AS col4, __common_expr_2 AS col5, col6, __common_expr_4 AS my_agg(test.c)
988 Aggregate: groupBy=[[]], aggr=[[avg(test.a) AS __common_expr_1, my_agg(test.a) AS __common_expr_2, avg(test.b) AS col3, avg(test.c) AS __common_expr_3, my_agg(test.b) AS col6, my_agg(test.c) AS __common_expr_4]]
989 TableScan: test
990 "
991 )?;
992
993 // test: trafo after aggregate
994 let plan = LogicalPlanBuilder::from(table_scan.clone())
995 .aggregate(
996 iter::empty::<Expr>(),
997 vec![
998 lit(1) + avg(col("a")),

Callers

nothing calls this directly

Calls 6

newFunction · 0.85
test_table_scanFunction · 0.50
AggregateFunctionClass · 0.50
cloneMethod · 0.45
buildMethod · 0.45
aggregateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…