ClickHouse
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
15namespace DB
16{
17namespace ErrorCodes
18{
19extern const int ILLEGAL_TYPE_OF_ARGUMENT;
20extern const int BAD_ARGUMENTS;
21extern const int ILLEGAL_COLUMN;
22
23}
24
25namespace
26{
27
28class FunctionS2RectContains : public IFunction
29{
30public:
31static constexpr auto name = "s2RectContains";
32
33static FunctionPtr create(ContextPtr)
34{
35return std::make_shared<FunctionS2RectContains>();
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 i = 0; i < getNumberOfArguments(); ++i)
52{
53const auto * arg = arguments[i].get();
54if (!WhichDataType(arg).isUInt64())
55throw Exception(
56ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
57"Illegal type {} of argument {} of function {}. Must be UInt64",
58arg->getName(), i, getName());
59}
60
61return std::make_shared<DataTypeUInt8>();
62}
63
64ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
65{
66auto non_const_arguments = arguments;
67for (auto & argument : non_const_arguments)
68argument.column = argument.column->convertToFullColumnIfConst();
69
70const auto * col_lo = checkAndGetColumn<ColumnUInt64>(non_const_arguments[0].column.get());
71if (!col_lo)
72throw Exception(
73ErrorCodes::ILLEGAL_COLUMN,
74"Illegal type {} of argument {} of function {}. Must be UInt64",
75arguments[0].type->getName(),
761,
77getName());
78const auto & data_low = col_lo->getData();
79
80const auto * col_hi = checkAndGetColumn<ColumnUInt64>(non_const_arguments[1].column.get());
81if (!col_hi)
82throw Exception(
83ErrorCodes::ILLEGAL_COLUMN,
84"Illegal type {} of argument {} of function {}. Must be UInt64",
85arguments[1].type->getName(),
862,
87getName());
88const auto & data_hi = col_hi->getData();
89
90const auto * col_point = checkAndGetColumn<ColumnUInt64>(non_const_arguments[2].column.get());
91if (!col_point)
92throw Exception(
93ErrorCodes::ILLEGAL_COLUMN,
94"Illegal type {} of argument {} of function {}. Must be UInt64",
95arguments[2].type->getName(),
963,
97getName());
98const auto & data_point = col_point->getData();
99
100auto dst = ColumnVector<UInt8>::create();
101auto & dst_data = dst->getData();
102dst_data.reserve(input_rows_count);
103
104for (size_t row = 0; row < input_rows_count; ++row)
105{
106const auto lo = S2CellId(data_low[row]);
107const auto hi = S2CellId(data_hi[row]);
108const auto point = S2CellId(data_point[row]);
109
110S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());
111
112if (!point.is_valid())
113throw 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
117if (!rect.is_valid())
118throw 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
123dst_data.emplace_back(rect.Contains(point.ToLatLng()));
124}
125
126return dst;
127}
128
129};
130
131}
132
133REGISTER_FUNCTION(S2RectContains)
134{
135factory.registerFunction<FunctionS2RectContains>();
136}
137
138
139}
140
141#endif
142