bit.slt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/bit
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. # not supported yet
  20. halt
  21. mode cockroach
  22. query TTT
  23. SELECT B'1000101'::BIT(4)::STRING,
  24. B'1000101'::BIT(4),
  25. B'1000101'
  26. ----
  27. 1000 1000 1000101
  28. statement ok
  29. CREATE TABLE bits(a BIT, b BIT(4), c VARBIT, d VARBIT(4))
  30. query TT colnames
  31. SHOW CREATE TABLE bits
  32. ----
  33. table_name create_statement
  34. bits CREATE TABLE bits (
  35. a BIT NULL,
  36. b BIT(4) NULL,
  37. c VARBIT NULL,
  38. d VARBIT(4) NULL,
  39. FAMILY "primary" (a, b, c, d, rowid)
  40. )
  41. subtest bit_fixed1
  42. statement ok
  43. INSERT INTO bits(a) VALUES (B'1'), (B'0');
  44. statement error bit string length 0 does not match type BIT
  45. INSERT INTO bits(a) VALUES (B'')
  46. statement error bit string length 4 does not match type BIT
  47. INSERT INTO bits(a) VALUES (B'1110')
  48. subtest bit_fixed4
  49. statement ok
  50. INSERT INTO bits(b) VALUES (B'0000'), (B'1001');
  51. statement error bit string length 0 does not match type BIT\(4\)
  52. INSERT INTO bits(b) VALUES (B'')
  53. statement error bit string length 3 does not match type BIT\(4\)
  54. INSERT INTO bits(b) VALUES (B'111')
  55. statement error bit string length 9 does not match type BIT\(4\)
  56. INSERT INTO bits(b) VALUES (B'111000111')
  57. subtest bit_varying_unlimited
  58. statement ok
  59. INSERT INTO bits(c) VALUES (B'1'), (B'0'), (B''), (B'1110'),
  60. (B'0101010101010101001101010101010101010101010101010101010101010101010010101') -- more than 64 bits
  61. subtest bit_varying_limited
  62. statement ok
  63. INSERT INTO bits(d) VALUES (B'1'), (B'0'), (B''), (B'1110')
  64. statement error bit string length 73 too large for type VARBIT\(4\)
  65. INSERT INTO bits(d) VALUES
  66. (B'0101010101010101001101010101010101010101010101010101010101010101010010101') -- more than 64 bits
  67. subtest results
  68. query TITITITI colnames
  69. SELECT a, length(a::STRING) an,
  70. b, length(b::STRING) bn,
  71. c, length(c::STRING) cn,
  72. d, length(d::STRING) dn
  73. FROM bits
  74. ORDER BY 1,2,3,4,5,6,7,8
  75. ----
  76. a an b bn c cn d dn
  77. NULL NULL NULL NULL NULL NULL · 0
  78. NULL NULL NULL NULL NULL NULL 0 1
  79. NULL NULL NULL NULL NULL NULL 1 1
  80. NULL NULL NULL NULL NULL NULL 1110 4
  81. NULL NULL NULL NULL · 0 NULL NULL
  82. NULL NULL NULL NULL 0 1 NULL NULL
  83. NULL NULL NULL NULL 0101010101010101001101010101010101010101010101010101010101010101010010101 73 NULL NULL
  84. NULL NULL NULL NULL 1 1 NULL NULL
  85. NULL NULL NULL NULL 1110 4 NULL NULL
  86. NULL NULL 0000 4 NULL NULL NULL NULL
  87. NULL NULL 1001 4 NULL NULL NULL NULL
  88. 0 1 NULL NULL NULL NULL NULL NULL
  89. 1 1 NULL NULL NULL NULL NULL NULL
  90. subtest bit_arith
  91. statement ok
  92. INSERT INTO bits(b) VALUES (B'0110'), (B'0011')
  93. statement ok
  94. INSERT INTO bits(c) VALUES (B'1010'), (B'11')
  95. # Shifts always truncate/pad to the bit array size.
  96. query TTTTTTT colnames
  97. SELECT x.b,
  98. x.b << 0 AS l0,
  99. x.b >> 0 AS r0,
  100. x.b << -1 AS lm1,
  101. x.b >> 1 AS r1,
  102. x.b >> -1 AS rm11,
  103. x.b << 1 AS l1
  104. FROM bits x
  105. WHERE x.b IS NOT NULL
  106. ORDER BY 1,2,3,4,5,6,7
  107. ----
  108. b l0 r0 lm1 r1 rm11 l1
  109. 0000 0000 0000 0000 0000 0000 0000
  110. 0011 0011 0011 0001 0001 0110 0110
  111. 0110 0110 0110 0011 0011 1100 1100
  112. 1001 1001 1001 0100 0100 0010 0010
  113. # Concat works on mixed bit arrays.
  114. query TTTT rowsort
  115. SELECT x.b, y.c, x.b || y.c, y.c || x.b FROM bits x, bits y WHERE x.b IS NOT NULL AND length(y.c::string) < 5
  116. ----
  117. 0000 1 00001 10000
  118. 0000 0 00000 00000
  119. 0000 · 0000 0000
  120. 0000 1110 00001110 11100000
  121. 0000 1010 00001010 10100000
  122. 0000 11 000011 110000
  123. 1001 1 10011 11001
  124. 1001 0 10010 01001
  125. 1001 · 1001 1001
  126. 1001 1110 10011110 11101001
  127. 1001 1010 10011010 10101001
  128. 1001 11 100111 111001
  129. 0110 1 01101 10110
  130. 0110 0 01100 00110
  131. 0110 · 0110 0110
  132. 0110 1110 01101110 11100110
  133. 0110 1010 01101010 10100110
  134. 0110 11 011011 110110
  135. 0011 1 00111 10011
  136. 0011 0 00110 00011
  137. 0011 · 0011 0011
  138. 0011 1110 00111110 11100011
  139. 0011 1010 00111010 10100011
  140. 0011 11 001111 110011
  141. query TT rowsort
  142. SELECT x.b, ~x.b AS comp FROM bits x WHERE b IS NOT NULL
  143. ----
  144. 0000 1111
  145. 1001 0110
  146. 0110 1001
  147. 0011 1100
  148. statement ok
  149. DELETE FROM bits; INSERT INTO bits(c) VALUES (B'0'), (B'1')
  150. query TT rowsort
  151. SELECT x.c, ~x.c AS comp FROM bits x
  152. ----
  153. 0 1
  154. 1 0
  155. query TTTTT rowsort
  156. SELECT x.c AS v1, y.c AS v2,
  157. x.c & y.c AS "and",
  158. x.c | y.c AS "or",
  159. x.c # y.c AS "xor"
  160. FROM bits x, bits y
  161. ----
  162. 0 0 0 0 0
  163. 0 1 0 1 1
  164. 1 0 0 1 1
  165. 1 1 1 1 0
  166. subtest bit_ordering
  167. statement ok
  168. CREATE TABLE obits(x VARBIT);
  169. INSERT INTO obits(x) VALUES
  170. (B'0'),
  171. (B'1'),
  172. (B'0000'),
  173. (B'0001'),
  174. (B'010'),
  175. (B'10'),
  176. (B'11'),
  177. (B''),
  178. (B'00100'),
  179. (B'00110'),
  180. (B'00001'),
  181. (B'1001001010101'),
  182. (B'01001001010101'),
  183. (B'11001001010101')
  184. # Check unindexed ordering.
  185. query T
  186. SELECT * FROM obits ORDER BY x
  187. ----
  188. ·
  189. 0
  190. 0000
  191. 00001
  192. 0001
  193. 00100
  194. 00110
  195. 010
  196. 01001001010101
  197. 1
  198. 10
  199. 1001001010101
  200. 11
  201. 11001001010101
  202. # Check indexed ordering.
  203. statement ok
  204. CREATE INDEX obits_idx ON obits(x)
  205. query T
  206. SELECT * FROM obits@obits_idx ORDER BY x
  207. ----
  208. ·
  209. 0
  210. 0000
  211. 00001
  212. 0001
  213. 00100
  214. 00110
  215. 010
  216. 01001001010101
  217. 1
  218. 10
  219. 1001001010101
  220. 11
  221. 11001001010101
  222. subtest bit_arrays
  223. query TT colnames
  224. SELECT ARRAY[B'101011'] AS a, '{111001}'::VARBIT[] AS b
  225. ----
  226. a b
  227. {101011} {111001}
  228. statement ok
  229. CREATE TABLE obitsa(x VARBIT(20)[]);
  230. INSERT INTO obitsa(x) VALUES
  231. (ARRAY[B'01', B'']),
  232. (ARRAY[B'01', B'0']),
  233. (ARRAY[B'01', B'1']),
  234. (ARRAY[B'01', B'0000']),
  235. (ARRAY[B'01', B'0001']),
  236. (ARRAY[B'01', B'010']),
  237. (ARRAY[B'01', B'10']),
  238. (ARRAY[B'01', B'11']),
  239. (ARRAY[B'01', B'']),
  240. (ARRAY[B'01', B'00100']),
  241. (ARRAY[B'01', B'00110']),
  242. (ARRAY[B'01', B'00001']),
  243. (ARRAY[B'01', B'1001001010101']),
  244. (ARRAY[B'01', B'01001001010101']),
  245. (ARRAY[B'01', B'11001001010101'])
  246. query T
  247. SELECT create_statement FROM [SHOW CREATE obitsa]
  248. ----
  249. CREATE TABLE obitsa (
  250. x VARBIT(20)[] NULL,
  251. FAMILY "primary" (x, rowid)
  252. )
  253. # Check unindexed ordering.
  254. query T rowsort
  255. SELECT * FROM obitsa
  256. ----
  257. {01,""}
  258. {01,0}
  259. {01,1}
  260. {01,0000}
  261. {01,0001}
  262. {01,010}
  263. {01,10}
  264. {01,11}
  265. {01,""}
  266. {01,00100}
  267. {01,00110}
  268. {01,00001}
  269. {01,1001001010101}
  270. {01,01001001010101}
  271. {01,11001001010101}