encode.slt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 `encode` and `decode` functions.
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE unencoded (val bytea)
  13. statement ok
  14. INSERT INTO unencoded VALUES (NULL), ('\x00fffe65'), ('a'), ('ab'), ('abc'), ('abcd')
  15. # ==> base64 format
  16. query TT
  17. SELECT encode(val, 'base64'), decode(encode(val, 'base64'), 'base64') FROM unencoded ORDER BY val
  18. ----
  19. AP/+ZQ== [0,␠255,␠254,␠101]
  20. YQ== a
  21. YWI= ab
  22. YWJj abc
  23. YWJjZA== abcd
  24. NULL NULL
  25. # base64 special case: test that the encoded output is wrapped at 76 characters.
  26. mode standard
  27. query T multiline
  28. SELECT encode('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', 'base64')
  29. ----
  30. YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5emFiY2Rl
  31. ZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlq
  32. a2xtbm9wcXJzdHV2d3h5emFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6
  33. EOF
  34. mode cockroach
  35. query error invalid base64 end sequence
  36. SELECT decode('a', 'base64')
  37. query error unexpected "=" while decoding base64 sequence
  38. SELECT decode('=', 'base64')
  39. query error invalid symbol "@" found while decoding base64 sequence
  40. SELECT decode('aaa@', 'base64')
  41. query error invalid symbol "\\u\{2\}" found while decoding base64 sequence
  42. SELECT decode(e'aaa\u0002', 'base64')
  43. # ==> hex format
  44. query TT
  45. SELECT encode(val, 'hex'), decode(encode(val, 'hex'), 'hex') FROM unencoded ORDER BY val
  46. ----
  47. 00fffe65 [0,␠255,␠254,␠101]
  48. 61 a
  49. 6162 ab
  50. 616263 abc
  51. 61626364 abcd
  52. NULL NULL
  53. # hex special case: encoded bytes can be separated by whitespace.
  54. query T
  55. SELECT decode(E'41 42\t43', 'hex')
  56. ----
  57. ABC
  58. # Though individual digits within a byte cannot.
  59. query error invalid hexadecimal digit: " "
  60. SELECT decode('a a', 'hex')
  61. query error invalid hexadecimal digit: "x"
  62. SELECT decode('xx', 'hex')
  63. query error invalid hexadecimal data: odd number of digits
  64. SELECT decode('0', 'hex')
  65. # ==> escape format
  66. query TT
  67. SELECT encode(val, 'escape'), decode(encode(val, 'escape'), 'escape') FROM unencoded ORDER BY val
  68. ----
  69. \000\377\376e [0,␠255,␠254,␠101]
  70. a a
  71. ab ab
  72. abc abc
  73. abcd abcd
  74. NULL NULL
  75. query error invalid input syntax for type bytea
  76. SELECT decode('\9', 'escape')
  77. # checks https://github.com/MaterializeInc/database-issues/issues/3311
  78. query T
  79. SELECT encode('se', 'base64')
  80. ----
  81. c2U=
  82. query T
  83. SELECT decode(encode('se', 'base64'), 'base64')
  84. ----
  85. se