Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::ConflictManager Class Reference
Collaboration diagram for duckdb::ConflictManager:

Classes

struct  ConflictData

Public Member Functions

 ConflictManager (const VerifyExistenceType lookup_type, const idx_t chunk_size, optional_ptr< ConflictInfo > conflict_info=nullptr)
bool AddHit (const idx_t index_in_chunk, const row_t row_id)
 Returns true, if we need to throw, otherwise, adds the hit and returns false.
bool AddSecondHit (const idx_t index_in_chunk, const row_t row_id)
 Returns true, if we need to throw, otherwise, adds the second hit and returns false.
bool AddNull (const idx_t index_in_chunk)
 Returns true, if we need to throw, otherwise, adds the NULL and returns false.
optional_idx GetFirstInvalidIndex (const idx_t count, const bool negate=false)
 Returns the index of the first (in)valid row, if any.
void FinalizeGlobal (DuckTransaction &transaction, DataTable &table)
 Finalizes a global conflict manager.
void FinalizeLocal (DataTable &table, LocalStorage &storage)
 Finalizes a local conflict manager.
void FinishLookup ()
 Determines if we are finished registering conflicts.
const ConflictInfo & GetConflictInfo () const
 Get the conflict information.
void SetMode (const ConflictManagerMode mode_p)
 Sets the mode of the conflict manager.
void AddIndex (BoundIndex &index, optional_ptr< BoundIndex > delete_index)
 Adds an index and its respective delete_index.
bool IndexMatches (BoundIndex &index)
 Returns true, if the index is in this conflict manager.
const vector< reference< BoundIndex > > & MatchingIndexes () const
 Returns a reference to the matching indexes.
const vector< optional_ptr< BoundIndex > > & MatchingDeleteIndexes () const
 Returns a reference to the matching delete indexes.
VerifyExistenceType GetVerifyExistenceType () const
 Returns the existence verification type.
bool HasConflicts () const
 Returns true, if there are any conflicts, else false.
idx_t ConflictCount () const
 Returns the number of conflicts.
Vector & GetRowIds ()
SelectionVector & GetInvertedSel () const
idx_t GetFirstIndex () const
 Returns the first index in a chunk with a conflict.
ValidityArray & GetFirstValidity ()
 Returns the validity array of the first conflict data.

Static Public Attributes

static constexpr uint8_t FIRST = 0
static constexpr uint8_t SECOND = 1

Private Member Functions

bool IsConflict (LookupResultType type)
 Returns true, if we register a conflict for the lookup type.
bool AddHit (const idx_t index_in_chunk, const std::function< void()> &callback)
 Adds a hit to the conflicts.
void Finalize (const std::function< bool(const row_t row_id)> &callback)
 Determine visible row ID for each index with two possible row IDs.
bool ShouldThrow (const idx_t index_in_chunk) const
 Returns true, if the conflict manager should throw an exception, else false.
bool IgnoreNulls () const
 Returns true, if we ignore NULLs, else false.
ConflictData & GetConflictData (const idx_t i)
void AddRowId (const idx_t index_in_chunk, const row_t row_id)
 Adds a row ID to the primary conflict data.
void AddSecondRowId (const idx_t index_in_chunk, const row_t row_id)
 Adds a row ID to the secondary conflict data.

Private Attributes

VerifyExistenceType verify_existence_type
 The type of constraint verification for which we're using the conflict manager.
idx_t chunk_size
 The count of the data chunk for which we've created the conflict manager.
optional_ptr< ConflictInfo > conflict_info
 Optional information to match indexes to the conflict target.
ConflictManagerMode mode
 The mode of the conflict manager.
vector< reference< BoundIndex > > matching_indexes
 Indexes matching the conflict target.
vector< optional_ptr< BoundIndex > > matching_delete_indexes
 Delete indexes matching the conflict target.
case_insensitive_set_t index_names
 All matching indexes by their name (unique identifier).
unordered_set< idx_t > conflict_rows
 Registers all conflicting rows in a data chunk.
bool finished = false
 True, if we can skip recording any further conflicts.
array< ConflictData, 2 > conflict_data

Detailed Description

The conflict manager tracks conflicts during constraint verification. It decides when/if to throw, and records conflicts to be used later in case of ON CONFLICT DO. The conflict manager operates on a per-chunk basis, i.e., we construct a new conflict manager per incoming chunk. On conflict manager can gather conflicts across multiple indexes, if they match the ON CONFLICT DO target.

Constructor & Destructor Documentation

◆ ConflictManager()

duckdb::ConflictManager::ConflictManager ( const VerifyExistenceType lookup_type,
const idx_t chunk_size,
optional_ptr< ConflictInfo > conflict_info = nullptr )
116619 mode(ConflictManagerMode::THROW) {
116620}
VerifyExistenceType verify_existence_type
The type of constraint verification for which we're using the conflict manager.
Definition duckdb.cpp:55889
ConflictManagerMode mode
The mode of the conflict manager.
Definition duckdb.cpp:55895
optional_ptr< ConflictInfo > conflict_info
Optional information to match indexes to the conflict target.
Definition duckdb.cpp:55893
idx_t chunk_size
The count of the data chunk for which we've created the conflict manager.
Definition duckdb.cpp:55891

Member Function Documentation

◆ AddHit() [1/2]

bool duckdb::ConflictManager::AddHit ( const idx_t index_in_chunk,
const row_t row_id )

Returns true, if we need to throw, otherwise, adds the hit and returns false.

116676 {
116677 return AddHit(index_in_chunk, [&]() { AddRowId(index_in_chunk, row_id); });
116678}
void AddRowId(const idx_t index_in_chunk, const row_t row_id)
Adds a row ID to the primary conflict data.
Definition duckdb.cpp:55986
bool AddHit(const idx_t index_in_chunk, const row_t row_id)
Returns true, if we need to throw, otherwise, adds the hit and returns false.
Definition duckdb.cpp:116676
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddSecondHit()

bool duckdb::ConflictManager::AddSecondHit ( const idx_t index_in_chunk,
const row_t row_id )

Returns true, if we need to throw, otherwise, adds the second hit and returns false.

116680 {
116681 return AddHit(index_in_chunk, [&]() { AddSecondRowId(index_in_chunk, row_id); });
116682}
void AddSecondRowId(const idx_t index_in_chunk, const row_t row_id)
Adds a row ID to the secondary conflict data.
Definition duckdb.cpp:55999
Here is the call graph for this function:

◆ AddNull()

bool duckdb::ConflictManager::AddNull ( const idx_t index_in_chunk)

Returns true, if we need to throw, otherwise, adds the NULL and returns false.

116684 {
116685 D_ASSERT(index_in_chunk < chunk_size);
116686 if (!IsConflict(LookupResultType::LOOKUP_NULL)) {
116687 return false;
116688 }
116689 auto row_id = static_cast<row_t>(DConstants::INVALID_INDEX);
116690 return AddHit(index_in_chunk, row_id);
116691}
bool IsConflict(LookupResultType type)
Returns true, if we register a conflict for the lookup type.
Definition duckdb.cpp:116635
int64_t row_t
The type used for row identifiers.
Definition duckdb.hpp:240
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFirstInvalidIndex()

optional_idx duckdb::ConflictManager::GetFirstInvalidIndex ( const idx_t count,
const bool negate = false )

Returns the index of the first (in)valid row, if any.

116693 {
116694 auto &validity = GetFirstValidity();
116695 for (idx_t i = 0; i < count; i++) {
116696 if (negate && !validity.RowIsValid(i)) {
116697 return i;
116698 } else if (!negate && validity.RowIsValid(i)) {
116699 return i;
116700 }
116701 }
116702 return optional_idx();
116703}
ValidityArray & GetFirstValidity()
Returns the validity array of the first conflict data.
Definition duckdb.cpp:55882
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ FinalizeGlobal()

void duckdb::ConflictManager::FinalizeGlobal ( DuckTransaction & transaction,
DataTable & table )

Finalizes a global conflict manager.

116723 {
116724 Finalize([&](const row_t row_id) { return table.CanFetch(transaction, row_id); });
116725}
void Finalize(const std::function< bool(const row_t row_id)> &callback)
Determine visible row ID for each index with two possible row IDs.
Definition duckdb.cpp:116705
Here is the call graph for this function:

◆ FinalizeLocal()

void duckdb::ConflictManager::FinalizeLocal ( DataTable & table,
LocalStorage & storage )

Finalizes a local conflict manager.

116727 {
116728 Finalize([&](const row_t row_id) { return storage.CanFetch(table, row_id); });
116729}
Here is the call graph for this function:

◆ FinishLookup()

void duckdb::ConflictManager::FinishLookup ( )

Determines if we are finished registering conflicts.

116622 {
116623 if (mode == ConflictManagerMode::THROW) {
116624 return;
116625 }
116626 if (!conflict_info->SingleIndexTarget()) {
116627 return;
116628 }
116629
116630 // We recorded conflicts of one of the indexes.
116631 // We can skip any duplicate indexes matching the same conflict target.
116633}
bool finished
True, if we can skip recording any further conflicts.
Definition duckdb.cpp:55907
bool HasConflicts() const
Returns true, if there are any conflicts, else false.
Definition duckdb.cpp:55855
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConflictInfo()

const ConflictInfo & duckdb::ConflictManager::GetConflictInfo ( ) const
inline

Get the conflict information.

55822 {
55823 return *conflict_info;
55824 }

◆ SetMode()

void duckdb::ConflictManager::SetMode ( const ConflictManagerMode mode_p)
inline

Sets the mode of the conflict manager.

55826 {
55827 D_ASSERT(mode_p != ConflictManagerMode::SCAN || conflict_info != nullptr);
55828 mode = mode_p;
55829 }

◆ AddIndex()

void duckdb::ConflictManager::AddIndex ( BoundIndex & index,
optional_ptr< BoundIndex > delete_index )
inline

Adds an index and its respective delete_index.

55832 {
55833 matching_indexes.push_back(index);
55834 matching_delete_indexes.push_back(delete_index);
55835 index_names.insert(index.name);
55836 }
vector< reference< BoundIndex > > matching_indexes
Indexes matching the conflict target.
Definition duckdb.cpp:55898
case_insensitive_set_t index_names
All matching indexes by their name (unique identifier).
Definition duckdb.cpp:55902
vector< optional_ptr< BoundIndex > > matching_delete_indexes
Delete indexes matching the conflict target.
Definition duckdb.cpp:55900
index

◆ IndexMatches()

bool duckdb::ConflictManager::IndexMatches ( BoundIndex & index)
inline

Returns true, if the index is in this conflict manager.

55838 {
55839 return index_names.find(index.name) != index_names.end();
55840 }

◆ MatchingIndexes()

const vector< reference< BoundIndex > > & duckdb::ConflictManager::MatchingIndexes ( ) const
inline

Returns a reference to the matching indexes.

55842 {
55843 return matching_indexes;
55844 }

◆ MatchingDeleteIndexes()

const vector< optional_ptr< BoundIndex > > & duckdb::ConflictManager::MatchingDeleteIndexes ( ) const
inline

Returns a reference to the matching delete indexes.

55846 {
55848 }

◆ GetVerifyExistenceType()

VerifyExistenceType duckdb::ConflictManager::GetVerifyExistenceType ( ) const
inline

Returns the existence verification type.

55851 {
55852 return verify_existence_type;
55853 }
Here is the caller graph for this function:

◆ HasConflicts()

bool duckdb::ConflictManager::HasConflicts ( ) const
inline

Returns true, if there are any conflicts, else false.

55855 {
55856 return conflict_data[FIRST].sel != nullptr;
55857 }
array< ConflictData, 2 > conflict_data
Definition duckdb.cpp:55957
Here is the caller graph for this function:

◆ ConflictCount()

idx_t duckdb::ConflictManager::ConflictCount ( ) const
inline

Returns the number of conflicts.

55859 {
55860 if (!HasConflicts()) {
55861 return 0;
55862 }
55863 return conflict_data[FIRST].count;
55864 }
Here is the call graph for this function:

◆ GetRowIds()

Vector & duckdb::ConflictManager::GetRowIds ( )
inline

Returns a reference to the row IDs. Must be called after Finalize[Global|Local].

55867 {
55868 D_ASSERT(!conflict_data[SECOND].sel);
55869 return *GetConflictData(FIRST).row_ids;
55870 }
ConflictData & GetConflictData(const idx_t i)
Definition duckdb.cpp:55973
unique_ptr< Vector > row_ids
Row IDs.
Definition duckdb.cpp:55919
Here is the call graph for this function:

◆ GetInvertedSel()

SelectionVector & duckdb::ConflictManager::GetInvertedSel ( ) const
inline

Returns a reference to the inverted selection vector. Must be called after Finalize[Global|Local].

55873 {
55874 D_ASSERT(!conflict_data[SECOND].sel);
55875 return *conflict_data[FIRST].inverted_sel;
55876 }
Here is the caller graph for this function:

◆ GetFirstIndex()

idx_t duckdb::ConflictManager::GetFirstIndex ( ) const
inline

Returns the first index in a chunk with a conflict.

55878 {
55879 return GetInvertedSel().get_index(0);
55880 }
SelectionVector & GetInvertedSel() const
Definition duckdb.cpp:55873
Here is the call graph for this function:

◆ GetFirstValidity()

ValidityArray & duckdb::ConflictManager::GetFirstValidity ( )
inline

Returns the validity array of the first conflict data.

55882 {
55883 D_ASSERT(HasConflicts());
55884 return conflict_data[FIRST].validity;
55885 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsConflict()

bool duckdb::ConflictManager::IsConflict ( LookupResultType type)
private

Returns true, if we register a conflict for the lookup type.

116635 {
116636 switch (type) {
116637 case LookupResultType::LOOKUP_NULL:
116638 if (IgnoreNulls()) {
116639 return false;
116640 }
116641 // If we ignore NULL, then we treat this as a hit.
116642 return IsConflict(LookupResultType::LOOKUP_HIT);
116643 case LookupResultType::LOOKUP_HIT:
116644 return true;
116645 case LookupResultType::LOOKUP_MISS:
116646 return false;
116647 default:
116648 throw NotImplementedException("Type not implemented for LookupResultType");
116649 }
116650}
bool IgnoreNulls() const
Returns true, if we ignore NULLs, else false.
Definition duckdb.cpp:116742
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddHit() [2/2]

bool duckdb::ConflictManager::AddHit ( const idx_t index_in_chunk,
const std::function< void()> & callback )
private

Adds a hit to the conflicts.

116652 {
116653 D_ASSERT(index_in_chunk < chunk_size);
116654 if (ShouldThrow(index_in_chunk)) {
116655 return true;
116656 }
116657
116658 if (mode == ConflictManagerMode::THROW) {
116659 // On THROW, and if the index is part of the previously scanned conflicts,
116660 // we ignore the conflict.
116661 D_ASSERT(!ShouldThrow(index_in_chunk));
116662 return false;
116663 }
116664 D_ASSERT(conflict_info);
116665
116666 // Add the conflict and don't throw.
116667 D_ASSERT(mode == ConflictManagerMode::SCAN);
116668 D_ASSERT(!ShouldThrow(index_in_chunk));
116669 if (finished) {
116670 return false;
116671 }
116672 callback();
116673 return false;
116674}
bool ShouldThrow(const idx_t index_in_chunk) const
Returns true, if the conflict manager should throw an exception, else false.
Definition duckdb.cpp:116731
Here is the call graph for this function:

◆ Finalize()

void duckdb::ConflictManager::Finalize ( const std::function< bool(const row_t row_id)> & callback)
private

Determine visible row ID for each index with two possible row IDs.

116705 {
116706 if (!GetConflictData(SECOND).sel) {
116707 return;
116708 }
116709
116710 for (idx_t i = 0; i < GetConflictData(SECOND).count; i++) {
116711 auto row_id = GetConflictData(SECOND).row_ids_data[i];
116712 if (callback(row_id)) {
116713 // Replace the primary row ID with the secondary row ID.
116714 auto index = GetConflictData(SECOND).inverted_sel->get_index(i);
116715 auto count = GetConflictData(FIRST).sel->get_index(index);
116716 GetConflictData(FIRST).row_ids_data[count] = row_id;
116717 }
116718 }
116719
116720 GetConflictData(SECOND).Reset();
116721}
idx_t count
Conflict count.
Definition duckdb.cpp:55913
unique_ptr< SelectionVector > sel
Links the conflicting rows to their row IDs (index_in_chunk -> count).
Definition duckdb.cpp:55915
row_t * row_ids_data
Optional row ID data.
Definition duckdb.cpp:55921
unique_ptr< SelectionVector > inverted_sel
Inverted selection vector to slice the input chunk (count -> index_in_chunk).
Definition duckdb.cpp:55917
void Reset()
Resets the conflict data.
Definition duckdb.cpp:55944
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ShouldThrow()

bool duckdb::ConflictManager::ShouldThrow ( const idx_t index_in_chunk) const
private

Returns true, if the conflict manager should throw an exception, else false.

116731 {
116732 // Never throw on scans.
116733 if (mode == ConflictManagerMode::SCAN) {
116734 return false;
116735 }
116736
116737 // If we have already seen a conflict for this index, then we don't throw.
116738 D_ASSERT(mode == ConflictManagerMode::THROW);
116739 return conflict_rows.find(index_in_chunk) == conflict_rows.end();
116740}
unordered_set< idx_t > conflict_rows
Registers all conflicting rows in a data chunk.
Definition duckdb.cpp:55905
Here is the caller graph for this function:

◆ IgnoreNulls()

bool duckdb::ConflictManager::IgnoreNulls ( ) const
private

Returns true, if we ignore NULLs, else false.

116742 {
116743 switch (verify_existence_type) {
116744 case VerifyExistenceType::APPEND:
116745 return true;
116746 case VerifyExistenceType::APPEND_FK:
116747 return false;
116748 case VerifyExistenceType::DELETE_FK:
116749 return true;
116750 default:
116751 throw InternalException("Type not implemented for VerifyExistenceType");
116752 }
116753}
Here is the caller graph for this function:

◆ GetConflictData()

ConflictData & duckdb::ConflictManager::GetConflictData ( const idx_t i)
inlineprivate

Returns a reference to the conflict data (FIRST or SECOND), and initializes it, if uninitialized.

55973 {
55974 if (conflict_data[i].sel) {
55975 return conflict_data[i];
55976 }
55977 conflict_data[i].sel = make_uniq<SelectionVector>(chunk_size);
55978 conflict_data[i].inverted_sel = make_uniq<SelectionVector>(chunk_size);
55979 conflict_data[i].validity.Initialize(chunk_size, false);
55980 conflict_data[i].row_ids = make_uniq<Vector>(LogicalType::ROW_TYPE, chunk_size);
55981 conflict_data[i].row_ids_data = FlatVector::GetData<row_t>(*conflict_data[i].row_ids);
55982 return conflict_data[i];
55983 }
Here is the caller graph for this function:

◆ AddRowId()

void duckdb::ConflictManager::AddRowId ( const idx_t index_in_chunk,
const row_t row_id )
inlineprivate

Adds a row ID to the primary conflict data.

55986 {
55987 // Only ON CONFLICT DO NOTHING can have multiple conflict targets,
55988 // which can cause multiple row IDs per index_in_chunk.
55989 // We let them overwrite each other, as we don't need the row IDs later.
55990 auto elem = conflict_rows.find(index_in_chunk);
55991 if (elem == conflict_rows.end()) {
55992 // We have not yet seen this conflict: insert.
55993 conflict_rows.insert(index_in_chunk);
55994 GetConflictData(FIRST).Insert(index_in_chunk, row_id);
55995 }
55996 }
void Insert(const idx_t index_in_chunk, const row_t row_id)
Inserts a conflict into the two selection vectors, the row IDs, and the validity.
Definition duckdb.cpp:55927
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddSecondRowId()

void duckdb::ConflictManager::AddSecondRowId ( const idx_t index_in_chunk,
const row_t row_id )
inlineprivate

Adds a row ID to the secondary conflict data.

55999 {
56000 D_ASSERT(conflict_rows.find(index_in_chunk) != conflict_rows.end());
56001 GetConflictData(SECOND).Insert(index_in_chunk, row_id);
56002 }
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ conflict_data

array<ConflictData, 2> duckdb::ConflictManager::conflict_data
private

With the introduction of delete indexes, it is now possible to have up to two row IDs in a UNIQUE/PRIMARY KEY leaf. Only one of the row IDs is visible to the transaction in which we're using the conflict manager. Thus, we need to register both row IDs when scanning, and later Finalize the conflict data to only contain the visible row ID.


The documentation for this class was generated from the following file: