Skip to content

Commit 6fdd154

Browse files
committed
vendor: Update vendored sources to duckdb/duckdb@892a03d
Fix an issue in upserts where the local append state was not correctly flushed (duckdb/duckdb#17109)
1 parent b6e3388 commit 6fdd154

File tree

7 files changed

+29
-43
lines changed

7 files changed

+29
-43
lines changed

src/duckdb/src/execution/operator/persistent/physical_insert.cpp

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void PhysicalInsert::GetInsertInfo(const BoundCreateTableInfo &info, vector<Logi
7575

7676
InsertGlobalState::InsertGlobalState(ClientContext &context, const vector<LogicalType> &return_types,
7777
DuckTableEntry &table)
78-
: table(table), insert_count(0), initialized(false), return_collection(context, return_types) {
78+
: table(table), insert_count(0), return_collection(context, return_types) {
7979
}
8080

8181
InsertLocalState::InsertLocalState(ClientContext &context, const vector<LogicalType> &types,
@@ -275,11 +275,6 @@ static idx_t PerformOnConflictAction(InsertLocalState &lstate, InsertGlobalState
275275
return update_chunk.size();
276276
}
277277
auto &local_storage = LocalStorage::Get(context.client, data_table.db);
278-
if (gstate.initialized) {
279-
// Flush any local appends that could be referenced by the UPDATE.
280-
data_table.FinalizeLocalAppend(gstate.append_state);
281-
gstate.initialized = false;
282-
}
283278
local_storage.Update(data_table, row_ids, set_columns, update_chunk);
284279
return update_chunk.size();
285280
}
@@ -289,11 +284,6 @@ static idx_t PerformOnConflictAction(InsertLocalState &lstate, InsertGlobalState
289284
data_table.Delete(delete_state, context.client, row_ids, update_chunk.size());
290285
} else {
291286
auto &local_storage = LocalStorage::Get(context.client, data_table.db);
292-
if (gstate.initialized) {
293-
// Flush any local appends that could be referenced by the DELETE.
294-
data_table.FinalizeLocalAppend(gstate.append_state);
295-
gstate.initialized = false;
296-
}
297287
local_storage.Delete(data_table, row_ids, update_chunk.size());
298288
}
299289

@@ -427,7 +417,7 @@ static void VerifyOnConflictCondition(ExecutionContext &context, DataChunk &comb
427417
throw InternalException("VerifyAppendConstraints was expected to throw but didn't");
428418
}
429419

430-
auto &indexes = local_storage.GetIndexes(data_table);
420+
auto &indexes = local_storage.GetIndexes(context.client, data_table);
431421
auto storage = local_storage.GetStorage(data_table);
432422
DataTable::VerifyUniqueIndexes(indexes, storage, tuples, nullptr);
433423
throw InternalException("VerifyUniqueIndexes was expected to throw but didn't");
@@ -451,7 +441,7 @@ static idx_t HandleInsertConflicts(TableCatalogEntry &table, ExecutionContext &c
451441
auto &constraint_state = lstate.GetConstraintState(data_table, table);
452442
data_table.VerifyAppendConstraints(constraint_state, context.client, tuples, storage, &conflict_manager);
453443
} else {
454-
auto &indexes = local_storage.GetIndexes(data_table);
444+
auto &indexes = local_storage.GetIndexes(context.client, data_table);
455445
DataTable::VerifyUniqueIndexes(indexes, storage, tuples, &conflict_manager);
456446
}
457447

@@ -529,7 +519,7 @@ idx_t PhysicalInsert::OnConflictHandling(TableCatalogEntry &table, ExecutionCont
529519
ConflictInfo conflict_info(conflict_target);
530520

531521
auto &global_indexes = data_table.GetDataTableInfo()->GetIndexes();
532-
auto &local_indexes = local_storage.GetIndexes(data_table);
522+
auto &local_indexes = local_storage.GetIndexes(context.client, data_table);
533523

534524
unordered_set<BoundIndex *> matched_indexes;
535525
if (conflict_info.column_ids.empty()) {
@@ -623,19 +613,14 @@ SinkResultType PhysicalInsert::Sink(ExecutionContext &context, DataChunk &insert
623613
insert_chunk.Flatten();
624614

625615
if (!parallel) {
626-
if (!gstate.initialized) {
627-
storage.InitializeLocalAppend(gstate.append_state, table, context.client, bound_constraints);
628-
gstate.initialized = true;
629-
}
630-
631616
idx_t updated_tuples = OnConflictHandling(table, context, gstate, lstate, insert_chunk);
632617

633618
gstate.insert_count += insert_chunk.size();
634619
gstate.insert_count += updated_tuples;
635620
if (return_chunk) {
636621
gstate.return_collection.Append(insert_chunk);
637622
}
638-
storage.LocalAppend(gstate.append_state, context.client, insert_chunk, true);
623+
storage.LocalAppend(table, context.client, insert_chunk, bound_constraints);
639624
if (action_type == OnConflictAction::UPDATE && lstate.update_chunk.size() != 0) {
640625
(void)HandleInsertConflicts<true>(table, context, lstate, gstate, lstate.update_chunk, *this);
641626
(void)HandleInsertConflicts<false>(table, context, lstate, gstate, lstate.update_chunk, *this);
@@ -701,13 +686,14 @@ SinkCombineResultType PhysicalInsert::Combine(ExecutionContext &context, Operato
701686
gstate.insert_count += append_count;
702687
if (append_count < row_group_size) {
703688
// we have few rows - append to the local storage directly
704-
storage.InitializeLocalAppend(gstate.append_state, table, context.client, bound_constraints);
689+
LocalAppendState append_state;
690+
storage.InitializeLocalAppend(append_state, table, context.client, bound_constraints);
705691
auto &transaction = DuckTransaction::Get(context.client, table.catalog);
706692
collection.Scan(transaction, [&](DataChunk &insert_chunk) {
707-
storage.LocalAppend(gstate.append_state, context.client, insert_chunk, false);
693+
storage.LocalAppend(append_state, context.client, insert_chunk, false);
708694
return true;
709695
});
710-
storage.FinalizeLocalAppend(gstate.append_state);
696+
storage.FinalizeLocalAppend(append_state);
711697
} else {
712698
// we have written rows to disk optimistically - merge directly into the transaction-local storage
713699
lstate.optimistic_writer->WriteLastRowGroup(collection);
@@ -722,12 +708,6 @@ SinkCombineResultType PhysicalInsert::Combine(ExecutionContext &context, Operato
722708

723709
SinkFinalizeType PhysicalInsert::Finalize(Pipeline &pipeline, Event &event, ClientContext &context,
724710
OperatorSinkFinalizeInput &input) const {
725-
auto &gstate = input.global_state.Cast<InsertGlobalState>();
726-
if (!parallel && gstate.initialized) {
727-
auto &table = gstate.table;
728-
auto &storage = table.GetStorage();
729-
storage.FinalizeLocalAppend(gstate.append_state);
730-
}
731711
return SinkFinalizeType::READY;
732712
}
733713

src/duckdb/src/function/table/version/pragma_version.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#ifndef DUCKDB_PATCH_VERSION
2-
#define DUCKDB_PATCH_VERSION "0-dev2456"
2+
#define DUCKDB_PATCH_VERSION "0-dev2459"
33
#endif
44
#ifndef DUCKDB_MINOR_VERSION
55
#define DUCKDB_MINOR_VERSION 3
@@ -8,10 +8,10 @@
88
#define DUCKDB_MAJOR_VERSION 1
99
#endif
1010
#ifndef DUCKDB_VERSION
11-
#define DUCKDB_VERSION "v1.3.0-dev2456"
11+
#define DUCKDB_VERSION "v1.3.0-dev2459"
1212
#endif
1313
#ifndef DUCKDB_SOURCE_ID
14-
#define DUCKDB_SOURCE_ID "cae7a680bc"
14+
#define DUCKDB_SOURCE_ID "892a03d26e"
1515
#endif
1616
#include "duckdb/function/table/system_functions.hpp"
1717
#include "duckdb/main/database.hpp"

src/duckdb/src/include/duckdb/execution/operator/persistent/physical_insert.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class InsertGlobalState : public GlobalSinkState {
3131
mutex lock;
3232
DuckTableEntry &table;
3333
idx_t insert_count;
34-
bool initialized;
35-
LocalAppendState append_state;
3634
ColumnDataCollection return_collection;
3735
};
3836

src/duckdb/src/include/duckdb/storage/data_table.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class DataTable : public enable_shared_from_this<DataTable> {
111111
void LocalAppend(TableCatalogEntry &table, ClientContext &context, DataChunk &chunk,
112112
const vector<unique_ptr<BoundConstraint>> &bound_constraints, Vector &row_ids,
113113
DataChunk &delete_chunk);
114+
//! Appends to the transaction-local storage of this table
115+
void LocalAppend(TableCatalogEntry &table, ClientContext &context, DataChunk &chunk,
116+
const vector<unique_ptr<BoundConstraint>> &bound_constraints);
114117
//! Append a chunk to the transaction-local storage of this table.
115118
void LocalWALAppend(TableCatalogEntry &table, ClientContext &context, DataChunk &chunk,
116119
const vector<unique_ptr<BoundConstraint>> &bound_constraints);

src/duckdb/src/include/duckdb/transaction/local_storage.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class LocalStorage {
180180
void MoveStorage(DataTable &old_dt, DataTable &new_dt);
181181
void FetchChunk(DataTable &table, Vector &row_ids, idx_t count, const vector<StorageIndex> &col_ids,
182182
DataChunk &chunk, ColumnFetchState &fetch_state);
183-
TableIndexList &GetIndexes(DataTable &table);
183+
TableIndexList &GetIndexes(ClientContext &context, DataTable &table);
184184
optional_ptr<LocalTableStorage> GetStorage(DataTable &table);
185185

186186
void VerifyNewConstraint(DataTable &parent, const BoundConstraint &constraint);

src/duckdb/src/storage/data_table.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ void DataTable::VerifyForeignKeyConstraint(optional_ptr<LocalTableStorage> stora
580580

581581
// Local constraint verification.
582582
if (local_verification) {
583-
auto &local_indexes = local_storage.GetIndexes(data_table);
583+
auto &local_indexes = local_storage.GetIndexes(context, data_table);
584584
local_indexes.VerifyForeignKey(storage, dst_keys_ptr, dst_chunk, local_conflicts);
585585
local_conflicts.Finalize();
586586
auto &local_matches = local_conflicts.Conflicts();
@@ -601,7 +601,7 @@ void DataTable::VerifyForeignKeyConstraint(optional_ptr<LocalTableStorage> stora
601601
// check whether or not the chunk can be inserted or deleted into the referenced table' storage
602602
index = data_table.info->indexes.FindForeignKeyIndex(dst_keys_ptr, fk_type);
603603
if (local_verification) {
604-
auto &transact_index = local_storage.GetIndexes(data_table);
604+
auto &transact_index = local_storage.GetIndexes(context, data_table);
605605
// check whether or not the chunk can be inserted or deleted into the referenced table' storage
606606
transaction_index = transact_index.FindForeignKeyIndex(dst_keys_ptr, fk_type);
607607
}
@@ -878,6 +878,14 @@ void DataTable::LocalAppend(LocalAppendState &state, ClientContext &context, Dat
878878
LocalStorage::Append(state, chunk);
879879
}
880880

881+
void DataTable::LocalAppend(TableCatalogEntry &table, ClientContext &context, DataChunk &chunk,
882+
const vector<unique_ptr<BoundConstraint>> &bound_constraints) {
883+
LocalAppendState append_state;
884+
InitializeLocalAppend(append_state, table, context, bound_constraints);
885+
LocalAppend(append_state, context, chunk, false);
886+
FinalizeLocalAppend(append_state);
887+
}
888+
881889
void DataTable::FinalizeLocalAppend(LocalAppendState &state) {
882890
LocalStorage::FinalizeAppend(state);
883891
}

src/duckdb/src/storage/local_storage.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,9 @@ void LocalStorage::FetchChunk(DataTable &table, Vector &row_ids, idx_t count, co
657657
storage->row_groups->Fetch(transaction, chunk, col_ids, row_ids, count, fetch_state);
658658
}
659659

660-
TableIndexList &LocalStorage::GetIndexes(DataTable &table) {
661-
auto storage = table_manager.GetStorage(table);
662-
if (!storage) {
663-
throw InternalException("LocalStorage::GetIndexes - local storage not found");
664-
}
665-
return storage->append_indexes;
660+
TableIndexList &LocalStorage::GetIndexes(ClientContext &context, DataTable &table) {
661+
auto &storage = table_manager.GetOrCreateStorage(context, table);
662+
return storage.append_indexes;
666663
}
667664

668665
optional_ptr<LocalTableStorage> LocalStorage::GetStorage(DataTable &table) {

0 commit comments

Comments
 (0)