bytea.slt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. # Tests for the bytea type.
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE test (ord int, b bytea)
  13. statement ok
  14. INSERT INTO test VALUES (0, 'hello'), (1, '你好'), (2, NULL), (3, ''), (4, 'nonprintablechar:')
  15. query II rowsort
  16. SELECT ord, length(b) FROM test
  17. ----
  18. 0 5
  19. 1 6
  20. 2 NULL
  21. 3 0
  22. 4 18
  23. query II rowsort
  24. SELECT ord, length(b, 'utf-8') FROM test
  25. ----
  26. 0 5
  27. 1 2
  28. 2 NULL
  29. 3 0
  30. 4 18
  31. query I
  32. SELECT length('\xDEADBEEF'::bytea)
  33. ----
  34. 4
  35. query I
  36. SELECT octet_length('\xDEADBEEF'::bytea)
  37. ----
  38. 4
  39. query I
  40. SELECT bit_length('\xDEADBEEF'::bytea)
  41. ----
  42. 32
  43. query I
  44. SELECT length('DEADBEEF'::bytea)
  45. ----
  46. 8
  47. query I
  48. SELECT octet_length('DEADBEEF'::bytea)
  49. ----
  50. 8
  51. query I
  52. SELECT octet_length('DEADBEEF'::text);
  53. ----
  54. 8
  55. query I
  56. SELECT bit_length('DEADBEEF'::bytea)
  57. ----
  58. 64
  59. query I
  60. SELECT bit_length('DEADBEEF'::text);
  61. ----
  62. 64
  63. query I
  64. SELECT bit_count('\x1234567890'::bytea);
  65. ----
  66. 15
  67. query I
  68. SELECT bit_count('\x00'::bytea);
  69. ----
  70. 0
  71. query I
  72. SELECT bit_count('\x0F'::bytea);
  73. ----
  74. 4
  75. query I
  76. SELECT bit_count('\xFF'::bytea);
  77. ----
  78. 8
  79. query I
  80. SELECT bit_count('\xF0FF'::bytea);
  81. ----
  82. 12
  83. query I
  84. SELECT get_byte('\x1234567890'::bytea, 4);
  85. ----
  86. 144
  87. query I
  88. SELECT get_bit('\x1234567890'::bytea, 30);
  89. ----
  90. 1
  91. query II
  92. SELECT n, get_bit('\x1234567890'::bytea, n) FROM generate_series(0, 39) as n ORDER BY n DESC;
  93. ----
  94. 39 1
  95. 38 0
  96. 37 0
  97. 36 1
  98. 35 0
  99. 34 0
  100. 33 0
  101. 32 0
  102. 31 0
  103. 30 1
  104. 29 1
  105. 28 1
  106. 27 1
  107. 26 0
  108. 25 0
  109. 24 0
  110. 23 0
  111. 22 1
  112. 21 0
  113. 20 1
  114. 19 0
  115. 18 1
  116. 17 1
  117. 16 0
  118. 15 0
  119. 14 0
  120. 13 1
  121. 12 1
  122. 11 0
  123. 10 1
  124. 9 0
  125. 8 0
  126. 7 0
  127. 6 0
  128. 5 0
  129. 4 1
  130. 3 0
  131. 2 0
  132. 1 1
  133. 0 0
  134. query I
  135. SELECT get_bit('\xF00a'::bytea, 13);
  136. ----
  137. 0
  138. query I
  139. SELECT get_bit('\xF00a'::bytea, 5);
  140. ----
  141. 1
  142. query II
  143. SELECT n, get_bit('\xF00a'::bytea, n) FROM generate_series(0, 15) as n ORDER BY n DESC;
  144. ----
  145. 15 0
  146. 14 0
  147. 13 0
  148. 12 0
  149. 11 1
  150. 10 0
  151. 9 1
  152. 8 0
  153. 7 1
  154. 6 1
  155. 5 1
  156. 4 1
  157. 3 0
  158. 2 0
  159. 1 0
  160. 0 0
  161. statement error index 16 out of valid range, 0..15
  162. SELECT get_bit('\xF00a'::bytea, 16);
  163. statement error
  164. SELECT length('deadbeef'::text, 'utf-8')
  165. query IT rowsort
  166. SELECT ord, b::text FROM test
  167. ----
  168. 0 \x68656c6c6f
  169. 1 \xe4bda0e5a5bd
  170. 2 NULL
  171. 3 \x
  172. 4 \x6e6f6e7072696e7461626c65636861723a06
  173. query IT rowsort
  174. SELECT ord, convert_from(b, 'utf-8') FROM test
  175. ----
  176. 0 hello
  177. 1 你好
  178. 2 NULL
  179. 3 (empty)
  180. 4 nonprintablechar:
  181. query error invalid encoding name 'invalid encoding'
  182. SELECT convert_from(b, 'invalid encoding') FROM test
  183. query error invalid utf-8 sequence of 1 bytes
  184. SELECT convert_from('\x00ff', 'utf-8')
  185. # get_byte
  186. statement ok
  187. CREATE TABLE test_value (v bytea);
  188. statement ok
  189. INSERT INTO test_value VALUES ('\x1234567890'::bytea);
  190. query error index -1 out of valid range, 0..4
  191. SELECT get_byte(v, -1) FROM test_value
  192. query error index 5 out of valid range, 0..4
  193. SELECT get_byte(v, 5) FROM test_value
  194. query error index 0 out of valid range, 0..-1
  195. SELECT get_byte('\x'::bytea, 0)
  196. query IIIIII
  197. SELECT
  198. get_byte(v, 0),
  199. get_byte(v, 1),
  200. get_byte(v, 2),
  201. get_byte(v, 3),
  202. get_byte(v, 4),
  203. get_byte(v, NULL)
  204. FROM test_value
  205. ----
  206. 18 52 86 120 144 NULL
  207. query II
  208. SELECT
  209. get_byte(NULL, 0),
  210. get_byte(NULL, 2);
  211. ----
  212. NULL NULL
  213. # constant_time_eq for bytea
  214. statement ok
  215. CREATE TABLE test_eq_bytea (ord integer, data1 bytea, data2 bytea);
  216. statement ok
  217. INSERT INTO test_eq_bytea VALUES
  218. (1, '\x1234567890'::bytea, '\x1234567890'::bytea),
  219. (2, '\x1234567890'::bytea, '\x9999999999'::bytea),
  220. (3, 'GOODBYTE'::bytea, 'GOODBYTE'::bytea);
  221. query B
  222. SELECT constant_time_eq(data1, data2) FROM test_eq_bytea ORDER BY ord
  223. ----
  224. true
  225. false
  226. true
  227. query BBB
  228. SELECT
  229. constant_time_eq('x', NULL),
  230. constant_time_eq(NULL, 'x'),
  231. constant_time_eq(NULL, NULL)
  232. ----
  233. NULL
  234. NULL
  235. NULL