OmniSciDB  4201147b46
 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 4943 of file ParserNode.cpp.

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

Referenced by execute().

4944  {
4945  auto& catalog = session.getCatalog();
4946  if (!td) {
4947  throw std::runtime_error("Table " + *table_ + " does not exist.");
4948  } else {
4949  if (td->isView) {
4950  throw std::runtime_error("Adding columns to a view is not supported.");
4951  }
4953  if (table_is_temporary(td)) {
4954  throw std::runtime_error(
4955  "Adding columns to temporary tables is not yet supported.");
4956  }
4957  }
4958 
4959  check_alter_table_privilege(session, td);
4960 
4961  if (0 == coldefs_.size()) {
4962  coldefs_.push_back(std::move(coldef_));
4963  }
4964 
4965  for (const auto& coldef : coldefs_) {
4966  auto& new_column_name = *coldef->get_column_name();
4967  if (catalog.getMetadataForColumn(td->tableId, new_column_name) != nullptr) {
4968  throw std::runtime_error("Column " + new_column_name + " already exists.");
4969  }
4970  }
4971 }
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:65
void validate_table_type(const TableDescriptor *td, const TableType expected_table_type, const std::string &command)
Definition: DdlUtils.cpp:693
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 4973 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().

4974  {
4975  if (read_only_mode) {
4976  throw std::runtime_error("ADD COLUMN invalid in read only mode.");
4977  }
4978  // TODO: Review add and drop column implementation
4979  const auto execute_write_lock = heavyai::unique_lock<heavyai::shared_mutex>(
4982  auto& catalog = session.getCatalog();
4983  const auto td_with_lock =
4985  catalog, *table_, true);
4986  const auto td = td_with_lock();
4987 
4988  check_executable(session, td);
4989 
4990  CHECK(td->fragmenter);
4991  if (std::dynamic_pointer_cast<Fragmenter_Namespace::SortedOrderFragmenter>(
4992  td->fragmenter)) {
4993  throw std::runtime_error(
4994  "Adding columns to a table is not supported when using the \"sort_column\" "
4995  "option.");
4996  }
4997 
4998  // invalidate cached item
4999  std::vector<int> table_key{catalog.getCurrentDB().dbId, td->tableId};
5000  ResultSetCacheInvalidator::invalidateCachesByTable(boost::hash_value(table_key));
5001 
5002  // Do not take a data write lock, as the fragmenter may call `deleteFragments`
5003  // during a cap operation. Note that the schema write lock will prevent concurrent
5004  // inserts along with all other queries.
5005 
5006  catalog.getSqliteConnector().query("BEGIN TRANSACTION");
5007  try {
5008  std::map<const std::string, const ColumnDescriptor> cds;
5009  std::map<const int, const ColumnDef*> cid_coldefs;
5010  for (const auto& coldef : coldefs_) {
5011  ColumnDescriptor cd;
5012  setColumnDescriptor(cd, coldef.get());
5013  catalog.addColumn(*td, cd);
5014  cds.emplace(*coldef->get_column_name(), cd);
5015  cid_coldefs.emplace(cd.columnId, coldef.get());
5016 
5017  // expand geo column to phy columns
5018  if (cd.columnType.is_geometry()) {
5019  std::list<ColumnDescriptor> phy_geo_columns;
5020  catalog.expandGeoColumn(cd, phy_geo_columns);
5021  for (auto& cd : phy_geo_columns) {
5022  catalog.addColumn(*td, cd);
5023  cds.emplace(cd.columnName, cd);
5024  cid_coldefs.emplace(cd.columnId, nullptr);
5025  }
5026  }
5027  }
5028 
5029  std::unique_ptr<import_export::Loader> loader(new import_export::Loader(catalog, td));
5030  std::vector<std::unique_ptr<import_export::TypedImportBuffer>> import_buffers;
5031  for (const auto& cd : cds) {
5032  import_buffers.emplace_back(std::make_unique<import_export::TypedImportBuffer>(
5033  &cd.second, loader->getStringDict(&cd.second)));
5034  }
5035  loader->setAddingColumns(true);
5036 
5037  // set_geo_physical_import_buffer below needs a sorted import_buffers
5038  std::sort(import_buffers.begin(),
5039  import_buffers.end(),
5040  [](decltype(import_buffers[0])& a, decltype(import_buffers[0])& b) {
5041  return a->getColumnDesc()->columnId < b->getColumnDesc()->columnId;
5042  });
5043 
5044  size_t nrows = td->fragmenter->getNumRows();
5045  // if sharded, get total nrows from all sharded tables
5046  if (td->nShards > 0) {
5047  const auto physical_tds = catalog.getPhysicalTablesDescriptors(td);
5048  nrows = 0;
5049  std::for_each(physical_tds.begin(), physical_tds.end(), [&nrows](const auto& td) {
5050  nrows += td->fragmenter->getNumRows();
5051  });
5052  }
5053  if (nrows > 0) {
5054  int skip_physical_cols = 0;
5055  for (const auto cit : cid_coldefs) {
5056  const auto cd = catalog.getMetadataForColumn(td->tableId, cit.first);
5057  const auto coldef = cit.second;
5058  const bool is_null = !cd->default_value.has_value();
5059 
5060  if (cd->columnType.get_notnull() && is_null) {
5061  throw std::runtime_error("Default value required for column " + cd->columnName +
5062  " because of NOT NULL constraint");
5063  }
5064 
5065  for (auto it = import_buffers.begin(); it < import_buffers.end(); ++it) {
5066  auto& import_buffer = *it;
5067  if (cd->columnId == import_buffer->getColumnDesc()->columnId) {
5068  if (coldef != nullptr ||
5069  skip_physical_cols-- <= 0) { // skip non-null phy col
5070  import_buffer->add_value(cd,
5071  cd->default_value.value_or("NULL"),
5072  is_null,
5074  if (cd->columnType.is_geometry()) {
5075  std::vector<double> coords, bounds;
5076  std::vector<int> ring_sizes, poly_rings;
5077  int render_group = 0;
5078  SQLTypeInfo tinfo{cd->columnType};
5080  cd->default_value.value_or("NULL"),
5081  tinfo,
5082  coords,
5083  bounds,
5084  ring_sizes,
5085  poly_rings,
5086  false)) {
5087  throw std::runtime_error("Bad geometry data: '" +
5088  cd->default_value.value_or("NULL") + "'");
5089  }
5090  size_t col_idx = 1 + std::distance(import_buffers.begin(), it);
5092  cd,
5093  import_buffers,
5094  col_idx,
5095  coords,
5096  bounds,
5097  ring_sizes,
5098  poly_rings,
5099  render_group);
5100  // skip following phy cols
5101  skip_physical_cols = cd->columnType.get_physical_cols();
5102  }
5103  }
5104  break;
5105  }
5106  }
5107  }
5108  }
5109 
5110  if (!loader->loadNoCheckpoint(import_buffers, nrows, &session)) {
5111  throw std::runtime_error("loadNoCheckpoint failed!");
5112  }
5113  catalog.roll(true);
5114  catalog.resetTableEpochFloor(td->tableId);
5115  loader->checkpoint();
5116  catalog.getSqliteConnector().query("END TRANSACTION");
5117  } catch (...) {
5118  catalog.roll(false);
5119  catalog.getSqliteConnector().query("ROLLBACK TRANSACTION");
5120  throw;
5121  }
5122 }
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:360
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:937
std::optional< std::string > default_value
Catalog & getCatalog() const
Definition: SessionInfo.h:65
static std::shared_ptr< MutexType > getMutex(const LockType lockType, const KeyType &key)
Definition: LegacyLockMgr.h:51
#define CHECK(condition)
Definition: Logger.h:222
bool is_geometry() const
Definition: sqltypes.h:522
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:1654
SQLTypeInfo columnType
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
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: