OmniSciDB  2e3a973ef4
Parser::CreateTableAsSelectStmt Class Reference

#include <ParserNode.h>

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

Public Member Functions

 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) override
 
- Public Member Functions inherited from Parser::InsertIntoTableAsSelectStmt
 InsertIntoTableAsSelectStmt (const std::string *table_name, const std::string *select_query, std::list< std::string *> *c)
 
void populateData (QueryStateProxy, bool validate_table)
 
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

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

Additional Inherited Members

- Public Attributes inherited from Parser::InsertIntoTableAsSelectStmt
DistributedConnectorleafs_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 1109 of file ParserNode.h.

Constructor & Destructor Documentation

◆ CreateTableAsSelectStmt()

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 1111 of file ParserNode.h.

1116  : InsertIntoTableAsSelectStmt(table_name, select_query, nullptr)
1117  , is_temporary_(is_temporary)
1118  , if_not_exists_(if_not_exists) {
1119  if (s) {
1120  for (const auto& e : *s) {
1121  storage_options_.emplace_back(e);
1122  }
1123  delete s;
1124  }
1125  }
InsertIntoTableAsSelectStmt(const std::string *table_name, const std::string *select_query, std::list< std::string *> *c)
Definition: ParserNode.h:1075
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1132

Member Function Documentation

◆ execute()

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

Reimplemented from Parser::InsertIntoTableAsSelectStmt.

Definition at line 2977 of file ParserNode.cpp.

References CHECK, Catalog_Namespace::SessionInfo::checkDBAccessPrivileges(), Data_Namespace::CPU_LEVEL, query_state::QueryState::create(), AccessPrivileges::CREATE_TABLE, DEFAULT_FRAGMENT_ROWS, DEFAULT_MAX_CHUNK_SIZE, 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::LocalConnector::getColumnDescriptors(), logger::INFO, Fragmenter_Namespace::INSERT_ORDER, TableDescriptor::isView, kENCODING_DICT, TableDescriptor::keyMetainfo, LOG, TableDescriptor::maxChunkSize, TableDescriptor::maxFragRows, TableDescriptor::maxRows, TableDescriptor::nColumns, TableDescriptor::persistenceLevel, Parser::LocalConnector::query(), run_benchmark_import::result, Parser::anonymous_namespace{ParserNode.cpp}::serialize_key_metainfo(), STDLOG, TableDBObjectType, TableDescriptor::tableName, TableDescriptor::userId, and Catalog_Namespace::UserMetadata::userId.

2977  {
2978  auto session_copy = session;
2979  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
2980  &session_copy, boost::null_deleter());
2982  auto stdlog = STDLOG(query_state);
2983 
2984  LocalConnector local_connector;
2985  auto& catalog = session.getCatalog();
2986  bool create_table = nullptr == leafs_connector_;
2987 
2988  if (create_table) {
2989  // check access privileges
2992  throw std::runtime_error("CTAS failed. Table " + table_name_ +
2993  " will not be created. User has no create privileges.");
2994  }
2995 
2996  if (catalog.getMetadataForTable(table_name_) != nullptr) {
2997  if (if_not_exists_) {
2998  return;
2999  }
3000  throw std::runtime_error("Table " + table_name_ +
3001  " already exists and no data was loaded.");
3002  }
3003 
3004  // only validate the select query so we get the target types
3005  // correctly, but do not populate the result set
3006  auto result = local_connector.query(
3007  query_state->createQueryStateProxy(), select_query_, {}, true);
3008  const auto column_descriptors_for_create =
3009  local_connector.getColumnDescriptors(result, true);
3010 
3011  // some validation as the QE might return some out of range column types
3012  for (auto& cd : column_descriptors_for_create) {
3013  if (cd.columnType.is_decimal() && cd.columnType.get_precision() > 18) {
3014  throw std::runtime_error(cd.columnName + ": Precision too high, max 18.");
3015  }
3016  }
3017 
3018  TableDescriptor td;
3019  td.tableName = table_name_;
3020  td.userId = session.get_currentUser().userId;
3021  td.nColumns = column_descriptors_for_create.size();
3022  td.isView = false;
3023  td.fragmenter = nullptr;
3029  if (is_temporary_) {
3031  } else {
3033  }
3034 
3035  bool use_shared_dictionaries = true;
3036 
3037  if (!storage_options_.empty()) {
3038  for (auto& p : storage_options_) {
3039  if (boost::to_lower_copy<std::string>(*p->get_name()) ==
3040  "use_shared_dictionaries") {
3041  const StringLiteral* literal =
3042  dynamic_cast<const StringLiteral*>(p->get_value());
3043  if (nullptr == literal) {
3044  throw std::runtime_error(
3045  "USE_SHARED_DICTIONARIES must be a string parameter");
3046  }
3047  std::string val = boost::to_lower_copy<std::string>(*literal->get_stringval());
3048  use_shared_dictionaries = val == "true" || val == "1" || val == "t";
3049  } else {
3050  get_table_definitions_for_ctas(td, p, column_descriptors_for_create);
3051  }
3052  }
3053  }
3054 
3055  std::vector<SharedDictionaryDef> sharedDictionaryRefs;
3056 
3057  if (use_shared_dictionaries) {
3058  const auto source_column_descriptors =
3059  local_connector.getColumnDescriptors(result, false);
3060  const auto mapping = catalog.getDictionaryToColumnMapping();
3061 
3062  for (auto& source_cd : source_column_descriptors) {
3063  const auto& ti = source_cd.columnType;
3064  if (ti.is_string()) {
3065  if (ti.get_compression() == kENCODING_DICT) {
3066  int dict_id = ti.get_comp_param();
3067  auto it = mapping.find(dict_id);
3068  if (mapping.end() != it) {
3069  const auto targetColumn = it->second;
3070  auto targetTable =
3071  catalog.getMetadataForTable(targetColumn->tableId, false);
3072  CHECK(targetTable);
3073  LOG(INFO) << "CTAS: sharing text dictionary on column "
3074  << source_cd.columnName << " with " << targetTable->tableName
3075  << "." << targetColumn->columnName;
3076  sharedDictionaryRefs.push_back(
3077  SharedDictionaryDef(source_cd.columnName,
3078  targetTable->tableName,
3079  targetColumn->columnName));
3080  }
3081  }
3082  }
3083  }
3084  }
3085 
3086  // currently no means of defining sharding in CTAS
3087  td.keyMetainfo = serialize_key_metainfo(nullptr, sharedDictionaryRefs);
3088 
3089  catalog.createTable(td, column_descriptors_for_create, sharedDictionaryRefs, true);
3090  // TODO (max): It's transactionally unsafe, should be fixed: we may create object
3091  // w/o privileges
3092  SysCatalog::instance().createDBObject(
3093  session.get_currentUser(), td.tableName, TableDBObjectType, catalog);
3094  }
3095 
3096  try {
3097  populateData(query_state->createQueryStateProxy(), false);
3098  } catch (...) {
3099  if (!g_cluster) {
3100  const TableDescriptor* created_td = catalog.getMetadataForTable(table_name_);
3101  if (created_td) {
3102  catalog.dropTable(created_td);
3103  }
3104  }
3105  throw;
3106  }
3107 }
std::string tableName
#define LOG(tag)
Definition: Logger.h:188
void populateData(QueryStateProxy, bool validate_table)
#define DEFAULT_MAX_CHUNK_SIZE
const UserMetadata & get_currentUser() const
Definition: SessionInfo.h:68
void get_table_definitions_for_ctas(TableDescriptor &td, const std::unique_ptr< NameValueAssign > &p, const std::list< ColumnDescriptor > &columns)
#define DEFAULT_MAX_ROWS
std::list< std::unique_ptr< NameValueAssign > > storage_options_
Definition: ParserNode.h:1132
Catalog & getCatalog() const
Definition: SessionInfo.h:65
static std::shared_ptr< QueryState > create(ARGS &&... args)
Definition: QueryState.h:140
static const AccessPrivileges CREATE_TABLE
Definition: DBObject.h:159
std::string keyMetainfo
std::shared_ptr< Fragmenter_Namespace::AbstractFragmenter > fragmenter
#define DEFAULT_PAGE_SIZE
#define DEFAULT_FRAGMENT_ROWS
Fragmenter_Namespace::FragmenterType fragType
Data_Namespace::MemoryLevel persistenceLevel
bool checkDBAccessPrivileges(const DBObjectType &permissionType, const AccessPrivileges &privs, const std::string &objectName="") const
Definition: SessionInfo.cpp:24
DistributedConnector * leafs_connector_
Definition: ParserNode.h:1097
#define CHECK(condition)
Definition: Logger.h:197
bool g_cluster
specifies the content in-memory of a row in the table metadata table
#define STDLOG(...)
Definition: QueryState.h:225
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:

Member Data Documentation

◆ if_not_exists_

const bool Parser::CreateTableAsSelectStmt::if_not_exists_
private

Definition at line 1131 of file ParserNode.h.

◆ is_temporary_

const bool Parser::CreateTableAsSelectStmt::is_temporary_
private

Definition at line 1130 of file ParserNode.h.

◆ storage_options_

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

Definition at line 1132 of file ParserNode.h.


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