OmniSciDB  06b3bd477c
 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 ()
 

Public Attributes

DistributedConnectorleafs_connector_ = nullptr
 

Private Member Functions

void parseOptions (import_export::CopyParams &copy_params, import_export::QueryExporter::FileType &file_type, std::string &layer_name, import_export::QueryExporter::FileCompression &file_compression, import_export::QueryExporter::ArrayNullHandling &array_null_handling)
 

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

Constructor & Destructor Documentation

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

Definition at line 1808 of file ParserNode.h.

References options.

1809  : select_stmt(q), file_path(p) {
1810  if (o) {
1811  for (const auto e : *o) {
1812  options.emplace_back(e);
1813  }
1814  delete o;
1815  }
1816  }
std::unique_ptr< std::string > file_path
Definition: ParserNode.h:1824
std::list< std::unique_ptr< NameValueAssign > > options
Definition: ParserNode.h:1825
std::unique_ptr< std::string > select_stmt
Definition: ParserNode.h:1823

Member Function Documentation

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

Implements Parser::DDLStmt.

Definition at line 4350 of file ParserNode.cpp.

References import_export::QueryExporter::create(), query_state::QueryState::create(), ddl_utils::EXPORT, Catalog_Namespace::SessionInfo::get_session_id(), Catalog_Namespace::SessionInfo::getCatalog(), import_export::QueryExporter::kAbortWithWarning, import_export::QueryExporter::kCSV, import_export::QueryExporter::kNone, Parser::LocalConnector::query(), STDLOG, and ddl_utils::validate_allowed_file_path().

4350  {
4351  auto session_copy = session;
4352  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
4353  &session_copy, boost::null_deleter());
4354  auto query_state = query_state::QueryState::create(session_ptr, *select_stmt);
4355  auto stdlog = STDLOG(query_state);
4356  auto query_state_proxy = query_state->createQueryStateProxy();
4357 
4358  auto& catalog = session.getCatalog();
4359 
4360  LocalConnector local_connector;
4361 
4362  if (!leafs_connector_) {
4363  leafs_connector_ = &local_connector;
4364  }
4365 
4366  import_export::CopyParams copy_params;
4367  // @TODO(se) move rest to CopyParams when we have a Thrift endpoint
4370  std::string layer_name;
4375 
4376  parseOptions(copy_params, file_type, layer_name, file_compression, array_null_handling);
4377 
4378  if (file_path->empty()) {
4379  throw std::runtime_error("Invalid file path for COPY TO");
4380  } else if (!boost::filesystem::path(*file_path).is_absolute()) {
4381  std::string file_name = boost::filesystem::path(*file_path).filename().string();
4382  std::string file_dir =
4383  catalog.getBasePath() + "/mapd_export/" + session.get_session_id() + "/";
4384  if (!boost::filesystem::exists(file_dir)) {
4385  if (!boost::filesystem::create_directories(file_dir)) {
4386  throw std::runtime_error("Directory " + file_dir + " cannot be created.");
4387  }
4388  }
4389  *file_path = file_dir + file_name;
4390  } else {
4391  // Above branch will create a new file in the mapd_export directory. If that path is
4392  // not exercised, go through applicable file path validations.
4395  }
4396 
4397  // get column info
4398  auto column_info_result =
4399  local_connector.query(query_state_proxy, *select_stmt, {}, true);
4400 
4401  // create exporter for requested file type
4402  auto query_exporter = import_export::QueryExporter::create(file_type);
4403 
4404  // default layer name to file path stem if it wasn't specified
4405  if (layer_name.size() == 0) {
4406  layer_name = boost::filesystem::path(*file_path).stem().string();
4407  }
4408 
4409  // begin export
4410  query_exporter->beginExport(*file_path,
4411  layer_name,
4412  copy_params,
4413  column_info_result.targets_meta,
4414  file_compression,
4415  array_null_handling);
4416 
4417  // how many fragments?
4418  size_t outer_frag_count =
4419  leafs_connector_->getOuterFragmentCount(query_state_proxy, *select_stmt);
4420  size_t outer_frag_end = outer_frag_count == 0 ? 1 : outer_frag_count;
4421 
4422  // loop fragments
4423  for (size_t outer_frag_idx = 0; outer_frag_idx < outer_frag_end; outer_frag_idx++) {
4424  // limit the query to just this fragment
4425  std::vector<size_t> allowed_outer_fragment_indices;
4426  if (outer_frag_count) {
4427  allowed_outer_fragment_indices.push_back(outer_frag_idx);
4428  }
4429 
4430  // run the query
4431  std::vector<AggregatedResult> query_results = leafs_connector_->query(
4432  query_state_proxy, *select_stmt, allowed_outer_fragment_indices);
4433 
4434  // export the results
4435  query_exporter->exportResults(query_results);
4436  }
4437 
4438  // end export
4439  query_exporter->endExport();
4440 }
virtual std::vector< AggregatedResult > query(QueryStateProxy, std::string &sql_query_string, std::vector< size_t > outer_frag_indices)=0
void parseOptions(import_export::CopyParams &copy_params, import_export::QueryExporter::FileType &file_type, std::string &layer_name, import_export::QueryExporter::FileCompression &file_compression, import_export::QueryExporter::ArrayNullHandling &array_null_handling)
static std::shared_ptr< QueryState > create(ARGS &&...args)
Definition: QueryState.h:140
DistributedConnector * leafs_connector_
Definition: ParserNode.h:1820
void validate_allowed_file_path(const std::string &file_path, const DataTransferType data_transfer_type)
Definition: DdlUtils.cpp:611
static std::unique_ptr< QueryExporter > create(const FileType file_type)
std::unique_ptr< std::string > file_path
Definition: ParserNode.h:1824
virtual size_t getOuterFragmentCount(QueryStateProxy, std::string &sql_query_string)=0
std::string get_session_id() const
Definition: SessionInfo.h:73
Catalog & getCatalog() const
Definition: SessionInfo.h:65
std::unique_ptr< std::string > select_stmt
Definition: ParserNode.h:1823
#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 1818 of file ParserNode.h.

References select_stmt.

Referenced by DBHandler::sql_execute_impl().

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

+ Here is the caller graph for this function:

void Parser::ExportQueryStmt::parseOptions ( import_export::CopyParams copy_params,
import_export::QueryExporter::FileType file_type,
std::string &  layer_name,
import_export::QueryExporter::FileCompression file_compression,
import_export::QueryExporter::ArrayNullHandling array_null_handling 
)
private

Definition at line 4442 of file ParserNode.cpp.

References Parser::anonymous_namespace{ParserNode.cpp}::bool_from_string_literal(), import_export::CopyParams::delimiter, import_export::CopyParams::escape, Parser::StringLiteral::get_stringval(), import_export::HAS_HEADER, import_export::CopyParams::has_header, import_export::QueryExporter::kAbortWithWarning, import_export::QueryExporter::kCSV, import_export::QueryExporter::kExportSentinels, import_export::QueryExporter::kExportZeros, import_export::QueryExporter::kGeoJSON, import_export::QueryExporter::kGeoJSONL, import_export::QueryExporter::kGZip, import_export::QueryExporter::kNone, import_export::QueryExporter::kNullEntireField, import_export::QueryExporter::kShapefile, import_export::QueryExporter::kZip, import_export::CopyParams::line_delim, import_export::NO_HEADER, import_export::CopyParams::null_str, import_export::CopyParams::quote, and import_export::CopyParams::quoted.

4447  {
4448  // defaults for non-CopyParams values
4450  layer_name.clear();
4452 
4453  if (!options.empty()) {
4454  for (auto& p : options) {
4455  if (boost::iequals(*p->get_name(), "delimiter")) {
4456  const StringLiteral* str_literal =
4457  dynamic_cast<const StringLiteral*>(p->get_value());
4458  if (str_literal == nullptr) {
4459  throw std::runtime_error("Delimiter option must be a string.");
4460  } else if (str_literal->get_stringval()->length() != 1) {
4461  throw std::runtime_error("Delimiter must be a single character string.");
4462  }
4463  copy_params.delimiter = (*str_literal->get_stringval())[0];
4464  } else if (boost::iequals(*p->get_name(), "nulls")) {
4465  const StringLiteral* str_literal =
4466  dynamic_cast<const StringLiteral*>(p->get_value());
4467  if (str_literal == nullptr) {
4468  throw std::runtime_error("Nulls option must be a string.");
4469  }
4470  copy_params.null_str = *str_literal->get_stringval();
4471  } else if (boost::iequals(*p->get_name(), "header")) {
4472  const StringLiteral* str_literal =
4473  dynamic_cast<const StringLiteral*>(p->get_value());
4474  if (str_literal == nullptr) {
4475  throw std::runtime_error("Header option must be a boolean.");
4476  }
4477  copy_params.has_header = bool_from_string_literal(str_literal)
4480  } else if (boost::iequals(*p->get_name(), "quote")) {
4481  const StringLiteral* str_literal =
4482  dynamic_cast<const StringLiteral*>(p->get_value());
4483  if (str_literal == nullptr) {
4484  throw std::runtime_error("Quote option must be a string.");
4485  } else if (str_literal->get_stringval()->length() != 1) {
4486  throw std::runtime_error("Quote must be a single character string.");
4487  }
4488  copy_params.quote = (*str_literal->get_stringval())[0];
4489  } else if (boost::iequals(*p->get_name(), "escape")) {
4490  const StringLiteral* str_literal =
4491  dynamic_cast<const StringLiteral*>(p->get_value());
4492  if (str_literal == nullptr) {
4493  throw std::runtime_error("Escape option must be a string.");
4494  } else if (str_literal->get_stringval()->length() != 1) {
4495  throw std::runtime_error("Escape must be a single character string.");
4496  }
4497  copy_params.escape = (*str_literal->get_stringval())[0];
4498  } else if (boost::iequals(*p->get_name(), "line_delimiter")) {
4499  const StringLiteral* str_literal =
4500  dynamic_cast<const StringLiteral*>(p->get_value());
4501  if (str_literal == nullptr) {
4502  throw std::runtime_error("Line_delimiter option must be a string.");
4503  } else if (str_literal->get_stringval()->length() != 1) {
4504  throw std::runtime_error("Line_delimiter must be a single character string.");
4505  }
4506  copy_params.line_delim = (*str_literal->get_stringval())[0];
4507  } else if (boost::iequals(*p->get_name(), "quoted")) {
4508  const StringLiteral* str_literal =
4509  dynamic_cast<const StringLiteral*>(p->get_value());
4510  if (str_literal == nullptr) {
4511  throw std::runtime_error("Quoted option must be a boolean.");
4512  }
4513  copy_params.quoted = bool_from_string_literal(str_literal);
4514  } else if (boost::iequals(*p->get_name(), "file_type")) {
4515  const StringLiteral* str_literal =
4516  dynamic_cast<const StringLiteral*>(p->get_value());
4517  if (str_literal == nullptr) {
4518  throw std::runtime_error("File Type option must be a string.");
4519  }
4520  auto file_type_str =
4521  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
4522  if (file_type_str == "csv") {
4524  } else if (file_type_str == "geojson") {
4526  } else if (file_type_str == "geojsonl") {
4528  } else if (file_type_str == "shapefile") {
4530  } else {
4531  throw std::runtime_error(
4532  "File Type option must be 'CSV', 'GeoJSON', 'GeoJSONL' or 'Shapefile'");
4533  }
4534  } else if (boost::iequals(*p->get_name(), "layer_name")) {
4535  const StringLiteral* str_literal =
4536  dynamic_cast<const StringLiteral*>(p->get_value());
4537  if (str_literal == nullptr) {
4538  throw std::runtime_error("Layer Name option must be a string.");
4539  }
4540  layer_name = *str_literal->get_stringval();
4541  } else if (boost::iequals(*p->get_name(), "file_compression")) {
4542  const StringLiteral* str_literal =
4543  dynamic_cast<const StringLiteral*>(p->get_value());
4544  if (str_literal == nullptr) {
4545  throw std::runtime_error("File Compression option must be a string.");
4546  }
4547  auto file_compression_str =
4548  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
4549  if (file_compression_str == "none") {
4551  } else if (file_compression_str == "gzip") {
4553  } else if (file_compression_str == "zip") {
4555  } else {
4556  throw std::runtime_error(
4557  "File Compression option must be 'None', 'GZip', or 'Zip'");
4558  }
4559  } else if (boost::iequals(*p->get_name(), "array_null_handling")) {
4560  const StringLiteral* str_literal =
4561  dynamic_cast<const StringLiteral*>(p->get_value());
4562  if (str_literal == nullptr) {
4563  throw std::runtime_error("Array Null Handling option must be a string.");
4564  }
4565  auto array_null_handling_str =
4566  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
4567  if (array_null_handling_str == "abort") {
4568  array_null_handling =
4570  } else if (array_null_handling_str == "raw") {
4571  array_null_handling =
4573  } else if (array_null_handling_str == "zero") {
4574  array_null_handling =
4576  } else if (array_null_handling_str == "nullfield") {
4577  array_null_handling =
4579  } else {
4580  throw std::runtime_error(
4581  "Array Null Handling option must be 'Abort', 'Raw', 'Zero', or "
4582  "'NullField'");
4583  }
4584  } else {
4585  throw std::runtime_error("Invalid option for COPY: " + *p->get_name());
4586  }
4587  }
4588  }
4589 }
bool bool_from_string_literal(const Parser::StringLiteral *str_literal)
Definition: ParserNode.cpp:909
ImportHeaderRow has_header
Definition: CopyParams.h:48
std::list< std::unique_ptr< NameValueAssign > > options
Definition: ParserNode.h:1825

+ Here is the call graph for this function:

Member Data Documentation

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

Definition at line 1824 of file ParserNode.h.

DistributedConnector* Parser::ExportQueryStmt::leafs_connector_ = nullptr

Definition at line 1820 of file ParserNode.h.

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

Definition at line 1825 of file ParserNode.h.

Referenced by ExportQueryStmt().

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

Definition at line 1823 of file ParserNode.h.

Referenced by get_select_stmt().


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