ClickHouse
100 строк · 2.8 Кб
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 <IO/WriteHelpers.h>
10#include <Common/typeid_cast.h>
11#include <base/range.h>
12
13#include <constants.h>
14#include <h3api.h>
15
16
17namespace DB
18{
19namespace ErrorCodes
20{
21extern const int ILLEGAL_TYPE_OF_ARGUMENT;
22extern const int ILLEGAL_COLUMN;
23extern const int INCORRECT_DATA;
24}
25
26namespace
27{
28
29class FunctionH3CellAreaRads2 final : public IFunction
30{
31public:
32static constexpr auto name = "h3CellAreaRads2";
33
34static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3CellAreaRads2>(); }
35
36std::string getName() const override { return name; }
37
38size_t getNumberOfArguments() const override { return 1; }
39bool useDefaultImplementationForConstants() const override { return true; }
40bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
41
42DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
43{
44const auto * arg = arguments[0].get();
45if (!WhichDataType(arg).isUInt64())
46throw Exception(
47ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
48"Illegal type {} of argument {} of function {}. Must be UInt64",
49arg->getName(), 1, getName());
50
51return std::make_shared<DataTypeFloat64>();
52}
53
54ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
55{
56auto non_const_arguments = arguments;
57for (auto & argument : non_const_arguments)
58argument.column = argument.column->convertToFullColumnIfConst();
59
60const auto * column = checkAndGetColumn<ColumnUInt64>(non_const_arguments[0].column.get());
61if (!column)
62throw Exception(
63ErrorCodes::ILLEGAL_COLUMN,
64"Illegal type {} of argument {} of function {}. Must be UInt64",
65arguments[0].type->getName(),
661,
67getName());
68const auto & data = column->getData();
69
70auto dst = ColumnVector<Float64>::create();
71auto & dst_data = dst->getData();
72dst_data.resize(input_rows_count);
73
74for (size_t row = 0; row < input_rows_count; ++row)
75{
76const UInt64 index = data[row];
77
78CellBoundary boundary{};
79auto err = cellToBoundary(index, &boundary);
80if (err)
81throw Exception(ErrorCodes::INCORRECT_DATA, "Incorrect H3 index: {}, error: {}", index, err);
82
83Float64 res = cellAreaRads2(index);
84dst_data[row] = res;
85}
86
87return dst;
88}
89};
90
91}
92
93REGISTER_FUNCTION(H3CellAreaRads2)
94{
95factory.registerFunction<FunctionH3CellAreaRads2>();
96}
97
98}
99
100#endif
101