<commit_before>/*-------------------------------------------------------------------------
 *
 * ddl_utilities.cpp
 * file description
 *
 * Copyright(c) 2015, CMU
 *
 * /peloton/src/backend/bridge/ddl_utilities.cpp
 *
 *-------------------------------------------------------------------------
 */

#include <cassert>
#include <iostream>

#include "backend/bridge/ddl/ddl_utils.h"
#include "backend/common/logger.h"
#include "backend/storage/database.h"

#include "parser/parse_type.h"
#include "utils/syscache.h"
#include "miscadmin.h"
#include "parser/parse_utilcmd.h"
#include "access/htup_details.h"
#include "utils/resowner.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"

namespace peloton {
namespace bridge {

//===--------------------------------------------------------------------===//
// DDL Utils
//===--------------------------------------------------------------------===//

/**
 * @brief preparing data 
 * @param parsetree
 */
void DDLUtils::peloton_prepare_data(Node* parsetree){
  switch(nodeTag(parsetree)){
      case T_CreateStmt:
      case T_CreateForeignTableStmt:{
        List* stmts = ((CreateStmt*)parsetree)->stmts;
        ListCell* l;

        foreach (l, stmts) {
          Node* stmt = (Node*)lfirst(l);
          if (IsA(stmt, CreateStmt)) {
            CreateStmt* Cstmt = (CreateStmt*)stmt;

            // Get the column list from the create statement
            List* ColumnList = (List*)(Cstmt->tableElts);

            // Parse the CreateStmt and construct ColumnInfo
            ListCell* entry;
            foreach (entry, ColumnList) {
              ColumnDef* coldef = static_cast<ColumnDef*>(lfirst(entry));

              Oid typeoid = typenameTypeId(NULL, coldef->typeName);
              int32 typemod;
              typenameTypeIdAndMod(NULL, coldef->typeName, &typeoid, &typemod);
        
              // Get type length
              Type tup = typeidType(typeoid);
              int typelen = typeLen(tup);
              ReleaseSysCache(tup);
        
              // For a fixed-size type, typlen is the number of bytes in the internal
              // representation of the type. But for a variable-length type, typlen is
              // negative.
              if (typelen == -1) typelen = typemod;

              // Use existing TypeName structure
              coldef->typeName->typeOid = typeoid;
              coldef->typeName->typemod = typelen;
            }
          }
        }
      break;
    }
  }
}

/**
 * @brief parsing create statement
 * @param Cstmt a create statement
 * @param column_infos to create a table
 * @param refernce_table_infos to store reference table to the table
 */
void DDLUtils::ParsingCreateStmt(
    CreateStmt* Cstmt, std::vector<catalog::Column>& column_infos,
    std::vector<catalog::ForeignKey>& foreign_keys) {
  assert(Cstmt);

  //===--------------------------------------------------------------------===//
  // Column Infomation
  //===--------------------------------------------------------------------===//

  // Get the column list from the create statement
  List* ColumnList = (List*)(Cstmt->tableElts);

  // Parse the CreateStmt and construct ColumnInfo
  ListCell* entry;
  foreach (entry, ColumnList) {
    ColumnDef* coldef = static_cast<ColumnDef*>(lfirst(entry));

    // Get the type oid and type mod with given typeName
    Oid typeoid = coldef->typeName->typeOid;
    int typelen = coldef->typeName->typemod;

    ValueType column_valueType =
        PostgresValueTypeToPelotonValueType((PostgresValueType)typeoid);
    int column_length = typelen;
    std::string column_name = coldef->colname;

    //===--------------------------------------------------------------------===//
    // Column Constraint
    //===--------------------------------------------------------------------===//

    std::vector<catalog::Constraint> column_constraints;

    if (coldef->raw_default != NULL) {
      catalog::Constraint constraint(CONSTRAINT_TYPE_DEFAULT, "",
                                     coldef->raw_default);<commit_msg>Added new variables in ColumnDef to avoid potential risk<commit_after>/*-------------------------------------------------------------------------
 *
 * ddl_utilities.cpp
 * file description
 *
 * Copyright(c) 2015, CMU
 *
 * /peloton/src/backend/bridge/ddl_utilities.cpp
 *
 *-------------------------------------------------------------------------
 */

#include <cassert>
#include <iostream>

#include "backend/bridge/ddl/ddl_utils.h"
#include "backend/common/logger.h"
#include "backend/storage/database.h"

#include "parser/parse_type.h"
#include "utils/syscache.h"
#include "miscadmin.h"
#include "parser/parse_utilcmd.h"
#include "access/htup_details.h"
#include "utils/resowner.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"

namespace peloton {
namespace bridge {

//===--------------------------------------------------------------------===//
// DDL Utils
//===--------------------------------------------------------------------===//

/**
 * @brief preparing data 
 * @param parsetree
 */
void DDLUtils::peloton_prepare_data(Node* parsetree){
  switch(nodeTag(parsetree)){
      case T_CreateStmt:
      case T_CreateForeignTableStmt:{
        List* stmts = ((CreateStmt*)parsetree)->stmts;
        ListCell* l;

        foreach (l, stmts) {
          Node* stmt = (Node*)lfirst(l);
          if (IsA(stmt, CreateStmt)) {
            CreateStmt* Cstmt = (CreateStmt*)stmt;

            // Get the column list from the create statement
            List* ColumnList = (List*)(Cstmt->tableElts);

            // Parse the CreateStmt and construct ColumnInfo
            ListCell* entry;
            foreach (entry, ColumnList) {
              ColumnDef* coldef = static_cast<ColumnDef*>(lfirst(entry));

              Oid typeoid = typenameTypeId(NULL, coldef->typeName);
              int32 typemod;
              typenameTypeIdAndMod(NULL, coldef->typeName, &typeoid, &typemod);
        
              // Get type length
              Type tup = typeidType(typeoid);
              int typelen = typeLen(tup);
              ReleaseSysCache(tup);
        
              // For a fixed-size type, typlen is the number of bytes in the internal
              // representation of the type. But for a variable-length type, typlen is
              // negative.
              if (typelen == -1) typelen = typemod;

              // Use existing TypeName structure
              coldef->typeName->type_oid = typeoid;
              coldef->typeName->type_len = typelen;
            }
          }
        }
      break;
    }
    break;
  }
}

/**
 * @brief parsing create statement
 * @param Cstmt a create statement
 * @param column_infos to create a table
 * @param refernce_table_infos to store reference table to the table
 */
void DDLUtils::ParsingCreateStmt(
    CreateStmt* Cstmt, std::vector<catalog::Column>& column_infos,
    std::vector<catalog::ForeignKey>& foreign_keys) {
  assert(Cstmt);

  //===--------------------------------------------------------------------===//
  // Column Infomation
  //===--------------------------------------------------------------------===//

  // Get the column list from the create statement
  List* ColumnList = (List*)(Cstmt->tableElts);

  // Parse the CreateStmt and construct ColumnInfo
  ListCell* entry;
  foreach (entry, ColumnList) {
    ColumnDef* coldef = static_cast<ColumnDef*>(lfirst(entry));

    // Get the type oid and type mod with given typeName
    Oid typeoid = coldef->typeName->type_oid;
    int typelen = coldef->typeName->type_len;

    ValueType column_valueType =
        PostgresValueTypeToPelotonValueType((PostgresValueType)typeoid);
    int column_length = typelen;
    std::string column_name = coldef->colname;

    //===--------------------------------------------------------------------===//
    // Column Constraint
    //===--------------------------------------------------------------------===//

    std::vector<catalog::Constraint> column_constraints;

    if (coldef->raw_default != NULL) {
      catalog::Constraint constraint(CONSTRAINT_TYPE_DEFAULT, "",
                                     coldef->raw_default);
{'fraction_non_alphanumeric': 0.2426129426129426, 'fraction_numerical': 0.0017094017094017094, 'mean_word_length': 1.63206940874036, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5269539', 'n_tokens_mistral': 2204, 'n_tokens_neox': 2171, 'n_words': 506}