Skip to content

Commit

Permalink
Add more geo tests (vesoft-inc#3293)
Browse files Browse the repository at this point in the history
* add some geo index tests

* add some geo row writer test

* remove common clause license

* add col19 of geo null

* remove debug log

Co-authored-by: cpw <13495049+CPWstatic@users.noreply.github.com>
Co-authored-by: Yee <2520865+yixinglu@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 18, 2021
1 parent 9ebc49d commit 8d548db
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Checks: '-*,clang-diagnostic-*,clang-analyzer-*,-misc-unused-parameters,
google-upgrade-googletest-case,
modernize-avoid-bind,
modernize-concat-nested-namespaces,
-modernize-concat-nested-namespaces,
modernize-deprecated-headers,
modernize-deprecated-ios-base-aliases,
modernize-loop-convert,
Expand Down
2 changes: 1 addition & 1 deletion src/codec/RowWriterV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ std::string RowWriterV2::processOutOfSpace() noexcept {
// Now let's process all strings
for (size_t i = 0; i < schema_->getNumFields(); i++) {
auto field = schema_->field(i);
if (field->type() != PropertyType::STRING) {
if (field->type() != PropertyType::STRING && field->type() != PropertyType::GEOGRAPHY) {
continue;
}

Expand Down
62 changes: 61 additions & 1 deletion src/codec/test/RowWriterV2Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ const DateTime dt = {2020, 2, 20, 10, 30, 45, 0};
const Value sVal("Hello world!");
const Value iVal(64);
const Time t = {10, 30, 45, 0};
// POINT(179.0 89.9)
const Geography geogPoint = Point(Coordinate(179.0, 89.9));
// LINESTRING(0 1, 1 2, 3 7)
const Geography geogLineString =
LineString(std::vector<Coordinate>{Coordinate(0, 1), Coordinate(1, 2), Coordinate(3, 7)});
// POLYGON((-108.7 35.0, -100.0 46.5, -90.7 34.9, -108.7 35.0),
// (-100.1 41.4, -102.9 37.6, -96.8 37.5, -100.1 41.4))
const Geography geogPolygon = Polygon(
std::vector<std::vector<Coordinate>>{std::vector<Coordinate>{Coordinate(-108.7, 35.0),
Coordinate(-100.0, 46.5),
Coordinate(-90.7, 34.9),
Coordinate(-108.7, 35.0)},
std::vector<Coordinate>{Coordinate(-100.1, 41.4),
Coordinate(-102.9, 37.6),
Coordinate(-96.8, 37.5),
Coordinate(-100.1, 41.4)}});

TEST(RowWriterV2, NoDefaultValue) {
SchemaWriter schema(12 /*Schema version*/);
Expand All @@ -45,6 +61,13 @@ TEST(RowWriterV2, NoDefaultValue) {
schema.appendCol("Col13", PropertyType::DATETIME);
schema.appendCol("Col14", PropertyType::INT64, 0, true);
schema.appendCol("Col15", PropertyType::INT32, 0, true);
schema.appendCol(
"Col16", PropertyType::GEOGRAPHY, 0, false, nullptr, meta::cpp2::GeoShape::POINT);
schema.appendCol(
"Col17", PropertyType::GEOGRAPHY, 0, false, nullptr, meta::cpp2::GeoShape::LINESTRING);
schema.appendCol(
"Col18", PropertyType::GEOGRAPHY, 0, false, nullptr, meta::cpp2::GeoShape::POLYGON);
schema.appendCol("Col19", PropertyType::GEOGRAPHY, 0, true, nullptr, meta::cpp2::GeoShape::ANY);

ASSERT_EQ(Value::Type::STRING, sVal.type());
ASSERT_EQ(Value::Type::INT, iVal.type());
Expand All @@ -65,6 +88,10 @@ TEST(RowWriterV2, NoDefaultValue) {
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.set(12, dt));
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.setNull(13));
// Purposely skip the col15
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.set(15, geogPoint));
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.set(16, geogLineString));
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.set(17, geogPolygon));
// Purposely skip the col19
ASSERT_EQ(WriteResult::SUCCEEDED, writer1.finish());

RowWriterV2 writer2(&schema);
Expand All @@ -83,10 +110,16 @@ TEST(RowWriterV2, NoDefaultValue) {
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.set("Col13", dt));
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.setNull("Col14"));
// Purposely skip the col15
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.set("Col16", geogPoint));
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.set("Col17", geogLineString));
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.set("Col18", geogPolygon));
// Purposely skip the col19
ASSERT_EQ(WriteResult::SUCCEEDED, writer2.finish());

std::string encoded1 = std::move(writer1).moveEncodedStr();
std::string encoded2 = writer2.getEncodedStr();
LOG(INFO) << "encoded1, size=" << encoded1.size() << "content=" << folly::hexlify(encoded1);
LOG(INFO) << "encoded2, size=" << encoded2.size() << "content=" << folly::hexlify(encoded2);

auto reader1 = RowReaderWrapper::getRowReader(&schema, encoded1);
auto reader2 = RowReaderWrapper::getRowReader(&schema, encoded2);
Expand Down Expand Up @@ -175,7 +208,7 @@ TEST(RowWriterV2, NoDefaultValue) {
EXPECT_EQ(t, v1.getTime());
EXPECT_EQ(v1, v2);

// Col1333
// Col13
v1 = reader1->getValueByName("Col13");
v2 = reader2->getValueByIndex(12);
EXPECT_EQ(Value::Type::DATETIME, v1.type());
Expand All @@ -193,6 +226,33 @@ TEST(RowWriterV2, NoDefaultValue) {
v2 = reader2->getValueByIndex(14);
EXPECT_EQ(Value::Type::NULLVALUE, v1.type());
EXPECT_EQ(v1, v2);

// Col16
v1 = reader1->getValueByName("Col16");
v2 = reader2->getValueByIndex(15);
EXPECT_EQ(Value::Type::GEOGRAPHY, v1.type());
EXPECT_EQ(geogPoint, v1.getGeography());
EXPECT_EQ(v1, v2);

// Col17
v1 = reader1->getValueByName("Col17");
v2 = reader2->getValueByIndex(16);
EXPECT_EQ(Value::Type::GEOGRAPHY, v1.type());
EXPECT_EQ(geogLineString, v1.getGeography());
EXPECT_EQ(v1, v2);

// Col18
v1 = reader1->getValueByName("Col18");
v2 = reader2->getValueByIndex(17);
EXPECT_EQ(Value::Type::GEOGRAPHY, v1.type());
EXPECT_EQ(geogPolygon, v1.getGeography());
EXPECT_EQ(v1, v2);

// Col19
v1 = reader1->getValueByName("Col19");
v2 = reader2->getValueByIndex(18);
EXPECT_EQ(Value::Type::NULLVALUE, v1.type());
EXPECT_EQ(v1, v2);
}

TEST(RowWriterV2, WithDefaultValue) {
Expand Down
23 changes: 22 additions & 1 deletion src/common/geo/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,29 @@ nebula_add_test(
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:time_utils_obj>
$<TARGET_OBJECTS:time_obj>
$<TARGET_OBJECTS:fs_obj>
LIBRARIES
gtest
gtest_main
)

nebula_add_test(
NAME
geo_index_test
SOURCES
GeoIndexTest.cpp
OBJECTS
$<TARGET_OBJECTS:wkt_wkb_io_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:geo_index_obj>
$<TARGET_OBJECTS:storage_thrift_obj>
$<TARGET_OBJECTS:meta_thrift_obj>
$<TARGET_OBJECTS:common_thrift_obj>
LIBRARIES
${ROCKSDB_LIBRARIES}
${THRIFT_LIBRARIES}
${PROXYGEN_LIBRARIES}
wangle
gtest
gtest_main
)
184 changes: 184 additions & 0 deletions src/common/geo/test/GeoIndexTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include <gtest/gtest.h>

#include <unordered_set>

#include "common/base/Base.h"
#include "common/geo/GeoIndex.h"

namespace nebula {
namespace geo {

std::vector<uint64_t> toUint64Vector(std::vector<int64_t> expect) {
auto reinterpretInt64AsUint64 = [](int64_t i) -> uint64_t {
const char* c = reinterpret_cast<const char*>(&i);
uint64_t u = *reinterpret_cast<const uint64_t*>(c);
return u;
};

std::vector<uint64_t> transformedExpect;
transformedExpect.reserve(expect.size());
for (int64_t i : expect) {
transformedExpect.push_back(reinterpretInt64AsUint64(i));
}
return transformedExpect;
}

// The tested wkt data is generated by https://clydedacruz.github.io/openstreetmap-wkt-playground/
TEST(indexCells, point) {
geo::RegionCoverParams rc;
geo::GeoIndex geoIndex(rc);
{
auto point = Geography::fromWKT("POINT(1.0 1.0)").value();
auto cells = geoIndex.indexCells(point);
EXPECT_EQ(toUint64Vector({1153277837650709461}), cells);
}
{
auto point = Geography::fromWKT("POINT(179.9 -89.999)").value();
auto cells = geoIndex.indexCells(point);
EXPECT_EQ(toUint64Vector({-5764607523209916331}), cells);
}
{
auto point = Geography::fromWKT("POINT(0.0 0.0)").value();
auto cells = geoIndex.indexCells(point);
EXPECT_EQ(toUint64Vector({1152921504606846977}), cells);
}
{
auto point = Geography::fromWKT("POINT(-36.843143 79.9999999)").value();
auto cells = geoIndex.indexCells(point);
EXPECT_EQ(toUint64Vector({5738492864430648919}), cells);
}
}

TEST(indexCells, lineString) {
geo::RegionCoverParams rc;
geo::GeoIndex geoIndex(rc);
{
auto line = Geography::fromWKT("LINESTRING(1.0 1.0, 2.0 2.0)").value();
auto cells = geoIndex.indexCells(line);
std::vector<int64_t> expect{
1153290940513779712,
1154047404446580736,
1154064996699734016,
1154135365443911680,
1154164685843464192,
1154328879221964800,
1154346471676444672,
};
EXPECT_EQ(toUint64Vector(expect), cells);
}
{
auto line = Geography::fromWKT(
"LINESTRING(-100.03601074218751 40.74400563300867,-96.96516036987305 "
"39.60634945766583,-91.84398651123048 39.706526341505366)")
.value();
auto cells = geoIndex.indexCells(line);
std::vector<int64_t> expect{-8676255050873438208,
-8675903207152549888,
-8674214357292285952,
-8665770107990966272,
-8665207158037544960,
-8664644208084123648,
-8664081258130702336,
-8656692539992047616};
EXPECT_EQ(toUint64Vector(expect), cells);
}
{
auto line = Geography::fromWKT(
"LINESTRING(-109.18024063110352 40.96952973563833,-102.11740493774414 "
"40.98832114106014,-102.00119018554688 37.07120386611709,-108.97098541259767 "
"37.00392356248513,-109.09063339233398 40.94178643285866)")
.value();
auto cells = geoIndex.indexCells(line);
std::vector<int64_t> expect{-8715591178868752384,
-8714183803985199104,
-8712494954124935168,
-8702080379986640896,
-8699828580172955648,
-8693407432266743808,
-8688569581104529408,
-8686317781290844160};
EXPECT_EQ(toUint64Vector(expect), cells);
}
}
TEST(indexCells, polygon) {
geo::RegionCoverParams rc;
geo::GeoIndex geoIndex(rc);
{
auto polygon = Geography::fromWKT(
"POLYGON((-105.59286117553711 43.12955341892069,-98.76176834106447 "
"44.11877181138391,-93.97396087646486 "
"38.023348535033705,-105.59286117553711 43.12955341892069))")
.value();
auto cells = geoIndex.indexCells(polygon);
std::vector<int64_t> expect{-8690821380918214656,
-8686317781290844160,
-8684065981477158912,
-8678436481942945792,
-8665770107990966272,
-8665207158037544960,
-8664644208084123648,
-8662955358223859712};
EXPECT_EQ(toUint64Vector(expect), cells);
}
{
auto polygon =
Geography::fromWKT(
"POLYGON((-107.24699020385744 45.21638951846552,-91.75283432006836 "
"46.158312926461235,-90.07295608520508 35.17914020576748,-109.77504730224612 "
"38.65334327823746,-107.24699020385744 45.21638951846552))")
.value();
auto cells = geoIndex.indexCells(polygon);
std::vector<int64_t> expect{5958262307011166208,
5967269506265907200,
5994291104030130176,
6002172403378028544,
-8714465278961909760,
-8702080379986640896,
-8696450880452427776,
-8687443681197686784,
-8678436481942945792,
-8669429282688204800,
-8660422083433463808,
-8651414884178722816};
EXPECT_EQ(toUint64Vector(expect), cells);
}
{
auto polygon =
Geography::fromWKT(
"POLYGON((-107.17094421386722 51.23698687887105,-100.24475097656253 "
"50.57407993312597,-101.63520812988283 47.57050358015326,-108.1597137451172 "
"47.614032638527846,-107.17094421386722 51.23698687887105),(-106.00682258605956 "
"50.35416859141216,-105.23014068603514 50.212503875989455,-105.55715560913085 "
"49.755319847594194,-106.36962890624999 49.95817799043337,-106.00682258605956 "
"50.35416859141216),(-103.90560150146483 49.21126151433475,-102.1109676361084 "
"49.32232483567492,-102.99759864807127 48.52160729809822,-103.90560150146483 "
"49.21126151433475))")
.value();
auto cells = geoIndex.indexCells(polygon);
std::vector<int64_t> expect{5969732412312125440,
5971192563753811968,
5971491630916567040,
5972899005800120320,
5986409804682231808,
5988661604495917056,
5990913404309602304,
5997668803750658048};
EXPECT_EQ(toUint64Vector(expect), cells);
}
}

} // namespace geo
} // namespace nebula

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
folly::init(&argc, &argv, true);
google::SetStderrLogging(google::INFO);

return RUN_ALL_TESTS();
}

0 comments on commit 8d548db

Please sign in to comment.