Skip to content

Commit

Permalink
fix: make sure CASE WHEN pick first true branch when WHEN clause is t…
Browse files Browse the repository at this point in the history
…rue (#8477)

* fix: make case when pick first true branch when when clause is true

* add more test
  • Loading branch information
haohuaijin authored Dec 15, 2023
1 parent 14c99b8 commit 5a24ec9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions datafusion/physical-expr/src/expressions/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ impl CaseExpr {
0 => Cow::Borrowed(&when_match),
_ => Cow::Owned(prep_null_mask_filter(&when_match)),
};
// Make sure we only consider rows that have not been matched yet
let when_match = and(&when_match, &remainder)?;

let then_value = self.when_then_expr[i]
.1
Expand Down Expand Up @@ -206,6 +208,8 @@ impl CaseExpr {
0 => Cow::Borrowed(when_value),
_ => Cow::Owned(prep_null_mask_filter(when_value)),
};
// Make sure we only consider rows that have not been matched yet
let when_value = and(&when_value, &remainder)?;

let then_value = self.when_then_expr[i]
.1
Expand Down
10 changes: 10 additions & 0 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1943,3 +1943,13 @@ select
;
----
true true true true true true true true true true

query I
SELECT ALL - CASE WHEN NOT - AVG ( - 41 ) IS NULL THEN 47 WHEN NULL IS NULL THEN COUNT ( * ) END + 93 + - - 44 * 91 + CASE + 44 WHEN - - 21 * 69 - 12 THEN 58 ELSE - 3 END * + + 23 * + 84 * - - 59
----
-337914

query T
SELECT CASE 3 WHEN 1+2 THEN 'first' WHEN 1+1+1 THEN 'second' END
----
first

0 comments on commit 5a24ec9

Please sign in to comment.