| 1 | #include "config_functions.h" | 
|---|---|
| 2 | #if USE_H3 | 
| 3 | # include <Columns/ColumnsNumber.h> | 
| 4 | # include <DataTypes/DataTypesNumber.h> | 
| 5 | # include <Functions/FunctionFactory.h> | 
| 6 | # include <Functions/IFunction.h> | 
| 7 | # include <Common/typeid_cast.h> | 
| 8 | # include <ext/range.h> | 
| 9 | |
| 10 | # include <h3api.h> | 
| 11 | |
| 12 | |
| 13 | namespace DB | 
| 14 | { | 
| 15 | class FunctionH3EdgeAngle : public IFunction | 
| 16 | { | 
| 17 | public: | 
| 18 | static constexpr auto name = "h3EdgeAngle"; | 
| 19 | |
| 20 | static FunctionPtr create(const Context &) { return std::make_shared<FunctionH3EdgeAngle>(); } | 
| 21 | |
| 22 | std::string getName() const override { return name; } | 
| 23 | |
| 24 | size_t getNumberOfArguments() const override { return 1; } | 
| 25 | bool useDefaultImplementationForConstants() const override { return true; } | 
| 26 | |
| 27 | DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override | 
| 28 | { | 
| 29 | auto arg = arguments[0].get(); | 
| 30 | if (!WhichDataType(arg).isUInt8()) | 
| 31 | throw Exception( | 
| 32 | "Illegal type "+ arg->getName() + " of argument "+ std::to_string(1) + " of function "+ getName() + ". Must be UInt8", | 
| 33 | ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | 
| 34 | |
| 35 | return std::make_shared<DataTypeFloat64>(); | 
| 36 | } | 
| 37 | |
| 38 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override | 
| 39 | { | 
| 40 | const auto col_hindex = block.getByPosition(arguments[0]).column.get(); | 
| 41 | |
| 42 | auto dst = ColumnVector<Float64>::create(); | 
| 43 | auto & dst_data = dst->getData(); | 
| 44 | dst_data.resize(input_rows_count); | 
| 45 | |
| 46 | for (const auto row : ext::range(0, input_rows_count)) | 
| 47 | { | 
| 48 | const int resolution = col_hindex->getUInt(row); | 
| 49 | |
| 50 | // Numerical constant is 180 degrees / pi / Earth radius, Earth radius is from h3 sources | 
| 51 | Float64 res = 8.99320592271288084e-6 * edgeLengthM(resolution); | 
| 52 | |
| 53 | dst_data[row] = res; | 
| 54 | } | 
| 55 | |
| 56 | block.getByPosition(result).column = std::move(dst); | 
| 57 | } | 
| 58 | }; | 
| 59 | |
| 60 | |
| 61 | void registerFunctionH3EdgeAngle(FunctionFactory & factory) | 
| 62 | { | 
| 63 | factory.registerFunction<FunctionH3EdgeAngle>(); | 
| 64 | } | 
| 65 | |
| 66 | } | 
| 67 | #endif | 
| 68 | 
