ClickHouse

Форк
0
/
s2RectAdd.cpp 
151 строка · 4.9 Кб
1
#include "config.h"
2

3
#if USE_S2_GEOMETRY
4

5
#include <Columns/ColumnsNumber.h>
6
#include <Columns/ColumnTuple.h>
7
#include <DataTypes/DataTypesNumber.h>
8
#include <DataTypes/DataTypeTuple.h>
9
#include <Functions/FunctionFactory.h>
10
#include <Common/typeid_cast.h>
11
#include <base/range.h>
12

13
#include "s2_fwd.h"
14

15
namespace DB
16
{
17

18
namespace ErrorCodes
19
{
20
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
21
    extern const int BAD_ARGUMENTS;
22
    extern const int ILLEGAL_COLUMN;
23
}
24

25
namespace
26
{
27

28
class FunctionS2RectAdd : public IFunction
29
{
30
public:
31
    static constexpr auto name = "s2RectAdd";
32

33
    static FunctionPtr create(ContextPtr)
34
    {
35
        return std::make_shared<FunctionS2RectAdd>();
36
    }
37

38
    std::string getName() const override
39
    {
40
        return name;
41
    }
42

43
    size_t getNumberOfArguments() const override { return 3; }
44

45
    bool useDefaultImplementationForConstants() const override { return true; }
46

47
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
48

49
    DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
50
    {
51
        for (size_t index = 0; index < getNumberOfArguments(); ++index)
52
        {
53
            const auto * arg = arguments[index].get();
54
            if (!WhichDataType(arg).isUInt64())
55
                throw Exception(
56
                    ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
57
                    "Illegal type {} of argument {} of function {}. Must be UInt64",
58
                    arg->getName(), index, getName());
59
        }
60

61
        DataTypePtr element = std::make_shared<DataTypeUInt64>();
62

63
        return std::make_shared<DataTypeTuple>(DataTypes{element, element});
64
    }
65

66
    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
67
    {
68
        auto non_const_arguments = arguments;
69
        for (auto & argument : non_const_arguments)
70
            argument.column = argument.column->convertToFullColumnIfConst();
71

72
        const auto * col_lo = checkAndGetColumn<ColumnUInt64>(non_const_arguments[0].column.get());
73
        if (!col_lo)
74
            throw Exception(
75
                ErrorCodes::ILLEGAL_COLUMN,
76
                "Illegal type {} of argument {} of function {}. Must be UInt64",
77
                arguments[0].type->getName(),
78
                1,
79
                getName());
80
        const auto & data_low = col_lo->getData();
81

82
        const auto * col_hi = checkAndGetColumn<ColumnUInt64>(non_const_arguments[1].column.get());
83
        if (!col_hi)
84
            throw Exception(
85
                ErrorCodes::ILLEGAL_COLUMN,
86
                "Illegal type {} of argument {} of function {}. Must be UInt64",
87
                arguments[1].type->getName(),
88
                2,
89
                getName());
90
        const auto & data_hi = col_hi->getData();
91

92
        const auto * col_point = checkAndGetColumn<ColumnUInt64>(non_const_arguments[2].column.get());
93
        if (!col_point)
94
            throw Exception(
95
                ErrorCodes::ILLEGAL_COLUMN,
96
                "Illegal type {} of argument {} of function {}. Must be UInt64",
97
                arguments[2].type->getName(),
98
                3,
99
                getName());
100
        const auto & data_point = col_point->getData();
101

102
        auto col_res_first = ColumnUInt64::create();
103
        auto col_res_second = ColumnUInt64::create();
104

105
        auto & vec_res_first = col_res_first->getData();
106
        vec_res_first.reserve(input_rows_count);
107

108
        auto & vec_res_second = col_res_second->getData();
109
        vec_res_second.reserve(input_rows_count);
110

111
        for (size_t row = 0; row < input_rows_count; ++row)
112
        {
113
            const auto lo = S2CellId(data_low[row]);
114
            const auto hi = S2CellId(data_hi[row]);
115
            const auto point = S2CellId(data_point[row]);
116

117
            S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());
118

119
            if (!point.is_valid())
120
                throw Exception(ErrorCodes::BAD_ARGUMENTS,
121
                    "Point is invalid. For valid point the latitude is between -90 and 90 degrees inclusive "
122
                    "and the longitude is between -180 and 180 degrees inclusive.");
123

124
            if (!rect.is_valid())
125
                throw Exception(ErrorCodes::BAD_ARGUMENTS,
126
                    "Rectangle is invalid. For valid rectangles the latitude bounds do not exceed "
127
                    "Pi/2 in absolute value and the longitude bounds do not exceed Pi in absolute value. "
128
                    "Also, if either the latitude or longitude bound is empty then both must be. ");
129

130
            rect.AddPoint(point.ToPoint());
131

132
            vec_res_first.emplace_back(S2CellId(rect.lo()).id());
133
            vec_res_second.emplace_back(S2CellId(rect.hi()).id());
134
        }
135

136
        return ColumnTuple::create(Columns{std::move(col_res_first), std::move(col_res_second)});
137
    }
138

139
};
140

141
}
142

143
REGISTER_FUNCTION(S2RectAdd)
144
{
145
    factory.registerFunction<FunctionS2RectAdd>();
146
}
147

148

149
}
150

151
#endif
152

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

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

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

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