OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parser::CreateTableAsSelectStmt Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::CreateTableAsSelectStmt:
+ Collaboration diagram for Parser::CreateTableAsSelectStmt:

Public Member Functions

 CreateTableAsSelectStmt (const rapidjson::Value &payload)
 
 CreateTableAsSelectStmt (const std::string *table_name, const std::string *select_query, const bool is_temporary, const bool if_not_exists, std::list< NameValueAssign * > *s)
 
void execute (const Catalog_Namespace::SessionInfo &session, bool read_only_mode) override
 
- Public Member Functions inherited from Parser::InsertIntoTableAsSelectStmt
 InsertIntoTableAsSelectStmt (const rapidjson::Value &payload)
 
 InsertIntoTableAsSelectStmt (const std::string *table_name, const std::string *select_query, std::list< std::string * > *c)
 
void populateData (QueryStateProxy, const TableDescriptor *td, bool validate_table, bool for_CTAS=false)
 
std::string & get_table ()
 
std::string & get_select_query ()
 
- 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

bool is_temporary_
 
bool if_not_exists_
 
std::list< std::unique_ptr
< NameValueAssign > > 
storage_options_
 

Additional Inherited Members

- Public Attributes inherited from Parser::InsertIntoTableAsSelectStmt
QueryConnectorleafs_connector_ = nullptr
 
- Protected Attributes inherited from Parser::InsertIntoTableAsSelectStmt
std::vector< std::unique_ptr
< std::string > > 
column_list_
 
std::string table_name_
 
std::string select_query_
 

Detailed Description

Definition at line 1168 of file ParserNode.h.

Constructor & Destructor Documentation

Parser::CreateTableAsSelectStmt::CreateTableAsSelectStmt ( const rapidjson::Value &  payload)

Definition at line 4186 of file ParserNode.cpp.

References if_not_exists_, is_temporary_, json_bool(), Parser::anonymous_namespace{ParserNode.cpp}::parse_options(), and storage_options_.

4187  : InsertIntoTableAsSelectStmt(payload) {
4188  if (payload.HasMember("temporary")) {
4189  is_temporary_ = json_bool(payload["temporary"]);
4190  } else {
4191  is_temporary_ = false;
4192  }
4193 
4194  if (payload.HasMember("ifNotExists")) {
4195  if_not_exists_ = json_bool(payload["ifNotExists"]);
4196  } else {
4197  if_not_exists_ = false;
4198  }
4199 
4200  parse_options(payload, storage_options_);
4201 }
const bool json_bool(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:49
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1193
void parse_options(const rapidjson::Value &payload, std::list< std::unique_ptr< NameValueAssign >> &nameValueList, bool stringToNull=false, bool stringToInteger=false)
InsertIntoTableAsSelectStmt(const rapidjson::Value &payload)

+ Here is the call graph for this function:

Parser::CreateTableAsSelectStmt::CreateTableAsSelectStmt ( const std::string *  table_name,
const std::string *  select_query,
const bool  is_temporary,
const bool  if_not_exists,
std::list< NameValueAssign * > *  s 
)
inline

Definition at line 1171 of file ParserNode.h.

References storage_options_.

1176  : InsertIntoTableAsSelectStmt(table_name, select_query, nullptr)
1177  , is_temporary_(is_temporary)
1178  , if_not_exists_(if_not_exists) {
1179  if (s) {
1180  for (const auto& e : *s) {
1181  storage_options_.emplace_back(e);
1182  }
1183  delete s;
1184  }
1185  }
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1193
InsertIntoTableAsSelectStmt(const rapidjson::Value &payload)

Member Function Documentation

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

Reimplemented from Parser::InsertIntoTableAsSelectStmt.

Definition at line 4203 of file ParserNode.cpp.

References Parser::anonymous_namespace{ParserNode.cpp}::acquire_query_table_locks(), CHECK, Catalog_Namespace::SessionInfo::checkDBAccessPrivileges(), Data_Namespace::CPU_LEVEL, query_state::QueryState::create(), bench_batch_streaming_ingest::create_table(), AccessPrivileges::CREATE_TABLE, Catalog_Namespace::Catalog::createTable(), DEFAULT_FRAGMENT_ROWS, DEFAULT_MAX_CHUNK_SIZE, DEFAULT_MAX_ROLLBACK_EPOCHS, DEFAULT_MAX_ROWS, DEFAULT_PAGE_SIZE, Data_Namespace::DISK_LEVEL, Catalog_Namespace::Catalog::dropTable(), legacylockmgr::ExecutorOuterLock, TableDescriptor::fragmenter, TableDescriptor::fragPageSize, TableDescriptor::fragType, g_cluster, Catalog_Namespace::SessionInfo::get_currentUser(), Parser::StringLiteral::get_stringval(), Parser::anonymous_namespace{ParserNode.cpp}::get_table_definitions_for_ctas(), Catalog_Namespace::SessionInfo::getCatalog(), Parser::LocalQueryConnector::getColumnDescriptors(), Catalog_Namespace::Catalog::getDictionaryToColumnMapping(), Catalog_Namespace::Catalog::getMetadataForTable(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), if_not_exists_, logger::INFO, Fragmenter_Namespace::INSERT_ORDER, is_temporary_, TableDescriptor::isView, kENCODING_DICT, TableDescriptor::keyMetainfo, Parser::InsertIntoTableAsSelectStmt::leafs_connector_, LOG, TableDescriptor::maxChunkSize, TableDescriptor::maxFragRows, TableDescriptor::maxRollbackEpochs, TableDescriptor::maxRows, TableDescriptor::nColumns, TableDescriptor::persistenceLevel, Parser::InsertIntoTableAsSelectStmt::populateData(), Parser::LocalQueryConnector::query(), Parser::InsertIntoTableAsSelectStmt::select_query_, Parser::anonymous_namespace{ParserNode.cpp}::serialize_key_metainfo(), STDLOG, storage_options_, Parser::InsertIntoTableAsSelectStmt::table_name_, TableDBObjectType, TableDescriptor::tableName, TableDescriptor::userId, and Catalog_Namespace::UserMetadata::userId.

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

4204  {
4205  if (read_only_mode) {
4206  throw std::runtime_error("CREATE TABLE invalid in read only mode.");
4207  }
4208  auto session_copy = session;
4209  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
4210  &session_copy, boost::null_deleter());
4211  auto query_state = query_state::QueryState::create(session_ptr, select_query_);
4212  auto stdlog = STDLOG(query_state);
4213  LocalQueryConnector local_connector;
4214  auto& catalog = session.getCatalog();
4215  bool create_table = nullptr == leafs_connector_;
4216 
4217  std::set<std::string> select_tables;
4218  if (create_table) {
4219  const auto execute_write_lock =
4223 
4224  // check access privileges
4227  throw std::runtime_error("CTAS failed. Table " + table_name_ +
4228  " will not be created. User has no create privileges.");
4229  }
4230 
4231  if (catalog.getMetadataForTable(table_name_) != nullptr) {
4232  if (if_not_exists_) {
4233  return;
4234  }
4235  throw std::runtime_error("Table " + table_name_ +
4236  " already exists and no data was loaded.");
4237  }
4238 
4239  // only validate the select query so we get the target types
4240  // correctly, but do not populate the result set
4241  // we currently have exclusive access to the system so this is safe
4242  auto validate_result = local_connector.query(
4243  query_state->createQueryStateProxy(), select_query_, {}, true, false);
4244 
4245  const auto column_descriptors_for_create =
4246  local_connector.getColumnDescriptors(validate_result, true);
4247 
4248  // some validation as the QE might return some out of range column types
4249  for (auto& cd : column_descriptors_for_create) {
4250  if (cd.columnType.is_decimal() && cd.columnType.get_precision() > 18) {
4251  throw std::runtime_error(cd.columnName + ": Precision too high, max 18.");
4252  }
4253  }
4254 
4255  TableDescriptor td;
4256  td.tableName = table_name_;
4257  td.userId = session.get_currentUser().userId;
4258  td.nColumns = column_descriptors_for_create.size();
4259  td.isView = false;
4260  td.fragmenter = nullptr;
4267  if (is_temporary_) {
4269  } else {
4271  }
4272 
4273  bool use_shared_dictionaries = true;
4274 
4275  if (!storage_options_.empty()) {
4276  for (auto& p : storage_options_) {
4277  if (boost::to_lower_copy<std::string>(*p->get_name()) ==
4278  "use_shared_dictionaries") {
4279  const StringLiteral* literal =
4280  dynamic_cast<const StringLiteral*>(p->get_value());
4281  if (nullptr == literal) {
4282  throw std::runtime_error(
4283  "USE_SHARED_DICTIONARIES must be a string parameter");
4284  }
4285  std::string val = boost::to_lower_copy<std::string>(*literal->get_stringval());
4286  use_shared_dictionaries = val == "true" || val == "1" || val == "t";
4287  } else {
4288  get_table_definitions_for_ctas(td, p, column_descriptors_for_create);
4289  }
4290  }
4291  }
4292 
4293  std::vector<SharedDictionaryDef> sharedDictionaryRefs;
4294 
4295  if (use_shared_dictionaries) {
4296  const auto source_column_descriptors =
4297  local_connector.getColumnDescriptors(validate_result, false);
4298  const auto mapping = catalog.getDictionaryToColumnMapping();
4299 
4300  for (auto& source_cd : source_column_descriptors) {
4301  const auto& ti = source_cd.columnType;
4302  if (ti.is_string()) {
4303  if (ti.get_compression() == kENCODING_DICT) {
4304  int dict_id = ti.get_comp_param();
4305  auto it = mapping.find(dict_id);
4306  if (mapping.end() != it) {
4307  const auto targetColumn = it->second;
4308  auto targetTable =
4309  catalog.getMetadataForTable(targetColumn->tableId, false);
4310  CHECK(targetTable);
4311  LOG(INFO) << "CTAS: sharing text dictionary on column "
4312  << source_cd.columnName << " with " << targetTable->tableName
4313  << "." << targetColumn->columnName;
4314  sharedDictionaryRefs.emplace_back(
4315  source_cd.columnName, targetTable->tableName, targetColumn->columnName);
4316  }
4317  }
4318  }
4319  }
4320  }
4321 
4322  // currently no means of defining sharding in CTAS
4323  td.keyMetainfo = serialize_key_metainfo(nullptr, sharedDictionaryRefs);
4324 
4325  catalog.createTable(td, column_descriptors_for_create, sharedDictionaryRefs, true);
4326  // TODO (max): It's transactionally unsafe, should be fixed: we may create object
4327  // w/o privileges
4328  SysCatalog::instance().createDBObject(
4329  session.get_currentUser(), td.tableName, TableDBObjectType, catalog);
4330  }
4331 
4332  // note there is a time where we do not have any executor outer lock here. someone could
4333  // come along and mess with the data or other tables.
4334  const auto execute_read_lock =
4338 
4339  auto locks = acquire_query_table_locks(
4340  catalog, select_query_, query_state->createQueryStateProxy(), table_name_);
4341  const TableDescriptor* td = catalog.getMetadataForTable(table_name_);
4342  try {
4343  populateData(query_state->createQueryStateProxy(), td, false, true);
4344  } catch (...) {
4345  if (!g_cluster) {
4346  const TableDescriptor* created_td = catalog.getMetadataForTable(table_name_);
4347  if (created_td) {
4348  catalog.dropTable(created_td);
4349  }
4350  }
4351  throw;
4352  }
4353 }
static std::shared_ptr< WrapperType< MutexType > > getMutex(const LockType lockType, const KeyType &key)
int32_t maxRollbackEpochs
std::string tableName
#define LOG(tag)
Definition: Logger.h:216
void populateData(QueryStateProxy, const TableDescriptor *td, bool validate_table, bool for_CTAS=false)
static std::shared_ptr< QueryState > create(ARGS &&...args)
Definition: QueryState.h:145
#define DEFAULT_MAX_CHUNK_SIZE
void get_table_definitions_for_ctas(TableDescriptor &td, const std::unique_ptr< NameValueAssign > &p, const std::list< ColumnDescriptor > &columns)
std::shared_lock< T > shared_lock
#define DEFAULT_MAX_ROWS
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1193
lockmgr::LockedTableDescriptors acquire_query_table_locks(Catalog_Namespace::Catalog &catalog, const std::string &query_str, const QueryStateProxy &query_state_proxy, const std::optional< std::string > &insert_table_name={})
std::unique_lock< T > unique_lock
#define DEFAULT_MAX_ROLLBACK_EPOCHS
specifies the content in-memory of a row in the table metadata table
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:158
std::string keyMetainfo
std::shared_ptr< Fragmenter_Namespace::AbstractFragmenter > fragmenter
#define DEFAULT_PAGE_SIZE
Catalog & getCatalog() const
Definition: SessionInfo.h:65
#define DEFAULT_FRAGMENT_ROWS
Fragmenter_Namespace::FragmenterType fragType
Data_Namespace::MemoryLevel persistenceLevel
#define CHECK(condition)
Definition: Logger.h:222
bool g_cluster
bool checkDBAccessPrivileges(const DBObjectType &permissionType, const AccessPrivileges &privs, const std::string &objectName="") const
Definition: SessionInfo.cpp:24
const UserMetadata & get_currentUser() const
Definition: SessionInfo.h:71
#define STDLOG(...)
Definition: QueryState.h:235
std::string serialize_key_metainfo(const ShardKeyDef *shard_key_def, const std::vector< SharedDictionaryDef > &shared_dict_defs)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

bool Parser::CreateTableAsSelectStmt::if_not_exists_
private

Definition at line 1192 of file ParserNode.h.

Referenced by CreateTableAsSelectStmt(), and execute().

bool Parser::CreateTableAsSelectStmt::is_temporary_
private

Definition at line 1191 of file ParserNode.h.

Referenced by CreateTableAsSelectStmt(), and execute().

std::list<std::unique_ptr<NameValueAssign> > Parser::CreateTableAsSelectStmt::storage_options_
private

Definition at line 1193 of file ParserNode.h.

Referenced by CreateTableAsSelectStmt(), and execute().


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