OmniSciDB  c0231cc57d
 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 4214 of file ParserNode.cpp.

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

4215  : InsertIntoTableAsSelectStmt(payload) {
4216  if (payload.HasMember("temporary")) {
4217  is_temporary_ = json_bool(payload["temporary"]);
4218  } else {
4219  is_temporary_ = false;
4220  }
4221 
4222  if (payload.HasMember("ifNotExists")) {
4223  if_not_exists_ = json_bool(payload["ifNotExists"]);
4224  } else {
4225  if_not_exists_ = false;
4226  }
4227 
4228  parse_options(payload, storage_options_);
4229 }
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 4231 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, 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().

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