OmniSciDB  c1a53651b2
 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 1168 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 4234 of file ParserNode.cpp.

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

4235  : InsertIntoTableAsSelectStmt(payload) {
4236  if (payload.HasMember("temporary")) {
4237  is_temporary_ = json_bool(payload["temporary"]);
4238  } else {
4239  is_temporary_ = false;
4240  }
4241 
4242  if (payload.HasMember("ifNotExists")) {
4243  if_not_exists_ = json_bool(payload["ifNotExists"]);
4244  } else {
4245  if_not_exists_ = false;
4246  }
4247 
4248  parse_options(payload, storage_options_);
4249 }
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 4251 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, 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(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), 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().

4252  {
4253  if (read_only_mode) {
4254  throw std::runtime_error("CREATE TABLE invalid in read only mode.");
4255  }
4256  auto session_copy = session;
4257  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
4258  &session_copy, boost::null_deleter());
4259  auto query_state = query_state::QueryState::create(session_ptr, select_query_);
4260  auto stdlog = STDLOG(query_state);
4261  LocalQueryConnector local_connector;
4262  auto& catalog = session.getCatalog();
4263  bool create_table = nullptr == leafs_connector_;
4264 
4265  std::set<std::string> select_tables;
4266  if (create_table) {
4267  const auto execute_write_lock =
4271 
4272  // check access privileges
4275  throw std::runtime_error("CTAS failed. Table " + table_name_ +
4276  " will not be created. User has no create privileges.");
4277  }
4278 
4279  if (catalog.getMetadataForTable(table_name_) != nullptr) {
4280  if (if_not_exists_) {
4281  return;
4282  }
4283  throw std::runtime_error("Table " + table_name_ +
4284  " already exists and no data was loaded.");
4285  }
4286 
4287  // only validate the select query so we get the target types
4288  // correctly, but do not populate the result set
4289  // we currently have exclusive access to the system so this is safe
4290  auto validate_result = local_connector.query(
4291  query_state->createQueryStateProxy(), select_query_, {}, true, false);
4292 
4293  auto column_descriptors_for_create =
4294  local_connector.getColumnDescriptors(validate_result, true);
4295 
4296  // some validation as the QE might return some out of range column types
4297  for (auto& cd : column_descriptors_for_create) {
4298  if (cd.columnType.is_decimal() &&
4299  cd.columnType.get_precision() > sql_constants::kMaxNumericPrecision) {
4300  throw std::runtime_error(cd.columnName + ": Precision too high, max " +
4302  ".");
4303  }
4304  }
4305 
4306  TableDescriptor td;
4307  td.tableName = table_name_;
4308  td.userId = session.get_currentUser().userId;
4309  td.nColumns = column_descriptors_for_create.size();
4310  td.isView = false;
4311  td.fragmenter = nullptr;
4318  if (is_temporary_) {
4320  } else {
4322  }
4323 
4324  bool use_shared_dictionaries = true;
4325  bool force_geo_compression = true;
4326 
4327  if (!storage_options_.empty()) {
4328  for (auto& p : storage_options_) {
4329  if (boost::to_lower_copy<std::string>(*p->get_name()) ==
4330  "use_shared_dictionaries") {
4331  const StringLiteral* literal =
4332  dynamic_cast<const StringLiteral*>(p->get_value());
4333  if (nullptr == literal) {
4334  throw std::runtime_error(
4335  "USE_SHARED_DICTIONARIES must be a string parameter");
4336  }
4337  std::string val = boost::to_lower_copy<std::string>(*literal->get_stringval());
4338  use_shared_dictionaries = val == "true" || val == "1" || val == "t";
4339  } else if (boost::to_lower_copy<std::string>(*p->get_name()) ==
4340  "force_geo_compression") {
4341  const StringLiteral* literal =
4342  dynamic_cast<const StringLiteral*>(p->get_value());
4343  if (nullptr == literal) {
4344  throw std::runtime_error("FORCE_GEO_COMPRESSION must be a string parameter");
4345  }
4346  std::string val = boost::to_lower_copy<std::string>(*literal->get_stringval());
4347  force_geo_compression = val == "true" || val == "1" || val == "t";
4348  } else {
4349  get_table_definitions_for_ctas(td, p, column_descriptors_for_create);
4350  }
4351  }
4352  }
4353 
4354  std::vector<SharedDictionaryDef> sharedDictionaryRefs;
4355 
4356  if (use_shared_dictionaries) {
4357  const auto source_column_descriptors =
4358  local_connector.getColumnDescriptors(validate_result, false);
4359  const auto mapping = catalog.getDictionaryToColumnMapping();
4360 
4361  for (auto& source_cd : source_column_descriptors) {
4362  const auto& ti = source_cd.columnType;
4363  if (ti.is_string()) {
4364  if (ti.get_compression() == kENCODING_DICT) {
4365  int dict_id = ti.get_comp_param();
4366  auto it = mapping.find(dict_id);
4367  if (mapping.end() != it) {
4368  const auto targetColumn = it->second;
4369  auto targetTable =
4370  catalog.getMetadataForTable(targetColumn->tableId, false);
4371  CHECK(targetTable);
4372  LOG(INFO) << "CTAS: sharing text dictionary on column "
4373  << source_cd.columnName << " with " << targetTable->tableName
4374  << "." << targetColumn->columnName;
4375  sharedDictionaryRefs.emplace_back(
4376  source_cd.columnName, targetTable->tableName, targetColumn->columnName);
4377  }
4378  }
4379  }
4380  }
4381  }
4382 
4383  if (force_geo_compression) {
4384  for (auto& cd_for_create : column_descriptors_for_create) {
4385  auto& ti = cd_for_create.columnType;
4386  if (ti.is_geometry() && ti.get_output_srid() == 4326) {
4387  // turn on GEOINT32 compression
4388  ti.set_compression(kENCODING_GEOINT);
4389  ti.set_comp_param(32);
4390  }
4391  }
4392  }
4393 
4394  // currently no means of defining sharding in CTAS
4395  td.keyMetainfo = serialize_key_metainfo(nullptr, sharedDictionaryRefs);
4396 
4397  catalog.createTable(td, column_descriptors_for_create, sharedDictionaryRefs, true);
4398  // TODO (max): It's transactionally unsafe, should be fixed: we may create object
4399  // w/o privileges
4400  SysCatalog::instance().createDBObject(
4401  session.get_currentUser(), td.tableName, TableDBObjectType, catalog);
4402  }
4403 
4404  // note there is a time where we do not have any executor outer lock here. someone could
4405  // come along and mess with the data or other tables.
4406  const auto execute_read_lock =
4410 
4411  auto locks = acquire_query_table_locks(
4412  catalog.name(), select_query_, query_state->createQueryStateProxy(), table_name_);
4413  const TableDescriptor* td = catalog.getMetadataForTable(table_name_);
4414  try {
4415  populateData(query_state->createQueryStateProxy(), td, false, true);
4416  } catch (...) {
4417  if (!g_cluster) {
4418  const TableDescriptor* created_td = catalog.getMetadataForTable(table_name_);
4419  if (created_td) {
4420  catalog.dropTable(created_td);
4421  }
4422  }
4423  throw;
4424  }
4425 }
static std::shared_ptr< WrapperType< MutexType > > getMutex(const LockType lockType, const KeyType &key)
int32_t maxRollbackEpochs
static constexpr int32_t kMaxNumericPrecision
Definition: sqltypes.h:48
std::string tableName
#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)
std::shared_lock< T > shared_lock
#define DEFAULT_MAX_ROWS
std::unique_ptr< QueryConnector > leafs_connector_
Definition: ParserNode.h:1156
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1193
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
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
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 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: