From e7e7b0cefb3712efea6e4571749ed285c54ec30d Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Sat, 29 Aug 2026 14:29:31 -0400 Subject: [PATCH 1/6] fix(c++): handle nonzero vertex start indices Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/src/graphar/high-level/vertices_builder.h | 17 ++++----- cpp/test/test_builder.cc | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/cpp/src/graphar/high-level/vertices_builder.h b/cpp/src/graphar/high-level/vertices_builder.h index 4ddccada7..6944d02ff 100644 --- a/cpp/src/graphar/high-level/vertices_builder.h +++ b/cpp/src/graphar/high-level/vertices_builder.h @@ -338,17 +338,14 @@ class VerticesBuilder { ValidateLevel validate_level = ValidateLevel::default_validate) { // validate GAR_RETURN_NOT_OK(validate(v, index, validate_level)); - // add a vertex - if (index == -1) { - v.SetId(vertices_.size()); - vertices_.push_back(v); - } else { - v.SetId(index); - if (index >= static_cast(vertices_.size())) { - vertices_.resize(index + 1); - } - vertices_[index] = v; + const IdType local_index = index == -1 + ? static_cast(vertices_.size()) + : index - start_vertex_index_; + v.SetId(start_vertex_index_ + local_index); + if (local_index >= static_cast(vertices_.size())) { + vertices_.resize(local_index + 1); } + vertices_[local_index] = v; num_vertices_++; return Status::OK(); } diff --git a/cpp/test/test_builder.cc b/cpp/test/test_builder.cc index 16ba5a218..a650d80a2 100644 --- a/cpp/test/test_builder.cc +++ b/cpp/test/test_builder.cc @@ -101,6 +101,42 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") { REQUIRE(indexed_vertex.GetId() == 7); REQUIRE(indexed_vertex.Empty()); + SECTION("nonzero start vertex index") { + const IdType nonzero_start_index = vertex_info->GetChunkSize(); + auto nonzero_builder = + builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero/", + nonzero_start_index) + .value(); + nonzero_builder->SetValidateLevel(ValidateLevel::weak_validate); + + builder::Vertex auto_indexed_vertex; + auto_indexed_vertex.AddProperty("id", int64_t{10}); + REQUIRE(nonzero_builder->AddVertex(auto_indexed_vertex).ok()); + REQUIRE(auto_indexed_vertex.GetId() == nonzero_start_index); + + builder::Vertex explicitly_indexed_vertex; + explicitly_indexed_vertex.AddProperty("id", int64_t{11}); + REQUIRE(nonzero_builder + ->AddVertex(explicitly_indexed_vertex, nonzero_start_index + 1) + .ok()); + REQUIRE(explicitly_indexed_vertex.GetId() == nonzero_start_index + 1); + REQUIRE(nonzero_builder->Dump().ok()); + + auto nonzero_chunk = + "/tmp/nonzero/vertex/person/id/chunk" + + std::to_string(nonzero_start_index / vertex_info->GetChunkSize()); + std::unique_ptr nonzero_reader; + REQUIRE(graphar::util::OpenParquetArrowReader( + nonzero_chunk, arrow::default_memory_pool(), &nonzero_reader) + .ok()); + auto nonzero_table = ReadParquetTable(nonzero_reader.get()).value(); + REQUIRE(nonzero_table->num_rows() == 2); + auto vertex_index_array = std::static_pointer_cast( + nonzero_table->GetColumnByName("_graphArVertexIndex")->chunk(0)); + REQUIRE(vertex_index_array->Value(0) == nonzero_start_index); + REQUIRE(vertex_index_array->Value(1) == nonzero_start_index + 1); + } + // add vertices std::ifstream fp(test_data_dir + "/ldbc_sample/person_0_0.csv"); std::string line; From 001b771cf3e2c6b50320bac0a59273d5d25d3bc4 Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Sat, 29 Aug 2026 14:49:10 -0400 Subject: [PATCH 2/6] fix(c++): reject vertex indices below start Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/src/graphar/high-level/vertices_builder.h | 5 +++++ cpp/test/test_builder.cc | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/cpp/src/graphar/high-level/vertices_builder.h b/cpp/src/graphar/high-level/vertices_builder.h index 6944d02ff..b2c8ef9be 100644 --- a/cpp/src/graphar/high-level/vertices_builder.h +++ b/cpp/src/graphar/high-level/vertices_builder.h @@ -341,6 +341,11 @@ class VerticesBuilder { const IdType local_index = index == -1 ? static_cast(vertices_.size()) : index - start_vertex_index_; + if (local_index < 0) { + return Status::IndexError("The vertex index ", index, + " is smaller than the start index ", + start_vertex_index_); + } v.SetId(start_vertex_index_ + local_index); if (local_index >= static_cast(vertices_.size())) { vertices_.resize(local_index + 1); diff --git a/cpp/test/test_builder.cc b/cpp/test/test_builder.cc index a650d80a2..46c0342fe 100644 --- a/cpp/test/test_builder.cc +++ b/cpp/test/test_builder.cc @@ -103,6 +103,17 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") { SECTION("nonzero start vertex index") { const IdType nonzero_start_index = vertex_info->GetChunkSize(); + auto no_validate_builder = + builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero-no-validate/", + nonzero_start_index) + .value(); + builder::Vertex below_start_vertex; + below_start_vertex.AddProperty("id", int64_t{9}); + REQUIRE( + no_validate_builder->AddVertex(below_start_vertex, 5).IsIndexError()); + REQUIRE_FALSE(below_start_vertex.HasId()); + REQUIRE(no_validate_builder->GetNum() == 0); + auto nonzero_builder = builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero/", nonzero_start_index) From 6288d8066c5eb686098da7017d1b71c94b534368 Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Sat, 29 Aug 2026 15:31:58 -0400 Subject: [PATCH 3/6] fix(c++): enforce vertex storage invariants Signed-off-by: 1fanwang <1fannnw@gmail.com> --- .../graphar/high-level/vertices_builder.cc | 23 +++++++++---------- cpp/src/graphar/high-level/vertices_builder.h | 5 ---- cpp/test/test_builder.cc | 10 ++++++++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/cpp/src/graphar/high-level/vertices_builder.cc b/cpp/src/graphar/high-level/vertices_builder.cc index 000938314..2169f5ee1 100644 --- a/cpp/src/graphar/high-level/vertices_builder.cc +++ b/cpp/src/graphar/high-level/vertices_builder.cc @@ -35,18 +35,6 @@ Status VerticesBuilder::validate(const Vertex& v, IdType index, if (validate_level == ValidateLevel::default_validate) { validate_level = validate_level_; } - // no validate - if (validate_level == ValidateLevel::no_validate) { - return Status::OK(); - } - - // weak validate - // can not add new vertices after dumping - if (is_saved_) { - return Status::Invalid( - "The vertices builder has been saved, can not add " - "new vertices any more"); - } // the start vertex index must be aligned with the chunk size if (start_vertex_index_ % vertex_info_->GetChunkSize() != 0) { return Status::IndexError("The start vertex index ", start_vertex_index_, @@ -59,7 +47,18 @@ Status VerticesBuilder::validate(const Vertex& v, IdType index, " is smaller than the start index ", start_vertex_index_); } + // no validate + if (validate_level == ValidateLevel::no_validate) { + return Status::OK(); + } + // weak validate + // can not add new vertices after dumping + if (is_saved_) { + return Status::Invalid( + "The vertices builder has been saved, can not add " + "new vertices any more"); + } // strong validate if (validate_level == ValidateLevel::strong_validate) { for (auto& property : v.GetProperties()) { diff --git a/cpp/src/graphar/high-level/vertices_builder.h b/cpp/src/graphar/high-level/vertices_builder.h index b2c8ef9be..6944d02ff 100644 --- a/cpp/src/graphar/high-level/vertices_builder.h +++ b/cpp/src/graphar/high-level/vertices_builder.h @@ -341,11 +341,6 @@ class VerticesBuilder { const IdType local_index = index == -1 ? static_cast(vertices_.size()) : index - start_vertex_index_; - if (local_index < 0) { - return Status::IndexError("The vertex index ", index, - " is smaller than the start index ", - start_vertex_index_); - } v.SetId(start_vertex_index_ + local_index); if (local_index >= static_cast(vertices_.size())) { vertices_.resize(local_index + 1); diff --git a/cpp/test/test_builder.cc b/cpp/test/test_builder.cc index 46c0342fe..e81ba90ed 100644 --- a/cpp/test/test_builder.cc +++ b/cpp/test/test_builder.cc @@ -114,6 +114,16 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") { REQUIRE_FALSE(below_start_vertex.HasId()); REQUIRE(no_validate_builder->GetNum() == 0); + auto unaligned_builder = builder::VerticesBuilder::Make( + vertex_info, "/tmp/unaligned-no-validate/", + nonzero_start_index + 50) + .value(); + builder::Vertex unaligned_vertex; + unaligned_vertex.AddProperty("id", int64_t{10}); + REQUIRE(unaligned_builder->AddVertex(unaligned_vertex).IsIndexError()); + REQUIRE_FALSE(unaligned_vertex.HasId()); + REQUIRE(unaligned_builder->GetNum() == 0); + auto nonzero_builder = builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero/", nonzero_start_index) From e23c20afb94a5d1f373a8f81c7c6da2e6d4ccc7f Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Sat, 29 Aug 2026 15:55:03 -0400 Subject: [PATCH 4/6] fix(c++): reject negative vertex start indices Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/src/graphar/high-level/vertices_builder.cc | 4 ++++ cpp/test/test_builder.cc | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/cpp/src/graphar/high-level/vertices_builder.cc b/cpp/src/graphar/high-level/vertices_builder.cc index 2169f5ee1..cdd9280f5 100644 --- a/cpp/src/graphar/high-level/vertices_builder.cc +++ b/cpp/src/graphar/high-level/vertices_builder.cc @@ -35,6 +35,10 @@ Status VerticesBuilder::validate(const Vertex& v, IdType index, if (validate_level == ValidateLevel::default_validate) { validate_level = validate_level_; } + if (start_vertex_index_ < 0) { + return Status::IndexError("The start vertex index ", start_vertex_index_, + " is smaller than 0"); + } // the start vertex index must be aligned with the chunk size if (start_vertex_index_ % vertex_info_->GetChunkSize() != 0) { return Status::IndexError("The start vertex index ", start_vertex_index_, diff --git a/cpp/test/test_builder.cc b/cpp/test/test_builder.cc index e81ba90ed..e40bdcda4 100644 --- a/cpp/test/test_builder.cc +++ b/cpp/test/test_builder.cc @@ -124,6 +124,17 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") { REQUIRE_FALSE(unaligned_vertex.HasId()); REQUIRE(unaligned_builder->GetNum() == 0); + auto negative_start_builder = + builder::VerticesBuilder::Make( + vertex_info, "/tmp/negative-no-validate/", -nonzero_start_index) + .value(); + builder::Vertex negative_start_vertex; + negative_start_vertex.AddProperty("id", int64_t{10}); + REQUIRE(negative_start_builder->AddVertex(negative_start_vertex) + .IsIndexError()); + REQUIRE_FALSE(negative_start_vertex.HasId()); + REQUIRE(negative_start_builder->GetNum() == 0); + auto nonzero_builder = builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero/", nonzero_start_index) From 5ec6e5ed41e3ead0c38e6963632d443f962c9fdb Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Sat, 29 Aug 2026 16:32:07 -0400 Subject: [PATCH 5/6] test(c++): prioritize vertex offset regression Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/test/test_builder.cc | 64 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/cpp/test/test_builder.cc b/cpp/test/test_builder.cc index e40bdcda4..db415d9bb 100644 --- a/cpp/test/test_builder.cc +++ b/cpp/test/test_builder.cc @@ -103,38 +103,6 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") { SECTION("nonzero start vertex index") { const IdType nonzero_start_index = vertex_info->GetChunkSize(); - auto no_validate_builder = - builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero-no-validate/", - nonzero_start_index) - .value(); - builder::Vertex below_start_vertex; - below_start_vertex.AddProperty("id", int64_t{9}); - REQUIRE( - no_validate_builder->AddVertex(below_start_vertex, 5).IsIndexError()); - REQUIRE_FALSE(below_start_vertex.HasId()); - REQUIRE(no_validate_builder->GetNum() == 0); - - auto unaligned_builder = builder::VerticesBuilder::Make( - vertex_info, "/tmp/unaligned-no-validate/", - nonzero_start_index + 50) - .value(); - builder::Vertex unaligned_vertex; - unaligned_vertex.AddProperty("id", int64_t{10}); - REQUIRE(unaligned_builder->AddVertex(unaligned_vertex).IsIndexError()); - REQUIRE_FALSE(unaligned_vertex.HasId()); - REQUIRE(unaligned_builder->GetNum() == 0); - - auto negative_start_builder = - builder::VerticesBuilder::Make( - vertex_info, "/tmp/negative-no-validate/", -nonzero_start_index) - .value(); - builder::Vertex negative_start_vertex; - negative_start_vertex.AddProperty("id", int64_t{10}); - REQUIRE(negative_start_builder->AddVertex(negative_start_vertex) - .IsIndexError()); - REQUIRE_FALSE(negative_start_vertex.HasId()); - REQUIRE(negative_start_builder->GetNum() == 0); - auto nonzero_builder = builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero/", nonzero_start_index) @@ -167,6 +135,38 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") { nonzero_table->GetColumnByName("_graphArVertexIndex")->chunk(0)); REQUIRE(vertex_index_array->Value(0) == nonzero_start_index); REQUIRE(vertex_index_array->Value(1) == nonzero_start_index + 1); + + auto no_validate_builder = + builder::VerticesBuilder::Make(vertex_info, "/tmp/nonzero-no-validate/", + nonzero_start_index) + .value(); + builder::Vertex below_start_vertex; + below_start_vertex.AddProperty("id", int64_t{9}); + REQUIRE( + no_validate_builder->AddVertex(below_start_vertex, 5).IsIndexError()); + REQUIRE_FALSE(below_start_vertex.HasId()); + REQUIRE(no_validate_builder->GetNum() == 0); + + auto unaligned_builder = builder::VerticesBuilder::Make( + vertex_info, "/tmp/unaligned-no-validate/", + nonzero_start_index + 50) + .value(); + builder::Vertex unaligned_vertex; + unaligned_vertex.AddProperty("id", int64_t{10}); + REQUIRE(unaligned_builder->AddVertex(unaligned_vertex).IsIndexError()); + REQUIRE_FALSE(unaligned_vertex.HasId()); + REQUIRE(unaligned_builder->GetNum() == 0); + + auto negative_start_builder = + builder::VerticesBuilder::Make( + vertex_info, "/tmp/negative-no-validate/", -nonzero_start_index) + .value(); + builder::Vertex negative_start_vertex; + negative_start_vertex.AddProperty("id", int64_t{10}); + REQUIRE(negative_start_builder->AddVertex(negative_start_vertex) + .IsIndexError()); + REQUIRE_FALSE(negative_start_vertex.HasId()); + REQUIRE(negative_start_builder->GetNum() == 0); } // add vertices From 6113123c4d9423ca75773d36e7bbcbceded6b5ac Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Sat, 29 Aug 2026 16:50:55 -0400 Subject: [PATCH 6/6] test(c++): check parquet read result Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/test/test_builder.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cpp/test/test_builder.cc b/cpp/test/test_builder.cc index db415d9bb..ec686ca6c 100644 --- a/cpp/test/test_builder.cc +++ b/cpp/test/test_builder.cc @@ -129,7 +129,9 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") { REQUIRE(graphar::util::OpenParquetArrowReader( nonzero_chunk, arrow::default_memory_pool(), &nonzero_reader) .ok()); - auto nonzero_table = ReadParquetTable(nonzero_reader.get()).value(); + auto maybe_nonzero_table = ReadParquetTable(nonzero_reader.get()); + REQUIRE(maybe_nonzero_table.ok()); + auto nonzero_table = maybe_nonzero_table.ValueOrDie(); REQUIRE(nonzero_table->num_rows() == 2); auto vertex_index_array = std::static_pointer_cast( nonzero_table->GetColumnByName("_graphArVertexIndex")->chunk(0));