OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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)
 
 ExportQueryStmt (const rapidjson::Value &payload)
 
void execute (const Catalog_Namespace::SessionInfo &session, bool read_only_mode) 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

std::unique_ptr< QueryConnectorleafs_connector_
 

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

Constructor & Destructor Documentation

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

Definition at line 1837 of file ParserNode.h.

References options_.

1838  : select_stmt_(q), file_path_(p) {
1839  if (o) {
1840  for (const auto e : *o) {
1841  options_.emplace_back(e);
1842  }
1843  delete o;
1844  }
1845  }
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1855
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1854
std::list< std::unique_ptr< NameValueAssign > > options_
Definition: ParserNode.h:1856
Parser::ExportQueryStmt::ExportQueryStmt ( const rapidjson::Value &  payload)

Definition at line 6440 of file ParserNode.cpp.

References CHECK, file_path_, json_str(), options_, Parser::anonymous_namespace{ParserNode.cpp}::parse_options(), and select_stmt_.

6440  {
6441  CHECK(payload.HasMember("filePath"));
6442  file_path_ = std::make_unique<std::string>(json_str(payload["filePath"]));
6443 
6444  CHECK(payload.HasMember("query"));
6445  select_stmt_ = std::make_unique<std::string>(json_str(payload["query"]));
6446 
6447  if ((*select_stmt_).back() != ';') {
6448  (*select_stmt_).push_back(';');
6449  }
6450  // Export wrapped everything with ` quotes which need cleanup
6451  boost::replace_all((*select_stmt_), "`", "");
6452 
6453  parse_options(payload, options_);
6454 }
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:46
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1855
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1854
void parse_options(const rapidjson::Value &payload, std::list< std::unique_ptr< NameValueAssign >> &nameValueList, bool stringToNull=false, bool stringToInteger=false)
std::list< std::unique_ptr< NameValueAssign > > options_
Definition: ParserNode.h:1856
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the call graph for this function:

Member Function Documentation

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

Implements Parser::DDLStmt.

Definition at line 6456 of file ParserNode.cpp.

References Parser::anonymous_namespace{ParserNode.cpp}::acquire_query_table_locks(), import_export::QueryExporter::create(), query_state::QueryState::create(), query_state::QueryState::createQueryStateProxy(), ddl_utils::EXPORT, file_path_, g_base_path, Catalog_Namespace::SessionInfo::get_session_id(), legacylockmgr::getExecuteReadLock(), import_export::QueryExporter::kAbortWithWarning, import_export::QueryExporter::kCSV, shared::kDefaultExportDirName, import_export::QueryExporter::kNone, leafs_connector_, parseOptions(), Parser::LocalQueryConnector::query(), select_stmt_, STDLOG, and ddl_utils::validate_allowed_file_path().

Referenced by heavydb.cursor.Cursor::executemany().

6457  {
6458  // valid in read_only_mode
6459  auto session_copy = session;
6460  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
6461  &session_copy, boost::null_deleter());
6462  auto query_state = query_state::QueryState::create(session_ptr, *select_stmt_);
6463  auto stdlog = STDLOG(query_state);
6464  auto query_state_proxy = query_state->createQueryStateProxy();
6465 
6466  if (!leafs_connector_) {
6467  leafs_connector_ = std::make_unique<LocalQueryConnector>();
6468  }
6469 
6470  import_export::CopyParams copy_params;
6471  // @TODO(se) move rest to CopyParams when we have a Thrift endpoint
6474  std::string layer_name;
6479 
6480  parseOptions(copy_params, file_type, layer_name, file_compression, array_null_handling);
6481 
6482  if (file_path_->empty()) {
6483  throw std::runtime_error("Invalid file path for COPY TO");
6484  } else if (!boost::filesystem::path(*file_path_).is_absolute()) {
6485  std::string file_name = boost::filesystem::path(*file_path_).filename().string();
6486  std::string file_dir = g_base_path + "/" + shared::kDefaultExportDirName + "/" +
6487  session.get_session_id() + "/";
6488  if (!boost::filesystem::exists(file_dir)) {
6489  if (!boost::filesystem::create_directories(file_dir)) {
6490  throw std::runtime_error("Directory " + file_dir + " cannot be created.");
6491  }
6492  }
6493  *file_path_ = file_dir + file_name;
6494  } else {
6495  // Above branch will create a new file in the export directory. If that
6496  // path is not exercised, go through applicable file path validations.
6499  }
6500 
6501  const auto execute_read_lock = legacylockmgr::getExecuteReadLock();
6502  auto locks = acquire_query_table_locks(
6503  session_ptr->getCatalog().name(), *select_stmt_, query_state_proxy);
6504 
6505  // get column info
6506  LocalQueryConnector local_connector;
6507  auto column_info_result =
6508  local_connector.query(query_state_proxy, *select_stmt_, {}, true, false);
6509 
6510  // create exporter for requested file type
6511  auto query_exporter = import_export::QueryExporter::create(file_type);
6512 
6513  // default layer name to file path stem if it wasn't specified
6514  if (layer_name.size() == 0) {
6515  layer_name = boost::filesystem::path(*file_path_).stem().string();
6516  }
6517 
6518  // begin export
6519  query_exporter->beginExport(*file_path_,
6520  layer_name,
6521  copy_params,
6522  column_info_result.targets_meta,
6523  file_compression,
6524  array_null_handling);
6525 
6526  // how many fragments?
6527  size_t outer_frag_count =
6528  leafs_connector_->getOuterFragmentCount(query_state_proxy, *select_stmt_);
6529  size_t outer_frag_end = outer_frag_count == 0 ? 1 : outer_frag_count;
6530 
6531  // loop fragments
6532  for (size_t outer_frag_idx = 0; outer_frag_idx < outer_frag_end; outer_frag_idx++) {
6533  // limit the query to just this fragment
6534  std::vector<size_t> allowed_outer_fragment_indices;
6535  if (outer_frag_count) {
6536  allowed_outer_fragment_indices.push_back(outer_frag_idx);
6537  }
6538 
6539  // run the query
6540  std::vector<AggregatedResult> query_results = leafs_connector_->query(
6541  query_state_proxy, *select_stmt_, allowed_outer_fragment_indices, false);
6542 
6543  // export the results
6544  query_exporter->exportResults(query_results);
6545  }
6546 
6547  // end export
6548  query_exporter->endExport();
6549 }
std::unique_ptr< QueryConnector > leafs_connector_
Definition: ParserNode.h:1851
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)
auto getExecuteReadLock()
static std::shared_ptr< QueryState > create(ARGS &&...args)
Definition: QueryState.h:148
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1855
const std::string kDefaultExportDirName
static std::unique_ptr< QueryExporter > create(const FileType file_type)
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1854
std::string g_base_path
Definition: SysCatalog.cpp:62
lockmgr::LockedTableDescriptors acquire_query_table_locks(const std::string &insert_table_db_name, const std::string &query_str, const QueryStateProxy &query_state_proxy, const std::optional< std::string > &insert_table_name={})
void validate_allowed_file_path(const std::string &file_path, const DataTransferType data_transfer_type, const bool allow_wildcards)
Definition: DdlUtils.cpp:822
std::string get_session_id() const
Definition: SessionInfo.h:93
#define STDLOG(...)
Definition: QueryState.h:234

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1849 of file ParserNode.h.

References select_stmt_.

1849 { return *select_stmt_; }
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1854
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 6551 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::CopyParams::has_header, import_export::QueryExporter::kAbortWithWarning, import_export::QueryExporter::kCSV, import_export::QueryExporter::kExportSentinels, import_export::QueryExporter::kExportZeros, import_export::QueryExporter::kFlatGeobuf, import_export::QueryExporter::kGeoJSON, import_export::QueryExporter::kGeoJSONL, import_export::QueryExporter::kGZip, import_export::kHasHeader, import_export::kNoHeader, import_export::QueryExporter::kNone, import_export::QueryExporter::kNullEntireField, import_export::QueryExporter::kShapefile, import_export::QueryExporter::kZip, import_export::CopyParams::line_delim, import_export::CopyParams::null_str, options_, import_export::CopyParams::quote, and import_export::CopyParams::quoted.

Referenced by execute().

6556  {
6557  // defaults for non-CopyParams values
6559  layer_name.clear();
6561 
6562  if (!options_.empty()) {
6563  for (auto& p : options_) {
6564  if (boost::iequals(*p->get_name(), "delimiter")) {
6565  const StringLiteral* str_literal =
6566  dynamic_cast<const StringLiteral*>(p->get_value());
6567  if (str_literal == nullptr) {
6568  throw std::runtime_error("Delimiter option must be a string.");
6569  } else if (str_literal->get_stringval()->length() != 1) {
6570  throw std::runtime_error("Delimiter must be a single character string.");
6571  }
6572  copy_params.delimiter = (*str_literal->get_stringval())[0];
6573  } else if (boost::iequals(*p->get_name(), "nulls")) {
6574  const StringLiteral* str_literal =
6575  dynamic_cast<const StringLiteral*>(p->get_value());
6576  if (str_literal == nullptr) {
6577  throw std::runtime_error("Nulls option must be a string.");
6578  }
6579  copy_params.null_str = *str_literal->get_stringval();
6580  } else if (boost::iequals(*p->get_name(), "header")) {
6581  const StringLiteral* str_literal =
6582  dynamic_cast<const StringLiteral*>(p->get_value());
6583  if (str_literal == nullptr) {
6584  throw std::runtime_error("Header option must be a boolean.");
6585  }
6586  copy_params.has_header = bool_from_string_literal(str_literal)
6589  } else if (boost::iequals(*p->get_name(), "quote")) {
6590  const StringLiteral* str_literal =
6591  dynamic_cast<const StringLiteral*>(p->get_value());
6592  if (str_literal == nullptr) {
6593  throw std::runtime_error("Quote option must be a string.");
6594  } else if (str_literal->get_stringval()->length() != 1) {
6595  throw std::runtime_error("Quote must be a single character string.");
6596  }
6597  copy_params.quote = (*str_literal->get_stringval())[0];
6598  } else if (boost::iequals(*p->get_name(), "escape")) {
6599  const StringLiteral* str_literal =
6600  dynamic_cast<const StringLiteral*>(p->get_value());
6601  if (str_literal == nullptr) {
6602  throw std::runtime_error("Escape option must be a string.");
6603  } else if (str_literal->get_stringval()->length() != 1) {
6604  throw std::runtime_error("Escape must be a single character string.");
6605  }
6606  copy_params.escape = (*str_literal->get_stringval())[0];
6607  } else if (boost::iequals(*p->get_name(), "line_delimiter")) {
6608  const StringLiteral* str_literal =
6609  dynamic_cast<const StringLiteral*>(p->get_value());
6610  if (str_literal == nullptr) {
6611  throw std::runtime_error("Line_delimiter option must be a string.");
6612  } else if (str_literal->get_stringval()->length() != 1) {
6613  throw std::runtime_error("Line_delimiter must be a single character string.");
6614  }
6615  copy_params.line_delim = (*str_literal->get_stringval())[0];
6616  } else if (boost::iequals(*p->get_name(), "quoted")) {
6617  const StringLiteral* str_literal =
6618  dynamic_cast<const StringLiteral*>(p->get_value());
6619  if (str_literal == nullptr) {
6620  throw std::runtime_error("Quoted option must be a boolean.");
6621  }
6622  copy_params.quoted = bool_from_string_literal(str_literal);
6623  } else if (boost::iequals(*p->get_name(), "file_type")) {
6624  const StringLiteral* str_literal =
6625  dynamic_cast<const StringLiteral*>(p->get_value());
6626  if (str_literal == nullptr) {
6627  throw std::runtime_error("File Type option must be a string.");
6628  }
6629  auto file_type_str =
6630  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6631  if (file_type_str == "csv") {
6633  } else if (file_type_str == "geojson") {
6635  } else if (file_type_str == "geojsonl") {
6637  } else if (file_type_str == "shapefile") {
6639  } else if (file_type_str == "flatgeobuf") {
6641  } else {
6642  throw std::runtime_error(
6643  "File Type option must be 'CSV', 'GeoJSON', 'GeoJSONL', "
6644  "'Shapefile', or 'FlatGeobuf'");
6645  }
6646  } else if (boost::iequals(*p->get_name(), "layer_name")) {
6647  const StringLiteral* str_literal =
6648  dynamic_cast<const StringLiteral*>(p->get_value());
6649  if (str_literal == nullptr) {
6650  throw std::runtime_error("Layer Name option must be a string.");
6651  }
6652  layer_name = *str_literal->get_stringval();
6653  } else if (boost::iequals(*p->get_name(), "file_compression")) {
6654  const StringLiteral* str_literal =
6655  dynamic_cast<const StringLiteral*>(p->get_value());
6656  if (str_literal == nullptr) {
6657  throw std::runtime_error("File Compression option must be a string.");
6658  }
6659  auto file_compression_str =
6660  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6661  if (file_compression_str == "none") {
6663  } else if (file_compression_str == "gzip") {
6665  } else if (file_compression_str == "zip") {
6667  } else {
6668  throw std::runtime_error(
6669  "File Compression option must be 'None', 'GZip', or 'Zip'");
6670  }
6671  } else if (boost::iequals(*p->get_name(), "array_null_handling")) {
6672  const StringLiteral* str_literal =
6673  dynamic_cast<const StringLiteral*>(p->get_value());
6674  if (str_literal == nullptr) {
6675  throw std::runtime_error("Array Null Handling option must be a string.");
6676  }
6677  auto array_null_handling_str =
6678  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6679  if (array_null_handling_str == "abort") {
6680  array_null_handling =
6682  } else if (array_null_handling_str == "raw") {
6683  array_null_handling =
6685  } else if (array_null_handling_str == "zero") {
6686  array_null_handling =
6688  } else if (array_null_handling_str == "nullfield") {
6689  array_null_handling =
6691  } else {
6692  throw std::runtime_error(
6693  "Array Null Handling option must be 'Abort', 'Raw', 'Zero', or "
6694  "'NullField'");
6695  }
6696  } else {
6697  throw std::runtime_error("Invalid option for COPY: " + *p->get_name());
6698  }
6699  }
6700  }
6701 }
bool bool_from_string_literal(const Parser::StringLiteral *str_literal)
ImportHeaderRow has_header
Definition: CopyParams.h:46
std::list< std::unique_ptr< NameValueAssign > > options_
Definition: ParserNode.h:1856

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

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

Definition at line 1855 of file ParserNode.h.

Referenced by execute(), and ExportQueryStmt().

std::unique_ptr<QueryConnector> Parser::ExportQueryStmt::leafs_connector_

Definition at line 1851 of file ParserNode.h.

Referenced by execute().

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

Definition at line 1856 of file ParserNode.h.

Referenced by ExportQueryStmt(), and parseOptions().

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

Definition at line 1854 of file ParserNode.h.

Referenced by execute(), ExportQueryStmt(), and get_select_stmt().


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