OmniSciDB  72c90bc290
 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
std::unique_ptr< QueryConnectorleafs_connector_
 
- 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 1169 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 4682 of file ParserNode.cpp.

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

4683  : InsertIntoTableAsSelectStmt(payload) {
4684  if (payload.HasMember("temporary")) {
4685  is_temporary_ = json_bool(payload["temporary"]);
4686  } else {
4687  is_temporary_ = false;
4688  }
4689 
4690  if (payload.HasMember("ifNotExists")) {
4691  if_not_exists_ = json_bool(payload["ifNotExists"]);
4692  } else {
4693  if_not_exists_ = false;
4694  }
4695 
4696  parse_options(payload, storage_options_);
4697 }
const bool json_bool(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:51
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1194
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 1172 of file ParserNode.h.

References storage_options_.

1177  : InsertIntoTableAsSelectStmt(table_name, select_query, nullptr)
1178  , is_temporary_(is_temporary)
1179  , if_not_exists_(if_not_exists) {
1180  if (s) {
1181  for (const auto& e : *s) {
1182  storage_options_.emplace_back(e);
1183  }
1184  delete s;
1185  }
1186  }
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1194
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 4699 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, DEFAULT_FRAGMENT_ROWS, DEFAULT_MAX_CHUNK_SIZE, DEFAULT_MAX_ROLLBACK_EPOCHS, DEFAULT_MAX_ROWS, DEFAULT_PAGE_SIZE, Data_Namespace::DISK_LEVEL, 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(), legacylockmgr::getExecuteReadLock(), legacylockmgr::getExecuteWriteLock(), if_not_exists_, logger::INFO, Fragmenter_Namespace::INSERT_ORDER, is_temporary_, TableDescriptor::isView, kENCODING_DICT, kENCODING_GEOINT, TableDescriptor::keyMetainfo, sql_constants::kMaxNumericPrecision, 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, to_string(), TableDescriptor::userId, and Catalog_Namespace::UserMetadata::userId.

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

4700  {
4701  if (read_only_mode) {
4702  throw std::runtime_error("CREATE TABLE invalid in read only mode.");
4703  }
4704  auto session_copy = session;
4705  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
4706  &session_copy, boost::null_deleter());
4707  auto query_state = query_state::QueryState::create(session_ptr, select_query_);
4708  auto stdlog = STDLOG(query_state);
4709  LocalQueryConnector local_connector;
4710  auto& catalog = session.getCatalog();
4711  bool create_table = nullptr == leafs_connector_;
4712 
4713  std::set<std::string> select_tables;
4714  if (create_table) {
4715  const auto execute_write_lock = legacylockmgr::getExecuteWriteLock();
4716 
4717  // check access privileges
4720  throw std::runtime_error("CTAS failed. Table " + table_name_ +
4721  " will not be created. User has no create privileges.");
4722  }
4723 
4724  if (catalog.getMetadataForTable(table_name_) != nullptr) {
4725  if (if_not_exists_) {
4726  return;
4727  }
4728  throw std::runtime_error("Table " + table_name_ +
4729  " already exists and no data was loaded.");
4730  }
4731 
4732  // only validate the select query so we get the target types
4733  // correctly, but do not populate the result set
4734  // we currently have exclusive access to the system so this is safe
4735  auto validate_result = local_connector.query(
4736  query_state->createQueryStateProxy(), select_query_, {}, true, false);
4737 
4738  auto column_descriptors_for_create =
4739  local_connector.getColumnDescriptors(validate_result, true);
4740 
4741  // some validation as the QE might return some out of range column types
4742  for (auto& cd : column_descriptors_for_create) {
4743  if (cd.columnType.is_decimal() &&
4744  cd.columnType.get_precision() > sql_constants::kMaxNumericPrecision) {
4745  throw std::runtime_error(cd.columnName + ": Precision too high, max " +
4747  ".");
4748  }
4749  }
4750 
4751  TableDescriptor td;
4752  td.tableName = table_name_;
4753  td.userId = session.get_currentUser().userId;
4754  td.nColumns = column_descriptors_for_create.size();
4755  td.isView = false;
4756  td.fragmenter = nullptr;
4763  if (is_temporary_) {
4765  } else {
4767  }
4768 
4769  bool use_shared_dictionaries = true;
4770  bool force_geo_compression = true;
4771 
4772  if (!storage_options_.empty()) {
4773  for (auto& p : storage_options_) {
4774  if (boost::to_lower_copy<std::string>(*p->get_name()) ==
4775  "use_shared_dictionaries") {
4776  const StringLiteral* literal =
4777  dynamic_cast<const StringLiteral*>(p->get_value());
4778  if (nullptr == literal) {
4779  throw std::runtime_error(
4780  "USE_SHARED_DICTIONARIES must be a string parameter");
4781  }
4782  std::string val = boost::to_lower_copy<std::string>(*literal->get_stringval());
4783  use_shared_dictionaries = val == "true" || val == "1" || val == "t";
4784  } else if (boost::to_lower_copy<std::string>(*p->get_name()) ==
4785  "force_geo_compression") {
4786  const StringLiteral* literal =
4787  dynamic_cast<const StringLiteral*>(p->get_value());
4788  if (nullptr == literal) {
4789  throw std::runtime_error("FORCE_GEO_COMPRESSION must be a string parameter");
4790  }
4791  std::string val = boost::to_lower_copy<std::string>(*literal->get_stringval());
4792  force_geo_compression = val == "true" || val == "1" || val == "t";
4793  } else {
4794  get_table_definitions_for_ctas(td, p, column_descriptors_for_create);
4795  }
4796  }
4797  }
4798 
4799  std::vector<SharedDictionaryDef> sharedDictionaryRefs;
4800 
4801  if (use_shared_dictionaries) {
4802  const auto source_column_descriptors =
4803  local_connector.getColumnDescriptors(validate_result, false);
4804  const auto mapping = catalog.getDictionaryToColumnMapping();
4805 
4806  for (auto& source_cd : source_column_descriptors) {
4807  const auto& ti = source_cd.columnType;
4808  if (ti.is_string()) {
4809  if (ti.get_compression() == kENCODING_DICT) {
4810  int dict_id = ti.get_comp_param();
4811  auto it = mapping.find(dict_id);
4812  if (mapping.end() != it) {
4813  const auto targetColumn = it->second;
4814  auto targetTable =
4815  catalog.getMetadataForTable(targetColumn->tableId, false);
4816  CHECK(targetTable);
4817  LOG(INFO) << "CTAS: sharing text dictionary on column "
4818  << source_cd.columnName << " with " << targetTable->tableName
4819  << "." << targetColumn->columnName;
4820  sharedDictionaryRefs.emplace_back(
4821  source_cd.columnName, targetTable->tableName, targetColumn->columnName);
4822  }
4823  }
4824  }
4825  }
4826  }
4827 
4828  if (force_geo_compression) {
4829  for (auto& cd_for_create : column_descriptors_for_create) {
4830  auto& ti = cd_for_create.columnType;
4831  if (ti.is_geometry() && ti.get_output_srid() == 4326) {
4832  // turn on GEOINT32 compression
4833  ti.set_compression(kENCODING_GEOINT);
4834  ti.set_comp_param(32);
4835  }
4836  }
4837  }
4838 
4839  // currently no means of defining sharding in CTAS
4840  td.keyMetainfo = serialize_key_metainfo(nullptr, sharedDictionaryRefs);
4841 
4842  catalog.createTable(td, column_descriptors_for_create, sharedDictionaryRefs, true);
4843  // TODO (max): It's transactionally unsafe, should be fixed: we may create object
4844  // w/o privileges
4845  SysCatalog::instance().createDBObject(
4846  session.get_currentUser(), td.tableName, TableDBObjectType, catalog);
4847  }
4848 
4849  // note there is a time where we do not have any executor outer lock here. someone could
4850  // come along and mess with the data or other tables.
4851  const auto execute_read_lock = legacylockmgr::getExecuteReadLock();
4852 
4853  auto locks = acquire_query_table_locks(
4854  catalog.name(), select_query_, query_state->createQueryStateProxy(), table_name_);
4855  const TableDescriptor* td = catalog.getMetadataForTable(table_name_);
4856  try {
4857  populateData(query_state->createQueryStateProxy(), td, false, true);
4858  } catch (...) {
4859  if (!g_cluster) {
4860  const TableDescriptor* created_td = catalog.getMetadataForTable(table_name_);
4861  if (created_td) {
4862  catalog.dropTable(created_td);
4863  }
4864  }
4865  throw;
4866  }
4867 }
int32_t maxRollbackEpochs
static constexpr int32_t kMaxNumericPrecision
Definition: sqltypes.h:58
std::string tableName
auto getExecuteReadLock()
#define LOG(tag)
Definition: Logger.h:285
void populateData(QueryStateProxy, const TableDescriptor *td, bool validate_table, bool for_CTAS=false)
static std::shared_ptr< QueryState > create(ARGS &&...args)
Definition: QueryState.h:148
#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::string to_string(char const *&&v)
#define DEFAULT_MAX_ROWS
std::unique_ptr< QueryConnector > leafs_connector_
Definition: ParserNode.h:1157
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1194
#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
lockmgr::LockedTableDescriptors acquire_query_table_locks(const std::string &insert_table_db_name, const std::string &query_str, const QueryStateProxy &query_state_proxy, const std::optional< std::string > &insert_table_name={})
std::string keyMetainfo
std::shared_ptr< Fragmenter_Namespace::AbstractFragmenter > fragmenter
#define DEFAULT_PAGE_SIZE
Catalog & getCatalog() const
Definition: SessionInfo.h:75
#define DEFAULT_FRAGMENT_ROWS
Fragmenter_Namespace::FragmenterType fragType
Data_Namespace::MemoryLevel persistenceLevel
#define CHECK(condition)
Definition: Logger.h:291
bool g_cluster
auto getExecuteWriteLock()
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:88
#define STDLOG(...)
Definition: QueryState.h:234
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 1193 of file ParserNode.h.

Referenced by CreateTableAsSelectStmt(), and execute().

bool Parser::CreateTableAsSelectStmt::is_temporary_
private

Definition at line 1192 of file ParserNode.h.

Referenced by CreateTableAsSelectStmt(), and execute().

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

Definition at line 1194 of file ParserNode.h.

Referenced by CreateTableAsSelectStmt(), and execute().


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