Varbinary functions (DuneSQL)
Varbinary Functions
Dune SQL represents varbinarys using the varbinary type.
To make it simpler to work with varbinarys we have the following helper functions, which work with these two kinds of representation. They simplify interactions with varbinarys, as they automatically account for the 0x
-prefix and use byte index instead of character index. For instance, the varbinary_substring methods take indexes by byte, not by character (twice the varbinary length).
You can use these function to extract data from undecoded events logs, or calldata. For instance, if you have a function that takes a uint256
as an argument, you can use varbinary_to_uint256
to extract the value from the calldata. You’ll oftentimes need to use varbinary_substring
to extract the correct part of the calldata or use other bytearray manipulation functions to get the correct value.
Varbinary to Numeric Functions
The varbinary conversion functions throw an overflow exception if the varbinary is larger than the number of bytes supported of the type, even if the most significant bytes are all zero. It is possible to use varbinary_ltrim
in order to trim the zero bytes from the left.
Here is a dashboard with examples covering all of the below functions. For a more comprehensive look at all special DuneSQL functions, use this dashboard from cryptuschrist.
varbinary_to_integer()
varbinary_to_integer(varbinary)
→ integer
Returns the INTEGER
value of a big-endian varbinary of length <= 4 representing the integer in two’s complement. If the varbinary has length < 4 it is padded with zero bytes.
varbinary_to_bigint()
varbinary_to_bigint(varbinary)
→ bigint
Returns the BIGINT
value of a big-endian varbinary of length <= 8 representing the bigint in two’s complement. If the varbinary has length < 8 it is padded with zero bytes.
varbinary_to_decimal()
varbinary_to_decimal(varbinary)
→ decimal(38,0)
Returns the DECIMAL(38,0)
value of a big-endian varbinary of length <= 16 representing the decimal(38,0) in two’s complement. If the varbinary has length < 16 it is padded with zero bytes.
varbinary_to_uint256()
varbinary_to_uint256(varbinary)
→ uint256
Returns the UINT256
of a big-endian varbinary of length <= 32 representing the unsigned integer. If the varbinary has length < 32 it is padded with zero bytes.
varbinary_to_int256()
varbinary_to_int256(varbinary)
→ int256
Returns the INT256
of a big-endian varbinary of length <= 32 representing the signed integer. If the varbinary has length < 32 it is padded with zero bytes.
bytea2numeric()
bytea2numeric(varbinary)
→ bigint
This function has been deprecated. It is an alias for varbinary_to_bigint
.
Varbinary to text
from_utf8()
from_utf8(varbinary)
→ varchar
Converts a varbinary to a string using the UTF-8 encoding.
most commonly will have to be used like this:
Text to varbinary
from_hex()
from_hex(varchar)
→ varbinary
Converts a varbinary expression in datatype string
to varbinary
datatype
cast(x as varbinary)
in these cases as it will actually encode the string as varbinary which is different from converting the expression to varbinary
. Varbinary Manipulation Functions
varbinary_concat()
varbinary_concat(varbinary, varbinary)
→ varbinary
Concatenates two varbinarys or strings.
varbinary_concat(varchar, varchar)
→ varchar
Concatenates two varbinarys or strings.
varbinary_length()
varbinary_length(varbinary)
→ bigint
Returns the length of a varbinary.
varbinary_length(varchar)
→ bigint
Returns the length of a string.
varbinary_ltrim()
varbinary_ltrim(varbinary)
→ varbinary
Removes zero bytes or spaces from the beginning of a varbinary
varbinary_ltrim(varchar)
→ varchar
Removes spaces from the beginning of a string.
varbinary_rtrim()
varbinary_rtrim(varbinary) or varbinary_rtrim(varchar)
→ varbinary or varchar
Removes zero bytes or spaces from the end of a varbinary or string.
varbinary_position()
varbinary_position(varbinary, varbinary)
→ bigint
Returns the index of the first occurrence of a given bytearray or string (or 0 if not found) within a varbinary or string.
varbinary_position(varchar, varchar)
→ bigint
Returns the index of the first occurrence of a given bytearray or string (or 0 if not found) within a varbinary or string.
varbinary_replace()
varbinary_replace(varbinary, varbinary, varbinary)
→ varbinary
Greedily replaces occurrences of a pattern within a varbinary.
varbinary_replace(varchar, varchar, varchar)
→ varchar
Greedily replaces occurrences of a pattern within a string.
varbinary_reverse()
varbinary_reverse(varbinary)
→ varbinary
Reverses a given varbinary.
varbinary_reverse(varchar)
→ varchar
Reverses a given string.
varbinary_starts_with()
varbinary_starts_with(varbinary, varbinary)
→ boolean
Determines whether a varbinary starts with a prefix.
varbinary_starts_with(varchar, varchar)
→ boolean
Determines whether a string starts with a prefix.
varbinary_substring()
varbinary_substring(varbinary, integer)
→ varbinary
Returns a suffix varbinary or string starting at a given index.
varbinary_substring(varchar, integer)
→ varchar
Returns a suffix string starting at a given index.
Returns a sub varbinary or string of a given length starting at an index.
varbinary_substring(varchar, integer, integer)
→ varchar
Returns a sub string of a given length starting at an index.
varbinary_substring(varbinary, integer, integer)
→ varbinary
Was this page helpful?