mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-20 08:40:50 +00:00
Address a few review comments.
- Consider byteswap injective. - Make function case-insensitive. - Add in-code documentation and copy-paste it to the markdown docs.
This commit is contained in:
parent
ed2c85bbce
commit
302291e17b
@ -716,11 +716,7 @@ Result:
|
|||||||
|
|
||||||
Accepts an unsigned integer `operand` and returns the integer which is obtained by swapping the **endianness** of `operand` i.e. reversing the bytes of the `operand`.
|
Accepts an unsigned integer `operand` and returns the integer which is obtained by swapping the **endianness** of `operand` i.e. reversing the bytes of the `operand`.
|
||||||
|
|
||||||
**Syntax**
|
Currently, this is implemented for UInt8, UInt16, UInt32 and UInt64.
|
||||||
|
|
||||||
```sql
|
|
||||||
byteSwap(operand)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
@ -743,8 +739,7 @@ The above example can be worked out in the following manner:
|
|||||||
|
|
||||||
Note that, in step#1, one can also choose to convert the operand to bytes in little-endian as long as one also assumes little-endian when converting back to integer in step#3.
|
Note that, in step#1, one can also choose to convert the operand to bytes in little-endian as long as one also assumes little-endian when converting back to integer in step#3.
|
||||||
|
|
||||||
This can be particularly useful when one wants to reverse values of data-types which are stored as unsigned integers under the hood and allow conversions from unsigned integers to themselves (such as IPV4). For example:
|
One use-case of this function is reversing IPv4s:
|
||||||
|
|
||||||
```result
|
```result
|
||||||
┌─toIPv4(3351772109)─┐
|
┌─toIPv4(3351772109)─┐
|
||||||
│ 199.199.251.205 │
|
│ 199.199.251.205 │
|
||||||
|
@ -60,7 +60,7 @@ struct NameByteSwap
|
|||||||
{
|
{
|
||||||
static constexpr auto name = "byteSwap";
|
static constexpr auto name = "byteSwap";
|
||||||
};
|
};
|
||||||
using FunctionByteSwap = FunctionUnaryArithmetic<ByteSwapImpl, NameByteSwap, false>;
|
using FunctionByteSwap = FunctionUnaryArithmetic<ByteSwapImpl, NameByteSwap, true>;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +73,53 @@ struct FunctionUnaryArithmeticMonotonicity<NameByteSwap>
|
|||||||
|
|
||||||
REGISTER_FUNCTION(ByteSwap)
|
REGISTER_FUNCTION(ByteSwap)
|
||||||
{
|
{
|
||||||
factory.registerFunction<FunctionByteSwap>();
|
factory.registerFunction<FunctionByteSwap>(
|
||||||
|
FunctionDocumentation{
|
||||||
|
.description = R"(
|
||||||
|
Accepts an unsigned integer `operand` and returns the integer which is obtained by swapping the **endianness** of `operand` i.e. reversing the bytes of the `operand`.
|
||||||
|
|
||||||
|
Currently, this is implemented for UInt8, UInt16, UInt32 and UInt64.
|
||||||
|
|
||||||
|
**Example**
|
||||||
|
|
||||||
|
```sql
|
||||||
|
byteSwap(3351772109)
|
||||||
|
```
|
||||||
|
|
||||||
|
Result:
|
||||||
|
|
||||||
|
```result
|
||||||
|
┌─byteSwap(3351772109)─┐
|
||||||
|
│ 3455829959 │
|
||||||
|
└──────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
The above example can be worked out in the following manner:
|
||||||
|
1. First, convert the integer operand (base 10) to its equivalent hexadecimal interpretation (base 16) in big-endian format i.e. 3351772109 -> C7 C7 FB CD (4 bytes)
|
||||||
|
2. Then, reverse the bytes i.e. C7 C7 FB CD -> CD FB C7 C7
|
||||||
|
3. Finally, the convert the hexadecimal number back to an integer assuming big-endian i.e. CD FB C7 C7 -> 3455829959
|
||||||
|
|
||||||
|
Note that, in step#1, one can also choose to convert the operand to bytes in little-endian as long as one also assumes little-endian when converting back to integer in step#3.
|
||||||
|
|
||||||
|
One use-case of this function is reversing IPv4s:
|
||||||
|
```result
|
||||||
|
┌─toIPv4(3351772109)─┐
|
||||||
|
│ 199.199.251.205 │
|
||||||
|
└────────────────────┘
|
||||||
|
|
||||||
|
┌─toIPv4(byteSwap(3351772109))─┐
|
||||||
|
│ 205.251.199.199 │
|
||||||
|
└──────────────────────────────┘
|
||||||
|
```
|
||||||
|
)",
|
||||||
|
.examples{
|
||||||
|
{"8-bit", "SELECT byteSwap(54)", "54"},
|
||||||
|
{"16-bit", "SELECT byteSwap(4135)", "10000"},
|
||||||
|
{"32-bit", "SELECT byteSwap(3351772109)", "3455829959"},
|
||||||
|
{"64-bit", "SELECT byteSwap(123294967295)", "18439412204227788800"},
|
||||||
|
},
|
||||||
|
.categories{"Mathematical"}},
|
||||||
|
FunctionFactory::CaseInsensitive);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user