EXPORT-SET
Description
EXPORT_SET converts each bit of an integer into a specified string and concatenates the strings into a result. For each bit in bits, the result contains on if the bit is 1 and off if the bit is 0. The bits are checked in order from right to left (i.e., from the least significant bit to the most significant bit), but concatenated into the result string from left to right. The bits are separated by the separator (default is a comma ,).
The number of bits displayed is determined by the number_of_bits parameter, with a default of 64 bits. If not specified or out of range (greater than 64), it is automatically treated as 64 bits.
This function behaves similarly to MySQL's EXPORT_SET function.
Syntax
EXPORT_SET(bits, on, off[, separator[, number_of_bits]])
Parameters
| Parameter | Description |
|---|---|
bits | A LARGEINT value to convert. Values less than -2^63 are treated as -2^63. Values greater than 2^64 - 1 are treated as 2^63 - 1. |
on | The string displayed when the bit is 1 |
off | The string displayed when the bit is 0 |
separator | Optional. The string used to separate the bit representations. Defaults to ','. |
number_of_bits | Optional. An INT value specifying the number of bits to display. Defaults to 64. Values outside [0, 64] are treated as 64. |
Return value
Returns a string containing on for each 1 bit and off for each 0 bit, starting with the least significant bit. The strings are concatenated in order, separated by separator. The result represents number_of_bits bits.
If number_of_bits is outside [-2^31, 2^31 - 1] or any argument is NULL, the function returns NULL.
Example
SELECT EXPORT_SET(-2, '1', '0');
+---------------------------------------------------------------------------------------------------------------------------------+
| EXPORT_SET(-2, '1', '0') |
+---------------------------------------------------------------------------------------------------------------------------------+
| 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 |
+---------------------------------------------------------------------------------------------------------------------------------+
SELECT EXPORT_SET(5, '1', '0', '||', 5);
+----------------------------------+
| EXPORT_SET(5, '1', '0', '||', 5) |
+----------------------------------+
| 1||0||1||0||0 |
+----------------------------------+
The following table-based examples require an existing test_export_set table with the columns and rows shown below.
SELECT `bits`, `on`, `off`, `sep`, `num_of_b`
FROM `test_export_set`;
+-------------+--------+-------+-------+----------+
| bits | on | off | sep | num_of_b |
+-------------+--------+-------+-------+----------+
| -1 | 1 | 0 | , | 50 |
| -2 | 1 | 0 | | 64 |
| 5 | Y | N | , | 5 |
| 5 | 1 | 0 | | 64 |
| 5 | | 0 | | 65 |
| 6 | 1 | | | 63 |
| 19284249819 | 1 | 0 | , | 64 |
| 9 | apache | doris | |123| | 64 |
| NULL | 1 | 0 | , | 5 |
| 5 | NULL | 0 | | 5 |
| 5 | 1 | NULL | , | 10 |
| 5 | 1 | 0 | NULL | 10 |
| 5 | 1 | 0 | , | NULL |
+-------------+--------+-------+-------+----------+
SELECT `bits`, `on`, `off`, `sep`, `num_of_b`,
EXPORT_SET(`bits`, `on`, `off`, `sep`, `num_of_b`) AS ans
FROM `test_export_set`;
+-------------+--------+-------+-------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bits | on | off | sep | num_of_b | ans |
+-------------+--------+-------+-------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -1 | 1 | 0 | , | 50 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 |
| -2 | 1 | 0 | | 64 | 0111111111111111111111111111111111111111111111111111111111111111 |
| 5 | Y | N | , | 5 | Y,N,Y,N,N |
| 5 | 1 | 0 | | 64 | 1010000000000000000000000000000000000000000000000000000000000000 |
| 5 | | 0 | | 65 | 00000000000000000000000000000000000000000000000000000000000000 |
| 6 | 1 | | | 63 | 11 |
| 19284249819 | 1 | 0 | , | 64 | 1,1,0,1,1,0,1,1,0,0,1,1,0,0,1,0,0,1,1,1,0,1,1,0,1,0,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 9 | apache | doris | |123| | 64 | apache|123|doris|123|doris|123|apache|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris|123|doris |
| NULL | 1 | 0 | , | 5 | NULL |
| 5 | NULL | 0 | | 5 | NULL |
| 5 | 1 | NULL | , | 10 | NULL |
| 5 | 1 | 0 | NULL | 10 | NULL |
| 5 | 1 | 0 | , | NULL | NULL |
+-------------+--------+-------+-------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-- max value: 2^64 - 1
SELECT EXPORT_SET(18446744073709551615, '1', '0');
+---------------------------------------------------------------------------------------------------------------------------------+
| EXPORT_SET(18446744073709551615, '1', '0') |
+---------------------------------------------------------------------------------------------------------------------------------+
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 |
+---------------------------------------------------------------------------------------------------------------------------------+
-- 2^64 out of bound, the bits will be set to 2^63 - 1
SELECT EXPORT_SET(18446744073709551616, '1', '0');
+---------------------------------------------------------------------------------------------------------------------------------+
| EXPORT_SET(18446744073709551616, '1', '0') |
+---------------------------------------------------------------------------------------------------------------------------------+
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 |
+---------------------------------------------------------------------------------------------------------------------------------+
-- min value: -2^63
SELECT EXPORT_SET(-9223372036854775808, '1', '0');
+---------------------------------------------------------------------------------------------------------------------------------+
| EXPORT_SET(-9223372036854775808, '1', '0') |
+---------------------------------------------------------------------------------------------------------------------------------+
| 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 |
+---------------------------------------------------------------------------------------------------------------------------------+
-- out of bound, the bits will be set to -2^63
SELECT EXPORT_SET(-184467440737095516161, '1', '0');
+---------------------------------------------------------------------------------------------------------------------------------+
| EXPORT_SET(-184467440737095516161, '1', '0') |
+---------------------------------------------------------------------------------------------------------------------------------+
| 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 |
+---------------------------------------------------------------------------------------------------------------------------------+