Skip to content

Commit c424e77

Browse files
committed
Check all array schemas.
1 parent afa9175 commit c424e77

File tree

4 files changed

+108
-32
lines changed

4 files changed

+108
-32
lines changed

test/src/unit-request-handlers.cc

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
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"
@@ -41,6 +42,7 @@
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

7275
struct 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

8994
struct 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

136148
TEST_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

152200
TEST_CASE_METHOD(
@@ -353,7 +401,9 @@ TEST_CASE_METHOD(
353401
RequestHandlerFx::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+
415485
shared_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

439509
std::tuple<

test/src/unit-rest-array-schema-load.cc

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ struct ArraySchemaLoadFx {
4545
~ArraySchemaLoadFx() = default;
4646

4747
void create_array();
48-
void check_schema(const ArraySchema& schema) const;
4948

5049
test::VFSTestSetup vfs_test_setup_;
5150
std::string uri_;
@@ -71,7 +70,11 @@ TEST_CASE_METHOD(
7170
ArraySchemaExperimental::get_enumeration(ctx_, schema, "my_enum"),
7271
matcher);
7372

74-
check_schema(schema);
73+
// Schema was constructed prior to creating the array, the array URI is empty.
74+
// Set the schema's array_uri without opening the array.
75+
schema_.ptr()->array_schema_->set_array_uri(sm::URI(uri_));
76+
test::schema_equiv(
77+
*schema.ptr()->array_schema_, *schema_.ptr()->array_schema_);
7578
}
7679

7780
TEST_CASE_METHOD(
@@ -85,7 +88,8 @@ TEST_CASE_METHOD(
8588
REQUIRE_NOTHROW(
8689
ArraySchemaExperimental::get_enumeration(ctx_, schema, "my_enum"));
8790

88-
check_schema(schema);
91+
test::schema_equiv(
92+
*schema.ptr()->array_schema_, *schema_.ptr()->array_schema_);
8993
}
9094

9195
ArraySchemaLoadFx::ArraySchemaLoadFx()
@@ -125,26 +129,3 @@ void ArraySchemaLoadFx::create_array() {
125129

126130
Array::create(uri_, schema_);
127131
}
128-
129-
void ArraySchemaLoadFx::check_schema(const ArraySchema& schema) const {
130-
CHECK(schema.array_type() == schema_.array_type());
131-
CHECK(schema.attributes().size() == schema_.attributes().size());
132-
for (unsigned int i = 0; i < schema.attribute_num(); i++) {
133-
auto a = schema_.attribute(i);
134-
auto b = schema.attribute(i);
135-
CHECK(a.cell_val_num() == b.cell_val_num());
136-
CHECK(a.name() == b.name());
137-
CHECK(a.type() == b.type());
138-
CHECK(a.nullable() == b.nullable());
139-
CHECK(
140-
AttributeExperimental::get_enumeration_name(ctx_, a) ==
141-
AttributeExperimental::get_enumeration_name(ctx_, b));
142-
}
143-
CHECK(schema.capacity() == schema_.capacity());
144-
CHECK(schema.cell_order() == schema_.cell_order());
145-
CHECK(schema.tile_order() == schema_.tile_order());
146-
CHECK(schema.allows_dups() == schema_.allows_dups());
147-
CHECK(
148-
schema.ptr()->array_schema_->array_uri().to_string() ==
149-
sm::URI(uri_).to_string());
150-
}

test/support/src/helpers.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "tiledb/common/stdx_string.h"
5050
#include "tiledb/sm/c_api/tiledb_struct_def.h"
5151
#include "tiledb/sm/cpp_api/tiledb"
52+
#include "tiledb/sm/cpp_api/tiledb_experimental"
5253
#include "tiledb/sm/enums/encryption_type.h"
5354
#include "tiledb/sm/filesystem/uri.h"
5455
#include "tiledb/sm/global_state/unit_test_config.h"
@@ -1621,6 +1622,26 @@ void read_sparse_v11(
16211622
tiledb_query_free(&query);
16221623
}
16231624

1625+
void schema_equiv(
1626+
const sm::ArraySchema& schema1, const sm::ArraySchema& schema2) {
1627+
CHECK(schema1.array_type() == schema2.array_type());
1628+
CHECK(schema1.attributes().size() == schema2.attributes().size());
1629+
for (unsigned int i = 0; i < schema2.attribute_num(); i++) {
1630+
auto a = schema2.attribute(i);
1631+
auto b = schema1.attribute(i);
1632+
CHECK(a->cell_val_num() == b->cell_val_num());
1633+
CHECK(a->name() == b->name());
1634+
CHECK(a->type() == b->type());
1635+
CHECK(a->nullable() == b->nullable());
1636+
CHECK(a->get_enumeration_name() == b->get_enumeration_name());
1637+
}
1638+
CHECK(schema1.capacity() == schema2.capacity());
1639+
CHECK(schema1.cell_order() == schema2.cell_order());
1640+
CHECK(schema1.tile_order() == schema2.tile_order());
1641+
CHECK(schema1.allows_dups() == schema2.allows_dups());
1642+
CHECK(schema1.array_uri().to_string() == schema2.array_uri().to_string());
1643+
}
1644+
16241645
template void check_subarray<int8_t>(
16251646
tiledb::sm::Subarray& subarray, const SubarrayRanges<int8_t>& ranges);
16261647
template void check_subarray<uint8_t>(

test/support/src/helpers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,10 @@ void write_sparse_v11(
957957
*/
958958
void read_sparse_v11(
959959
tiledb_ctx_t* ctx, const std::string& array_name, uint64_t timestamp);
960+
961+
void schema_equiv(
962+
const sm::ArraySchema& schema1, const sm::ArraySchema& schema2);
963+
960964
} // namespace tiledb::test
961965

962966
#endif

0 commit comments

Comments
 (0)