Skip to content

Commit 2d8dacb

Browse files
committed
Begin filling in unit-query-add-predicate.cc
1 parent fd12c4f commit 2d8dacb

File tree

3 files changed

+329
-48
lines changed

3 files changed

+329
-48
lines changed

test/src/unit-query-add-predicate.cc

Lines changed: 220 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
#include <test/support/assert_helpers.h>
4141
#include <test/support/tdb_catch.h>
4242

43+
#include "test/support/src/array_templates.h"
4344
#include "test/support/src/error_helpers.h"
4445
#include "test/support/src/helpers.h"
4546
#include "test/support/src/vfs_helpers.h"
47+
#include "tiledb/api/c_api/array/array_api_internal.h"
4648
#include "tiledb/sm/cpp_api/tiledb"
4749
#include "tiledb/sm/cpp_api/tiledb_experimental"
4850

@@ -52,6 +54,14 @@ using namespace tiledb::test;
5254
// no rapidcheck
5355
using Asserter = AsserterCatch;
5456

57+
// query result type for the array schema used in these tests
58+
using Cells = templates::Fragment2D<
59+
uint64_t,
60+
uint64_t,
61+
std::optional<int32_t>,
62+
std::vector<char>,
63+
std::optional<int32_t>>;
64+
5565
struct QueryAddPredicateFx {
5666
VFSTestSetup vfs_test_setup_;
5767

@@ -72,8 +82,80 @@ struct QueryAddPredicateFx {
7282
* of the schema given above
7383
*/
7484
void write_array(const std::string& path, tiledb_array_type_t atype);
85+
86+
Cells query_array(
87+
const std::string& path,
88+
tiledb_layout_t layout,
89+
std::vector<const char*> predicates);
90+
91+
Cells query_array(
92+
const std::string& path, tiledb_layout_t layout, const char* predicate) {
93+
return query_array(path, layout, std::vector<const char*>{predicate});
94+
}
95+
96+
static const Cells INPUT;
7597
};
7698

99+
const Cells QueryAddPredicateFx::INPUT = Cells{
100+
.d1_ = templates::query_buffers<uint64_t>(
101+
{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}),
102+
.d2_ = templates::query_buffers<uint64_t>(
103+
{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}),
104+
.atts_ = std::make_tuple(
105+
templates::query_buffers<std::optional<int32_t>>(
106+
std::vector<std::optional<int32_t>>{
107+
15,
108+
std::nullopt,
109+
std::nullopt,
110+
12,
111+
std::nullopt,
112+
10,
113+
9,
114+
std::nullopt,
115+
7,
116+
6,
117+
5,
118+
4,
119+
std::nullopt,
120+
2,
121+
1,
122+
0}),
123+
templates::query_buffers<std::string>(std::vector<std::string>{
124+
"one",
125+
"two",
126+
"three",
127+
"four",
128+
"five",
129+
"six",
130+
"seven",
131+
"eight",
132+
"nine",
133+
"ten",
134+
"eleven",
135+
"twelve",
136+
"thirteen",
137+
"fourteen",
138+
"fifteen",
139+
"sixteen"}),
140+
templates::query_buffers<std::optional<int32_t>>(
141+
std::vector<std::optional<int32_t>>{
142+
4,
143+
4,
144+
7,
145+
std::nullopt,
146+
7,
147+
7,
148+
std::nullopt,
149+
0,
150+
1,
151+
std::nullopt,
152+
3,
153+
4,
154+
std::nullopt,
155+
6,
156+
7,
157+
std::nullopt}))};
158+
77159
void QueryAddPredicateFx::create_array(
78160
const std::string& path, tiledb_array_type_t atype) {
79161
auto ctx = context();
@@ -119,62 +201,81 @@ void QueryAddPredicateFx::write_array(
119201
Array array(ctx, path, TILEDB_WRITE);
120202
Query query(ctx, array);
121203

122-
std::vector<uint64_t> rows = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
123-
std::vector<uint64_t> cols = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
124-
125-
if (atype == TILEDB_SPARSE) {
126-
query.set_data_buffer("row", rows).set_data_buffer("col", cols);
127-
} else {
204+
if (atype == TILEDB_DENSE) {
128205
Subarray s(ctx, array);
129-
s.add_range(0, 1, 4);
130-
s.add_range(1, 1, 4);
206+
s.add_range<uint64_t>(0, 1, 4);
207+
s.add_range<uint64_t>(1, 1, 4);
131208
query.set_layout(TILEDB_ROW_MAJOR).set_subarray(s);
209+
210+
templates::Fragment<
211+
std::optional<int32_t>,
212+
std::vector<char>,
213+
std::optional<int32_t>>
214+
cells = {.atts_ = INPUT.atts_};
215+
216+
auto field_sizes = templates::query::make_field_sizes<Asserter>(cells);
217+
templates::query::set_fields<Asserter>(
218+
ctx.ptr().get(),
219+
query.ptr().get(),
220+
field_sizes,
221+
cells,
222+
array.ptr().get()->array_schema_latest());
223+
224+
query.submit();
225+
} else {
226+
auto field_sizes =
227+
templates::query::make_field_sizes<Asserter>(const_cast<Cells&>(INPUT));
228+
templates::query::set_fields<Asserter>(
229+
ctx.ptr().get(),
230+
query.ptr().get(),
231+
field_sizes,
232+
const_cast<Cells&>(INPUT),
233+
array.ptr().get()->array_schema_latest());
234+
query.submit();
235+
}
236+
}
237+
238+
Cells QueryAddPredicateFx::query_array(
239+
const std::string& path,
240+
tiledb_layout_t layout,
241+
std::vector<const char*> predicates) {
242+
auto ctx = context();
243+
244+
Array array(ctx, path, TILEDB_READ);
245+
Query query(ctx, array);
246+
247+
query.set_layout(layout);
248+
249+
Cells out;
250+
out.resize(32);
251+
252+
auto field_sizes =
253+
templates::query::make_field_sizes<Asserter>(out, out.size());
254+
255+
templates::query::set_fields<Asserter>(
256+
ctx.ptr().get(),
257+
query.ptr().get(),
258+
field_sizes,
259+
out,
260+
array.ptr().get()->array_schema_latest());
261+
262+
for (const char* pred : predicates) {
263+
QueryExperimental::add_predicate(ctx, query, pred);
132264
}
133265

134-
std::vector<int32_t> a_values = {
135-
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
136-
std::vector<uint8_t> a_validity(a_values.size(), 1);
137-
a_validity[1] = a_validity[2] = a_validity[3] = a_validity[5] =
138-
a_validity[8] = a_validity[13] = 0;
139-
140-
std::vector<std::string> v_strings = {
141-
"one",
142-
"two",
143-
"three",
144-
"four",
145-
"five",
146-
"six",
147-
"seven",
148-
"eight",
149-
"nine",
150-
"ten",
151-
"eleven",
152-
"twelve",
153-
"thirteen",
154-
"fourteen",
155-
"fifteen",
156-
"sixteen"};
157-
std::vector<char> v_values;
158-
std::vector<uint64_t> v_offsets;
159-
for (const auto& s : v_strings) {
160-
v_offsets.push_back(v_values.size());
161-
v_values.insert(v_values.end(), s.begin(), s.end());
266+
if (array.schema().array_type() == TILEDB_DENSE) {
267+
Subarray s(ctx, array);
268+
s.add_range<uint64_t>(0, 1, 4);
269+
s.add_range<uint64_t>(1, 1, 4);
270+
query.set_subarray(s);
162271
}
163272

164-
std::vector<int32_t> e_keys = {
165-
4, 4, 7, 4, 7, 7, 7, 0, 1, 2, 3, 4, 5, 6, 7, 6};
166-
std::vector<uint8_t> e_validity(e_keys.size(), 1);
167-
e_validity[3] = e_validity[6] = e_validity[9] = e_validity[12] =
168-
e_validity[15] = 0;
273+
const auto st = query.submit();
274+
REQUIRE(st == Query::Status::COMPLETE);
169275

170-
query.set_data_buffer("a", a_values)
171-
.set_validity_buffer("a", a_validity)
172-
.set_data_buffer("v", v_values)
173-
.set_offsets_buffer("v", v_offsets)
174-
.set_data_buffer("e", e_keys)
175-
.set_validity_buffer("e", e_validity);
276+
templates::query::resize_fields<Asserter>(out, field_sizes);
176277

177-
query.submit();
278+
return out;
178279
}
179280

180281
TEST_CASE_METHOD(
@@ -268,3 +369,74 @@ TEST_CASE_METHOD(
268369
}
269370
}
270371
}
372+
373+
TEST_CASE_METHOD(
374+
QueryAddPredicateFx,
375+
"C API: Test query add predicate dense",
376+
"[capi][query][add_predicate]") {
377+
const std::string array_name =
378+
vfs_test_setup_.array_uri("test_qeury_add_predicate_dense");
379+
380+
create_array(array_name, TILEDB_DENSE);
381+
write_array(array_name, TILEDB_DENSE);
382+
383+
// FIXME: error messages
384+
REQUIRE_THROWS(query_array(array_name, TILEDB_UNORDERED, "row >= 3"));
385+
REQUIRE_THROWS(query_array(array_name, TILEDB_ROW_MAJOR, "row >= 3"));
386+
REQUIRE_THROWS(query_array(array_name, TILEDB_COL_MAJOR, "row >= 3"));
387+
REQUIRE_THROWS(query_array(array_name, TILEDB_GLOBAL_ORDER, "row >= 3"));
388+
REQUIRE_THROWS(query_array(array_name, TILEDB_HILBERT, "row >= 3"));
389+
}
390+
391+
TEST_CASE_METHOD(
392+
QueryAddPredicateFx,
393+
"C API: Test query add predicate legacy",
394+
"[capi][query][add_predicate]") {
395+
const std::string array_name =
396+
vfs_test_setup_.array_uri("test_qeury_add_predicate_legacy");
397+
// TODO
398+
}
399+
400+
TEST_CASE_METHOD(
401+
QueryAddPredicateFx,
402+
"C API: Test query add predicate sparse unsupported query order",
403+
"[capi][query][add_predicate]") {
404+
const std::string array_name =
405+
vfs_test_setup_.array_uri("test_qeury_add_predicate_sparse_unsupported");
406+
407+
create_array(array_name, TILEDB_SPARSE);
408+
write_array(array_name, TILEDB_SPARSE);
409+
// TODO
410+
}
411+
412+
TEST_CASE_METHOD(
413+
QueryAddPredicateFx,
414+
"C API: Test query add predicate sparse global order",
415+
"[capi][query][add_predicate]") {
416+
const std::string array_name =
417+
vfs_test_setup_.array_uri("test_qeury_add_predicate_sparse_global_order");
418+
419+
create_array(array_name, TILEDB_SPARSE);
420+
write_array(array_name, TILEDB_SPARSE);
421+
422+
SECTION("WHERE TRUE") {
423+
const auto result = query_array(array_name, TILEDB_GLOBAL_ORDER, "TRUE");
424+
CHECK(result == INPUT);
425+
}
426+
427+
SECTION("WHERE a IS NULL") {
428+
// TODO
429+
}
430+
431+
SECTION("WHERE b < 'fourteen'") {
432+
// TODO
433+
}
434+
435+
SECTION("WHERE row + col <= 4") {
436+
// TODO
437+
}
438+
439+
SECTION("WHERE coalesce(a, row) > a") {
440+
// TODO
441+
}
442+
}

0 commit comments

Comments
 (0)