diff --git a/dbms/src/Dictionaries/PolygonDictionaryImplementations.cpp b/dbms/src/Dictionaries/PolygonDictionaryImplementations.cpp index a053bfc84bc..5f3b1d25cf4 100644 --- a/dbms/src/Dictionaries/PolygonDictionaryImplementations.cpp +++ b/dbms/src/Dictionaries/PolygonDictionaryImplementations.cpp @@ -65,7 +65,7 @@ GridPolygonDictionary::GridPolygonDictionary( InputType input_type_, PointType point_type_): IPolygonDictionary(database_, name_, dict_struct_, std::move(source_ptr_), dict_lifetime_, input_type_, point_type_), - grid(kMinIntersections, kMaxDepth,polygons) + grid(kMinIntersections, kMaxDepth, polygons) { std::vector order(polygons.size()); std::iota(order.begin(), order.end(), 0); @@ -116,8 +116,16 @@ SmartPolygonDictionary::SmartPolygonDictionary( const DictionaryLifetime dict_lifetime_, InputType input_type_, PointType point_type_) - : IPolygonDictionary(database_, name_, dict_struct_, std::move(source_ptr_), dict_lifetime_, input_type_, point_type_) + : IPolygonDictionary(database_, name_, dict_struct_, std::move(source_ptr_), dict_lifetime_, input_type_, point_type_), + grid(kMinIntersections, kMaxDepth, polygons) { + std::vector order(polygons.size()); + std::iota(order.begin(), order.end(), 0); + std::sort(order.begin(), order.end(), [&](auto lhs, auto rhs) + { + return areas[lhs] < areas[rhs]; + }); + grid.init(order); auto log = &Logger::get("BucketsPolygonIndex"); buckets.reserve(polygons.size()); for (size_t i = 0; i < polygons.size(); ++i) @@ -141,6 +149,7 @@ std::shared_ptr SmartPolygonDictionary::clone() const bool SmartPolygonDictionary::find(const Point & point, size_t & id) const { + /* bool found = false; double area = 0; for (size_t i = 0; i < polygons.size(); ++i) @@ -158,6 +167,24 @@ bool SmartPolygonDictionary::find(const Point & point, size_t & id) const } } return found; + */ + bool found = false; + auto cell = grid.find(point.get<0>(), point.get<1>()); + if (cell) + { + for (size_t i = 0; i < (cell->polygon_ids).size(); ++i) + { + const auto & candidate = (cell->polygon_ids)[i]; + size_t unused = 0; + if ((cell->is_covered_by)[i] || buckets[candidate].find(point, unused)) + { + found = true; + id = candidate; + break; + } + } + } + return found; } template diff --git a/dbms/src/Dictionaries/PolygonDictionaryImplementations.h b/dbms/src/Dictionaries/PolygonDictionaryImplementations.h index f23dab78502..5f6df4d33d1 100644 --- a/dbms/src/Dictionaries/PolygonDictionaryImplementations.h +++ b/dbms/src/Dictionaries/PolygonDictionaryImplementations.h @@ -77,6 +77,9 @@ private: bool find(const Point & point, size_t & id) const override; std::vector buckets; + GridRoot grid; + static constexpr size_t kMinIntersections = 1; + static constexpr size_t kMaxDepth = 10; }; }