Merge pull request #55607 from rschu1ze/byteswap-128-256

byteswap: Add 16/32-byte integer support
This commit is contained in:
Robert Schulze 2023-10-14 10:30:29 +02:00 committed by GitHub
commit a47cff1981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 4 deletions

View File

@ -444,7 +444,7 @@ DB::Exception: Decimal result's scale is less than argument's one: While process
## byteSwap
Reverses the bytes of an integer, i.e. changes its [endianness](https://en.wikipedia.org/wiki/Endianness). Currently, integers of up to 64 bit are supported.
Reverses the bytes of an integer, i.e. changes its [endianness](https://en.wikipedia.org/wiki/Endianness).
**Syntax**

View File

@ -17,6 +17,15 @@ T byteSwap(T x)
return std::byteswap(x);
}
template <typename T>
requires std::is_same_v<T, UInt128> || std::is_same_v<T, Int128> || std::is_same_v<T, UInt256> || std::is_same_v<T, Int256>
T byteSwap(T x)
{
T dest;
reverseMemcpy(&dest, &x, sizeof(T));
return dest;
}
template <typename T>
T byteSwap(T)
{
@ -55,7 +64,7 @@ REGISTER_FUNCTION(ByteSwap)
factory.registerFunction<FunctionByteSwap>(
FunctionDocumentation{
.description = R"(
Reverses the bytes of an integer, i.e. changes its [endianness](https://en.wikipedia.org/wiki/Endianness). Currently, integers of up to 64 bit are supported.
Reverses the bytes of an integer, i.e. changes its [endianness](https://en.wikipedia.org/wiki/Endianness).
**Example**

View File

@ -25,5 +25,9 @@
-549755813889
4039370097989451775
128
72057594037927936
-2361183241434822606849
1329227995784915872903807060280344576
-43556142965880123323311949751266331066369
0
1

View File

@ -33,6 +33,12 @@ SELECT byteSwap(-2147483649::Int64);
SELECT byteSwap(-1242525266376::Int64);
SELECT byteSwap(-9223372036854775808::Int64);
SELECT byteSwap(18446744073709551616::UInt128);
SELECT byteSwap(-9223372036854775809::Int128);
SELECT byteSwap(340282366920938463463374607431768211456::UInt256);
SELECT byteSwap(-170141183460469231731687303715884105729::Int256);
-- Booleans are interpreted as UInt8
SELECT byteSwap(false);
SELECT byteSwap(true);
@ -52,6 +58,4 @@ SELECT byteSwap(generateUUIDv4()); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT byteSwap(toDecimal32(2, 4)); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT byteSwap(toFloat32(123.456)); -- { serverError NOT_IMPLEMENTED }
SELECT byteSwap(toFloat64(123.456)); -- { serverError NOT_IMPLEMENTED }
SELECT byteSwap(18446744073709551616::UInt128); -- { serverError NOT_IMPLEMENTED }
SELECT byteSwap(-9223372036854775809::Int128); -- { serverError NOT_IMPLEMENTED }