// FIXME: Should remove when the simple_optimizer tears down // Creates the update plan with index scan. UpdatePlan::UpdatePlan(const parser::UpdateStatement *parse_tree, std::vector<oid_t> &key_column_ids, std::vector<ExpressionType> &expr_types, std::vector<type::Value> &values, oid_t &index_id) : update_primary_key_(false) { std::vector<oid_t> column_ids; BuildInitialUpdatePlan(parse_tree, column_ids); // Set primary key update flag for (auto update_clause : updates_) { std::string column_name = update_clause->column; oid_t column_id = target_table_->GetSchema()->GetColumnID(column_name); update_primary_key_ = target_table_->GetSchema()->GetColumn(column_id).IsPrimary(); } // Create index scan desc std::vector<expression::AbstractExpression *> runtime_keys; auto index = target_table_->GetIndex(index_id); planner::IndexScanPlan::IndexScanDesc index_scan_desc( index, key_column_ids, expr_types, values, runtime_keys); // Create plan node. LOG_TRACE("Creating a index scan plan"); auto predicate_cpy = where_ == nullptr ? nullptr : where_->Copy(); std::unique_ptr<planner::IndexScanPlan> index_scan_node( new planner::IndexScanPlan(target_table_, predicate_cpy, column_ids, index_scan_desc, true)); LOG_TRACE("Index scan plan created"); AddChild(std::move(index_scan_node)); }
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(); }
void RunRead() { 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()); ///////////////////////////////////////////////////////// // MATERIALIZE ///////////////////////////////////////////////////////// // Create and set up materialization executor std::unordered_map<oid_t, oid_t> old_to_new_cols; for (oid_t col_itr = 0; col_itr < column_count; col_itr++) { old_to_new_cols[col_itr] = col_itr; } auto output_schema = catalog::Schema::CopySchema(user_table->GetSchema()); bool physify_flag = true; // is going to create a physical tile planner::MaterializationPlan mat_node(old_to_new_cols, output_schema, physify_flag); executor::MaterializationExecutor mat_executor(&mat_node, nullptr); mat_executor.AddChild(&index_scan_executor); ///////////////////////////////////////////////////////// // EXECUTE ///////////////////////////////////////////////////////// std::vector<executor::AbstractExecutor *> executors; executors.push_back(&mat_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; } }