View a markdown version of this page

Blob and encoding functions - Amazon Athena

Blob and encoding functions

Use these functions to convert text to and from its UTF-8 bytes, to write bytes as base64, hexadecimal, or binary digits, and to read such text back into a BLOB. The hex and bin functions also write an integer in base 16 or base 2. You can also get the size of a blob in bytes, and repeat a blob, a string, or a list.

bin function

Returns a string of 0 and 1 characters. For an integer, the result is the number in base 2, with no leading zeros. For a VARCHAR value, the result has 8 digits for each byte of the string, in order. If value is NULL, the result is NULL.

A negative BIGINT, HUGEINT, or smaller integer value is shown in two's complement form. The result then has 64 digits for BIGINT and smaller integer types, and 128 digits for HUGEINT. A negative BIGNUM value is shown as a minus sign followed by the binary digits of its absolute value, so bin(CAST(-5 AS BIGNUM)) returns -101.

to_binary is an alias of bin.

Syntax

bin(value)

Arguments

The bin function takes the following argument:

value

The value to convert to binary digits. It can be an integer value, such as INTEGER, BIGINT, UBIGINT, HUGEINT, UHUGEINT, or BIGNUM, or a VARCHAR value. It can also be a DOUBLE or REAL value, which bin rounds to the nearest integer first.

Return type

VARCHAR

Examples

The following SQL query returns the binary digits of the number 42.

SELECT bin(42) AS result

The query returns the following result.

result

101010

The following SQL query uses the to_binary alias to convert a string. The letter A is the byte 01000001, and the letter a is the byte 01100001.

SELECT to_binary('Aa') AS result

The query returns the following result.

result

0100000101100001

The following SQL query compares the number 42 with the string '42'. The string is converted byte by byte, so the results differ.

SELECT bin(42) AS from_number, bin('42') AS from_string

The query returns the following result.

from_number from_string

101010

0011010000110010

The following SQL query converts the negative number -5. The result has 64 digits, so the query shows its length and its last 8 digits.

SELECT length(bin(-5)) AS digits, right(bin(-5), 8) AS last_8_digits

The query returns the following result.

digits last_8_digits

64

11111011

Considerations

A number inside a string is converted as text. bin('42') returns the bits of the characters 4 and 2, not the bits of the number 42. To get the bits of the number, cast the string to BIGINT first.

bin does not accept a DECIMAL value, such as the literal 3.7. Cast the value to an integer type first, for example bin(CAST(3.7 AS BIGINT)), which returns 100. A DOUBLE or REAL value is rounded to the nearest integer and converted as a BIGNUM. A value exactly halfway between two integers is rounded to the even one, so bin(CAST(2.5 AS DOUBLE)) returns 10. A negative value gets a minus sign instead of two's complement digits, so bin(CAST(-5 AS DOUBLE)) returns -101.

To pad the result with leading zeros to a fixed width, use the lpad function, for example lpad(bin(5), 8, '0'), which returns 00000101.

decode function

Returns the bytes of blob read as UTF-8 text, as a VARCHAR value. Use it to turn a BLOB back into text, for example the result of encode or from_base64. If blob is NULL, the result is NULL.

By default, the query fails if blob holds bytes that are not valid UTF-8. With error_option, you can replace each invalid byte with a question mark (?) or leave the invalid bytes out instead. A byte can start a multi-byte UTF-8 character that the bytes after it do not complete. In that case, 'replace' can also turn the next valid character into a question mark. That character is then lost. For example, decode('ab\xC3cd'::BLOB, 'replace') returns ab??d, without the c. 'ignore' keeps that character, so decode('ab\xC3cd'::BLOB, 'ignore') returns abcd.

Syntax

decode(blob [, error_option ])

Arguments

The decode function takes the following arguments:

blob

The bytes to convert, as a BLOB value.

error_option

The option that sets what happens to bytes that are not valid UTF-8, as a VARCHAR value. With 'strict', the default, the query fails. With 'replace', each invalid byte becomes ?. If a byte starts a multi-byte character that the bytes after it do not complete, 'replace' can also turn the next valid character into ?. With 'ignore', the function leaves the invalid bytes out of the result and keeps the valid characters. This argument is optional.

Return type

VARCHAR

Examples

The following SQL query converts a BLOB of two bytes, written in hexadecimal, to text. The two bytes are the UTF-8 codes for the letters h and i.

SELECT decode('\x68\x69'::BLOB) AS result

The query returns the following result.

result

hi

The following SQL query decodes the two UTF-8 bytes of the letter u with a diaeresis, and compares the result with that letter, chr(252).

SELECT decode( '\xC3\xBC'::BLOB ) = chr(252) AS is_u_diaeresis

The query returns the following result.

is_u_diaeresis

true

The following SQL query decodes a BLOB with one byte, \xA0, that is not valid UTF-8. With 'replace', the byte becomes ?. With 'ignore', it is left out.

SELECT decode('ab\xA0cd'::BLOB, 'replace') AS replaced, decode('ab\xA0cd'::BLOB, 'ignore') AS ignored

The query returns the following result.

replaced ignored

ab?cd

abcd

The following SQL query wraps the call in TRY, so the expression returns NULL instead of failing on the byte that is not valid UTF-8.

SELECT TRY(decode('\xA0'::BLOB)) IS NULL AS is_null

The query returns the following result.

is_null

true

Considerations

To get NULL instead of an error for a value that is not valid UTF-8, wrap the call in TRY, as in TRY(decode(blob)).

Differences from Athena engine version 3

In Athena engine version 3, you convert binary data to text with from_utf8, which replaces invalid UTF-8 with the Unicode replacement character (U+FFFD) and does not fail. DuckDB does not have from_utf8. Use decode, which fails on invalid UTF-8 unless you pass 'replace' or 'ignore'. With 'replace', each invalid byte becomes a question mark (?), not U+FFFD. When a multi-byte character is cut short, that option can also turn the next valid character into a question mark.

encode function

Returns the UTF-8 bytes of string as a BLOB. A character outside the ASCII range becomes two or more bytes, so the result can have more bytes than string has characters. If string is NULL, the result is NULL.

Use encode to pass text to a function that takes a BLOB, such as to_base64 or octet_length. To convert the bytes back to text, use the decode function.

Syntax

encode(string)

Arguments

The encode function takes the following argument:

string

The string to convert, as a VARCHAR value.

Return type

BLOB

Examples

The following SQL query converts a string to a BLOB. Each byte is a printable ASCII character, so the result shows the same text.

SELECT encode('abc') AS result

The query returns the following result.

result

abc

The following SQL query converts a string that ends with the letter e with an acute accent, chr(233). That letter becomes two bytes, which the result shows in \x form.

SELECT encode('caf' || chr(233)) AS result

The query returns the following result.

result

caf\xC3\xA9

Considerations

encode accepts only VARCHAR values. To encode a number, cast it first, as in encode(CAST(123 AS VARCHAR)).

Differences from Athena engine version 3

In Athena engine version 3, you convert a string to its UTF-8 bytes with to_utf8, which returns VARBINARY. DuckDB does not have to_utf8. Use encode, which returns BLOB.

from_base64 function

Decodes string, which holds base64-encoded data, and returns the decoded bytes as a BLOB. If string is NULL, the result is NULL.

To turn decoded UTF-8 bytes into text, pass the result to decode, which returns a VARCHAR. To encode a value as base64, use the to_base64 function.

Syntax

from_base64(string)

Arguments

The from_base64 function takes the following argument:

string

The base64-encoded data to decode, as a VARCHAR value.

Return type

BLOB

Examples

The following SQL query decodes the base64 string QQ== into a BLOB that holds one byte, which the result shows as the letter A.

SELECT from_base64('QQ==') AS result

The query returns the following result.

result

A

The following SQL query decodes a base64 string and converts the bytes to VARCHAR with decode.

SELECT decode(from_base64('aGVsbG8=')) AS result

The query returns the following result.

result

hello

Considerations

If string is not valid base64, for example if its length is not a multiple of 4 or it holds a character outside the base64 alphabet, the query fails.

hex function

Returns value as a string of uppercase hexadecimal digits. For an integer, the result is the number in base 16, without leading zeros, so hex(42) returns 2A. For a VARCHAR value, the result has two digits for each byte of the string's UTF-8 encoding. For a BLOB value, the result has two digits for each byte. If value is NULL, the result is NULL.

to_hex is an alias of hex. To convert hexadecimal digits back to bytes, use the unhex function. For lowercase digits, pass the result to lower.

Syntax

hex(value)

Arguments

The hex function takes the following argument:

value

The value to convert to hexadecimal digits. It can be an integer value, such as INTEGER, BIGINT, UBIGINT, HUGEINT, UHUGEINT, or BIGNUM, or a VARCHAR or BLOB value. It can also be a DOUBLE or FLOAT value, which hex rounds to the nearest integer first.

Return type

VARCHAR

Examples

The following SQL query converts the integer 42 to hexadecimal.

SELECT hex(42) AS result

The query returns the following result.

result

2A

The following SQL query uses the to_hex alias on a string, and converts the same text as a BLOB. Both return the hexadecimal digits of the bytes H, e, l, l, o.

SELECT to_hex('Hello') AS from_string, hex(CAST('Hello' AS BLOB)) AS from_blob

The query returns the following result.

from_string from_blob

48656C6C6F

48656C6C6F

The following SQL query converts a negative integer. The result is the integer's 64-bit two's complement form.

SELECT hex(-1) AS result

The query returns the following result.

result

FFFFFFFFFFFFFFFF

The following SQL query passes a DOUBLE value. The value is rounded to the integer 4 before conversion.

SELECT hex(CAST(3.7 AS DOUBLE)) AS result

The query returns the following result.

result

4

Considerations

A negative BIGINT value, or a negative value of a smaller integer type such as INTEGER, is shown as the 16 digits of its 64-bit two's complement form. For example, hex(-1) returns FFFFFFFFFFFFFFFF. A negative HUGEINT value is shown as the 32 digits of its 128-bit two's complement form, and a negative BIGNUM value is shown with a minus sign.

A DOUBLE or FLOAT value is rounded to the nearest integer first, so hex does not show the bits of a floating-point number. A negative DOUBLE or FLOAT value is shown with a minus sign, not in two's complement form, so hex(CAST(-1.0 AS DOUBLE)) returns -1. A DECIMAL value, such as the literal 3.7, makes the query fail. Cast it to an integer type first.

Differences from Athena engine version 3

In Athena engine version 3, to_hex takes a VARBINARY value, so a query converts a string first, as in to_hex(to_utf8(string)). DuckDB does not have to_utf8. Pass the string to to_hex directly, as in to_hex(string), which returns the same uppercase digits for the string's UTF-8 bytes.

octet_length function

Returns the size of value in bytes. For a BLOB, the size is the number of bytes that it holds. For a BIT value, it is the number of bytes that hold its bits: the number of bits divided by 8, rounded up. A bitstring of 7 bits returns 1, and one of 9 bits returns 2.

If value is NULL, the result is NULL.

Syntax

octet_length(value)

Arguments

The octet_length function takes the following argument:

value

The value to measure, as a BLOB or BIT value.

Return type

BIGINT

Examples

The following SQL query returns the number of bytes in a BLOB.

SELECT octet_length('\xAA\xBB'::BLOB) AS result

The query returns the following result.

result

2

The following SQL query returns the number of bytes that a 7-bit bitstring takes. It writes the type as BITSTRING, which is another name for BIT.

SELECT octet_length('1101011'::BITSTRING) AS result

The query returns the following result.

result

1

The following SQL query compares the bytes and the characters of a string that ends with the letter e with an acute accent, chr(233). That letter is two bytes in UTF-8.

SELECT octet_length(encode('caf' || chr(233))) AS bytes, length('caf' || chr(233)) AS characters

The query returns the following result.

bytes characters

5

4

Considerations

octet_length does not accept VARCHAR values, and a string literal or a NULL literal with no type fails because it could be either a BLOB or a BIT value. To count the bytes of a string, pass its UTF-8 bytes, as in octet_length(encode(string)). To count its characters, use the length function.

length does not accept a BLOB value. Use octet_length instead.

Differences from Athena engine version 3

In Athena engine version 3, length returns the number of bytes in a VARBINARY value. In DuckDB, length does not accept a BLOB value, so use octet_length instead.

repeat function

Returns value repeated count times. The scalar form joins the copies into one value of the same type: one string for a VARCHAR, one blob for a BLOB, and one longer list for a list. If count is 0 or negative, the result is empty: an empty string, an empty blob, or an empty list. If any argument is NULL, the result is NULL.

The table form, which you use in the FROM clause, returns count rows, each with value in its one column.

Syntax

repeat(value, count) FROM repeat(value, count) [ AS table_alias(column_alias) ]

Arguments

The repeat function takes the following arguments:

value

The value to repeat. In the scalar form, it is a VARCHAR, BLOB, or list value. In the table form, it is a constant of any type to return in each row.

count

The number of repeats in the scalar form, or the number of rows in the table form, as a BIGINT.

Return type

In the scalar form, the type of value: VARCHAR, BLOB, or a list of the same element type, such as INTEGER[]. In the table form, a table with one column of the type of value.

Examples

The following SQL query repeats a string five times.

SELECT repeat('A', 5) AS result

The query returns the following result.

result

AAAAA

The following SQL query repeats a BLOB of two bytes five times.

SELECT repeat('\xAA\xBB'::BLOB, 5) AS result

The query returns the following result.

result

\xAA\xBB\xAA\xBB\xAA\xBB\xAA\xBB\xAA\xBB

The following SQL query repeats the elements of a list three times.

SELECT repeat([1, 2], 3) AS result

The query returns the following result.

result

[1, 2, 1, 2, 1, 2]

The following SQL query uses the table form to return a string in three rows, and numbers the rows.

SELECT row_number() OVER () AS n, value FROM repeat('hello', 3) AS r(value) ORDER BY n

The query returns the following results.

n value

1

hello

2

hello

3

hello

Considerations

The scalar form does not accept numbers. To repeat a number as text, cast it to VARCHAR first. To build a list that holds one value count times, wrap the value in a list, as in repeat([value], count).

In the table form, value and count must be constants, so you cannot pass a column from another table in the same FROM clause. A negative or NULL count fails the query.

In the table form, the column is named after the value, for example hello for repeat('hello', 3). To give the column a name of your own, use a table alias with a column list, as in repeat('hello', 3) AS r(value).

Differences from Athena engine version 3

In Athena engine version 3, repeat(element, count) returns an array that holds element count times. In DuckDB, repeat of a string returns one string, the input repeated count times. To build a list that holds a value count times, wrap the value in a list, as in repeat([element], count).

to_base64 function

Returns the base64 encoding of blob as a VARCHAR value. To encode text, convert it to a BLOB first with encode, which uses the UTF-8 bytes of the text. To decode a base64 string, use the from_base64 function.

base64 is an alias of to_base64.

Syntax

to_base64(blob)

Arguments

The to_base64 function takes the following argument:

blob

The bytes to encode, as a BLOB value.

Return type

VARCHAR

Examples

The following SQL query encodes the UTF-8 bytes of the text hello.

SELECT to_base64(encode('hello')) AS result

The query returns the following result.

result

aGVsbG8=

The following SQL query uses the base64 alias to encode a BLOB that holds one byte, the ASCII code for the letter A.

SELECT base64('A'::BLOB) AS result

The query returns the following result.

result

QQ==

Considerations

to_base64 accepts only BLOB values, and a VARCHAR column does not convert to BLOB automatically. To encode a text column, wrap it in encode, as in to_base64(encode(column)).

unbin function

Returns the bytes that value spells out in binary digits, with eight digits for each byte. For example, 01000001 is the byte for the letter A. When the number of digits is not a multiple of eight, the first byte takes the leftover digits, padded on the left with zeros.

An empty string returns an empty BLOB, not NULL. If value is NULL, the result is NULL. from_binary is an alias of unbin.

Syntax

unbin(value)

Arguments

The unbin function takes the following argument:

value

The binary digits to decode, as a VARCHAR value that contains only the digits 0 and 1.

Return type

BLOB

Examples

The following SQL query converts 16 binary digits to two bytes. The bytes are the ASCII codes for A and B, so the result is shown as AB.

SELECT unbin('0100000101000010') AS result

The query returns the following result.

result

AB

The following SQL query uses the from_binary alias. The result is one byte with the value 6. That byte is not a printable character, so it is shown as an escape sequence.

SELECT from_binary('0110') AS result

The query returns the following result.

result

\x06

The following SQL query passes nine digits and uses octet_length to count the bytes in the result. The first byte takes the leftover digit, so the result has two bytes.

SELECT octet_length(unbin('100000001')) AS result

The query returns the following result.

result

2

Considerations

Any character other than 0 or 1 makes the query fail.

unhex function

Returns the bytes that value spells out in hexadecimal digits, with two digits for each byte. The digits can be uppercase or lowercase. When the number of digits is odd, the first byte takes the single leftover digit.

An empty string returns an empty BLOB, not NULL. If value is NULL, the result is NULL. To turn the bytes into text, use the decode function. To convert a value to hexadecimal digits, use the hex function. from_hex is an alias of unhex.

Syntax

unhex(value)

Arguments

The unhex function takes the following argument:

value

The hexadecimal digits to decode, as a VARCHAR value. It can contain only hexadecimal digits: 0 to 9, and A to F in either case.

Return type

BLOB

Examples

The following SQL query converts the hexadecimal digits 2A to one byte. That byte is the ASCII code for an asterisk, so the result is shown as *.

SELECT unhex('2A') AS result

The query returns the following result.

result

*

The following SQL query uses the from_hex alias with lowercase digits, and converts the resulting bytes to text with decode.

SELECT decode(from_hex('48656c6c6f')) AS result

The query returns the following result.

result

Hello

Considerations

Any character that is not a hexadecimal digit makes the query fail, including the x of a 0x prefix. To convert a value that has the prefix, remove it first, for example with unhex(substring(value, 3)).