mirror of
https://github.com/postgres/postgres.git
synced 2025-12-10 14:22:35 +03:00
Adjust constant-folding of CASE expressions so that the simple comparison
form of CASE (eg, CASE 0 WHEN 1 THEN ...) can be constant-folded as it was in 7.4. Also, avoid constant-folding result expressions that are certainly unreachable --- the former coding was a bit cavalier about this and could generate unexpected results for all-constant CASE expressions. Add regression test cases. Per report from Vlad Marchenko.
This commit is contained in:
@@ -72,6 +72,23 @@ SELECT '6' AS "One",
|
||||
6 | 6
|
||||
(1 row)
|
||||
|
||||
-- Constant-expression folding shouldn't evaluate unreachable subexpressions
|
||||
SELECT CASE WHEN 1=0 THEN 1/0 WHEN 1=1 THEN 1 ELSE 2/0 END;
|
||||
case
|
||||
------
|
||||
1
|
||||
(1 row)
|
||||
|
||||
SELECT CASE 1 WHEN 0 THEN 1/0 WHEN 1 THEN 1 ELSE 2/0 END;
|
||||
case
|
||||
------
|
||||
1
|
||||
(1 row)
|
||||
|
||||
-- However we do not currently suppress folding of potentially
|
||||
-- reachable subexpressions
|
||||
SELECT CASE WHEN i > 100 THEN 1/0 ELSE 0 END FROM case_tbl;
|
||||
ERROR: division by zero
|
||||
-- Test for cases involving untyped literals in test expression
|
||||
SELECT CASE 'a' WHEN 'a' THEN 1 ELSE 2 END;
|
||||
case
|
||||
|
||||
@@ -58,6 +58,14 @@ SELECT '6' AS "One",
|
||||
ELSE 7
|
||||
END AS "Two WHEN with default";
|
||||
|
||||
-- Constant-expression folding shouldn't evaluate unreachable subexpressions
|
||||
SELECT CASE WHEN 1=0 THEN 1/0 WHEN 1=1 THEN 1 ELSE 2/0 END;
|
||||
SELECT CASE 1 WHEN 0 THEN 1/0 WHEN 1 THEN 1 ELSE 2/0 END;
|
||||
|
||||
-- However we do not currently suppress folding of potentially
|
||||
-- reachable subexpressions
|
||||
SELECT CASE WHEN i > 100 THEN 1/0 ELSE 0 END FROM case_tbl;
|
||||
|
||||
-- Test for cases involving untyped literals in test expression
|
||||
SELECT CASE 'a' WHEN 'a' THEN 1 ELSE 2 END;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user