-
Notifications
You must be signed in to change notification settings - Fork 33
/
imcs.h
380 lines (346 loc) · 10.3 KB
/
imcs.h
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#ifndef __IMCS_H__
#define __IMCS_H__
#include <postgres.h>
#include <access/hash.h>
#include <access/xact.h>
#include <funcapi.h>
#include <storage/ipc.h>
#include <miscadmin.h>
#include <utils/guc.h>
#include <utils/builtins.h>
#include <utils/lsyscache.h>
#include <utils/memutils.h>
#include <utils/typcache.h>
#include <catalog/pg_type.h>
#include <catalog/pg_attrdef.h>
#include <catalog/namespace.h>
#include <libpq/pqformat.h>
struct imcs_page_t_;
typedef struct imcs_page_t_ imcs_page_t;
typedef uint64 imcs_pos_t;
typedef uint64 imcs_count_t;
extern int imcs_page_size;
extern int imcs_tile_size;
extern int imcs_dict_size;
extern bool imcs_sync_load;
extern bool imcs_use_rle;
extern int imcs_cache_size;
extern char* imcs_file_path;
#define IMCS_INFINITY (-1)
#define IMCS_MAX_ERROR_MSG_LEN 256
#define IMCS_SMALL_DICTIONARY 0x10000 /* 64kb - identifier can be stored in two bytes */
#ifdef __GNUC__
#define IMCS_ALIGN_16 __attribute__((aligned(16)))
#else
#define IMCS_ALIGN_16
#endif
typedef union
{
int8 val_int8;
int16 val_int16;
int32 val_int32;
int64 val_int64;
float val_float;
double val_double;
char* val_ptr;
} imcs_key_t;
typedef union
{
char arr_char[1];
int8 arr_int8[1];
int16 arr_int16[1];
int32 arr_int32[1];
int64 arr_int64[1];
float arr_float[1];
double arr_double[1];
} imcs_tile_t IMCS_ALIGN_16;
typedef enum
{
TID_int8,
TID_int16,
TID_int32,
TID_date,
TID_int64,
TID_time,
TID_timestamp,
TID_money,
TID_float,
TID_double,
TID_char
} imcs_elem_typeid_t;
extern const imcs_elem_typeid_t imcs_underlying_type[];
extern const char* const imcs_type_mnems[];
extern const Oid imcs_elem_type_to_oid[];
typedef enum {
IMCS_ASC_ORDER,
IMCS_DESC_ORDER
} imcs_order_t;
struct imcs_iterator_t_;
typedef bool(*imcs_iterator_next_t)(struct imcs_iterator_t_* iterator);
typedef void(*imcs_iterator_reset_t)(struct imcs_iterator_t_* iterator);
typedef bool(*imcs_iterator_prepare_t)(struct imcs_iterator_t_* iterator);
typedef void(*imcs_iterator_merge_t)(struct imcs_iterator_t_* dst, struct imcs_iterator_t_* src);
/**
* Timeseries header
*/
typedef struct imcs_timeseries_t
{
imcs_page_t* root_page; /* root of B-Tree */
imcs_elem_typeid_t elem_type;
bool is_timestamp;
int elem_size;
imcs_count_t count;
} imcs_timeseries_t;
typedef enum
{
FLAG_RANDOM_ACCESS = 1, /* it is timeseries stored as B-Tree, supporting random acess by position */
FLAG_CONTEXT_FREE = 2, /* each element can be calculated independetly: such timeseries allows concurrent execution */
FLAG_PREPARED = 4, /* result was already prepared by prepare() function during parallel query execution */
FLAG_CONSTANT = 8, /* timeries of repeated costant element */
FLAG_TRANSLATED = 16 /* character element value was replaced with integer identifier using dictionary */
} imcs_flags_t;
typedef struct
{
jmp_buf unwind_buf;
int err_code;
char err_msg[IMCS_MAX_ERROR_MSG_LEN];
} imcs_error_handler_t;
extern void imcs_ereport(int err_code, char const* err_msg,...) __attribute__((noreturn));
typedef struct {
char* val;
size_t len;
} imcs_dict_key_t;
typedef struct {
imcs_dict_key_t key;
size_t code;
} imcs_dict_entry_t;
extern imcs_dict_entry_t** imcs_dict_code_map;
enum imcs_commands
{
imcs_cmd_parse,
imcs_cmd_const,
imcs_cmd_cast,
imcs_cmd_add,
imcs_cmd_mul,
imcs_cmd_sub,
imcs_cmd_div,
imcs_cmd_mod,
imcs_cmd_pow,
imcs_cmd_and,
imcs_cmd_or,
imcs_cmd_xor,
imcs_cmd_concat,
imcs_cmd_cat,
imcs_cmd_eq,
imcs_cmd_ne,
imcs_cmd_ge,
imcs_cmd_le,
imcs_cmd_lt,
imcs_cmd_gt,
imcs_cmd_maxof,
imcs_cmd_minof,
imcs_cmd_neg,
imcs_cmd_not,
imcs_cmd_bit_not,
imcs_cmd_abs,
imcs_cmd_limit,
imcs_cmd_sin,
imcs_cmd_cos,
imcs_cmd_tan,
imcs_cmd_exp,
imcs_cmd_asin,
imcs_cmd_acos,
imcs_cmd_atan,
imcs_cmd_sqrt,
imcs_cmd_log,
imcs_cmd_ceil,
imcs_cmd_floor,
imcs_cmd_isnan,
imcs_cmd_wsum,
imcs_cmd_wavg,
imcs_cmd_corr,
imcs_cmd_cov,
imcs_cmd_norm,
imcs_cmd_thin,
imcs_cmd_iif,
imcs_cmd_if,
imcs_cmd_filter,
imcs_cmd_filter_pos,
imcs_cmd_filter_first_pos,
imcs_cmd_unique,
imcs_cmd_reverse,
imcs_cmd_diff,
imcs_cmd_trend,
imcs_cmd_repeat,
imcs_cmd_count,
imcs_cmd_approxdc,
imcs_cmd_max,
imcs_cmd_min,
imcs_cmd_avg,
imcs_cmd_sum,
imcs_cmd_prd,
imcs_cmd_var,
imcs_cmd_dev,
imcs_cmd_all,
imcs_cmd_any,
imcs_cmd_median,
imcs_cmd_group_count,
imcs_cmd_group_approxdc,
imcs_cmd_group_max,
imcs_cmd_group_min,
imcs_cmd_group_avg,
imcs_cmd_group_sum,
imcs_cmd_group_var,
imcs_cmd_group_dev,
imcs_cmd_group_all,
imcs_cmd_group_any,
imcs_cmd_group_last,
imcs_cmd_group_first,
imcs_cmd_win_group_max,
imcs_cmd_win_group_min,
imcs_cmd_win_group_avg,
imcs_cmd_win_group_sum,
imcs_cmd_win_group_var,
imcs_cmd_win_group_dev,
imcs_cmd_win_group_all,
imcs_cmd_win_group_any,
imcs_cmd_win_group_last,
imcs_cmd_win_group_first,
imcs_cmd_grid_max,
imcs_cmd_grid_min,
imcs_cmd_grid_avg,
imcs_cmd_grid_sum,
imcs_cmd_grid_var,
imcs_cmd_grid_dev,
imcs_cmd_window_max,
imcs_cmd_window_min,
imcs_cmd_window_avg,
imcs_cmd_window_sum,
imcs_cmd_window_var,
imcs_cmd_window_dev,
imcs_cmd_window_ema,
imcs_cmd_window_atr,
imcs_cmd_hash_count,
imcs_cmd_hash_dup_count,
imcs_cmd_hash_max,
imcs_cmd_hash_min,
imcs_cmd_hash_avg,
imcs_cmd_hash_sum,
imcs_cmd_hash_all,
imcs_cmd_hash_any,
imcs_cmd_top_max,
imcs_cmd_top_min,
imcs_cmd_top_max_pos,
imcs_cmd_top_min_pos,
imcs_cmd_cum_max,
imcs_cmd_cum_min,
imcs_cmd_cum_avg,
imcs_cmd_cum_sum,
imcs_cmd_cum_prd,
imcs_cmd_cum_var,
imcs_cmd_cum_dev,
imcs_cmd_histogram,
imcs_cmd_cross,
imcs_cmd_extrema,
imcs_cmd_stretch,
imcs_cmd_stretch0,
imcs_cmd_asof_join,
imcs_cmd_asof_join_pos,
imcs_cmd_join,
imcs_cmd_join_pos,
imcs_cmd_map,
imcs_cmd_union,
imcs_cmd_empty,
imcs_cmd_project,
imcs_cmd_project_agg,
imcs_cmd_year,
imcs_cmd_month,
imcs_cmd_mday,
imcs_cmd_wday,
imcs_cmd_hour,
imcs_cmd_minute,
imcs_cmd_second,
imcs_cmd_week,
imcs_cmd_quarter,
imcs_cmd_call,
imcs_cmd_to_array,
imcs_cmd_from_array,
imcs_cmd_like,
imcs_cmd_ilike,
imcs_cmd_sort,
imcs_cmd_sort_pos,
imcs_cmd_rank,
imcs_cmd_dense_rank,
imcs_cmd_quantile,
imcs_cmd_last_command,
imcs_cmd_int8_from = imcs_cmd_cast,
imcs_cmd_int16_from = imcs_cmd_cast,
imcs_cmd_int32_from = imcs_cmd_cast,
imcs_cmd_int64_from = imcs_cmd_cast,
imcs_cmd_float_from = imcs_cmd_cast,
imcs_cmd_double_from = imcs_cmd_cast,
imcs_cmd_stretch_int32 = imcs_cmd_stretch,
imcs_cmd_stretch_int64 = imcs_cmd_stretch,
imcs_cmd_stretch0_int32 = imcs_cmd_stretch0,
imcs_cmd_stretch0_int64 = imcs_cmd_stretch0,
imcs_cmd_asof_join_int32 = imcs_cmd_asof_join,
imcs_cmd_asof_join_int64 = imcs_cmd_asof_join,
imcs_cmd_join_int32 = imcs_cmd_join,
imcs_cmd_join_int64 = imcs_cmd_join,
imcs_cmd_int8_call = imcs_cmd_call,
imcs_cmd_int16_call = imcs_cmd_call,
imcs_cmd_int32_call = imcs_cmd_call,
imcs_cmd_int64_call = imcs_cmd_call,
imcs_cmd_float_call = imcs_cmd_call,
imcs_cmd_double_call = imcs_cmd_call
};
/*
* Timeseries iterator: it is used not only for iteration but for constructing pipe of different operations with sequence
*/
typedef struct imcs_iterator_t_
{
imcs_iterator_next_t next; /* method for obtaning next portion of values */
imcs_iterator_reset_t reset; /* start iteration from beginning */
imcs_iterator_prepare_t prepare; /* prepare iterator (used to start parallel processing) */
imcs_iterator_merge_t merge; /* merge two iterators (used to merge results of parallel processing) */
uint16 elem_size; /* size of element */
uint16 tile_size; /* number of tile items */
uint16 tile_offs; /* offset to first not handled tile item */
uint8 rle_offs; /* index within same RLE value */
uint8 flags; /* bitmap of imcs_flags_t flags */
imcs_pos_t first_pos; /* first sequence number (inclusive) */
imcs_pos_t next_pos; /* sequence number of element returned by sudsequent invocation of next() function */
imcs_pos_t last_pos; /* last sequence number (inclusive) */
struct imcs_iterator_t_* opd[3]; /*operands of sequence operator */
imcs_elem_typeid_t elem_type; /* result element type */
uint32 iterator_size; /* size fo iterator + tile data + context */
imcs_timeseries_t* cs_hdr; /* header of stored timeseries, NULL for sequence iterators */
void* context;
imcs_tile_t tile; /* tile of values */
} imcs_iterator_t, *imcs_iterator_h;
void* imcs_alloc(size_t size);
void imcs_free(void* ptr);
void* imcs_alloc_aligned(size_t size);
uint64 imcs_used_memory(void);
imcs_timeseries_t* imcs_get_timeseries(char const* id, imcs_elem_typeid_t elem_type, bool is_timestamp, int elem_size, bool create);
imcs_page_t* imcs_new_page(void);
void imcs_free_page(imcs_page_t* pg);
imcs_iterator_h imcs_new_iterator(size_t elem_size, size_t context_size);
imcs_iterator_h imcs_clone_iterator(imcs_iterator_h iterator);
void imcs_reset_iterator(imcs_iterator_h);
imcs_iterator_h imcs_cast(imcs_iterator_h input, imcs_elem_typeid_t elem_type, int elem_size);
imcs_count_t imcs_count(imcs_iterator_h input);
imcs_iterator_h imcs_parallel_iterator(imcs_iterator_h iterator);
struct imcs_adt_parser_t;
typedef Datum (*imcs_adt_parse_t)(struct imcs_adt_parser_t* parser, char* value, size_t size);
typedef struct imcs_adt_parser_t {
imcs_adt_parse_t parse;
FmgrInfo proc;
Oid input_oid;
Oid param_oid;
} imcs_adt_parser_t;
static inline imcs_iterator_h imcs_operand(imcs_iterator_h iterator)
{
return (iterator->opd[0] == NULL) ? imcs_clone_iterator(iterator) : iterator; /* clone leaves */
}
#endif