OmniSciDB  29e35f4d58
Parser::ExportQueryStmt Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::ExportQueryStmt:
+ Collaboration diagram for Parser::ExportQueryStmt:

Public Member Functions

 ExportQueryStmt (std::string *q, std::string *p, std::list< NameValueAssign *> *o)
 
void execute (const Catalog_Namespace::SessionInfo &session) override
 
const std::string get_select_stmt () const
 
- 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

std::unique_ptr< std::string > select_stmt
 
std::unique_ptr< std::string > file_path
 
std::list< std::unique_ptr< NameValueAssign > > options
 

Detailed Description

Definition at line 1745 of file ParserNode.h.

Constructor & Destructor Documentation

◆ ExportQueryStmt()

Parser::ExportQueryStmt::ExportQueryStmt ( std::string *  q,
std::string *  p,
std::list< NameValueAssign *> *  o 
)
inline

Definition at line 1747 of file ParserNode.h.

1748  : select_stmt(q), file_path(p) {
1749  if (o) {
1750  for (const auto e : *o) {
1751  options.emplace_back(e);
1752  }
1753  delete o;
1754  }
1755  }
std::unique_ptr< std::string > file_path
Definition: ParserNode.h:1761
std::list< std::unique_ptr< NameValueAssign > > options
Definition: ParserNode.h:1762
std::unique_ptr< std::string > select_stmt
Definition: ParserNode.h:1760

Member Function Documentation

◆ execute()

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

Implements Parser::DDLStmt.

Definition at line 4139 of file ParserNode.cpp.

References Parser::anonymous_namespace{ParserNode.cpp}::bool_from_string_literal(), CHECK, CHECK_EQ, query_state::QueryState::create(), datum_to_string(), Importer_NS::CopyParams::delimiter, Importer_NS::CopyParams::escape, Catalog_Namespace::SessionInfo::get_session_id(), Parser::StringLiteral::get_stringval(), Catalog_Namespace::SessionInfo::getCatalog(), Parser::getResultSet(), Importer_NS::HAS_HEADER, Importer_NS::CopyParams::has_header, anonymous_namespace{TypedDataAccessors.h}::is_null(), kBIGINT, kBOOLEAN, kDATE, kFLOAT, kINT, kNUMERIC, kSMALLINT, kTIME, kTIMESTAMP, kTINYINT, Importer_NS::CopyParams::line_delim, Importer_NS::NO_HEADER, NULL_BIGINT, NULL_BOOLEAN, NULL_DOUBLE, NULL_FLOAT, NULL_INT, NULL_SMALLINT, Importer_NS::CopyParams::null_str, NULL_TINYINT, Importer_NS::CopyParams::quote, Importer_NS::CopyParams::quoted, STDLOG, and to_string().

4139  {
4140  auto session_copy = session;
4141  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
4142  &session_copy, boost::null_deleter());
4144  auto stdlog = STDLOG(query_state);
4145  if (SysCatalog::instance().isAggregator()) {
4146  // allow copy to statement for stand alone leafs
4147  throw std::runtime_error("Distributed export not supported yet");
4148  }
4149  auto& catalog = session.getCatalog();
4150  Importer_NS::CopyParams copy_params;
4151  if (!options.empty()) {
4152  for (auto& p : options) {
4153  if (boost::iequals(*p->get_name(), "delimiter")) {
4154  const StringLiteral* str_literal =
4155  dynamic_cast<const StringLiteral*>(p->get_value());
4156  if (str_literal == nullptr) {
4157  throw std::runtime_error("Delimiter option must be a string.");
4158  } else if (str_literal->get_stringval()->length() != 1) {
4159  throw std::runtime_error("Delimiter must be a single character string.");
4160  }
4161  copy_params.delimiter = (*str_literal->get_stringval())[0];
4162  } else if (boost::iequals(*p->get_name(), "nulls")) {
4163  const StringLiteral* str_literal =
4164  dynamic_cast<const StringLiteral*>(p->get_value());
4165  if (str_literal == nullptr) {
4166  throw std::runtime_error("Nulls option must be a string.");
4167  }
4168  copy_params.null_str = *str_literal->get_stringval();
4169  } else if (boost::iequals(*p->get_name(), "header")) {
4170  const StringLiteral* str_literal =
4171  dynamic_cast<const StringLiteral*>(p->get_value());
4172  if (str_literal == nullptr) {
4173  throw std::runtime_error("Header option must be a boolean.");
4174  }
4175  copy_params.has_header = bool_from_string_literal(str_literal)
4178  } else if (boost::iequals(*p->get_name(), "quote")) {
4179  const StringLiteral* str_literal =
4180  dynamic_cast<const StringLiteral*>(p->get_value());
4181  if (str_literal == nullptr) {
4182  throw std::runtime_error("Quote option must be a string.");
4183  } else if (str_literal->get_stringval()->length() != 1) {
4184  throw std::runtime_error("Quote must be a single character string.");
4185  }
4186  copy_params.quote = (*str_literal->get_stringval())[0];
4187  } else if (boost::iequals(*p->get_name(), "escape")) {
4188  const StringLiteral* str_literal =
4189  dynamic_cast<const StringLiteral*>(p->get_value());
4190  if (str_literal == nullptr) {
4191  throw std::runtime_error("Escape option must be a string.");
4192  } else if (str_literal->get_stringval()->length() != 1) {
4193  throw std::runtime_error("Escape must be a single character string.");
4194  }
4195  copy_params.escape = (*str_literal->get_stringval())[0];
4196  } else if (boost::iequals(*p->get_name(), "line_delimiter")) {
4197  const StringLiteral* str_literal =
4198  dynamic_cast<const StringLiteral*>(p->get_value());
4199  if (str_literal == nullptr) {
4200  throw std::runtime_error("Line_delimiter option must be a string.");
4201  } else if (str_literal->get_stringval()->length() != 1) {
4202  throw std::runtime_error("Line_delimiter must be a single character string.");
4203  }
4204  copy_params.line_delim = (*str_literal->get_stringval())[0];
4205  } else if (boost::iequals(*p->get_name(), "quoted")) {
4206  const StringLiteral* str_literal =
4207  dynamic_cast<const StringLiteral*>(p->get_value());
4208  if (str_literal == nullptr) {
4209  throw std::runtime_error("Quoted option must be a boolean.");
4210  }
4211  copy_params.quoted = bool_from_string_literal(str_literal);
4212  } else {
4213  throw std::runtime_error("Invalid option for COPY: " + *p->get_name());
4214  }
4215  }
4216  }
4217  std::vector<TargetMetaInfo> targets;
4218  const auto results =
4219  getResultSet(query_state->createQueryStateProxy(), *select_stmt, targets);
4220  TargetMetaInfo* td = targets.data();
4221 
4222  std::ofstream outfile;
4223  if (file_path->empty() || !boost::filesystem::path(*file_path).is_absolute()) {
4224  std::string file_name;
4225  if (file_path->empty()) {
4226  file_name = session.get_session_id() + ".txt";
4227  } else {
4228  file_name = boost::filesystem::path(*file_path).filename().string();
4229  }
4230  *file_path = catalog.getBasePath() + "/mapd_export/" + session.get_session_id() + "/";
4231  if (!boost::filesystem::exists(*file_path)) {
4232  if (!boost::filesystem::create_directories(*file_path)) {
4233  throw std::runtime_error("Directory " + *file_path + " cannot be created.");
4234  }
4235  }
4236  *file_path += file_name;
4237  }
4238  outfile.open(*file_path);
4239  if (!outfile) {
4240  throw std::runtime_error("Cannot open file: " + *file_path);
4241  }
4243  bool not_first = false;
4244  size_t i = 0;
4245  for (const auto& target : targets) {
4246  std::string col_name = target.get_resname();
4247  if (col_name.empty()) {
4248  col_name = "result_" + std::to_string(i + 1);
4249  }
4250  if (not_first) {
4251  outfile << copy_params.delimiter;
4252  } else {
4253  not_first = true;
4254  }
4255  outfile << col_name;
4256  ++i;
4257  }
4258  outfile << copy_params.line_delim;
4259  }
4260  while (true) {
4261  const auto crt_row = results->getNextRow(true, true);
4262  if (crt_row.empty()) {
4263  break;
4264  }
4265  bool not_first = false;
4266  for (size_t i = 0; i < results->colCount(); ++i) {
4267  bool is_null;
4268  const auto tv = crt_row[i];
4269  const auto scalar_tv = boost::get<ScalarTargetValue>(&tv);
4270  if (not_first) {
4271  outfile << copy_params.delimiter;
4272  } else {
4273  not_first = true;
4274  }
4275  if (copy_params.quoted) {
4276  outfile << copy_params.quote;
4277  }
4278  const auto& ti = td[i].get_type_info();
4279  if (!scalar_tv) {
4280  outfile << datum_to_string(crt_row[i], ti, " | ");
4281  if (copy_params.quoted) {
4282  outfile << copy_params.quote;
4283  }
4284  continue;
4285  }
4286  if (boost::get<int64_t>(scalar_tv)) {
4287  auto int_val = *(boost::get<int64_t>(scalar_tv));
4288  switch (ti.get_type()) {
4289  case kBOOLEAN:
4290  is_null = (int_val == NULL_BOOLEAN);
4291  break;
4292  case kTINYINT:
4293  is_null = (int_val == NULL_TINYINT);
4294  break;
4295  case kSMALLINT:
4296  is_null = (int_val == NULL_SMALLINT);
4297  break;
4298  case kINT:
4299  is_null = (int_val == NULL_INT);
4300  break;
4301  case kBIGINT:
4302  is_null = (int_val == NULL_BIGINT);
4303  break;
4304  case kTIME:
4305  case kTIMESTAMP:
4306  case kDATE:
4307  is_null = (int_val == NULL_BIGINT);
4308  break;
4309  default:
4310  is_null = false;
4311  }
4312  if (is_null) {
4313  outfile << copy_params.null_str;
4314  } else if (ti.get_type() == kTIME) {
4315  const auto t = static_cast<time_t>(int_val);
4316  std::tm tm_struct;
4317  gmtime_r(&t, &tm_struct);
4318  char buf[9];
4319  strftime(buf, 9, "%T", &tm_struct);
4320  outfile << buf;
4321  } else {
4322  outfile << int_val;
4323  }
4324  } else if (boost::get<double>(scalar_tv)) {
4325  auto real_val = *(boost::get<double>(scalar_tv));
4326  if (ti.get_type() == kFLOAT) {
4327  is_null = (real_val == NULL_FLOAT);
4328  } else {
4329  is_null = (real_val == NULL_DOUBLE);
4330  }
4331  if (is_null) {
4332  outfile << copy_params.null_str;
4333  } else if (ti.get_type() == kNUMERIC) {
4334  outfile << std::setprecision(ti.get_precision()) << real_val;
4335  } else {
4336  outfile << std::setprecision(std::numeric_limits<double>::digits10 + 1)
4337  << real_val;
4338  }
4339  } else if (boost::get<float>(scalar_tv)) {
4340  CHECK_EQ(kFLOAT, ti.get_type());
4341  auto real_val = *(boost::get<float>(scalar_tv));
4342  if (real_val == NULL_FLOAT) {
4343  outfile << copy_params.null_str;
4344  } else {
4345  outfile << std::setprecision(std::numeric_limits<float>::digits10 + 1)
4346  << real_val;
4347  }
4348  } else {
4349  auto s = boost::get<NullableString>(scalar_tv);
4350  is_null = !s || boost::get<void*>(s);
4351  if (is_null) {
4352  outfile << copy_params.null_str;
4353  } else {
4354  auto s_notnull = boost::get<std::string>(s);
4355  CHECK(s_notnull);
4356  if (!copy_params.quoted) {
4357  outfile << *s_notnull;
4358  } else {
4359  size_t q = s_notnull->find(copy_params.quote);
4360  if (q == std::string::npos) {
4361  outfile << *s_notnull;
4362  } else {
4363  std::string str(*s_notnull);
4364  while (q != std::string::npos) {
4365  str.insert(q, 1, copy_params.escape);
4366  q = str.find(copy_params.quote, q + 2);
4367  }
4368  outfile << str;
4369  }
4370  }
4371  }
4372  }
4373  if (copy_params.quoted) {
4374  outfile << copy_params.quote;
4375  }
4376  }
4377  outfile << copy_params.line_delim;
4378  }
4379  outfile.close();
4380 }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
#define NULL_DOUBLE
Definition: sqltypes.h:179
std::string null_str
Definition: CopyParams.h:47
Definition: sqltypes.h:52
#define NULL_BIGINT
Definition: sqltypes.h:177
std::string datum_to_string(const TargetValue &tv, const SQLTypeInfo &ti, const std::string &delim)
std::shared_ptr< ResultSet > getResultSet(QueryStateProxy query_state_proxy, const std::string select_stmt, std::vector< TargetMetaInfo > &targets, bool validate_only=false)
std::string get_session_id() const
Definition: SessionInfo.h:98
bool bool_from_string_literal(const Parser::StringLiteral *str_literal)
Definition: ParserNode.cpp:892
std::unique_ptr< std::string > file_path
Definition: ParserNode.h:1761
std::string to_string(char const *&&v)
#define NULL_TINYINT
Definition: sqltypes.h:174
Catalog & getCatalog() const
Definition: SessionInfo.h:90
#define NULL_FLOAT
Definition: sqltypes.h:178
static std::shared_ptr< QueryState > create(ARGS &&... args)
Definition: QueryState.h:140
ImportHeaderRow has_header
Definition: CopyParams.h:48
#define NULL_INT
Definition: sqltypes.h:176
Definition: sqltypes.h:56
bool is_null(const T &v, const SQLTypeInfo &t)
std::list< std::unique_ptr< NameValueAssign > > options
Definition: ParserNode.h:1762
#define NULL_SMALLINT
Definition: sqltypes.h:175
#define CHECK(condition)
Definition: Logger.h:193
Definition: sqltypes.h:48
std::unique_ptr< std::string > select_stmt
Definition: ParserNode.h:1760
#define NULL_BOOLEAN
Definition: sqltypes.h:173
#define STDLOG(...)
Definition: QueryState.h:225
+ Here is the call graph for this function:

◆ get_select_stmt()

const std::string Parser::ExportQueryStmt::get_select_stmt ( ) const
inline

Definition at line 1757 of file ParserNode.h.

1757 { return *select_stmt; }
std::unique_ptr< std::string > select_stmt
Definition: ParserNode.h:1760

Member Data Documentation

◆ file_path

std::unique_ptr<std::string> Parser::ExportQueryStmt::file_path
private

Definition at line 1761 of file ParserNode.h.

◆ options

std::list<std::unique_ptr<NameValueAssign> > Parser::ExportQueryStmt::options
private

Definition at line 1762 of file ParserNode.h.

◆ select_stmt

std::unique_ptr<std::string> Parser::ExportQueryStmt::select_stmt
private

Definition at line 1760 of file ParserNode.h.


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