44#include " duckdb/common/unordered_map.hpp"
55#include " duckdb/common/vector_operations/vector_operations.hpp"
66#include " duckdb/execution/expression_executor.hpp"
7+ #include " duckdb/execution/index/art/art_builder.hpp"
78#include " duckdb/execution/index/art/art_key.hpp"
9+ #include " duckdb/execution/index/art/art_merger.hpp"
10+ #include " duckdb/execution/index/art/art_operator.hpp"
11+ #include " duckdb/execution/index/art/art_scanner.hpp"
812#include " duckdb/execution/index/art/base_leaf.hpp"
913#include " duckdb/execution/index/art/base_node.hpp"
1014#include " duckdb/execution/index/art/iterator.hpp"
2125#include " duckdb/storage/metadata/metadata_reader.hpp"
2226#include " duckdb/storage/table/scan_state.hpp"
2327#include " duckdb/storage/table_io_manager.hpp"
24- #include " duckdb/execution/index/art/art_scanner.hpp"
25- #include " duckdb/execution/index/art/art_merger.hpp"
26- #include " duckdb/execution/index/art/art_operator.hpp"
2728
2829namespace duckdb {
2930
@@ -424,76 +425,17 @@ void ART::GenerateKeyVectors(ArenaAllocator &allocator, DataChunk &input, Vector
424425}
425426
426427// ===--------------------------------------------------------------------===//
427- // Construct from sorted data.
428+ // Build from sorted data.
428429// ===--------------------------------------------------------------------===//
429430
430- bool ART::ConstructInternal (const unsafe_vector<ARTKey> &keys, const unsafe_vector<ARTKey> &row_ids, Node &node,
431- ARTKeySection §ion) {
432- D_ASSERT (section.start < keys.size ());
433- D_ASSERT (section.end < keys.size ());
434- D_ASSERT (section.start <= section.end );
435-
436- auto &start = keys[section.start ];
437- auto &end = keys[section.end ];
438- D_ASSERT (start.len != 0 );
439-
440- // Increment the depth until we reach a leaf or find a mismatching byte.
441- auto prefix_depth = section.depth ;
442- while (start.len != section.depth && start.ByteMatches (end, section.depth )) {
443- section.depth ++;
444- }
445-
446- if (start.len == section.depth ) {
447- // We reached a leaf. All the bytes of start_key and end_key match.
448- auto row_id_count = section.end - section.start + 1 ;
449- if (IsUnique () && row_id_count != 1 ) {
450- return false ;
451- }
452-
453- reference<Node> ref (node);
454- auto count = UnsafeNumericCast<uint8_t >(start.len - prefix_depth);
455- Prefix::New (*this , ref, start, prefix_depth, count);
456- if (row_id_count == 1 ) {
457- Leaf::New (ref, row_ids[section.start ].GetRowId ());
458- } else {
459- // Loop and insert the row IDs.
460- // We cannot use Construct in the leaf because row IDs are not sorted.
461- ArenaAllocator arena (BufferAllocator::Get (db));
462- for (idx_t i = section.start ; i < section.start + row_id_count; i++) {
463- ARTOperator::Insert (arena, *this , ref, row_ids[i], 0 , row_ids[i], GateStatus::GATE_SET, nullptr ,
464- IndexAppendMode::DEFAULT);
465- }
466- ref.get ().SetGateStatus (GateStatus::GATE_SET);
467- }
468- return true ;
469- }
470-
471- // Create a new node and recurse.
472- unsafe_vector<ARTKeySection> children;
473- section.GetChildSections (children, keys);
474-
475- // Create the prefix.
476- reference<Node> ref (node);
477- auto prefix_length = section.depth - prefix_depth;
478- Prefix::New (*this , ref, start, prefix_depth, prefix_length);
479-
480- // Create the node.
481- Node::New (*this , ref, Node::GetNodeType (children.size ()));
482- for (auto &child : children) {
483- Node new_child;
484- auto success = ConstructInternal (keys, row_ids, new_child, child);
485- Node::InsertChild (*this , ref, child.key_byte , new_child);
486- if (!success) {
487- return false ;
488- }
489- }
490- return true ;
491- }
431+ ARTConflictType ART::Build (unsafe_vector<ARTKey> &keys, unsafe_vector<ARTKey> &row_ids, const idx_t row_count) {
432+ ArenaAllocator arena (BufferAllocator::Get (db));
433+ ARTBuilder builder (arena, *this , keys, row_ids);
434+ builder.Init (tree, row_count - 1 );
492435
493- bool ART::Construct (unsafe_vector<ARTKey> &keys, unsafe_vector<ARTKey> &row_ids, const idx_t row_count) {
494- ARTKeySection section (0 , row_count - 1 , 0 , 0 );
495- if (!ConstructInternal (keys, row_ids, tree, section)) {
496- return false ;
436+ auto result = builder.Build ();
437+ if (result != ARTConflictType::NO_CONFLICT) {
438+ return result;
497439 }
498440
499441#ifdef DEBUG
@@ -504,7 +446,8 @@ bool ART::Construct(unsafe_vector<ARTKey> &keys, unsafe_vector<ARTKey> &row_ids,
504446 it.Scan (empty_key, NumericLimits<row_t >().Maximum (), row_ids_debug, false );
505447 D_ASSERT (row_count == row_ids_debug.size ());
506448#endif
507- return true ;
449+
450+ return ARTConflictType::NO_CONFLICT;
508451}
509452
510453// ===--------------------------------------------------------------------===//
0 commit comments