ClickHouse
90 строк · 2.5 Кб
1#include "config.h"
2
3#if USE_H3
4
5#include <Columns/ColumnsNumber.h>
6#include <DataTypes/DataTypesNumber.h>
7#include <Functions/FunctionFactory.h>
8#include <Functions/IFunction.h>
9#include <Common/typeid_cast.h>
10#include <base/range.h>
11
12#include <h3api.h>
13
14
15namespace DB
16{
17namespace ErrorCodes
18{
19extern const int ILLEGAL_TYPE_OF_ARGUMENT;
20extern const int ILLEGAL_COLUMN;
21}
22
23namespace
24{
25
26class FunctionH3IsPentagon : public IFunction
27{
28public:
29static constexpr auto name = "h3IsPentagon";
30
31static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3IsPentagon>(); }
32
33std::string getName() const override { return name; }
34
35size_t getNumberOfArguments() const override { return 1; }
36bool useDefaultImplementationForConstants() const override { return true; }
37bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
38
39DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
40{
41const auto * arg = arguments[0].get();
42if (!WhichDataType(arg).isUInt64())
43throw Exception(
44ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
45"Illegal type {} of argument {} of function {}. Must be UInt64",
46arg->getName(), 1, getName());
47
48return std::make_shared<DataTypeUInt8>();
49}
50
51ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
52{
53auto non_const_arguments = arguments;
54for (auto & argument : non_const_arguments)
55argument.column = argument.column->convertToFullColumnIfConst();
56
57const auto * column = checkAndGetColumn<ColumnUInt64>(non_const_arguments[0].column.get());
58if (!column)
59throw Exception(
60ErrorCodes::ILLEGAL_COLUMN,
61"Illegal type {} of argument {} of function {}. Must be UInt64",
62arguments[0].type->getName(),
631,
64getName());
65
66const auto & data = column->getData();
67
68auto dst = ColumnVector<UInt8>::create();
69auto & dst_data = dst->getData();
70dst_data.resize(input_rows_count);
71
72for (size_t row = 0; row < input_rows_count; ++row)
73{
74UInt8 res = isPentagon(data[row]);
75dst_data[row] = res;
76}
77return dst;
78}
79};
80
81}
82
83REGISTER_FUNCTION(H3IsPentagon)
84{
85factory.registerFunction<FunctionH3IsPentagon>();
86}
87
88}
89
90#endif
91