boolean.out 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. SELECT BOOLTBL1.*, BOOLTBL2.*
  262. FROM BOOLTBL1, BOOLTBL2
  263. WHERE BOOLTBL2.f1 <> BOOLTBL1.f1;
  264. f1 | f1
  265. ----+----
  266. t | f
  267. t | f
  268. t | f
  269. t | f
  270. t | f
  271. t | f
  272. t | f
  273. t | f
  274. t | f
  275. t | f
  276. t | f
  277. t | f
  278. (12 rows)
  279. SELECT BOOLTBL1.*, BOOLTBL2.*
  280. FROM BOOLTBL1, BOOLTBL2
  281. WHERE boolne(BOOLTBL2.f1,BOOLTBL1.f1);
  282. f1 | f1
  283. ----+----
  284. t | f
  285. t | f
  286. t | f
  287. t | f
  288. t | f
  289. t | f
  290. t | f
  291. t | f
  292. t | f
  293. t | f
  294. t | f
  295. t | f
  296. (12 rows)
  297. SELECT BOOLTBL1.*, BOOLTBL2.*
  298. FROM BOOLTBL1, BOOLTBL2
  299. WHERE BOOLTBL2.f1 = BOOLTBL1.f1 and BOOLTBL1.f1 = bool 'false';
  300. f1 | f1
  301. ----+----
  302. f | f
  303. f | f
  304. f | f
  305. f | f
  306. (4 rows)
  307. SELECT BOOLTBL1.*, BOOLTBL2.*
  308. FROM BOOLTBL1, BOOLTBL2
  309. WHERE BOOLTBL2.f1 = BOOLTBL1.f1 or BOOLTBL1.f1 = bool 'true'
  310. ORDER BY BOOLTBL1.f1, BOOLTBL2.f1;
  311. f1 | f1
  312. ----+----
  313. f | f
  314. f | f
  315. f | f
  316. f | f
  317. t | f
  318. t | f
  319. t | f
  320. t | f
  321. t | f
  322. t | f
  323. t | f
  324. t | f
  325. t | f
  326. t | f
  327. t | f
  328. t | f
  329. (16 rows)
  330. --
  331. -- SQL syntax
  332. -- Try all combinations to ensure that we get nothing when we expect nothing
  333. -- - thomas 2000-01-04
  334. --
  335. SELECT f1
  336. FROM BOOLTBL1
  337. WHERE f1 IS TRUE;
  338. f1
  339. ----
  340. t
  341. t
  342. t
  343. (3 rows)
  344. SELECT f1
  345. FROM BOOLTBL1
  346. WHERE f1 IS NOT FALSE;
  347. f1
  348. ----
  349. t
  350. t
  351. t
  352. (3 rows)
  353. SELECT f1
  354. FROM BOOLTBL1
  355. WHERE f1 IS FALSE;
  356. f1
  357. ----
  358. f
  359. (1 row)
  360. SELECT f1
  361. FROM BOOLTBL1
  362. WHERE f1 IS NOT TRUE;
  363. f1
  364. ----
  365. f
  366. (1 row)
  367. SELECT f1
  368. FROM BOOLTBL2
  369. WHERE f1 IS TRUE;
  370. f1
  371. ----
  372. (0 rows)
  373. SELECT f1
  374. FROM BOOLTBL2
  375. WHERE f1 IS NOT FALSE;
  376. f1
  377. ----
  378. (0 rows)
  379. SELECT f1
  380. FROM BOOLTBL2
  381. WHERE f1 IS FALSE;
  382. f1
  383. ----
  384. f
  385. f
  386. f
  387. f
  388. (4 rows)
  389. SELECT f1
  390. FROM BOOLTBL2
  391. WHERE f1 IS NOT TRUE;
  392. f1
  393. ----
  394. f
  395. f
  396. f
  397. f
  398. (4 rows)
  399. --
  400. -- Tests for BooleanTest
  401. --
  402. CREATE TABLE BOOLTBL3 (d text, b bool, o int);
  403. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('true', true, 1);
  404. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('false', false, 2);
  405. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('null', null, 3);
  406. SELECT
  407. d,
  408. b IS TRUE AS istrue,
  409. b IS NOT TRUE AS isnottrue,
  410. b IS FALSE AS isfalse,
  411. b IS NOT FALSE AS isnotfalse,
  412. b IS UNKNOWN AS isunknown,
  413. b IS NOT UNKNOWN AS isnotunknown
  414. FROM booltbl3 ORDER BY o;
  415. d | istrue | isnottrue | isfalse | isnotfalse | isunknown | isnotunknown
  416. -------+--------+-----------+---------+------------+-----------+--------------
  417. true | t | f | f | t | f | t
  418. false | f | t | t | f | f | t
  419. null | f | t | f | t | t | f
  420. (3 rows)
  421. -- Test to make sure short-circuiting and NULL handling is
  422. -- correct. Use a table as source to prevent constant simplification
  423. -- to interfer.
  424. CREATE TABLE booltbl4(isfalse bool, istrue bool, isnul bool);
  425. INSERT INTO booltbl4 VALUES (false, true, null);
  426. \pset null '(null)'
  427. -- AND expression need to return null if there's any nulls and not all
  428. -- of the value are true
  429. SELECT istrue AND isnul AND istrue FROM booltbl4;
  430. ?column?
  431. ----------
  432. (null)
  433. (1 row)
  434. SELECT istrue AND istrue AND isnul FROM booltbl4;
  435. ?column?
  436. ----------
  437. (null)
  438. (1 row)
  439. SELECT isnul AND istrue AND istrue FROM booltbl4;
  440. ?column?
  441. ----------
  442. (null)
  443. (1 row)
  444. SELECT isfalse AND isnul AND istrue FROM booltbl4;
  445. ?column?
  446. ----------
  447. f
  448. (1 row)
  449. SELECT istrue AND isfalse AND isnul FROM booltbl4;
  450. ?column?
  451. ----------
  452. f
  453. (1 row)
  454. SELECT isnul AND istrue AND isfalse FROM booltbl4;
  455. ?column?
  456. ----------
  457. f
  458. (1 row)
  459. -- OR expression need to return null if there's any nulls and none
  460. -- of the value is true
  461. SELECT isfalse OR isnul OR isfalse FROM booltbl4;
  462. ?column?
  463. ----------
  464. (null)
  465. (1 row)
  466. SELECT isfalse OR isfalse OR isnul FROM booltbl4;
  467. ?column?
  468. ----------
  469. (null)
  470. (1 row)
  471. SELECT isnul OR isfalse OR isfalse FROM booltbl4;
  472. ?column?
  473. ----------
  474. (null)
  475. (1 row)
  476. SELECT isfalse OR isnul OR istrue FROM booltbl4;
  477. ?column?
  478. ----------
  479. t
  480. (1 row)
  481. SELECT istrue OR isfalse OR isnul FROM booltbl4;
  482. ?column?
  483. ----------
  484. t
  485. (1 row)
  486. SELECT isnul OR istrue OR isfalse FROM booltbl4;
  487. ?column?
  488. ----------
  489. t
  490. (1 row)
  491. --
  492. -- Clean up
  493. -- Many tables are retained by the regression test, but these do not seem
  494. -- particularly useful so just get rid of them for now.
  495. -- - thomas 1997-11-30
  496. --
  497. DROP TABLE BOOLTBL1;
  498. DROP TABLE BOOLTBL2;
  499. DROP TABLE BOOLTBL3;
  500. DROP TABLE BOOLTBL4;