2022-09-28 12:35:02 +00:00
|
|
|
#include "config.h"
|
2020-11-04 14:51:41 +00:00
|
|
|
|
|
|
|
#if USE_H3
|
|
|
|
|
2020-06-19 10:06:42 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/GatherUtils/GatherUtils.h>
|
|
|
|
#include <Functions/GatherUtils/Sources.h>
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2020-01-24 20:16:06 +00:00
|
|
|
|
2020-06-19 10:06:42 +00:00
|
|
|
#include <h3api.h>
|
2020-01-24 20:16:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
2020-01-24 20:16:06 +00:00
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-01-24 20:16:06 +00:00
|
|
|
using namespace GatherUtils;
|
|
|
|
|
|
|
|
class FunctionStringToH3 : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "stringToH3";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionStringToH3>(); }
|
2020-01-24 20:16:06 +00:00
|
|
|
|
|
|
|
std::string getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2020-01-24 20:16:06 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2020-04-22 08:45:14 +00:00
|
|
|
const auto * arg = arguments[0].get();
|
2020-01-24 20:16:06 +00:00
|
|
|
if (!WhichDataType(arg).isStringOrFixedString())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument {} of function {}. "
|
|
|
|
"Must be String or FixedString", arg->getName(), std::to_string(1), getName());
|
2020-01-24 20:16:06 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2020-01-24 20:16:06 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
const auto * col_hindex = arguments[0].column.get();
|
2020-01-24 20:16:06 +00:00
|
|
|
|
|
|
|
auto dst = ColumnVector<UInt64>::create();
|
|
|
|
auto & dst_data = dst->getData();
|
|
|
|
dst_data.resize(input_rows_count);
|
|
|
|
|
2020-04-22 08:45:14 +00:00
|
|
|
if (const auto * h3index = checkAndGetColumn<ColumnString>(col_hindex))
|
2020-01-24 20:16:06 +00:00
|
|
|
execute<StringSource>(StringSource(*h3index), dst_data);
|
2020-04-22 08:45:14 +00:00
|
|
|
else if (const auto * h3index_fixed = checkAndGetColumn<ColumnFixedString>(col_hindex))
|
2020-01-24 20:16:06 +00:00
|
|
|
execute<FixedStringSource>(FixedStringSource(*h3index_fixed), dst_data);
|
|
|
|
else if (const ColumnConst * h3index_const = checkAndGetColumnConst<ColumnString>(col_hindex))
|
|
|
|
execute<ConstSource<StringSource>>(ConstSource<StringSource>(*h3index_const), dst_data);
|
|
|
|
else if (const ColumnConst * h3index_const_fixed = checkAndGetColumnConst<ColumnFixedString>(col_hindex))
|
|
|
|
execute<ConstSource<FixedStringSource>>(ConstSource<FixedStringSource>(*h3index_const_fixed), dst_data);
|
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column as argument of function {}", getName());
|
2020-01-24 20:16:06 +00:00
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
return dst;
|
2020-01-24 20:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename H3IndexSource>
|
|
|
|
static void execute(H3IndexSource h3index_source, PaddedPODArray<UInt64> & res_data)
|
|
|
|
{
|
|
|
|
size_t row_num = 0;
|
|
|
|
|
|
|
|
while (!h3index_source.isEnd())
|
|
|
|
{
|
|
|
|
auto h3index = h3index_source.getWhole();
|
|
|
|
|
2021-08-23 10:59:01 +00:00
|
|
|
// convert to std::string and get the c_str to have the delimiting \0 at the end.
|
2022-07-18 09:41:01 +00:00
|
|
|
auto h3index_str = std::string(reinterpret_cast<const char *>(h3index.data), h3index.size);
|
2020-01-24 20:16:06 +00:00
|
|
|
res_data[row_num] = stringToH3(h3index_str.c_str());
|
|
|
|
|
|
|
|
if (res_data[row_num] == 0)
|
|
|
|
{
|
2023-12-26 11:53:00 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Invalid H3 index: {} in function {}", h3index_str, name);
|
2020-01-24 20:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h3index_source.next();
|
|
|
|
++row_num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2020-01-24 20:16:06 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(StringToH3)
|
2020-01-24 20:16:06 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionStringToH3>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-11-04 14:51:41 +00:00
|
|
|
|
|
|
|
#endif
|