ClickHouse
94 строки · 2.6 Кб
1#include "config.h"2
3#if USE_H34
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 DB18{
19namespace ErrorCodes20{
21extern const int ILLEGAL_TYPE_OF_ARGUMENT;22extern const int ILLEGAL_COLUMN;23}
24
25namespace
26{
27
28class FunctionH3ExactEdgeLengthKm : public IFunction29{
30public:31static constexpr auto name = "h3ExactEdgeLengthKm";32
33static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3ExactEdgeLengthKm>(); }34
35std::string getName() const override { return name; }36
37size_t getNumberOfArguments() const override { return 1; }38bool useDefaultImplementationForConstants() const override { return true; }39bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }40
41DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override42{43const auto * arg = arguments[0].get();44if (!WhichDataType(arg).isUInt64())45throw Exception(46ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,47"Illegal type {} of argument {} of function {}. Must be UInt64",48arg->getName(), 1, getName());49
50return std::make_shared<DataTypeFloat64>();51}52
53ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override54{55auto non_const_arguments = arguments;56for (auto & argument : non_const_arguments)57argument.column = argument.column->convertToFullColumnIfConst();58
59const auto * column = checkAndGetColumn<ColumnUInt64>(non_const_arguments[0].column.get());60if (!column)61throw Exception(62ErrorCodes::ILLEGAL_COLUMN,63"Illegal type {} of argument {} of function {}. Must be UInt64",64arguments[0].type->getName(),651,66getName());67
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];77Float64 res = exactEdgeLengthKm(index);78dst_data[row] = res;79}80
81return dst;82}83};84
85}
86
87REGISTER_FUNCTION(H3ExactEdgeLengthKm)88{
89factory.registerFunction<FunctionH3ExactEdgeLengthKm>();90}
91
92}
93
94#endif95