OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EmbeddedDbTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <boost/filesystem.hpp>
18 #include <boost/program_options.hpp>
19 #include <exception>
20 #include <iostream>
21 #include "DBEngine.h"
22 
23 #include <arrow/api.h>
24 #include <arrow/csv/reader.h>
25 #include <arrow/io/file.h>
26 #include "Shared/ArrowUtil.h"
27 
28 using namespace EmbeddedDatabase;
29 
30 int main(int argc, char* argv[]) {
31  std::string base_path;
32  int calcite_port = 5555;
33  bool columnar_output = true;
34  namespace po = boost::program_options;
35 
36  po::options_description desc("Options");
37  desc.add_options()("help,h", "Print help messages ")(
38  "data",
39  po::value<std::string>(&base_path)->required(),
40  "Directory path to HeavyDB catalogs")(
41  "calcite-port",
42  po::value<int>(&calcite_port)->default_value(calcite_port),
43  "Calcite port")("columnar-output",
44  po::value<bool>(&columnar_output)->default_value(columnar_output),
45  "Enable columnar_output");
46 
47  po::positional_options_description positionalOptions;
48  positionalOptions.add("data", 1);
49 
50  po::variables_map vm;
51 
52  try {
53  po::store(po::command_line_parser(argc, argv)
54  .options(desc)
55  .positional(positionalOptions)
56  .run(),
57  vm);
58  if (vm.count("help")) {
59  std::cout << desc;
60  return 0;
61  }
62  po::notify(vm);
63  } catch (boost::program_options::error& e) {
64  std::cerr << "Usage Error: " << e.what() << std::endl;
65  return 1;
66  }
67 
68  try {
69  auto opt_str = base_path + " --calcite-port " + std::to_string(calcite_port);
70  if (columnar_output) {
71  opt_str += " --columnar-output";
72  }
73  auto dbe = DBEngine::create(opt_str);
74  if (dbe) {
75  arrow::io::IOContext io_context = arrow::io::default_io_context();
76  auto arrow_parse_options = arrow::csv::ParseOptions::Defaults();
77  auto arrow_read_options = arrow::csv::ReadOptions::Defaults();
78  auto arrow_convert_options = arrow::csv::ConvertOptions::Defaults();
79  std::shared_ptr<arrow::io::ReadableFile> inp;
80  auto file_result = arrow::io::ReadableFile::Open("/localdisk/artemale/test.csv");
81  ARROW_THROW_NOT_OK(file_result.status());
82  inp = file_result.ValueOrDie();
83  auto table_reader_result = arrow::csv::TableReader::Make(io_context,
84  inp,
85  arrow_read_options,
86  arrow_parse_options,
87  arrow_convert_options);
88  ARROW_THROW_NOT_OK(table_reader_result.status());
89  auto table_reader = table_reader_result.ValueOrDie();
90  std::shared_ptr<arrow::Table> arrowTable;
91  auto arrow_table_result = table_reader->Read();
92  ARROW_THROW_NOT_OK(arrow_table_result.status());
93  arrowTable = arrow_table_result.ValueOrDie();
94  dbe->importArrowTable("test", arrowTable);
95 
96  auto schema = dbe->getTableDetails("test");
97  for (auto& item : schema) {
98  std::cout << item.col_name << std::endl;
99  }
100  auto cursor = dbe->executeDML("select * from test");
101  if (cursor) {
102  std::cout << cursor->getRowCount() << " rows selected" << std::endl;
103  std::shared_ptr<arrow::RecordBatch> rbatch = cursor->getArrowRecordBatch();
104  } else {
105  std::cerr << "Cursor is NULL" << std::endl;
106  }
107  }
108  } catch (std::exception& e) {
109  std::cerr << e.what() << "\n";
110  }
111  return 0;
112 }
#define ARROW_THROW_NOT_OK(s)
Definition: ArrowUtil.h:36
std::string to_string(char const *&&v)
tuple dbe
Definition: setup.in.py:24
static std::shared_ptr< DBEngine > create(const std::string &cmd_line)
Definition: DBEngine.cpp:450
static bool run