<commit_before>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// transaction_tests_util.cpp
//
// Identification: test/concurrency/transaction_tests_util.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "concurrency/testing_transaction_util.h"

#include "catalog/catalog.h"
#include "catalog/catalog_storage_manager.h"
#include "executor/delete_executor.h"
#include "executor/executor_context.h"
#include "executor/index_scan_executor.h"
#include "executor/insert_executor.h"
#include "executor/logical_tile_factory.h"
#include "executor/mock_executor.h"
#include "executor/seq_scan_executor.h"
#include "executor/update_executor.h"
#include "expression/expression_util.h"
#include "planner/delete_plan.h"
#include "planner/index_scan_plan.h"
#include "planner/insert_plan.h"
#include "storage/database.h"
#include "storage/tile.h"

namespace peloton {
namespace executor {
class ExecutorContext;
}
namespace planner {
class InsertPlan;
class ProjectInfo;
}
namespace test {

storage::DataTable *TestingTransactionUtil::CreateCombinedPrimaryKeyTable() {
  auto id_column =
      catalog::Column(type::TypeId::INTEGER,
                      type::Type::GetTypeSize(type::TypeId::INTEGER), "id", true);
  id_column.AddConstraint(
      catalog::Constraint(ConstraintType::NOTNULL, "not_null"));
  auto value_column = catalog::Column(
      type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
      "value", true);
  value_column.AddConstraint(
      catalog::Constraint(ConstraintType::NOTNULL, "not_null"));

  // Create the table
  catalog::Schema *table_schema =
      new catalog::Schema({id_column, value_column});
  auto table_name = "TEST_TABLE";
  size_t tuples_per_tilegroup = 100;
  auto table = storage::TableFactory::GetDataTable(
     CATALOG_DATABASE_OID, TEST_TABLE_OID, table_schema, table_name, tuples_per_tilegroup, true, false);

  // Create index on the (id, value) column
  std::vector<oid_t> key_attrs = {0, 1};
  auto tuple_schema = table->GetSchema();
  bool unique = false;
  auto key_schema = catalog::Schema::CopySchema(tuple_schema, key_attrs);
  key_schema->SetIndexedColumns(key_attrs);

  auto index_metadata = new index::IndexMetadata(
      "primary_btree_index", 1234, TEST_TABLE_OID, CATALOG_DATABASE_OID, IndexType::BWTREE,
      IndexConstraintType::PRIMARY_KEY, tuple_schema, key_schema, key_attrs,
      unique);

  std::shared_ptr<index::Index> pkey_index(
      index::IndexFactory::GetIndex(index_metadata));

  table->AddIndex(pkey_index);

  // Insert tuple
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  for (int i = 0; i < 10; i++) {
    ExecuteInsert(txn, table, i, i);
  }
  txn_manager.CommitTransaction(txn);

  return table;
}

storage::DataTable *TestingTransactionUtil::CreatePrimaryKeyUniqueKeyTable() {
  auto id_column =
      catalog::Column(type::TypeId::INTEGER,
                      type::Type::GetTypeSize(type::TypeId::INTEGER), "id", true);
  id_column.AddConstraint(
      catalog::Constraint(ConstraintType::NOTNULL, "not_null"));
  auto value_column = catalog::Column(
      type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
      "value", true);

  // Create the table
  catalog::Schema *table_schema =
      new catalog::Schema({id_column, value_column});
  auto table_name = "TEST_TABLE";
  size_t tuples_per_tilegroup = 100;
  auto table = storage::TableFactory::GetDataTable(
     CATALOG_DATABASE_OID, TEST_TABLE_OID, table_schema, table_name, tuples_per_tilegroup, true, false);

  // Create primary index on the id column
  std::vector<oid_t> key_attrs = {0};
  auto tuple_schema = table->GetSchema();
  bool unique = false;
  auto key_schema = catalog::Schema::CopySchema(tuple_schema, key_attrs);
  key_schema->SetIndexedColumns(key_attrs);

  auto index_metadata = new index::IndexMetadata(
      "primary_btree_index", 1234, TEST_TABLE_OID, CATALOG_DATABASE_OID, IndexType::BWTREE,
      IndexConstraintType::PRIMARY_KEY, tuple_schema, key_schema, key_attrs,
      unique);

  std::shared_ptr<index::Index> pkey_index(
      index::IndexFactory::GetIndex(index_metadata));

  table->AddIndex(pkey_index);

  // Create unique index on the value column
  std::vector<oid_t> key_attrs2 = {1};
  auto tuple_schema2 = table->GetSchema();
  bool unique2 = false;
  auto key_schema2 = catalog::Schema::CopySchema(tuple_schema2, key_attrs2);
  key_schema2->SetIndexedColumns(key_attrs2);
  auto index_metadata2 =
      new index::IndexMetadata("unique_btree_index", 1235, TEST_TABLE_OID, CATALOG_DATABASE_OID,
                               IndexType::BWTREE, IndexConstraintType::UNIQUE,
                               tuple_schema2, key_schema2, key_attrs2, unique2);

  std::shared_ptr<index::Index> ukey_index(
      index::IndexFactory::GetIndex(index_metadata2));

  table->AddIndex(ukey_index);

  // Insert tuple
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  for (int i = 0; i < 10; i++) {
    ExecuteInsert(txn, table, i, i);
  }
  txn_manager.CommitTransaction(txn);

  return table;
}

storage::DataTable *TestingTransactionUtil::CreateTable(
    int num_key, std::string table_name, oid_t database_id, oid_t relation_id,
    oid_t index_oid, bool need_primary_index) {
  auto id_column =
      catalog::Column(type::TypeId::INTEGER,
                      type::Type::GetTypeSize(type::TypeId::INTEGER), "id", true);
  auto value_column = catalog::Column(
      type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
      "value", true);

  // Create the table
  catalog::Schema *table_schema =
      new catalog::Schema({id_column, value_column});

  size_t tuples_per_tilegroup = 100;
  auto table = storage::TableFactory::GetDataTable(
      database_id, relation_id, table_schema, table_name, tuples_per_tilegroup,
      true, false);

  // Create index on the id column
  std::vector<oid_t> key_attrs = {0};
  auto tuple_schema = table->GetSchema();
  bool unique = false;
  auto key_schema = catalog::Schema::CopySchema(tuple_schema, key_attrs);
  key_schema->SetIndexedColumns(key_attrs);

  auto index_metadata = new index::IndexMetadata(
      "primary_btree_index", index_oid, TEST_TABLE_OID, CATALOG_DATABASE_OID, IndexType::BWTREE,
      need_primary_index ? IndexConstraintType::PRIMARY_KEY
                         : IndexConstraintType::DEFAULT,
      tuple_schema, key_schema, key_attrs, unique);

  std::shared_ptr<index::Index> pkey_index(
      index::IndexFactory::GetIndex(index_metadata));

  table->AddIndex(pkey_index);

  // add this table to current database
  catalog::Catalog::GetInstance();
  LOG_INFO("the database_id is %d", database_id);
  storage::Database *db;
  try{
      db = catalog::CatalogStorageManager::GetInstance()->GetDatabaseWithOid(database_id);
  } catch (CatalogException &e) {
      LOG_TRACE("Can't find database %d! ", database_id);
  }
  PL_ASSERT(db);
  db->AddTable(table);

  // Insert tuple<commit_msg>Found the culprit, should run now<commit_after>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// transaction_tests_util.cpp
//
// Identification: test/concurrency/transaction_tests_util.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "concurrency/testing_transaction_util.h"

#include "catalog/catalog.h"
#include "catalog/catalog_storage_manager.h"
#include "executor/delete_executor.h"
#include "executor/executor_context.h"
#include "executor/index_scan_executor.h"
#include "executor/insert_executor.h"
#include "executor/logical_tile_factory.h"
#include "executor/mock_executor.h"
#include "executor/seq_scan_executor.h"
#include "executor/update_executor.h"
#include "expression/expression_util.h"
#include "planner/delete_plan.h"
#include "planner/index_scan_plan.h"
#include "planner/insert_plan.h"
#include "storage/database.h"
#include "storage/tile.h"

namespace peloton {
namespace executor {
class ExecutorContext;
}
namespace planner {
class InsertPlan;
class ProjectInfo;
}
namespace test {

storage::DataTable *TestingTransactionUtil::CreateCombinedPrimaryKeyTable() {
  auto id_column =
      catalog::Column(type::TypeId::INTEGER,
                      type::Type::GetTypeSize(type::TypeId::INTEGER), "id", true);
  id_column.AddConstraint(
      catalog::Constraint(ConstraintType::NOTNULL, "not_null"));
  auto value_column = catalog::Column(
      type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
      "value", true);
  value_column.AddConstraint(
      catalog::Constraint(ConstraintType::NOTNULL, "not_null"));

  // Create the table
  catalog::Schema *table_schema =
      new catalog::Schema({id_column, value_column});
  auto table_name = "TEST_TABLE";
  size_t tuples_per_tilegroup = 100;
  auto table = storage::TableFactory::GetDataTable(
     CATALOG_DATABASE_OID, TEST_TABLE_OID, table_schema, table_name, tuples_per_tilegroup, true, false);

  // Create index on the (id, value) column
  std::vector<oid_t> key_attrs = {0, 1};
  auto tuple_schema = table->GetSchema();
  bool unique = false;
  auto key_schema = catalog::Schema::CopySchema(tuple_schema, key_attrs);
  key_schema->SetIndexedColumns(key_attrs);

  auto index_metadata = new index::IndexMetadata(
      "primary_btree_index", 1234, TEST_TABLE_OID, CATALOG_DATABASE_OID, IndexType::BWTREE,
      IndexConstraintType::PRIMARY_KEY, tuple_schema, key_schema, key_attrs,
      unique);

  std::shared_ptr<index::Index> pkey_index(
      index::IndexFactory::GetIndex(index_metadata));

  table->AddIndex(pkey_index);

  // Insert tuple
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  for (int i = 0; i < 10; i++) {
    ExecuteInsert(txn, table, i, i);
  }
  txn_manager.CommitTransaction(txn);

  return table;
}

storage::DataTable *TestingTransactionUtil::CreatePrimaryKeyUniqueKeyTable() {
  auto id_column =
      catalog::Column(type::TypeId::INTEGER,
                      type::Type::GetTypeSize(type::TypeId::INTEGER), "id", true);
  id_column.AddConstraint(
      catalog::Constraint(ConstraintType::NOTNULL, "not_null"));
  auto value_column = catalog::Column(
      type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
      "value", true);

  // Create the table
  catalog::Schema *table_schema =
      new catalog::Schema({id_column, value_column});
  auto table_name = "TEST_TABLE";
  size_t tuples_per_tilegroup = 100;
  auto table = storage::TableFactory::GetDataTable(
     CATALOG_DATABASE_OID, TEST_TABLE_OID, table_schema, table_name, tuples_per_tilegroup, true, false);

  // Create primary index on the id column
  std::vector<oid_t> key_attrs = {0};
  auto tuple_schema = table->GetSchema();
  bool unique = false;
  auto key_schema = catalog::Schema::CopySchema(tuple_schema, key_attrs);
  key_schema->SetIndexedColumns(key_attrs);

  auto index_metadata = new index::IndexMetadata(
      "primary_btree_index", 1234, TEST_TABLE_OID, CATALOG_DATABASE_OID, IndexType::BWTREE,
      IndexConstraintType::PRIMARY_KEY, tuple_schema, key_schema, key_attrs,
      unique);

  std::shared_ptr<index::Index> pkey_index(
      index::IndexFactory::GetIndex(index_metadata));

  table->AddIndex(pkey_index);

  // Create unique index on the value column
  std::vector<oid_t> key_attrs2 = {1};
  auto tuple_schema2 = table->GetSchema();
  bool unique2 = false;
  auto key_schema2 = catalog::Schema::CopySchema(tuple_schema2, key_attrs2);
  key_schema2->SetIndexedColumns(key_attrs2);
  auto index_metadata2 =
      new index::IndexMetadata("unique_btree_index", 1235, TEST_TABLE_OID, CATALOG_DATABASE_OID,
                               IndexType::BWTREE, IndexConstraintType::UNIQUE,
                               tuple_schema2, key_schema2, key_attrs2, unique2);

  std::shared_ptr<index::Index> ukey_index(
      index::IndexFactory::GetIndex(index_metadata2));

  table->AddIndex(ukey_index);

  // Insert tuple
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  for (int i = 0; i < 10; i++) {
    ExecuteInsert(txn, table, i, i);
  }
  txn_manager.CommitTransaction(txn);

  return table;
}

storage::DataTable *TestingTransactionUtil::CreateTable(
    int num_key, std::string table_name, oid_t database_id, oid_t relation_id,
    oid_t index_oid, bool need_primary_index) {
  auto id_column =
      catalog::Column(type::TypeId::INTEGER,
                      type::Type::GetTypeSize(type::TypeId::INTEGER), "id", true);
  auto value_column = catalog::Column(
      type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
      "value", true);

  // Create the table
  catalog::Schema *table_schema =
      new catalog::Schema({id_column, value_column});

  size_t tuples_per_tilegroup = 100;
  auto table = storage::TableFactory::GetDataTable(
      database_id, relation_id, table_schema, table_name, tuples_per_tilegroup,
      true, false);

  // Create index on the id column
  std::vector<oid_t> key_attrs = {0};
  auto tuple_schema = table->GetSchema();
  bool unique = false;
  auto key_schema = catalog::Schema::CopySchema(tuple_schema, key_attrs);
  key_schema->SetIndexedColumns(key_attrs);

  auto index_metadata = new index::IndexMetadata(
      "primary_btree_index", index_oid, TEST_TABLE_OID, CATALOG_DATABASE_OID, IndexType::BWTREE,
      need_primary_index ? IndexConstraintType::PRIMARY_KEY
                         : IndexConstraintType::DEFAULT,
      tuple_schema, key_schema, key_attrs, unique);

  std::shared_ptr<index::Index> pkey_index(
      index::IndexFactory::GetIndex(index_metadata));

  table->AddIndex(pkey_index);

  // add this table to current database
  catalog::Catalog::GetInstance();
  LOG_INFO("the database_id is %d", database_id);
  storage::Database *db = nullptr;
  try{
      db = catalog::CatalogStorageManager::GetInstance()->GetDatabaseWithOid(database_id);
  } catch (CatalogException &e) {
      LOG_TRACE("Can't find database %d! ", database_id);
      return nullptr;
  }
  PL_ASSERT(db);
  db->AddTable(table);

  // Insert tuple
{'fraction_non_alphanumeric': 0.14267051705170516, 'fraction_numerical': 0.007150715071507151, 'mean_word_length': 4.320043891733723, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, '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': '3129262', 'n_tokens_mistral': 4689, 'n_tokens_neox': 4519, 'n_words': 859}