/
query_condition.h
582 lines (525 loc) · 18.9 KB
/
query_condition.h
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/**
* @file query_condition.h
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 2021-2022 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
*
* Defines the QueryCondition class.
*/
#ifndef TILEDB_QUERY_CONDITION_H
#define TILEDB_QUERY_CONDITION_H
#include "external/include/span/span.hpp"
#include <unordered_set>
#include "tiledb/common/status.h"
#include "tiledb/sm/array_schema/array_schema.h"
#include "tiledb/sm/enums/query_condition_op.h"
#include "tiledb/sm/query/ast/query_ast.h"
using namespace tiledb::common;
namespace tiledb {
namespace sm {
class FragmentMetadata;
struct ResultCellSlab;
class ResultTile;
enum class QueryConditionCombinationOp : uint8_t;
class QueryCondition {
public:
/* ********************************* */
/* CONSTRUCTORS & DESTRUCTORS */
/* ********************************* */
/** Default constructor. */
QueryCondition();
/** Constructor from a marker. */
QueryCondition(const std::string& condition_marker);
/** Constructor from a tree. */
QueryCondition(tdb_unique_ptr<tiledb::sm::ASTNode>&& tree);
/** Constructor from a tree and marker. */
QueryCondition(
const uint64_t condition_index,
const std::string& condition_marker,
tdb_unique_ptr<tiledb::sm::ASTNode>&& tree);
/** Copy constructor. */
QueryCondition(const QueryCondition& rhs);
/** Move constructor. */
QueryCondition(QueryCondition&& rhs);
/** Destructor. */
~QueryCondition();
/* ********************************* */
/* OPERATORS */
/* ********************************* */
/** Copy-assignment operator. */
QueryCondition& operator=(const QueryCondition& rhs);
/** Move-assignment operator. */
QueryCondition& operator=(QueryCondition&& rhs);
/* ********************************* */
/* API */
/* ********************************* */
/**
* Initializes the instance.
*
* @param field_name The name of the field this operation applies to.
* @param condition_value The value to compare to.
* @param condition_value_size The byte size of condition_value.
* @param op The relational operation between the value of the field
* and `condition_value`.
*/
Status init(
std::string&& field_name,
const void* condition_value,
uint64_t condition_value_size,
const QueryConditionOp& op);
/**
* Verifies that the current state contains supported comparison
* operations. Currently, we support the following:
* - Fixed-size, single-value, non-nullable attributes.
* - The QueryConditionCombinationOp::AND operator.
*
* @param array_schema The current array schena.
* @return Status
*/
Status check(const ArraySchema& array_schema) const;
/**
* Combines this instance with the right-hand-side instance by
* the given operator.
*
* @param rhs The condition instance to combine with.
* @param combination_op The operation to combine them.
* @param combined_cond The output condition to mutate. This is
* expected to be allocated by the caller.
* @return Status
*/
Status combine(
const QueryCondition& rhs,
QueryConditionCombinationOp combination_op,
QueryCondition* combined_cond) const;
/**
* Returns true if this condition does not have any nodes in the AST
* representing the query condition.
*/
bool empty() const;
/**
* Returns a set of all unique field names among the value nodes in the AST
* representing the query condition.
*/
std::unordered_set<std::string>& field_names() const;
/**
* Returns the timestamp for this condition.
*/
uint64_t condition_timestamp() const;
/**
* Applies this query condition to `result_cell_slabs`.
*
* @param array_schema The array schema associated with `result_cell_slabs`.
* @param fragment_metadata The fragment metadata.
* @param result_cell_slabs The cell slabs to filter. Mutated to remove cell
* slabs that do not meet the criteria in this query condition.
* @param stride The stride between cells.
* @return Status
*/
Status apply(
const ArraySchema& array_schema,
const std::vector<shared_ptr<FragmentMetadata>>& fragment_metadata,
std::vector<ResultCellSlab>& result_cell_slabs,
uint64_t stride) const;
/**
* Applies this query condition to a set of cells.
*
* @param array_schema The array schema.
* @param result_tile The result tile to get the cells from.
* @param start The start cell.
* @param length The number of cells to process.
* @param src_cell The cell offset in the source tile.
* @param stride The stride between cells.
* @param result_buffer The buffer to use for results.
* @return Status
*/
Status apply_dense(
const ArraySchema& array_schema,
ResultTile* result_tile,
const uint64_t start,
const uint64_t length,
const uint64_t src_cell,
const uint64_t stride,
uint8_t* result_buffer);
/**
* Applies this query condition to a set of cells.
*
* @param array_schema The array schema.
* @param result_tile The result tile to get the cells from.
* @param result_bitmap The bitmap to use for results.
* @return Status
*/
template <typename BitmapType>
Status apply_sparse(
const ArraySchema& array_schema,
ResultTile& result_tile,
std::vector<BitmapType>& result_bitmap);
/**
* Reverse the query condition using De Morgan's law.
*/
QueryCondition negated_condition();
/**
* Sets the AST. This is internal state to only be used in
* the serialization path.
*/
void set_ast(tdb_unique_ptr<ASTNode>&& ast);
/**
* Returns the AST object. This is internal state to only be used in testing
* and the serialization path.
*/
const tdb_unique_ptr<ASTNode>& ast() const;
/**
* Returns the condition marker.
*/
const std::string& condition_marker() const;
/**
* Returns the condition index.
*/
uint64_t condition_index() const;
private:
/* ********************************* */
/* PRIVATE DATATYPES */
/* ********************************* */
/**
* Performs a binary comparison between two primitive types.
* We use a `struct` here because it can support partial
* template specialization while a standard function does not.
*
* Note that this comparator includes if statements that will
* prevent vectorization.
*/
template <typename T, QueryConditionOp Cmp>
struct BinaryCmpNullChecks;
/**
* Performs a binary comparison between two primitive types.
* We use a `struct` here because it can support partial
* template specialization while a standard function does not.
*/
template <typename T, QueryConditionOp Cmp>
struct BinaryCmp;
/* ********************************* */
/* PRIVATE ATTRIBUTES */
/* ********************************* */
/** Marker used to reference which file the condition came from. */
std::string condition_marker_;
/** Index for the condition. */
size_t condition_index_;
/** AST Tree structure representing the condition. **/
tdb_unique_ptr<tiledb::sm::ASTNode> tree_{};
/** Caches all field names in the value nodes of the AST. */
mutable std::unordered_set<std::string> field_names_;
/* ********************************* */
/* PRIVATE METHODS */
/* ********************************* */
/**
* Applies a value node on primitive-typed result cell slabs,
* templated for a query condition operator.
*
* @param node The value node to apply.
* @param fragment_metadata The fragment metadata.
* @param stride The stride between cells.
* @param var_size The attribute is var sized or not.
* @param nullable The attribute is nullable or not.
* @param fill_value The fill value for the cells.
* @param combination_op The combination op.
* @param result_cell_bitmap The input cell bitmap.
* @return The filtered cell slabs.
*/
template <typename T, QueryConditionOp Op, typename CombinationOp>
void apply_ast_node(
const tdb_unique_ptr<ASTNode>& node,
const std::vector<shared_ptr<FragmentMetadata>>& fragment_metadata,
uint64_t stride,
const bool var_size,
const bool nullable,
const ByteVecValue& fill_value,
const std::vector<ResultCellSlab>& result_cell_slabs,
CombinationOp combination_op,
std::vector<uint8_t>& result_cell_bitmap) const;
/**
* Applies a value node on primitive-typed result cell slabs.
*
* @param node The value node to apply.
* @param fragment_metadata The fragment metadata.
* @param stride The stride between cells.
* @param var_size The attribute is var sized or not.
* @param nullable The attribute is nullable or not.
* @param fill_value The fill value for the cells.
* @param combination_op The combination op.
* @param result_cell_bitmap The input cell bitmap.
* @return Status, filtered cell slabs.
*/
template <typename T, typename CombinationOp>
void apply_ast_node(
const tdb_unique_ptr<ASTNode>& node,
const std::vector<shared_ptr<FragmentMetadata>>& fragment_metadata,
uint64_t stride,
const bool var_size,
const bool nullable,
const ByteVecValue& fill_value,
const std::vector<ResultCellSlab>& result_cell_slabs,
CombinationOp combination_op,
std::vector<uint8_t>& result_cell_bitmap) const;
/**
* Applies a value node to filter result cells from the input
* result cell slabs.
*
* @param node The value node to apply.
* @param array_schema The current array schema.
* @param fragment_metadata The fragment metadata.
* @param stride The stride between cells.
* @param combination_op The combination op.
* @param result_cell_bitmap The input cell bitmap.
* @return Status, filtered cell slabs.
*/
template <typename CombinationOp>
void apply_ast_node(
const tdb_unique_ptr<ASTNode>& node,
const ArraySchema& array_schema,
const std::vector<shared_ptr<FragmentMetadata>>& fragment_metadata,
uint64_t stride,
const std::vector<ResultCellSlab>& result_cell_slabs,
CombinationOp combination_op,
std::vector<uint8_t>& result_cell_bitmap) const;
/**
* Applies the query condition represented with the AST to
* `result_cell_slabs`.
*
* @param node The node to apply.
* @param array_schema The array schema associated with `result_cell_slabs`.
* @param fragment_metadata The fragment metadata.
* @param stride The stride between cells.
* @param combination_op The combination op.
* @param result_cell_bitmap A bitmap representation of cell slabs to filter.
* Mutated to remove cell slabs that do not meet the criteria in this query
* condition.
* @return Filtered cell slabs.
*/
template <typename CombinationOp = std::logical_and<uint8_t>>
void apply_tree(
const tdb_unique_ptr<ASTNode>& node,
const ArraySchema& array_schema,
const std::vector<shared_ptr<FragmentMetadata>>& fragment_metadata,
uint64_t stride,
const std::vector<ResultCellSlab>& result_cell_slabs,
CombinationOp combination_op,
std::vector<uint8_t>& result_cell_bitmap) const;
/**
* Applies a value node on a dense result tile,
* templated for a query condition operator.
*
* @param node The value node to apply.
* @param result_tile The result tile to get the cells from.
* @param start The start cell.
* @param src_cell The cell offset in the source tile.
* @param stride The stride between cells.
* @param var_size The attribute is var sized or not.
* @param combination_op The combination op.
* @param result_buffer The result buffer.
*/
template <typename T, QueryConditionOp Op, typename CombinationOp>
void apply_ast_node_dense(
const tdb_unique_ptr<ASTNode>& node,
ResultTile* result_tile,
const uint64_t start,
const uint64_t src_cell,
const uint64_t stride,
const bool var_size,
const bool nullable,
CombinationOp combination_op,
span<uint8_t> result_buffer) const;
/**
* Applies a value node on a dense result tile.
*
* @param node The node to apply.
* @param result_tile The result tile to get the cells from.
* @param start The start cell.
* @param src_cell The cell offset in the source tile.
* @param stride The stride between cells.
* @param var_size The attribute is var sized or not.
* @param combination_op The combination op.
* @param result_buffer The result buffer.
* @return Status.
*/
template <typename T, typename CombinationOp>
void apply_ast_node_dense(
const tdb_unique_ptr<ASTNode>& node,
ResultTile* result_tile,
const uint64_t start,
const uint64_t src_cell,
const uint64_t stride,
const bool var_size,
const bool nullable,
CombinationOp combination_op,
span<uint8_t> result_buffer) const;
/**
* Applies a value node to filter result cells from the input
* result tile.
*
* @param node The node to apply.
* @param array_schema The current array schema.
* @param result_tile The result tile to get the cells from.
* @param start The start cell.
* @param src_cell The cell offset in the source tile.
* @param stride The stride between cells.
* @param combination_op The combination op.
* @param result_buffer The result buffer.
* @return Status.
*/
template <typename CombinationOp>
void apply_ast_node_dense(
const tdb_unique_ptr<ASTNode>& node,
const ArraySchema& array_schema,
ResultTile* result_tile,
const uint64_t start,
const uint64_t src_cell,
const uint64_t stride,
CombinationOp combination_op,
span<uint8_t> result_buffer) const;
/**
* Applies the query condition represented with the AST to a set of cells.
*
* @param node The node to apply.
* @param array_schema The array schema.
* @param result_tile The result tile to get the cells from.
* @param start The start cell.
* @param src_cell The cell offset in the source tile.
* @param stride The stride between cells.
* @param combination_op The combination op.
* @param result_buffer The buffer to use for results.
* @return Void.
*/
template <typename CombinationOp = std::logical_and<uint8_t>>
void apply_tree_dense(
const tdb_unique_ptr<ASTNode>& node,
const ArraySchema& array_schema,
ResultTile* result_tile,
const uint64_t start,
const uint64_t src_cell,
const uint64_t stride,
CombinationOp combination_op,
span<uint8_t> result_buffer) const;
/**
* Applies a value node on a sparse result tile,
* templated for a query condition operator.
*
* @param node The node to apply.
* @param result_tile The result tile to get the cells from.
* @param var_size The attribute is var sized or not.
* @param combination_op The combination op.
* @param result_bitmap The result bitmap.
*/
template <
typename T,
QueryConditionOp Op,
typename BitmapType,
typename CombinationOp,
typename nullable>
void apply_ast_node_sparse(
const tdb_unique_ptr<ASTNode>& node,
ResultTile& result_tile,
const bool var_size,
CombinationOp combination_op,
std::vector<BitmapType>& result_bitmap) const;
/**
* Applies a value node on a sparse result tile,
* templated on the nullable operator.
*
* @param node The node to apply.
* @param result_tile The result tile to get the cells from.
* @param var_size The attribute is var sized or not.
* @param combination_op The combination op.
* @param result_bitmap The result bitmap.
* @return Status.
*/
template <
typename T,
typename BitmapType,
typename CombinationOp,
typename nullable>
void apply_ast_node_sparse(
const tdb_unique_ptr<ASTNode>& node,
ResultTile& result_tile,
const bool var_size,
CombinationOp combination_op,
std::vector<BitmapType>& result_bitmap) const;
/**
* Applies a value node on a sparse result tile.
*
* @param node The node to apply.
* @param result_tile The result tile to get the cells from.
* @param var_size The attribute is var sized or not.
* @param combination_op The combination op.
* @param result_bitmap The result bitmap.
*/
template <typename T, typename BitmapType, typename CombinationOp>
void apply_ast_node_sparse(
const tdb_unique_ptr<ASTNode>& node,
ResultTile& result_tile,
const bool var_size,
const bool nullable,
CombinationOp combination_op,
std::vector<BitmapType>& result_bitmap) const;
/**
* Applies a value node to filter result cells from the input
* result tile.
*
* @param node The node to apply.
* @param array_schema The current array schema.
* @param result_tile The result tile to get the cells from.
* @param combination_op The combination op.
* @param result_bitmap The result bitmap.
* @return Status.
*/
template <typename BitmapType, typename CombinationOp>
void apply_ast_node_sparse(
const tdb_unique_ptr<ASTNode>& node,
const ArraySchema& array_schema,
ResultTile& result_tile,
CombinationOp combination_op,
std::vector<BitmapType>& result_bitmap) const;
/**
* Applies the query condition represented with the AST to a set of cells.
*
* @param node The node to apply.
* @param array_schema The array schema.
* @param result_tile The result tile to get the cells from.
* @param combination_op The combination op.
* @param result_bitmap The bitmap to use for results.
* @return Void.
*/
template <
typename BitmapType,
typename CombinationOp = std::logical_and<BitmapType>>
void apply_tree_sparse(
const tdb_unique_ptr<ASTNode>& node,
const ArraySchema& array_schema,
ResultTile& result_tile,
CombinationOp combination_op,
std::vector<BitmapType>& result_bitmap) const;
};
} // namespace sm
} // namespace tiledb
#endif // TILEDB_QUERY_CONDITION_H