1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
#include "s3select.h"
#include "gtest/gtest.h"
#include <string>
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
using namespace s3selectEngine;
std::string run_expression_in_C_prog(const char* expression)
{
//purpose: per use-case a c-file is generated, compiles , and finally executed.
// side note: its possible to do the following: cat test_hello.c | gcc -pipe -x c - -o /dev/stdout > ./1
// gcc can read and write from/to pipe (use pipe2()) i.e. not using file-system , BUT should also run gcc-output from memory
const int C_FILE_SIZE=(1024*1024);
std::string c_test_file = std::string("/tmp/test_s3.c");
std::string c_run_file = std::string("/tmp/s3test");
FILE* fp_c_file = fopen(c_test_file.c_str(), "w");
//contain return result
char result_buff[100];
char* prog_c;
if(fp_c_file)
{
prog_c = (char*)malloc(C_FILE_SIZE);
size_t sz=sprintf(prog_c, "#include <stdio.h>\n \
int main() \
{\
printf(\"%%f\\n\",(double)(%s));\
} ", expression);
int status = fwrite(prog_c, 1, sz, fp_c_file);
fclose(fp_c_file);
}
std::string gcc_and_run_cmd = std::string("gcc ") + c_test_file + " -o " + c_run_file + " -Wall && " + c_run_file;
FILE* fp_build = popen(gcc_and_run_cmd.c_str(), "r"); //TODO read stderr from pipe
if(!fp_build)
{
return std::string("#ERROR#");
}
fgets(result_buff, sizeof(result_buff), fp_build);
unlink(c_run_file.c_str());
unlink(c_test_file.c_str());
return std::string(result_buff);
}
#define OPER oper[ rand() % oper.size() ]
class gen_expr
{
private:
int open = 0;
std::string oper= {"+-+*/*"};
std::string gexpr()
{
return std::to_string(rand() % 1000) + ".0" + OPER + std::to_string(rand() % 1000) + ".0";
}
std::string g_openp()
{
if ((rand() % 3) == 0)
{
open++;
return std::string("(");
}
return std::string("");
}
std::string g_closep()
{
if ((rand() % 2) == 0 && open > 0)
{
open--;
return std::string(")");
}
return std::string("");
}
public:
std::string generate()
{
std::string exp = "";
open = 0;
for (int i = 0; i < 10; i++)
{
exp = (exp.size() > 0 ? exp + OPER : std::string("")) + g_openp() + gexpr() + OPER + gexpr() + g_closep();
}
if (open)
for (; open--;)
{
exp += ")";
}
return exp;
}
};
std::string run_s3select(std::string expression)
{
s3select s3select_syntax;
s3select_syntax.parse_query(expression.c_str());
std::string s3select_result;
s3selectEngine::csv_object s3_csv_object(&s3select_syntax);
std::string in = "1,1,1,1\n";
s3_csv_object.run_s3select_on_object(s3select_result, in.c_str(), in.size(), false, false, true);
s3select_result = s3select_result.substr(0, s3select_result.find_first_of(","));
return s3select_result;
}
TEST(TestS3SElect, s3select_vs_C)
{
//purpose: validate correct processing of arithmetical expression, it is done by running the same expression
// in C program.
// the test validate that syntax and execution-tree (including precedence rules) are done correctly
for(int y=0; y<10; y++)
{
gen_expr g;
std::string exp = g.generate();
std::string c_result = run_expression_in_C_prog( exp.c_str() );
char* err=0;
double c_dbl_res = strtod(c_result.c_str(), &err);
std::string input_query = "select " + exp + " from stdin;" ;
std::string s3select_res = run_s3select(input_query);
double s3select_dbl_res = strtod(s3select_res.c_str(), &err);
//std::cout << exp << " " << s3select_dbl_res << " " << s3select_res << " " << c_dbl_res/s3select_dbl_res << std::endl;
//std::cout << exp << std::endl;
ASSERT_EQ(c_dbl_res, s3select_dbl_res);
}
}
TEST(TestS3SElect, ParseQuery)
{
//TODO syntax issues ?
//TODO error messeges ?
s3select s3select_syntax;
run_s3select(std::string("select (1+1) from stdin;"));
ASSERT_EQ(0, 0);
}
TEST(TestS3SElect, int_compare_operator)
{
value a10(10), b11(11), c10(10);
ASSERT_EQ( a10 < b11, true );
ASSERT_EQ( a10 > b11, false );
ASSERT_EQ( a10 >= c10, true );
ASSERT_EQ( a10 <= c10, true );
ASSERT_EQ( a10 != b11, true );
ASSERT_EQ( a10 == b11, false );
ASSERT_EQ( a10 == c10, true );
}
TEST(TestS3SElect, float_compare_operator)
{
value a10(10.1), b11(11.2), c10(10.1);
ASSERT_EQ( a10 < b11, true );
ASSERT_EQ( a10 > b11, false );
ASSERT_EQ( a10 >= c10, true );
ASSERT_EQ( a10 <= c10, true );
ASSERT_EQ( a10 != b11, true );
ASSERT_EQ( a10 == b11, false );
ASSERT_EQ( a10 == c10, true );
}
TEST(TestS3SElect, string_compare_operator)
{
value s1("abc"), s2("def"), s3("abc");
ASSERT_EQ( s1 < s2, true );
ASSERT_EQ( s1 > s2, false );
ASSERT_EQ( s1 <= s3, true );
ASSERT_EQ( s1 >= s3, true );
ASSERT_EQ( s1 != s2, true );
ASSERT_EQ( s1 == s3, true );
ASSERT_EQ( s1 == s2, false );
}
TEST(TestS3SElect, arithmetic_operator)
{
value a(1), b(2), c(3), d(4);
ASSERT_EQ( (a+b).i64(), 3 );
ASSERT_EQ( (value(0)-value(2)*value(4)).i64(), -8 );
ASSERT_EQ( (value(1.23)-value(0.1)*value(2)).dbl(), 1.03 );
a=int64_t(1); //a+b modify a
ASSERT_EQ( ( (a+b) * (c+d) ).i64(), 21 );
}
TEST(TestS3SElect, timestamp_function)
{
// TODO: support formats listed here:
// https://docs.aws.amazon.com/AmazonS3/latest/dev/s3-glacier-select-sql-reference-date.html#s3-glacier-select-sql-reference-to-timestamp
const std::string timestamp = "2007-02-23:14:33:01";
// TODO: out_simestamp should be the same as timestamp
const std::string out_timestamp = "2007-Feb-23 14:33:01";
const std::string input_query = "select timestamp(\"" + timestamp + "\") from stdin;" ;
std::string s3select_res = run_s3select(input_query);
ASSERT_EQ(s3select_res, out_timestamp);
}
TEST(TestS3SElect, utcnow_function)
{
const boost::posix_time::ptime now(boost::posix_time::second_clock::universal_time());
const std::string input_query = "select utcnow() from stdin;" ;
auto s3select_res = run_s3select(input_query);
const boost::posix_time::ptime res_now;
ASSERT_EQ(s3select_res, boost::posix_time::to_simple_string(now));
}
|