-
Notifications
You must be signed in to change notification settings - Fork 178
/
unit-cppapi-filter.cc
300 lines (258 loc) · 9.93 KB
/
unit-cppapi-filter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @file unit-cppapi-filter.cc
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 2017-2021 TileDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @section DESCRIPTION
*
* Tests the C++ API for filter related functions.
*/
#include "catch.hpp"
#include "tiledb/sm/cpp_api/tiledb"
static void check_filters(
const tiledb::FilterList& answer, const tiledb::FilterList& check) {
REQUIRE(check.nfilters() == answer.nfilters());
REQUIRE(check.max_chunk_size() == answer.max_chunk_size());
for (uint32_t i = 0; i < check.nfilters(); i++) {
auto f_answer = answer.filter(i), f_check = check.filter(i);
REQUIRE(f_check.filter_type() == f_answer.filter_type());
}
}
TEST_CASE("C++ API: Filter options", "[cppapi][filter]") {
using namespace tiledb;
Context ctx;
// Test filter creation and option setting/getting
Filter f(ctx, TILEDB_FILTER_BZIP2);
int32_t get_level;
f.get_option(TILEDB_COMPRESSION_LEVEL, &get_level);
REQUIRE(get_level == -1);
int32_t set_level = 5;
f.set_option(TILEDB_COMPRESSION_LEVEL, &set_level);
f.get_option(TILEDB_COMPRESSION_LEVEL, &get_level);
REQUIRE(get_level == 5);
// Check templated version
f.set_option(TILEDB_COMPRESSION_LEVEL, 4);
f.get_option(TILEDB_COMPRESSION_LEVEL, &get_level);
REQUIRE(get_level == 4);
// Check templated version with wrong type throws exception
uint32_t wrong_type_u = 1;
REQUIRE_THROWS_AS(
f.set_option(TILEDB_COMPRESSION_LEVEL, wrong_type_u),
std::invalid_argument);
REQUIRE_THROWS_AS(
f.get_option(TILEDB_COMPRESSION_LEVEL, &wrong_type_u),
std::invalid_argument);
// Check that you can bypass type safety (don't do this).
f.get_option(TILEDB_COMPRESSION_LEVEL, (void*)&wrong_type_u);
REQUIRE(wrong_type_u == 4);
// Unsupported option
uint32_t window;
REQUIRE_THROWS_AS(
f.set_option(TILEDB_BIT_WIDTH_MAX_WINDOW, &window), TileDBError);
REQUIRE_THROWS_AS(
f.get_option(TILEDB_BIT_WIDTH_MAX_WINDOW, &window), TileDBError);
Filter f2(ctx, TILEDB_FILTER_BIT_WIDTH_REDUCTION);
int32_t wrong_type_i = 1;
REQUIRE_THROWS_AS(f2.set_option(TILEDB_COMPRESSION_LEVEL, 1), TileDBError);
REQUIRE_THROWS_AS(
f2.set_option(TILEDB_BIT_WIDTH_MAX_WINDOW, -1), std::invalid_argument);
REQUIRE_THROWS_AS(
f2.set_option(TILEDB_BIT_WIDTH_MAX_WINDOW, wrong_type_i),
std::invalid_argument);
}
TEST_CASE("C++ API: Filter lists", "[cppapi][filter]") {
using namespace tiledb;
Context ctx;
Filter f1(ctx, TILEDB_FILTER_BIT_WIDTH_REDUCTION),
f2(ctx, TILEDB_FILTER_BZIP2);
const int32_t set_level = 5;
f2.set_option(TILEDB_COMPRESSION_LEVEL, &set_level);
FilterList list(ctx);
REQUIRE(list.nfilters() == 0);
REQUIRE(list.max_chunk_size() == 65536);
list.set_max_chunk_size(10000);
REQUIRE(list.max_chunk_size() == 10000);
list.add_filter(f1).add_filter(f2);
REQUIRE(list.nfilters() == 2);
Filter f1_get = list.filter(0), f2_get(list.filter(1));
REQUIRE_THROWS_AS(list.filter(2), TileDBError);
REQUIRE(f1_get.filter_type() == TILEDB_FILTER_BIT_WIDTH_REDUCTION);
REQUIRE(f2_get.filter_type() == TILEDB_FILTER_BZIP2);
int32_t get_level;
f2_get.get_option(TILEDB_COMPRESSION_LEVEL, &get_level);
REQUIRE(get_level == set_level);
list.add_filter({ctx, TILEDB_FILTER_BYTESHUFFLE});
REQUIRE(list.nfilters() == 3);
}
TEST_CASE("C++ API: Filter lists on array", "[cppapi][filter]") {
using namespace tiledb;
Context ctx;
VFS vfs(ctx);
std::string array_name = "cpp_unit_array";
if (vfs.is_dir(array_name))
vfs.remove_dir(array_name);
// Create schema with filter lists
FilterList a1_filters(ctx);
a1_filters.set_max_chunk_size(10000);
a1_filters.add_filter({ctx, TILEDB_FILTER_BYTESHUFFLE})
.add_filter({ctx, TILEDB_FILTER_BZIP2})
.add_filter({ctx, TILEDB_FILTER_CHECKSUM_MD5})
.add_filter({ctx, TILEDB_FILTER_CHECKSUM_SHA256});
FilterList a2_filters(ctx);
a2_filters.add_filter({ctx, TILEDB_FILTER_ZSTD})
.add_filter({ctx, TILEDB_FILTER_CHECKSUM_MD5})
.add_filter({ctx, TILEDB_FILTER_CHECKSUM_SHA256});
auto a1 = Attribute::create<int>(ctx, "a1");
auto a2 = Attribute::create<std::string>(ctx, "a2");
a1.set_filter_list(a1_filters);
a2.set_filter_list(a2_filters);
Domain domain(ctx);
auto d1 = Dimension::create<int>(ctx, "d1", {{0, 100}}, 10);
auto d2 = Dimension::create<int>(ctx, "d2", {{0, 100}}, 10);
domain.add_dimensions(d1, d2);
ArraySchema schema(ctx, TILEDB_SPARSE);
schema.set_domain(domain);
schema.add_attributes(a1, a2);
FilterList offsets_filters(ctx);
offsets_filters.add_filter({ctx, TILEDB_FILTER_POSITIVE_DELTA})
.add_filter({ctx, TILEDB_FILTER_BYTESHUFFLE})
.add_filter({ctx, TILEDB_FILTER_LZ4})
.add_filter({ctx, TILEDB_FILTER_CHECKSUM_MD5})
.add_filter({ctx, TILEDB_FILTER_CHECKSUM_SHA256});
schema.set_coords_filter_list(a1_filters)
.set_offsets_filter_list(offsets_filters);
// Create array
Array::create(array_name, schema);
// Write to array
std::vector<int> a1_data = {1, 2};
std::vector<std::string> a2_data = {"abc", "defg"};
auto a2buf = ungroup_var_buffer(a2_data);
std::vector<int> coords = {0, 0, 10, 10};
Array array(ctx, array_name, TILEDB_WRITE);
Query query(ctx, array);
query.set_data_buffer("a1", a1_data)
.set_data_buffer("a2", a2buf.second)
.set_offsets_buffer("a2", a2buf.first)
.set_coordinates(coords)
.set_layout(TILEDB_UNORDERED);
REQUIRE(query.submit() == Query::Status::COMPLETE);
array.close();
// Sanity check reading
array.open(TILEDB_READ);
std::vector<int> subarray = {0, 10, 0, 10};
std::vector<int> a1_read(2);
std::vector<uint64_t> a2_read_off(2);
std::string a2_read_data;
a2_read_data.resize(7);
Query query_r(ctx, array);
query_r.set_subarray(subarray)
.set_layout(TILEDB_ROW_MAJOR)
.set_data_buffer("a1", a1_read)
.set_data_buffer("a2", a2_read_data)
.set_offsets_buffer("a2", a2_read_off);
REQUIRE(query_r.submit() == Query::Status::COMPLETE);
array.close();
auto ret = query_r.result_buffer_elements();
REQUIRE(ret.size() == 2);
REQUIRE(ret["a1"].first == 0);
REQUIRE(ret["a1"].second == 2);
REQUIRE(ret["a2"].first == 2);
REQUIRE(ret["a2"].second == 7);
REQUIRE(a1_read[0] == 1);
REQUIRE(a1_read[1] == 2);
REQUIRE(a2_read_off[0] == 0);
REQUIRE(a2_read_off[1] == 3);
REQUIRE(a2_read_data.substr(0, 7) == "abcdefg");
// Check reading filter lists.
array.open(TILEDB_READ);
auto schema_r = array.schema();
check_filters(a1_filters, schema_r.coords_filter_list());
check_filters(offsets_filters, schema_r.offsets_filter_list());
check_filters(a1_filters, schema_r.attribute("a1").filter_list());
check_filters(a2_filters, schema_r.attribute("a2").filter_list());
array.close();
// Clean up
if (vfs.is_dir(array_name))
vfs.remove_dir(array_name);
}
TEST_CASE("C++ API: Filter strings with RLE", "[cppapi][filter][rle-strings]") {
using namespace tiledb;
Context ctx;
VFS vfs(ctx);
std::string array_name = "cpp_unit_array";
if (vfs.is_dir(array_name))
vfs.remove_dir(array_name);
// Create schema with filter lists
FilterList a1_filters(ctx);
a1_filters.add_filter({ctx, TILEDB_FILTER_RLE});
auto a1 = Attribute::create<std::string>(ctx, "a1");
a1.set_filter_list(a1_filters);
Domain domain(ctx);
auto d1 = Dimension::create<int>(ctx, "d1", {{0, 100}}, 10);
auto d2 = Dimension::create<int>(ctx, "d2", {{0, 100}}, 10);
domain.add_dimensions(d1, d2);
ArraySchema schema(ctx, TILEDB_SPARSE);
schema.set_domain(domain);
schema.add_attributes(a1);
// Create array
Array::create(array_name, schema);
// Write to array
std::vector<std::string> a1_data = {
"foo", "foo", "foobar", "bar", "bar", "bar", "bar"};
auto a1buf = ungroup_var_buffer(a1_data);
std::vector<int> coords = {
0, 0, 10, 10, 20, 20, 20, 30, 30, 30, 30, 40, 40, 40};
Array array(ctx, array_name, TILEDB_WRITE);
Query query(ctx, array);
query.set_data_buffer("a1", a1buf.second)
.set_offsets_buffer("a1", a1buf.first)
.set_coordinates(coords)
.set_layout(TILEDB_UNORDERED);
REQUIRE(query.submit() == Query::Status::COMPLETE);
array.close();
// Sanity check reading
array.open(TILEDB_READ);
std::vector<int> subarray = {0, 40, 0, 40};
std::vector<uint64_t> a1_read_off(7);
std::string a1_read_data;
a1_read_data.resize(24);
Query query_r(ctx, array);
query_r.set_subarray(subarray)
.set_layout(TILEDB_ROW_MAJOR)
.set_data_buffer("a1", a1_read_data)
.set_offsets_buffer("a1", a1_read_off);
REQUIRE(query_r.submit() == Query::Status::COMPLETE);
array.close();
auto ret = query_r.result_buffer_elements();
REQUIRE(ret.size() == 1);
REQUIRE(ret["a1"].first == 7);
REQUIRE(ret["a1"].second == 24);
std::vector<uint64_t> exp_offsets = {0, 3, 6, 12, 15, 18, 21};
REQUIRE(a1_read_off == exp_offsets);
REQUIRE(a1_read_data.substr(0, 24) == "foofoofoobarbarbarbarbar");
// Clean up
if (vfs.is_dir(array_name))
vfs.remove_dir(array_name);
}