forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathart.cpp
More file actions
1520 lines (1302 loc) · 52.1 KB
/
art.cpp
File metadata and controls
1520 lines (1302 loc) · 52.1 KB
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "duckdb/execution/index/art/art.hpp"
#include "duckdb/common/assert.hpp"
#include "duckdb/common/helper.hpp"
#include "duckdb/common/typedefs.hpp"
#include "duckdb/common/types/conflict_manager.hpp"
#include "duckdb/common/unordered_map.hpp"
#include "duckdb/common/vector_operations/vector_operations.hpp"
#include "duckdb/execution/expression_executor.hpp"
#include "duckdb/execution/index/art/art_builder.hpp"
#include "duckdb/execution/index/art/art_key.hpp"
#include "duckdb/execution/index/art/art_merger.hpp"
#include "duckdb/execution/index/art/art_operator.hpp"
#include "duckdb/execution/index/art/art_scanner.hpp"
#include "duckdb/execution/index/art/base_leaf.hpp"
#include "duckdb/execution/index/art/base_node.hpp"
#include "duckdb/execution/index/art/iterator.hpp"
#include "duckdb/execution/index/art/leaf.hpp"
#include "duckdb/execution/index/art/node256.hpp"
#include "duckdb/execution/index/art/node256_leaf.hpp"
#include "duckdb/execution/index/art/node48.hpp"
#include "duckdb/execution/index/art/prefix.hpp"
#include "duckdb/optimizer/matcher/expression_matcher.hpp"
#include "duckdb/planner/expression/bound_between_expression.hpp"
#include "duckdb/planner/expression/bound_comparison_expression.hpp"
#include "duckdb/planner/expression/bound_constant_expression.hpp"
#include "duckdb/storage/arena_allocator.hpp"
#include "duckdb/storage/metadata/metadata_reader.hpp"
#include "duckdb/storage/table/append_state.hpp"
#include "duckdb/storage/table/scan_state.hpp"
#include "duckdb/storage/table_io_manager.hpp"
namespace duckdb {
struct ARTIndexScanState : public IndexScanState {
//! The predicates to scan.
//! A single predicate for point lookups, and two predicates for range scans.
Value values[2];
//! The expressions over the scan predicates.
ExpressionType expressions[2];
bool checked = false;
//! All scanned row IDs.
set<row_t> row_ids;
};
struct ARTIndexCompoundKeyScanState : public IndexScanState {
//! The predicates to scan.
//! A single predicate for each constituent key in a compound index.
vector<Value> values;
//! The expressions over the scan predicates.
vector<ExpressionType> expressions;
bool checked = false;
//! All scanned row IDs.
set<row_t> row_ids;
};
//===--------------------------------------------------------------------===//
// ART
//===--------------------------------------------------------------------===//
ART::ART(const string &name, const IndexConstraintType index_constraint_type, const vector<column_t> &column_ids,
TableIOManager &table_io_manager, const vector<unique_ptr<Expression>> &unbound_expressions,
AttachedDatabase &db,
const shared_ptr<array<unsafe_unique_ptr<FixedSizeAllocator>, ALLOCATOR_COUNT>> &allocators_ptr,
const IndexStorageInfo &info)
: BoundIndex(name, ART::TYPE_NAME, index_constraint_type, column_ids, table_io_manager, unbound_expressions, db),
allocators(allocators_ptr), owns_data(false) {
// FIXME: Use the new byte representation function to support nested types.
for (idx_t i = 0; i < types.size(); i++) {
switch (types[i]) {
case PhysicalType::BOOL:
case PhysicalType::INT8:
case PhysicalType::INT16:
case PhysicalType::INT32:
case PhysicalType::INT64:
case PhysicalType::INT128:
case PhysicalType::UINT8:
case PhysicalType::UINT16:
case PhysicalType::UINT32:
case PhysicalType::UINT64:
case PhysicalType::UINT128:
case PhysicalType::FLOAT:
case PhysicalType::DOUBLE:
case PhysicalType::VARCHAR:
break;
default:
throw InvalidTypeException(logical_types[i], "Invalid type for index key.");
}
}
// Initialize the allocators.
SetPrefixCount(info);
if (!allocators) {
owns_data = true;
auto prefix_size = NumericCast<idx_t>(prefix_count) + NumericCast<idx_t>(Prefix::METADATA_SIZE);
auto &block_manager = table_io_manager.GetIndexBlockManager();
array<unsafe_unique_ptr<FixedSizeAllocator>, ALLOCATOR_COUNT> allocator_array = {
make_unsafe_uniq<FixedSizeAllocator>(prefix_size, block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Leaf), block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Node4), block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Node16), block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Node48), block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Node256), block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Node7Leaf), block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Node15Leaf), block_manager),
make_unsafe_uniq<FixedSizeAllocator>(sizeof(Node256Leaf), block_manager),
};
allocators =
make_shared_ptr<array<unsafe_unique_ptr<FixedSizeAllocator>, ALLOCATOR_COUNT>>(std::move(allocator_array));
}
if (!info.IsValid()) {
// We create a new ART.
storage_version = db.GetStorageManager().GetStorageVersion();
return;
}
if (info.root_block_ptr.IsValid()) {
// Backwards compatibility.
Deserialize(info.root_block_ptr);
return;
}
// Set the root node and initialize the allocators.
tree.Set(info.root);
InitAllocators(info);
// Set the storage version of the ART
auto it = info.options.find("storage_version");
if (it != info.options.end()) {
// If this is an existing index with a saved storage version, use it.
storage_version = it->second.GetValue<idx_t>();
} else {
// Otherwise, this must be an existing index without a saved storage version.
// We started saving the storage version in v1.5.0, so if it is not present,
// we can not make any general assumptions about the exact storage version.
storage_version = optional_idx::Invalid();
}
}
//===--------------------------------------------------------------------===//
// Initialize Scans
//===--------------------------------------------------------------------===//
static unique_ptr<IndexScanState> InitializeScanSinglePredicate(const Value &value,
const ExpressionType expression_type) {
auto result = make_uniq<ARTIndexScanState>();
result->values[0] = value;
result->expressions[0] = expression_type;
return std::move(result);
}
static unique_ptr<IndexScanState> InitializeScanTwoPredicates(const Value &low_value,
const ExpressionType low_expression_type,
const Value &high_value,
const ExpressionType high_expression_type) {
auto result = make_uniq<ARTIndexScanState>();
result->values[0] = low_value;
result->expressions[0] = low_expression_type;
result->values[1] = high_value;
result->expressions[1] = high_expression_type;
return std::move(result);
}
// Build compound scan state by building individual index scans and collecting their exprs/values
unique_ptr<IndexScanState> ART::TryInitializeCompoundKeyScan(const vector<unique_ptr<Expression>> &index_exprs,
vector<vector<unique_ptr<Expression>>> &exprs) {
auto compound_scan_state = make_uniq<ARTIndexCompoundKeyScanState>();
for (idx_t i = 0; i < index_exprs.size(); ++i) {
auto index_expr = &index_exprs[i];
auto filter_exprs = &exprs[i];
for (const auto &filter_expr : *filter_exprs) {
auto single_scan = ART::TryInitializeScan(**index_expr, *filter_expr);
if (!single_scan) {
return nullptr;
}
auto single_scan_concrete = single_scan->Cast<ARTIndexScanState>();
if (single_scan_concrete.expressions[0] != ExpressionType::COMPARE_EQUAL) {
return nullptr;
}
compound_scan_state->values.push_back(single_scan_concrete.values[0]);
compound_scan_state->expressions.push_back(single_scan_concrete.expressions[0]);
}
}
return std::move(compound_scan_state);
}
unique_ptr<IndexScanState> ART::TryInitializeScan(const Expression &expr, const Expression &filter_expr) {
Value low_value, high_value, equal_value;
ExpressionType low_comparison_type = ExpressionType::INVALID, high_comparison_type = ExpressionType::INVALID;
// Try to find a matching index for any of the filter expressions.
ComparisonExpressionMatcher matcher;
// Match on a comparison type.
matcher.expr_type = make_uniq<ComparisonExpressionTypeMatcher>();
// Match on a constant comparison with the indexed expression.
matcher.matchers.push_back(make_uniq<ExpressionEqualityMatcher>(expr));
matcher.matchers.push_back(make_uniq<ConstantExpressionMatcher>());
matcher.policy = SetMatcher::Policy::UNORDERED;
vector<reference<Expression>> bindings;
auto filter_match =
matcher.Match(const_cast<Expression &>(filter_expr), bindings); // NOLINT: Match does not alter the expr.
if (filter_match) {
// This is a range or equality comparison with a constant value, so we can use the index.
// bindings[0] = the expression
// bindings[1] = the index expression
// bindings[2] = the constant
auto &comparison = bindings[0].get().Cast<BoundComparisonExpression>();
auto constant_value = bindings[2].get().Cast<BoundConstantExpression>().value;
auto comparison_type = comparison.GetExpressionType();
if (comparison.left->GetExpressionType() == ExpressionType::VALUE_CONSTANT) {
// The expression is on the right side, we flip the comparison expression.
comparison_type = FlipComparisonExpression(comparison_type);
}
if (comparison_type == ExpressionType::COMPARE_EQUAL) {
// An equality value overrides any other bounds.
equal_value = constant_value;
} else if (comparison_type == ExpressionType::COMPARE_GREATERTHANOREQUALTO ||
comparison_type == ExpressionType::COMPARE_GREATERTHAN) {
// This is a lower bound.
low_value = constant_value;
low_comparison_type = comparison_type;
} else {
// This is an upper bound.
high_value = constant_value;
high_comparison_type = comparison_type;
}
} else if (filter_expr.GetExpressionType() == ExpressionType::COMPARE_BETWEEN) {
auto &between = filter_expr.Cast<BoundBetweenExpression>();
if (!between.input->Equals(expr)) {
// The expression does not match the index expression.
return nullptr;
}
if (between.lower->GetExpressionType() != ExpressionType::VALUE_CONSTANT ||
between.upper->GetExpressionType() != ExpressionType::VALUE_CONSTANT) {
// Not a constant expression.
return nullptr;
}
low_value = between.lower->Cast<BoundConstantExpression>().value;
low_comparison_type = between.lower_inclusive ? ExpressionType::COMPARE_GREATERTHANOREQUALTO
: ExpressionType::COMPARE_GREATERTHAN;
high_value = (between.upper->Cast<BoundConstantExpression>()).value;
high_comparison_type =
between.upper_inclusive ? ExpressionType::COMPARE_LESSTHANOREQUALTO : ExpressionType::COMPARE_LESSTHAN;
}
// FIXME: add another if...else... to match rewritten BETWEEN,
// i.e., WHERE i BETWEEN 50 AND 1502 is rewritten to CONJUNCTION_AND.
// We cannot use an index scan.
if (equal_value.IsNull() && low_value.IsNull() && high_value.IsNull()) {
return nullptr;
}
// Initialize the index scan state and return it.
if (!equal_value.IsNull()) {
// Equality predicate.
return InitializeScanSinglePredicate(equal_value, ExpressionType::COMPARE_EQUAL);
}
if (!low_value.IsNull() && !high_value.IsNull()) {
// Two-sided predicate.
return InitializeScanTwoPredicates(low_value, low_comparison_type, high_value, high_comparison_type);
}
if (!low_value.IsNull()) {
// Less-than predicate.
return InitializeScanSinglePredicate(low_value, low_comparison_type);
}
// Greater-than predicate.
return InitializeScanSinglePredicate(high_value, high_comparison_type);
}
unique_ptr<IndexScanState> ART::InitializeFullScan() {
return make_uniq<ARTIndexScanState>();
}
//===--------------------------------------------------------------------===//
// ART Keys
//===--------------------------------------------------------------------===//
template <class T, bool IS_NOT_NULL>
static void TemplatedGenerateKeys(ArenaAllocator &allocator, Vector &input, idx_t count, unsafe_vector<ARTKey> &keys) {
D_ASSERT(keys.size() >= count);
UnifiedVectorFormat data;
input.ToUnifiedFormat(count, data);
auto input_data = UnifiedVectorFormat::GetData<T>(data);
for (idx_t i = 0; i < count; i++) {
auto idx = data.sel->get_index(i);
if (IS_NOT_NULL || data.validity.RowIsValid(idx)) {
ARTKey::CreateARTKey<T>(allocator, keys[i], input_data[idx]);
continue;
}
// We need to reset the key value in the reusable keys vector.
keys[i] = ARTKey();
}
}
template <class T, bool IS_NOT_NULL>
static void ConcatenateKeys(ArenaAllocator &allocator, Vector &input, idx_t count, unsafe_vector<ARTKey> &keys) {
UnifiedVectorFormat data;
input.ToUnifiedFormat(count, data);
auto input_data = UnifiedVectorFormat::GetData<T>(data);
for (idx_t i = 0; i < count; i++) {
auto idx = data.sel->get_index(i);
if (IS_NOT_NULL) {
auto other_key = ARTKey::CreateARTKey<T>(allocator, input_data[idx]);
keys[i].Concat(allocator, other_key);
continue;
}
// A previous column entry was NULL.
if (keys[i].Empty()) {
continue;
}
// This column entry is NULL, so we set the whole key to NULL.
if (!data.validity.RowIsValid(idx)) {
keys[i] = ARTKey();
continue;
}
// Concatenate the keys.
auto other_key = ARTKey::CreateARTKey<T>(allocator, input_data[idx]);
keys[i].Concat(allocator, other_key);
}
}
template <bool IS_NOT_NULL>
void GenerateKeysInternal(ArenaAllocator &allocator, DataChunk &input, unsafe_vector<ARTKey> &keys) {
switch (input.data[0].GetType().InternalType()) {
case PhysicalType::BOOL:
TemplatedGenerateKeys<bool, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::INT8:
TemplatedGenerateKeys<int8_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::INT16:
TemplatedGenerateKeys<int16_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::INT32:
TemplatedGenerateKeys<int32_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::INT64:
TemplatedGenerateKeys<int64_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::INT128:
TemplatedGenerateKeys<hugeint_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::UINT8:
TemplatedGenerateKeys<uint8_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::UINT16:
TemplatedGenerateKeys<uint16_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::UINT32:
TemplatedGenerateKeys<uint32_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::UINT64:
TemplatedGenerateKeys<uint64_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::UINT128:
TemplatedGenerateKeys<uhugeint_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::FLOAT:
TemplatedGenerateKeys<float, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::DOUBLE:
TemplatedGenerateKeys<double, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
case PhysicalType::VARCHAR:
TemplatedGenerateKeys<string_t, IS_NOT_NULL>(allocator, input.data[0], input.size(), keys);
break;
default:
throw InternalException("Invalid type for index");
}
// We concatenate the keys for each remaining column of a compound key.
for (idx_t i = 1; i < input.ColumnCount(); i++) {
switch (input.data[i].GetType().InternalType()) {
case PhysicalType::BOOL:
ConcatenateKeys<bool, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::INT8:
ConcatenateKeys<int8_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::INT16:
ConcatenateKeys<int16_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::INT32:
ConcatenateKeys<int32_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::INT64:
ConcatenateKeys<int64_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::INT128:
ConcatenateKeys<hugeint_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::UINT8:
ConcatenateKeys<uint8_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::UINT16:
ConcatenateKeys<uint16_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::UINT32:
ConcatenateKeys<uint32_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::UINT64:
ConcatenateKeys<uint64_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::UINT128:
ConcatenateKeys<uhugeint_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::FLOAT:
ConcatenateKeys<float, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::DOUBLE:
ConcatenateKeys<double, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
case PhysicalType::VARCHAR:
ConcatenateKeys<string_t, IS_NOT_NULL>(allocator, input.data[i], input.size(), keys);
break;
default:
throw InternalException("Invalid type for index");
}
}
}
template <>
void ART::GenerateKeys<>(ArenaAllocator &allocator, DataChunk &input, unsafe_vector<ARTKey> &keys) {
GenerateKeysInternal<false>(allocator, input, keys);
}
template <>
void ART::GenerateKeys<true>(ArenaAllocator &allocator, DataChunk &input, unsafe_vector<ARTKey> &keys) {
GenerateKeysInternal<true>(allocator, input, keys);
}
static bool KeyInputNeedConversion(const vector<LogicalType> &types, optional_idx storage_version) {
// We only started tracking the storage version of the index in v1.5.0.
// Old GEOMETRY columns (pre v1.5.0) had a different internal representation.
if (!storage_version.IsValid() || (storage_version.GetIndex() < 7)) {
for (auto &type : types) {
// ART does not support nested types, so we only need to check the top-level type.
if (type.id() == LogicalTypeId::GEOMETRY) {
return true;
}
}
}
return false;
}
static void ConvertKeyInput(DataChunk &input, DataChunk &result) {
vector<LogicalType> new_types;
for (auto &type : input.GetTypes()) {
if (type.id() == LogicalTypeId::GEOMETRY) {
new_types.push_back(LogicalType::BLOB);
} else {
new_types.push_back(type);
}
}
// Initialize the result chunk with the new types
result.Initialize(Allocator::DefaultAllocator(), new_types, input.size());
// Reference or convert the input data into the result chunk
for (idx_t i = 0; i < input.ColumnCount(); i++) {
if (input.data[i].GetType().id() == LogicalTypeId::GEOMETRY) {
Geometry::ToSpatialGeometry(input.data[i], result.data[i], input.size());
} else {
result.data[i].Reference(input.data[i]);
}
}
result.SetCardinality(input.size());
}
void ART::GenerateKeyVectors(ArenaAllocator &allocator, DataChunk &input, Vector &row_ids, unsafe_vector<ARTKey> &keys,
unsafe_vector<ARTKey> &row_id_keys) {
auto key_input = &input;
DataChunk converted_chunk;
// Do we need to convert the input first before generating keys?
if (KeyInputNeedConversion(input.GetTypes(), storage_version)) {
ConvertKeyInput(input, converted_chunk);
key_input = &converted_chunk;
}
GenerateKeys<>(allocator, *key_input, keys);
DataChunk row_id_chunk;
row_id_chunk.Initialize(Allocator::DefaultAllocator(), vector<LogicalType> {LogicalType::ROW_TYPE},
key_input->size());
row_id_chunk.data[0].Reference(row_ids);
row_id_chunk.SetCardinality(key_input->size());
GenerateKeys<>(allocator, row_id_chunk, row_id_keys);
}
//===--------------------------------------------------------------------===//
// Build from sorted data.
//===--------------------------------------------------------------------===//
ARTConflictType ART::Build(unsafe_vector<ARTKey> &keys, unsafe_vector<ARTKey> &row_ids, const idx_t row_count) {
ArenaAllocator arena(BufferAllocator::Get(db));
ARTBuilder builder(arena, *this, keys, row_ids);
builder.Init(tree, row_count - 1);
auto result = builder.Build();
if (result != ARTConflictType::NO_CONFLICT) {
return result;
}
#ifdef DEBUG
set<row_t> row_ids_debug;
Iterator it(*this);
it.FindMinimum(tree);
ARTKey empty_key = ARTKey();
RowIdSetOutput output(row_ids_debug, NumericLimits<idx_t>().Maximum());
it.Scan(empty_key, output, false);
D_ASSERT(row_count == row_ids_debug.size());
#endif
return ARTConflictType::NO_CONFLICT;
}
//===--------------------------------------------------------------------===//
// Insert and Constraint Checking
//===--------------------------------------------------------------------===//
ErrorData ART::Insert(IndexLock &l, DataChunk &chunk, Vector &row_ids) {
IndexAppendInfo info;
return Insert(l, chunk, row_ids, info);
}
ErrorData ART::Insert(IndexLock &l, DataChunk &chunk, Vector &row_ids, IndexAppendInfo &info) {
D_ASSERT(row_ids.GetType().InternalType() == ROW_TYPE);
auto row_count = chunk.size();
ArenaAllocator arena(BufferAllocator::Get(db));
unsafe_vector<ARTKey> keys(row_count);
unsafe_vector<ARTKey> row_id_keys(row_count);
GenerateKeyVectors(arena, chunk, row_ids, keys, row_id_keys);
return InsertKeys(arena, keys, row_id_keys, row_count, DeleteIndexInfo(info.delete_indexes), info.append_mode,
&chunk);
}
ErrorData ART::InsertKeys(ArenaAllocator &arena, unsafe_vector<ARTKey> &keys, unsafe_vector<ARTKey> &row_id_keys,
idx_t row_count, const DeleteIndexInfo &delete_info, IndexAppendMode append_mode,
optional_ptr<DataChunk> chunk) {
auto conflict_type = ARTConflictType::NO_CONFLICT;
optional_idx conflict_idx;
auto was_empty = !tree.HasMetadata();
// Insert the entries into the index.
for (idx_t i = 0; i < row_count; i++) {
if (keys[i].Empty()) {
continue;
}
conflict_type = ARTOperator::Insert(arena, *this, tree, keys[i], 0, row_id_keys[i], GateStatus::GATE_NOT_SET,
delete_info, append_mode);
if (conflict_type != ARTConflictType::NO_CONFLICT) {
conflict_idx = i;
break;
}
}
// Remove any previously inserted entries.
if (conflict_type != ARTConflictType::NO_CONFLICT) {
D_ASSERT(conflict_idx.IsValid());
for (idx_t i = 0; i < conflict_idx.GetIndex(); i++) {
if (keys[i].Empty()) {
continue;
}
D_ASSERT(tree.GetGateStatus() == GateStatus::GATE_NOT_SET);
ARTOperator::Delete(*this, tree, keys[i], row_id_keys[i]);
}
}
if (was_empty) {
// All nodes are in-memory.
VerifyAllocationsInternal();
}
if (conflict_type == ARTConflictType::TRANSACTION) {
// chunk is only null when called from MergeCheckpointDeltas.
auto msg = chunk ? AppendRowError(*chunk, conflict_idx.GetIndex()) : string("???");
return ErrorData(TransactionException("write-write conflict on key: \"%s\"", msg));
}
if (conflict_type == ARTConflictType::CONSTRAINT) {
// chunk is only null when called from MergeCheckpointDeltas.
auto msg = chunk ? AppendRowError(*chunk, conflict_idx.GetIndex()) : string("???");
return ErrorData(ConstraintException("PRIMARY KEY or UNIQUE constraint violation: duplicate key \"%s\"", msg));
}
#ifdef DEBUG
for (idx_t i = 0; i < row_count; i++) {
if (keys[i].Empty()) {
continue;
}
auto leaf = ARTOperator::Lookup(*this, tree, keys[i], 0);
D_ASSERT(leaf);
D_ASSERT(ARTOperator::LookupInLeaf(*this, *leaf, row_id_keys[i]));
}
#endif
return ErrorData();
}
ErrorData ART::Append(IndexLock &l, DataChunk &chunk, Vector &row_ids) {
// Execute all column expressions before inserting the data chunk.
DataChunk expr_chunk;
expr_chunk.Initialize(Allocator::DefaultAllocator(), logical_types);
ExecuteExpressions(chunk, expr_chunk);
// Now insert the data chunk.
IndexAppendInfo info;
return Insert(l, expr_chunk, row_ids, info);
}
ErrorData ART::Append(IndexLock &l, DataChunk &chunk, Vector &row_ids, IndexAppendInfo &info) {
// Execute all column expressions before inserting the data chunk.
DataChunk expr_chunk;
expr_chunk.Initialize(Allocator::DefaultAllocator(), logical_types);
ExecuteExpressions(chunk, expr_chunk);
// Now insert the data chunk.
return Insert(l, expr_chunk, row_ids, info);
}
void ART::VerifyAppend(DataChunk &chunk, IndexAppendInfo &info, optional_ptr<ConflictManager> manager) {
if (manager) {
D_ASSERT(manager->GetVerifyExistenceType() == VerifyExistenceType::APPEND);
return VerifyConstraint(chunk, info, *manager);
}
ConflictManager local_manager(VerifyExistenceType::APPEND, chunk.size());
VerifyConstraint(chunk, info, local_manager);
}
//===--------------------------------------------------------------------===//
// Drop and Delete
//===--------------------------------------------------------------------===//
void ART::CommitDrop(IndexLock &index_lock) {
for (auto &allocator : *allocators) {
allocator->Reset();
}
tree.Clear();
}
idx_t ART::TryDelete(IndexLock &state, DataChunk &entries, Vector &row_ids, optional_ptr<SelectionVector> deleted_sel,
optional_ptr<SelectionVector> non_deleted_sel) {
// FIXME: We could pass a row_count in here, as we sometimes don't have to delete all row IDs in the chunk,
// FIXME: but rather all row IDs up to the conflicting row.
auto row_count = entries.size();
DataChunk expr_chunk;
expr_chunk.Initialize(Allocator::DefaultAllocator(), logical_types);
ExecuteExpressions(entries, expr_chunk);
ArenaAllocator allocator(BufferAllocator::Get(db));
unsafe_vector<ARTKey> keys(row_count);
unsafe_vector<ARTKey> row_id_keys(row_count);
GenerateKeyVectors(allocator, expr_chunk, row_ids, keys, row_id_keys);
return DeleteKeys(keys, row_id_keys, row_count, deleted_sel, non_deleted_sel);
}
idx_t ART::DeleteKeys(unsafe_vector<ARTKey> &keys, unsafe_vector<ARTKey> &row_id_keys, idx_t row_count,
optional_ptr<SelectionVector> deleted_sel, optional_ptr<SelectionVector> non_deleted_sel) {
idx_t delete_count = 0;
for (idx_t i = 0; i < row_count; i++) {
bool deleted = true;
if (!keys[i].Empty()) {
D_ASSERT(tree.GetGateStatus() == GateStatus::GATE_NOT_SET);
deleted = ARTOperator::Delete(*this, tree, keys[i], row_id_keys[i]);
}
if (deleted) {
if (deleted_sel) {
deleted_sel->set_index(delete_count, i);
}
delete_count++;
} else if (non_deleted_sel) {
idx_t non_delete_count = i - delete_count;
non_deleted_sel->set_index(non_delete_count, i);
}
}
if (!tree.HasMetadata()) {
// No more allocations.
VerifyAllocationsInternal();
}
#ifdef DEBUG
for (idx_t i = 0; i < row_count; i++) {
if (keys[i].Empty()) {
continue;
}
auto leaf = ARTOperator::Lookup(*this, tree, keys[i], 0);
if (leaf) {
auto contains_row_id = ARTOperator::LookupInLeaf(*this, *leaf, row_id_keys[i]);
D_ASSERT(!contains_row_id);
}
}
#endif
return delete_count;
}
//===--------------------------------------------------------------------===//
// Point and range lookups
//===--------------------------------------------------------------------===//
bool ART::FullScan(idx_t max_count, set<row_t> &row_ids) {
if (!tree.HasMetadata()) {
return true;
}
Iterator it(*this);
it.FindMinimum(tree);
ARTKey empty_key = ARTKey();
RowIdSetOutput output(row_ids, max_count);
return it.Scan(empty_key, output, false) == ARTScanResult::COMPLETED;
}
bool ART::SearchEqual(ARTKey &key, idx_t max_count, set<row_t> &row_ids) {
auto leaf = ARTOperator::Lookup(*this, tree, key, 0);
if (!leaf) {
return true;
}
Iterator it(*this);
it.FindMinimum(*leaf);
ARTKey empty_key = ARTKey();
RowIdSetOutput output(row_ids, max_count);
return it.Scan(empty_key, output, false) == ARTScanResult::COMPLETED;
}
bool ART::SearchGreater(ARTKey &key, bool equal, idx_t max_count, set<row_t> &row_ids) {
if (!tree.HasMetadata()) {
return true;
}
// Find the lowest value that satisfies the predicate.
Iterator it(*this);
// Early-out, if the maximum value in the ART is lower than the lower bound.
if (!it.LowerBound(tree, key, equal)) {
return true;
}
// We continue the scan. We do not check the bounds as any value following this value is
// greater and satisfies our predicate.
RowIdSetOutput output(row_ids, max_count);
return it.Scan(ARTKey(), output, false) == ARTScanResult::COMPLETED;
}
bool ART::SearchLess(ARTKey &upper_bound, bool equal, idx_t max_count, set<row_t> &row_ids) {
if (!tree.HasMetadata()) {
return true;
}
// Find the minimum value in the ART: we start scanning from this value.
Iterator it(*this);
it.FindMinimum(tree);
// Early-out, if the minimum value is higher than the upper bound.
if (it.current_key.GreaterThan(upper_bound, equal, it.GetNestedDepth())) {
return true;
}
// Continue the scan until we reach the upper bound.
RowIdSetOutput output(row_ids, max_count);
return it.Scan(upper_bound, output, equal) == ARTScanResult::COMPLETED;
}
bool ART::SearchCloseRange(ARTKey &lower_bound, ARTKey &upper_bound, bool left_equal, bool right_equal, idx_t max_count,
set<row_t> &row_ids) {
// Find the first node that satisfies the left predicate.
Iterator it(*this);
// Early-out, if the maximum value in the ART is lower than the lower bound.
if (!it.LowerBound(tree, lower_bound, left_equal)) {
return true;
}
// Continue the scan until we reach the upper bound.
RowIdSetOutput output(row_ids, max_count);
return it.Scan(upper_bound, output, right_equal) == ARTScanResult::COMPLETED;
}
bool ART::CompoundKeyScan(IndexScanState &state, const idx_t max_count, set<row_t> &row_ids) {
auto &scan_state = state.Cast<ARTIndexCompoundKeyScanState>();
if (scan_state.values.size() != types.size()) {
return false;
}
for (idx_t i = 0; i < scan_state.values.size(); ++i) {
D_ASSERT(scan_state.values[i].type().InternalType() == types[i]);
}
ArenaAllocator arena_allocator(Allocator::Get(db));
// Make a compound key from the collected state values
auto compound_key = ARTKey::CreateKey(arena_allocator, scan_state.values[0], storage_version);
for (idx_t i = 1; i < scan_state.values.size(); ++i) {
auto part_key = ARTKey::CreateKey(arena_allocator, scan_state.values[i], storage_version);
compound_key.Concat(arena_allocator, part_key);
}
lock_guard<mutex> l(lock);
return SearchEqual(compound_key, max_count, row_ids);
}
bool ART::Scan(IndexScanState &state, const idx_t max_count, set<row_t> &row_ids) {
auto &scan_state = state.Cast<ARTIndexScanState>();
if (scan_state.values[0].IsNull()) {
// full scan
lock_guard<mutex> l(lock);
return FullScan(max_count, row_ids);
}
D_ASSERT(scan_state.values[0].type().InternalType() == types[0]);
ArenaAllocator arena_allocator(Allocator::Get(db));
auto key = ARTKey::CreateKey(arena_allocator, scan_state.values[0], storage_version);
lock_guard<mutex> l(lock);
if (scan_state.values[1].IsNull()) {
// Single predicate.
switch (scan_state.expressions[0]) {
case ExpressionType::COMPARE_EQUAL:
return SearchEqual(key, max_count, row_ids);
case ExpressionType::COMPARE_GREATERTHANOREQUALTO:
return SearchGreater(key, true, max_count, row_ids);
case ExpressionType::COMPARE_GREATERTHAN:
return SearchGreater(key, false, max_count, row_ids);
case ExpressionType::COMPARE_LESSTHANOREQUALTO:
return SearchLess(key, true, max_count, row_ids);
case ExpressionType::COMPARE_LESSTHAN:
return SearchLess(key, false, max_count, row_ids);
default:
throw InternalException("Index scan type not implemented");
}
}
// Two predicates.
D_ASSERT(scan_state.values[1].type().InternalType() == types[0]);
auto upper_bound = ARTKey::CreateKey(arena_allocator, scan_state.values[1], storage_version);
bool left_equal = scan_state.expressions[0] == ExpressionType ::COMPARE_GREATERTHANOREQUALTO;
bool right_equal = scan_state.expressions[1] == ExpressionType ::COMPARE_LESSTHANOREQUALTO;
return SearchCloseRange(key, upper_bound, left_equal, right_equal, max_count, row_ids);
}
//===--------------------------------------------------------------------===//
// More Constraint Checking
//===--------------------------------------------------------------------===//
string ART::GenerateErrorKeyName(DataChunk &input, idx_t row_idx) {
DataChunk expr_chunk;
expr_chunk.Initialize(Allocator::DefaultAllocator(), logical_types);
ExecuteExpressions(input, expr_chunk);
string key_name;
for (idx_t k = 0; k < expr_chunk.ColumnCount(); k++) {
if (k > 0) {
key_name += ", ";
}
key_name += unbound_expressions[k]->GetName() + ": " + expr_chunk.data[k].GetValue(row_idx).ToString();
}
return key_name;
}
string ART::GenerateConstraintErrorMessage(VerifyExistenceType verify_type, const string &key_name) {
switch (verify_type) {
case VerifyExistenceType::APPEND: {
// APPEND to PK/UNIQUE table, but node/key already exists in PK/UNIQUE table.
string type = IsPrimary() ? "primary key" : "unique";
return StringUtil::Format("Duplicate key \"%s\" violates %s constraint.", key_name, type);
}
case VerifyExistenceType::APPEND_FK: {
// APPEND_FK to FK table, node/key does not exist in PK/UNIQUE table.
return StringUtil::Format(
"Violates foreign key constraint because key \"%s\" does not exist in the referenced table", key_name);
}
case VerifyExistenceType::DELETE_FK: {
// DELETE_FK that still exists in a FK table, i.e., not a valid delete.
return StringUtil::Format(
"Violates foreign key constraint because key \"%s\" is still referenced by a foreign "
"key in a different table. If this is an unexpected constraint violation, please refer to our "
"foreign key limitations in the documentation",
key_name);
}
default:
throw NotImplementedException("Type not implemented for VerifyExistenceType");
}
}
void ART::VerifyLeaf(const Node &leaf, const ARTKey &key, DeleteIndexInfo delete_index_info, ConflictManager &manager,
optional_idx &conflict_idx, idx_t i) {
// Get the set of deleted row ids for this value if we have any delete indexes
vector<row_t> deleted_row_ids;
if (delete_index_info.delete_indexes) {
for (auto &index : *delete_index_info.delete_indexes) {
auto &delete_art = index.get().Cast<ART>();
auto deleted_leaf = ARTOperator::Lookup(delete_art, delete_art.tree, key, 0);
if (!deleted_leaf) {
continue;
}
// All leaves in the delete ART are inlined.
if (deleted_leaf->GetType() != NType::LEAF_INLINED) {
throw InternalException("Non-inlined leaf?");
}
auto deleted_row_id = deleted_leaf->GetRowId();
deleted_row_ids.push_back(deleted_row_id);
}
}
if (leaf.GetType() == NType::LEAF_INLINED) {
auto this_row_id = leaf.GetRowId();
if (!deleted_row_ids.empty()) {
// The leaf is inlined, and the same key exists in the delete ART.
// check if the row-id matches - if it does there is no conflict
for (auto &deleted_row_id : deleted_row_ids) {
if (deleted_row_id == this_row_id) {
return;
}
}
}
if (manager.AddHit(i, this_row_id)) {
conflict_idx = i;
}
return;
}
// Fast path for FOREIGN KEY constraints.
// Up to here, the above code paths work implicitly for FKs, as the leaf is inlined.
// FIXME: proper foreign key + delete ART support.
if (index_constraint_type == IndexConstraintType::FOREIGN) {
D_ASSERT(deleted_row_ids.empty());
// We don't handle FK conflicts in UPSERT, so the row ID should not matter.
if (manager.AddHit(i, MAX_ROW_ID)) {
conflict_idx = i;
}
return;
}
// Scan the two row IDs in the leaf.
Iterator it(*this);
it.FindMinimum(leaf);
ARTKey empty_key = ARTKey();
set<row_t> row_ids;
RowIdSetOutput output(row_ids, 2);
auto result = it.Scan(empty_key, output, false);
if (result != ARTScanResult::COMPLETED || row_ids.size() != 2) {
throw InternalException("VerifyLeaf expects exactly two row IDs to be scanned");
}
if (!deleted_row_ids.empty()) {
for (const auto row_id : row_ids) {
for (auto deleted_row_id : deleted_row_ids) {
if (deleted_row_id == row_id) {
return;
}
}
}
}
auto row_id_it = row_ids.begin();
if (manager.AddHit(i, *row_id_it)) {
conflict_idx = i;
}
row_id_it++;
manager.AddSecondHit(i, *row_id_it);
}
void ART::VerifyConstraint(DataChunk &chunk, IndexAppendInfo &info, ConflictManager &manager) {
// Lock the index during constraint checking.
lock_guard<mutex> l(lock);
DataChunk expr_chunk;
expr_chunk.Initialize(Allocator::DefaultAllocator(), logical_types);
ExecuteExpressions(chunk, expr_chunk);