case.out 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. --
  2. -- CASE
  3. -- Test the case statement
  4. --
  5. CREATE TABLE CASE_TBL (
  6. i integer,
  7. f double precision
  8. );
  9. CREATE TABLE CASE2_TBL (
  10. i integer,
  11. j integer
  12. );
  13. INSERT INTO CASE_TBL VALUES (1, 10.1);
  14. INSERT INTO CASE_TBL VALUES (2, 20.2);
  15. INSERT INTO CASE_TBL VALUES (3, -30.3);
  16. INSERT INTO CASE_TBL VALUES (4, NULL);
  17. INSERT INTO CASE2_TBL VALUES (1, -1);
  18. INSERT INTO CASE2_TBL VALUES (2, -2);
  19. INSERT INTO CASE2_TBL VALUES (3, -3);
  20. INSERT INTO CASE2_TBL VALUES (2, -4);
  21. INSERT INTO CASE2_TBL VALUES (1, NULL);
  22. INSERT INTO CASE2_TBL VALUES (NULL, -6);
  23. --
  24. -- Simplest examples without tables
  25. --
  26. SELECT '3' AS "One",
  27. CASE
  28. WHEN 1 < 2 THEN 3
  29. END AS "Simple WHEN";
  30. One | Simple WHEN
  31. -----+-------------
  32. 3 | 3
  33. (1 row)
  34. SELECT '<NULL>' AS "One",
  35. CASE
  36. WHEN 1 > 2 THEN 3
  37. END AS "Simple default";
  38. One | Simple default
  39. --------+----------------
  40. <NULL> |
  41. (1 row)
  42. SELECT '3' AS "One",
  43. CASE
  44. WHEN 1 < 2 THEN 3
  45. ELSE 4
  46. END AS "Simple ELSE";
  47. One | Simple ELSE
  48. -----+-------------
  49. 3 | 3
  50. (1 row)
  51. SELECT '4' AS "One",
  52. CASE
  53. WHEN 1 > 2 THEN 3
  54. ELSE 4
  55. END AS "ELSE default";
  56. One | ELSE default
  57. -----+--------------
  58. 4 | 4
  59. (1 row)
  60. SELECT '6' AS "One",
  61. CASE
  62. WHEN 1 > 2 THEN 3
  63. WHEN 4 < 5 THEN 6
  64. ELSE 7
  65. END AS "Two WHEN with default";
  66. One | Two WHEN with default
  67. -----+-----------------------
  68. 6 | 6
  69. (1 row)
  70. SELECT '7' AS "None",
  71. CASE WHEN random() < 0 THEN 1
  72. END AS "NULL on no matches";
  73. None | NULL on no matches
  74. ------+--------------------
  75. 7 |
  76. (1 row)
  77. -- Constant-expression folding shouldn't evaluate unreachable subexpressions
  78. SELECT CASE WHEN 1=0 THEN 1/0 WHEN 1=1 THEN 1 ELSE 2/0 END;
  79. case
  80. ------
  81. 1
  82. (1 row)
  83. SELECT CASE 1 WHEN 0 THEN 1/0 WHEN 1 THEN 1 ELSE 2/0 END;
  84. case
  85. ------
  86. 1
  87. (1 row)
  88. -- However we do not currently suppress folding of potentially
  89. -- reachable subexpressions
  90. SELECT CASE WHEN i > 100 THEN 1/0 ELSE 0 END FROM case_tbl;
  91. ERROR: division by zero
  92. -- Test for cases involving untyped literals in test expression
  93. SELECT CASE 'a' WHEN 'a' THEN 1 ELSE 2 END;
  94. case
  95. ------
  96. 1
  97. (1 row)
  98. --
  99. -- Examples of targets involving tables
  100. --
  101. SELECT
  102. CASE
  103. WHEN i >= 3 THEN i
  104. END AS ">= 3 or Null"
  105. FROM CASE_TBL;
  106. >= 3 or Null
  107. --------------
  108. 3
  109. 4
  110. (4 rows)
  111. SELECT
  112. CASE WHEN i >= 3 THEN (i + i)
  113. ELSE i
  114. END AS "Simplest Math"
  115. FROM CASE_TBL;
  116. Simplest Math
  117. ---------------
  118. 1
  119. 2
  120. 6
  121. 8
  122. (4 rows)
  123. SELECT i AS "Value",
  124. CASE WHEN (i < 0) THEN 'small'
  125. WHEN (i = 0) THEN 'zero'
  126. WHEN (i = 1) THEN 'one'
  127. WHEN (i = 2) THEN 'two'
  128. ELSE 'big'
  129. END AS "Category"
  130. FROM CASE_TBL;
  131. Value | Category
  132. -------+----------
  133. 1 | one
  134. 2 | two
  135. 3 | big
  136. 4 | big
  137. (4 rows)
  138. SELECT
  139. CASE WHEN ((i < 0) or (i < 0)) THEN 'small'
  140. WHEN ((i = 0) or (i = 0)) THEN 'zero'
  141. WHEN ((i = 1) or (i = 1)) THEN 'one'
  142. WHEN ((i = 2) or (i = 2)) THEN 'two'
  143. ELSE 'big'
  144. END AS "Category"
  145. FROM CASE_TBL;
  146. Category
  147. ----------
  148. one
  149. two
  150. big
  151. big
  152. (4 rows)
  153. --
  154. -- Examples of qualifications involving tables
  155. --
  156. --
  157. -- NULLIF() and COALESCE()
  158. -- Shorthand forms for typical CASE constructs
  159. -- defined in the SQL standard.
  160. --
  161. SELECT * FROM CASE_TBL WHERE COALESCE(f,i) = 4;
  162. i | f
  163. ---+---
  164. 4 |
  165. (1 row)
  166. SELECT * FROM CASE_TBL WHERE NULLIF(f,i) = 2;
  167. i | f
  168. ---+---
  169. (0 rows)
  170. SELECT COALESCE(a.f, b.i, b.j)
  171. FROM CASE_TBL a, CASE2_TBL b;
  172. coalesce
  173. ----------
  174. 10.1
  175. 20.2
  176. -30.3
  177. 1
  178. 10.1
  179. 20.2
  180. -30.3
  181. 2
  182. 10.1
  183. 20.2
  184. -30.3
  185. 3
  186. 10.1
  187. 20.2
  188. -30.3
  189. 2
  190. 10.1
  191. 20.2
  192. -30.3
  193. 1
  194. 10.1
  195. 20.2
  196. -30.3
  197. -6
  198. (24 rows)
  199. SELECT *
  200. FROM CASE_TBL a, CASE2_TBL b
  201. WHERE COALESCE(a.f, b.i, b.j) = 2;
  202. i | f | i | j
  203. ---+---+---+----
  204. 4 | | 2 | -2
  205. 4 | | 2 | -4
  206. (2 rows)
  207. SELECT NULLIF(a.i,b.i) AS "NULLIF(a.i,b.i)",
  208. NULLIF(b.i, 4) AS "NULLIF(b.i,4)"
  209. FROM CASE_TBL a, CASE2_TBL b;
  210. NULLIF(a.i,b.i) | NULLIF(b.i,4)
  211. -----------------+---------------
  212. | 1
  213. 2 | 1
  214. 3 | 1
  215. 4 | 1
  216. 1 | 2
  217. | 2
  218. 3 | 2
  219. 4 | 2
  220. 1 | 3
  221. 2 | 3
  222. | 3
  223. 4 | 3
  224. 1 | 2
  225. | 2
  226. 3 | 2
  227. 4 | 2
  228. | 1
  229. 2 | 1
  230. 3 | 1
  231. 4 | 1
  232. 1 |
  233. 2 |
  234. 3 |
  235. 4 |
  236. (24 rows)
  237. SELECT *
  238. FROM CASE_TBL a, CASE2_TBL b
  239. WHERE COALESCE(f,b.i) = 2;
  240. i | f | i | j
  241. ---+---+---+----
  242. 4 | | 2 | -2
  243. 4 | | 2 | -4
  244. (2 rows)
  245. -- Tests for constant subexpression simplification
  246. explain (costs off)
  247. SELECT * FROM CASE_TBL WHERE NULLIF(1, 2) = 2;
  248. QUERY PLAN
  249. --------------------------
  250. Result
  251. One-Time Filter: false
  252. (2 rows)
  253. explain (costs off)
  254. SELECT * FROM CASE_TBL WHERE NULLIF(1, 1) IS NOT NULL;
  255. QUERY PLAN
  256. --------------------------
  257. Result
  258. One-Time Filter: false
  259. (2 rows)
  260. explain (costs off)
  261. SELECT * FROM CASE_TBL WHERE NULLIF(1, null) = 2;
  262. QUERY PLAN
  263. --------------------------
  264. Result
  265. One-Time Filter: false
  266. (2 rows)
  267. --
  268. -- Examples of updates involving tables
  269. --
  270. UPDATE CASE_TBL
  271. SET i = CASE WHEN i >= 3 THEN (- i)
  272. ELSE (2 * i) END;
  273. SELECT * FROM CASE_TBL;
  274. i | f
  275. ----+-------
  276. 2 | 10.1
  277. 4 | 20.2
  278. -3 | -30.3
  279. -4 |
  280. (4 rows)
  281. UPDATE CASE_TBL
  282. SET i = CASE WHEN i >= 2 THEN (2 * i)
  283. ELSE (3 * i) END;
  284. SELECT * FROM CASE_TBL;
  285. i | f
  286. -----+-------
  287. 4 | 10.1
  288. 8 | 20.2
  289. -9 | -30.3
  290. -12 |
  291. (4 rows)
  292. UPDATE CASE_TBL
  293. SET i = CASE WHEN b.i >= 2 THEN (2 * j)
  294. ELSE (3 * j) END
  295. FROM CASE2_TBL b
  296. WHERE j = -CASE_TBL.i;
  297. SELECT * FROM CASE_TBL;
  298. i | f
  299. -----+-------
  300. 8 | 20.2
  301. -9 | -30.3
  302. -12 |
  303. -8 | 10.1
  304. (4 rows)
  305. --
  306. -- Nested CASE expressions
  307. --
  308. -- This test exercises a bug caused by aliasing econtext->caseValue_isNull
  309. -- with the isNull argument of the inner CASE's CaseExpr evaluation. After
  310. -- evaluating the vol(null) expression in the inner CASE's second WHEN-clause,
  311. -- the isNull flag for the case test value incorrectly became true, causing
  312. -- the third WHEN-clause not to match. The volatile function calls are needed
  313. -- to prevent constant-folding in the planner, which would hide the bug.
  314. -- Wrap this in a single transaction so the transient '=' operator doesn't
  315. -- cause problems in concurrent sessions
  316. BEGIN;
  317. CREATE FUNCTION vol(text) returns text as
  318. 'begin return $1; end' language plpgsql volatile;
  319. SELECT CASE
  320. (CASE vol('bar')
  321. WHEN 'foo' THEN 'it was foo!'
  322. WHEN vol(null) THEN 'null input'
  323. WHEN 'bar' THEN 'it was bar!' END
  324. )
  325. WHEN 'it was foo!' THEN 'foo recognized'
  326. WHEN 'it was bar!' THEN 'bar recognized'
  327. ELSE 'unrecognized' END;
  328. case
  329. ----------------
  330. bar recognized
  331. (1 row)
  332. -- In this case, we can't inline the SQL function without confusing things.
  333. CREATE DOMAIN foodomain AS text;
  334. CREATE FUNCTION volfoo(text) returns foodomain as
  335. 'begin return $1::foodomain; end' language plpgsql volatile;
  336. CREATE FUNCTION inline_eq(foodomain, foodomain) returns boolean as
  337. 'SELECT CASE $2::text WHEN $1::text THEN true ELSE false END' language sql;
  338. CREATE OPERATOR = (procedure = inline_eq,
  339. leftarg = foodomain, rightarg = foodomain);
  340. SELECT CASE volfoo('bar') WHEN 'foo'::foodomain THEN 'is foo' ELSE 'is not foo' END;
  341. case
  342. ------------
  343. is not foo
  344. (1 row)
  345. ROLLBACK;
  346. -- Test multiple evaluation of a CASE arg that is a read/write object (#14472)
  347. -- Wrap this in a single transaction so the transient '=' operator doesn't
  348. -- cause problems in concurrent sessions
  349. BEGIN;
  350. CREATE DOMAIN arrdomain AS int[];
  351. CREATE FUNCTION make_ad(int,int) returns arrdomain as
  352. 'declare x arrdomain;
  353. begin
  354. x := array[$1,$2];
  355. return x;
  356. end' language plpgsql volatile;
  357. CREATE FUNCTION ad_eq(arrdomain, arrdomain) returns boolean as
  358. 'begin return array_eq($1, $2); end' language plpgsql;
  359. CREATE OPERATOR = (procedure = ad_eq,
  360. leftarg = arrdomain, rightarg = arrdomain);
  361. SELECT CASE make_ad(1,2)
  362. WHEN array[2,4]::arrdomain THEN 'wrong'
  363. WHEN array[2,5]::arrdomain THEN 'still wrong'
  364. WHEN array[1,2]::arrdomain THEN 'right'
  365. END;
  366. case
  367. -------
  368. right
  369. (1 row)
  370. ROLLBACK;
  371. -- Test interaction of CASE with ArrayCoerceExpr (bug #15471)
  372. BEGIN;
  373. CREATE TYPE casetestenum AS ENUM ('e', 'f', 'g');
  374. SELECT
  375. CASE 'foo'::text
  376. WHEN 'foo' THEN ARRAY['a', 'b', 'c', 'd'] || enum_range(NULL::casetestenum)::text[]
  377. ELSE ARRAY['x', 'y']
  378. END;
  379. array
  380. -----------------
  381. {a,b,c,d,e,f,g}
  382. (1 row)
  383. ROLLBACK;
  384. --
  385. -- Clean up
  386. --
  387. DROP TABLE CASE_TBL;
  388. DROP TABLE CASE2_TBL;