3232
3333#ifdef TILEDB_SERIALIZATION
3434
35+ #include " test/support/src/helpers.h"
3536#include " test/support/src/mem_helpers.h"
3637#include " test/support/tdb_catch.h"
3738#include " tiledb/api/c_api/buffer/buffer_api_internal.h"
4142#include " tiledb/sm/c_api/tiledb_serialization.h"
4243#include " tiledb/sm/c_api/tiledb_struct_def.h"
4344#include " tiledb/sm/cpp_api/tiledb"
45+ #include " tiledb/sm/cpp_api/tiledb_experimental"
4446#include " tiledb/sm/crypto/encryption_key.h"
4547#include " tiledb/sm/enums/array_type.h"
4648#include " tiledb/sm/enums/encryption_type.h"
@@ -67,6 +69,7 @@ struct RequestHandlerFx {
6769 Config cfg_;
6870 Context ctx_;
6971 EncryptionKey enc_key_;
72+ shared_ptr<ArraySchema> schema_;
7073};
7174
7275struct HandleLoadArraySchemaRequestFx : RequestHandlerFx {
@@ -84,6 +87,8 @@ struct HandleLoadArraySchemaRequestFx : RequestHandlerFx {
8487
8588 shared_ptr<const Enumeration> create_string_enumeration (
8689 std::string name, std::vector<std::string>& values);
90+
91+ shared_ptr<ArraySchema> schema_add_attribute (const std::string& attr_name);
8792};
8893
8994struct HandleQueryPlanRequestFx : RequestHandlerFx {
@@ -131,6 +136,13 @@ TEST_CASE_METHOD(
131136 auto schema = std::get<0 >(schema_response);
132137 REQUIRE (schema->has_enumeration (" enmr" ));
133138 REQUIRE (schema->get_loaded_enumeration_names ().size () == 0 );
139+ tiledb::test::schema_equiv (*schema, *schema_);
140+
141+ // We did not evolve the schema so there should only be one.
142+ auto all_schemas = std::get<1 >(schema_response);
143+ REQUIRE (all_schemas.size () == 1 );
144+ tiledb::test::schema_equiv (
145+ *all_schemas.find (schema->name ())->second , *schema_);
134146}
135147
136148TEST_CASE_METHOD (
@@ -147,6 +159,42 @@ TEST_CASE_METHOD(
147159 REQUIRE (schema->get_loaded_enumeration_names ().size () == 1 );
148160 REQUIRE (schema->get_loaded_enumeration_names ()[0 ] == " enmr" );
149161 REQUIRE (schema->get_enumeration (" enmr" ) != nullptr );
162+ tiledb::test::schema_equiv (*schema, *schema_);
163+
164+ // We did not evolve the schema so there should only be one.
165+ auto all_schemas = std::get<1 >(schema_response);
166+ REQUIRE (all_schemas.size () == 1 );
167+ tiledb::test::schema_equiv (
168+ *all_schemas.find (schema->name ())->second , *schema_);
169+ }
170+
171+ TEST_CASE_METHOD (
172+ HandleLoadArraySchemaRequestFx,
173+ " tiledb_handle_load_array_schema_request - multiple schemas" ,
174+ " [request_handler][load_array_schema][schema-evolution]" ) {
175+ auto stype = GENERATE (SerializationType::JSON, SerializationType::CAPNP);
176+
177+ create_array ();
178+
179+ std::vector<shared_ptr<ArraySchema>> all_schemas{schema_};
180+ all_schemas.push_back (schema_add_attribute (" b" ));
181+ all_schemas.push_back (schema_add_attribute (" c" ));
182+ all_schemas.push_back (schema_add_attribute (" d" ));
183+
184+ auto schema_response =
185+ call_handler (serialization::LoadArraySchemaRequest (cfg_), stype);
186+ auto schema = std::get<0 >(schema_response);
187+ REQUIRE (schema->has_enumeration (" enmr" ));
188+ REQUIRE (schema->get_loaded_enumeration_names ().size () == 1 );
189+ REQUIRE (schema->get_loaded_enumeration_names ()[0 ] == " enmr" );
190+ REQUIRE (schema->get_enumeration (" enmr" ) != nullptr );
191+ // The latest schema should be equal to the last applied evolution.
192+ tiledb::test::schema_equiv (*schema, *all_schemas.back ());
193+
194+ // Validate all array schemas returned from the request.
195+ for (int i = 0 ; const auto & s : std::get<1 >(schema_response)) {
196+ tiledb::test::schema_equiv (*s.second , *all_schemas[i++]);
197+ }
150198}
151199
152200TEST_CASE_METHOD (
@@ -353,7 +401,9 @@ TEST_CASE_METHOD(
353401RequestHandlerFx::RequestHandlerFx (const std::string uri)
354402 : memory_tracker_(tiledb::test::create_test_memory_tracker())
355403 , uri_(uri)
356- , ctx_(cfg_) {
404+ , ctx_(cfg_)
405+ , schema_(make_shared<ArraySchema>(
406+ ArrayType::DENSE, ctx_.resources().ephemeral_memory_tracker())) {
357407 delete_array ();
358408 throw_if_not_ok (enc_key_.set_key (EncryptionType::NO_ENCRYPTION, nullptr , 0 ));
359409}
@@ -412,9 +462,29 @@ HandleLoadArraySchemaRequestFx::create_string_enumeration(
412462 tiledb::test::create_test_memory_tracker ());
413463}
414464
465+ shared_ptr<ArraySchema> HandleLoadArraySchemaRequestFx::schema_add_attribute (
466+ const std::string& attr_name) {
467+ tiledb::Context ctx;
468+ tiledb::ArraySchemaEvolution ase (ctx);
469+ auto attr = tiledb::Attribute::create<int32_t >(ctx, attr_name);
470+ ase.add_attribute (attr);
471+ auto evolved = ase.ptr ()->array_schema_evolution_ ->evolve_schema (schema_);
472+ // Apply the schema evolution.
473+ Array::evolve_array_schema (
474+ this ->ctx_ .resources (),
475+ this ->uri_ ,
476+ ase.ptr ()->array_schema_evolution_ ,
477+ this ->enc_key_ );
478+
479+ // Update the original schema.
480+ schema_ = evolved;
481+ // Return the schema for validation.
482+ return evolved;
483+ }
484+
415485shared_ptr<ArraySchema> HandleLoadArraySchemaRequestFx::create_schema () {
416486 // Create a schema to serialize
417- auto schema =
487+ schema_ =
418488 make_shared<ArraySchema>(HERE (), ArrayType::SPARSE, memory_tracker_);
419489 auto dim =
420490 make_shared<Dimension>(HERE (), " dim1" , Datatype::INT32, memory_tracker_);
@@ -423,17 +493,17 @@ shared_ptr<ArraySchema> HandleLoadArraySchemaRequestFx::create_schema() {
423493
424494 auto dom = make_shared<Domain>(HERE (), memory_tracker_);
425495 throw_if_not_ok (dom->add_dimension (dim));
426- throw_if_not_ok (schema ->set_domain (dom));
496+ throw_if_not_ok (schema_ ->set_domain (dom));
427497
428498 std::vector<std::string> values = {" pig" , " cow" , " chicken" , " dog" , " cat" };
429499 auto enmr = create_string_enumeration (" enmr" , values);
430- schema ->add_enumeration (enmr);
500+ schema_ ->add_enumeration (enmr);
431501
432502 auto attr = make_shared<Attribute>(HERE (), " attr" , Datatype::INT32);
433503 attr->set_enumeration_name (" enmr" );
434- throw_if_not_ok (schema ->add_attribute (attr));
504+ throw_if_not_ok (schema_ ->add_attribute (attr));
435505
436- return schema ;
506+ return schema_ ;
437507}
438508
439509std::tuple<
0 commit comments