Skip to content

Apache DataFusion SQL Query Engine

bugfix: just allow having use expr in `groupby` or `aggr` (#4579)

jakevind33457c20c2b
diff --git a/datafusion/core/tests/sql/group_by.rs b/datafusion/core/tests/sql/group_by.rs
index fffcc356d4..5fb106d440 100644
--- a/datafusion/core/tests/sql/group_by.rs
+++ b/datafusion/core/tests/sql/group_by.rs
@@ -190,27 +190,6 @@ async fn csv_query_group_by_and_having_and_where() -> Result<()> {
Ok(())
}
-#[tokio::test]
-async fn csv_query_having_without_group_by() -> Result<()> {
- let ctx = SessionContext::new();
- register_aggregate_csv(&ctx).await?;
- let sql = "SELECT c1, c2, c3 FROM aggregate_test_100 HAVING c2 >= 4 AND c3 > 90";
- let actual = execute_to_batches(&ctx, sql).await;
- let expected = vec![
- "+----+----+-----+",
- "| c1 | c2 | c3 |",
- "+----+----+-----+",
- "| c | 4 | 123 |",
- "| c | 5 | 118 |",
- "| d | 4 | 102 |",
- "| e | 4 | 96 |",
- "| e | 4 | 97 |",
- "+----+----+-----+",
- ];
- assert_batches_sorted_eq!(expected, &actual);
- Ok(())
-}
-
#[tokio::test]
async fn csv_query_group_by_substr() -> Result<()> {
let ctx = SessionContext::new();
diff --git a/datafusion/sql/src/planner.rs b/datafusion/sql/src/planner.rs
index 266f593bac..ee38393181 100644
--- a/datafusion/sql/src/planner.rs
+++ b/datafusion/sql/src/planner.rs
@@ -1193,33 +1193,24 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
.collect::<Result<Vec<Expr>>>()?;
// process group by, aggregation or having
- let (plan, mut select_exprs_post_aggr, having_expr_post_aggr) =
- if !group_by_exprs.is_empty() || !aggr_exprs.is_empty() {
- self.aggregate(
- plan,
- &select_exprs,
- having_expr_opt.as_ref(),
- group_by_exprs,
- aggr_exprs,
- )?
- } else {
- if let Some(having_expr) = &having_expr_opt {
- let available_columns = select_exprs
- .iter()
- .map(|expr| expr_as_column_expr(expr, &plan))
- .collect::<Result<Vec<Expr>>>()?;
-
- // Ensure the HAVING expression is using only columns
- // provided by the SELECT.
- check_columns_satisfy_exprs(
- &available_columns,
- &[having_expr.clone()],
- "HAVING clause references column(s) not provided by the select",
- )?;
+ let (plan, mut select_exprs_post_aggr, having_expr_post_aggr) = if !group_by_exprs
+ .is_empty()
+ || !aggr_exprs.is_empty()
+ {
+ self.aggregate(
+ plan,
+ &select_exprs,
+ having_expr_opt.as_ref(),
+ group_by_exprs,
+ aggr_exprs,
+ )?
+ } else {
+ match having_expr_opt {
+ Some(having_expr) => return Err(DataFusionError::Plan(
+ format!("HAVING clause references: {} must appear in the GROUP BY clause or be used in an aggregate function", having_expr))),
+ None => (plan, select_exprs, having_expr_opt)
}
-
- (plan, select_exprs, having_expr_opt)
- };
+ };
let plan = if let Some(having_expr_post_aggr) = having_expr_post_aggr {
LogicalPlanBuilder::from(plan)
@@ -3664,10 +3655,11 @@ mod tests {
let sql = "SELECT id, age
FROM person
HAVING age > 100 AND age < 200";
- let expected = "Projection: person.id, person.age\
- \n Filter: person.age > Int64(100) AND person.age < Int64(200)\
- \n TableScan: person";
- quick_test(sql, expected);
+ let err = logical_plan(sql).expect_err("query should have failed");
+ assert_eq!(
+ "Plan(\"HAVING clause references: person.age > Int64(100) AND person.age < Int64(200) must appear in the GROUP BY clause or be used in an aggregate function\")",
+ format!("{:?}", err)
+ );
}
#[test]
@@ -3677,7 +3669,20 @@ mod tests {
HAVING first_name = 'M'";
let err = logical_plan(sql).expect_err("query should have failed");
assert_eq!(
- "Plan(\"HAVING clause references column(s) not provided by the select: Expression person.first_name could not be resolved from available columns: person.id, person.age\")",
+ "Plan(\"HAVING clause references: person.first_name = Utf8(\\\"M\\\") must appear in the GROUP BY clause or be used in an aggregate function\")",
+ format!("{:?}", err)
+ );
+ }
+
+ #[test]
+ fn select_with_having_refers_to_invalid_column() {
+ let sql = "SELECT id, MAX(age)
+ FROM person
+ GROUP BY id
+ HAVING first_name = 'M'";
+ let err = logical_plan(sql).expect_err("query should have failed");
+ assert_eq!(
+ "Plan(\"HAVING clause references non-aggregate values: Expression person.first_name could not be resolved from available columns: person.id, MAX(person.age)\")",
format!("{:?}", err)
);
}
@@ -3689,9 +3694,7 @@ mod tests {
HAVING age > 100";
let err = logical_plan(sql).expect_err("query should have failed");
assert_eq!(
- "Plan(\"HAVING clause references column(s) not provided by the select: \
- Expression person.age could not be resolved from available columns: \
- person.id, person.age + Int64(1)\")",
+ "Plan(\"HAVING clause references: person.age > Int64(100) must appear in the GROUP BY clause or be used in an aggregate function\")",
format!("{:?}", err)
);
}