<commit_before>//===----------------------------------------------------------------------===//
//
//                         PelotonDB
//
// materialization_executor.cpp
//
// Identification: src/backend/executor/materialization_executor.cpp
//
// Copyright (c) 2015, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "backend/executor/materialization_executor.h"

#include <cassert>
#include <memory>
#include <utility>

#include "backend/planner/materialization_plan.h"
#include "backend/executor/logical_tile.h"
#include "backend/executor/logical_tile_factory.h"
#include "backend/storage/tile.h"
#include "backend/storage/data_table.h"

namespace peloton {
namespace executor {

// Row-oriented materialization
void MaterializeRowAtAtATime(LogicalTile *source_tile,
                             const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
                             const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
                             storage::Tile *dest_tile);

// Column-oriented materialization
void MaterializeColumnAtATime(LogicalTile *source_tile,
                              const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
                              const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
                              storage::Tile *dest_tile);

/**
 * @brief Constructor for the materialization executor.
 * @param node Materialization node corresponding to this executor.
 */
MaterializationExecutor::MaterializationExecutor(
    planner::AbstractPlan *node, ExecutorContext *executor_context)
: AbstractExecutor(node, executor_context) {}

/**
 * @brief Nothing to init at the moment.
 * @return true on success, false otherwise.
 */
bool MaterializationExecutor::DInit() {
  assert(children_.size() == 1);

  return true;
}

/**
 * @brief Generates map from each base tile to columns originally from that
 *        base tile to be materialized.
 * @param column_ids Ids of columns to be materialized.
 * @param source_tile Logical tile that contains mapping from columns to
 *        base tiles.
 * @param tile_to_cols Map to be populated with mappings from tile to columns.
 *
 * We generate this mapping so that we can materialize columns tile by tile for
 * efficiency reasons.
 */
void MaterializationExecutor::GenerateTileToColMap(
    const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
    LogicalTile *source_tile,
    std::unordered_map<storage::Tile *, std::vector<oid_t>> &
    cols_in_physical_tile) {
  for (const auto &kv : old_to_new_cols) {
    oid_t col = kv.first;

    // figure out base physical tile for column in logical tile
    storage::Tile *base_tile = source_tile->GetBaseTile(col);

    std::vector<oid_t> &cols_from_tile = cols_in_physical_tile[base_tile];
    cols_from_tile.push_back(col);
  }
}

/**
 * @brief Does the actual copying of data into the new physical tile.
 * @param source_tile Source tile to copy data from.
 * @param tile_to_cols Map from base tile to columns in that tile
 *        to be materialized.
 * @param dest_tile New tile to copy data into.
 */
void MaterializationExecutor::MaterializeByTiles(
    LogicalTile *source_tile,
    const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
    const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
    storage::Tile *dest_tile) {

  auto dest_tile_column_count = dest_tile->GetColumnCount();
  // TODO: Make this a parameter
  oid_t column_count_threshold = 20;
  bool row_wise_materialization = false;

  if(peloton_layout == LAYOUT_COLUMN)
    row_wise_materialization = true;

  if(peloton_layout == LAYOUT_HYBRID &&
      dest_tile_column_count > column_count_threshold)
    row_wise_materialization = true;

  // Materialize as needed
  if(row_wise_materialization == true) {
    MaterializeRowAtAtATime(source_tile, old_to_new_cols, tile_to_cols, dest_tile);
  }
  else {
    MaterializeColumnAtATime(source_tile, old_to_new_cols, tile_to_cols, dest_tile);
  }

}

void MaterializeRowAtAtATime(LogicalTile *source_tile,
                             const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
                             const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
                             storage::Tile *dest_tile) {

  ///////////////////////////
  // EACH PHYSICAL TILE
  ///////////////////////////
  // Copy over all data from each base tile.
  for (const auto &kv : tile_to_cols) {
    const std::vector<oid_t> &old_column_ids = kv.second;
<commit_msg>Checkpoint<commit_after>//===----------------------------------------------------------------------===//
//
//                         PelotonDB
//
// materialization_executor.cpp
//
// Identification: src/backend/executor/materialization_executor.cpp
//
// Copyright (c) 2015, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "backend/executor/materialization_executor.h"

#include <cassert>
#include <memory>
#include <utility>

#include "backend/planner/materialization_plan.h"
#include "backend/executor/logical_tile.h"
#include "backend/executor/logical_tile_factory.h"
#include "backend/storage/tile.h"
#include "backend/storage/data_table.h"

namespace peloton {
namespace executor {

// Row-oriented materialization
void MaterializeRowAtAtATime(LogicalTile *source_tile,
                             const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
                             const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
                             storage::Tile *dest_tile);

// Column-oriented materialization
void MaterializeColumnAtATime(LogicalTile *source_tile,
                              const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
                              const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
                              storage::Tile *dest_tile);

/**
 * @brief Constructor for the materialization executor.
 * @param node Materialization node corresponding to this executor.
 */
MaterializationExecutor::MaterializationExecutor(
    planner::AbstractPlan *node, ExecutorContext *executor_context)
: AbstractExecutor(node, executor_context) {}

/**
 * @brief Nothing to init at the moment.
 * @return true on success, false otherwise.
 */
bool MaterializationExecutor::DInit() {
  assert(children_.size() == 1);

  return true;
}

/**
 * @brief Generates map from each base tile to columns originally from that
 *        base tile to be materialized.
 * @param column_ids Ids of columns to be materialized.
 * @param source_tile Logical tile that contains mapping from columns to
 *        base tiles.
 * @param tile_to_cols Map to be populated with mappings from tile to columns.
 *
 * We generate this mapping so that we can materialize columns tile by tile for
 * efficiency reasons.
 */
void MaterializationExecutor::GenerateTileToColMap(
    const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
    LogicalTile *source_tile,
    std::unordered_map<storage::Tile *, std::vector<oid_t>> &
    cols_in_physical_tile) {
  for (const auto &kv : old_to_new_cols) {
    oid_t col = kv.first;

    // figure out base physical tile for column in logical tile
    storage::Tile *base_tile = source_tile->GetBaseTile(col);

    std::vector<oid_t> &cols_from_tile = cols_in_physical_tile[base_tile];
    cols_from_tile.push_back(col);
  }
}

/**
 * @brief Does the actual copying of data into the new physical tile.
 * @param source_tile Source tile to copy data from.
 * @param tile_to_cols Map from base tile to columns in that tile
 *        to be materialized.
 * @param dest_tile New tile to copy data into.
 */
void MaterializationExecutor::MaterializeByTiles(
    LogicalTile *source_tile,
    const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
    const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
    storage::Tile *dest_tile) {

  auto dest_tile_column_count = dest_tile->GetColumnCount();
  // TODO: Make this a parameter
  oid_t column_count_threshold = 20;
  bool row_wise_materialization = true;

  if(peloton_layout == LAYOUT_COLUMN)
    row_wise_materialization = false;

  if(peloton_layout == LAYOUT_HYBRID &&
      dest_tile_column_count < column_count_threshold)
    row_wise_materialization = false;

  // Materialize as needed
  if(row_wise_materialization == true) {
    MaterializeRowAtAtATime(source_tile, old_to_new_cols, tile_to_cols, dest_tile);
  }
  else {
    MaterializeColumnAtATime(source_tile, old_to_new_cols, tile_to_cols, dest_tile);
  }

}

void MaterializeRowAtAtATime(LogicalTile *source_tile,
                             const std::unordered_map<oid_t, oid_t> &old_to_new_cols,
                             const std::unordered_map<storage::Tile *, std::vector<oid_t>> &tile_to_cols,
                             storage::Tile *dest_tile) {

  ///////////////////////////
  // EACH PHYSICAL TILE
  ///////////////////////////
  // Copy over all data from each base tile.
  for (const auto &kv : tile_to_cols) {
    const std::vector<oid_t> &old_column_ids = kv.second;

{'fraction_non_alphanumeric': 0.13733075435203096, 'fraction_numerical': 0.0015044057597249087, 'mean_word_length': 2.9453158117846545, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3827131', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2747, 'n_words': 666}