ClickHouse/dbms/src/Functions/CRC32.cpp

69 lines
1.9 KiB
C++
Raw Normal View History

2019-06-18 07:26:47 +00:00
#include <zlib.h>
2019-06-17 21:49:37 +00:00
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionStringOrArrayToT.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
2019-06-19 07:59:38 +00:00
/** Calculates the CRC32 of a string
2019-06-17 21:49:37 +00:00
*/
struct CRC32Impl
{
static constexpr auto is_fixed_to_constant = true;
static void vector(const ColumnString::Chars & data, const ColumnString::Offsets & offsets, PaddedPODArray<UInt32> & res)
{
size_t size = offsets.size();
ColumnString::Offset prev_offset = 0;
for (size_t i = 0; i < size; ++i)
{
2019-06-18 07:26:47 +00:00
res[i] = do_crc32(data, prev_offset, offsets[i] - prev_offset - 1);
2019-06-17 21:49:37 +00:00
prev_offset = offsets[i];
}
}
2019-06-18 07:26:47 +00:00
static void vector_fixed_to_constant(const ColumnString::Chars & data, size_t n, UInt32 & res) { res = do_crc32(data, 0, n); }
2019-06-17 21:49:37 +00:00
static void vector_fixed_to_vector(const ColumnString::Chars & data, size_t n, PaddedPODArray<UInt32> & res)
{
size_t size = data.size() / n;
for (size_t i = 0; i < size; ++i)
{
2019-06-18 07:26:47 +00:00
res[i] = do_crc32(data, i * n, n);
2019-06-17 21:49:37 +00:00
}
}
[[noreturn]] static void array(const ColumnString::Offsets & /*offsets*/, PaddedPODArray<UInt32> & /*res*/)
2019-06-17 21:49:37 +00:00
{
2019-06-19 07:59:38 +00:00
throw Exception("Cannot apply function CRC32 to Array argument", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2019-06-17 21:49:37 +00:00
}
private:
2019-06-18 07:26:47 +00:00
static uint32_t do_crc32(const ColumnString::Chars & buf, size_t offset, size_t size)
2019-06-17 21:49:37 +00:00
{
2019-06-18 07:26:47 +00:00
const unsigned char * p = reinterpret_cast<const unsigned char *>(&buf[0]) + offset;
return crc32(0L, p, size);
2019-06-17 21:49:37 +00:00
}
};
struct NameCRC32
{
2019-06-19 22:31:51 +00:00
static constexpr auto name = "CRC32";
2019-06-17 21:49:37 +00:00
};
using FunctionCRC32 = FunctionStringOrArrayToT<CRC32Impl, NameCRC32, UInt32>;
void registerFunctionCRC32(FunctionFactory & factory)
{
2019-06-19 08:33:45 +00:00
factory.registerFunction<FunctionCRC32>(NameCRC32::name, FunctionFactory::CaseInsensitive);
2019-06-17 21:49:37 +00:00
}
}