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

#include <ParserNode.h>

+ Inheritance diagram for Parser::AddColumnStmt:
+ Collaboration diagram for Parser::AddColumnStmt:

Public Member Functions

 AddColumnStmt (std::string *tab, ColumnDef *coldef)
 
 AddColumnStmt (std::string *tab, std::list< ColumnDef * > *coldefs)
 
void execute (const Catalog_Namespace::SessionInfo &session) override
 
void check_executable (const Catalog_Namespace::SessionInfo &session, const TableDescriptor *td)
 
const std::string * get_table () const
 
- Public Member Functions inherited from Parser::DDLStmt
void setColumnDescriptor (ColumnDescriptor &cd, const ColumnDef *coldef)
 
- Public Member Functions inherited from Parser::Node
virtual ~Node ()
 

Private Attributes

std::unique_ptr< std::string > table
 
std::unique_ptr< ColumnDefcoldef
 
std::list< std::unique_ptr
< ColumnDef > > 
coldefs
 

Detailed Description

Definition at line 1337 of file ParserNode.h.

Constructor & Destructor Documentation

Parser::AddColumnStmt::AddColumnStmt ( std::string *  tab,
ColumnDef coldef 
)
inline

Definition at line 1339 of file ParserNode.h.

1339 : table(tab), coldef(coldef) {}
std::unique_ptr< std::string > table
Definition: ParserNode.h:1352
std::unique_ptr< ColumnDef > coldef
Definition: ParserNode.h:1353
Parser::AddColumnStmt::AddColumnStmt ( std::string *  tab,
std::list< ColumnDef * > *  coldefs 
)
inline

Definition at line 1340 of file ParserNode.h.

References coldef, and coldefs.

1340  : table(tab) {
1341  for (const auto coldef : *coldefs) {
1342  this->coldefs.emplace_back(coldef);
1343  }
1344  delete coldefs;
1345  }
std::list< std::unique_ptr< ColumnDef > > coldefs
Definition: ParserNode.h:1354
std::unique_ptr< std::string > table
Definition: ParserNode.h:1352
std::unique_ptr< ColumnDef > coldef
Definition: ParserNode.h:1353

Member Function Documentation

void Parser::AddColumnStmt::check_executable ( const Catalog_Namespace::SessionInfo session,
const TableDescriptor td 
)

Definition at line 4091 of file ParserNode.cpp.

References Parser::check_alter_table_privilege(), Catalog_Namespace::SessionInfo::getCatalog(), TableDescriptor::isView, test_readcsv::table, ddl_utils::TABLE, table_is_temporary(), TableDescriptor::tableId, and ddl_utils::validate_table_type().

4092  {
4093  auto& catalog = session.getCatalog();
4094  if (!td) {
4095  throw std::runtime_error("Table " + *table + " does not exist.");
4096  } else {
4097  if (td->isView) {
4098  throw std::runtime_error("Adding columns to a view is not supported.");
4099  }
4101  if (table_is_temporary(td)) {
4102  throw std::runtime_error(
4103  "Adding columns to temporary tables is not yet supported.");
4104  }
4105  };
4106 
4107  check_alter_table_privilege(session, td);
4108 
4109  if (0 == coldefs.size()) {
4110  coldefs.push_back(std::move(coldef));
4111  }
4112 
4113  for (const auto& coldef : coldefs) {
4114  auto& new_column_name = *coldef->get_column_name();
4115  if (catalog.getMetadataForColumn(td->tableId, new_column_name) != nullptr) {
4116  throw std::runtime_error("Column " + new_column_name + " already exists.");
4117  }
4118  }
4119 }
std::list< std::unique_ptr< ColumnDef > > coldefs
Definition: ParserNode.h:1354
std::unique_ptr< std::string > table
Definition: ParserNode.h:1352
void check_alter_table_privilege(const Catalog_Namespace::SessionInfo &session, const TableDescriptor *td)
bool table_is_temporary(const TableDescriptor *const td)
Catalog & getCatalog() const
Definition: SessionInfo.h:67
void validate_table_type(const TableDescriptor *td, const TableType expected_table_type, const std::string &command)
Definition: DdlUtils.cpp:554
std::unique_ptr< ColumnDef > coldef
Definition: ParserNode.h:1353

+ Here is the call graph for this function:

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

Implements Parser::DDLStmt.

Definition at line 4121 of file ParserNode.cpp.

References CHECK, ColumnDescriptor::columnId, ColumnDescriptor::columnName, ColumnDescriptor::columnType, legacylockmgr::ExecutorOuterLock, TableDescriptor::fragmenter, Parser::StringLiteral::get_stringval(), Catalog_Namespace::SessionInfo::getCatalog(), Geospatial::GeoTypesFactory::getGeoColumns(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), SQLTypeInfo::is_geometry(), TableDescriptor::nShards, import_export::Importer::set_geo_physical_import_buffer(), gpu_enabled::sort(), test_readcsv::table, and TableDescriptor::tableId.

Referenced by Parser::AlterTableStmt::delegateExecute(), and omnisci.cursor.Cursor::executemany().

4121  {
4122  auto& catalog = session.getCatalog();
4123 
4124  // TODO(adb): the catalog should be handling this locking.
4125  const auto execute_write_lock = mapd_unique_lock<mapd_shared_mutex>(
4128 
4129  const auto td_with_lock =
4131  catalog, *table, true);
4132  const auto td = td_with_lock();
4133 
4134  check_executable(session, td);
4135 
4136  CHECK(td->fragmenter);
4137  if (std::dynamic_pointer_cast<Fragmenter_Namespace::SortedOrderFragmenter>(
4138  td->fragmenter)) {
4139  throw std::runtime_error(
4140  "Adding columns to a table is not supported when using the \"sort_column\" "
4141  "option.");
4142  }
4143 
4144  // Do not take a data write lock, as the fragmenter may call `deleteFragments`
4145  // during a cap operation. Note that the schema write lock will prevent concurrent
4146  // inserts along with all other queries.
4147 
4148  catalog.getSqliteConnector().query("BEGIN TRANSACTION");
4149  try {
4150  std::map<const std::string, const ColumnDescriptor> cds;
4151  std::map<const int, const ColumnDef*> cid_coldefs;
4152  for (const auto& coldef : coldefs) {
4153  ColumnDescriptor cd;
4154  setColumnDescriptor(cd, coldef.get());
4155  catalog.addColumn(*td, cd);
4156  cds.emplace(*coldef->get_column_name(), cd);
4157  cid_coldefs.emplace(cd.columnId, coldef.get());
4158 
4159  // expand geo column to phy columns
4160  if (cd.columnType.is_geometry()) {
4161  std::list<ColumnDescriptor> phy_geo_columns;
4162  catalog.expandGeoColumn(cd, phy_geo_columns);
4163  for (auto& cd : phy_geo_columns) {
4164  catalog.addColumn(*td, cd);
4165  cds.emplace(cd.columnName, cd);
4166  cid_coldefs.emplace(cd.columnId, nullptr);
4167  }
4168  }
4169  }
4170 
4171  std::unique_ptr<import_export::Loader> loader(new import_export::Loader(catalog, td));
4172  std::vector<std::unique_ptr<import_export::TypedImportBuffer>> import_buffers;
4173  for (const auto& cd : cds) {
4174  import_buffers.emplace_back(std::make_unique<import_export::TypedImportBuffer>(
4175  &cd.second, loader->getStringDict(&cd.second)));
4176  }
4177  loader->setAddingColumns(true);
4178 
4179  // set_geo_physical_import_buffer below needs a sorted import_buffers
4180  std::sort(import_buffers.begin(),
4181  import_buffers.end(),
4182  [](decltype(import_buffers[0])& a, decltype(import_buffers[0])& b) {
4183  return a->getColumnDesc()->columnId < b->getColumnDesc()->columnId;
4184  });
4185 
4186  size_t nrows = td->fragmenter->getNumRows();
4187  // if sharded, get total nrows from all sharded tables
4188  if (td->nShards > 0) {
4189  const auto physical_tds = catalog.getPhysicalTablesDescriptors(td);
4190  nrows = 0;
4191  std::for_each(physical_tds.begin(), physical_tds.end(), [&nrows](const auto& td) {
4192  nrows += td->fragmenter->getNumRows();
4193  });
4194  }
4195  if (nrows > 0) {
4196  int skip_physical_cols = 0;
4197  for (const auto cit : cid_coldefs) {
4198  const auto cd = catalog.getMetadataForColumn(td->tableId, cit.first);
4199  const auto coldef = cit.second;
4200  const auto column_constraint = coldef ? coldef->get_column_constraint() : nullptr;
4201  std::string defaultval = "";
4202  if (column_constraint) {
4203  auto defaultlp = column_constraint->get_defaultval();
4204  auto defaultsp = dynamic_cast<const StringLiteral*>(defaultlp);
4205  defaultval = defaultsp ? *defaultsp->get_stringval()
4206  : defaultlp ? defaultlp->to_string() : "";
4207  }
4208  bool isnull = column_constraint ? (0 == defaultval.size()) : true;
4209  if (boost::to_upper_copy<std::string>(defaultval) == "NULL") {
4210  isnull = true;
4211  }
4212 
4213  if (isnull) {
4214  if (column_constraint && column_constraint->get_notnull()) {
4215  throw std::runtime_error("Default value required for column " +
4216  cd->columnName + " (NULL value not supported)");
4217  }
4218  }
4219 
4220  for (auto it = import_buffers.begin(); it < import_buffers.end(); ++it) {
4221  auto& import_buffer = *it;
4222  if (cd->columnId == import_buffer->getColumnDesc()->columnId) {
4223  if (coldef != nullptr ||
4224  skip_physical_cols-- <= 0) { // skip non-null phy col
4225  import_buffer->add_value(
4226  cd, defaultval, isnull, import_export::CopyParams());
4227  if (cd->columnType.is_geometry()) {
4228  std::vector<double> coords, bounds;
4229  std::vector<int> ring_sizes, poly_rings;
4230  int render_group = 0;
4231  SQLTypeInfo tinfo{cd->columnType};
4233  tinfo,
4234  coords,
4235  bounds,
4236  ring_sizes,
4237  poly_rings,
4238  false)) {
4239  throw std::runtime_error("Bad geometry data: '" + defaultval + "'");
4240  }
4241  size_t col_idx = 1 + std::distance(import_buffers.begin(), it);
4243  cd,
4244  import_buffers,
4245  col_idx,
4246  coords,
4247  bounds,
4248  ring_sizes,
4249  poly_rings,
4250  render_group);
4251  // skip following phy cols
4252  skip_physical_cols = cd->columnType.get_physical_cols();
4253  }
4254  }
4255  break;
4256  }
4257  }
4258  }
4259  }
4260 
4261  if (!loader->loadNoCheckpoint(import_buffers, nrows, &session)) {
4262  throw std::runtime_error("loadNoCheckpoint failed!");
4263  }
4264  catalog.roll(true);
4265  loader->checkpoint();
4266  catalog.getSqliteConnector().query("END TRANSACTION");
4267  } catch (...) {
4268  catalog.roll(false);
4269  catalog.getSqliteConnector().query("ROLLBACK TRANSACTION");
4270  throw;
4271  }
4272 }
std::list< std::unique_ptr< ColumnDef > > coldefs
Definition: ParserNode.h:1354
void setColumnDescriptor(ColumnDescriptor &cd, const ColumnDef *coldef)
std::unique_ptr< std::string > table
Definition: ParserNode.h:1352
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
int get_physical_cols() const
Definition: sqltypes.h:335
static void set_geo_physical_import_buffer(const Catalog_Namespace::Catalog &catalog, const ColumnDescriptor *cd, std::vector< std::unique_ptr< TypedImportBuffer >> &import_buffers, size_t &col_idx, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, int render_group)
Definition: Importer.cpp:1460
specifies the content in-memory of a row in the column metadata table
void check_executable(const Catalog_Namespace::SessionInfo &session, const TableDescriptor *td)
static bool getGeoColumns(const std::string &wkt_or_wkb_hex, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool promote_poly_to_mpoly=false)
Definition: Types.cpp:937
Catalog & getCatalog() const
Definition: SessionInfo.h:67
static std::shared_ptr< MutexType > getMutex(const LockType lockType, const KeyType &key)
Definition: LegacyLockMgr.h:51
std::unique_ptr< ColumnDef > coldef
Definition: ParserNode.h:1353
#define CHECK(condition)
Definition: Logger.h:206
bool is_geometry() const
Definition: sqltypes.h:501
SQLTypeInfo columnType
std::string columnName

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1349 of file ParserNode.h.

References table.

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

Member Data Documentation

std::unique_ptr<ColumnDef> Parser::AddColumnStmt::coldef
private

Definition at line 1353 of file ParserNode.h.

Referenced by AddColumnStmt().

std::list<std::unique_ptr<ColumnDef> > Parser::AddColumnStmt::coldefs
private

Definition at line 1354 of file ParserNode.h.

Referenced by AddColumnStmt().

std::unique_ptr<std::string> Parser::AddColumnStmt::table
private

Definition at line 1352 of file ParserNode.h.

Referenced by get_table().


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