The sink method is called constantly with new input, as long as new input is available. Note that this method CAN be called in parallel, proper locking is needed when accessing dat a inside the GlobalSinkState.
194833 {
194834 auto &g_state = input.global_state.Cast<DeleteGlobalState>();
194835 auto &l_state = input.local_state.Cast<DeleteLocalState>();
194836
194837 auto &transaction = DuckTransaction::Get(context.client, table.db);
194838 auto &row_ids = chunk.
data[row_id_index];
194839
194840 lock_guard<mutex> delete_guard(g_state.delete_lock);
194841 if (!return_chunk && !g_state.has_unique_indexes) {
194842 g_state.deleted_count += table.Delete(*l_state.delete_state, context.client, row_ids, chunk.size());
194843 return SinkResultType::NEED_MORE_INPUT;
194844 }
194845
194846 auto types = table.GetTypes();
194847 auto to_be_fetched = vector<bool>(
types.size(), return_chunk);
194848 vector<StorageIndex> column_ids;
194849 vector<LogicalType> column_types;
194850 if (return_chunk) {
194851
194852 column_types =
types;
194853 for (
idx_t i = 0; i < table.ColumnCount(); i++) {
194854 column_ids.emplace_back(i);
194855 }
194856
194857 } else {
194858
194859 auto &local_storage = LocalStorage::Get(context.client, table.db);
194860 auto storage = local_storage.GetStorage(table);
194861 unordered_set<column_t> indexed_column_id_set;
194862 for (auto &index : storage->delete_indexes.Indexes()) {
194863 if (!
index.IsBound() || !
index.IsUnique()) {
194864 continue;
194865 }
194866 auto &set =
index.GetColumnIdSet();
194867 indexed_column_id_set.insert(set.begin(), set.end());
194868 }
194869 for (auto &col : indexed_column_id_set) {
194870 column_ids.emplace_back(col);
194871 }
194872 sort(column_ids.begin(), column_ids.end());
194873 for (auto &col : column_ids) {
194874 auto i = col.GetPrimaryIndex();
194875 to_be_fetched[i] = true;
194876 column_types.push_back(
types[i]);
194877 }
194878 }
194879
194880 l_state.delete_chunk.Reset();
194881 row_ids.Flatten(chunk.size());
194882
194883
194884 DataChunk fetch_chunk;
194885 fetch_chunk.
Initialize(Allocator::Get(context.client), column_types, chunk.size());
194886 auto fetch_state = ColumnFetchState();
194887 table.Fetch(transaction, fetch_chunk, column_ids, row_ids, chunk.size(), fetch_state);
194888
194889
194890 idx_t fetch_idx = 0;
194891 for (
idx_t i = 0; i < table.ColumnCount(); i++) {
194892 if (to_be_fetched[i]) {
194893 l_state.delete_chunk.data[i].Reference(fetch_chunk.
data[fetch_idx++]);
194894 continue;
194895 }
194896 l_state.delete_chunk.data[i].Reference(Value(
types[i]));
194897 }
194898 l_state.delete_chunk.SetCardinality(fetch_chunk);
194899
194900
194901 if (g_state.has_unique_indexes && l_state.delete_chunk.size() != 0) {
194902 auto &local_storage = LocalStorage::Get(context.client, table.db);
194903 auto storage = local_storage.GetStorage(table);
194904 storage->AppendToDeleteIndexes(row_ids, l_state.delete_chunk);
194905 }
194906
194907 auto deleted_count = table.Delete(*l_state.delete_state, context.client, row_ids, chunk.size());
194908 g_state.deleted_count += deleted_count;
194909
194910
194911 if (return_chunk) {
194912
194913 map<row_t, idx_t> new_row_ids_deleted;
194914 auto flat_ids = FlatVector::GetData<row_t>(row_ids);
194915 for (
idx_t i = 0; i < chunk.size(); i++) {
194916
194917
194918
194919 auto row_id = flat_ids[i];
194920 auto already_deleted = g_state.deleted_row_ids.find(row_id) != g_state.deleted_row_ids.end();
194921 auto newly_deleted = new_row_ids_deleted.find(row_id) != new_row_ids_deleted.end();
194922 if (!already_deleted && !newly_deleted) {
194923 new_row_ids_deleted[row_id] = i;
194924 g_state.deleted_row_ids.insert(row_id);
194925 }
194926 }
194927
194928 D_ASSERT(new_row_ids_deleted.size() == deleted_count);
194929 if (deleted_count < l_state.delete_chunk.size()) {
194930 SelectionVector delete_sel(0, deleted_count);
194931 idx_t chunk_index = 0;
194932 for (auto &row_id_to_chunk_index : new_row_ids_deleted) {
194933 delete_sel.set_index(chunk_index, row_id_to_chunk_index.second);
194934 chunk_index++;
194935 }
194936 l_state.delete_chunk.Slice(delete_sel, deleted_count);
194937 }
194938 g_state.return_collection.Append(l_state.delete_chunk);
194939 }
194940
194941 return SinkResultType::NEED_MORE_INPUT;
194942}
DUCKDB_API void Initialize(ClientContext &context, const vector< LogicalType > &types, idx_t capacity=STANDARD_VECTOR_SIZE)
Definition duckdb.cpp:116791
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
void sort(InputArray src, OutputArray dst, int flags)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237