ClickHouse/docs/en/sql-reference/functions/encoding-functions.md

407 lines
12 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
toc_priority: 52
toc_title: Encoding
---
# Encoding Functions {#encoding-functions}
2020-03-18 18:43:51 +00:00
## char {#char}
2020-02-02 02:55:57 +00:00
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**
2020-03-20 10:10:48 +00:00
``` sql
char(number_1, [number_2, ..., number_n]);
```
**Arguments**
- `number_1, number_2, ..., number_n` — Numerical arguments interpreted as integers. Types: [Int](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md).
**Returned value**
- a string of given bytes.
Type: `String`.
**Example**
Query:
2020-03-20 10:10:48 +00:00
``` sql
SELECT char(104.1, 101, 108.9, 108.9, 111) AS hello;
```
Result:
2020-03-20 10:10:48 +00:00
``` text
┌─hello─┐
│ hello │
└───────┘
```
2020-02-02 02:55:57 +00:00
You can construct a string of arbitrary encoding by passing the corresponding bytes. Here is example for UTF-8:
Query:
2020-03-20 10:10:48 +00:00
``` sql
SELECT char(0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, 0xD1, 0x82) AS hello;
```
Result:
2020-03-20 10:10:48 +00:00
``` text
┌─hello──┐
│ привет │
└────────┘
```
Query:
2020-03-20 10:10:48 +00:00
``` sql
SELECT char(0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD) AS hello;
```
Result:
2020-03-20 10:10:48 +00:00
``` text
┌─hello─┐
│ 你好 │
└───────┘
```
2020-03-18 18:43:51 +00:00
## hex {#hex}
2020-03-20 10:10:48 +00:00
Returns a string containing the arguments hexadecimal representation.
2021-02-16 11:12:44 +00:00
Alias: `HEX`.
**Syntax**
2020-03-20 10:10:48 +00:00
``` sql
2020-01-20 10:51:40 +00:00
hex(arg)
```
2020-02-02 20:40:46 +00:00
The function is using uppercase letters `A-F` and not using any prefixes (like `0x`) or suffixes (like `h`).
2021-07-29 15:37:32 +00:00
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.
2021-09-01 14:26:36 +00:00
Values of type [Date](../../sql-reference/data-types/date.md) and [DateTime](../../sql-reference/data-types/datetime.md) are formatted as corresponding integers (the number of days since Epoch for Date and the value of Unix Timestamp for DateTime).
2020-02-02 20:40:46 +00:00
2021-09-01 14:26:36 +00:00
For [String](../../sql-reference/data-types/string.md) and [FixedString](../../sql-reference/data-types/fixedstring.md), all bytes are simply encoded as two hexadecimal numbers. Zero bytes are not omitted.
2021-09-01 14:26:36 +00:00
Values of [Float](../../sql-reference/data-types/float.md) and [Decimal](../../sql-reference/data-types/decimal.md) 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.
**Arguments**
- `arg` — A value to convert to hexadecimal. Types: [String](../../sql-reference/data-types/string.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md), [Decimal](../../sql-reference/data-types/decimal.md), [Date](../../sql-reference/data-types/date.md) or [DateTime](../../sql-reference/data-types/datetime.md).
**Returned value**
- A string with the hexadecimal representation of the argument.
2021-09-01 14:26:36 +00:00
Type: [String](../../sql-reference/data-types/string.md).
2021-09-01 14:26:36 +00:00
**Examples**
Query:
``` sql
SELECT hex(1);
```
Result:
``` text
01
```
2020-01-20 10:51:40 +00:00
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-09-01 14:26:36 +00:00
SELECT hex(toFloat32(number)) AS hex_presentation FROM numbers(15, 2);
```
2020-01-20 10:51:40 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
┌─hex_presentation─┐
│ 00007041 │
│ 00008041 │
└──────────────────┘
```
2020-01-20 10:51:40 +00:00
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-09-01 14:26:36 +00:00
SELECT hex(toFloat64(number)) AS hex_presentation FROM numbers(15, 2);
```
2020-01-20 10:51:40 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
┌─hex_presentation─┐
│ 0000000000002E40 │
│ 0000000000003040 │
└──────────────────┘
```
2021-05-25 06:21:45 +00:00
## unhex {#unhexstr}
Performs the opposite operation of [hex](#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).
2021-05-24 09:55:33 +00:00
2021-05-29 13:55:20 +00:00
If you want to convert the result to a number, you can use the [reverse](../../sql-reference/functions/string-functions.md#reverse) and [reinterpretAs<Type>](../../sql-reference/functions/type-conversion-functions.md#type-conversion-functions) functions.
2021-05-24 09:55:33 +00:00
2021-05-29 14:29:56 +00:00
!!! note "Note"
If `unhex` is invoked from within the `clickhouse-client`, binary strings display using UTF-8.
2021-05-29 14:29:56 +00:00
2021-05-25 11:47:39 +00:00
Alias: `UNHEX`.
2021-05-24 09:55:33 +00:00
**Syntax**
``` sql
unhex(arg)
```
**Arguments**
- `arg` — A string containing any number of hexadecimal digits. Type: [String](../../sql-reference/data-types/string.md).
2021-05-29 14:29:56 +00:00
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 isnt thrown). For a numeric argument the inverse of hex(N) is not performed by unhex().
2021-05-24 09:55:33 +00:00
**Returned value**
- A binary string (BLOB).
2021-05-24 09:55:33 +00:00
Type: [String](../../sql-reference/data-types/string.md).
**Example**
Query:
``` sql
SELECT unhex('303132'), UNHEX('4D7953514C');
```
Result:
``` text
┌─unhex('303132')─┬─unhex('4D7953514C')─┐
│ 012 │ MySQL │
└─────────────────┴─────────────────────┘
```
2021-05-29 10:19:46 +00:00
Query:
2021-05-29 13:55:20 +00:00
2021-05-29 10:19:46 +00:00
``` sql
2021-05-29 13:55:20 +00:00
SELECT reinterpretAsUInt64(reverse(unhex('FFF'))) AS num;
2021-05-29 10:19:46 +00:00
```
Result:
2021-05-29 13:55:20 +00:00
2021-05-29 10:19:46 +00:00
``` text
2021-05-29 13:55:20 +00:00
┌──num─┐
│ 4095 │
└──────┘
2021-05-29 10:19:46 +00:00
```
## bin {#bin}
Returns a string containing the arguments binary representation.
**Syntax**
``` sql
bin(arg)
```
2021-08-26 11:16:05 +00:00
Alias: `BIN`.
2021-08-26 11:16:05 +00:00
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.
2021-08-30 13:35:27 +00:00
Values of type [Date](../../sql-reference/data-types/date.md) and [DateTime](../../sql-reference/data-types/datetime.md) are formatted as corresponding integers (the number of days since Epoch for `Date` and the value of Unix Timestamp for `DateTime`).
2021-08-30 14:10:13 +00:00
For [String](../../sql-reference/data-types/string.md) and [FixedString](../../sql-reference/data-types/fixedstring.md), all bytes are simply encoded as eight binary numbers. Zero bytes are not omitted.
2021-08-31 11:24:56 +00:00
Values of [Float](../../sql-reference/data-types/float.md) and [Decimal](../../sql-reference/data-types/decimal.md) 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.
**Arguments**
2021-08-30 14:10:13 +00:00
- `arg` — A value to convert to binary. [String](../../sql-reference/data-types/string.md), [FixedString](../../sql-reference/data-types/fixedstring.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md), [Decimal](../../sql-reference/data-types/decimal.md), [Date](../../sql-reference/data-types/date.md), or [DateTime](../../sql-reference/data-types/datetime.md).
**Returned value**
- A string with the binary representation of the argument.
2021-08-26 11:16:05 +00:00
Type: [String](../../sql-reference/data-types/string.md).
2021-08-26 11:16:05 +00:00
**Examples**
Query:
``` sql
SELECT bin(14);
```
Result:
``` text
┌─bin(14)──┐
│ 00001110 │
└──────────┘
```
Query:
``` sql
2021-08-26 11:16:05 +00:00
SELECT bin(toFloat32(number)) AS bin_presentation FROM numbers(15, 2);
```
Result:
``` text
┌─bin_presentation─────────────────┐
│ 00000000000000000111000001000001 │
│ 00000000000000001000000001000001 │
└──────────────────────────────────┘
```
Query:
``` sql
2021-08-26 11:16:05 +00:00
SELECT bin(toFloat64(number)) AS bin_presentation FROM numbers(15, 2);
```
Result:
``` text
┌─bin_presentation─────────────────────────────────────────────────┐
│ 0000000000000000000000000000000000000000000000000010111001000000 │
│ 0000000000000000000000000000000000000000000000000011000001000000 │
└──────────────────────────────────────────────────────────────────┘
```
## unbin {#unbinstr}
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](#bin).
**Syntax**
``` sql
unbin(arg)
```
2021-08-26 11:16:05 +00:00
Alias: `UNBIN`.
2021-09-01 18:05:22 +00:00
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](../../sql-reference/functions/string-functions.md#reverse) and [reinterpretAs<Type>](../../sql-reference/functions/type-conversion-functions.md#reinterpretasuint8163264) functions.
2021-08-26 11:16:05 +00:00
!!! note "Note"
If `unbin` is invoked from within the `clickhouse-client`, binary strings are displayed using UTF-8.
2021-09-11 07:46:55 +00:00
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 isnt thrown).
2021-08-26 11:16:05 +00:00
**Arguments**
- `arg` — A string containing any number of binary digits. [String](../../sql-reference/data-types/string.md).
**Returned value**
- A binary string (BLOB).
Type: [String](../../sql-reference/data-types/string.md).
2021-08-26 11:16:05 +00:00
**Examples**
Query:
``` sql
SELECT UNBIN('001100000011000100110010'), UNBIN('0100110101111001010100110101000101001100');
```
Result:
``` text
┌─unbin('001100000011000100110010')─┬─unbin('0100110101111001010100110101000101001100')─┐
│ 012 │ MySQL │
└───────────────────────────────────┴───────────────────────────────────────────────────┘
```
Query:
``` sql
2021-08-26 11:16:05 +00:00
SELECT reinterpretAsUInt64(reverse(unbin('1110'))) AS num;
```
Result:
``` text
┌─num─┐
2021-08-26 11:16:05 +00:00
│ 14 │
└─────┘
```
2020-03-20 10:10:48 +00:00
## UUIDStringToNum(str) {#uuidstringtonumstr}
Accepts a string containing 36 characters in the format `123e4567-e89b-12d3-a456-426655440000`, and returns it as a set of bytes in a FixedString(16).
2020-03-20 10:10:48 +00:00
## UUIDNumToString(str) {#uuidnumtostringstr}
Accepts a FixedString(16) value. Returns a string containing 36 characters in text format.
2020-03-20 10:10:48 +00:00
## bitmaskToList(num) {#bitmasktolistnum}
Accepts an integer. Returns a string containing the list of powers of two that total the source number when summed. They are comma-separated without spaces in text format, in ascending order.
2020-03-20 10:10:48 +00:00
## bitmaskToArray(num) {#bitmasktoarraynum}
Accepts an integer. Returns an array of UInt64 numbers containing the list of powers of two that total the source number when summed. Numbers in the array are in ascending order.
## bitPositionsToArray(num) {#bitpositionstoarraynum}
2021-05-11 04:39:03 +00:00
Accepts an integer and converts it to an unsigned integer. Returns an array of `UInt64` numbers containing the list of positions of bits of `arg` that equal `1`, in ascending order.
2021-05-13 06:26:12 +00:00
**Syntax**
```sql
bitPositionsToArray(arg)
2021-05-13 06:26:12 +00:00
```
**Arguments**
2021-06-29 01:55:31 +00:00
- `arg` — Integer value. [Int/UInt](../../sql-reference/data-types/int-uint.md).
2021-05-13 06:26:12 +00:00
**Returned value**
- An array containing a list of positions of bits that equal `1`, in ascending order.
2021-06-29 01:38:33 +00:00
Type: [Array](../../sql-reference/data-types/array.md)([UInt64](../../sql-reference/data-types/int-uint.md)).
2021-05-13 06:26:12 +00:00
**Example**
Query:
``` sql
SELECT bitPositionsToArray(toInt8(1)) AS bit_positions;
2021-05-13 06:26:12 +00:00
```
Result:
``` text
┌─bit_positions─┐
│ [0] │
└───────────────┘
2021-05-13 06:26:12 +00:00
```
Query:
``` sql
2021-09-01 14:26:36 +00:00
SELECT bitPositionsToArray(toInt8(-1)) AS bit_positions;
2021-05-13 06:26:12 +00:00
```
Result:
``` text
┌─bit_positions─────┐
2021-05-13 06:26:12 +00:00
│ [0,1,2,3,4,5,6,7] │
└───────────────────┘
```