void UpdateTuple(storage::DataTable *table) { auto &txn_manager = concurrency::OptimisticTxnManager::GetInstance(); auto txn = txn_manager.BeginTransaction(); std::unique_ptr<executor::ExecutorContext> context( new executor::ExecutorContext(txn)); // Update std::vector<oid_t> update_column_ids = {2}; std::vector<Value> values; Value update_val = ValueFactory::GetDoubleValue(23.5); planner::ProjectInfo::TargetList target_list; planner::ProjectInfo::DirectMapList direct_map_list; target_list.emplace_back( 2, expression::ExpressionUtil::ConstantValueFactory(update_val)); LOG_INFO("%lu", target_list.at(0).first); direct_map_list.emplace_back(0, std::pair<oid_t, oid_t>(0, 0)); direct_map_list.emplace_back(1, std::pair<oid_t, oid_t>(0, 1)); direct_map_list.emplace_back(3, std::pair<oid_t, oid_t>(0, 3)); planner::UpdatePlan update_node( table, new planner::ProjectInfo(std::move(target_list), std::move(direct_map_list))); executor::UpdateExecutor update_executor(&update_node, context.get()); // Predicate // WHERE ATTR_0 < 70 expression::TupleValueExpression *tup_val_exp = new expression::TupleValueExpression(0, 0); expression::ConstantValueExpression *const_val_exp = new expression::ConstantValueExpression( ValueFactory::GetIntegerValue(70)); auto predicate = new expression::ComparisonExpression<expression::CmpLt>( EXPRESSION_TYPE_COMPARE_LESSTHAN, tup_val_exp, const_val_exp); // Seq scan std::vector<oid_t> column_ids = {0}; planner::SeqScanPlan seq_scan_node(table, predicate, column_ids); executor::SeqScanExecutor seq_scan_executor(&seq_scan_node, context.get()); // Parent-Child relationship update_node.AddChild(&seq_scan_node); update_executor.AddChild(&seq_scan_executor); EXPECT_TRUE(update_executor.Init()); while (update_executor.Execute()) ; txn_manager.CommitTransaction(); }
bool TransactionTestsUtil::ExecuteUpdateByValue(concurrency::Transaction *txn, storage::DataTable *table, int old_value, int new_value) { std::unique_ptr<executor::ExecutorContext> context( new executor::ExecutorContext(txn)); Value update_val = ValueFactory::GetIntegerValue(new_value); // ProjectInfo planner::ProjectInfo::TargetList target_list; planner::ProjectInfo::DirectMapList direct_map_list; target_list.emplace_back( 1, expression::ExpressionUtil::ConstantValueFactory(update_val)); direct_map_list.emplace_back(0, std::pair<oid_t, oid_t>(0, 0)); // Update plan std::unique_ptr<const planner::ProjectInfo> project_info( new planner::ProjectInfo(std::move(target_list), std::move(direct_map_list))); planner::UpdatePlan update_node(table, std::move(project_info)); executor::UpdateExecutor update_executor(&update_node, context.get()); // Predicate auto tup_val_exp = new expression::TupleValueExpression(0, 1); auto const_val_exp = new expression::ConstantValueExpression( ValueFactory::GetIntegerValue(old_value)); auto predicate = new expression::ComparisonExpression<expression::CmpEq>( EXPRESSION_TYPE_COMPARE_EQUAL, tup_val_exp, const_val_exp); // Seq scan std::vector<oid_t> column_ids = {0, 1}; std::unique_ptr<planner::SeqScanPlan> seq_scan_node( new planner::SeqScanPlan(table, predicate, column_ids)); executor::SeqScanExecutor seq_scan_executor(seq_scan_node.get(), context.get()); update_node.AddChild(std::move(seq_scan_node)); update_executor.AddChild(&seq_scan_executor); EXPECT_TRUE(update_executor.Init()); return update_executor.Execute(); }
bool TransactionTestsUtil::ExecuteUpdate(concurrency::Transaction *transaction, storage::DataTable *table, int id, int value) { std::unique_ptr<executor::ExecutorContext> context( new executor::ExecutorContext(transaction)); Value update_val = ValueFactory::GetIntegerValue(value); // ProjectInfo planner::ProjectInfo::TargetList target_list; planner::ProjectInfo::DirectMapList direct_map_list; target_list.emplace_back( 1, expression::ExpressionUtil::ConstantValueFactory(update_val)); direct_map_list.emplace_back(0, std::pair<oid_t, oid_t>(0, 0)); // Update plan std::unique_ptr<const planner::ProjectInfo> project_info( new planner::ProjectInfo(std::move(target_list), std::move(direct_map_list))); planner::UpdatePlan update_node(table, std::move(project_info)); executor::UpdateExecutor update_executor(&update_node, context.get()); // Predicate auto predicate = MakePredicate(id); // Seq scan std::vector<oid_t> column_ids = {0}; std::unique_ptr<planner::SeqScanPlan> seq_scan_node( new planner::SeqScanPlan(table, predicate, column_ids)); executor::SeqScanExecutor seq_scan_executor(seq_scan_node.get(), context.get()); update_node.AddChild(std::move(seq_scan_node)); update_executor.AddChild(&seq_scan_executor); EXPECT_TRUE(update_executor.Init()); return update_executor.Execute(); }
void RunUpdate() { auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); ///////////////////////////////////////////////////////// // INDEX SCAN + PREDICATE ///////////////////////////////////////////////////////// std::unique_ptr<executor::ExecutorContext> context( new executor::ExecutorContext(txn)); // Column ids to be added to logical tile after scan. std::vector<oid_t> column_ids; oid_t column_count = state.column_count + 1; for (oid_t col_itr = 0; col_itr < column_count; col_itr++) { column_ids.push_back(col_itr); } // Create and set up index scan executor std::vector<oid_t> key_column_ids; std::vector<ExpressionType> expr_types; std::vector<Value> values; std::vector<expression::AbstractExpression *> runtime_keys; auto tuple_count = state.scale_factor * DEFAULT_TUPLES_PER_TILEGROUP; auto lookup_key = rand() % tuple_count; key_column_ids.push_back(0); expr_types.push_back( ExpressionType::EXPRESSION_TYPE_COMPARE_EQUAL); values.push_back(ValueFactory::GetIntegerValue(lookup_key)); auto ycsb_pkey_index = user_table->GetIndexWithOid(user_table_pkey_index_oid); planner::IndexScanPlan::IndexScanDesc index_scan_desc( ycsb_pkey_index, key_column_ids, expr_types, values, runtime_keys); // Create plan node. auto predicate = nullptr; planner::IndexScanPlan index_scan_node(user_table, predicate, column_ids, index_scan_desc); // Run the executor executor::IndexScanExecutor index_scan_executor(&index_scan_node, context.get()); ///////////////////////////////////////////////////////// // UPDATE ///////////////////////////////////////////////////////// planner::ProjectInfo::TargetList target_list; planner::ProjectInfo::DirectMapList direct_map_list; // Update the second attribute for (oid_t col_itr = 0; col_itr < column_count; col_itr++) { if(col_itr != 1) { direct_map_list.emplace_back(col_itr, std::pair<oid_t, oid_t>(0, col_itr)); } } Value update_val = ValueFactory::GetStringValue(std::string("updated")); target_list.emplace_back(1, expression::ExpressionUtil::ConstantValueFactory(update_val)); planner::UpdatePlan update_node( user_table, new planner::ProjectInfo(std::move(target_list), std::move(direct_map_list))); executor::UpdateExecutor update_executor(&update_node, context.get()); update_executor.AddChild(&index_scan_executor); ///////////////////////////////////////////////////////// // EXECUTE ///////////////////////////////////////////////////////// std::vector<executor::AbstractExecutor *> executors; executors.push_back(&update_executor); ExecuteTest(executors); txn_manager.CommitTransaction(); }
bool RunMixed(ZipfDistribution &zipf, FastRandom &rng) { auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); concurrency::Transaction *txn = txn_manager.BeginTransaction(); std::unique_ptr<executor::ExecutorContext> context( new executor::ExecutorContext(txn)); // Column ids to be added to logical tile. std::vector<oid_t> column_ids; oid_t column_count = state.column_count + 1; // read all the attributes in a tuple. for (oid_t col_itr = 0; col_itr < column_count; col_itr++) { column_ids.push_back(col_itr); } // Create and set up index scan executor std::vector<oid_t> key_column_ids; std::vector<ExpressionType> expr_types; key_column_ids.push_back(0); expr_types.push_back(ExpressionType::EXPRESSION_TYPE_COMPARE_EQUAL); std::vector<expression::AbstractExpression *> runtime_keys; for (int i = 0; i < state.operation_count; i++) { auto rng_val = rng.NextUniform(); if (rng_val < state.update_ratio) { ///////////////////////////////////////////////////////// // PERFORM UPDATE ///////////////////////////////////////////////////////// // set up parameter values std::vector<type::Value > values; auto lookup_key = zipf.GetNextNumber(); values.push_back(type::ValueFactory::GetIntegerValue(lookup_key).Copy()); auto ycsb_pkey_index = user_table->GetIndexWithOid(user_table_pkey_index_oid); planner::IndexScanPlan::IndexScanDesc index_scan_desc( ycsb_pkey_index, key_column_ids, expr_types, values, runtime_keys); // Create plan node. auto predicate = nullptr; planner::IndexScanPlan index_scan_node(user_table, predicate, column_ids, index_scan_desc); // Run the executor executor::IndexScanExecutor index_scan_executor(&index_scan_node, context.get()); TargetList target_list; DirectMapList direct_map_list; // update multiple attributes for (oid_t col_itr = 0; col_itr < column_count; col_itr++) { if (col_itr == 1) { if (state.string_mode == true) { std::string update_raw_value(100, 'a'); type::Value update_val = type::ValueFactory::GetVarcharValue(update_raw_value).Copy(); target_list.emplace_back( col_itr, expression::ExpressionUtil::ConstantValueFactory(update_val)); } else { int update_raw_value = 1; type::Value update_val = type::ValueFactory::GetIntegerValue(update_raw_value).Copy(); target_list.emplace_back( col_itr, expression::ExpressionUtil::ConstantValueFactory(update_val)); } } else { direct_map_list.emplace_back(col_itr, std::pair<oid_t, oid_t>(0, col_itr)); } } std::unique_ptr<const planner::ProjectInfo> project_info( new planner::ProjectInfo(std::move(target_list), std::move(direct_map_list))); planner::UpdatePlan update_node(user_table, std::move(project_info)); executor::UpdateExecutor update_executor(&update_node, context.get()); update_executor.AddChild(&index_scan_executor); ExecuteUpdate(&update_executor); if (txn->GetResult() != Result::RESULT_SUCCESS) { txn_manager.AbortTransaction(txn); return false; } } else { ///////////////////////////////////////////////////////// // PERFORM READ ///////////////////////////////////////////////////////// // set up parameter values std::vector<type::Value > values; auto lookup_key = zipf.GetNextNumber(); values.push_back(type::ValueFactory::GetIntegerValue(lookup_key).Copy()); auto ycsb_pkey_index = user_table->GetIndexWithOid(user_table_pkey_index_oid); planner::IndexScanPlan::IndexScanDesc index_scan_desc( ycsb_pkey_index, key_column_ids, expr_types, values, runtime_keys); // Create plan node. auto predicate = nullptr; planner::IndexScanPlan index_scan_node(user_table, predicate, column_ids, index_scan_desc); // Run the executor executor::IndexScanExecutor index_scan_executor(&index_scan_node, context.get()); ExecuteRead(&index_scan_executor); if (txn->GetResult() != Result::RESULT_SUCCESS) { txn_manager.AbortTransaction(txn); return false; } } } // transaction passed execution. PL_ASSERT(txn->GetResult() == Result::RESULT_SUCCESS); auto result = txn_manager.CommitTransaction(txn); if (result == Result::RESULT_SUCCESS) { return true; } else { // transaction failed commitment. PL_ASSERT(result == Result::RESULT_ABORTED || result == Result::RESULT_FAILURE); return false; } }