OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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

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

Definition at line 1747 of file ParserNode.h.

References options.

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

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

Implements Parser::DDLStmt.

Definition at line 4140 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().

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

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

Definition at line 1757 of file ParserNode.h.

References select_stmt.

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

Member Data Documentation

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

Definition at line 1761 of file ParserNode.h.

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

Definition at line 1762 of file ParserNode.h.

Referenced by ExportQueryStmt().

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

Definition at line 1760 of file ParserNode.h.

Referenced by get_select_stmt().


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