boolean.out 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. --
  2. -- BOOLEAN
  3. --
  4. --
  5. -- sanity check - if this fails go insane!
  6. --
  7. SELECT 1 AS one;
  8. one
  9. -----
  10. 1
  11. (1 row)
  12. -- ******************testing built-in type bool********************
  13. -- check bool input syntax
  14. SELECT true AS true;
  15. true
  16. ------
  17. t
  18. (1 row)
  19. SELECT false AS false;
  20. false
  21. -------
  22. f
  23. (1 row)
  24. SELECT bool 't' AS true;
  25. true
  26. ------
  27. t
  28. (1 row)
  29. SELECT bool ' f ' AS false;
  30. false
  31. -------
  32. f
  33. (1 row)
  34. SELECT bool 'true' AS true;
  35. true
  36. ------
  37. t
  38. (1 row)
  39. SELECT bool 'test' AS error;
  40. ERROR: invalid input syntax for type boolean: "test"
  41. LINE 1: SELECT bool 'test' AS error;
  42. ^
  43. SELECT bool 'false' AS false;
  44. false
  45. -------
  46. f
  47. (1 row)
  48. SELECT bool 'foo' AS error;
  49. ERROR: invalid input syntax for type boolean: "foo"
  50. LINE 1: SELECT bool 'foo' AS error;
  51. ^
  52. SELECT bool 'y' AS true;
  53. true
  54. ------
  55. t
  56. (1 row)
  57. SELECT bool 'yes' AS true;
  58. true
  59. ------
  60. t
  61. (1 row)
  62. SELECT bool 'yeah' AS error;
  63. ERROR: invalid input syntax for type boolean: "yeah"
  64. LINE 1: SELECT bool 'yeah' AS error;
  65. ^
  66. SELECT bool 'n' AS false;
  67. false
  68. -------
  69. f
  70. (1 row)
  71. SELECT bool 'no' AS false;
  72. false
  73. -------
  74. f
  75. (1 row)
  76. SELECT bool 'nay' AS error;
  77. ERROR: invalid input syntax for type boolean: "nay"
  78. LINE 1: SELECT bool 'nay' AS error;
  79. ^
  80. SELECT bool 'on' AS true;
  81. true
  82. ------
  83. t
  84. (1 row)
  85. SELECT bool 'off' AS false;
  86. false
  87. -------
  88. f
  89. (1 row)
  90. SELECT bool 'of' AS false;
  91. false
  92. -------
  93. f
  94. (1 row)
  95. SELECT bool 'o' AS error;
  96. ERROR: invalid input syntax for type boolean: "o"
  97. LINE 1: SELECT bool 'o' AS error;
  98. ^
  99. SELECT bool 'on_' AS error;
  100. ERROR: invalid input syntax for type boolean: "on_"
  101. LINE 1: SELECT bool 'on_' AS error;
  102. ^
  103. SELECT bool 'off_' AS error;
  104. ERROR: invalid input syntax for type boolean: "off_"
  105. LINE 1: SELECT bool 'off_' AS error;
  106. ^
  107. SELECT bool '1' AS true;
  108. true
  109. ------
  110. t
  111. (1 row)
  112. SELECT bool '11' AS error;
  113. ERROR: invalid input syntax for type boolean: "11"
  114. LINE 1: SELECT bool '11' AS error;
  115. ^
  116. SELECT bool '0' AS false;
  117. false
  118. -------
  119. f
  120. (1 row)
  121. SELECT bool '000' AS error;
  122. ERROR: invalid input syntax for type boolean: "000"
  123. LINE 1: SELECT bool '000' AS error;
  124. ^
  125. SELECT bool '' AS error;
  126. ERROR: invalid input syntax for type boolean: ""
  127. LINE 1: SELECT bool '' AS error;
  128. ^
  129. -- and, or, not in qualifications
  130. SELECT bool 't' or bool 'f' AS true;
  131. true
  132. ------
  133. t
  134. (1 row)
  135. SELECT bool 't' and bool 'f' AS false;
  136. false
  137. -------
  138. f
  139. (1 row)
  140. SELECT not bool 'f' AS true;
  141. true
  142. ------
  143. t
  144. (1 row)
  145. SELECT bool 't' = bool 'f' AS false;
  146. false
  147. -------
  148. f
  149. (1 row)
  150. SELECT bool 't' <> bool 'f' AS true;
  151. true
  152. ------
  153. t
  154. (1 row)
  155. SELECT bool 't' > bool 'f' AS true;
  156. true
  157. ------
  158. t
  159. (1 row)
  160. SELECT bool 't' >= bool 'f' AS true;
  161. true
  162. ------
  163. t
  164. (1 row)
  165. SELECT bool 'f' < bool 't' AS true;
  166. true
  167. ------
  168. t
  169. (1 row)
  170. SELECT bool 'f' <= bool 't' AS true;
  171. true
  172. ------
  173. t
  174. (1 row)
  175. -- explicit casts to/from text
  176. SELECT 'TrUe'::text::boolean AS true, 'fAlse'::text::boolean AS false;
  177. true | false
  178. ------+-------
  179. t | f
  180. (1 row)
  181. SELECT ' true '::text::boolean AS true,
  182. ' FALSE'::text::boolean AS false;
  183. true | false
  184. ------+-------
  185. t | f
  186. (1 row)
  187. SELECT true::boolean::text AS true, false::boolean::text AS false;
  188. true | false
  189. ------+-------
  190. true | false
  191. (1 row)
  192. SELECT ' tru e '::text::boolean AS invalid; -- error
  193. ERROR: invalid input syntax for type boolean: " tru e "
  194. SELECT ''::text::boolean AS invalid; -- error
  195. ERROR: invalid input syntax for type boolean: ""
  196. CREATE TABLE BOOLTBL1 (f1 bool);
  197. INSERT INTO BOOLTBL1 (f1) VALUES (bool 't');
  198. INSERT INTO BOOLTBL1 (f1) VALUES (bool 'True');
  199. INSERT INTO BOOLTBL1 (f1) VALUES (bool 'true');
  200. -- BOOLTBL1 should be full of true's at this point
  201. SELECT BOOLTBL1.* FROM BOOLTBL1;
  202. f1
  203. ----
  204. t
  205. t
  206. t
  207. (3 rows)
  208. SELECT BOOLTBL1.*
  209. FROM BOOLTBL1
  210. WHERE f1 = bool 'true';
  211. f1
  212. ----
  213. t
  214. t
  215. t
  216. (3 rows)
  217. SELECT BOOLTBL1.*
  218. FROM BOOLTBL1
  219. WHERE f1 <> bool 'false';
  220. f1
  221. ----
  222. t
  223. t
  224. t
  225. (3 rows)
  226. SELECT BOOLTBL1.*
  227. FROM BOOLTBL1
  228. WHERE booleq(bool 'false', f1);
  229. f1
  230. ----
  231. (0 rows)
  232. INSERT INTO BOOLTBL1 (f1) VALUES (bool 'f');
  233. SELECT BOOLTBL1.*
  234. FROM BOOLTBL1
  235. WHERE f1 = bool 'false';
  236. f1
  237. ----
  238. f
  239. (1 row)
  240. CREATE TABLE BOOLTBL2 (f1 bool);
  241. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'f');
  242. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'false');
  243. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'False');
  244. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'FALSE');
  245. -- This is now an invalid expression
  246. -- For pre-v6.3 this evaluated to false - thomas 1997-10-23
  247. INSERT INTO BOOLTBL2 (f1)
  248. VALUES (bool 'XXX');
  249. ERROR: invalid input syntax for type boolean: "XXX"
  250. LINE 2: VALUES (bool 'XXX');
  251. ^
  252. -- BOOLTBL2 should be full of false's at this point
  253. SELECT BOOLTBL2.* FROM BOOLTBL2;
  254. f1
  255. ----
  256. f
  257. f
  258. f
  259. f
  260. (4 rows)
  261. --
  262. -- SQL syntax
  263. -- Try all combinations to ensure that we get nothing when we expect nothing
  264. -- - thomas 2000-01-04
  265. --
  266. SELECT f1
  267. FROM BOOLTBL1
  268. WHERE f1 IS TRUE;
  269. f1
  270. ----
  271. t
  272. t
  273. t
  274. (3 rows)
  275. SELECT f1
  276. FROM BOOLTBL1
  277. WHERE f1 IS NOT FALSE;
  278. f1
  279. ----
  280. t
  281. t
  282. t
  283. (3 rows)
  284. SELECT f1
  285. FROM BOOLTBL1
  286. WHERE f1 IS FALSE;
  287. f1
  288. ----
  289. f
  290. (1 row)
  291. SELECT f1
  292. FROM BOOLTBL1
  293. WHERE f1 IS NOT TRUE;
  294. f1
  295. ----
  296. f
  297. (1 row)
  298. SELECT f1
  299. FROM BOOLTBL2
  300. WHERE f1 IS TRUE;
  301. f1
  302. ----
  303. (0 rows)
  304. SELECT f1
  305. FROM BOOLTBL2
  306. WHERE f1 IS NOT FALSE;
  307. f1
  308. ----
  309. (0 rows)
  310. SELECT f1
  311. FROM BOOLTBL2
  312. WHERE f1 IS FALSE;
  313. f1
  314. ----
  315. f
  316. f
  317. f
  318. f
  319. (4 rows)
  320. SELECT f1
  321. FROM BOOLTBL2
  322. WHERE f1 IS NOT TRUE;
  323. f1
  324. ----
  325. f
  326. f
  327. f
  328. f
  329. (4 rows)
  330. --
  331. -- Tests for BooleanTest
  332. --
  333. CREATE TABLE BOOLTBL3 (d text, b bool, o int);
  334. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('true', true, 1);
  335. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('false', false, 2);
  336. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('null', null, 3);
  337. -- Test to make sure short-circuiting and NULL handling is
  338. -- correct. Use a table as source to prevent constant simplification
  339. -- to interfer.
  340. CREATE TABLE booltbl4(isfalse bool, istrue bool, isnul bool);
  341. INSERT INTO booltbl4 VALUES (false, true, null);
  342. \pset null '(null)'
  343. -- AND expression need to return null if there's any nulls and not all
  344. -- of the value are true
  345. SELECT istrue AND isnul AND istrue FROM booltbl4;
  346. ?column?
  347. ----------
  348. (null)
  349. (1 row)
  350. SELECT istrue AND istrue AND isnul FROM booltbl4;
  351. ?column?
  352. ----------
  353. (null)
  354. (1 row)
  355. SELECT isnul AND istrue AND istrue FROM booltbl4;
  356. ?column?
  357. ----------
  358. (null)
  359. (1 row)
  360. SELECT isfalse AND isnul AND istrue FROM booltbl4;
  361. ?column?
  362. ----------
  363. f
  364. (1 row)
  365. SELECT istrue AND isfalse AND isnul FROM booltbl4;
  366. ?column?
  367. ----------
  368. f
  369. (1 row)
  370. SELECT isnul AND istrue AND isfalse FROM booltbl4;
  371. ?column?
  372. ----------
  373. f
  374. (1 row)
  375. -- OR expression need to return null if there's any nulls and none
  376. -- of the value is true
  377. SELECT isfalse OR isnul OR isfalse FROM booltbl4;
  378. ?column?
  379. ----------
  380. (null)
  381. (1 row)
  382. SELECT isfalse OR isfalse OR isnul FROM booltbl4;
  383. ?column?
  384. ----------
  385. (null)
  386. (1 row)
  387. SELECT isnul OR isfalse OR isfalse FROM booltbl4;
  388. ?column?
  389. ----------
  390. (null)
  391. (1 row)
  392. SELECT isfalse OR isnul OR istrue FROM booltbl4;
  393. ?column?
  394. ----------
  395. t
  396. (1 row)
  397. SELECT istrue OR isfalse OR isnul FROM booltbl4;
  398. ?column?
  399. ----------
  400. t
  401. (1 row)
  402. SELECT isnul OR istrue OR isfalse FROM booltbl4;
  403. ?column?
  404. ----------
  405. t
  406. (1 row)
  407. --
  408. -- Clean up
  409. -- Many tables are retained by the regression test, but these do not seem
  410. -- particularly useful so just get rid of them for now.
  411. -- - thomas 1997-11-30
  412. --
  413. DROP TABLE BOOLTBL1;
  414. DROP TABLE BOOLTBL2;
  415. DROP TABLE BOOLTBL3;