Skip to content

Apache DataFusion SQL Query Engine

fix: Error if both `group_by` and `aggr` is empty in `AggregateExec::try_new()` (#25786)

Yongting Youbdad988f23f4
diff --git a/datafusion/core/tests/physical_optimizer/enforce_sorting.rs b/datafusion/core/tests/physical_optimizer/enforce_sorting.rs
index 04019feb66..b5a2a720cb 100644
--- a/datafusion/core/tests/physical_optimizer/enforce_sorting.rs
+++ b/datafusion/core/tests/physical_optimizer/enforce_sorting.rs
@@ -2095,7 +2095,7 @@ async fn test_remove_unnecessary_sort3() -> Result<()> {
EnforceSortingTest::new(physical_plan.clone()).with_repartition_sorts(true);
assert_snapshot!(test.run(), @r"
Input Plan:
- AggregateExec: mode=Final, gby=[], aggr=[]
+ AggregateExec: mode=Single, gby=[], aggr=[COUNT(*)]
SortPreservingMergeExec: [nullable_col@0 ASC, non_nullable_col@1 ASC]
SortExec: expr=[nullable_col@0 ASC, non_nullable_col@1 ASC], preserve_partitioning=[true]
RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1, maintains_sort_order=true
@@ -2104,7 +2104,7 @@ async fn test_remove_unnecessary_sort3() -> Result<()> {
DataSourceExec: partitions=1, partition_sizes=[0]
Optimized Plan:
- AggregateExec: mode=Final, gby=[], aggr=[]
+ AggregateExec: mode=Single, gby=[], aggr=[COUNT(*)]
DataSourceExec: partitions=1, partition_sizes=[0]
");
diff --git a/datafusion/core/tests/physical_optimizer/limited_distinct_aggregation.rs b/datafusion/core/tests/physical_optimizer/limited_distinct_aggregation.rs
index 9a43ba01da..1d5737d243 100644
--- a/datafusion/core/tests/physical_optimizer/limited_distinct_aggregation.rs
+++ b/datafusion/core/tests/physical_optimizer/limited_distinct_aggregation.rs
@@ -532,14 +532,14 @@ fn test_no_group_by() -> Result<()> {
let source = mock_data()?;
let schema = source.schema();
- // `SELECT <aggregate with no expressions> FROM DataSourceExec LIMIT 10;`, Single AggregateExec
+ // `SELECT COUNT(*) FROM DataSourceExec LIMIT 10;`, Single AggregateExec
let single_agg = AggregateExec::try_new(
AggregateMode::Single,
build_group_by(&schema, vec![]),
- vec![], /* aggr_expr */
- vec![], /* filter_expr */
- source, /* input */
- schema, /* input_schema */
+ vec![Arc::new(TestAggregate::CountStar.count_expr(&schema))],
+ vec![None],
+ source,
+ schema,
)?;
let limit_exec = LocalLimitExec::new(
Arc::new(single_agg),
@@ -553,7 +553,7 @@ fn test_no_group_by() -> Result<()> {
actual,
@r"
LocalLimitExec: fetch=10
- AggregateExec: mode=Single, gby=[], aggr=[]
+ AggregateExec: mode=Single, gby=[], aggr=[COUNT(*)]
DataSourceExec: partitions=1, partition_sizes=[1]
"
);
diff --git a/datafusion/core/tests/physical_optimizer/test_utils.rs b/datafusion/core/tests/physical_optimizer/test_utils.rs
index 6b10efffa9..3733dba39c 100644
--- a/datafusion/core/tests/physical_optimizer/test_utils.rs
+++ b/datafusion/core/tests/physical_optimizer/test_utils.rs
@@ -378,10 +378,10 @@ pub fn aggregate_exec(input: Arc<dyn ExecutionPlan>) -> Arc<dyn ExecutionPlan> {
let schema = input.schema();
Arc::new(
AggregateExec::try_new(
- AggregateMode::Final,
+ AggregateMode::Single,
PhysicalGroupBy::default(),
- vec![],
- vec![],
+ vec![Arc::new(TestAggregate::CountStar.count_expr(&schema))],
+ vec![None],
input,
schema,
)
diff --git a/datafusion/physical-plan/src/aggregates/mod.rs b/datafusion/physical-plan/src/aggregates/mod.rs
index d82d6de61c..9437f1d83e 100644
--- a/datafusion/physical-plan/src/aggregates/mod.rs
+++ b/datafusion/physical-plan/src/aggregates/mod.rs
@@ -979,7 +979,14 @@ impl AggregateExec {
&self.cache
}
- /// Create a new hash aggregate execution plan
+ /// Create a new hash aggregate execution plan.
+ ///
+ /// # Errors
+ ///
+ /// Returns an internal error if the provided args can't build a valid aggregation:
+ ///
+ /// - Both grouping and aggregate expressions are empty, without grouping sets.
+ /// - The numbers of aggregate expressions and filter expressions differ.
pub fn try_new(
mode: AggregateMode,
group_by: impl Into<Arc<PhysicalGroupBy>>,
@@ -1023,6 +1030,12 @@ impl AggregateExec {
let group_by = group_by.into();
let filter_expr = filter_expr.into();
+ if group_by.is_true_no_grouping() && aggr_expr.is_empty() {
+ return internal_err!(
+ "Grouping expressions and aggregate expressions cannot both be empty"
+ );
+ }
+
// Make sure arguments are consistent in size
assert_eq_or_internal_err!(
aggr_expr.len(),
@@ -3350,7 +3363,7 @@ mod tests {
use arrow::compute::{SortOptions, concat_batches};
use arrow::datatypes::{Int32Type, Int64Type};
use datafusion_common::test_util::{batches_to_sort_string, batches_to_string};
- use datafusion_common::{DataFusionError, internal_err};
+ use datafusion_common::{DataFusionError, assert_contains, internal_err};
use datafusion_execution::config::SessionConfig;
use datafusion_execution::memory_pool::{
FairSpillPool, MemoryPool, PeakRecordingPool,
@@ -3414,6 +3427,31 @@ mod tests {
Ok(schema)
}
+ /// If both group_by and aggr is empty, return error since it's not a valid aggregate
+ #[test]
+ fn test_try_new_rejects_empty_aggregate() -> Result<()> {
+ let schema = create_test_schema()?;
+ for group_by in [
+ PhysicalGroupBy::default(),
+ PhysicalGroupBy::new_single(vec![]),
+ ] {
+ let err = AggregateExec::try_new(
+ AggregateMode::Single,
+ group_by,
+ vec![],
+ vec![],
+ Arc::new(EmptyExec::new(Arc::clone(&schema))),
+ Arc::clone(&schema),
+ )
+ .unwrap_err();
+ assert_contains!(
+ err.to_string(),
+ "Grouping expressions and aggregate expressions cannot both be empty"
+ );
+ }
+ Ok(())
+ }
+
/// some mock data to aggregates
fn some_data() -> (Arc<Schema>, Vec<RecordBatch>) {
// define a schema.
diff --git a/datafusion/sqllogictest/test_files/grouping.slt b/datafusion/sqllogictest/test_files/grouping.slt
index 5e364eb3ce..738b7ae722 100644
--- a/datafusion/sqllogictest/test_files/grouping.slt
+++ b/datafusion/sqllogictest/test_files/grouping.slt
@@ -310,6 +310,12 @@ SELECT COUNT(*) FROM test GROUP BY GROUPING SETS (());
----
2
+# An empty grouping set is valid even without aggregate expressions.
+query I
+SELECT 1 FROM test GROUP BY GROUPING SETS (());
+----
+1
+
# grouping_sets_with_empty_set
query I
SELECT SUM(v1) FROM generate_series(10) AS t1(v1) GROUP BY GROUPING SETS(())