Materialize Documentation
s
Join the Community github/materialize

encode and decode functions

The encode function encodes binary data into one of several textual representations. The decode function does the reverse.

Signatures

encode(b: bytea, format: text) -> text
decode(s: text, format: text) -> bytea

Details

Supported formats

The following formats are supported by both encode and decode.

base64

The base64 format is defined in Section 6.8 of RFC 2045.

To comply with the RFC, the encode function inserts a newline (\n) after every 76 characters. The decode function ignores any whitespace in its input.

escape

The escape format renders zero bytes and bytes with the high bit set (0x80 - 0xff) as an octal escape sequence (\nnn), renders backslashes as \\, and renders all other characters literally. The decode function rejects invalid escape sequences (e.g., \9 or \a).

hex

The hex format represents each byte of input as two hexadecimal digits, with the most significant digit first. The encode function uses lowercase for the a-f digits, with no whitespace between digits. The decode function accepts lowercase or uppercase for the a - f digits and permits whitespace between each encoded byte, though not within a byte.

Examples

Encoding and decoding in the base64 format:

SELECT encode('\x00404142ff', 'base64');
  encode
----------
 AEBBQv8=
SELECT decode('A   EB BQv8 =', 'base64');
    decode
--------------
 \x00404142ff
SELECT encode('This message is long enough that the output will run to multiple lines.', 'base64');
                                    encode
------------------------------------------------------------------------------
 VGhpcyBtZXNzYWdlIGlzIGxvbmcgZW5vdWdoIHRoYXQgdGhlIG91dHB1dCB3aWxsIHJ1biB0byBt+
 dWx0aXBsZSBsaW5lcy4=

Encoding and decoding in the escape format:

SELECT encode('\x00404142ff', 'escape');
   encode
-------------
 \000@AB\377
SELECT decode('\000@AB\377', 'escape');
    decode
--------------
 \x00404142ff

Encoding and decoding in the hex format:

SELECT encode('\x00404142ff', 'hex');
   encode
------------
 00404142ff
SELECT decode('00  40  41  42  ff', 'hex');
    decode
--------------
 \x00404142ff