ClickHouse
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
15namespace DB
16{
17
18namespace ErrorCodes
19{
20extern const int ILLEGAL_TYPE_OF_ARGUMENT;
21extern const int BAD_ARGUMENTS;
22extern const int ILLEGAL_COLUMN;
23}
24
25namespace
26{
27
28class FunctionS2RectAdd : public IFunction
29{
30public:
31static constexpr auto name = "s2RectAdd";
32
33static FunctionPtr create(ContextPtr)
34{
35return std::make_shared<FunctionS2RectAdd>();
36}
37
38std::string getName() const override
39{
40return name;
41}
42
43size_t getNumberOfArguments() const override { return 3; }
44
45bool useDefaultImplementationForConstants() const override { return true; }
46
47bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
48
49DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
50{
51for (size_t index = 0; index < getNumberOfArguments(); ++index)
52{
53const auto * arg = arguments[index].get();
54if (!WhichDataType(arg).isUInt64())
55throw Exception(
56ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
57"Illegal type {} of argument {} of function {}. Must be UInt64",
58arg->getName(), index, getName());
59}
60
61DataTypePtr element = std::make_shared<DataTypeUInt64>();
62
63return std::make_shared<DataTypeTuple>(DataTypes{element, element});
64}
65
66ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
67{
68auto non_const_arguments = arguments;
69for (auto & argument : non_const_arguments)
70argument.column = argument.column->convertToFullColumnIfConst();
71
72const auto * col_lo = checkAndGetColumn<ColumnUInt64>(non_const_arguments[0].column.get());
73if (!col_lo)
74throw Exception(
75ErrorCodes::ILLEGAL_COLUMN,
76"Illegal type {} of argument {} of function {}. Must be UInt64",
77arguments[0].type->getName(),
781,
79getName());
80const auto & data_low = col_lo->getData();
81
82const auto * col_hi = checkAndGetColumn<ColumnUInt64>(non_const_arguments[1].column.get());
83if (!col_hi)
84throw Exception(
85ErrorCodes::ILLEGAL_COLUMN,
86"Illegal type {} of argument {} of function {}. Must be UInt64",
87arguments[1].type->getName(),
882,
89getName());
90const auto & data_hi = col_hi->getData();
91
92const auto * col_point = checkAndGetColumn<ColumnUInt64>(non_const_arguments[2].column.get());
93if (!col_point)
94throw Exception(
95ErrorCodes::ILLEGAL_COLUMN,
96"Illegal type {} of argument {} of function {}. Must be UInt64",
97arguments[2].type->getName(),
983,
99getName());
100const auto & data_point = col_point->getData();
101
102auto col_res_first = ColumnUInt64::create();
103auto col_res_second = ColumnUInt64::create();
104
105auto & vec_res_first = col_res_first->getData();
106vec_res_first.reserve(input_rows_count);
107
108auto & vec_res_second = col_res_second->getData();
109vec_res_second.reserve(input_rows_count);
110
111for (size_t row = 0; row < input_rows_count; ++row)
112{
113const auto lo = S2CellId(data_low[row]);
114const auto hi = S2CellId(data_hi[row]);
115const auto point = S2CellId(data_point[row]);
116
117S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());
118
119if (!point.is_valid())
120throw 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
124if (!rect.is_valid())
125throw 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
130rect.AddPoint(point.ToPoint());
131
132vec_res_first.emplace_back(S2CellId(rect.lo()).id());
133vec_res_second.emplace_back(S2CellId(rect.hi()).id());
134}
135
136return ColumnTuple::create(Columns{std::move(col_res_first), std::move(col_res_second)});
137}
138
139};
140
141}
142
143REGISTER_FUNCTION(S2RectAdd)
144{
145factory.registerFunction<FunctionS2RectAdd>();
146}
147
148
149}
150
151#endif
152