boolean.sql 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. --
  2. -- BOOLEAN
  3. --
  4. --
  5. -- sanity check - if this fails go insane!
  6. --
  7. SELECT 1 AS one;
  8. -- ******************testing built-in type bool********************
  9. -- check bool input syntax
  10. SELECT true AS true;
  11. SELECT false AS false;
  12. SELECT bool 't' AS true;
  13. SELECT bool ' f ' AS false;
  14. SELECT bool 'true' AS true;
  15. SELECT bool 'test' AS error;
  16. SELECT bool 'false' AS false;
  17. SELECT bool 'foo' AS error;
  18. SELECT bool 'y' AS true;
  19. SELECT bool 'yes' AS true;
  20. SELECT bool 'yeah' AS error;
  21. SELECT bool 'n' AS false;
  22. SELECT bool 'no' AS false;
  23. SELECT bool 'nay' AS error;
  24. SELECT bool 'on' AS true;
  25. SELECT bool 'off' AS false;
  26. SELECT bool 'of' AS false;
  27. SELECT bool 'o' AS error;
  28. SELECT bool 'on_' AS error;
  29. SELECT bool 'off_' AS error;
  30. SELECT bool '1' AS true;
  31. SELECT bool '11' AS error;
  32. SELECT bool '0' AS false;
  33. SELECT bool '000' AS error;
  34. SELECT bool '' AS error;
  35. -- and, or, not in qualifications
  36. SELECT bool 't' or bool 'f' AS true;
  37. SELECT bool 't' and bool 'f' AS false;
  38. SELECT not bool 'f' AS true;
  39. SELECT bool 't' = bool 'f' AS false;
  40. SELECT bool 't' <> bool 'f' AS true;
  41. SELECT bool 't' > bool 'f' AS true;
  42. SELECT bool 't' >= bool 'f' AS true;
  43. SELECT bool 'f' < bool 't' AS true;
  44. SELECT bool 'f' <= bool 't' AS true;
  45. -- explicit casts to/from text
  46. SELECT 'TrUe'::text::boolean AS true, 'fAlse'::text::boolean AS false;
  47. SELECT ' true '::text::boolean AS true,
  48. ' FALSE'::text::boolean AS false;
  49. SELECT true::boolean::text AS true, false::boolean::text AS false;
  50. SELECT ' tru e '::text::boolean AS invalid; -- error
  51. SELECT ''::text::boolean AS invalid; -- error
  52. CREATE TABLE BOOLTBL1 (f1 bool);
  53. INSERT INTO BOOLTBL1 (f1) VALUES (bool 't');
  54. INSERT INTO BOOLTBL1 (f1) VALUES (bool 'True');
  55. INSERT INTO BOOLTBL1 (f1) VALUES (bool 'true');
  56. -- BOOLTBL1 should be full of true's at this point
  57. SELECT BOOLTBL1.* FROM BOOLTBL1;
  58. SELECT BOOLTBL1.*
  59. FROM BOOLTBL1
  60. WHERE f1 = bool 'true';
  61. SELECT BOOLTBL1.*
  62. FROM BOOLTBL1
  63. WHERE f1 <> bool 'false';
  64. SELECT BOOLTBL1.*
  65. FROM BOOLTBL1
  66. WHERE booleq(bool 'false', f1);
  67. INSERT INTO BOOLTBL1 (f1) VALUES (bool 'f');
  68. SELECT BOOLTBL1.*
  69. FROM BOOLTBL1
  70. WHERE f1 = bool 'false';
  71. CREATE TABLE BOOLTBL2 (f1 bool);
  72. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'f');
  73. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'false');
  74. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'False');
  75. INSERT INTO BOOLTBL2 (f1) VALUES (bool 'FALSE');
  76. -- This is now an invalid expression
  77. -- For pre-v6.3 this evaluated to false - thomas 1997-10-23
  78. INSERT INTO BOOLTBL2 (f1)
  79. VALUES (bool 'XXX');
  80. -- BOOLTBL2 should be full of false's at this point
  81. SELECT BOOLTBL2.* FROM BOOLTBL2;
  82. --
  83. -- SQL syntax
  84. -- Try all combinations to ensure that we get nothing when we expect nothing
  85. -- - thomas 2000-01-04
  86. --
  87. SELECT f1
  88. FROM BOOLTBL1
  89. WHERE f1 IS TRUE;
  90. SELECT f1
  91. FROM BOOLTBL1
  92. WHERE f1 IS NOT FALSE;
  93. SELECT f1
  94. FROM BOOLTBL1
  95. WHERE f1 IS FALSE;
  96. SELECT f1
  97. FROM BOOLTBL1
  98. WHERE f1 IS NOT TRUE;
  99. SELECT f1
  100. FROM BOOLTBL2
  101. WHERE f1 IS TRUE;
  102. SELECT f1
  103. FROM BOOLTBL2
  104. WHERE f1 IS NOT FALSE;
  105. SELECT f1
  106. FROM BOOLTBL2
  107. WHERE f1 IS FALSE;
  108. SELECT f1
  109. FROM BOOLTBL2
  110. WHERE f1 IS NOT TRUE;
  111. --
  112. -- Tests for BooleanTest
  113. --
  114. CREATE TABLE BOOLTBL3 (d text, b bool, o int);
  115. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('true', true, 1);
  116. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('false', false, 2);
  117. INSERT INTO BOOLTBL3 (d, b, o) VALUES ('null', null, 3);
  118. -- Test to make sure short-circuiting and NULL handling is
  119. -- correct. Use a table as source to prevent constant simplification
  120. -- to interfer.
  121. CREATE TABLE booltbl4(isfalse bool, istrue bool, isnul bool);
  122. INSERT INTO booltbl4 VALUES (false, true, null);
  123. \pset null '(null)'
  124. -- AND expression need to return null if there's any nulls and not all
  125. -- of the value are true
  126. SELECT istrue AND isnul AND istrue FROM booltbl4;
  127. SELECT istrue AND istrue AND isnul FROM booltbl4;
  128. SELECT isnul AND istrue AND istrue FROM booltbl4;
  129. SELECT isfalse AND isnul AND istrue FROM booltbl4;
  130. SELECT istrue AND isfalse AND isnul FROM booltbl4;
  131. SELECT isnul AND istrue AND isfalse FROM booltbl4;
  132. -- OR expression need to return null if there's any nulls and none
  133. -- of the value is true
  134. SELECT isfalse OR isnul OR isfalse FROM booltbl4;
  135. SELECT isfalse OR isfalse OR isnul FROM booltbl4;
  136. SELECT isnul OR isfalse OR isfalse FROM booltbl4;
  137. SELECT isfalse OR isnul OR istrue FROM booltbl4;
  138. SELECT istrue OR isfalse OR isnul FROM booltbl4;
  139. SELECT isnul OR istrue OR isfalse FROM booltbl4;
  140. --
  141. -- Clean up
  142. -- Many tables are retained by the regression test, but these do not seem
  143. -- particularly useful so just get rid of them for now.
  144. -- - thomas 1997-11-30
  145. --
  146. DROP TABLE BOOLTBL1;
  147. DROP TABLE BOOLTBL2;
  148. DROP TABLE BOOLTBL3;