OmniSciDB  04ee39c94c
StorageTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 <csignal>
18 #include <cstring>
19 #include <iostream>
20 #include <string>
21 
22 #include <cstdlib>
23 #include <exception>
24 #include <memory>
25 
26 #include <thread>
27 
28 #include <boost/functional/hash.hpp>
29 #include "../Analyzer/Analyzer.h"
30 #include "../Catalog/Catalog.h"
31 #include "../DataMgr/DataMgr.h"
32 #include "../Fragmenter/Fragmenter.h"
33 #include "../Parser/ParserNode.h"
34 #include "../Parser/parser.h"
35 #include "../QueryRunner/QueryRunner.h"
36 #include "PopulateTableRandom.h"
37 #include "ScanTable.h"
38 #include "Shared/MapDParameters.h"
39 #include "TestHelpers.h"
40 #include "boost/filesystem.hpp"
41 #include "boost/program_options.hpp"
42 #include "gtest/gtest.h"
43 
44 using namespace std;
45 using namespace Catalog_Namespace;
46 using namespace Analyzer;
47 using namespace Fragmenter_Namespace;
48 
49 #ifndef BASE_PATH
50 #define BASE_PATH "./tmp"
51 #endif
52 
54 namespace {
55 
56 inline void run_ddl_statement(const string& input_str) {
57  QR::get()->runDDLStatement(input_str);
58 }
59 
60 bool storage_test(const string& table_name, size_t num_rows) {
61  vector<size_t> insert_col_hashs =
62  populate_table_random(table_name, num_rows, *QR::get()->getCatalog());
63  vector<size_t> scan_col_hashs =
64  scan_table_return_hash(table_name, *QR::get()->getCatalog());
65  vector<size_t> scan_col_hashs2 =
66  scan_table_return_hash_non_iter(table_name, *QR::get()->getCatalog());
67  return insert_col_hashs == scan_col_hashs && insert_col_hashs == scan_col_hashs2;
68 }
69 
70 void simple_thread_wrapper(const string& table_name, size_t num_rows, size_t thread_id) {
71  populate_table_random(table_name, num_rows, *QR::get()->getCatalog());
72 }
73 
74 bool storage_test_parallel(const string& table_name,
75  size_t num_rows,
76  size_t thread_count) {
77  // Constructs a number of threads and have them push records to the table in parallel
78  vector<std::thread> myThreads;
79  for (size_t i = 0; i < thread_count; i++) {
80  myThreads.emplace_back(simple_thread_wrapper, table_name, num_rows / thread_count, i);
81  }
82  for (auto& t : myThreads) {
83  t.join();
84  }
85  vector<size_t> scan_col_hashs =
86  scan_table_return_hash(table_name, *QR::get()->getCatalog());
87  vector<size_t> scan_col_hashs2 =
88  scan_table_return_hash_non_iter(table_name, *QR::get()->getCatalog());
89  return scan_col_hashs == scan_col_hashs2;
90 }
91 } // namespace
92 
93 #define SMALL 100000
94 #define LARGE 1000000
95 
96 TEST(StorageLarge, Numbers) {
97  ASSERT_NO_THROW(run_ddl_statement("drop table if exists numbers;"););
98  ASSERT_NO_THROW(
100  "create table numbers (a smallint, b int, c bigint, d numeric(17,3), e "
101  "double, f float);"););
102  EXPECT_TRUE(storage_test("numbers", LARGE));
103  ASSERT_NO_THROW(run_ddl_statement("drop table numbers;"););
104 }
105 
106 TEST(StorageSmall, Strings) {
107  ASSERT_NO_THROW(run_ddl_statement("drop table if exists strings;"););
108  ASSERT_NO_THROW(
110  "create table strings (x varchar(10) encoding none, y text encoding none);"););
111  EXPECT_TRUE(storage_test("strings", SMALL));
112  ASSERT_NO_THROW(run_ddl_statement("drop table strings;"););
113 }
114 
115 TEST(StorageSmall, AllTypes) {
116  ASSERT_NO_THROW(run_ddl_statement("drop table if exists alltypes;"););
117  ASSERT_NO_THROW(
118  run_ddl_statement("create table alltypes (a smallint, b int, c bigint, d "
119  "numeric(17,3), e double, f float, "
120  "g timestamp(0), g_3 timestamp(3), g_6 timestamp(6), g_9 "
121  "timestamp(9), h time(0), i date, "
122  "x varchar(10) encoding none, y text encoding none);"););
123  EXPECT_TRUE(storage_test("alltypes", SMALL));
124  ASSERT_NO_THROW(run_ddl_statement("drop table alltypes;"););
125 }
126 
127 TEST(StorageRename, AllTypes) {
128  ASSERT_NO_THROW(run_ddl_statement("drop table if exists original_table;"););
129  ASSERT_NO_THROW(
130  run_ddl_statement("create table original_table (a smallint, b int, c bigint, d "
131  "numeric(17,3), e double, f float, "
132  "g timestamp(0), g_3 timestamp(3), g_6 timestamp(6), g_9 "
133  "timestamp(9), h time(0), i date, "
134  "x varchar(10) encoding none, y text encoding none);"););
135  EXPECT_TRUE(storage_test("original_table", SMALL));
136 
137  ASSERT_NO_THROW(run_ddl_statement("drop table if exists new_table;"););
138  ASSERT_NO_THROW(
139  run_ddl_statement("create table new_table (a smallint, b int, c bigint, d "
140  "numeric(17,3), e double, f float, "
141  "g timestamp(0), g_3 timestamp(3), g_6 timestamp(6), g_9 "
142  "timestamp(9), h time(0), i date, "
143  "x varchar(10) encoding none, y text encoding none);"););
144  EXPECT_TRUE(storage_test("new_table", SMALL));
145 
146  ASSERT_NO_THROW(run_ddl_statement("alter table original_table rename to old_table;"););
147 
148  ASSERT_NO_THROW(run_ddl_statement("alter table new_table rename to original_table;"););
149 
150  ASSERT_NO_THROW(run_ddl_statement("drop table old_table;"););
151 
152  ASSERT_NO_THROW(
153  run_ddl_statement("create table new_table (a smallint, b int, c bigint, d "
154  "numeric(17,3), e double, f float, "
155  "g timestamp(0), g_3 timestamp(3), g_6 timestamp(6), g_9 "
156  "timestamp(9), h time(0), i date, "
157  "x varchar(10) encoding none, y text encoding none);"););
158 
159  ASSERT_NO_THROW(run_ddl_statement("drop table original_table;"););
160  ASSERT_NO_THROW(run_ddl_statement("drop table new_table;"););
161 }
162 
163 TEST(StorageSmallParallel, AllTypes) {
164  ASSERT_NO_THROW(run_ddl_statement("drop table if exists alltypes;"););
165  ASSERT_NO_THROW(
167  "create table alltypes (a smallint, b int, c bigint, d numeric(17,3), e "
168  "double, f float, g timestamp(0), g_3 timestamp(3), g_6 timestamp(6), g_9 "
169  "timestamp(9), h time(0), i date, x varchar(10) encoding none, y text encoding "
170  "none);"););
171  EXPECT_TRUE(
172  storage_test_parallel("alltypes", SMALL, std::thread::hardware_concurrency()));
173  ASSERT_NO_THROW(run_ddl_statement("drop table alltypes;"););
174 }
175 
176 int main(int argc, char* argv[]) {
178  ::testing::InitGoogleTest(&argc, argv);
179 
181 
182  int err{0};
183  try {
184  err = RUN_ALL_TESTS();
185  } catch (const std::exception& e) {
186  LOG(ERROR) << e.what();
187  }
188  QR::reset();
189  return err;
190 }
#define SMALL
Definition: StorageTest.cpp:93
const int8_t const int64_t * num_rows
vector< size_t > scan_table_return_hash(const string &table_name, const Catalog &cat)
Definition: ScanTable.cpp:108
#define LOG(tag)
Definition: Logger.h:182
Populate a table with random data.
vector< size_t > scan_table_return_hash_non_iter(const string &table_name, const Catalog &cat)
Definition: ScanTable.cpp:145
TEST(StorageLarge, Numbers)
Definition: StorageTest.cpp:96
Scan through each column of a table via Chunk iterators.
static QueryRunner * init(const char *db_path, const std::string &udf_filename="", const size_t max_gpu_mem=0, const int reserved_gpu_mem=256<< 20)
Definition: QueryRunner.h:70
void simple_thread_wrapper(const string &table_name, size_t num_rows, size_t thread_id)
Definition: StorageTest.cpp:70
bool storage_test(const string &table_name, size_t num_rows)
Definition: StorageTest.cpp:60
std::vector< size_t > populate_table_random(const std::string &table_name, const size_t num_rows, const Catalog &cat)
virtual void runDDLStatement(const std::string &)
static QueryRunner * get()
Definition: QueryRunner.h:115
int main(int argc, char *argv[])
bool storage_test_parallel(const string &table_name, size_t num_rows, size_t thread_count)
Definition: StorageTest.cpp:74
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194
#define LARGE
Definition: StorageTest.cpp:94
#define BASE_PATH
Definition: StorageTest.cpp:50
void run_ddl_statement(std::string ddl)