OmniSciDB  c07336695a
CommandFunctors.h File Reference
#include "CommandResolutionChain.h"
#include "ThriftOps.h"
#include "gen-cpp/MapD.h"
#include "ClientContext.h"
#include "RegexSupport.h"
#include <rapidjson/document.h>
#include <fstream>
#include <iostream>
#include <type_traits>
#include "Fragmenter/InsertOrderFragmenter.h"
#include "MapDServer.h"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/preprocessor/facilities/overload.hpp>
#include "Shared/base64.h"
+ Include dependency graph for CommandFunctors.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  ContextOperations< CONTEXT_OP_POLICY >
 
struct  DefaultContextOpPolicy
 
class  CmdBase< CONTEXT_OPS_TYPE, TAG_TYPE >
 
class  CmdStringUtilities
 

Macros

#define StandardCommand_3(CommandName, CommandOperations, CommandTag)
 
#define StandardCommand_2(CommandName, CommandOperations)   StandardCommand_3(CommandName, CommandOperations, CmdDeterminant)
 
#define StandardCommand(...)   BOOST_PP_OVERLOAD(StandardCommand_, __VA_ARGS__)(__VA_ARGS__)
 

Functions

 StandardCommand (Status, { ContextOps::get_status(cmdContext(), output_stream);})
 
 StandardCommand (CopyGeo, { std::cout<< "Error: The \opygeo command is deprecated. Use: COPY <table> FROM " "'<file>' WITH (geo='true');"<< std::endl;})
 
 StandardCommand (RoleList, { cmdContext().privs_user_name.clear();cmdContext().privs_user_name=p[1];ContextOps::get_all_roles_for_user(cmdContext());})
 
 StandardCommand (Roles, ContextOps::get_all_roles(cmdContext()))
 
 StandardCommand (ListUsers, { returned_list_regex< kGET_USERS >(p, cmdContext(), output_stream);}, RegexCmdDeterminant)
 
 StandardCommand (ListTables, { returned_list_regex< kGET_PHYSICAL_TABLES >(p, cmdContext(), output_stream);}, RegexCmdDeterminant)
 
 StandardCommand (ListViews, { returned_list_regex< kGET_VIEWS >(p, cmdContext(), output_stream);}, RegexCmdDeterminant)
 
 StandardCommand (Help, { std::cout<< "\ [regex] List all users, optionally matching regex.\;std::cout<< "\l List all databases.\";std::cout<< "\t [regex] List all tables, optionally matching regex.\";std::cout<< "\v [regex] List all views, optionally matching regex.\";std::cout<< "\d< table > List all columns of a table or a view.\";std::cout<< "\c< database >< user >< password >.\";std::cout<< "\db [database|...] Switch database. Use ... to switch to your default.\";std::cout<< "\dash List all dashboards accessible by user.\";std::cout<< "\o< table > Return a memory optimized schema based on current data " "distribution in table.\";std::cout<< "\gpu Execute in GPU mode 's.\";std::cout<< "\cpu Execute in CPU mode 's.\";std::cout<< "\multiline Set multi-line command line mode.\";std::cout<< "\singleline Set single-line command line mode.\";std::cout<< "\historylen< number > Set history buffer size(default 100).\";std::cout<< "\timing Print timing information.\";std::cout<< "\notiming Do not print timing information.\";std::cout<< "\memory_summary Print memory usage summary.\";std::cout<< "\version Print OmniSci Server version.\";std::cout<< "\copy< file path >< table > Copy data from file to table.\";std::cout<< "\status Get status of the server and the leaf nodes.\";std::cout<< "\export_dashboard< dashboard name >< filename >< optional:dashboard " "owner name > Exports a dashboard to a " "file.\";std::cout<< "\import_dashboard< dashboard name >< filename > Imports a dashboard from " "a file\";std::cout<< "\roles Reports all roles.\";std::cout<< "\role_list< userName > Reports all roles granted to user.\";std::cout<< "\privileges {< roleName >|< userName >} Reports all database objects " "privileges granted to role or " "user.\";std::cout<< "\object_privileges {database|table}< object_name > Reports all " "privileges granted to an object for all " "roles and users.\";std::cout<< "\detect {parquet}< file_name|s3_details > Reads a sample of the " "specified file and returns a CREATE TABLE statement\";std::cout<< "\clear_cpu Releases CPU memory held by OmniSci server Data Manager\";std::cout<< "\clear_gpu Releases GPU memory held by OmniSci server Data Manager\";std::cout<< "\q Quit.\";std::cout.flush();})
 
 StandardCommand (ListDatabases, { thrift_op< kGET_DATABASES >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "Database | Owner"<< std::endl;for(auto p :lambda_context.dbinfos_return) { output_stream<< p.db_name<< " | "<< p.db_owner<< '\n';} output_stream.flush();});})
 
 StandardCommand (ConnectToDB, { if(cmdContext().session !=INVALID_SESSION_ID) { thrift_op< kDISCONNECT >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "Disconnected from database "<< cmdContext().db_name<< std::endl;});} cmdContext().db_name=p[1];cmdContext().user_name=p[2];cmdContext().passwd=p[3];thrift_op< kCONNECT >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "User "<< lambda_context.user_name<< " connected to database "<< lambda_context.db_name<< std::endl;});})
 
 StandardCommand (SwitchDatabase, { if(p.size()==2) { if(p[1]==std::string("...")) { cmdContext().db_name.clear();} else { cmdContext().db_name=p[1];} thrift_op< kSWITCH_DATABASE >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "User "<< lambda_context.user_name<< " switched to database "<< lambda_context.db_name<< std::endl;});} else if(p.size()==1) { thrift_op< kREPORT_DATABASE >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "User "<< lambda_context.user_name<< " is using database "<< lambda_context.db_name<< std::endl;});} })
 
 StandardCommand (ListColumns, { decltype(p[1])&table_name(p[1]);auto unserialize_key_metainfo=[](const std::string key_metainfo) -> std::vector< std::string > { std::vector< std::string > keys_with_spec;rapidjson::Document document;document.Parse(key_metainfo.c_str());CHECK(!document.HasParseError());CHECK(document.IsArray());for(auto it=document.Begin();it !=document.End();++it) { const auto &key_with_spec_json= *it;CHECK(key_with_spec_json.IsObject());const std::string type=key_with_spec_json["type"].GetString();const std::string name=key_with_spec_json["name"].GetString();auto key_with_spec=type+" ("+name+")";if(type=="SHARED DICTIONARY") { key_with_spec+=" REFERENCES ";const std::string foreign_table=key_with_spec_json["foreign_table"].GetString();const std::string foreign_column=key_with_spec_json["foreign_column"].GetString();key_with_spec+=foreign_table+"("+foreign_column+")";} else { CHECK(type=="SHARD KEY");} keys_with_spec.push_back(key_with_spec);} return keys_with_spec;};auto on_success_lambda=[&](ContextType &context) { const auto table_details=context.table_details;if(table_details.view_sql.empty()) { std::string temp_holder(" ");if(table_details.is_temporary) { temp_holder=" TEMPORARY ";} output_stream<< "CREATE"+temp_holder+"TABLE "+table_name+" (\;} else { output_stream<< "CREATE VIEW "+table_name+" AS "+table_details.view_sql<< "\";output_stream<< "\"<< "View columns:"<< "\\";} std::string comma_or_blank("");for(TColumnType p :table_details.row_desc) { if(p.is_system) { continue;} std::string encoding="";if(p.col_type.type==TDatumType::STR) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} else if(p.col_type.type==TDatumType::POINT|| p.col_type.type==TDatumType::LINESTRING|| p.col_type.type==TDatumType::POLYGON|| p.col_type.type==TDatumType::MULTIPOLYGON) { if(p.col_type.scale==4326) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} } else { encoding=(p.col_type.encoding==0 ? "" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} output_stream<< comma_or_blank<< p.col_name<< " "<< thrift_to_name(p.col_type)<<(p.col_type.nullable ? "" :" NOT NULL")<< encoding;comma_or_blank=",\";} if(table_details.view_sql.empty()) { const auto keys_with_spec=unserialize_key_metainfo(table_details.key_metainfo);for(const auto &key_with_spec :keys_with_spec) { output_stream<< ",\"<< key_with_spec;} output_stream<< ")\";comma_or_blank="";std::string frag="";std::string page="";std::string row="";std::string partition_detail="";if(DEFAULT_FRAGMENT_ROWS !=table_details.fragment_size) { frag="FRAGMENT_SIZE="+std::to_string(table_details.fragment_size);comma_or_blank=", ";} if(table_details.shard_count) { auto shard_count=table_details.shard_count;if(context.cluster_status.size() > 1) { size_t leaf_count=0;for(const auto &node :context.cluster_status) { leaf_count+=node.role==TRole::type::LEAF ? 1 :0;} shard_count *=leaf_count;} frag+=comma_or_blank+"SHARD_COUNT="+std::to_string(shard_count);comma_or_blank=", ";} if(DEFAULT_PAGE_SIZE !=table_details.page_size) { page=comma_or_blank+"PAGE_SIZE="+std::to_string(table_details.page_size);comma_or_blank=", ";} if(DEFAULT_MAX_ROWS !=table_details.max_rows) { row=comma_or_blank+"MAX_ROWS="+std::to_string(table_details.max_rows);comma_or_blank=", ";} if(table_details.partition_detail !=TPartitionDetail::DEFAULT) { partition_detail=comma_or_blank+"PARTITIONS=";switch(table_details.partition_detail) { case TPartitionDetail::REPLICATED:partition_detail+=" 'REPLICATED'";break;case TPartitionDetail::SHARDED:partition_detail+=" 'SHARDED'";break;default:partition_detail+=" 'OTHER'";break;} } std::string with=frag+page+row+partition_detail;if(with.length() > 0) { output_stream<< "WITH("<< with<< ")\";} } else { output_stream<< "\";} };thrift_op< kGET_TABLE_DETAILS >(cmdContext(), table_name.c_str(), on_success_lambda);})
 
 StandardCommand (ExportDashboard, { decltype(p[1])&dashboard_name(p[1]);decltype(p[2])&filename(p[2]);cmdContext().view_name=dashboard_name;cmdContext().dashboard_owner=p.size()==4 ? p[3] :"";thrift_op< kGET_DASHBOARDS >(cmdContext(), [&](ContextType &lambda_context) { if(lambda_context.dash_names.size()==0) { output_stream<< "No dashboards exist."<< std::endl;return;} std::vector< TDashboard > dashboards;for(auto &dash :lambda_context.dash_names) { if(dash.dashboard_name==lambda_context.view_name) { dashboards.push_back(dash);} } if(dashboards.size()==0) { output_stream<< "Dashboard "+lambda_context.view_name+" does not exist"<< std::endl;return;} int view_id;if(dashboards.size() > 1 &&p.size()==3) { output_stream<< "Multiple dashboards with name '"+lambda_context.view_name+"' exist. To help disambiguate identical dashboards please try " "\xport_dashboard <dash_name> <exportfile> <dash_owner>."<< std::endl;return;} else if(dashboards.size() > 1 &&p.size()==4) { bool found=false;for(auto &dash :dashboards) { if(dash.dashboard_owner==lambda_context.dashboard_owner) { view_id=dash.dashboard_id;found=true;} } if(!found) { output_stream<< "No dashboard with owner name '"+lambda_context.dashboard_owner+"' found."<< std::endl;return;} } else { view_id=dashboards[0].dashboard_id;} cmdContext().dash_id=view_id;thrift_op< kGET_DASHBOARD >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "Exporting dashboard "<< lambda_context.dash_return.dashboard_name<< " to file "<< filename<< std::endl;std::ofstream dashfile;dashfile.open(filename);if(dashfile.is_open()) { dashfile<< lambda_context.dash_return.dashboard_name<< std::endl;dashfile<< lambda_context.dash_return.dashboard_metadata<< std::endl;dashfile<< mapd::decode_base64(lambda_context.dash_return.dashboard_state);dashfile.close();} else { output_stream<< "Could not open file `"<< filename<< "`"<< std::endl;} });}, [&](ContextType &) { output_stream<< "Failed to export dashboard."<< std::endl;});})
 
 StandardCommand (ImportDashboard, { decltype(p[1])&dashboard_name(p[1]);decltype(p[2])&filename(p[2]);cmdContext().view_name=dashboard_name;cmdContext().view_state="";cmdContext().view_metadata="";std::ifstream dashfile;std::string state;std::string old_name;dashfile.open(filename);if(dashfile.is_open()) { std::getline(dashfile, old_name);std::getline(dashfile, cmdContext().view_metadata);std::getline(dashfile, state);dashfile.close();} else { output_stream<< "Could not open file `"<< filename<< "`"<< std::endl;return;} if(!replace(state, std::string("\itle\\+old_name+"\,"), std::string("\title\:\"+cmdContext().view_name+"\,"))) { output_stream<< "Failed to update title."<< std::endl;return;} cmdContext().view_state=mapd::encode_base64(state);output_stream<< "Importing dashboard "<< cmdContext().view_name<< " from file "<< filename<< std::endl;thrift_op< kIMPORT_DASHBOARD >(cmdContext(), DoNothing(), [&](ContextType &) { output_stream<< "Failed to import dashboard."<< std::endl; });})
 
 StandardCommand (GetOptimizedSchema, { decltype(p[1])&table_name(p[1]);auto get_row_count=[](const TQueryResult &query_result) -> size_t { CHECK(!query_result.row_set.row_desc.empty());if(query_result.row_set.columns.empty()) { return 0;} CHECK_EQ(query_result.row_set.columns.size(), query_result.row_set.row_desc.size());return query_result.row_set.columns.front().nulls.size();};auto run_query=[get_row_count](ContextType &context, std::string query) -> int { thrift_op< kSQL >(context, query.c_str());CHECK(get_row_count(context.query_return));return context.query_return.row_set.columns[0].data.int_col[0];};auto get_optimal_size=[run_query](ClientContext &context, std::string table_name, std::string col_name, int col_type) -> int { switch(col_type) { case TDatumType::STR:{ int strings=run_query(context, "select count(distinct "+col_name+") from "+table_name+";");if(strings< pow(2, 8)) { return 8;} else { if(strings< pow(2, 16)) { return 16;} else { return 32;} } } case TDatumType::TIME:{ return 32;} case TDatumType::DATE:case TDatumType::TIMESTAMP:{ return run_query(context, "select case when (extract( epoch from mn) > -2147483648 and " "extract (epoch from mx) < " "2147483647) then 32 else 0 end from (select min("+col_name+") mn, max("+col_name+") mx from "+table_name+" );");} case TDatumType::BIGINT:{ return run_query(context, "select case when (mn > -128 and mx < 127) then 8 else case " "when (mn > -32768 and mx < 32767) " "then 16 else case when (mn > -2147483648 and mx < 2147483647) " "then 32 else 0 end end end from " "(select min("+col_name+") mn, max("+col_name+") mx from "+table_name+" );");} case TDatumType::INT:{ return run_query(context, "select case when (mn > -128 and mx < 127) then 8 else case " "when (mn > -32768 and mx < 32767) " "then 16 else 0 end end from " "(select min("+col_name+") mn, max("+col_name+") mx from "+table_name+" );");} } return 0;};auto on_success_lambda=[&](ContextType &context) { const auto table_details=context.table_details;if(table_details.view_sql.empty()) { output_stream<< "CREATE TABLE "+table_name+" (\;} else { std::cerr<< "Can 't optimize a view, only the underlying tables\";return;} std::string comma_or_blank("");for(TColumnType p :table_details.row_desc) { std::string encoding="";if(p.col_type.type==TDatumType::STR) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(get_optimal_size( context, table_name, p.col_name, p.col_type.type))+ ")");} else if(p.col_type.type==TDatumType::POINT|| p.col_type.type==TDatumType::LINESTRING|| p.col_type.type==TDatumType::POLYGON|| p.col_type.type==TDatumType::MULTIPOLYGON) { if(p.col_type.scale==4326) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} } else { int opt_size=get_optimal_size(context, table_name, p.col_name, p.col_type.type);encoding=(opt_size==0 ? "" :" ENCODING FIXED("+std::to_string(opt_size)+")");} output_stream<< comma_or_blank<< p.col_name<< " "<< thrift_to_name(p.col_type)<<(p.col_type.nullable ? "" :" NOT NULL")<< encoding;comma_or_blank=",\";} if(table_details.view_sql.empty()) { output_stream<< ")\";comma_or_blank="";std::string frag="";std::string page="";std::string row="";if(DEFAULT_FRAGMENT_ROWS !=table_details.fragment_size) { frag=" FRAGMENT_SIZE="+std::to_string(table_details.fragment_size);comma_or_blank=",";} if(DEFAULT_PAGE_SIZE !=table_details.page_size) { page=comma_or_blank+" PAGE_SIZE="+std::to_string(table_details.page_size);comma_or_blank=",";} if(DEFAULT_MAX_ROWS !=table_details.max_rows) { row=comma_or_blank+" MAX_ROWS="+std::to_string(table_details.max_rows);} std::string with=frag+page+row;if(with.length() > 0) { output_stream<< "WITH("<< with<< ")\";} } else { output_stream<< "\";} };thrift_op< kGET_TABLE_DETAILS >(cmdContext(), table_name.c_str(), on_success_lambda);})
 
 StandardCommand (ListDashboards, ContextOps::get_dashboards(cmdContext()))
 

Macro Definition Documentation

◆ StandardCommand

#define StandardCommand (   ...)    BOOST_PP_OVERLOAD(StandardCommand_, __VA_ARGS__)(__VA_ARGS__)

Definition at line 177 of file CommandFunctors.h.

◆ StandardCommand_2

#define StandardCommand_2 (   CommandName,
  CommandOperations 
)    StandardCommand_3(CommandName, CommandOperations, CmdDeterminant)

Definition at line 175 of file CommandFunctors.h.

◆ StandardCommand_3

#define StandardCommand_3 (   CommandName,
  CommandOperations,
  CommandTag 
)
Value:
template <typename CONTEXT_OP_POLICY = DefaultContextOpPolicy, \
template <typename> class CONTEXT_OPS_TYPE = ContextOperations, \
typename COMMAND_TAG_TYPE = CommandTag> \
class CommandName##Cmd \
: public CmdBase<CONTEXT_OPS_TYPE<CONTEXT_OP_POLICY>, COMMAND_TAG_TYPE>, \
public CmdStringUtilities { \
public: \
using Super = CmdBase<CONTEXT_OPS_TYPE<CONTEXT_OP_POLICY>, COMMAND_TAG_TYPE>; \
using ContextOps = typename Super::ContextOps; \
using ContextType = typename Super::ContextType; \
CommandName##Cmd(typename Super::ContextType& c) : Super(c) {} \
using Super::cmdContext; \
template <typename PARAMETERS> \
void operator()(PARAMETERS const& p, std::ostream& output_stream = std::cout) { \
do { \
CommandOperations; \
} while (0); \
} \
}
void c(const std::string &query_string, const ExecutorDeviceType device_type)

Definition at line 154 of file CommandFunctors.h.

Function Documentation

◆ StandardCommand() [1/16]

StandardCommand ( Status  ,
{ ContextOps::get_status(cmdContext(), output_stream);}   
)

◆ StandardCommand() [2/16]

StandardCommand ( CopyGeo  ,
{ std::cout<< "Error: The \ygeo command is deprecated. Use: COPY <table> FROM " "'<file>' WITH (geo='true');"<< std::endl;}   
)

◆ StandardCommand() [3/16]

StandardCommand ( RoleList  ,
{ cmdContext().privs_user_name.clear();cmdContext().privs_user_name=p[1];ContextOps::get_all_roles_for_user(cmdContext());}   
)

◆ StandardCommand() [4/16]

StandardCommand ( Roles  ,
ContextOps::get_all_roles(cmdContext())   
)

◆ StandardCommand() [5/16]

StandardCommand ( ListUsers  ,
{ returned_list_regex< kGET_USERS >(p, cmdContext(), output_stream);}  ,
RegexCmdDeterminant   
)

◆ StandardCommand() [6/16]

StandardCommand ( ListTables  ,
{ returned_list_regex< kGET_PHYSICAL_TABLES >(p, cmdContext(), output_stream);}  ,
RegexCmdDeterminant   
)

◆ StandardCommand() [7/16]

StandardCommand ( ListViews  ,
{ returned_list_regex< kGET_VIEWS >(p, cmdContext(), output_stream);}  ,
RegexCmdDeterminant   
)

◆ StandardCommand() [8/16]

StandardCommand ( Help  ,
{ std::cout<< "\regex] List all users, optionally matching regex.\td::cout<< "\l List all databases.\";std::cout<< "\t [regex] List all tables, optionally matching regex.\";std::cout<< "\v [regex] List all views, optionally matching regex.\";std::cout<< "\d< table > List all columns of a table or a view.\";std::cout<< "\c< database >< user >< password >.\";std::cout<< "\db [database|...] Switch database. Use ... to switch to your default.\";std::cout<< "\dash List all dashboards accessible by user.\";std::cout<< "\o< table > Return a memory optimized schema based on current data " "distribution in table.\";std::cout<< "\gpu Execute in GPU mode 's.\";std::cout<< "\cpu Execute in CPU mode 's.\";std::cout<< "\multiline Set multi-line command line mode.\";std::cout<< "\singleline Set single-line command line mode.\";std::cout<< "\historylen< number > Set history buffer size(default 100).\";std::cout<< "\timing Print timing information.\";std::cout<< "\notiming Do not print timing information.\";std::cout<< "\memory_summary Print memory usage summary.\";std::cout<< "\version Print OmniSci Server version.\";std::cout<< "\copy< file path >< table > Copy data from file to table.\";std::cout<< "\status Get status of the server and the leaf nodes.\";std::cout<< "\export_dashboard< dashboard name >< filename >< optional:dashboard " "owner name > Exports a dashboard to a " "file.\";std::cout<< "\import_dashboard< dashboard name >< filename > Imports a dashboard from " "a file\";std::cout<< "\roles Reports all roles.\";std::cout<< "\role_list< userName > Reports all roles granted to user.\";std::cout<< "\privileges {< roleName >|< userName >} Reports all database objects " "privileges granted to role or " "user.\";std::cout<< "\object_privileges {database|table}< object_name > Reports all " "privileges granted to an object for all " "roles and users.\";std::cout<< "\detect {parquet}< file_name|s3_details > Reads a sample of the " "specified file and returns a CREATE TABLE statement\";std::cout<< "\clear_cpu Releases CPU memory held by OmniSci server Data Manager\";std::cout<< "\clear_gpu Releases GPU memory held by OmniSci server Data Manager\";std::cout<< "\q Quit.\";std::cout.flush();}   
)

◆ StandardCommand() [9/16]

StandardCommand ( ListDatabases  ,
{ thrift_op< kGET_DATABASES >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "Database | Owner"<< std::endl;for(auto p :lambda_context.dbinfos_return) { output_stream<< p.db_name<< " | "<< p.db_owner<< '\n';} output_stream.flush();});}   
)

◆ StandardCommand() [10/16]

StandardCommand ( ConnectToDB  ,
{ if(cmdContext().session !=INVALID_SESSION_ID) { thrift_op< kDISCONNECT >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "Disconnected from database "<< cmdContext().db_name<< std::endl;});} cmdContext().db_name=p[1];cmdContext().user_name=p[2];cmdContext().passwd=p[3];thrift_op< kCONNECT >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "User "<< lambda_context.user_name<< " connected to database "<< lambda_context.db_name<< std::endl;});}   
)

◆ StandardCommand() [11/16]

StandardCommand ( SwitchDatabase  ,
{ if(p.size()==2) { if(p[1]==std::string("...")) { cmdContext().db_name.clear();} else { cmdContext().db_name=p[1];} thrift_op< kSWITCH_DATABASE >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "User "<< lambda_context.user_name<< " switched to database "<< lambda_context.db_name<< std::endl;});} else if(p.size()==1) { thrift_op< kREPORT_DATABASE >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "User "<< lambda_context.user_name<< " is using database "<< lambda_context.db_name<< std::endl;});} }   
)

◆ StandardCommand() [12/16]

StandardCommand ( ListColumns  ,
{ decltype(p[1])&table_name(p[1]);auto unserialize_key_metainfo=[](const std::string key_metainfo) -> std::vector< std::string > { std::vector< std::string > keys_with_spec;rapidjson::Document document;document.Parse(key_metainfo.c_str());CHECK(!document.HasParseError());CHECK(document.IsArray());for(auto it=document.Begin();it !=document.End();++it) { const auto &key_with_spec_json= *it;CHECK(key_with_spec_json.IsObject());const std::string type=key_with_spec_json["type"].GetString();const std::string name=key_with_spec_json["name"].GetString();auto key_with_spec=type+" ("+name+")";if(type=="SHARED DICTIONARY") { key_with_spec+=" REFERENCES ";const std::string foreign_table=key_with_spec_json["foreign_table"].GetString();const std::string foreign_column=key_with_spec_json["foreign_column"].GetString();key_with_spec+=foreign_table+"("+foreign_column+")";} else { CHECK(type=="SHARD KEY");} keys_with_spec.push_back(key_with_spec);} return keys_with_spec;};auto on_success_lambda=[&](ContextType &context) { const auto table_details=context.table_details;if(table_details.view_sql.empty()) { std::string temp_holder(" ");if(table_details.is_temporary) { temp_holder=" TEMPORARY ";} output_stream<< "CREATE"+temp_holder+"TABLE "+table_name+" (\ else { output_stream<< "CREATE VIEW "+table_name+" AS "+table_details.view_sql<< "\";output_stream<< "\"<< "View columns:"<< "\\";} std::string comma_or_blank("");for(TColumnType p :table_details.row_desc) { if(p.is_system) { continue;} std::string encoding="";if(p.col_type.type==TDatumType::STR) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} else if(p.col_type.type==TDatumType::POINT|| p.col_type.type==TDatumType::LINESTRING|| p.col_type.type==TDatumType::POLYGON|| p.col_type.type==TDatumType::MULTIPOLYGON) { if(p.col_type.scale==4326) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} } else { encoding=(p.col_type.encoding==0 ? "" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} output_stream<< comma_or_blank<< p.col_name<< " "<< thrift_to_name(p.col_type)<<(p.col_type.nullable ? "" :" NOT NULL")<< encoding;comma_or_blank=",\";} if(table_details.view_sql.empty()) { const auto keys_with_spec=unserialize_key_metainfo(table_details.key_metainfo);for(const auto &key_with_spec :keys_with_spec) { output_stream<< ",\"<< key_with_spec;} output_stream<< ")\";comma_or_blank="";std::string frag="";std::string page="";std::string row="";std::string partition_detail="";if(DEFAULT_FRAGMENT_ROWS !=table_details.fragment_size) { frag="FRAGMENT_SIZE="+std::to_string(table_details.fragment_size);comma_or_blank=", ";} if(table_details.shard_count) { auto shard_count=table_details.shard_count;if(context.cluster_status.size() > 1) { size_t leaf_count=0;for(const auto &node :context.cluster_status) { leaf_count+=node.role==TRole::type::LEAF ? 1 :0;} shard_count *=leaf_count;} frag+=comma_or_blank+"SHARD_COUNT="+std::to_string(shard_count);comma_or_blank=", ";} if(DEFAULT_PAGE_SIZE !=table_details.page_size) { page=comma_or_blank+"PAGE_SIZE="+std::to_string(table_details.page_size);comma_or_blank=", ";} if(DEFAULT_MAX_ROWS !=table_details.max_rows) { row=comma_or_blank+"MAX_ROWS="+std::to_string(table_details.max_rows);comma_or_blank=", ";} if(table_details.partition_detail !=TPartitionDetail::DEFAULT) { partition_detail=comma_or_blank+"PARTITIONS=";switch(table_details.partition_detail) { case TPartitionDetail::REPLICATED:partition_detail+=" 'REPLICATED'";break;case TPartitionDetail::SHARDED:partition_detail+=" 'SHARDED'";break;default:partition_detail+=" 'OTHER'";break;} } std::string with=frag+page+row+partition_detail;if(with.length() > 0) { output_stream<< "WITH("<< with<< ")\";} } else { output_stream<< "\";} };thrift_op< kGET_TABLE_DETAILS >(cmdContext(), table_name.c_str(), on_success_lambda);}   
)

◆ StandardCommand() [13/16]

StandardCommand ( ExportDashboard  ,
{ decltype(p[1])&dashboard_name(p[1]);decltype(p[2])&filename(p[2]);cmdContext().view_name=dashboard_name;cmdContext().dashboard_owner=p.size()==4 ? p[3] :"";thrift_op< kGET_DASHBOARDS >(cmdContext(), [&](ContextType &lambda_context) { if(lambda_context.dash_names.size()==0) { output_stream<< "No dashboards exist."<< std::endl;return;} std::vector< TDashboard > dashboards;for(auto &dash :lambda_context.dash_names) { if(dash.dashboard_name==lambda_context.view_name) { dashboards.push_back(dash);} } if(dashboards.size()==0) { output_stream<< "Dashboard "+lambda_context.view_name+" does not exist"<< std::endl;return;} int view_id;if(dashboards.size() > 1 &&p.size()==3) { output_stream<< "Multiple dashboards with name '"+lambda_context.view_name+"' exist. To help disambiguate identical dashboards please try " "\ort_dashboard <dash_name> <exportfile> <dash_owner>."<< std::endl;return;} else if(dashboards.size() > 1 &&p.size()==4) { bool found=false;for(auto &dash :dashboards) { if(dash.dashboard_owner==lambda_context.dashboard_owner) { view_id=dash.dashboard_id;found=true;} } if(!found) { output_stream<< "No dashboard with owner name '"+lambda_context.dashboard_owner+"' found."<< std::endl;return;} } else { view_id=dashboards[0].dashboard_id;} cmdContext().dash_id=view_id;thrift_op< kGET_DASHBOARD >(cmdContext(), [&](ContextType &lambda_context) { output_stream<< "Exporting dashboard "<< lambda_context.dash_return.dashboard_name<< " to file "<< filename<< std::endl;std::ofstream dashfile;dashfile.open(filename);if(dashfile.is_open()) { dashfile<< lambda_context.dash_return.dashboard_name<< std::endl;dashfile<< lambda_context.dash_return.dashboard_metadata<< std::endl;dashfile<< mapd::decode_base64(lambda_context.dash_return.dashboard_state);dashfile.close();} else { output_stream<< "Could not open file `"<< filename<< "`"<< std::endl;} });}, [&](ContextType &) { output_stream<< "Failed to export dashboard."<< std::endl;});}   
)

◆ StandardCommand() [14/16]

StandardCommand ( ImportDashboard  ,
{ decltype(p[1])&dashboard_name(p[1]);decltype(p[2])&filename(p[2]);cmdContext().view_name=dashboard_name;cmdContext().view_state="";cmdContext().view_metadata="";std::ifstream dashfile;std::string state;std::string old_name;dashfile.open(filename);if(dashfile.is_open()) { std::getline(dashfile, old_name);std::getline(dashfile, cmdContext().view_metadata);std::getline(dashfile, state);dashfile.close();} else { output_stream<< "Could not open file `"<< filename<< "`"<< std::endl;return;} if(!replace(state, std::string("\le\old_name+"\,"), std::string("\title\:\"+cmdContext().view_name+"\,"))) { output_stream<< "Failed to update title."<< std::endl;return;} cmdContext().view_state=mapd::encode_base64(state);output_stream<< "Importing dashboard "<< cmdContext().view_name<< " from file "<< filename<< std::endl;thrift_op< kIMPORT_DASHBOARD >(cmdContext(), DoNothing(), [&](ContextType &) { output_stream<< "Failed to import dashboard."<< std::endl; });}   
)

◆ StandardCommand() [15/16]

StandardCommand ( GetOptimizedSchema  ,
{ decltype(p[1])&table_name(p[1]);auto get_row_count=[](const TQueryResult &query_result) -> size_t { CHECK(!query_result.row_set.row_desc.empty());if(query_result.row_set.columns.empty()) { return 0;} CHECK_EQ(query_result.row_set.columns.size(), query_result.row_set.row_desc.size());return query_result.row_set.columns.front().nulls.size();};auto run_query=[get_row_count](ContextType &context, std::string query) -> int { thrift_op< kSQL >(context, query.c_str());CHECK(get_row_count(context.query_return));return context.query_return.row_set.columns[0].data.int_col[0];};auto get_optimal_size=[run_query](ClientContext &context, std::string table_name, std::string col_name, int col_type) -> int { switch(col_type) { case TDatumType::STR:{ int strings=run_query(context, "select count(distinct "+col_name+") from "+table_name+";");if(strings< pow(2, 8)) { return 8;} else { if(strings< pow(2, 16)) { return 16;} else { return 32;} } } case TDatumType::TIME:{ return 32;} case TDatumType::DATE:case TDatumType::TIMESTAMP:{ return run_query(context, "select case when (extract( epoch from mn) > -2147483648 and " "extract (epoch from mx) < " "2147483647) then 32 else 0 end from (select min("+col_name+") mn, max("+col_name+") mx from "+table_name+" );");} case TDatumType::BIGINT:{ return run_query(context, "select case when (mn > -128 and mx < 127) then 8 else case " "when (mn > -32768 and mx < 32767) " "then 16 else case when (mn > -2147483648 and mx < 2147483647) " "then 32 else 0 end end end from " "(select min("+col_name+") mn, max("+col_name+") mx from "+table_name+" );");} case TDatumType::INT:{ return run_query(context, "select case when (mn > -128 and mx < 127) then 8 else case " "when (mn > -32768 and mx < 32767) " "then 16 else 0 end end from " "(select min("+col_name+") mn, max("+col_name+") mx from "+table_name+" );");} } return 0;};auto on_success_lambda=[&](ContextType &context) { const auto table_details=context.table_details;if(table_details.view_sql.empty()) { output_stream<< "CREATE TABLE "+table_name+" (\ else { std::cerr<< "Can 't optimize a view, only the underlying tables\";return;} std::string comma_or_blank("");for(TColumnType p :table_details.row_desc) { std::string encoding="";if(p.col_type.type==TDatumType::STR) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(get_optimal_size( context, table_name, p.col_name, p.col_type.type))+ ")");} else if(p.col_type.type==TDatumType::POINT|| p.col_type.type==TDatumType::LINESTRING|| p.col_type.type==TDatumType::POLYGON|| p.col_type.type==TDatumType::MULTIPOLYGON) { if(p.col_type.scale==4326) { encoding=(p.col_type.encoding==0 ? " ENCODING NONE" :" ENCODING "+thrift_to_encoding_name(p.col_type)+"("+ std::to_string(p.col_type.comp_param)+")");} } else { int opt_size=get_optimal_size(context, table_name, p.col_name, p.col_type.type);encoding=(opt_size==0 ? "" :" ENCODING FIXED("+std::to_string(opt_size)+")");} output_stream<< comma_or_blank<< p.col_name<< " "<< thrift_to_name(p.col_type)<<(p.col_type.nullable ? "" :" NOT NULL")<< encoding;comma_or_blank=",\";} if(table_details.view_sql.empty()) { output_stream<< ")\";comma_or_blank="";std::string frag="";std::string page="";std::string row="";if(DEFAULT_FRAGMENT_ROWS !=table_details.fragment_size) { frag=" FRAGMENT_SIZE="+std::to_string(table_details.fragment_size);comma_or_blank=",";} if(DEFAULT_PAGE_SIZE !=table_details.page_size) { page=comma_or_blank+" PAGE_SIZE="+std::to_string(table_details.page_size);comma_or_blank=",";} if(DEFAULT_MAX_ROWS !=table_details.max_rows) { row=comma_or_blank+" MAX_ROWS="+std::to_string(table_details.max_rows);} std::string with=frag+page+row;if(with.length() > 0) { output_stream<< "WITH("<< with<< ")\";} } else { output_stream<< "\";} };thrift_op< kGET_TABLE_DETAILS >(cmdContext(), table_name.c_str(), on_success_lambda);}   
)

◆ StandardCommand() [16/16]

StandardCommand ( ListDashboards  ,
ContextOps::get_dashboards(cmdContext())   
)