Encoding functions

char

Returns the string with the length as the number of passed arguments and each byte has the value of corresponding argument. Accepts multiple arguments of numeric types. If the value of argument is out of range of UInt8 data type, it is converted to UInt8 with possible rounding and overflow.

Syntax

char(number_1, [number_2, ..., number_n])

Arguments

  • number_1, number_2, ..., number_n: Numerical arguments interpreted as integers. Types: Int, Float.

Returned value

  • a string of given bytes. String.

Example

Query:

SELECT char(104.1, 101, 108.9, 108.9, 111) AS hello

Result:

┌─hello─┐
│ hello │
└───────┘

You can construct a string of arbitrary encoding by passing the corresponding bytes. Here is example for UTF-8:

Query:

SELECT char(0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, 0xD1, 0x82) AS hello

Result:

┌─hello──┐
│ привет │
└────────┘

Query:

SELECT char(0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD) AS hello

Result:

┌─hello─┐
│ 你好  │
└───────┘

hex

Returns a string containing the argument’s hexadecimal representation.

Alias: HEX.

Syntax

hex(arg)

The function is using uppercase letters A-F and not using any prefixes (like 0x) or suffixes (like h).

For integer arguments, it prints hex digits (“nibbles”) from the most significant to least significant (big-endian or “human-readable” order). It starts with the most significant non-zero byte (leading zero bytes are omitted) but always prints both digits of every byte even if the leading digit is zero.

Values of type Date and DateTime are formatted as corresponding integers (the number of days since Epoch for Date and the value of Unix Timestamp for DateTime).

For String and FixedString, all bytes are simply encoded as two hexadecimal numbers. Zero bytes are not omitted.

Values of Float and Decimal types are encoded as their representation in memory. As we support little-endian architecture, they are encoded in little-endian. Zero leading/trailing bytes are not omitted.

Values of UUID type are encoded as big-endian order string.

Arguments

  • arg: A value to convert to hexadecimal. Types: String, UInt, Float, Decimal, Date or DateTime.

Returned value

  • A string with the hexadecimal representation of the argument. String.

Examples

Query:

SELECT hex(1)

Result:

01

Query:

SELECT hex(toFloat32(number)) AS hex_presentation FROM numbers(15, 2)

Result:

┌─hex_presentation─┐
│ 00007041         │
│ 00008041         │
└──────────────────┘

Query:

SELECT hex(toFloat64(number)) AS hex_presentation FROM numbers(15, 2)

Result:

┌─hex_presentation─┐
│ 0000000000002E40 │
│ 0000000000003040 │
└──────────────────┘

Query:

SELECT lower(hex(toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0'))) as uuid_hex

Result:

┌─uuid_hex─────────────────────────┐
│ 61f0c4045cb311e7907ba6006ad3dba0 │
└──────────────────────────────────┘

unhex

Performs the opposite operation of hex. It interprets each pair of hexadecimal digits (in the argument) as a number and converts it to the byte represented by the number. The return value is a binary string (BLOB).

If you want to convert the result to a number, you can use the reverse and reinterpretAs<Type> functions.

Alias: UNHEX.

Syntax

unhex(arg)

Arguments

  • arg: A string containing any number of hexadecimal digits. String, FixedString.

Supports both uppercase and lowercase letters A-F. The number of hexadecimal digits does not have to be even. If it is odd, the last digit is interpreted as the least significant half of the 00-0F byte. If the argument string contains anything other than hexadecimal digits, some implementation-defined result is returned (an exception isn’t thrown). For a numeric argument the inverse of hex(N) is not performed by unhex().

Returned value

  • A binary string (BLOB). String.

Example

Query:

SELECT unhex('303132'), UNHEX('4D7953514C')

Result:

┌─unhex('303132')─┬─unhex('4D7953514C')─┐
│ 012             │ MySQL               │
└─────────────────┴─────────────────────┘

Query:

SELECT reinterpretAsUInt64(reverse(unhex('FFF'))) AS num

Result:

┌──num─┐
│ 4095 │
└──────┘

bin

Returns a string containing the argument’s binary representation.

Syntax

bin(arg)

Alias: BIN.

For integer arguments, it prints bin digits from the most significant to least significant (big-endian or “human-readable” order). It starts with the most significant non-zero byte (leading zero bytes are omitted) but always prints eight digits of every byte if the leading digit is zero.

Values of type Date and DateTime are formatted as corresponding integers (the number of days since Epoch for Date and the value of Unix Timestamp for DateTime).

For String and FixedString, all bytes are simply encoded as eight binary numbers. Zero bytes are not omitted.

Values of Float and Decimal types are encoded as their representation in memory. As we support little-endian architecture, they are encoded in little-endian. Zero leading/trailing bytes are not omitted.

Values of UUID type are encoded as big-endian order string.

Arguments

  • arg: A value to convert to binary. String, FixedString, UInt, Float, Decimal, Date, or DateTime.

Returned value

  • A string with the binary representation of the argument. String.

Examples

Query:

SELECT bin(14)

Result:

┌─bin(14)──┐
│ 00001110 │
└──────────┘

Query:

SELECT bin(toFloat32(number)) AS bin_presentation FROM numbers(15, 2)

Result:

┌─bin_presentation─────────────────┐
│ 00000000000000000111000001000001 │
│ 00000000000000001000000001000001 │
└──────────────────────────────────┘

Query:

SELECT bin(toFloat64(number)) AS bin_presentation FROM numbers(15, 2)

Result:

┌─bin_presentation─────────────────────────────────────────────────┐
│ 0000000000000000000000000000000000000000000000000010111001000000 │
│ 0000000000000000000000000000000000000000000000000011000001000000 │
└─────────────────────────────────────────��────────────────────────┘

Query:

SELECT bin(toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0')) as bin_uuid

Result:

┌─bin_uuid─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 01100001111100001100010000000100010111001011001100010001111001111001000001111011101001100000000001101010110100111101101110100000 │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

unbin

Interprets each pair of binary digits (in the argument) as a number and converts it to the byte represented by the number. The functions performs the opposite operation to bin.

Syntax

unbin(arg)

Alias: UNBIN.

For a numeric argument unbin() does not return the inverse of bin(). If you want to convert the result to a number, you can use the reverse and reinterpretAs<Type> functions.

Supports binary digits 0 and 1. The number of binary digits does not have to be multiples of eight. If the argument string contains anything other than binary digits, some implementation-defined result is returned (an exception isn’t thrown).

Arguments

  • arg: A string containing any number of binary digits. String, FixedString.

Returned value

  • A binary string (BLOB). String.

Example

Query:

SELECT unbin('303132'), UNBIN('4D7953514C')

Result:

┌─unbin('303132')─┬─unbin('4D7953514C')─┐
│ 012             │ MySQL               │
└─────────────────┴─────────────────────┘

bitAnd

Computes the logical conjunction of two bitmaps.

Syntax

bitmapAnd(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapToArray(bitmapAnd(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

┌─res─┐
│ [3] │
└─────┘

bitmapOr

Computes the logical disjunction of two bitmaps.

Syntax

bitmapOr(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapToArray(bitmapOr(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

┌─res─────────┐
│ [1,2,3,4,5] │
└─────────────┘

bitmapXor

Xor-s two bitmaps.

Syntax

bitmapXor(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapToArray(bitmapXor(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

┌─res───────┐
│ [1,2,4,5] │
└───────────┘

bitmapAndnot

Computes the logical conjunction of two bitmaps and negates the result.

Syntax

bitmapAndnot(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapToArray(bitmapAndnot(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

┌─res───┐
│ [1,2] │
└───────┘

bitmapAndCardinality

Returns the cardinality of the logical conjunction of two bitmaps.

Syntax

bitmapAndCardinality(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapAndCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

┌─res─┐
│   1 │
└─────┘

bitmapOrCardinality

Returns the cardinality of the logical disjunction of two bitmaps.

bitmapOrCardinality(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapOrCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

┌─res─┐
│   5 │
└─────┘

bitmapXorCardinality

Returns the cardinality of the XOR of two bitmaps.

bitmapXorCardinality(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapXorCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

┌─res─┐
│   4 │
└─────┘

bitmapAndnotCardinality

Returns the cardinality of the AND-NOT operation of two bitmaps.

bitmapAndnotCardinality(bitmap, bitmap)

Arguments

  • bitmap – Bitmap object.

Example

SELECT bitmapAndnotCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

┌─res─┐
│   2 │
└─────┘
Was this page helpful?
Updated