Apache DataFusion SQL Query Engine
fix: allow group by same expr in Aggregate (#6097)
jakevindfe3534e0557
diff --git a/datafusion/common/src/dfschema.rs b/datafusion/common/src/dfschema.rsindex 23833e8e13..8105ada59f 100644--- a/datafusion/common/src/dfschema.rs+++ b/datafusion/common/src/dfschema.rs@@ -24,7 +24,6 @@ use std::hash::Hash;use std::sync::Arc;use crate::error::{unqualified_field_not_found, DataFusionError, Result, SchemaError};-use crate::utils::quote_identifier;use crate::{field_not_found, Column, OwnedTableReference, TableReference};use arrow::compute::can_cast_types;@@ -217,20 +216,7 @@ impl DFSchema {(None, Some(_)) | (None, None) => field.name() == name,}).map(|(idx, _)| idx);- match matches.next() {- None => Ok(None),- Some(idx) => match matches.next() {- None => Ok(Some(idx)),- // found more than one matches- Some(_) => Err(DataFusionError::Internal(format!(- "Ambiguous reference to qualified field named {}.{}",- qualifier- .map(|q| q.to_quoted_string())- .unwrap_or("<unqualified>".to_string()),- quote_identifier(name)- ))),- },- }+ Ok(matches.next())}/// Find the index of the column with the given qualifier and namediff --git a/datafusion/core/tests/sqllogictests/test_files/groupby.slt b/datafusion/core/tests/sqllogictests/test_files/groupby.sltindex bd081048f8..83981095bf 100644--- a/datafusion/core/tests/sqllogictests/test_files/groupby.slt+++ b/datafusion/core/tests/sqllogictests/test_files/groupby.slt@@ -58,3 +58,9 @@ SELECT 38 FROM tab0 AS cor0 GROUP BY cor0.col1, cor0.col1;----3838++query I rowsort+SELECT cor0.col1 AS col2 FROM tab0 AS cor0 GROUP BY cor0.col1, cor0.col1;+----+0+81diff --git a/datafusion/expr/src/utils.rs b/datafusion/expr/src/utils.rsindex 3aec826d81..e6b0cc43f1 100644--- a/datafusion/expr/src/utils.rs+++ b/datafusion/expr/src/utils.rs@@ -956,13 +956,12 @@ pub fn from_plan(}/// Find all columns referenced from an aggregate query-fn agg_cols(agg: &Aggregate) -> Result<Vec<Column>> {- Ok(agg- .aggr_expr+fn agg_cols(agg: &Aggregate) -> Vec<Column> {+ agg.aggr_expr.iter().chain(&agg.group_expr).flat_map(find_columns_referenced_by_expr)- .collect())+ .collect()}fn exprlist_to_fields_aggregate(@@ -970,7 +969,7 @@ fn exprlist_to_fields_aggregate(plan: &LogicalPlan,agg: &Aggregate,) -> Result<Vec<DFField>> {- let agg_cols = agg_cols(agg)?;+ let agg_cols = agg_cols(agg);let mut fields = vec![];for expr in exprs {match expr {