src/backend/executor/insert_executor.cpp
/**
 * @brief Adds a column to the logical tile, using the position lists.
 * @return true on success, false otherwise.
 */
@brief Adds a column to the logical tile, using the position lists.
@return true on success, false otherwise.
@brief Adds a column to the logical tile using the position lists . @return true on success false otherwise .
bool InsertExecutor::DExecute() {
  if (done_) return false;
  assert(!done_);
  assert(executor_context_ != nullptr);
  const planner::InsertPlan &node = GetPlanNode<planner::InsertPlan>();
  storage::DataTable *target_table = node.GetTable();
  oid_t bulk_insert_count = node.GetBulkInsertCount();
  assert(target_table);
  auto &transaction_manager =
      concurrency::TransactionManagerFactory::GetInstance();
  auto executor_pool = executor_context_->GetExecutorContextPool();
  if (children_.size() == 1) {
    LOG_TRACE("Insert executor :: 1 child ");
    if (!children_[0]->Execute()) {
      return false;
    }
    std::unique_ptr<LogicalTile> logical_tile(children_[0]->GetOutput());
    assert(logical_tile.get() != nullptr);
    auto target_table_schema = target_table->GetSchema();
    auto column_count = target_table_schema->GetColumnCount();
    std::unique_ptr<storage::Tuple> tuple(
        new storage::Tuple(target_table_schema, true));
    for (oid_t tuple_id : *logical_tile) {
      expression::ContainerTuple<LogicalTile> cur_tuple(logical_tile.get(),
                                                        tuple_id);
      for (oid_t column_itr = 0; column_itr < column_count; column_itr++)
        tuple->SetValue(column_itr, cur_tuple.GetValue(column_itr),
                        executor_pool);
      peloton::ItemPointer location = target_table->InsertTuple(tuple.get());
      if (location.block == INVALID_OID) {
        transaction_manager.SetTransactionResult(
            peloton::Result::RESULT_FAILURE);
        return false;
      }
      auto res =
          transaction_manager.PerformInsert(location);
      if (!res) {
        transaction_manager.SetTransactionResult(RESULT_FAILURE);
        return res;
      }
      executor_context_->num_processed += 1;  
    }
    return true;
  }
  else if (children_.size() == 0) {
    LOG_TRACE("Insert executor :: 0 child ");
    auto schema = target_table->GetSchema();
    auto project_info = node.GetProjectInfo();
    auto tuple = node.GetTuple();
    std::unique_ptr<storage::Tuple> project_tuple;
    if(tuple == nullptr) {
      assert(project_info);
      assert(project_info->GetDirectMapList().size() == 0);
      project_tuple.reset(new storage::Tuple(schema, true));
      for (auto target : project_info->GetTargetList()) {
        peloton::Value value =
            target.second->Evaluate(nullptr, nullptr, executor_context_);
        project_tuple->SetValue(target.first, value, executor_pool);
      }
      tuple = project_tuple.get();
    }
    for (oid_t insert_itr = 0; insert_itr < bulk_insert_count; insert_itr++) {
      ItemPointer location = target_table->InsertTuple(tuple);
      LOG_TRACE("Inserted into location: %u, %u", location.block,
                location.offset);
      if (location.block == INVALID_OID) {
        LOG_TRACE("Failed to Insert. Set txn failure.");
        transaction_manager.SetTransactionResult(Result::RESULT_FAILURE);
        return false;
      }
      auto res =
          transaction_manager.PerformInsert(location);
      if (!res) {
        transaction_manager.SetTransactionResult(RESULT_FAILURE);
        return res;
      }
      executor_context_->num_processed += 1;  
    }
    done_ = true;
    return true;
  }
  return true;
}
bool InsertExecutor :: DExecute ( ) { if ( done_ ) return false ; assert ( ! done_ ) ; assert ( executor_context_ != nullptr ) ; const planner :: InsertPlan & node = GetPlanNode < planner :: InsertPlan > ( ) ; storage :: DataTable * target_table = node . GetTable ( ) ; oid_t bulk_insert_count = node . GetBulkInsertCount ( ) ; assert ( target_table ) ; auto & transaction_manager = concurrency :: TransactionManagerFactory :: GetInstance ( ) ; auto executor_pool = executor_context_ -> GetExecutorContextPool ( ) ; if ( children_ . size ( ) == 1 ) { LOG_TRACE ( " " ) ; if ( ! children_ [ 0 ] -> Execute ( ) ) { return false ; } std :: unique_ptr < LogicalTile > logical_tile ( children_ [ 0 ] -> GetOutput ( ) ) ; assert ( logical_tile . get ( ) != nullptr ) ; auto target_table_schema = target_table -> GetSchema ( ) ; auto column_count = target_table_schema -> GetColumnCount ( ) ; std :: unique_ptr < storage :: Tuple > tuple ( new storage :: Tuple ( target_table_schema , true ) ) ; for ( oid_t tuple_id : * logical_tile ) { expression :: ContainerTuple < LogicalTile > cur_tuple ( logical_tile . get ( ) , tuple_id ) ; for ( oid_t column_itr = 0 ; column_itr < column_count ; column_itr ++ ) tuple -> SetValue ( column_itr , cur_tuple . GetValue ( column_itr ) , executor_pool ) ; peloton :: ItemPointer location = target_table -> InsertTuple ( tuple . get ( ) ) ; if ( location . block == INVALID_OID ) { transaction_manager . SetTransactionResult ( peloton :: Result :: RESULT_FAILURE ) ; return false ; } auto res = transaction_manager . PerformInsert ( location ) ; if ( ! res ) { transaction_manager . SetTransactionResult ( RESULT_FAILURE ) ; return res ; } executor_context_ -> num_processed += 1 ; } return true ; } else if ( children_ . size ( ) == 0 ) { LOG_TRACE ( " " ) ; auto schema = target_table -> GetSchema ( ) ; auto project_info = node . GetProjectInfo ( ) ; auto tuple = node . GetTuple ( ) ; std :: unique_ptr < storage :: Tuple > project_tuple ; if ( tuple == nullptr ) { assert ( project_info ) ; assert ( project_info -> GetDirectMapList ( ) . size ( ) == 0 ) ; project_tuple . reset ( new storage :: Tuple ( schema , true ) ) ; for ( auto target : project_info -> GetTargetList ( ) ) { peloton :: Value value = target . second -> Evaluate ( nullptr , nullptr , executor_context_ ) ; project_tuple -> SetValue ( target . first , value , executor_pool ) ; } tuple = project_tuple . get ( ) ; } for ( oid_t insert_itr = 0 ; insert_itr < bulk_insert_count ; insert_itr ++ ) { ItemPointer location = target_table -> InsertTuple ( tuple ) ; LOG_TRACE ( " " , location . block , location . offset ) ; if ( location . block == INVALID_OID ) { LOG_TRACE ( " " ) ; transaction_manager . SetTransactionResult ( Result :: RESULT_FAILURE ) ; return false ; } auto res = transaction_manager . PerformInsert ( location ) ; if ( ! res ) { transaction_manager . SetTransactionResult ( RESULT_FAILURE ) ; return res ; } executor_context_ -> num_processed += 1 ; } done_ = true ; return true ; } return true ; }
@brief Adds a column to the logical tile, using the position lists.
@brief Adds a column to the logical tile using the position lists .
// Inserting a logical tile. // Go over the logical tile // Materialize the logical tile tuple // insert one // Inserting a collection of tuples from plan node // Extract expressions from plan node and construct the tuple. // For now we just handle a single tuple // Check if this is not a raw tuple // Otherwise, there must exist a project info // There should be no direct maps // Set tuple to point to temporary project tuple // Bulk Insert Mode // Carry out insertion // insert one