ClickHouse

Форк
0
/
s2RectContains.cpp 
141 строка · 4.4 Кб
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
namespace ErrorCodes
18
{
19
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
20
    extern const int BAD_ARGUMENTS;
21
    extern const int ILLEGAL_COLUMN;
22

23
}
24

25
namespace
26
{
27

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

33
    static FunctionPtr create(ContextPtr)
34
    {
35
        return std::make_shared<FunctionS2RectContains>();
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 i = 0; i < getNumberOfArguments(); ++i)
52
        {
53
            const auto * arg = arguments[i].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(), i, getName());
59
        }
60

61
        return std::make_shared<DataTypeUInt8>();
62
    }
63

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

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

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

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

100
        auto dst = ColumnVector<UInt8>::create();
101
        auto & dst_data = dst->getData();
102
        dst_data.reserve(input_rows_count);
103

104
        for (size_t row = 0; row < input_rows_count; ++row)
105
        {
106
            const auto lo = S2CellId(data_low[row]);
107
            const auto hi = S2CellId(data_hi[row]);
108
            const auto point = S2CellId(data_point[row]);
109

110
            S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());
111

112
            if (!point.is_valid())
113
                throw Exception(ErrorCodes::BAD_ARGUMENTS,
114
                    "Point is invalid. For valid point the latitude is between -90 and 90 degrees inclusive "
115
                    "and the longitude is between -180 and 180 degrees inclusive.");
116

117
            if (!rect.is_valid())
118
                throw Exception(ErrorCodes::BAD_ARGUMENTS,
119
                    "Rectangle is invalid. For valid rectangles the latitude bounds do not exceed "
120
                    "Pi/2 in absolute value and the longitude bounds do not exceed Pi in absolute value. "
121
                    "Also, if either the latitude or longitude bound is empty then both must be. ");
122

123
            dst_data.emplace_back(rect.Contains(point.ToLatLng()));
124
        }
125

126
        return dst;
127
    }
128

129
};
130

131
}
132

133
REGISTER_FUNCTION(S2RectContains)
134
{
135
    factory.registerFunction<FunctionS2RectContains>();
136
}
137

138

139
}
140

141
#endif
142

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

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

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

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