OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DdlCommandExecutor Class Reference

#include <DdlCommandExecutor.h>

+ Collaboration diagram for DdlCommandExecutor:

Public Member Functions

 DdlCommandExecutor (const std::string &ddl_statement, std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr)
 
ExecutionResult execute ()
 
bool isShowUserSessions ()
 
bool isShowQueries ()
 
bool isShowCreateTable ()
 
bool isKillQuery ()
 
bool isAlterSystemClear ()
 
std::string returnCacheType ()
 
const std::string getTargetQuerySessionToKill ()
 
DistributedExecutionDetails getDistributedExecutionDetails ()
 
const std::string commandStr ()
 

Private Attributes

std::string ddl_statement_
 
std::string ddl_command_
 
std::unique_ptr< DdlCommandDataddl_data_
 
std::shared_ptr
< Catalog_Namespace::SessionInfo
const
session_ptr_
 

Detailed Description

Definition at line 222 of file DdlCommandExecutor.h.

Constructor & Destructor Documentation

DdlCommandExecutor::DdlCommandExecutor ( const std::string &  ddl_statement,
std::shared_ptr< Catalog_Namespace::SessionInfo const session_ptr 
)

Definition at line 311 of file DdlCommandExecutor.cpp.

References CHECK, ddl_command_, ddl_data_, ddl_statement_, and VLOG.

314  : session_ptr_(session_ptr) {
315  CHECK(!ddl_statement.empty());
316  ddl_statement_ = ddl_statement;
317 
318  // parse the incoming query,
319  // cache the parsed rapidjson object inside a DdlCommandDataImpl
320  // store the "abstract/base class" reference in ddl_data_
321  DdlCommandDataImpl* ddl_query_data = new DdlCommandDataImpl(ddl_statement);
322  ddl_data_ = std::unique_ptr<DdlCommandData>(ddl_query_data);
323 
324  VLOG(2) << "Parsing JSON DDL from Calcite: " << ddl_statement;
325  auto& ddl_query = ddl_query_data->query();
326  CHECK(ddl_query.IsObject()) << ddl_statement;
327  CHECK(ddl_query.HasMember("payload"));
328  CHECK(ddl_query["payload"].IsObject());
329  const auto& payload = ddl_query["payload"].GetObject();
330  CHECK(payload.HasMember("command"));
331  CHECK(payload["command"].IsString());
332  ddl_command_ = payload["command"].GetString();
333 }
std::unique_ptr< DdlCommandData > ddl_data_
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_
#define CHECK(condition)
Definition: Logger.h:211
#define VLOG(n)
Definition: Logger.h:305

Member Function Documentation

const std::string DdlCommandExecutor::commandStr ( )

Returns command string, can be useful for logging, conversion

Definition at line 588 of file DdlCommandExecutor.cpp.

References ddl_command_.

Referenced by DBHandler::executeDdl().

588  {
589  return ddl_command_;
590 }

+ Here is the caller graph for this function:

ExecutionResult DdlCommandExecutor::execute ( )

Parses given JSON string and routes resulting payload to appropriate DDL command class for execution.

Parameters
returnExecutionResult of DDL command execution (if applicable)

Definition at line 335 of file DdlCommandExecutor.cpp.

References ddl_command_, ddl_data_, Parser::AlterTableStmt::delegate(), legacylockmgr::ExecutorOuterLock, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), run_benchmark_import::result, session_ptr_, ExecutionResult::SimpleResult, UNREACHABLE, and ExecutionResult::updateResultSet().

Referenced by DBHandler::executeDdl(), omnisci.cursor.Cursor::executemany(), and QueryRunner::QueryRunner::runDDLStatement().

335  {
337 
338  // the following commands use parser node locking to ensure safe concurrent access
339  if (ddl_command_ == "CREATE_TABLE") {
340  auto create_table_stmt = Parser::CreateTableStmt(extractPayload(*ddl_data_));
341  create_table_stmt.execute(*session_ptr_);
342  return result;
343  } else if (ddl_command_ == "CREATE_VIEW") {
344  auto create_view_stmt = Parser::CreateViewStmt(extractPayload(*ddl_data_));
345  create_view_stmt.execute(*session_ptr_);
346  return result;
347  } else if (ddl_command_ == "DROP_TABLE") {
348  auto drop_table_stmt = Parser::DropTableStmt(extractPayload(*ddl_data_));
349  drop_table_stmt.execute(*session_ptr_);
350  return result;
351  } else if (ddl_command_ == "DROP_VIEW") {
352  auto drop_view_stmt = Parser::DropViewStmt(extractPayload(*ddl_data_));
353  drop_view_stmt.execute(*session_ptr_);
354  return result;
355  } else if (ddl_command_ == "RENAME_TABLE") {
356  auto rename_table_stmt = Parser::RenameTableStmt(extractPayload(*ddl_data_));
357  rename_table_stmt.execute(*session_ptr_);
358  return result;
359  } else if (ddl_command_ == "ALTER_TABLE") {
361  if (stmt != nullptr) {
362  stmt->execute(*session_ptr_);
363  }
364  return result;
365  } else if (ddl_command_ == "TRUNCATE_TABLE") {
366  auto truncate_table_stmt = Parser::TruncateTableStmt(extractPayload(*ddl_data_));
367  truncate_table_stmt.execute(*session_ptr_);
368  return result;
369  } else if (ddl_command_ == "DUMP_TABLE") {
370  auto dump_table_stmt = Parser::DumpTableStmt(extractPayload(*ddl_data_));
371  dump_table_stmt.execute(*session_ptr_);
372  return result;
373  } else if (ddl_command_ == "RESTORE_TABLE") {
374  auto restore_table_stmt = Parser::RestoreTableStmt(extractPayload(*ddl_data_));
375  restore_table_stmt.execute(*session_ptr_);
376  return result;
377  } else if (ddl_command_ == "OPTIMIZE_TABLE") {
378  auto optimize_table_stmt = Parser::OptimizeTableStmt(extractPayload(*ddl_data_));
379  optimize_table_stmt.execute(*session_ptr_);
380  return result;
381  } else if (ddl_command_ == "SHOW_CREATE_TABLE") {
382  auto show_create_table_stmt = Parser::ShowCreateTableStmt(extractPayload(*ddl_data_));
383  show_create_table_stmt.execute(*session_ptr_);
384  const auto create_string = show_create_table_stmt.getCreateStmt();
385  result.updateResultSet(create_string, ExecutionResult::SimpleResult);
386  return result;
387  } else if (ddl_command_ == "COPY_TABLE") {
388  auto copy_table_stmt = Parser::CopyTableStmt(extractPayload(*ddl_data_));
389  copy_table_stmt.execute(*session_ptr_);
390  return result;
391  } else if (ddl_command_ == "EXPORT_QUERY") {
392  auto export_query_stmt = Parser::ExportQueryStmt(extractPayload(*ddl_data_));
393  export_query_stmt.execute(*session_ptr_);
394  return result;
395 
396  } else if (ddl_command_ == "CREATE_DB") {
397  auto create_db_stmt = Parser::CreateDBStmt(extractPayload(*ddl_data_));
398  create_db_stmt.execute(*session_ptr_);
399  return result;
400  } else if (ddl_command_ == "DROP_DB") {
401  auto drop_db_stmt = Parser::DropDBStmt(extractPayload(*ddl_data_));
402  drop_db_stmt.execute(*session_ptr_);
403  return result;
404  } else if (ddl_command_ == "RENAME_DB") {
405  auto rename_db_stmt = Parser::RenameDBStmt(extractPayload(*ddl_data_));
406  rename_db_stmt.execute(*session_ptr_);
407  return result;
408  } else if (ddl_command_ == "CREATE_USER") {
409  auto create_user_stmt = Parser::CreateUserStmt(extractPayload(*ddl_data_));
410  create_user_stmt.execute(*session_ptr_);
411  return result;
412  } else if (ddl_command_ == "DROP_USER") {
413  auto drop_user_stmt = Parser::DropUserStmt(extractPayload(*ddl_data_));
414  drop_user_stmt.execute(*session_ptr_);
415  return result;
416  } else if (ddl_command_ == "ALTER_USER") {
417  auto alter_user_stmt = Parser::AlterUserStmt(extractPayload(*ddl_data_));
418  alter_user_stmt.execute(*session_ptr_);
419  return result;
420  } else if (ddl_command_ == "RENAME_USER") {
421  auto rename_user_stmt = Parser::RenameUserStmt(extractPayload(*ddl_data_));
422  rename_user_stmt.execute(*session_ptr_);
423  return result;
424  } else if (ddl_command_ == "CREATE_ROLE") {
425  auto create_role_stmt = Parser::CreateRoleStmt(extractPayload(*ddl_data_));
426  create_role_stmt.execute(*session_ptr_);
427  return result;
428  } else if (ddl_command_ == "DROP_ROLE") {
429  auto drop_role_stmt = Parser::DropRoleStmt(extractPayload(*ddl_data_));
430  drop_role_stmt.execute(*session_ptr_);
431  return result;
432  } else if (ddl_command_ == "GRANT_ROLE") {
433  auto grant_role_stmt = Parser::GrantRoleStmt(extractPayload(*ddl_data_));
434  grant_role_stmt.execute(*session_ptr_);
435  return result;
436  } else if (ddl_command_ == "REVOKE_ROLE") {
437  auto revoke_role_stmt = Parser::RevokeRoleStmt(extractPayload(*ddl_data_));
438  revoke_role_stmt.execute(*session_ptr_);
439  return result;
440  } else if (ddl_command_ == "GRANT_PRIVILEGE") {
441  auto grant_privilege_stmt = Parser::GrantPrivilegesStmt(extractPayload(*ddl_data_));
442  grant_privilege_stmt.execute(*session_ptr_);
443  return result;
444  } else if (ddl_command_ == "REVOKE_PRIVILEGE") {
445  auto revoke_privileges_stmt =
447  revoke_privileges_stmt.execute(*session_ptr_);
448  return result;
449  } else if (ddl_command_ == "CREATE_DATAFRAME") {
450  auto create_dataframe_stmt = Parser::CreateDataframeStmt(extractPayload(*ddl_data_));
451  create_dataframe_stmt.execute(*session_ptr_);
452  return result;
453  } else if (ddl_command_ == "VALIDATE_SYSTEM") {
454  // VALIDATE should have been excuted in outer context before it reaches here
455  UNREACHABLE();
456  }
457 
458  // the following commands require a global unique lock until proper table locking has
459  // been implemented and/or verified
460  auto execute_write_lock = mapd_unique_lock<mapd_shared_mutex>(
463  // TODO(vancouver): add appropriate table locking
464 
465  if (ddl_command_ == "CREATE_SERVER") {
466  result = CreateForeignServerCommand{*ddl_data_, session_ptr_}.execute();
467  } else if (ddl_command_ == "DROP_SERVER") {
468  result = DropForeignServerCommand{*ddl_data_, session_ptr_}.execute();
469  } else if (ddl_command_ == "CREATE_FOREIGN_TABLE") {
470  result = CreateForeignTableCommand{*ddl_data_, session_ptr_}.execute();
471  } else if (ddl_command_ == "DROP_FOREIGN_TABLE") {
472  result = DropForeignTableCommand{*ddl_data_, session_ptr_}.execute();
473  } else if (ddl_command_ == "SHOW_TABLES") {
474  result = ShowTablesCommand{*ddl_data_, session_ptr_}.execute();
475  } else if (ddl_command_ == "SHOW_TABLE_DETAILS") {
476  result = ShowTableDetailsCommand{*ddl_data_, session_ptr_}.execute();
477  } else if (ddl_command_ == "SHOW_DATABASES") {
478  result = ShowDatabasesCommand{*ddl_data_, session_ptr_}.execute();
479  } else if (ddl_command_ == "SHOW_SERVERS") {
480  result = ShowForeignServersCommand{*ddl_data_, session_ptr_}.execute();
481  } else if (ddl_command_ == "ALTER_SERVER") {
482  result = AlterForeignServerCommand{*ddl_data_, session_ptr_}.execute();
483  } else if (ddl_command_ == "ALTER_FOREIGN_TABLE") {
484  result = AlterForeignTableCommand{*ddl_data_, session_ptr_}.execute();
485  } else if (ddl_command_ == "REFRESH_FOREIGN_TABLES") {
486  result = RefreshForeignTablesCommand{*ddl_data_, session_ptr_}.execute();
487  } else if (ddl_command_ == "SHOW_DISK_CACHE_USAGE") {
488  result = ShowDiskCacheUsageCommand{*ddl_data_, session_ptr_}.execute();
489  } else if (ddl_command_ == "SHOW_USER_DETAILS") {
490  result = ShowUserDetailsCommand{*ddl_data_, session_ptr_}.execute();
491  } else if (ddl_command_ == "SHOW_ROLES") {
492  result = ShowRolesCommand{*ddl_data_, session_ptr_}.execute();
493  } else if (ddl_command_ == "REASSIGN_OWNED") {
494  result = ReassignOwnedCommand{*ddl_data_, session_ptr_}.execute();
495  } else {
496  throw std::runtime_error("Unsupported DDL command");
497  }
498 
499  return result;
500 }
std::unique_ptr< DdlCommandData > ddl_data_
#define UNREACHABLE()
Definition: Logger.h:255
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
void updateResultSet(const std::string &query_ra, RType type, bool success=true)
std::shared_ptr< Catalog_Namespace::SessionInfo const > session_ptr_
static std::shared_ptr< MutexType > getMutex(const LockType lockType, const KeyType &key)
Definition: LegacyLockMgr.h:51
static std::unique_ptr< Parser::DDLStmt > delegate(const rapidjson::Value &payload)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

DistributedExecutionDetails DdlCommandExecutor::getDistributedExecutionDetails ( )

Returns an object indicating where command execution should take place and how results should be aggregated for distributed setups.

Definition at line 530 of file DdlCommandExecutor.cpp.

References DistributedExecutionDetails::aggregation_type, AGGREGATOR_ONLY, ALL_NODES, CHECK, ddl_command_, ddl_data_, DistributedExecutionDetails::execution_location, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), LEAVES_ONLY, NONE, and UNION.

530  {
531  DistributedExecutionDetails execution_details;
532  if (ddl_command_ == "CREATE_DATAFRAME" || ddl_command_ == "RENAME_TABLE" ||
533  ddl_command_ == "ALTER_TABLE" || ddl_command_ == "CREATE_TABLE" ||
534  ddl_command_ == "DROP_TABLE" || ddl_command_ == "TRUNCATE_TABLE" ||
535  ddl_command_ == "DUMP_TABLE" || ddl_command_ == "RESTORE_TABLE" ||
536  ddl_command_ == "OPTIMIZE_TABLE" || ddl_command_ == "CREATE_VIEW" ||
537  ddl_command_ == "DROP_VIEW" || ddl_command_ == "CREATE_DB" ||
538  ddl_command_ == "DROP_DB" || ddl_command_ == "RENAME_DB" ||
539  ddl_command_ == "CREATE_USER" || ddl_command_ == "DROP_USER" ||
540  ddl_command_ == "ALTER_USER" || ddl_command_ == "RENAME_USER" ||
541  ddl_command_ == "CREATE_ROLE" || ddl_command_ == "DROP_ROLE" ||
542  ddl_command_ == "GRANT_ROLE" || ddl_command_ == "REVOKE_ROLE" ||
543  ddl_command_ == "REASSIGN_OWNED") {
544  // group user/role/db commands
546  execution_details.aggregation_type = AggregationType::NONE;
547  } else if (ddl_command_ == "GRANT_PRIVILEGE" || ddl_command_ == "REVOKE_PRIVILEGE") {
548  auto& ddl_payload = extractPayload(*ddl_data_);
549  CHECK(ddl_payload.HasMember("type"));
550  const std::string& targetType = ddl_payload["type"].GetString();
551  if (targetType == "DASHBOARD") {
552  // dashboard commands should run on Aggregator alone
554  execution_details.aggregation_type = AggregationType::NONE;
555  } else {
557  execution_details.aggregation_type = AggregationType::NONE;
558  }
559 
560  } else if (ddl_command_ == "SHOW_TABLE_DETAILS") {
562  execution_details.aggregation_type = AggregationType::UNION;
563  } else {
564  // Commands that fall here : COPY_TABLE, EXPORT_QUERY, etc.
566  execution_details.aggregation_type = AggregationType::NONE;
567  }
568  return execution_details;
569 }
std::unique_ptr< DdlCommandData > ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
#define CHECK(condition)
Definition: Logger.h:211
ExecutionLocation execution_location

+ Here is the call graph for this function:

const std::string DdlCommandExecutor::getTargetQuerySessionToKill ( )

Returns target query session if this command is KILL QUERY

Definition at line 571 of file DdlCommandExecutor.cpp.

References CHECK, ddl_data_, anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload(), and isKillQuery().

Referenced by DBHandler::executeDdl().

571  {
572  // caller should check whether DDL indicates KillQuery request
573  // i.e., use isKillQuery() before calling this function
574  auto& ddl_payload = extractPayload(*ddl_data_);
575  CHECK(isKillQuery());
576  CHECK(ddl_payload.HasMember("querySession"));
577  const std::string& query_session = ddl_payload["querySession"].GetString();
578  // regex matcher for public_session: start_time{3}-session_id{4} (Example:819-4RDo)
579  boost::regex session_id_regex{R"([0-9]{3}-[a-zA-Z0-9]{4})",
580  boost::regex::extended | boost::regex::icase};
581  if (!boost::regex_match(query_session, session_id_regex)) {
582  throw std::runtime_error(
583  "Please provide the correct session ID of the query that you want to interrupt.");
584  }
585  return query_session;
586 }
std::unique_ptr< DdlCommandData > ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
#define CHECK(condition)
Definition: Logger.h:211

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DdlCommandExecutor::isAlterSystemClear ( )

Returns true if this command is ALTER SYSTEM CLEAR

Definition at line 518 of file DdlCommandExecutor.cpp.

References ddl_command_.

Referenced by DBHandler::executeDdl().

518  {
519  return (ddl_command_ == "ALTER_SYSTEM_CLEAR");
520 }

+ Here is the caller graph for this function:

bool DdlCommandExecutor::isKillQuery ( )

Returns true if this command is KILL QUERY

Definition at line 510 of file DdlCommandExecutor.cpp.

References ddl_command_.

Referenced by DBHandler::executeDdl(), and getTargetQuerySessionToKill().

510  {
511  return (ddl_command_ == "KILL_QUERY");
512 }

+ Here is the caller graph for this function:

bool DdlCommandExecutor::isShowCreateTable ( )

Returns true if this command is SHOW CREATE TABLE

Definition at line 514 of file DdlCommandExecutor.cpp.

References ddl_command_.

Referenced by DBHandler::executeDdl().

514  {
515  return (ddl_command_ == "SHOW_CREATE_TABLE");
516 }

+ Here is the caller graph for this function:

bool DdlCommandExecutor::isShowQueries ( )

Returns true if this command is SHOW QUERIES

Definition at line 506 of file DdlCommandExecutor.cpp.

References ddl_command_.

Referenced by DBHandler::executeDdl().

506  {
507  return (ddl_command_ == "SHOW_QUERIES");
508 }

+ Here is the caller graph for this function:

bool DdlCommandExecutor::isShowUserSessions ( )

Returns true if this command is SHOW USER SESSIONS

Definition at line 502 of file DdlCommandExecutor.cpp.

References ddl_command_.

Referenced by DBHandler::executeDdl().

502  {
503  return (ddl_command_ == "SHOW_USER_SESSIONS");
504 }

+ Here is the caller graph for this function:

std::string DdlCommandExecutor::returnCacheType ( )

Returns which kind of caches if to clear ALTER SYSTEM CLEAR

Definition at line 522 of file DdlCommandExecutor.cpp.

References CHECK, ddl_command_, ddl_data_, and anonymous_namespace{DdlCommandExecutor.cpp}::extractPayload().

Referenced by DBHandler::executeDdl().

522  {
523  CHECK(ddl_command_ == "ALTER_SYSTEM_CLEAR");
524  auto& ddl_payload = extractPayload(*ddl_data_);
525  CHECK(ddl_payload.HasMember("cacheType"));
526  CHECK(ddl_payload["cacheType"].IsString());
527  return ddl_payload["cacheType"].GetString();
528 }
std::unique_ptr< DdlCommandData > ddl_data_
const rapidjson::Value & extractPayload(const DdlCommandData &ddl_data)
#define CHECK(condition)
Definition: Logger.h:211

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

std::unique_ptr<DdlCommandData> DdlCommandExecutor::ddl_data_
private
std::string DdlCommandExecutor::ddl_statement_
private

Definition at line 285 of file DdlCommandExecutor.h.

Referenced by DdlCommandExecutor().

std::shared_ptr<Catalog_Namespace::SessionInfo const> DdlCommandExecutor::session_ptr_
private

Definition at line 288 of file DdlCommandExecutor.h.

Referenced by execute().


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