Skip to content

Commit

Permalink
Bury some gory details in array_templates.h
Browse files Browse the repository at this point in the history
  • Loading branch information
rroelke committed Jan 14, 2025
1 parent 92e70d7 commit b67a0a1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 81 deletions.
34 changes: 12 additions & 22 deletions test/performance/tiledb_submit_a_b.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,16 @@ static void run(
auto do_submit = [&](auto& key, auto& query, auto& outdata)
-> std::pair<tiledb_query_status_t, uint64_t> {
// make field size locations
auto dimension_sizes = []<typename... Ds>(std::tuple<Ds...> dimensions) {
return query_applicator<Asserter, Ds...>::make_field_sizes(dimensions);
}(outdata.dimensions());
auto attribute_sizes = []<typename... As>(std::tuple<As...> attributes) {
return query_applicator<Asserter, As...>::make_field_sizes(attributes);
}(outdata.attributes());
auto dimension_sizes =
templates::query::make_field_sizes<Asserter>(outdata.dimensions());
auto attribute_sizes =
templates::query::make_field_sizes<Asserter>(outdata.attributes());

// add fields to query
[&]<typename... Ds>(std::tuple<Ds...> dims) {
query_applicator<Asserter, Ds...>::set(
ctx, query, dimension_sizes, dims, dimension_name);
}(outdata.dimensions());
[&]<typename... As>(std::tuple<As...> atts) {
query_applicator<Asserter, As...>::set(
ctx, query, attribute_sizes, atts, attribute_name);
}(outdata.attributes());
templates::query::set_fields<Asserter>(
ctx, query, dimension_sizes, outdata.dimensions(), dimension_name);
templates::query::set_fields<Asserter>(
ctx, query, attribute_sizes, outdata.attributes(), attribute_name);

{
tiledb::sm::stats::DurationInstrument<TimeKeeper> qtimer =
Expand All @@ -296,14 +290,10 @@ static void run(
tiledb_query_status_t status;
TRY(ctx, tiledb_query_get_status(ctx, query, &status));

const uint64_t dim_num_cells = [&]<typename... Ds>(auto dims) {
return query_applicator<Asserter, Ds...>::num_cells(
dims, dimension_sizes);
}(outdata.dimensions());
const uint64_t att_num_cells = [&]<typename... As>(auto atts) {
return query_applicator<Asserter, As...>::num_cells(
atts, attribute_sizes);
}(outdata.attributes());
const uint64_t dim_num_cells = templates::query::num_cells<Asserter>(
outdata.dimensions(), dimension_sizes);
const uint64_t att_num_cells = templates::query::num_cells<Asserter>(
outdata.attributes(), attribute_sizes);

ASSERTER(dim_num_cells == att_num_cells);

Expand Down
97 changes: 38 additions & 59 deletions test/src/unit-sparse-global-order-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ using tiledb::sm::Datatype;
using tiledb::test::templates::AttributeType;
using tiledb::test::templates::DimensionType;
using tiledb::test::templates::FragmentType;
using tiledb::test::templates::query_applicator;

template <typename D1, typename D2>
using Subarray2DType = std::vector<std::pair<
Expand Down Expand Up @@ -600,12 +599,10 @@ void CSparseGlobalOrderFx::write_fragment(const Fragment& fragment) {
const auto attributes = fragment.attributes();

// make field size locations
auto dimension_sizes = []<typename... Ds>(std::tuple<Ds...> dimensions) {
return query_applicator<Asserter, Ds...>::make_field_sizes(dimensions);
}(dimensions);
auto attribute_sizes = []<typename... As>(std::tuple<As...> attributes) {
return query_applicator<Asserter, As...>::make_field_sizes(attributes);
}(attributes);
auto dimension_sizes =
templates::query::make_field_sizes<Asserter>(dimensions);
auto attribute_sizes =
templates::query::make_field_sizes<Asserter>(attributes);

// Create the query.
tiledb_query_t* query;
Expand All @@ -615,33 +612,27 @@ void CSparseGlobalOrderFx::write_fragment(const Fragment& fragment) {
ASSERTER(rc == TILEDB_OK);

// add dimensions to query
[&]<typename... Ds>(std::tuple<Ds...> dims) {
query_applicator<Asserter, Ds...>::set(
ctx_, query, dimension_sizes, dims, [](unsigned d) {
return "d" + std::to_string(d + 1);
});
}(dimensions);
templates::query::set_fields<Asserter>(
ctx_, query, dimension_sizes, dimensions, [](unsigned d) {
return "d" + std::to_string(d + 1);
});

// add attributes to query
[&]<typename... As>(std::tuple<As...> atts) {
query_applicator<Asserter, As...>::set(
ctx_, query, attribute_sizes, atts, [](unsigned a) {
return "a" + std::to_string(a + 1);
});
}(attributes);
templates::query::set_fields<Asserter>(
ctx_, query, attribute_sizes, attributes, [](unsigned a) {
return "a" + std::to_string(a + 1);
});

// Submit query.
rc = tiledb_query_submit(ctx_, query);
ASSERTER("" == error_if_any(rc));

// check that sizes match what we expect
const uint64_t expect_num_cells = fragment.size();
const uint64_t dim_num_cells = [&]<typename... Ds>(auto dims) {
return query_applicator<Asserter, Ds...>::num_cells(dims, dimension_sizes);
}(dimensions);
const uint64_t att_num_cells = [&]<typename... As>(auto atts) {
return query_applicator<Asserter, As...>::num_cells(atts, attribute_sizes);
}(attributes);
const uint64_t dim_num_cells =
templates::query::num_cells<Asserter>(dimensions, dimension_sizes);
const uint64_t att_num_cells =
templates::query::num_cells<Asserter>(attributes, attribute_sizes);

ASSERTER(dim_num_cells == expect_num_cells);
ASSERTER(att_num_cells == expect_num_cells);
Expand Down Expand Up @@ -2651,34 +2642,26 @@ void CSparseGlobalOrderFx::run_execute(Instance& instance) {
uint64_t outcursor = 0;
while (true) {
// make field size locations
auto dimension_sizes = [&]<typename... Ds>(std::tuple<Ds...> outdims) {
return query_applicator<Asserter, Ds...>::make_field_sizes(
outdims, instance.num_user_cells);
}(outdims);
auto attribute_sizes = [&]<typename... As>(std::tuple<As...> outdims) {
return query_applicator<Asserter, As...>::make_field_sizes(
outdims, instance.num_user_cells);
}(outatts);
auto dimension_sizes = templates::query::make_field_sizes<Asserter>(
outdims, instance.num_user_cells);
auto attribute_sizes = templates::query::make_field_sizes<Asserter>(
outatts, instance.num_user_cells);

// add fields to query
[&]<typename... Ds>(std::tuple<Ds...> dims) {
query_applicator<Asserter, Ds...>::set(
ctx_,
query,
dimension_sizes,
dims,
[](unsigned d) { return "d" + std::to_string(d + 1); },
outcursor);
}(outdims);
[&]<typename... As>(std::tuple<As...> atts) {
query_applicator<Asserter, As...>::set(
ctx_,
query,
attribute_sizes,
atts,
[](unsigned a) { return "a" + std::to_string(a + 1); },
outcursor);
}(outatts);
templates::query::set_fields<Asserter>(
ctx_,
query,
dimension_sizes,
outdims,
[](unsigned d) { return "d" + std::to_string(d + 1); },
outcursor);
templates::query::set_fields<Asserter>(
ctx_,
query,
attribute_sizes,
outatts,
[](unsigned a) { return "a" + std::to_string(a + 1); },
outcursor);

rc = tiledb_query_submit(ctx_, query);
{
Expand Down Expand Up @@ -2706,14 +2689,10 @@ void CSparseGlobalOrderFx::run_execute(Instance& instance) {
rc = tiledb_query_get_status(ctx_, query, &status);
ASSERTER(rc == TILEDB_OK);

const uint64_t dim_num_cells = [&]<typename... Ds>(auto dims) {
return query_applicator<Asserter, Ds...>::num_cells(
dims, dimension_sizes);
}(outdims);
const uint64_t att_num_cells = [&]<typename... As>(auto atts) {
return query_applicator<Asserter, As...>::num_cells(
atts, attribute_sizes);
}(outatts);
const uint64_t dim_num_cells =
templates::query::num_cells<Asserter>(outdims, dimension_sizes);
const uint64_t att_num_cells =
templates::query::num_cells<Asserter>(outatts, attribute_sizes);

ASSERTER(dim_num_cells == att_num_cells);

Expand Down
49 changes: 49 additions & 0 deletions test/support/src/array_templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,55 @@ struct query_applicator {
}
};

/**
* Helper namespace for actually using the `query_applicator`.
* Functions in this namespace help to deduce the template
* instantiation of `query_applicator`.
*/
namespace query {

/**
* @return a tuple containing the size of each input field
*/
template <typename Asserter>
auto make_field_sizes(
const auto& fields,
uint64_t cell_limit = std::numeric_limits<uint64_t>::max()) {
return [cell_limit]<typename... Ts>(std::tuple<Ts...> fields) {
return query_applicator<Asserter, Ts...>::make_field_sizes(
fields, cell_limit);
}(fields);
}

/**
* Set buffers on `query` for the tuple of field columns
*/
template <typename Asserter>
void set_fields(
tiledb_ctx_t* ctx,
tiledb_query_t* query,
auto& field_sizes,
auto fields,
std::function<std::string(unsigned)> field_name,
uint64_t cell_offset = 0) {
[&]<typename... Ts>(std::tuple<Ts...> fields) {
query_applicator<Asserter, Ts...>::set(
ctx, query, field_sizes, fields, field_name, cell_offset);
}(fields);
}

/**
* @return the number of cells written into `fields` by a read query
*/
template <typename Asserter>
uint64_t num_cells(const auto& fields, const auto& field_sizes) {
return [&]<typename... Ts>(auto fields) {
return query_applicator<Asserter, Ts...>::num_cells(fields, field_sizes);
}(fields);
}

} // namespace query

} // namespace tiledb::test::templates

#endif

0 comments on commit b67a0a1

Please sign in to comment.