OmniSciDB  c1a53651b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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, bool read_only_mode) 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 1371 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 1373 of file ParserNode.h.

1373 : table_(tab), coldef_(coldef) {}
std::unique_ptr< std::string > table_
Definition: ParserNode.h:1387
std::unique_ptr< ColumnDef > coldef_
Definition: ParserNode.h:1388
Parser::AddColumnStmt::AddColumnStmt ( std::string *  tab,
std::list< ColumnDef * > *  coldefs 
)
inline

Definition at line 1374 of file ParserNode.h.

References coldefs_.

1374  : table_(tab) {
1375  for (const auto coldef : *coldefs) {
1376  this->coldefs_.emplace_back(coldef);
1377  }
1378  delete coldefs;
1379  }
std::unique_ptr< std::string > table_
Definition: ParserNode.h:1387
std::list< std::unique_ptr< ColumnDef > > coldefs_
Definition: ParserNode.h:1389

Member Function Documentation

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

Definition at line 5032 of file ParserNode.cpp.

References Parser::check_alter_table_privilege(), coldef_, coldefs_, Catalog_Namespace::SessionInfo::getCatalog(), TableDescriptor::isView, ddl_utils::TABLE, table_, table_is_temporary(), TableDescriptor::tableId, and ddl_utils::validate_table_type().

Referenced by execute().

5033  {
5034  auto& catalog = session.getCatalog();
5035  if (!td) {
5036  throw std::runtime_error("Table " + *table_ + " does not exist.");
5037  } else {
5038  if (td->isView) {
5039  throw std::runtime_error("Adding columns to a view is not supported.");
5040  }
5042  if (table_is_temporary(td)) {
5043  throw std::runtime_error(
5044  "Adding columns to temporary tables is not yet supported.");
5045  }
5046  }
5047 
5048  check_alter_table_privilege(session, td);
5049 
5050  if (0 == coldefs_.size()) {
5051  coldefs_.push_back(std::move(coldef_));
5052  }
5053 
5054  for (const auto& coldef : coldefs_) {
5055  auto& new_column_name = *coldef->get_column_name();
5056  if (catalog.getMetadataForColumn(td->tableId, new_column_name) != nullptr) {
5057  throw std::runtime_error("Column " + new_column_name + " already exists.");
5058  }
5059  }
5060 }
std::unique_ptr< std::string > table_
Definition: ParserNode.h:1387
std::list< std::unique_ptr< ColumnDef > > coldefs_
Definition: ParserNode.h:1389
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:75
void validate_table_type(const TableDescriptor *td, const TableType expected_table_type, const std::string &command)
Definition: DdlUtils.cpp:708
std::unique_ptr< ColumnDef > coldef_
Definition: ParserNode.h:1388

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Implements Parser::DDLStmt.

Definition at line 5062 of file ParserNode.cpp.

References anonymous_namespace{Utm.h}::a, CHECK, check_executable(), coldefs_, ColumnDescriptor::columnId, ColumnDescriptor::columnName, ColumnDescriptor::columnType, legacylockmgr::ExecutorOuterLock, TableDescriptor::fragmenter, Catalog_Namespace::SessionInfo::getCatalog(), Geospatial::GeoTypesFactory::getGeoColumns(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), CacheInvalidator< CACHE_HOLDING_TYPES >::invalidateCachesByTable(), SQLTypeInfo::is_geometry(), is_null(), TableDescriptor::nShards, import_export::Importer::set_geo_physical_import_buffer(), Parser::DDLStmt::setColumnDescriptor(), gpu_enabled::sort(), table_, and TableDescriptor::tableId.

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

5063  {
5064  if (read_only_mode) {
5065  throw std::runtime_error("ADD COLUMN invalid in read only mode.");
5066  }
5067  // TODO: Review add and drop column implementation
5068  const auto execute_write_lock =
5072  auto& catalog = session.getCatalog();
5073  const auto td_with_lock =
5075  catalog, *table_, true);
5076  const auto td = td_with_lock();
5077 
5078  check_executable(session, td);
5079 
5080  CHECK(td->fragmenter);
5081  if (std::dynamic_pointer_cast<Fragmenter_Namespace::SortedOrderFragmenter>(
5082  td->fragmenter)) {
5083  throw std::runtime_error(
5084  "Adding columns to a table is not supported when using the \"sort_column\" "
5085  "option.");
5086  }
5087 
5088  // invalidate cached item
5089  std::vector<int> table_key{catalog.getCurrentDB().dbId, td->tableId};
5090  ResultSetCacheInvalidator::invalidateCachesByTable(boost::hash_value(table_key));
5091 
5092  // Do not take a data write lock, as the fragmenter may call `deleteFragments`
5093  // during a cap operation. Note that the schema write lock will prevent concurrent
5094  // inserts along with all other queries.
5095 
5096  catalog.getSqliteConnector().query("BEGIN TRANSACTION");
5097  try {
5098  std::map<const std::string, const ColumnDescriptor> cds;
5099  std::map<const int, const ColumnDef*> cid_coldefs;
5100  for (const auto& coldef : coldefs_) {
5101  ColumnDescriptor cd;
5102  setColumnDescriptor(cd, coldef.get());
5103  catalog.addColumn(*td, cd);
5104  cds.emplace(*coldef->get_column_name(), cd);
5105  cid_coldefs.emplace(cd.columnId, coldef.get());
5106 
5107  // expand geo column to phy columns
5108  if (cd.columnType.is_geometry()) {
5109  std::list<ColumnDescriptor> phy_geo_columns;
5110  catalog.expandGeoColumn(cd, phy_geo_columns);
5111  for (auto& cd : phy_geo_columns) {
5112  catalog.addColumn(*td, cd);
5113  cds.emplace(cd.columnName, cd);
5114  cid_coldefs.emplace(cd.columnId, nullptr);
5115  }
5116  }
5117  }
5118 
5119  std::unique_ptr<import_export::Loader> loader(new import_export::Loader(catalog, td));
5120  std::vector<std::unique_ptr<import_export::TypedImportBuffer>> import_buffers;
5121  for (const auto& cd : cds) {
5122  import_buffers.emplace_back(std::make_unique<import_export::TypedImportBuffer>(
5123  &cd.second, loader->getStringDict(&cd.second)));
5124  }
5125  loader->setAddingColumns(true);
5126 
5127  // set_geo_physical_import_buffer below needs a sorted import_buffers
5128  std::sort(import_buffers.begin(),
5129  import_buffers.end(),
5130  [](decltype(import_buffers[0])& a, decltype(import_buffers[0])& b) {
5131  return a->getColumnDesc()->columnId < b->getColumnDesc()->columnId;
5132  });
5133 
5134  size_t nrows = td->fragmenter->getNumRows();
5135  // if sharded, get total nrows from all sharded tables
5136  if (td->nShards > 0) {
5137  const auto physical_tds = catalog.getPhysicalTablesDescriptors(td);
5138  nrows = 0;
5139  std::for_each(physical_tds.begin(), physical_tds.end(), [&nrows](const auto& td) {
5140  nrows += td->fragmenter->getNumRows();
5141  });
5142  }
5143  if (nrows > 0) {
5144  int skip_physical_cols = 0;
5145  for (const auto cit : cid_coldefs) {
5146  const auto cd = catalog.getMetadataForColumn(td->tableId, cit.first);
5147  const auto coldef = cit.second;
5148  const bool is_null = !cd->default_value.has_value();
5149 
5150  if (cd->columnType.get_notnull() && is_null) {
5151  throw std::runtime_error("Default value required for column " + cd->columnName +
5152  " because of NOT NULL constraint");
5153  }
5154 
5155  for (auto it = import_buffers.begin(); it < import_buffers.end(); ++it) {
5156  auto& import_buffer = *it;
5157  if (cd->columnId == import_buffer->getColumnDesc()->columnId) {
5158  if (coldef != nullptr ||
5159  skip_physical_cols-- <= 0) { // skip non-null phy col
5160  import_buffer->add_value(cd,
5161  cd->default_value.value_or("NULL"),
5162  is_null,
5164  if (cd->columnType.is_geometry()) {
5165  std::vector<double> coords, bounds;
5166  std::vector<int> ring_sizes, poly_rings;
5167  int render_group = 0;
5168  SQLTypeInfo tinfo{cd->columnType};
5170  cd->default_value.value_or("NULL"),
5171  tinfo,
5172  coords,
5173  bounds,
5174  ring_sizes,
5175  poly_rings,
5176  false)) {
5177  throw std::runtime_error("Bad geometry data: '" +
5178  cd->default_value.value_or("NULL") + "'");
5179  }
5180  size_t col_idx = 1 + std::distance(import_buffers.begin(), it);
5182  cd,
5183  import_buffers,
5184  col_idx,
5185  coords,
5186  bounds,
5187  ring_sizes,
5188  poly_rings,
5189  render_group);
5190  // skip following phy cols
5191  skip_physical_cols = cd->columnType.get_physical_cols();
5192  }
5193  }
5194  break;
5195  }
5196  }
5197  }
5198  }
5199 
5200  if (!loader->loadNoCheckpoint(import_buffers, nrows, &session)) {
5201  throw std::runtime_error("loadNoCheckpoint failed!");
5202  }
5203  catalog.roll(true);
5204  catalog.resetTableEpochFloor(td->tableId);
5205  loader->checkpoint();
5206  catalog.getSqliteConnector().query("END TRANSACTION");
5207  } catch (...) {
5208  catalog.roll(false);
5209  catalog.getSqliteConnector().query("ROLLBACK TRANSACTION");
5210  throw;
5211  }
5212 }
static std::shared_ptr< WrapperType< MutexType > > getMutex(const LockType lockType, const KeyType &key)
static void invalidateCachesByTable(size_t table_key)
void setColumnDescriptor(ColumnDescriptor &cd, const ColumnDef *coldef)
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
constexpr double a
Definition: Utm.h:32
std::unique_ptr< std::string > table_
Definition: ParserNode.h:1387
int get_physical_cols() const
Definition: sqltypes.h:414
CONSTEXPR DEVICE bool is_null(const T &value)
std::list< std::unique_ptr< ColumnDef > > coldefs_
Definition: ParserNode.h:1389
std::unique_lock< T > unique_lock
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:1079
std::optional< std::string > default_value
Catalog & getCatalog() const
Definition: SessionInfo.h:75
#define CHECK(condition)
Definition: Logger.h:291
bool is_geometry() const
Definition: sqltypes.h:592
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, const bool force_null=false)
Definition: Importer.cpp:1627
SQLTypeInfo columnType
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:388
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 1384 of file ParserNode.h.

References table_.

1384 { return table_.get(); }
std::unique_ptr< std::string > table_
Definition: ParserNode.h:1387

Member Data Documentation

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

Definition at line 1388 of file ParserNode.h.

Referenced by check_executable().

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

Definition at line 1389 of file ParserNode.h.

Referenced by AddColumnStmt(), check_executable(), and execute().

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

Definition at line 1387 of file ParserNode.h.

Referenced by check_executable(), execute(), and get_table().


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