OmniSciDB  a5dc49c757
 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 6461 of file ParserNode.cpp.

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

6461  {
6462  CHECK(payload.HasMember("filePath"));
6463  file_path_ = std::make_unique<std::string>(json_str(payload["filePath"]));
6464 
6465  CHECK(payload.HasMember("query"));
6466  select_stmt_ = std::make_unique<std::string>(json_str(payload["query"]));
6467 
6468  if ((*select_stmt_).back() != ';') {
6469  (*select_stmt_).push_back(';');
6470  }
6471  // Export wrapped everything with ` quotes which need cleanup
6472  boost::replace_all((*select_stmt_), "`", "");
6473 
6474  parse_options(payload, options_);
6475 }
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 6477 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().

6478  {
6479  // valid in read_only_mode
6480  auto session_copy = session;
6481  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
6482  &session_copy, boost::null_deleter());
6483  auto query_state = query_state::QueryState::create(session_ptr, *select_stmt_);
6484  auto stdlog = STDLOG(query_state);
6485  auto query_state_proxy = query_state->createQueryStateProxy();
6486 
6487  if (!leafs_connector_) {
6488  leafs_connector_ = std::make_unique<LocalQueryConnector>();
6489  }
6490 
6491  import_export::CopyParams copy_params;
6492  // @TODO(se) move rest to CopyParams when we have a Thrift endpoint
6495  std::string layer_name;
6500 
6501  parseOptions(copy_params, file_type, layer_name, file_compression, array_null_handling);
6502 
6503  if (file_path_->empty()) {
6504  throw std::runtime_error("Invalid file path for COPY TO");
6505  } else if (!boost::filesystem::path(*file_path_).is_absolute()) {
6506  std::string file_name = boost::filesystem::path(*file_path_).filename().string();
6507  std::string file_dir = g_base_path + "/" + shared::kDefaultExportDirName + "/" +
6508  session.get_session_id() + "/";
6509  if (!boost::filesystem::exists(file_dir)) {
6510  if (!boost::filesystem::create_directories(file_dir)) {
6511  throw std::runtime_error("Directory " + file_dir + " cannot be created.");
6512  }
6513  }
6514  *file_path_ = file_dir + file_name;
6515  } else {
6516  // Above branch will create a new file in the export directory. If that
6517  // path is not exercised, go through applicable file path validations.
6520  }
6521 
6522  const auto execute_read_lock = legacylockmgr::getExecuteReadLock();
6523  auto locks = acquire_query_table_locks(
6524  session_ptr->getCatalog().name(), *select_stmt_, query_state_proxy);
6525 
6526  // get column info
6527  LocalQueryConnector local_connector;
6528  auto column_info_result =
6529  local_connector.query(query_state_proxy, *select_stmt_, {}, true, false);
6530 
6531  // create exporter for requested file type
6532  auto query_exporter = import_export::QueryExporter::create(file_type);
6533 
6534  // default layer name to file path stem if it wasn't specified
6535  if (layer_name.size() == 0) {
6536  layer_name = boost::filesystem::path(*file_path_).stem().string();
6537  }
6538 
6539  // begin export
6540  query_exporter->beginExport(*file_path_,
6541  layer_name,
6542  copy_params,
6543  column_info_result.targets_meta,
6544  file_compression,
6545  array_null_handling);
6546 
6547  // how many fragments?
6548  size_t outer_frag_count =
6549  leafs_connector_->getOuterFragmentCount(query_state_proxy, *select_stmt_);
6550  size_t outer_frag_end = outer_frag_count == 0 ? 1 : outer_frag_count;
6551 
6552  // loop fragments
6553  for (size_t outer_frag_idx = 0; outer_frag_idx < outer_frag_end; outer_frag_idx++) {
6554  // limit the query to just this fragment
6555  std::vector<size_t> allowed_outer_fragment_indices;
6556  if (outer_frag_count) {
6557  allowed_outer_fragment_indices.push_back(outer_frag_idx);
6558  }
6559 
6560  // run the query
6561  std::vector<AggregatedResult> query_results = leafs_connector_->query(
6562  query_state_proxy, *select_stmt_, allowed_outer_fragment_indices, false);
6563 
6564  // export the results
6565  query_exporter->exportResults(query_results);
6566  }
6567 
6568  // end export
6569  query_exporter->endExport();
6570 }
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 6572 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().

6577  {
6578  // defaults for non-CopyParams values
6580  layer_name.clear();
6582 
6583  if (!options_.empty()) {
6584  for (auto& p : options_) {
6585  if (boost::iequals(*p->get_name(), "delimiter")) {
6586  const StringLiteral* str_literal =
6587  dynamic_cast<const StringLiteral*>(p->get_value());
6588  if (str_literal == nullptr) {
6589  throw std::runtime_error("Delimiter option must be a string.");
6590  } else if (str_literal->get_stringval()->length() != 1) {
6591  throw std::runtime_error("Delimiter must be a single character string.");
6592  }
6593  copy_params.delimiter = (*str_literal->get_stringval())[0];
6594  } else if (boost::iequals(*p->get_name(), "nulls")) {
6595  const StringLiteral* str_literal =
6596  dynamic_cast<const StringLiteral*>(p->get_value());
6597  if (str_literal == nullptr) {
6598  throw std::runtime_error("Nulls option must be a string.");
6599  }
6600  copy_params.null_str = *str_literal->get_stringval();
6601  } else if (boost::iequals(*p->get_name(), "header")) {
6602  const StringLiteral* str_literal =
6603  dynamic_cast<const StringLiteral*>(p->get_value());
6604  if (str_literal == nullptr) {
6605  throw std::runtime_error("Header option must be a boolean.");
6606  }
6607  copy_params.has_header = bool_from_string_literal(str_literal)
6610  } else if (boost::iequals(*p->get_name(), "quote")) {
6611  const StringLiteral* str_literal =
6612  dynamic_cast<const StringLiteral*>(p->get_value());
6613  if (str_literal == nullptr) {
6614  throw std::runtime_error("Quote option must be a string.");
6615  } else if (str_literal->get_stringval()->length() != 1) {
6616  throw std::runtime_error("Quote must be a single character string.");
6617  }
6618  copy_params.quote = (*str_literal->get_stringval())[0];
6619  } else if (boost::iequals(*p->get_name(), "escape")) {
6620  const StringLiteral* str_literal =
6621  dynamic_cast<const StringLiteral*>(p->get_value());
6622  if (str_literal == nullptr) {
6623  throw std::runtime_error("Escape option must be a string.");
6624  } else if (str_literal->get_stringval()->length() != 1) {
6625  throw std::runtime_error("Escape must be a single character string.");
6626  }
6627  copy_params.escape = (*str_literal->get_stringval())[0];
6628  } else if (boost::iequals(*p->get_name(), "line_delimiter")) {
6629  const StringLiteral* str_literal =
6630  dynamic_cast<const StringLiteral*>(p->get_value());
6631  if (str_literal == nullptr) {
6632  throw std::runtime_error("Line_delimiter option must be a string.");
6633  } else if (str_literal->get_stringval()->length() != 1) {
6634  throw std::runtime_error("Line_delimiter must be a single character string.");
6635  }
6636  copy_params.line_delim = (*str_literal->get_stringval())[0];
6637  } else if (boost::iequals(*p->get_name(), "quoted")) {
6638  const StringLiteral* str_literal =
6639  dynamic_cast<const StringLiteral*>(p->get_value());
6640  if (str_literal == nullptr) {
6641  throw std::runtime_error("Quoted option must be a boolean.");
6642  }
6643  copy_params.quoted = bool_from_string_literal(str_literal);
6644  } else if (boost::iequals(*p->get_name(), "file_type")) {
6645  const StringLiteral* str_literal =
6646  dynamic_cast<const StringLiteral*>(p->get_value());
6647  if (str_literal == nullptr) {
6648  throw std::runtime_error("File Type option must be a string.");
6649  }
6650  auto file_type_str =
6651  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6652  if (file_type_str == "csv") {
6654  } else if (file_type_str == "geojson") {
6656  } else if (file_type_str == "geojsonl") {
6658  } else if (file_type_str == "shapefile") {
6660  } else if (file_type_str == "flatgeobuf") {
6662  } else {
6663  throw std::runtime_error(
6664  "File Type option must be 'CSV', 'GeoJSON', 'GeoJSONL', "
6665  "'Shapefile', or 'FlatGeobuf'");
6666  }
6667  } else if (boost::iequals(*p->get_name(), "layer_name")) {
6668  const StringLiteral* str_literal =
6669  dynamic_cast<const StringLiteral*>(p->get_value());
6670  if (str_literal == nullptr) {
6671  throw std::runtime_error("Layer Name option must be a string.");
6672  }
6673  layer_name = *str_literal->get_stringval();
6674  } else if (boost::iequals(*p->get_name(), "file_compression")) {
6675  const StringLiteral* str_literal =
6676  dynamic_cast<const StringLiteral*>(p->get_value());
6677  if (str_literal == nullptr) {
6678  throw std::runtime_error("File Compression option must be a string.");
6679  }
6680  auto file_compression_str =
6681  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6682  if (file_compression_str == "none") {
6684  } else if (file_compression_str == "gzip") {
6686  } else if (file_compression_str == "zip") {
6688  } else {
6689  throw std::runtime_error(
6690  "File Compression option must be 'None', 'GZip', or 'Zip'");
6691  }
6692  } else if (boost::iequals(*p->get_name(), "array_null_handling")) {
6693  const StringLiteral* str_literal =
6694  dynamic_cast<const StringLiteral*>(p->get_value());
6695  if (str_literal == nullptr) {
6696  throw std::runtime_error("Array Null Handling option must be a string.");
6697  }
6698  auto array_null_handling_str =
6699  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6700  if (array_null_handling_str == "abort") {
6701  array_null_handling =
6703  } else if (array_null_handling_str == "raw") {
6704  array_null_handling =
6706  } else if (array_null_handling_str == "zero") {
6707  array_null_handling =
6709  } else if (array_null_handling_str == "nullfield") {
6710  array_null_handling =
6712  } else {
6713  throw std::runtime_error(
6714  "Array Null Handling option must be 'Abort', 'Raw', 'Zero', or "
6715  "'NullField'");
6716  }
6717  } else {
6718  throw std::runtime_error("Invalid option for COPY: " + *p->get_name());
6719  }
6720  }
6721  }
6722 }
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: