View a markdown version of this page

Blob and bitstring functions in DuckDB - Amazon Athena

Blob and bitstring functions in DuckDB

Use blob and bitstring functions to work with binary data. A BLOB value holds a sequence of bytes, and a BIT value, which you can also write as BITSTRING, holds a sequence of bits. This category also has the hash functions md5, md5_number, md5_number_lower, md5_number_upper, sha1, and sha256. MD5 and SHA-1 are not secure for cryptographic use, so do not use the MD5 and SHA-1 functions to protect data. When you need a secure hash, use the sha256 function.

The category also has the repeat function, which repeats strings, blobs, and lists. To read files as binary data, use the read_blob table function. For more information about read_blob, see read_blob function.

To write a BLOB literal, cast a string to BLOB, as in '\xAA\xBB'::BLOB. Each \x followed by two hexadecimal digits stands for one byte, and each other ASCII character stands for its own byte. A cast to BLOB fails for a string that holds a character outside the ASCII range, such as chr(233). To get the UTF-8 bytes of such a string, use encode instead. To write a BIT literal, cast a string of 0 and 1 characters, as in '1011'::BIT. To convert text to the bytes of its UTF-8 encoding and back, use the encode function and the decode function. To convert a BLOB to or from base64 text, use the to_base64 function and the from_base64 function. To write a value as hexadecimal or binary digits, use the hex function or the bin function. To turn such digits back into a BLOB, use the unhex function or the unbin function.

Query results show a BLOB value as text. Bytes that are letters, digits, and most other printable ASCII characters appear as those characters, and other bytes appear as \x followed by two hexadecimal digits, for example \xAA. A BIT value appears as a string of 0 and 1 characters.

Hive and Iceberg tables cannot store BIT values. To store a bitstring in a table, cast it to VARCHAR, as in CAST(bits AS VARCHAR).

The following table lists the blob and bitstring functions, with a link to the description of each one.

Function Description

base64

Alias of to_base64.

bin

Returns the binary digits of an integer, or of the bytes of a string, as a string.

bit_count

Counts the 1 bits in an integer or a bitstring.

bit_length

Returns the number of bits in a string or a bitstring.

bit_position

Returns the position of the first occurrence of one bitstring inside another.

bitstring

Returns a bitstring widened to a given number of bits, with 0 bits added on the left.

decode

Reads the bytes of a BLOB as UTF-8 text and returns a string.

encode

Returns the UTF-8 bytes of a string as a BLOB.

from_base64

Decodes a base64-encoded string and returns the bytes as a BLOB.

from_binary

Alias of unbin.

from_hex

Alias of unhex.

get_bit

Returns the bit at a given position in a bitstring.

hash

Returns a 64-bit hash of one or more values of any type. It is not a cryptographic hash. The utility functions topic describes it.

hex

Converts an integer, a string, or a blob to hexadecimal text.

md5

Hashes a string or a blob with MD5 and returns the hash as hexadecimal text.

md5_number

Hashes a string or a blob with MD5 and returns the hash as an unsigned 128-bit integer.

md5_number_lower

Returns the most significant 64 bits of the 128-bit MD5 hash number of a value.

md5_number_upper

Returns the least significant 64 bits of the 128-bit MD5 hash number of a value.

octet_length

Returns the size of a blob or a bitstring in bytes.

repeat

Joins a given number of copies of a string, a blob, or a list into one value, or returns a value in a given number of rows.

set_bit

Returns a copy of a bitstring with the bit at a given position set to 0 or 1.

sha1

Hashes a string or a blob with SHA-1 and returns the hash as hexadecimal text.

sha256

Hashes a string or a blob with SHA-256 and returns the hash as hexadecimal text.

to_base64

Encodes the bytes of a BLOB as a base64 string.

to_binary

Alias of bin.

to_hex

Alias of hex.

unbin

Decodes a string of binary digits and returns the bytes as a BLOB.

unhex

Decodes a string of hexadecimal digits and returns the bytes as a BLOB.