Skip to content

Commit

Permalink
Temporary Fix: Query error when grouping by case expressions (#11133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahgao committed Jun 26, 2024
1 parent f6f63b9 commit 7e49ccf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 8 additions & 3 deletions datafusion/core/src/physical_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,18 @@ fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result<String> {
Expr::Case(case) => {
let mut name = "CASE ".to_string();
if let Some(e) = &case.expr {
let _ = write!(name, "{e} ");
let _ = write!(name, "{} ", create_physical_name(e, false)?);
}
for (w, t) in &case.when_then_expr {
let _ = write!(name, "WHEN {w} THEN {t} ");
let _ = write!(
name,
"WHEN {} THEN {} ",
create_physical_name(w, false)?,
create_physical_name(t, false)?
);
}
if let Some(e) = &case.else_expr {
let _ = write!(name, "ELSE {e} ");
let _ = write!(name, "ELSE {} ", create_physical_name(e, false)?);
}
name += "END";
Ok(name)
Expand Down
19 changes: 19 additions & 0 deletions datafusion/sqllogictest/test_files/group_by.slt
Original file line number Diff line number Diff line change
Expand Up @@ -5135,3 +5135,22 @@ GROUP BY
ts_chunk;
----
2024-01-01T00:00:00 4

# Issue: https://github.com/apache/datafusion/issues/11118
statement ok
CREATE TABLE test_case_expr(a INT, b TEXT) AS VALUES (1,'hello'), (2,'world')

query T
SELECT (CASE WHEN CONCAT(b, 'hello') = 'test' THEN 'good' ELSE 'bad' END) AS c
FROM test_case_expr GROUP BY c;
----
bad

query I rowsort
SELECT (CASE a::BIGINT WHEN 1 THEN 1 END) AS c FROM test_case_expr GROUP BY c;
----
1
NULL

statement ok
drop table test_case_expr

0 comments on commit 7e49ccf

Please sign in to comment.