Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions python/test_pysdk/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,33 @@ def test_update_table_with_one_block(self, suffix):
res = db_obj.drop_table("test_update_table_with_one_block"+suffix, ConflictType.Error)
assert res.error_code == ErrorCode.OK

def test_update_table_with_two_blocks(self, suffix):
# connect
db_obj = self.infinity_obj.get_database("default_db")
db_obj.drop_table("test_update_table_with_two_blocks"+suffix, ConflictType.Ignore)
table_obj = db_obj.create_table("test_update_table_with_two_blocks"+suffix,
{"c1": {"type": "int"}, "c2": {"type": "int"}},
ConflictType.Error)

# insert
values = [{"c1": 1, "c2": 2} for _ in range(6000)]
table_obj.insert(values)
insert_res, extra_result = table_obj.output(["*"]).to_df()
print(insert_res)

values = [{"c1": 1, "c2": 2} for _ in range(5000)]
table_obj.insert(values)
insert_res, extra_result = table_obj.output(["*"]).to_df()
print(insert_res)

# update
table_obj.update("c1 = 1", {"c2": 20})
delete_res, extra_result = table_obj.output(["*"]).to_df()
print(delete_res)

res = db_obj.drop_table("test_update_table_with_two_blocks"+suffix, ConflictType.Error)
assert res.error_code == ErrorCode.OK

def test_update_table_with_one_segment(self, suffix):
# connect
db_obj = self.infinity_obj.get_database("default_db")
Expand Down
8 changes: 1 addition & 7 deletions src/executor/operator/physical_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,7 @@ bool PhysicalUpdate::Execute(QueryContext *query_context, OperatorState *operato
SharedPtr<DataBlock> output_data_block = DataBlock::Make();
output_data_block->Init(output_column_vectors);

Status status = new_txn->Append(*table_info_->db_name_, *table_info_->table_name_, output_data_block);
if (!status.ok()) {
operator_state->status_ = status;
RecoverableError(status);
return false;
}
status = new_txn->Delete(*table_info_->db_name_, *table_info_->table_name_, row_ids);
Status status = new_txn->Update(*table_info_->db_name_, *table_info_->table_name_, output_data_block, row_ids);
if (!status.ok()) {
operator_state->status_ = status;
RecoverableError(status);
Expand Down
20 changes: 19 additions & 1 deletion src/storage/new_txn/base_txn_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ String DropIndexTxnStore::ToString() const {
}

String AppendTxnStore::ToString() const {
return fmt::format("{}: database: {}, db_id: {}, table: {}, table_id: {}", TransactionType2Str(type_), db_name_, db_id_, table_name_, table_id_);
return fmt::format("{}: database: {}, db_id: {}, table: {}, table_id: {}, appended: {}",
TransactionType2Str(type_),
db_name_,
db_id_,
table_name_,
table_id_,
row_ranges_.size());
}

String ImportTxnStore::ToString() const {
Expand Down Expand Up @@ -127,4 +133,16 @@ String DeleteTxnStore::ToString() const {
table_id_,
row_ids_.size());
}

String UpdateTxnStore::ToString() const {
return fmt::format("{}: database: {}, db_id: {}, table: {}, table_id: {}, appended: {}, deleted: {}",
TransactionType2Str(type_),
db_name_,
db_id_,
table_name_,
table_id_,
row_ranges_.size(),
row_ids_.size());
}

} // namespace infinity
26 changes: 26 additions & 0 deletions src/storage/new_txn/base_txn_store.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,30 @@ export struct DeleteTxnStore : public BaseTxnStore {

String ToString() const final;
};

export struct UpdateTxnStore : public BaseTxnStore {
UpdateTxnStore() : BaseTxnStore(TransactionType::kUpdate) {}

String db_name_{};
String db_id_str_{};
String table_name_{};
String table_id_str_{};
u64 db_id_{};
u64 table_id_{};

Vector<SharedPtr<DataBlock>> input_blocks_{};
Vector<String> index_ids_{}; // indexes will be appended

// For data append
Vector<Pair<RowID, u64>> row_ranges_{};

// For mem index
Vector<MemIndexRange> mem_indexes_to_append_{};
Vector<MemIndexRange> mem_indexes_to_dump_{};

Vector<RowID> row_ids_{};

String ToString() const final;
};

} // namespace infinity
26 changes: 26 additions & 0 deletions src/storage/new_txn/new_txn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,9 @@ bool NewTxn::CheckConflictTxnStore(NewTxn *previous_txn, String &cause, bool &re
case TransactionType::kDropColumn: {
return CheckConflictTxnStore(static_cast<const DropColumnsTxnStore &>(*base_txn_store_), previous_txn, cause, retry_query);
}
case TransactionType::kDropTable: {
return CheckConflictTxnStore(static_cast<const DropTableTxnStore &>(*base_txn_store_), previous_txn, cause, retry_query);
}
default: {
return false;
}
Expand Down Expand Up @@ -3164,6 +3167,29 @@ bool NewTxn::CheckConflictCmd(const WalCmdDropTableV2 &cmd, NewTxn *previous_txn
return false;
}

bool NewTxn::CheckConflictTxnStore(const DropTableTxnStore &txn_store, NewTxn *previous_txn, String &cause, bool &retry_query) {
const String &db_name = txn_store.db_name_;
bool conflict = false;
switch (previous_txn->base_txn_store_->type_) {
case TransactionType::kDropDB: {
DropDBTxnStore *drop_db_txn_store = static_cast<DropDBTxnStore *>(previous_txn->base_txn_store_.get());
if (drop_db_txn_store->db_name_ == db_name) {
retry_query = false;
conflict = true;
}
break;
}
default: {
}
}

if (conflict) {
cause = fmt::format("{} vs. {}", previous_txn->base_txn_store_->ToString(), txn_store.ToString());
return true;
}
return false;
}

bool NewTxn::CheckConflictTxnStore(const DumpMemIndexTxnStore &txn_store, NewTxn *previous_txn, String &cause, bool &retry_query) {
const String &db_name = txn_store.db_name_;
const String &table_name = txn_store.table_name_;
Expand Down
10 changes: 8 additions & 2 deletions src/storage/new_txn/new_txn.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ struct AppendState;
struct AppendRange;
enum class DumpIndexCause;
struct IndexReader;
struct BaseTxnStore;
struct TxnCommitterTask;

struct BaseTxnStore;
Expand All @@ -107,6 +106,7 @@ struct CompactTxnStore;
struct CreateIndexTxnStore;
struct DumpMemIndexTxnStore;
struct DeleteTxnStore;
struct DropTableTxnStore;

export struct CheckpointOption {
TxnTimeStamp checkpoint_ts_ = 0;
Expand Down Expand Up @@ -334,11 +334,16 @@ public:
Status Update(const String &db_name, const String &table_name, const SharedPtr<DataBlock> &input_block, const Vector<RowID> &row_ids);

private:
Tuple<Vector<Pair<RowID, u64>>, Status> GetRowRanges(TableMeeta &table_meta, const SharedPtr<DataBlock> &input_block);

Status AppendInner(const String &db_name,
const String &table_name,
const String &table_key,
TableMeeta &table_meta,
const SharedPtr<DataBlock> &input_block);
const SharedPtr<DataBlock> &input_block,
const Vector<Pair<RowID, u64>> &row_ranges);

Status DeleteInner(const String &db_name, const String &table_name, TableMeeta &table_meta, const Vector<RowID> &row_ids);

public:
// Status Delete(TableEntry *table_entry, const Vector<RowID> &row_ids, bool check_conflict = true);
Expand Down Expand Up @@ -622,6 +627,7 @@ private:
bool CheckConflictTxnStore(const CreateIndexTxnStore &txn_store, NewTxn *previous_txn, String &cause, bool &retry_query);
bool CheckConflictTxnStore(const DumpMemIndexTxnStore &txn_store, NewTxn *previous_txn, String &cause, bool &retry_query);
bool CheckConflictTxnStore(const DeleteTxnStore &txn_store, NewTxn *previous_txn, String &cause, bool &retry_query);
bool CheckConflictTxnStore(const DropTableTxnStore &txn_store, NewTxn *previous_txn, String &cause, bool &retry_query);

public:
static Status Cleanup(TxnTimeStamp ts, KVInstance *kv_instance);
Expand Down
Loading