ClickHouse

Форк
0
/
h3GetUnidirectionalEdge.cpp 
127 строк · 4.2 Кб
1
#include "config.h"
2

3
#if USE_H3
4

5
#include <Columns/ColumnArray.h>
6
#include <Columns/ColumnsNumber.h>
7
#include <DataTypes/DataTypeArray.h>
8
#include <DataTypes/DataTypesNumber.h>
9
#include <Functions/FunctionFactory.h>
10
#include <Functions/IFunction.h>
11
#include <Common/typeid_cast.h>
12
#include <IO/WriteHelpers.h>
13
#include <h3api.h>
14

15

16
namespace DB
17
{
18
namespace ErrorCodes
19
{
20
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
21
    extern const int ILLEGAL_COLUMN;
22
}
23

24
namespace
25
{
26

27
class FunctionH3GetUnidirectionalEdge : public IFunction
28
{
29
public:
30
    static constexpr auto name = "h3GetUnidirectionalEdge";
31

32
    static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionH3GetUnidirectionalEdge>(); }
33

34
    std::string getName() const override { return name; }
35

36
    size_t getNumberOfArguments() const override { return 2; }
37
    bool useDefaultImplementationForConstants() const override { return true; }
38
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
39

40
    DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
41
    {
42
        const auto * arg = arguments[0].get();
43
        if (!WhichDataType(arg).isUInt64())
44
            throw Exception(
45
                ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
46
                "Illegal type {} of argument {} of function {}. Must be UInt64",
47
                arg->getName(), 1, getName());
48

49
        arg = arguments[1].get();
50
        if (!WhichDataType(arg).isUInt64())
51
            throw Exception(
52
                ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
53
                "Illegal type {} of argument {} of function {}. Must be UInt64",
54
                arg->getName(), 2, getName());
55

56
        return std::make_shared<DataTypeUInt64>();
57
    }
58

59
    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
60
    {
61
        auto non_const_arguments = arguments;
62
        for (auto & argument : non_const_arguments)
63
            argument.column = argument.column->convertToFullColumnIfConst();
64

65
        const auto * col_hindex_origin = checkAndGetColumn<ColumnUInt64>(non_const_arguments[0].column.get());
66
        if (!col_hindex_origin)
67
            throw Exception(
68
                ErrorCodes::ILLEGAL_COLUMN,
69
                "Illegal type {} of argument {} of function {}. Must be UInt64.",
70
                arguments[0].type->getName(),
71
                1,
72
                getName());
73

74
        const auto & data_hindex_origin = col_hindex_origin->getData();
75

76
        const auto * col_hindex_dest = checkAndGetColumn<ColumnUInt64>(non_const_arguments[1].column.get());
77
        if (!col_hindex_dest)
78
            throw Exception(
79
                ErrorCodes::ILLEGAL_COLUMN,
80
                "Illegal type {} of argument {} of function {}. Must be UInt64.",
81
                arguments[1].type->getName(),
82
                2,
83
                getName());
84

85
        const auto & data_hindex_dest = col_hindex_dest->getData();
86

87
        auto dst = ColumnVector<UInt64>::create();
88
        auto & dst_data = dst->getData();
89
        dst_data.resize(input_rows_count);
90

91
        for (size_t row = 0; row < input_rows_count; ++row)
92
        {
93
            const UInt64 origin = data_hindex_origin[row];
94
            const UInt64 dest = data_hindex_dest[row];
95

96
            if (!isValidCell(origin))
97
                throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Invalid origin H3 index: {}", origin);
98
            if (!isValidCell(dest))
99
                throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Invalid dest H3 index: {}", dest);
100

101
            UInt64 res = getUnidirectionalEdge(origin, dest);
102
            dst_data[row] = res;
103
        }
104

105
        return dst;
106
    }
107

108
    /// suppress asan errors generated by the following:
109
    /// 'NEW_ADJUSTMENT_III' defined in '../contrib/h3/src/h3lib/lib/algos.c:142:24
110
    /// 'NEW_DIGIT_III' defined in '../contrib/h3/src/h3lib/lib/algos.c:121:24
111
    __attribute__((no_sanitize_address)) static inline UInt64 getUnidirectionalEdge(const UInt64 origin, const UInt64 dest)
112
    {
113
        const UInt64 res = cellsToDirectedEdge(origin, dest);
114
        return res;
115
    }
116
};
117

118
}
119

120
REGISTER_FUNCTION(H3GetUnidirectionalEdge)
121
{
122
    factory.registerFunction<FunctionH3GetUnidirectionalEdge>();
123
}
124

125
}
126

127
#endif
128

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.