ClickHouse
200 строк · 7.2 Кб
1#include <DataTypes/DataTypesNumber.h>
2#include <Columns/ColumnsNumber.h>
3#include <Columns/ColumnConst.h>
4#include <Common/typeid_cast.h>
5#include <Common/assert_cast.h>
6#include <Functions/IFunction.h>
7#include <Functions/FunctionHelpers.h>
8#include <Functions/FunctionFactory.h>
9#include <base/range.h>
10
11
12namespace DB
13{
14namespace ErrorCodes
15{
16extern const int ILLEGAL_TYPE_OF_ARGUMENT;
17extern const int TOO_MANY_ARGUMENTS_FOR_FUNCTION;
18extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
19extern const int ILLEGAL_COLUMN;
20}
21
22namespace
23{
24
25/**
26* The function checks if a point is in one of ellipses in set.
27* The number of arguments must be 2 + 4*N where N is the number of ellipses.
28* The arguments must be arranged as follows: (x, y, x_0, y_0, a_0, b_0, ..., x_i, y_i, a_i, b_i)
29* All ellipses parameters must be const values;
30*
31* The function first checks bounding box condition.
32* If a point is inside an ellipse's bounding box, the quadratic condition is evaluated.
33*
34* Here we assume that points in one columns are close and are likely to fit in one ellipse,
35* so the last success ellipse index is remembered to check this ellipse first for next point.
36*
37*/
38class FunctionPointInEllipses : public IFunction
39{
40public:
41static constexpr auto name = "pointInEllipses";
42static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionPointInEllipses>(); }
43
44private:
45
46struct Ellipse
47{
48Float64 x;
49Float64 y;
50Float64 a;
51Float64 b;
52};
53
54String getName() const override { return name; }
55
56bool isVariadic() const override { return true; }
57
58bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
59
60size_t getNumberOfArguments() const override { return 0; }
61
62DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
63{
64if (arguments.size() < 6 || arguments.size() % 4 != 2)
65{
66throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
67"Incorrect number of arguments of function {}. "
68"Must be 2 for your point plus 4 * N for ellipses (x_i, y_i, a_i, b_i).", getName());
69}
70
71/// For array on stack, see below.
72if (arguments.size() > 10000)
73{
74throw Exception(ErrorCodes::TOO_MANY_ARGUMENTS_FOR_FUNCTION,
75"Number of arguments of function {} is too large (maximum: 10000).",
76getName());
77}
78
79for (const auto arg_idx : collections::range(0, arguments.size()))
80{
81const auto * arg = arguments[arg_idx].get();
82if (!WhichDataType(arg).isFloat64())
83{
84throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument {} of function {}. "
85"Must be Float64", arg->getName(), std::to_string(arg_idx + 1), getName());
86}
87}
88
89return std::make_shared<DataTypeUInt8>();
90}
91
92ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
93{
94const auto size = input_rows_count;
95
96/// Prepare array of ellipses.
97size_t ellipses_count = (arguments.size() - 2) / 4;
98std::vector<Ellipse> ellipses(ellipses_count);
99
100for (const auto ellipse_idx : collections::range(0, ellipses_count))
101{
102Float64 ellipse_data[4];
103for (const auto idx : collections::range(0, 4))
104{
105size_t arg_idx = 2 + 4 * ellipse_idx + idx;
106const auto * column = arguments[arg_idx].column.get();
107if (const auto * col = checkAndGetColumnConst<ColumnVector<Float64>>(column))
108{
109ellipse_data[idx] = col->getValue<Float64>();
110}
111else
112{
113throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument {} of function {}. "
114"Must be const Float64", column->getName(), std::to_string(arg_idx + 1), getName());
115}
116}
117ellipses[ellipse_idx] = Ellipse{ellipse_data[0], ellipse_data[1], ellipse_data[2], ellipse_data[3]};
118}
119
120int const_cnt = 0;
121for (const auto idx : collections::range(0, 2))
122{
123const auto * column = arguments[idx].column.get();
124if (typeid_cast<const ColumnConst *> (column))
125{
126++const_cnt;
127}
128else if (!typeid_cast<const ColumnVector<Float64> *> (column))
129{
130throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}",
131column->getName(), getName());
132}
133}
134
135const auto * col_x = arguments[0].column.get();
136const auto * col_y = arguments[1].column.get();
137if (const_cnt == 0)
138{
139const auto * col_vec_x = assert_cast<const ColumnVector<Float64> *> (col_x);
140const auto * col_vec_y = assert_cast<const ColumnVector<Float64> *> (col_y);
141
142auto dst = ColumnVector<UInt8>::create();
143auto & dst_data = dst->getData();
144dst_data.resize(size);
145
146size_t start_index = 0;
147for (const auto row : collections::range(0, size))
148{
149dst_data[row] = isPointInEllipses(col_vec_x->getData()[row], col_vec_y->getData()[row], ellipses.data(), ellipses_count, start_index);
150}
151
152return dst;
153}
154else if (const_cnt == 2)
155{
156const auto * col_const_x = assert_cast<const ColumnConst *> (col_x);
157const auto * col_const_y = assert_cast<const ColumnConst *> (col_y);
158size_t start_index = 0;
159UInt8 res = isPointInEllipses(col_const_x->getValue<Float64>(), col_const_y->getValue<Float64>(), ellipses.data(), ellipses_count, start_index);
160return DataTypeUInt8().createColumnConst(size, res);
161}
162else
163{
164throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal types {}, {} of arguments 1, 2 of function {}. "
165"Both must be either const or vector", col_x->getName(), col_y->getName(), getName());
166}
167}
168
169static bool isPointInEllipses(Float64 x, Float64 y, const Ellipse * ellipses, size_t ellipses_count, size_t & start_index)
170{
171size_t index = 0 + start_index;
172for (size_t i = 0; i < ellipses_count; ++i)
173{
174Ellipse el = ellipses[index];
175double p1 = ((x - el.x) / el.a);
176double p2 = ((y - el.y) / el.b);
177if (x <= el.x + el.a && x >= el.x - el.a && y <= el.y + el.b && y >= el.y - el.b /// Bounding box check
178&& p1 * p1 + p2 * p2 <= 1.0) /// Precise check
179{
180start_index = index;
181return true;
182}
183++index;
184if (index == ellipses_count)
185{
186index = 0;
187}
188}
189return false;
190}
191};
192
193}
194
195REGISTER_FUNCTION(PointInEllipses)
196{
197factory.registerFunction<FunctionPointInEllipses>();
198}
199
200}
201