From 17d6a993fc17d533460c5f40f3908c708e057c18 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 23 May 2024 18:45:17 +0200 Subject: Merging upstream version 18.2.3. Signed-off-by: Daniel Baumann --- src/s3select/example/s3select_example.cpp | 64 ++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 9 deletions(-) (limited to 'src/s3select/example') diff --git a/src/s3select/example/s3select_example.cpp b/src/s3select/example/s3select_example.cpp index 71aff3d01..3a4b20dc1 100644 --- a/src/s3select/example/s3select_example.cpp +++ b/src/s3select/example/s3select_example.cpp @@ -11,6 +11,9 @@ using namespace s3selectEngine; using namespace BOOST_SPIRIT_CLASSIC_NS; +std::string output_format{}; +std::string header_info{}; + class awsCli_handler { @@ -154,7 +157,7 @@ public: //std::string get_error_description(){} - std::string get_result() + std::string& get_result() { return m_result; } @@ -196,14 +199,20 @@ public: { csv.ignore_header_info = true; } - else if (m_header_info.compare("USE") == 0) + else if (header_info.compare("USE") == 0) { csv.use_header_info = true; } - m_s3_csv_object = std::unique_ptr(new s3selectEngine::csv_object(s3select_syntax.get(), csv)); + if(output_format.compare("JSON") == 0) { + csv.output_json_format = true; + } + + m_s3_csv_object = std::unique_ptr(new s3selectEngine::csv_object()); + m_s3_csv_object->set_csv_query(s3select_syntax.get(), csv); } + if (s3select_syntax->get_error_description().empty() == false) { header_size = create_header_records(m_buff_header.get()); @@ -363,6 +372,8 @@ int process_json_query(const char* input_query,const char* fname) {//purpose: process json query s3select s3select_syntax; + s3selectEngine::json_object m_s3_json_object; + json_object::csv_definitions json; int status = s3select_syntax.parse_query(input_query); if (status != 0) { @@ -370,6 +381,10 @@ int process_json_query(const char* input_query,const char* fname) return -1; } + if(output_format.compare("JSON") == 0) { + json.output_json_format = true; + } + std::ifstream input_file_stream; try { input_file_stream = std::ifstream(fname, std::ios::in | std::ios::binary); @@ -381,7 +396,7 @@ int process_json_query(const char* input_query,const char* fname) } auto object_sz = boost::filesystem::file_size(fname); - json_object json_query_processor(&s3select_syntax); + m_s3_json_object.set_json_query(&s3select_syntax, json); std::string buff(BUFFER_SIZE,0); std::string result; @@ -393,10 +408,11 @@ int process_json_query(const char* input_query,const char* fname) { bytes_read += read_sz; std::cout << "read next chunk " << chunk_count++ << ":" << read_sz << ":" << bytes_read << "\r"; + result.clear(); try{ - status = json_query_processor.run_s3select_on_stream(result, buff.data(), read_sz, object_sz); + status = m_s3_json_object.run_s3select_on_stream(result, buff.data(), read_sz, object_sz, json.output_json_format); } catch (base_s3select_exception &e) { std::cout << e.what() << std::endl; @@ -416,7 +432,7 @@ int process_json_query(const char* input_query,const char* fname) std::cout << "failure upon processing " << std::endl; return -1; } - if(json_query_processor.is_sql_limit_reached()) + if(m_s3_json_object.is_sql_limit_reached()) { std::cout << "json processing reached limit " << std::endl; break; @@ -425,7 +441,7 @@ int process_json_query(const char* input_query,const char* fname) } try{ result.clear(); - json_query_processor.run_s3select_on_stream(result, 0, 0, object_sz); + m_s3_json_object.run_s3select_on_stream(result, 0, 0, object_sz, json.output_json_format); } catch (base_s3select_exception &e) { std::cout << e.what() << std::endl; @@ -499,6 +515,14 @@ int run_on_localFile(char* input_query) csv.use_header_info = false; csv.quote_fields_always=false; + if(output_format.compare("JSON") == 0) { + csv.output_json_format = true; + } + + if(header_info.compare("USE") == 0) { + csv.use_header_info = true; + } + #define CSV_QUOT "CSV_ALWAYS_QUOT" #define CSV_COL_DELIM "CSV_COLUMN_DELIMETER" #define CSV_ROW_DELIM "CSV_ROW_DELIMITER" @@ -521,7 +545,8 @@ int run_on_localFile(char* input_query) csv.use_header_info = true; } - s3selectEngine::csv_object s3_csv_object(&s3select_syntax, csv); + s3selectEngine::csv_object s3_csv_object; + s3_csv_object.set_csv_query(&s3select_syntax, csv); std::function fp_debug = [](const char* msg) { @@ -585,6 +610,14 @@ int run_on_localFile(char* input_query) return 0; } +std::string get_ranged_string(std::string& inp) +{ + size_t startPos = inp.find(""); + size_t endPos = inp.find(""); + + return inp.substr(startPos,endPos-startPos); +} + int run_on_single_query(const char* fname, const char* query) { @@ -632,11 +665,12 @@ int run_on_single_query(const char* fname, const char* query) if(status<0) { std::cout << "failure on execution " << std::endl; + std::cout << get_ranged_string( awscli->get_result() ) << std::endl; break; } else { - std::cout << awscli->get_result() << std::endl; + std::cout << get_ranged_string( awscli->get_result() ) << std::endl; } if(!read_sz || input_file_stream.eof()) @@ -662,12 +696,24 @@ int main(int argc,char **argv) continue; } + if (!strcmp(argv[i], "-output")) + {//object recieved as CLI parameter + output_format = argv[i + 1]; + continue; + } + if (!strcmp(argv[i], "-q")) { query = argv[i + 1]; continue; } + if (!strcmp(argv[i], "-HeaderInfo")) + { + header_info = argv[i + 1]; + continue; + } + if (!strcmp(argv[i], "-cmds")) {//query file contain many queries query_file = argv[i + 1]; -- cgit v1.2.3