-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.cpp
339 lines (331 loc) · 9.79 KB
/
bridge.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "bridge.h"
#include "loops.h"
using namespace std;
using namespace clickhouse;
void* chc_construct(char* host, char* username, char* password, char* default_database, long port, bool compression) {
try {
ClientOptions opts;
if (host)
opts.SetHost(string(host));
if (username)
opts.SetUser(string(username));
if (password)
opts.SetPassword(string(password));
if (default_database)
opts.SetDefaultDatabase(string(default_database));
if (port)
opts.SetPort(port);
if (compression)
opts.SetCompressionMethod(CompressionMethod::LZ4);
Client* client = new Client(opts);
return (void*)client;
} catch (const std::system_error& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
return NULL;
} catch (const clickhouse::ServerException& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
return NULL;
}
}
void chc_destruct(void* instance) {
Client* client = (Client*)instance;
if (client)
delete client;
}
void chc_execute(void* instance, char* query) {
Client* client = (Client*)instance;
try {
client->Execute(string(query));
} catch (const std::system_error& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
} catch (const clickhouse::ServerException& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
}
}
void chc_ping(void* instance) {
Client* client = (Client*)instance;
try {
client->Ping();
} catch (const std::system_error& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
} catch (const clickhouse::ServerException& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
}
}
void chc_reset_connection(void* instance) {
Client* client = (Client*)instance;
try {
client->ResetConnection();
} catch (const std::system_error& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
} catch (const clickhouse::ServerException& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
}
}
size_t chc_select(void* instance, char* query, zend_fcall_info* fci, zend_fcall_info_cache* fci_cache) {
Client* client = (Client*)instance;
try {
size_t total = 0;
auto onBlock = [&fci, &fci_cache, &total] (const Block& dblock) -> bool
{
zval result, block;
int ret;
char buf48[48];
size_t rowCount = dblock.GetRowCount(), colCount = dblock.GetColumnCount();
if (rowCount == 0)
return true;
total += rowCount;
/* Build an array from resulting block */
array_init_size(&block, rowCount);
zval rows[rowCount];
for (size_t i = 0; i < rowCount; ++i) {
array_init_size(&rows[i], colCount);
add_next_index_zval(&block, &rows[i]);
}
/* Iterate over columns */
for (size_t col = 0; col < colCount; ++col) {
#ifndef OVEROPTIMIZATION
const char* colName = dblock.GetColumnName(col).c_str();
#endif
switch (dblock[col]->Type()->GetCode()) {
case Type::Code::Int8:
{
auto colCast = dblock[col]->As<ColumnInt8>();
LOOP_AS_LONG;
}
case Type::Code::UInt8:
{
auto colCast = dblock[col]->As<ColumnUInt8>();
LOOP_AS_LONG;
}
case Type::Code::Int16:
{
auto colCast = dblock[col]->As<ColumnInt16>();
LOOP_AS_LONG;
}
case Type::Code::UInt16:
{
auto colCast = dblock[col]->As<ColumnUInt16>();
LOOP_AS_LONG;
}
case Type::Code::Int32:
{
auto colCast = dblock[col]->As<ColumnInt32>();
LOOP_AS_LONG;
}
case Type::Code::UInt32:
{
auto colCast = dblock[col]->As<ColumnUInt32>();
LOOP_AS_LONG;
}
case Type::Code::Int64:
{
auto colCast = dblock[col]->As<ColumnInt64>();
LOOP_AS_LONG;
}
case Type::Code::UInt64:
{
auto colCast = dblock[col]->As<ColumnUInt64>();
LOOP_AS_X(int128_to_pchar);
}
case Type::Code::Int128:
{
/* https://github.com/ClickHouse/ClickHouse/issues/746 */
auto colCast = dblock[col]->As<ColumnInt128>();
LOOP_AS_X(int128_to_pchar);
}
case Type::Code::Decimal:
case Type::Code::Decimal32:
case Type::Code::Decimal64:
case Type::Code::Decimal128:
{
auto colCast = dblock[col]->As<ColumnDecimal>();
size_t scale = dblock[col]->Type()->As<DecimalType>()->GetScale();
LOOP_AS_DEC128(scale);
}
case Type::Code::Float32:
{
auto colCast = dblock[col]->As<ColumnFloat32>();
LOOP_AS_DOUBLE;
}
case Type::Code::Float64:
{
auto colCast = dblock[col]->As<ColumnFloat64>();
LOOP_AS_DOUBLE;
}
case Type::Code::Enum8:
{
auto colCast = dblock[col]->As<ColumnEnum8>();
LOOP_AS_ENUM;
}
case Type::Code::Enum16:
{
auto colCast = dblock[col]->As<ColumnEnum16>();
LOOP_AS_ENUM;
}
case Type::Code::String:
{
auto colCast = dblock[col]->As<ColumnString>();
LOOP_AS_STRING;
}
case Type::Code::FixedString:
{
auto colCast = dblock[col]->As<ColumnFixedString>();
LOOP_AS_STRING;
}
case Type::Code::Date:
{
auto colCast = dblock[col]->As<ColumnDate>();
#ifdef DATE_STRINGIFY
LOOP_AS_X(date_to_pchar);
#else
LOOP_AS_LONG;
#endif
}
case Type::Code::DateTime:
{
auto colCast = dblock[col]->As<ColumnDateTime>();
#ifdef DATE_STRINGIFY
LOOP_AS_X(datetime_to_pchar);
#else
LOOP_AS_LONG;
#endif
}
case Type::Code::Nullable:
{
auto outerColCast = dblock[col]->As<ColumnNullable>();
switch (outerColCast->Type()->As<NullableType>()->GetNestedType()->GetCode()) {
case Type::Code::Int8:
{
auto colCast = outerColCast->Nested()->As<ColumnInt8>();
LOOP_NULLABLE_AS_LONG;
}
case Type::Code::UInt8:
{
auto colCast = outerColCast->Nested()->As<ColumnUInt8>();
LOOP_NULLABLE_AS_LONG;
}
case Type::Code::Int16:
{
auto colCast = outerColCast->Nested()->As<ColumnInt16>();
LOOP_NULLABLE_AS_LONG;
}
case Type::Code::UInt16:
{
auto colCast = outerColCast->Nested()->As<ColumnUInt16>();
LOOP_NULLABLE_AS_LONG;
}
case Type::Code::Int32:
{
auto colCast = outerColCast->Nested()->As<ColumnInt32>();
LOOP_NULLABLE_AS_LONG;
}
case Type::Code::UInt32:
{
auto colCast = outerColCast->Nested()->As<ColumnUInt32>();
LOOP_NULLABLE_AS_LONG;
}
case Type::Code::Int64:
{
auto colCast = outerColCast->Nested()->As<ColumnInt64>();
LOOP_NULLABLE_AS_LONG;
}
case Type::Code::UInt64:
{
auto colCast = outerColCast->Nested()->As<ColumnUInt64>();
LOOP_NULLABLE_AS_X(int128_to_pchar);
}
case Type::Code::Int128:
{
auto colCast = dblock[col]->As<ColumnInt128>();
LOOP_NULLABLE_AS_X(int128_to_pchar);
}
case Type::Code::Decimal:
case Type::Code::Decimal32:
case Type::Code::Decimal64:
case Type::Code::Decimal128:
{
auto colCast = outerColCast->Nested()->As<ColumnDecimal>();
size_t scale = outerColCast->Type()->As<NullableType>()->GetNestedType()->As<DecimalType>()->GetScale();
LOOP_NULLABLE_AS_DEC128(scale);
}
case Type::Code::Float32:
{
auto colCast = outerColCast->Nested()->As<ColumnFloat32>();
LOOP_NULLABLE_AS_DOUBLE;
}
case Type::Code::Float64:
{
auto colCast = outerColCast->Nested()->As<ColumnFloat64>();
LOOP_NULLABLE_AS_DOUBLE;
}
case Type::Code::Enum8:
{
auto colCast = outerColCast->Nested()->As<ColumnEnum8>();
LOOP_NULLABLE_AS_ENUM;
}
case Type::Code::Enum16:
{
auto colCast = outerColCast->Nested()->As<ColumnEnum16>();
LOOP_NULLABLE_AS_ENUM;
}
case Type::Code::String:
{
auto colCast = outerColCast->Nested()->As<ColumnString>();
LOOP_NULLABLE_AS_STRING;
}
case Type::Code::FixedString:
{
auto colCast = outerColCast->Nested()->As<ColumnFixedString>();
LOOP_NULLABLE_AS_STRING;
}
case Type::Code::Date:
{
auto colCast = outerColCast->Nested()->As<ColumnDate>();
#ifdef DATE_STRINGIFY
LOOP_NULLABLE_AS_X(date_to_pchar);
#else
LOOP_NULLABLE_AS_LONG;
#endif
}
case Type::Code::DateTime:
{
auto colCast = outerColCast->Nested()->As<ColumnDateTime>();
#ifdef DATE_STRINGIFY
LOOP_NULLABLE_AS_X(datetime_to_pchar);
#else
LOOP_NULLABLE_AS_LONG;
#endif
}
default:
{
LOOP_AS_NULL;
}
}
}
default:
{
LOOP_AS_NULL;
}
}
}
/* Send to callback */
fci->retval = &result;
fci->param_count = 1;
fci->params = █
fci->no_separation = 0;
ret = zend_call_function(fci, fci_cache);
zval_ptr_dtor(&block);
/* If not a boolean is returned, assume continue */
if ((Z_TYPE(result) != IS_TRUE) && (Z_TYPE(result) != IS_FALSE))
return true;
return (Z_TYPE(result) == IS_TRUE);
};
client->SelectCancelable(string(query), onBlock);
return total;
} catch (const std::system_error& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
} catch (const clickhouse::ServerException& e) {
zend_throw_exception_ex(zend_exception_get_default(), 1 TSRMLS_CC, e.what());
}
}