rangeMergeTest.cpp 8.2 KB
Newer Older
W
wpan 已提交
1 2 3 4 5 6 7 8 9 10 11 12
#include <gtest/gtest.h>
#include <iostream>

#include "qResultbuf.h"
#include "taos.h"
#include "taosdef.h"

#include "qFilter.h"

#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"

W
wpan 已提交
13
extern "C" {
W
wpan 已提交
14 15 16 17
  extern void* filterInitRangeCtx(int32_t type, int32_t options);
  extern int32_t filterGetRangeNum(void* h, int32_t* num);
  extern int32_t filterGetRangeRes(void* h, SFilterRange *ra);
  extern int32_t filterFreeRangeCtx(void* h);
W
wpan 已提交
18
  extern int32_t filterAddRange(void* h, SFilterRange* ra, int32_t optr);
W
wpan 已提交
19 20
}

W
wpan 已提交
21 22
namespace {

W
wpan 已提交
23

W
wpan 已提交
24 25
void intDataTest() {
  printf("running %s\n", __FUNCTION__);
W
wpan 已提交
26
  int32_t asize = 0;
W
wpan 已提交
27
  SFilterRange ra[10] = {0};
W
wpan 已提交
28 29 30 31
  int64_t *s =NULL;
  int64_t *e =NULL;
  int64_t s0[3] = {-100, 1, 3};
  int64_t e0[3] = {0   , 2, 4};
W
wpan 已提交
32 33 34 35 36 37
  int64_t s1[3] = {INT64_MIN, 0 , 3};
  int64_t e1[3] = {100      , 50, 4};
  int64_t s2[5] = {1 , 3  , 10,30,70};
  int64_t e2[5] = {10, 100, 20,50,120};
  int64_t s3[3] = {1 , 20 , 5};
  int64_t e3[3] = {10, 100, 25};
W
wpan 已提交
38 39 40
  int64_t s4[2] = {10, 0};
  int64_t e4[2] = {20, 5};
  int64_t s5[3] = {0, 6 ,7};
W
wpan 已提交
41
  int64_t e5[3] = {4, 10,20};
W
wpan 已提交
42

W
wpan 已提交
43 44
  int64_t rs[10];
  int64_t re[10];
W
wpan 已提交
45 46

  int32_t num = 0;
W
wpan 已提交
47
  void *h = NULL;
W
wpan 已提交
48 49 50

  s = s0;
  e = e0;
W
wpan 已提交
51 52
  asize = sizeof(s0)/sizeof(s[0]);  
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
53
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
54
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
55 56
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
57
    filterAddRange(h, ra, TSDB_RELATION_AND);
W
wpan 已提交
58
  }
W
wpan 已提交
59
  filterGetRangeNum(h, &num);
W
wpan 已提交
60
  ASSERT_EQ(num, 0);
W
wpan 已提交
61
  filterFreeRangeCtx(h);
W
wpan 已提交
62 63


W
wpan 已提交
64
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
65
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
66
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
67 68
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
69
    
W
wpan 已提交
70
    filterAddRange(h, ra, TSDB_RELATION_OR);
W
wpan 已提交
71
  }
W
wpan 已提交
72
  filterGetRangeNum(h, &num);
W
wpan 已提交
73
  ASSERT_EQ(num, 3);
W
wpan 已提交
74
  filterGetRangeRes(h, ra);
W
wpan 已提交
75 76 77 78 79 80
  ASSERT_EQ(ra[0].s, -100);
  ASSERT_EQ(ra[0].e, 0);
  ASSERT_EQ(ra[1].s, 1);
  ASSERT_EQ(ra[1].e, 2);
  ASSERT_EQ(ra[2].s, 3);
  ASSERT_EQ(ra[2].e, 4);  
W
wpan 已提交
81
  filterFreeRangeCtx(h);
W
wpan 已提交
82 83


W
wpan 已提交
84
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
85
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, FI_OPTION_TIMESTAMP);
W
wpan 已提交
86
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
87 88
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
89
    
W
wpan 已提交
90
    filterAddRange(h, ra, TSDB_RELATION_OR);
W
wpan 已提交
91
  }
W
wpan 已提交
92
  filterGetRangeNum(h, &num);
W
wpan 已提交
93
  ASSERT_EQ(num, 1);
W
wpan 已提交
94
  filterGetRangeRes(h, ra);
W
wpan 已提交
95 96
  ASSERT_EQ(ra[0].s, -100);
  ASSERT_EQ(ra[0].e, 4);
W
wpan 已提交
97
  filterFreeRangeCtx(h);
W
wpan 已提交
98

W
wpan 已提交
99

W
wpan 已提交
100 101 102
  s = s1;
  e = e1;
  asize = sizeof(s1)/sizeof(s[0]);
W
wpan 已提交
103
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
104
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
105
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
106 107
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
108

W
wpan 已提交
109
    filterAddRange(h, ra, TSDB_RELATION_AND);
W
wpan 已提交
110
  }
W
wpan 已提交
111
  filterGetRangeNum(h, &num);
W
wpan 已提交
112
  ASSERT_EQ(num, 1);
W
wpan 已提交
113
  filterGetRangeRes(h, ra);
W
wpan 已提交
114 115
  ASSERT_EQ(ra[0].s, 3);
  ASSERT_EQ(ra[0].e, 4);
W
wpan 已提交
116
  filterFreeRangeCtx(h);
W
wpan 已提交
117 118


W
wpan 已提交
119
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
120
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
121
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
122 123
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
124
    
W
wpan 已提交
125
    filterAddRange(h, ra, TSDB_RELATION_OR);
W
wpan 已提交
126
  }
W
wpan 已提交
127
  filterGetRangeNum(h, &num);
W
wpan 已提交
128
  ASSERT_EQ(num, 1);
W
wpan 已提交
129
  filterGetRangeRes(h, ra);
W
wpan 已提交
130 131
  ASSERT_EQ(ra[0].s, INT64_MIN);
  ASSERT_EQ(ra[0].e, 100);
W
wpan 已提交
132
  filterFreeRangeCtx(h);
W
wpan 已提交
133 134


W
wpan 已提交
135

W
wpan 已提交
136 137 138
  s = s2;
  e = e2;
  asize = sizeof(s2)/sizeof(s[0]);
W
wpan 已提交
139
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
140
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
141
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
142 143
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
144
    
W
wpan 已提交
145
    filterAddRange(h, ra, TSDB_RELATION_AND);
W
wpan 已提交
146
  }
W
wpan 已提交
147
  filterGetRangeNum(h, &num);
W
wpan 已提交
148
  ASSERT_EQ(num, 0);
W
wpan 已提交
149
  filterFreeRangeCtx(h);
W
wpan 已提交
150 151


W
wpan 已提交
152
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
153
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
154
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
155 156
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
157
    
W
wpan 已提交
158
    filterAddRange(h, ra, TSDB_RELATION_OR);
W
wpan 已提交
159
  }
W
wpan 已提交
160
  filterGetRangeNum(h, &num);
W
wpan 已提交
161
  ASSERT_EQ(num, 1);
W
wpan 已提交
162
  filterGetRangeRes(h, ra);
W
wpan 已提交
163 164
  ASSERT_EQ(ra[0].s, 1);
  ASSERT_EQ(ra[0].e, 120);
W
wpan 已提交
165
  filterFreeRangeCtx(h);
W
wpan 已提交
166 167


W
wpan 已提交
168
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
169
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
170
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
171 172
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
173
    
W
wpan 已提交
174
    filterAddRange(h, ra, i % 2 ? TSDB_RELATION_OR : TSDB_RELATION_AND);
W
wpan 已提交
175
  }
W
wpan 已提交
176
  filterGetRangeNum(h, &num);
W
wpan 已提交
177
  ASSERT_EQ(num, 0);
W
wpan 已提交
178
  filterFreeRangeCtx(h);
W
wpan 已提交
179 180


W
wpan 已提交
181
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
182
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
183
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
184 185
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
186
    
W
wpan 已提交
187
    filterAddRange(h, ra, i % 2 ? TSDB_RELATION_AND : TSDB_RELATION_OR);
W
wpan 已提交
188
  }
W
wpan 已提交
189
  filterGetRangeNum(h, &num);
W
wpan 已提交
190
  ASSERT_EQ(num, 1);
W
wpan 已提交
191
  filterGetRangeRes(h, ra);
W
wpan 已提交
192 193
  ASSERT_EQ(ra[0].s, 70);
  ASSERT_EQ(ra[0].e, 120);  
W
wpan 已提交
194
  filterFreeRangeCtx(h);
W
wpan 已提交
195 196


W
wpan 已提交
197 198 199
  s = s3;
  e = e3;
  asize = sizeof(s3)/sizeof(s[0]);
W
wpan 已提交
200
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
201
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
202
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
203 204
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
205
    
W
wpan 已提交
206
    filterAddRange(h, ra, TSDB_RELATION_AND);
W
wpan 已提交
207
  }
W
wpan 已提交
208
  filterGetRangeNum(h, &num);
W
wpan 已提交
209
  ASSERT_EQ(num, 0);
W
wpan 已提交
210
  filterFreeRangeCtx(h);
W
wpan 已提交
211 212


W
wpan 已提交
213
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
214
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
215
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
216 217
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
218
    
W
wpan 已提交
219
    filterAddRange(h, ra, TSDB_RELATION_OR);
W
wpan 已提交
220
  }
W
wpan 已提交
221
  filterGetRangeNum(h, &num);
W
wpan 已提交
222
  ASSERT_EQ(num, 1);
W
wpan 已提交
223
  filterGetRangeRes(h, ra);
W
wpan 已提交
224 225
  ASSERT_EQ(ra[0].s, 1);
  ASSERT_EQ(ra[0].e, 100);
W
wpan 已提交
226
  filterFreeRangeCtx(h);
W
wpan 已提交
227 228 229 230 231 232 233




  s = s4;
  e = e4;
  asize = sizeof(s4)/sizeof(s[0]);
W
wpan 已提交
234
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
235
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
236
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
237 238
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
239
    
W
wpan 已提交
240
    filterAddRange(h, ra, TSDB_RELATION_AND);
W
wpan 已提交
241
  }
W
wpan 已提交
242
  filterGetRangeNum(h, &num);
W
wpan 已提交
243
  ASSERT_EQ(num, 0);
W
wpan 已提交
244
  filterFreeRangeCtx(h);
W
wpan 已提交
245 246


W
wpan 已提交
247
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
248
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
249
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
250 251
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
252
    
W
wpan 已提交
253
    filterAddRange(h, ra, TSDB_RELATION_OR);
W
wpan 已提交
254
  }
W
wpan 已提交
255
  filterGetRangeNum(h, &num);
W
wpan 已提交
256
  ASSERT_EQ(num, 2);
W
wpan 已提交
257
  filterGetRangeRes(h, ra);
W
wpan 已提交
258 259 260 261
  ASSERT_EQ(ra[0].s, 0);
  ASSERT_EQ(ra[0].e, 5);
  ASSERT_EQ(ra[1].s, 10);
  ASSERT_EQ(ra[1].e, 20);
W
wpan 已提交
262
  filterFreeRangeCtx(h);
W
wpan 已提交
263 264 265 266 267


  s = s5;
  e = e5;
  asize = sizeof(s5)/sizeof(s[0]);
W
wpan 已提交
268
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
269
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
270
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
271 272
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
273
    
W
wpan 已提交
274
    filterAddRange(h, ra, TSDB_RELATION_AND);
W
wpan 已提交
275
  }
W
wpan 已提交
276
  filterGetRangeNum(h, &num);
W
wpan 已提交
277
  ASSERT_EQ(num, 0);
W
wpan 已提交
278
  filterFreeRangeCtx(h);
W
wpan 已提交
279 280


W
wpan 已提交
281
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
282
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
283
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
284 285
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
286
    
W
wpan 已提交
287
    filterAddRange(h, ra, TSDB_RELATION_OR);
W
wpan 已提交
288
  }
W
wpan 已提交
289
  filterGetRangeNum(h, &num);
W
wpan 已提交
290
  ASSERT_EQ(num, 2);
W
wpan 已提交
291
  filterGetRangeRes(h, ra);
W
wpan 已提交
292 293 294 295
  ASSERT_EQ(ra[0].s, 0);
  ASSERT_EQ(ra[0].e, 4);
  ASSERT_EQ(ra[1].s, 6);
  ASSERT_EQ(ra[1].e, 20);
W
wpan 已提交
296
  filterFreeRangeCtx(h);
W
wpan 已提交
297 298


W
wpan 已提交
299
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
300
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
301
  for (int32_t i = 0; i < asize; ++i) {
W
wpan 已提交
302 303
    ra[0].s = s[i];
    ra[0].e = e[i];
W
wpan 已提交
304
    
W
wpan 已提交
305
    filterAddRange(h, ra, (i == (asize -1)) ? TSDB_RELATION_AND : TSDB_RELATION_OR);
W
wpan 已提交
306
  }
W
wpan 已提交
307
  filterGetRangeNum(h, &num);
W
wpan 已提交
308
  ASSERT_EQ(num, 1);
W
wpan 已提交
309
  filterGetRangeRes(h, ra);
W
wpan 已提交
310 311
  ASSERT_EQ(ra[0].s, 7);
  ASSERT_EQ(ra[0].e, 10);
W
wpan 已提交
312
  filterFreeRangeCtx(h);
W
wpan 已提交
313 314 315 316 317 318 319 320 321



  int64_t s6[2] = {0, 4};
  int64_t e6[2] = {4, 6};
  s = s6;
  e = e6;
  asize = sizeof(s6)/sizeof(s[0]);
  memset(ra, 0, sizeof(ra));
W
wpan 已提交
322
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
323 324 325 326 327 328 329
  for (int32_t i = 0; i < asize; ++i) {
    ra[0].eflag = 1;
    ra[1].sflag = 4;

    ra[i].s = s[i];
    ra[i].e = e[i];
    
W
wpan 已提交
330
    filterAddRange(h, ra + i, TSDB_RELATION_AND);
W
wpan 已提交
331
  }
W
wpan 已提交
332
  filterGetRangeNum(h, &num);
333
  ASSERT_EQ(num, 1);
W
wpan 已提交
334
  filterFreeRangeCtx(h);
W
wpan 已提交
335

W
wpan 已提交
336

W
wpan 已提交
337 338

  memset(ra, 0, sizeof(ra));
W
wpan 已提交
339
  h = filterInitRangeCtx(TSDB_DATA_TYPE_BIGINT, 0);
W
wpan 已提交
340 341 342 343 344 345 346
  for (int32_t i = 0; i < asize; ++i) {
    ra[0].eflag = 1;
    ra[1].sflag = 1;

    ra[i].s = s[i];
    ra[i].e = e[i];
    
W
wpan 已提交
347
    filterAddRange(h, ra + i, TSDB_RELATION_OR);
W
wpan 已提交
348
  }
W
wpan 已提交
349
  filterGetRangeNum(h, &num);
W
wpan 已提交
350 351 352 353 354 355 356
  ASSERT_EQ(num, 2);
  ASSERT_EQ(ra[0].s, 0);
  ASSERT_EQ(ra[0].e, 4);
  ASSERT_EQ(ra[0].eflag, 1);
  ASSERT_EQ(ra[1].s, 4);
  ASSERT_EQ(ra[1].e, 6);
  ASSERT_EQ(ra[1].sflag, 1);
W
wpan 已提交
357
  filterFreeRangeCtx(h);
W
wpan 已提交
358

W
wpan 已提交
359 360 361 362 363 364 365 366 367
}


}  // namespace

TEST(testCase, rangeMergeTest) {
  intDataTest();

}