Skip to content

Commit

Permalink
support s2 index params
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Dec 6, 2021
1 parent 8209ae3 commit ae485d5
Show file tree
Hide file tree
Showing 26 changed files with 162 additions and 165 deletions.
28 changes: 4 additions & 24 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,24 +1712,14 @@ folly::Future<StatusOr<IndexID>> MetaClient::createTagIndex(GraphSpaceID spaceID
std::string tagName,
std::vector<cpp2::IndexFieldDef> fields,
bool ifNotExists,
const std::string* comment,
const int* s2_max_level,
const int* s2_max_cells) {
cpp2::IndexParams indexParams) {
cpp2::CreateTagIndexReq req;
req.set_space_id(spaceID);
req.set_index_name(std::move(indexName));
req.set_tag_name(std::move(tagName));
req.set_fields(std::move(fields));
req.set_if_not_exists(ifNotExists);
if (comment != nullptr) {
req.set_comment(*comment);
}
if (s2_max_level != nullptr) {
req.set_s2_max_level(*s2_max_level);
}
if (s2_max_cells != nullptr) {
req.set_s2_max_cells(*s2_max_cells);
}
req.set_index_params(std::move(indexParams));

folly::Promise<StatusOr<IndexID>> promise;
auto future = promise.getFuture();
Expand Down Expand Up @@ -1832,24 +1822,14 @@ folly::Future<StatusOr<IndexID>> MetaClient::createEdgeIndex(
std::string edgeName,
std::vector<cpp2::IndexFieldDef> fields,
bool ifNotExists,
const std::string* comment,
const int* s2_max_level,
const int* s2_max_cells) {
cpp2::IndexParams indexParams) {
cpp2::CreateEdgeIndexReq req;
req.set_space_id(spaceID);
req.set_index_name(std::move(indexName));
req.set_edge_name(std::move(edgeName));
req.set_fields(std::move(fields));
req.set_if_not_exists(ifNotExists);
if (comment != nullptr) {
req.set_comment(*comment);
}
if (s2_max_level != nullptr) {
req.set_s2_max_level(*s2_max_level);
}
if (s2_max_cells != nullptr) {
req.set_s2_max_cells(*s2_max_cells);
}
req.set_index_params(std::move(indexParams));

folly::Promise<StatusOr<IndexID>> promise;
auto future = promise.getFuture();
Expand Down
30 changes: 14 additions & 16 deletions src/clients/meta/MetaClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,13 @@ class MetaClient {
bool ifExists = false);

// Operations for index
folly::Future<StatusOr<IndexID>> createTagIndex(GraphSpaceID spaceID,
std::string indexName,
std::string tagName,
std::vector<cpp2::IndexFieldDef> fields,
bool ifNotExists = false,
const std::string* comment = nullptr,
const int* s2_max_level = nullptr,
const int* s2_max_cells = nullptr);
folly::Future<StatusOr<IndexID>> createTagIndex(
GraphSpaceID spaceID,
std::string indexName,
std::string tagName,
std::vector<cpp2::IndexFieldDef> fields,
bool ifNotExists = false,
meta::cpp2::IndexParams indexParams = cpp2::IndexParams{});

// Remove the define of tag index
folly::Future<StatusOr<bool>> dropTagIndex(GraphSpaceID spaceId,
Expand All @@ -333,14 +332,13 @@ class MetaClient {

folly::Future<StatusOr<std::vector<cpp2::IndexStatus>>> listTagIndexStatus(GraphSpaceID spaceId);

folly::Future<StatusOr<IndexID>> createEdgeIndex(GraphSpaceID spaceID,
std::string indexName,
std::string edgeName,
std::vector<cpp2::IndexFieldDef> fields,
bool ifNotExists = false,
const std::string* comment = nullptr,
const int* s2_max_level = nullptr,
const int* s2_max_cells = nullptr);
folly::Future<StatusOr<IndexID>> createEdgeIndex(
GraphSpaceID spaceID,
std::string indexName,
std::string edgeName,
std::vector<cpp2::IndexFieldDef> fields,
bool ifNotExists = false,
cpp2::IndexParams indexParams = cpp2::IndexParams{});

// Remove the definition of edge index
folly::Future<StatusOr<bool>> dropEdgeIndex(GraphSpaceID spaceId,
Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/IndexKeyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ class IndexKeyUtils final {
}

static std::vector<std::string> encodeGeography(const nebula::Geography& gg,
const int* s2_max_level,
const int* s2_max_cells) {
const int32_t* s2_max_level,
const int32_t* s2_max_cells) {
geo::RegionCoverParams rc;
if (!s2_max_level) {
rc.maxCellLevel_ = *s2_max_level;
Expand Down
41 changes: 30 additions & 11 deletions src/common/utils/test/IndexKeyUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ TEST(IndexKeyUtilsTest, nullableValue) {
cols.emplace_back(nullCol(folly::stringPrintf("col%ld", j), PropertyType::BOOL));
}
// TODO(jie) Add index key tests for geography
auto raws = IndexKeyUtils::encodeValues(std::move(values), std::move(cols));
auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
auto raws = IndexKeyUtils::encodeValues(std::move(values), indexItem.get());
u_short s = 0xfc00; /* the binary is '11111100 00000000'*/
std::string expected;
expected.append(reinterpret_cast<const char*>(&s), sizeof(u_short));
Expand All @@ -240,7 +242,9 @@ TEST(IndexKeyUtilsTest, nullableValue) {
for (int64_t j = 1; j <= 2; j++) {
cols.emplace_back(nullCol(folly::stringPrintf("col%ld", j), PropertyType::BOOL));
}
auto raws = IndexKeyUtils::encodeValues(std::move(values), std::move(cols));
auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
auto raws = IndexKeyUtils::encodeValues(std::move(values), indexItem.get());
u_short s = 0x4000; /* the binary is '01000000 00000000'*/
std::string expected;
expected.append(reinterpret_cast<const char*>(&s), sizeof(u_short));
Expand All @@ -255,7 +259,9 @@ TEST(IndexKeyUtilsTest, nullableValue) {
for (int64_t j = 1; j <= 2; j++) {
cols.emplace_back(nullCol(folly::stringPrintf("col%ld", j), PropertyType::BOOL));
}
auto raws = IndexKeyUtils::encodeValues(std::move(values), std::move(cols));
auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
auto raws = IndexKeyUtils::encodeValues(std::move(values), indexItem.get());
u_short s = 0x0000; /* the binary is '01000000 00000000'*/
std::string expected;
expected.append(reinterpret_cast<const char*>(&s), sizeof(u_short));
Expand All @@ -269,8 +275,9 @@ TEST(IndexKeyUtilsTest, nullableValue) {
values.emplace_back(Value(NullType::__NULL__));
cols.emplace_back(nullCol(folly::stringPrintf("col%ld", i), PropertyType::INT64));
}

auto raws = IndexKeyUtils::encodeValues(std::move(values), std::move(cols));
auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
auto raws = IndexKeyUtils::encodeValues(std::move(values), indexItem.get());
u_short s = 0xfff0; /* the binary is '11111111 11110000'*/
std::string expected;
expected.append(reinterpret_cast<const char*>(&s), sizeof(u_short));
Expand Down Expand Up @@ -308,7 +315,9 @@ TEST(IndexKeyUtilsTest, nullableValue) {
cols.emplace_back(nullCol(folly::stringPrintf("col_%ld_%ld", i, j), types[j]));
}
}
auto raws = IndexKeyUtils::encodeValues(std::move(values), cols);
auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
auto raws = IndexKeyUtils::encodeValues(std::move(values), indexItem.get());
u_short s = 0xaaa0; /* the binary is '10101010 10100000'*/
std::string expected;
expected.append(reinterpret_cast<const char*>(&s), sizeof(u_short));
Expand All @@ -322,7 +331,9 @@ TEST(IndexKeyUtilsTest, nullableValue) {
values.emplace_back(Value(NullType::__NULL__));
cols.emplace_back(nullCol(folly::stringPrintf("col%ld", i), PropertyType::BOOL));
}
auto raws = IndexKeyUtils::encodeValues(std::move(values), std::move(cols));
auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
auto raws = IndexKeyUtils::encodeValues(std::move(values), indexItem.get());
u_short s = 0xff80; /* the binary is '11111111 10000000'*/
std::string expected;
expected.append(reinterpret_cast<const char*>(&s), sizeof(u_short));
Expand Down Expand Up @@ -418,9 +429,11 @@ TEST(IndexKeyUtilsTest, getValueFromIndexKeyTest) {
};
auto expected = vertices;

auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
std::vector<std::string> indexKeys;
for (auto& row : vertices) {
auto values = IndexKeyUtils::encodeValues(std::move(row.second), cols);
auto values = IndexKeyUtils::encodeValues(std::move(row.second), indexItem.get());
ASSERT_EQ(indexValueSize, values[0].size());
auto keys =
IndexKeyUtils::vertexIndexKeys(vIdLen, partId, indexId, row.first, std::move(values));
Expand All @@ -440,9 +453,11 @@ TEST(IndexKeyUtilsTest, getValueFromIndexKeyTest) {
};
auto expected = edges;

auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
std::vector<std::string> indexKeys;
for (auto& row : edges) {
auto values = IndexKeyUtils::encodeValues(std::move(row.second), cols);
auto values = IndexKeyUtils::encodeValues(std::move(row.second), indexItem.get());
ASSERT_EQ(indexValueSize, values[0].size());

auto keys = IndexKeyUtils::edgeIndexKeys(
Expand Down Expand Up @@ -476,9 +491,11 @@ TEST(IndexKeyUtilsTest, getValueFromIndexKeyTest) {
{"9", {null, null, null, null, null, null}}};
auto expected = vertices;

auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
std::vector<std::string> indexKeys;
for (auto& row : vertices) {
auto values = IndexKeyUtils::encodeValues(std::move(row.second), cols);
auto values = IndexKeyUtils::encodeValues(std::move(row.second), indexItem.get());
ASSERT_EQ(indexValueSize, values[0].size());
auto keys =
IndexKeyUtils::vertexIndexKeys(vIdLen, partId, indexId, row.first, std::move(values));
Expand All @@ -503,9 +520,11 @@ TEST(IndexKeyUtilsTest, getValueFromIndexKeyTest) {
{"9", {null, null, null, null, null, null}}};
auto expected = edges;

auto indexItem = std::make_unique<meta::cpp2::IndexItem>();
indexItem->set_fields(cols);
std::vector<std::string> indexKeys;
for (auto& row : edges) {
auto values = IndexKeyUtils::encodeValues(std::move(row.second), cols);
auto values = IndexKeyUtils::encodeValues(std::move(row.second), indexItem.get());
ASSERT_EQ(indexValueSize, values[0].size());
auto keys = IndexKeyUtils::edgeIndexKeys(
vIdLen, partId, indexId, row.first, 0, row.first, std::move(values));
Expand Down
4 changes: 1 addition & 3 deletions src/graph/executor/maintain/EdgeIndexExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ folly::Future<Status> CreateEdgeIndexExecutor::execute() {
ceiNode->getSchemaName(),
ceiNode->getFields(),
ceiNode->getIfNotExists(),
ceiNode->getComment(),
ceiNode->getS2MaxLevel(),
ceiNode->getS2MaxCells())
ceiNode->getIndexParams())
.via(runner())
.thenValue([ceiNode, spaceId](StatusOr<IndexID> resp) {
if (!resp.ok()) {
Expand Down
4 changes: 1 addition & 3 deletions src/graph/executor/maintain/TagIndexExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ folly::Future<Status> CreateTagIndexExecutor::execute() {
ctiNode->getSchemaName(),
ctiNode->getFields(),
ctiNode->getIfNotExists(),
ctiNode->getComment(),
ctiNode->getS2MaxLevel(),
ctiNode->getS2MaxCells())
ctiNode->getIndexParams())
.via(runner())
.thenValue([ctiNode, spaceId](StatusOr<IndexID> resp) {
if (!resp.ok()) {
Expand Down
17 changes: 8 additions & 9 deletions src/graph/optimizer/rule/GeoPredicateIndexScanBaseRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,17 @@ StatusOr<TransformResult> GeoPredicateIndexScanBaseRule::transform(
DCHECK_EQ(fields.size(), 1); // geo field
auto& geoField = fields.back();
auto& geoColumnTypeDef = geoField.get_type();
bool indexedGeoColumnOnlyHasPoints =
geoColumnTypeDef.geo_shape_ref().has_value() &&
geoColumnTypeDef.geo_shape_ref().value() == meta::cpp2::GeoShape::POINT;
bool isPointColumn = geoColumnTypeDef.geo_shape_ref().has_value() &&
geoColumnTypeDef.geo_shape_ref().value() == meta::cpp2::GeoShape::POINT;

geo::RegionCoverParams rc;
if (indexItem.s2_max_level_ref().has_value()) {
rc.maxCellLevel_ = indexItem.s2_max_level_ref().value();
if (indexItem->s2_max_level_ref().has_value()) {
rc.maxCellLevel_ = indexItem->s2_max_level_ref().value();
}
if (indexItem.s2_max_cells_ref().has_value()) {
rc.maxCellNum_ = indexItem.s2_max_cells_ref().value();
if (indexItem->s2_max_cells_ref().has_value()) {
rc.maxCellNum_ = indexItem->s2_max_cells_ref().value();
}
geo::GeoIndex geoIndex(rc, indexedGeoColumnOnlyHasPoints);
geo::GeoIndex geoIndex(rc, isPointColumn);
std::vector<geo::ScanRange> scanRanges;
if (geoPredicateName == "st_intersects") {
scanRanges = geoIndex.intersects(geog);
Expand All @@ -115,7 +114,7 @@ StatusOr<TransformResult> GeoPredicateIndexScanBaseRule::transform(
}
std::vector<IndexQueryContext> idxCtxs;
idxCtxs.reserve(scanRanges.size());
auto fieldName = fields.back().get_name();
auto fieldName = geoField.get_name();
for (auto& scanRange : scanRanges) {
IndexQueryContext ictx;
auto indexColumnHint = scanRange.toIndexColumnHint();
Expand Down
4 changes: 1 addition & 3 deletions src/graph/planner/plan/Maintain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ std::unique_ptr<PlanNodeDescription> CreateIndexNode::explain() const {
}
addDescription("fields", folly::toJson(util::toJson(fields)), desc.get());
addDescription("ifNotExists", folly::to<std::string>(ifNotExists_), desc.get());
addDescription("comment", folly::toJson(util::toJson(*comment_)), desc.get());
addDescription("s2_max_level", folly::toJson(util::toJson(*s2MaxLevel_)), desc.get());
addDescription("s2_max_cells", folly::toJson(util::toJson(*s2MaxCells_)), desc.get());
addDescription("indexParams", folly::toJson(util::toJson(indexParams_)), desc.get());
return desc;
}

Expand Down
Loading

0 comments on commit ae485d5

Please sign in to comment.