OmniSciDB  ba1bac9284
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Parser::AlterTableStmt Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::AlterTableStmt:
+ Collaboration diagram for Parser::AlterTableStmt:

Public Member Functions

const std::string * get_table () const
 
void execute (const Catalog_Namespace::SessionInfo &session) override
 
- Public Member Functions inherited from Parser::DDLStmt
void setColumnDescriptor (ColumnDescriptor &cd, const ColumnDef *coldef)
 
- Public Member Functions inherited from Parser::Node
virtual ~Node ()
 

Static Public Member Functions

static void delegateExecute (const rapidjson::Value &payload, const Catalog_Namespace::SessionInfo &session)
 

Private Attributes

std::unique_ptr< std::string > table
 
const rapidjson::Value payload
 

Detailed Description

Definition at line 1157 of file ParserNode.h.

Member Function Documentation

void Parser::AlterTableStmt::delegateExecute ( const rapidjson::Value &  payload,
const Catalog_Namespace::SessionInfo session 
)
static

Definition at line 3642 of file ParserNode.cpp.

References CHECK, Parser::RenameTableStmt::execute(), Parser::RenameColumnStmt::execute(), Parser::AddColumnStmt::execute(), Parser::DropColumnStmt::execute(), Parser::AlterTableParamStmt::execute(), logger::FATAL, json_bool(), json_i64(), json_str(), LOG, split(), generate_TableFunctionsFactory_init::sql_types, to_sql_type(), and run_benchmark_import::type.

Referenced by DdlCommandExecutor::execute(), and Parser::execute_calcite_ddl().

3643  {
3644  CHECK(payload.HasMember("tableName"));
3645  auto tableName = json_str(payload["tableName"]);
3646 
3647  CHECK(payload.HasMember("alterType"));
3648  auto type = json_str(payload["alterType"]);
3649 
3650  if (type == "RENAME_TABLE") {
3651  CHECK(payload.HasMember("newTableName"));
3652  auto newTableName = json_str(payload["newTableName"]);
3653  Parser::RenameTableStmt(new std::string(tableName), new std::string(newTableName))
3654  .execute(session);
3655 
3656  } else if (type == "RENAME_COLUMN") {
3657  CHECK(payload.HasMember("columnName"));
3658  auto columnName = json_str(payload["columnName"]);
3659  CHECK(payload.HasMember("newColumnName"));
3660  auto newColumnName = json_str(payload["newColumnName"]);
3661  Parser::RenameColumnStmt(new std::string(tableName),
3662  new std::string(columnName),
3663  new std::string(newColumnName))
3664  .execute(session);
3665 
3666  } else if (type == "ADD_COLUMN") {
3667  CHECK(payload.HasMember("columnData"));
3668  CHECK(payload["columnData"].IsArray());
3669 
3670  // TODO : Shares alot of code below with CREATE_TABLE -> try to merge these two ?
3671  // unique_ptr vs regular
3672 
3673  // New Columns go into this list
3674  std::list<ColumnDef*>* table_element_list_ = new std::list<ColumnDef*>;
3675 
3676  const auto elements = payload["columnData"].GetArray();
3677  for (const auto& element : elements) {
3678  CHECK(element.IsObject());
3679  CHECK(element.HasMember("type"));
3680  if (json_str(element["type"]) == "SQL_COLUMN_DECLARATION") {
3681  CHECK(element.HasMember("name"));
3682  auto col_name = std::make_unique<std::string>(json_str(element["name"]));
3683  CHECK(element.HasMember("sqltype"));
3684  const auto sql_types = to_sql_type(json_str(element["sqltype"]));
3685 
3686  // decimal / numeric precision / scale
3687  int precision = -1;
3688  int scale = -1;
3689  if (element.HasMember("precision")) {
3690  precision = json_i64(element["precision"]);
3691  }
3692  if (element.HasMember("scale")) {
3693  scale = json_i64(element["scale"]);
3694  }
3695 
3696  std::optional<int64_t> array_size;
3697  if (element.HasMember("arraySize")) {
3698  // We do not yet support geo arrays
3699  array_size = json_i64(element["arraySize"]);
3700  }
3701  std::unique_ptr<SQLType> sql_type;
3702  if (element.HasMember("subtype")) {
3703  CHECK(element.HasMember("coordinateSystem"));
3704  const auto subtype_sql_types = to_sql_type(json_str(element["subtype"]));
3705  sql_type = std::make_unique<SQLType>(
3706  subtype_sql_types,
3707  static_cast<int>(sql_types),
3708  static_cast<int>(json_i64(element["coordinateSystem"])),
3709  false);
3710  } else if (precision > 0 && scale > 0) {
3711  sql_type = std::make_unique<SQLType>(sql_types,
3712  precision,
3713  scale,
3714  /*is_array=*/array_size.has_value(),
3715  array_size ? *array_size : -1);
3716  } else if (precision > 0) {
3717  sql_type = std::make_unique<SQLType>(sql_types,
3718  precision,
3719  0,
3720  /*is_array=*/array_size.has_value(),
3721  array_size ? *array_size : -1);
3722  } else {
3723  sql_type = std::make_unique<SQLType>(sql_types,
3724  /*is_array=*/array_size.has_value(),
3725  array_size ? *array_size : -1);
3726  }
3727  CHECK(sql_type);
3728 
3729  CHECK(element.HasMember("nullable"));
3730  const auto nullable = json_bool(element["nullable"]);
3731  std::unique_ptr<ColumnConstraintDef> constraint_def;
3732  if (!nullable) {
3733  StringLiteral* str_literal = nullptr;
3734  if (element.HasMember("expression") && !element["expression"].IsNull()) {
3735  std::string* defaultval = new std::string(json_str(element["expression"]));
3736  boost::algorithm::trim_if(*defaultval, boost::is_any_of(" \"'`"));
3737  str_literal = new StringLiteral(defaultval);
3738  }
3739 
3740  constraint_def =
3741  std::make_unique<ColumnConstraintDef>(/*notnull=*/true,
3742  /*unique=*/false,
3743  /*primarykey=*/false,
3744  /*defaultval=*/str_literal);
3745  }
3746  std::unique_ptr<CompressDef> compress_def;
3747  if (element.HasMember("encodingType") && !element["encodingType"].IsNull()) {
3748  std::string encoding_type = json_str(element["encodingType"]);
3749  CHECK(element.HasMember("encodingSize"));
3750  auto encoding_name =
3751  std::make_unique<std::string>(json_str(element["encodingType"]));
3752  compress_def = std::make_unique<CompressDef>(encoding_name.release(),
3753  json_i64(element["encodingSize"]));
3754  }
3755  auto col_def = new ColumnDef(col_name.release(),
3756  sql_type.release(),
3757  compress_def ? compress_def.release() : nullptr,
3758  constraint_def ? constraint_def.release() : nullptr);
3759  table_element_list_->emplace_back(col_def);
3760 
3761  } else {
3762  LOG(FATAL) << "Unsupported element type for ALTER TABLE: "
3763  << element["type"].GetString();
3764  }
3765  }
3766 
3767  Parser::AddColumnStmt(new std::string(tableName), table_element_list_)
3768  .execute(session);
3769 
3770  } else if (type == "DROP_COLUMN") {
3771  CHECK(payload.HasMember("columnData"));
3772  auto columnData = json_str(payload["columnData"]);
3773  // Convert columnData to std::list<std::string*>*
3774  // allocate std::list<> as DropColumnStmt will delete it;
3775  std::list<std::string*>* cols = new std::list<std::string*>;
3776  std::vector<std::string> cols1;
3777  boost::split(cols1, columnData, boost::is_any_of(","));
3778  for (auto s : cols1) {
3779  // strip leading/trailing spaces/quotes/single quotes
3780  boost::algorithm::trim_if(s, boost::is_any_of(" \"'`"));
3781  std::string* str = new std::string(s);
3782  cols->emplace_back(str);
3783  }
3784 
3785  Parser::DropColumnStmt(new std::string(tableName), cols).execute(session);
3786 
3787  } else if (type == "ALTER_OPTIONS") {
3788  CHECK(payload.HasMember("options"));
3789 
3790  if (payload["options"].IsObject()) {
3791  for (const auto& option : payload["options"].GetObject()) {
3792  std::string* option_name = new std::string(json_str(option.name));
3793  Literal* literal_value;
3794  if (option.value.IsString()) {
3795  std::string literal_string = json_str(option.value);
3796 
3797  // iff this string can be converted to INT
3798  // ... do so because it is necessary for AlterTableParamStmt
3799  std::size_t sz;
3800  int iVal = std::stoi(literal_string, &sz);
3801  if (sz == literal_string.size()) {
3802  literal_value = new IntLiteral(iVal);
3803  } else {
3804  literal_value = new StringLiteral(&literal_string);
3805  }
3806  } else if (option.value.IsInt() || option.value.IsInt64()) {
3807  literal_value = new IntLiteral(json_i64(option.value));
3808  } else if (option.value.IsNull()) {
3809  literal_value = new NullLiteral();
3810  } else {
3811  throw std::runtime_error("Unable to handle literal for " + *option_name);
3812  }
3813  CHECK(literal_value);
3814 
3815  NameValueAssign* p1 = new NameValueAssign(option_name, literal_value);
3816  Parser::AlterTableParamStmt(new std::string(tableName), p1).execute(session);
3817  }
3818 
3819  } else {
3820  CHECK(payload["options"].IsNull());
3821  }
3822  }
3823 }
SQLTypes to_sql_type(const std::string &type_name)
void execute(const Catalog_Namespace::SessionInfo &session) override
#define LOG(tag)
Definition: Logger.h:200
const bool json_bool(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:49
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
void execute(const Catalog_Namespace::SessionInfo &session) override
void execute(const Catalog_Namespace::SessionInfo &session) override
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
void execute(const Catalog_Namespace::SessionInfo &session) override
const int64_t json_i64(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:39
#define CHECK(condition)
Definition: Logger.h:206
void execute(const Catalog_Namespace::SessionInfo &session) override
const rapidjson::Value payload
Definition: ParserNode.h:1168

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Parser::AlterTableStmt::execute ( const Catalog_Namespace::SessionInfo session)
overridevirtual

Implements Parser::DDLStmt.

Definition at line 3640 of file ParserNode.cpp.

Referenced by omnisci.cursor.Cursor::executemany().

3640 {}

+ Here is the caller graph for this function:

const std::string* Parser::AlterTableStmt::get_table ( ) const
inline

Definition at line 1162 of file ParserNode.h.

References table.

1162 { return table.get(); }
std::unique_ptr< std::string > table
Definition: ParserNode.h:1167

Member Data Documentation

const rapidjson::Value Parser::AlterTableStmt::payload
private
std::unique_ptr<std::string> Parser::AlterTableStmt::table
private

Definition at line 1167 of file ParserNode.h.

Referenced by get_table().


The documentation for this class was generated from the following files: