OmniSciDB  c1a53651b2
 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 1848 of file ParserNode.h.

Constructor & Destructor Documentation

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

Definition at line 1850 of file ParserNode.h.

References options_.

1851  : select_stmt_(q), file_path_(p) {
1852  if (o) {
1853  for (const auto e : *o) {
1854  options_.emplace_back(e);
1855  }
1856  delete o;
1857  }
1858  }
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1868
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1867
std::list< std::unique_ptr< NameValueAssign > > options_
Definition: ParserNode.h:1869
Parser::ExportQueryStmt::ExportQueryStmt ( const rapidjson::Value &  payload)

Definition at line 6138 of file ParserNode.cpp.

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

6138  {
6139  CHECK(payload.HasMember("filePath"));
6140  file_path_ = std::make_unique<std::string>(json_str(payload["filePath"]));
6141 
6142  CHECK(payload.HasMember("query"));
6143  select_stmt_ = std::make_unique<std::string>(json_str(payload["query"]));
6144 
6145  if ((*select_stmt_).back() != ';') {
6146  (*select_stmt_).push_back(';');
6147  }
6148  // Export wrapped everything with ` quotes which need cleanup
6149  boost::replace_all((*select_stmt_), "`", "");
6150 
6151  parse_options(payload, options_);
6152 }
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1868
std::unique_ptr< std::string > select_stmt_
Definition: ParserNode.h:1867
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:1869
#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 6154 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(), 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().

6155  {
6156  // valid in read_only_mode
6157  auto session_copy = session;
6158  auto session_ptr = std::shared_ptr<Catalog_Namespace::SessionInfo>(
6159  &session_copy, boost::null_deleter());
6160  auto query_state = query_state::QueryState::create(session_ptr, *select_stmt_);
6161  auto stdlog = STDLOG(query_state);
6162  auto query_state_proxy = query_state->createQueryStateProxy();
6163 
6164  if (!leafs_connector_) {
6165  leafs_connector_ = std::make_unique<LocalQueryConnector>();
6166  }
6167 
6168  import_export::CopyParams copy_params;
6169  // @TODO(se) move rest to CopyParams when we have a Thrift endpoint
6172  std::string layer_name;
6177 
6178  parseOptions(copy_params, file_type, layer_name, file_compression, array_null_handling);
6179 
6180  if (file_path_->empty()) {
6181  throw std::runtime_error("Invalid file path for COPY TO");
6182  } else if (!boost::filesystem::path(*file_path_).is_absolute()) {
6183  std::string file_name = boost::filesystem::path(*file_path_).filename().string();
6184  std::string file_dir = g_base_path + "/" + shared::kDefaultExportDirName + "/" +
6185  session.get_session_id() + "/";
6186  if (!boost::filesystem::exists(file_dir)) {
6187  if (!boost::filesystem::create_directories(file_dir)) {
6188  throw std::runtime_error("Directory " + file_dir + " cannot be created.");
6189  }
6190  }
6191  *file_path_ = file_dir + file_name;
6192  } else {
6193  // Above branch will create a new file in the export directory. If that
6194  // path is not exercised, go through applicable file path validations.
6197  }
6198 
6199  const auto execute_read_lock =
6203  auto locks = acquire_query_table_locks(
6204  session_ptr->getCatalog().name(), *select_stmt_, query_state_proxy);
6205 
6206  // get column info
6207  LocalQueryConnector local_connector;
6208  auto column_info_result =
6209  local_connector.query(query_state_proxy, *select_stmt_, {}, true, false);
6210 
6211  // create exporter for requested file type
6212  auto query_exporter = import_export::QueryExporter::create(file_type);
6213 
6214  // default layer name to file path stem if it wasn't specified
6215  if (layer_name.size() == 0) {
6216  layer_name = boost::filesystem::path(*file_path_).stem().string();
6217  }
6218 
6219  // begin export
6220  query_exporter->beginExport(*file_path_,
6221  layer_name,
6222  copy_params,
6223  column_info_result.targets_meta,
6224  file_compression,
6225  array_null_handling);
6226 
6227  // how many fragments?
6228  size_t outer_frag_count =
6229  leafs_connector_->getOuterFragmentCount(query_state_proxy, *select_stmt_);
6230  size_t outer_frag_end = outer_frag_count == 0 ? 1 : outer_frag_count;
6231 
6232  // loop fragments
6233  for (size_t outer_frag_idx = 0; outer_frag_idx < outer_frag_end; outer_frag_idx++) {
6234  // limit the query to just this fragment
6235  std::vector<size_t> allowed_outer_fragment_indices;
6236  if (outer_frag_count) {
6237  allowed_outer_fragment_indices.push_back(outer_frag_idx);
6238  }
6239 
6240  // run the query
6241  std::vector<AggregatedResult> query_results = leafs_connector_->query(
6242  query_state_proxy, *select_stmt_, allowed_outer_fragment_indices, false);
6243 
6244  // export the results
6245  query_exporter->exportResults(query_results);
6246  }
6247 
6248  // end export
6249  query_exporter->endExport();
6250 }
static std::shared_ptr< WrapperType< MutexType > > getMutex(const LockType lockType, const KeyType &key)
std::unique_ptr< QueryConnector > leafs_connector_
Definition: ParserNode.h:1864
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:148
std::unique_ptr< std::string > file_path_
Definition: ParserNode.h:1868
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:1867
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:785
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 1862 of file ParserNode.h.

References select_stmt_.

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

6257  {
6258  // defaults for non-CopyParams values
6260  layer_name.clear();
6262 
6263  if (!options_.empty()) {
6264  for (auto& p : options_) {
6265  if (boost::iequals(*p->get_name(), "delimiter")) {
6266  const StringLiteral* str_literal =
6267  dynamic_cast<const StringLiteral*>(p->get_value());
6268  if (str_literal == nullptr) {
6269  throw std::runtime_error("Delimiter option must be a string.");
6270  } else if (str_literal->get_stringval()->length() != 1) {
6271  throw std::runtime_error("Delimiter must be a single character string.");
6272  }
6273  copy_params.delimiter = (*str_literal->get_stringval())[0];
6274  } else if (boost::iequals(*p->get_name(), "nulls")) {
6275  const StringLiteral* str_literal =
6276  dynamic_cast<const StringLiteral*>(p->get_value());
6277  if (str_literal == nullptr) {
6278  throw std::runtime_error("Nulls option must be a string.");
6279  }
6280  copy_params.null_str = *str_literal->get_stringval();
6281  } else if (boost::iequals(*p->get_name(), "header")) {
6282  const StringLiteral* str_literal =
6283  dynamic_cast<const StringLiteral*>(p->get_value());
6284  if (str_literal == nullptr) {
6285  throw std::runtime_error("Header option must be a boolean.");
6286  }
6287  copy_params.has_header = bool_from_string_literal(str_literal)
6290  } else if (boost::iequals(*p->get_name(), "quote")) {
6291  const StringLiteral* str_literal =
6292  dynamic_cast<const StringLiteral*>(p->get_value());
6293  if (str_literal == nullptr) {
6294  throw std::runtime_error("Quote option must be a string.");
6295  } else if (str_literal->get_stringval()->length() != 1) {
6296  throw std::runtime_error("Quote must be a single character string.");
6297  }
6298  copy_params.quote = (*str_literal->get_stringval())[0];
6299  } else if (boost::iequals(*p->get_name(), "escape")) {
6300  const StringLiteral* str_literal =
6301  dynamic_cast<const StringLiteral*>(p->get_value());
6302  if (str_literal == nullptr) {
6303  throw std::runtime_error("Escape option must be a string.");
6304  } else if (str_literal->get_stringval()->length() != 1) {
6305  throw std::runtime_error("Escape must be a single character string.");
6306  }
6307  copy_params.escape = (*str_literal->get_stringval())[0];
6308  } else if (boost::iequals(*p->get_name(), "line_delimiter")) {
6309  const StringLiteral* str_literal =
6310  dynamic_cast<const StringLiteral*>(p->get_value());
6311  if (str_literal == nullptr) {
6312  throw std::runtime_error("Line_delimiter option must be a string.");
6313  } else if (str_literal->get_stringval()->length() != 1) {
6314  throw std::runtime_error("Line_delimiter must be a single character string.");
6315  }
6316  copy_params.line_delim = (*str_literal->get_stringval())[0];
6317  } else if (boost::iequals(*p->get_name(), "quoted")) {
6318  const StringLiteral* str_literal =
6319  dynamic_cast<const StringLiteral*>(p->get_value());
6320  if (str_literal == nullptr) {
6321  throw std::runtime_error("Quoted option must be a boolean.");
6322  }
6323  copy_params.quoted = bool_from_string_literal(str_literal);
6324  } else if (boost::iequals(*p->get_name(), "file_type")) {
6325  const StringLiteral* str_literal =
6326  dynamic_cast<const StringLiteral*>(p->get_value());
6327  if (str_literal == nullptr) {
6328  throw std::runtime_error("File Type option must be a string.");
6329  }
6330  auto file_type_str =
6331  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6332  if (file_type_str == "csv") {
6334  } else if (file_type_str == "geojson") {
6336  } else if (file_type_str == "geojsonl") {
6338  } else if (file_type_str == "shapefile") {
6340  } else if (file_type_str == "flatgeobuf") {
6342  } else {
6343  throw std::runtime_error(
6344  "File Type option must be 'CSV', 'GeoJSON', 'GeoJSONL', "
6345  "'Shapefile', or 'FlatGeobuf'");
6346  }
6347  } else if (boost::iequals(*p->get_name(), "layer_name")) {
6348  const StringLiteral* str_literal =
6349  dynamic_cast<const StringLiteral*>(p->get_value());
6350  if (str_literal == nullptr) {
6351  throw std::runtime_error("Layer Name option must be a string.");
6352  }
6353  layer_name = *str_literal->get_stringval();
6354  } else if (boost::iequals(*p->get_name(), "file_compression")) {
6355  const StringLiteral* str_literal =
6356  dynamic_cast<const StringLiteral*>(p->get_value());
6357  if (str_literal == nullptr) {
6358  throw std::runtime_error("File Compression option must be a string.");
6359  }
6360  auto file_compression_str =
6361  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6362  if (file_compression_str == "none") {
6364  } else if (file_compression_str == "gzip") {
6366  } else if (file_compression_str == "zip") {
6368  } else {
6369  throw std::runtime_error(
6370  "File Compression option must be 'None', 'GZip', or 'Zip'");
6371  }
6372  } else if (boost::iequals(*p->get_name(), "array_null_handling")) {
6373  const StringLiteral* str_literal =
6374  dynamic_cast<const StringLiteral*>(p->get_value());
6375  if (str_literal == nullptr) {
6376  throw std::runtime_error("Array Null Handling option must be a string.");
6377  }
6378  auto array_null_handling_str =
6379  boost::algorithm::to_lower_copy(*str_literal->get_stringval());
6380  if (array_null_handling_str == "abort") {
6381  array_null_handling =
6383  } else if (array_null_handling_str == "raw") {
6384  array_null_handling =
6386  } else if (array_null_handling_str == "zero") {
6387  array_null_handling =
6389  } else if (array_null_handling_str == "nullfield") {
6390  array_null_handling =
6392  } else {
6393  throw std::runtime_error(
6394  "Array Null Handling option must be 'Abort', 'Raw', 'Zero', or "
6395  "'NullField'");
6396  }
6397  } else {
6398  throw std::runtime_error("Invalid option for COPY: " + *p->get_name());
6399  }
6400  }
6401  }
6402 }
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:1869

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

Referenced by execute(), and ExportQueryStmt().

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

Definition at line 1864 of file ParserNode.h.

Referenced by execute().

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

Definition at line 1869 of file ParserNode.h.

Referenced by ExportQueryStmt(), and parseOptions().

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

Definition at line 1867 of file ParserNode.h.

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


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