OmniSciDB  c0231cc57d
 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 1892 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 1894 of file ParserNode.h.

References options_.

1895  : select_stmt_(q), file_path_(p) {
1896  if (o) {
1897  for (const auto e : *o) {
1898  options_.emplace_back(e);
1899  }
1900  delete o;
1901  }
1902  }
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1912
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1911
std::list< std::unique_ptr< NameValueAssign > > options_
Definition: ParserNode.h:1913
Parser::ExportQueryStmt::ExportQueryStmt ( const rapidjson::Value &  payload)

Definition at line 6111 of file ParserNode.cpp.

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

6111  {
6112  CHECK(payload.HasMember("filePath"));
6113  file_path_ = std::make_unique<std::string>(json_str(payload["filePath"]));
6114 
6115  CHECK(payload.HasMember("query"));
6116  select_stmt_ = std::make_unique<std::string>(json_str(payload["query"]));
6117 
6118  if ((*select_stmt_).back() != ';') {
6119  (*select_stmt_).push_back(';');
6120  }
6121  // Export wrapped everything with ` quotes which need cleanup
6122  boost::replace_all((*select_stmt_), "`", "");
6123 
6124  parse_options(payload, options_);
6125 }
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1912
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1911
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:1913
#define CHECK(condition)
Definition: Logger.h:222

+ 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 6127 of file ParserNode.cpp.

References Parser::anonymous_namespace{ParserNode.cpp}::acquire_query_table_locks(), import_export::QueryExporter::create(), query_state::QueryState::create(), legacylockmgr::ExecutorOuterLock, ddl_utils::EXPORT, file_path_, g_base_path, Catalog_Namespace::SessionInfo::get_session_id(), legacylockmgr::LockMgr< MutexType, KeyType >::getMutex(), 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().

6128  {
6129  // valid in read_only_mode
6130  auto session_copy = session;
6131  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
6132  &session_copy, boost::null_deleter());
6133  auto query_state = query_state::QueryState::create(session_ptr, *select_stmt_);
6134  auto stdlog = STDLOG(query_state);
6135  auto query_state_proxy = query_state->createQueryStateProxy();
6136 
6137  if (!leafs_connector_) {
6138  leafs_connector_ = std::make_unique<LocalQueryConnector>();
6139  }
6140 
6141  import_export::CopyParams copy_params;
6142  // @TODO(se) move rest to CopyParams when we have a Thrift endpoint
6145  std::string layer_name;
6150 
6151  parseOptions(copy_params, file_type, layer_name, file_compression, array_null_handling);
6152 
6153  if (file_path_->empty()) {
6154  throw std::runtime_error("Invalid file path for COPY TO");
6155  } else if (!boost::filesystem::path(*file_path_).is_absolute()) {
6156  std::string file_name = boost::filesystem::path(*file_path_).filename().string();
6157  std::string file_dir = g_base_path + "/" + shared::kDefaultExportDirName + "/" +
6158  session.get_session_id() + "/";
6159  if (!boost::filesystem::exists(file_dir)) {
6160  if (!boost::filesystem::create_directories(file_dir)) {
6161  throw std::runtime_error("Directory " + file_dir + " cannot be created.");
6162  }
6163  }
6164  *file_path_ = file_dir + file_name;
6165  } else {
6166  // Above branch will create a new file in the export directory. If that
6167  // path is not exercised, go through applicable file path validations.
6170  }
6171 
6172  const auto execute_read_lock =
6176  auto locks = acquire_query_table_locks(
6177  session_ptr->getCatalog(), *select_stmt_, query_state_proxy);
6178 
6179  // get column info
6180  LocalQueryConnector local_connector;
6181  auto column_info_result =
6182  local_connector.query(query_state_proxy, *select_stmt_, {}, true, false);
6183 
6184  // create exporter for requested file type
6185  auto query_exporter = import_export::QueryExporter::create(file_type);
6186 
6187  // default layer name to file path stem if it wasn't specified
6188  if (layer_name.size() == 0) {
6189  layer_name = boost::filesystem::path(*file_path_).stem().string();
6190  }
6191 
6192  // begin export
6193  query_exporter->beginExport(*file_path_,
6194  layer_name,
6195  copy_params,
6196  column_info_result.targets_meta,
6197  file_compression,
6198  array_null_handling);
6199 
6200  // how many fragments?
6201  size_t outer_frag_count =
6202  leafs_connector_->getOuterFragmentCount(query_state_proxy, *select_stmt_);
6203  size_t outer_frag_end = outer_frag_count == 0 ? 1 : outer_frag_count;
6204 
6205  // loop fragments
6206  for (size_t outer_frag_idx = 0; outer_frag_idx < outer_frag_end; outer_frag_idx++) {
6207  // limit the query to just this fragment
6208  std::vector<size_t> allowed_outer_fragment_indices;
6209  if (outer_frag_count) {
6210  allowed_outer_fragment_indices.push_back(outer_frag_idx);
6211  }
6212 
6213  // run the query
6214  std::vector<AggregatedResult> query_results = leafs_connector_->query(
6215  query_state_proxy, *select_stmt_, allowed_outer_fragment_indices, false);
6216 
6217  // export the results
6218  query_exporter->exportResults(query_results);
6219  }
6220 
6221  // end export
6222  query_exporter->endExport();
6223 }
static std::shared_ptr< WrapperType< MutexType > > getMutex(const LockType lockType, const KeyType &key)
std::unique_ptr< QueryConnector > leafs_connector_
Definition: ParserNode.h:1908
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:145
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1912
const std::string kDefaultExportDirName
static std::unique_ptr< QueryExporter > create(const FileType file_type)
std::shared_lock< T > shared_lock
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1911
std::string g_base_path
Definition: SysCatalog.cpp:62
lockmgr::LockedTableDescriptors acquire_query_table_locks(Catalog_Namespace::Catalog &catalog, 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:785
std::string get_session_id() const
Definition: SessionInfo.h:93
#define STDLOG(...)
Definition: QueryState.h:235

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

References select_stmt_.

1906 { return *select_stmt_; }
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1911
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 6225 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().

6230  {
6231  // defaults for non-CopyParams values
6233  layer_name.clear();
6235 
6236  if (!options_.empty()) {
6237  for (auto& p : options_) {
6238  if (boost::iequals(*p->get_name(), "delimiter")) {
6239  const StringLiteral* str_literal =
6240  dynamic_cast<const StringLiteral*>(p->get_value());
6241  if (str_literal == nullptr) {
6242  throw std::runtime_error("Delimiter option must be a string.");
6243  } else if (str_literal->get_stringval()->length() != 1) {
6244  throw std::runtime_error("Delimiter must be a single character string.");
6245  }
6246  copy_params.delimiter = (*str_literal->get_stringval())[0];
6247  } else if (boost::iequals(*p->get_name(), "nulls")) {
6248  const StringLiteral* str_literal =
6249  dynamic_cast<const StringLiteral*>(p->get_value());
6250  if (str_literal == nullptr) {
6251  throw std::runtime_error("Nulls option must be a string.");
6252  }
6253  copy_params.null_str = *str_literal->get_stringval();
6254  } else if (boost::iequals(*p->get_name(), "header")) {
6255  const StringLiteral* str_literal =
6256  dynamic_cast<const StringLiteral*>(p->get_value());
6257  if (str_literal == nullptr) {
6258  throw std::runtime_error("Header option must be a boolean.");
6259  }
6260  copy_params.has_header = bool_from_string_literal(str_literal)
6263  } else if (boost::iequals(*p->get_name(), "quote")) {
6264  const StringLiteral* str_literal =
6265  dynamic_cast<const StringLiteral*>(p->get_value());
6266  if (str_literal == nullptr) {
6267  throw std::runtime_error("Quote option must be a string.");
6268  } else if (str_literal->get_stringval()->length() != 1) {
6269  throw std::runtime_error("Quote must be a single character string.");
6270  }
6271  copy_params.quote = (*str_literal->get_stringval())[0];
6272  } else if (boost::iequals(*p->get_name(), "escape")) {
6273  const StringLiteral* str_literal =
6274  dynamic_cast<const StringLiteral*>(p->get_value());
6275  if (str_literal == nullptr) {
6276  throw std::runtime_error("Escape option must be a string.");
6277  } else if (str_literal->get_stringval()->length() != 1) {
6278  throw std::runtime_error("Escape must be a single character string.");
6279  }
6280  copy_params.escape = (*str_literal->get_stringval())[0];
6281  } else if (boost::iequals(*p->get_name(), "line_delimiter")) {
6282  const StringLiteral* str_literal =
6283  dynamic_cast<const StringLiteral*>(p->get_value());
6284  if (str_literal == nullptr) {
6285  throw std::runtime_error("Line_delimiter option must be a string.");
6286  } else if (str_literal->get_stringval()->length() != 1) {
6287  throw std::runtime_error("Line_delimiter must be a single character string.");
6288  }
6289  copy_params.line_delim = (*str_literal->get_stringval())[0];
6290  } else if (boost::iequals(*p->get_name(), "quoted")) {
6291  const StringLiteral* str_literal =
6292  dynamic_cast<const StringLiteral*>(p->get_value());
6293  if (str_literal == nullptr) {
6294  throw std::runtime_error("Quoted option must be a boolean.");
6295  }
6296  copy_params.quoted = bool_from_string_literal(str_literal);
6297  } else if (boost::iequals(*p->get_name(), "file_type")) {
6298  const StringLiteral* str_literal =
6299  dynamic_cast<const StringLiteral*>(p->get_value());
6300  if (str_literal == nullptr) {
6301  throw std::runtime_error("File Type option must be a string.");
6302  }
6303  auto file_type_str =
6304  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6305  if (file_type_str == "csv") {
6307  } else if (file_type_str == "geojson") {
6309  } else if (file_type_str == "geojsonl") {
6311  } else if (file_type_str == "shapefile") {
6313  } else if (file_type_str == "flatgeobuf") {
6315  } else {
6316  throw std::runtime_error(
6317  "File Type option must be 'CSV', 'GeoJSON', 'GeoJSONL', "
6318  "'Shapefile', or 'FlatGeobuf'");
6319  }
6320  } else if (boost::iequals(*p->get_name(), "layer_name")) {
6321  const StringLiteral* str_literal =
6322  dynamic_cast<const StringLiteral*>(p->get_value());
6323  if (str_literal == nullptr) {
6324  throw std::runtime_error("Layer Name option must be a string.");
6325  }
6326  layer_name = *str_literal->get_stringval();
6327  } else if (boost::iequals(*p->get_name(), "file_compression")) {
6328  const StringLiteral* str_literal =
6329  dynamic_cast<const StringLiteral*>(p->get_value());
6330  if (str_literal == nullptr) {
6331  throw std::runtime_error("File Compression option must be a string.");
6332  }
6333  auto file_compression_str =
6334  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6335  if (file_compression_str == "none") {
6337  } else if (file_compression_str == "gzip") {
6339  } else if (file_compression_str == "zip") {
6341  } else {
6342  throw std::runtime_error(
6343  "File Compression option must be 'None', 'GZip', or 'Zip'");
6344  }
6345  } else if (boost::iequals(*p->get_name(), "array_null_handling")) {
6346  const StringLiteral* str_literal =
6347  dynamic_cast<const StringLiteral*>(p->get_value());
6348  if (str_literal == nullptr) {
6349  throw std::runtime_error("Array Null Handling option must be a string.");
6350  }
6351  auto array_null_handling_str =
6352  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6353  if (array_null_handling_str == "abort") {
6354  array_null_handling =
6356  } else if (array_null_handling_str == "raw") {
6357  array_null_handling =
6359  } else if (array_null_handling_str == "zero") {
6360  array_null_handling =
6362  } else if (array_null_handling_str == "nullfield") {
6363  array_null_handling =
6365  } else {
6366  throw std::runtime_error(
6367  "Array Null Handling option must be 'Abort', 'Raw', 'Zero', or "
6368  "'NullField'");
6369  }
6370  } else {
6371  throw std::runtime_error("Invalid option for COPY: " + *p->get_name());
6372  }
6373  }
6374  }
6375 }
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:1913

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

Referenced by execute(), and ExportQueryStmt().

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

Definition at line 1908 of file ParserNode.h.

Referenced by execute().

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

Definition at line 1913 of file ParserNode.h.

Referenced by ExportQueryStmt(), and parseOptions().

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

Definition at line 1911 of file ParserNode.h.

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


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