boolean.slt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. mode cockroach
  10. statement ok
  11. CREATE TABLE bools (b bool)
  12. statement ok
  13. INSERT INTO bools VALUES (TRUE), (FALSE), (NULL);
  14. statement ok
  15. CREATE TABLE bools_text (b text)
  16. statement ok
  17. INSERT INTO bools_text VALUES
  18. ('t'), ('tr'), ('tR'), ('tRuE'), ('TRUE'), (' tr '),
  19. ('f'), ('fa'), ('faL'), ('fAlsE'), ('FALSE'), (' fal '),
  20. ('on'), ('off'), ('On'), ('Off'), ('ON'), ('oFf'),
  21. ('1'), ('0'), (' 1'), ('0 ')
  22. query TB rowsort
  23. SELECT replace(b, ' ', 'x'), b::bool FROM bools_text
  24. ----
  25. xxxxxxxxxxxxxxx1 true
  26. xxxxxxxxxxxfalx false
  27. xxxxtrxxxx true
  28. 0xxxxxxxx false
  29. 0 false
  30. 1 true
  31. f false
  32. fa false
  33. faL false
  34. fAlsE false
  35. FALSE false
  36. off false
  37. oFf false
  38. Off false
  39. on true
  40. On true
  41. ON true
  42. t true
  43. tr true
  44. tR true
  45. tRuE true
  46. TRUE true
  47. query error invalid input syntax for type boolean: "blah"
  48. SELECT 'blah'::bool
  49. query error NOT argument must have type boolean, not type integer
  50. SELECT NOT 1
  51. query error AND argument must have type boolean, not type integer
  52. SELECT 1 AND 1
  53. query error OR argument must have type boolean, not type integer
  54. SELECT 1 OR 1
  55. query error OR argument must have type boolean, not type integer
  56. SELECT 1 OR FALSE
  57. query error OR argument must have type boolean, not type integer
  58. SELECT FALSE OR 1
  59. query error AND argument must have type boolean, not type integer
  60. SELECT 1 AND FALSE
  61. query error AND argument must have type boolean, not type integer
  62. SELECT FALSE AND 1
  63. query B colnames
  64. SELECT TRUE
  65. ----
  66. bool
  67. true
  68. query B colnames
  69. SELECT FALSE
  70. ----
  71. bool
  72. false
  73. query B
  74. SELECT NOT TRUE
  75. ----
  76. false
  77. query B
  78. SELECT NOT FALSE
  79. ----
  80. true
  81. query BBB rowsort
  82. SELECT a.b, b.b, a.b AND b.b FROM bools AS a CROSS JOIN bools AS b;
  83. ----
  84. true true true
  85. true false false
  86. true NULL NULL
  87. false true false
  88. false false false
  89. false NULL false
  90. NULL true NULL
  91. NULL false false
  92. NULL NULL NULL
  93. # Boolean AND error cases
  94. query error division by zero
  95. SELECT (1/0 > 0) AND TRUE;
  96. # Postgres returns the error instead
  97. query B
  98. SELECT (1/0 > 0) AND FALSE;
  99. ----
  100. false
  101. query error division by zero
  102. SELECT (1/0 > 0) AND NULL;
  103. query error division by zero
  104. SELECT TRUE AND (1/0 > 0);
  105. query B
  106. SELECT FALSE AND (1/0 > 0);
  107. ----
  108. false
  109. query error division by zero
  110. SELECT NULL AND (1/0 > 0);
  111. # Check we consistently pick the same error
  112. # Postgres returns the first error instead
  113. query error "32768" smallint out of range
  114. SELECT (32768::int2 > 0) AND (1/0 > 0);
  115. query error "32768" smallint out of range
  116. SELECT (1/0 > 0) AND (32768::int2 > 0);
  117. query BBB rowsort
  118. SELECT a.b, b.b, a.b OR b.b FROM bools AS a CROSS JOIN bools AS b;
  119. ----
  120. true true true
  121. true false true
  122. true NULL true
  123. false true true
  124. false false false
  125. false NULL NULL
  126. NULL true true
  127. NULL false NULL
  128. NULL NULL NULL
  129. # Boolean OR error cases
  130. # Postgres returns the error instead
  131. query B
  132. SELECT (1/0 > 0) OR TRUE;
  133. ----
  134. true
  135. query error division by zero
  136. SELECT (1/0 > 0) OR FALSE;
  137. query error division by zero
  138. SELECT (1/0 > 0) OR NULL;
  139. query B
  140. SELECT TRUE OR (1/0 > 0);
  141. ----
  142. true
  143. query error division by zero
  144. SELECT FALSE OR (1/0 > 0);
  145. query error division by zero
  146. SELECT NULL OR (1/0 > 0);
  147. # Check we consistently pick the same error
  148. # Postgres returns the first error instead
  149. query error smallint out of range
  150. SELECT (32768::int2 > 0) OR (1/0 > 0);
  151. query error smallint out of range
  152. SELECT (1/0 > 0) OR (32768::int2 > 0);
  153. query B
  154. SELECT TRUE AND NOT TRUE
  155. ----
  156. false
  157. query B
  158. SELECT NOT FALSE OR FALSE
  159. ----
  160. true
  161. ### integer to bool ###
  162. query B
  163. SELECT 99::int::bool
  164. ----
  165. true
  166. query B
  167. SELECT 0::int::bool
  168. ----
  169. false
  170. query B
  171. SELECT NOT 0::int::bool
  172. ----
  173. true
  174. ### int8 to bool ###
  175. query B
  176. SELECT 99::bigint::bool
  177. ----
  178. true
  179. query B
  180. SELECT 0::bigint::bool
  181. ----
  182. false
  183. query B
  184. SELECT NOT 0::bigint::bool
  185. ----
  186. true
  187. ### bool to integer ###
  188. query I
  189. SELECT true::int
  190. ----
  191. 1
  192. query I
  193. SELECT false::int
  194. ----
  195. 0
  196. statement ok
  197. CREATE TABLE x (a int, u int, j jsonb, b bool)
  198. # Ensure the NOT gets pushed into the binary operation.
  199. query T multiline
  200. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  201. NOT(a = u),
  202. NOT(a != u),
  203. NOT(a < u),
  204. NOT(a > u),
  205. NOT(a >= u),
  206. NOT(a <= u),
  207. NOT(NOT(b)),
  208. -- Doesn't have a negation.
  209. NOT(j @> '{}'::JSONB)
  210. FROM x
  211. ----
  212. Explained Query:
  213. Project (#4..=#9, #3{b}, #10) // { arity: 8 }
  214. Map ((#0{a} != #1{u}), (#0{a} = #1{u}), (#0{a} >= #1{u}), (#0{a} <= #1{u}), (#0{a} < #1{u}), (#0{a} > #1{u}), NOT((#2{j} @> {}))) // { arity: 11 }
  215. ReadStorage materialize.public.x // { arity: 4 }
  216. Source materialize.public.x
  217. Target cluster: quickstart
  218. EOF
  219. statement ok
  220. CREATE TABLE y (a boolean, b bool)
  221. # Bypass if statements with identical branches
  222. query T multiline
  223. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  224. CASE WHEN b THEN a ELSE a END
  225. FROM y
  226. ----
  227. Explained Query:
  228. Project (#0{a}) // { arity: 1 }
  229. ReadStorage materialize.public.y // { arity: 2 }
  230. Source materialize.public.y
  231. Target cluster: quickstart
  232. EOF
  233. # Bypass if statements with identical branches
  234. query T multiline
  235. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  236. CASE WHEN b THEN NULL ELSE true END
  237. FROM y
  238. ----
  239. Explained Query:
  240. Project (#2) // { arity: 1 }
  241. Map ((null OR NOT(#1{b}) OR (#1{b}) IS NULL)) // { arity: 3 }
  242. ReadStorage materialize.public.y // { arity: 2 }
  243. Source materialize.public.y
  244. Target cluster: quickstart
  245. EOF
  246. # Bypass if statements with identical branches
  247. query T multiline
  248. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  249. CASE WHEN b THEN NULL ELSE false END
  250. FROM y
  251. ----
  252. Explained Query:
  253. Project (#2) // { arity: 1 }
  254. Map ((#1{b} AND null AND (#1{b}) IS NOT NULL)) // { arity: 3 }
  255. ReadStorage materialize.public.y // { arity: 2 }
  256. Source materialize.public.y
  257. Target cluster: quickstart
  258. EOF
  259. query T multiline
  260. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  261. CASE WHEN b THEN true ELSE NULL END
  262. FROM y
  263. ----
  264. Explained Query:
  265. Project (#2) // { arity: 1 }
  266. Map ((null OR (#1{b} AND (#1{b}) IS NOT NULL))) // { arity: 3 }
  267. ReadStorage materialize.public.y // { arity: 2 }
  268. Source materialize.public.y
  269. Target cluster: quickstart
  270. EOF
  271. query T multiline
  272. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  273. CASE WHEN b THEN false ELSE NULL END
  274. FROM y
  275. ----
  276. Explained Query:
  277. Project (#2) // { arity: 1 }
  278. Map ((null AND (NOT(#1{b}) OR (#1{b}) IS NULL))) // { arity: 3 }
  279. ReadStorage materialize.public.y // { arity: 2 }
  280. Source materialize.public.y
  281. Target cluster: quickstart
  282. EOF
  283. query T multiline
  284. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT
  285. CASE WHEN b THEN false ELSE TRUE END
  286. FROM y
  287. ----
  288. Explained Query:
  289. Project (#2) // { arity: 1 }
  290. Map ((NOT(#1{b}) OR (#1{b}) IS NULL)) // { arity: 3 }
  291. ReadStorage materialize.public.y // { arity: 2 }
  292. Source materialize.public.y
  293. Target cluster: quickstart
  294. EOF
  295. statement ok
  296. CREATE TABLE z (a int, b int)
  297. statement ok
  298. insert into z values (null, null), (1, null), (null, 2), (1, 2), (2, 1)
  299. query T multiline
  300. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT *
  301. FROM z
  302. WHERE CASE WHEN a > b THEN FALSE ELSE TRUE END
  303. ----
  304. Explained Query:
  305. Filter ((#0{a}) IS NULL OR (#1{b}) IS NULL OR (#0{a} <= #1{b})) // { arity: 2 }
  306. ReadStorage materialize.public.z // { arity: 2 }
  307. Source materialize.public.z
  308. filter=(((#0{a}) IS NULL OR (#1{b}) IS NULL OR (#0{a} <= #1{b})))
  309. Target cluster: quickstart
  310. EOF
  311. query II rowsort
  312. SELECT *
  313. FROM z
  314. WHERE CASE WHEN a > b THEN FALSE ELSE TRUE END
  315. ----
  316. NULL NULL
  317. NULL 2
  318. 1 NULL
  319. 1 2
  320. # TODO: Reenable when database-issues#8807 is fixed
  321. # query T
  322. # SELECT CASE WHEN FALSE THEN 'short_string'::char(20) ELSE 'long_string_long_string'::char(30) END
  323. # ----
  324. # long_string_long_string