astTest.cpp 19.8 KB
Newer Older
1 2 3 4 5 6
#include <gtest/gtest.h>
#include <sys/time.h>
#include <cassert>
#include <iostream>

#include "taosmsg.h"
H
Haojun Liao 已提交
7
#include "qast.h"
8 9 10
#include "tsdb.h"
#include "tskiplist.h"

H
hjxilinx 已提交
11 12 13
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"

14 15 16 17 18 19 20 21 22
typedef struct ResultObj {
  int32_t numOfResult;
  char *  resultName[64];
} ResultObj;

static void initSchema(SSchema *pSchema, int32_t numOfCols);

static void initSchema_binary(SSchema *schema, int32_t numOfCols);

H
hzcheng 已提交
23 24
static SSkipList *createSkipList(SSchema *pSchema, int32_t numOfTags);
static SSkipList *createSkipList_binary(SSchema *pSchema, int32_t numOfTags);
25

H
hzcheng 已提交
26
static void dropMeter(SSkipList *pSkipList);
27

H
hzcheng 已提交
28
static void Right2LeftTest(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList);
29

H
hzcheng 已提交
30
static void Left2RightTest(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList);
31

H
hzcheng 已提交
32
static void IllegalExprTest(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList);
33

H
hzcheng 已提交
34 35
static void Left2RightTest_binary(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList);
static void Right2LeftTest_binary(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList);
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

void setValue(ResultObj *pResult, int32_t num, char **val) {
  pResult->numOfResult = num;
  for (int32_t i = 0; i < num; ++i) {
    pResult->resultName[i] = val[i];
  }
}

static void initSchema_binary(SSchema *schema, int32_t numOfCols) {
  schema[0].type = TSDB_DATA_TYPE_BINARY;
  schema[0].bytes = 8;
  strcpy(schema[0].name, "a");

  schema[1].type = TSDB_DATA_TYPE_DOUBLE;
  schema[1].bytes = 8;
  strcpy(schema[1].name, "b");

  schema[2].type = TSDB_DATA_TYPE_INT;
  schema[2].bytes = 20;
  strcpy(schema[2].name, "c");

  schema[3].type = TSDB_DATA_TYPE_BIGINT;
  schema[3].bytes = 8;
  strcpy(schema[3].name, "d");

  schema[4].type = TSDB_DATA_TYPE_SMALLINT;
  schema[4].bytes = 2;
  strcpy(schema[4].name, "e");

  schema[5].type = TSDB_DATA_TYPE_TINYINT;
  schema[5].bytes = 1;
  strcpy(schema[5].name, "f");

  schema[6].type = TSDB_DATA_TYPE_FLOAT;
  schema[6].bytes = 4;
  strcpy(schema[6].name, "g");

  schema[7].type = TSDB_DATA_TYPE_BOOL;
  schema[7].bytes = 1;
  strcpy(schema[7].name, "h");
}

static void initSchema(SSchema *schema, int32_t numOfCols) {
  schema[0].type = TSDB_DATA_TYPE_INT;
  schema[0].bytes = 8;
  strcpy(schema[0].name, "a");

  schema[1].type = TSDB_DATA_TYPE_DOUBLE;
  schema[1].bytes = 8;
  strcpy(schema[1].name, "b");

  schema[2].type = TSDB_DATA_TYPE_BINARY;
  schema[2].bytes = 20;
  strcpy(schema[2].name, "c");

  schema[3].type = TSDB_DATA_TYPE_BIGINT;
  schema[3].bytes = 8;
  strcpy(schema[3].name, "d");

  schema[4].type = TSDB_DATA_TYPE_SMALLINT;
  schema[4].bytes = 2;
  strcpy(schema[4].name, "e");

  schema[5].type = TSDB_DATA_TYPE_TINYINT;
  schema[5].bytes = 1;
  strcpy(schema[5].name, "f");

  schema[6].type = TSDB_DATA_TYPE_FLOAT;
  schema[6].bytes = 4;
  strcpy(schema[6].name, "g");

  schema[7].type = TSDB_DATA_TYPE_BOOL;
  schema[7].bytes = 1;
  strcpy(schema[7].name, "h");
}

H
hzcheng 已提交
112
// static void addOneNode(SSchema *pSchema, int32_t tagsLen, SSkipList *pSkipList,
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
//                       char *meterId, int32_t a, double b, char *c, int64_t d, int16_t e, int8_t f, float g,
//                       bool h, int32_t numOfTags) {
//  STabObj *pMeter = calloc(1, sizeof(STabObj));
//  pMeter->numOfTags = numOfTags;
//  pMeter->pTagData = calloc(1, tagsLen + TSDB_METER_ID_LEN);
//  strcpy(pMeter->meterId, meterId);
//
//  char *tags = pMeter->pTagData + TSDB_METER_ID_LEN;
//  int32_t offset = 0;
//
//  *(int32_t *) tags = a;
//
//  offset += pSchema[0].bytes;
//  *(double *) (tags + offset) = b;
//
//  offset += pSchema[1].bytes;
//  memcpy(tags + offset, c, 3);
//
//  offset += pSchema[2].bytes;
//  *(int64_t *) (tags + offset) = d;
//
//  offset += pSchema[3].bytes;
//  *(int16_t *) (tags + offset) = e;
//
//  offset += pSchema[4].bytes;
//  *(int8_t *) (tags + offset) = f;
//
//  offset += pSchema[5].bytes;
//  *(float *) (tags + offset) = g;
//
//  offset += pSchema[6].bytes;
//  *(int8_t *) (tags + offset) = h ? 1 : 0;
//
H
hzcheng 已提交
146 147
//  SSkipListKey pKey = SSkipListCreateKey(pSchema[0].type, tags, pSchema[0].bytes);
//  SSkipListPut(pSkipList, pMeter, &pKey, 1);
148 149
//}
//
H
hzcheng 已提交
150
// static void addOneNode_binary(SSchema *pSchema, int32_t tagsLen, SSkipList *pSkipList,
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
//                              char *meterId, int32_t a, double b, char *c, int64_t d, int16_t e, int8_t f, float g,
//                              bool h, int32_t numOfTags) {
//  STabObj *pMeter = calloc(1, sizeof(STabObj));
//  pMeter->numOfTags = numOfTags;
//  pMeter->pTagData = calloc(1, tagsLen + TSDB_METER_ID_LEN);
//  strcpy(pMeter->meterId, meterId);
//
//  char *tags = pMeter->pTagData + TSDB_METER_ID_LEN;
//  int32_t offset = 0;
//  memcpy(tags, c, pSchema[0].bytes);
//
//  offset += pSchema[0].bytes;
//  *(double *) (tags + offset) = b;
//
//  offset += pSchema[1].bytes;
//  *(int32_t *) (tags + offset) = a;
//
//  offset += pSchema[2].bytes;
//  *(int64_t *) (tags + offset) = d;
//
//  offset += pSchema[3].bytes;
//  *(int16_t *) (tags + offset) = e;
//
//  offset += pSchema[4].bytes;
//  *(int8_t *) (tags + offset) = f;
//
//  offset += pSchema[5].bytes;
//  *(float *) (tags + offset) = g;
//
//  offset += pSchema[6].bytes;
//  *(int8_t *) (tags + offset) = h ? 1 : 0;
//
H
hzcheng 已提交
183 184 185
//  SSkipListKey pKey = SSkipListCreateKey(pSchema[0].type, tags, pSchema[0].bytes);
//  SSkipListPut(pSkipList, pMeter, &pKey, 1);
//  SSkipListDestroyKey(&pKey);
186 187
//}

H
hzcheng 已提交
188 189 190
// static void dropMeter(SSkipList *pSkipList) {
//  SSkipListNode **pRes = NULL;
//  int32_t num = SSkipListIterateList(pSkipList, &pRes, NULL, NULL);
191
//  for (int32_t i = 0; i < num; ++i) {
H
hzcheng 已提交
192
//    SSkipListNode *pNode = pRes[i];
193 194 195 196 197 198 199 200
//    STabObj *pMeter = (STabObj *) pNode->pData;
//    free(pMeter->pTagData);
//    free(pMeter);
//    pNode->pData = NULL;
//  }
//  free(pRes);
//}

H
hzcheng 已提交
201
// static SSkipList *createSkipList(SSchema *pSchema, int32_t numOfTags) {
202 203 204 205 206
//  int32_t tagsLen = 0;
//  for (int32_t i = 0; i < numOfTags; ++i) {
//    tagsLen += pSchema[i].bytes;
//  }
//
H
hzcheng 已提交
207
//  SSkipList *pSkipList = SSkipListCreate(10, pSchema[0].type, 4);
208 209 210 211 212 213 214 215 216 217 218 219
//
//  addOneNode(pSchema, tagsLen, pSkipList, "tm0\0", 0, 10.5, "abc", 1000, -10000, -20, 1.0, true, 8);
//  addOneNode(pSchema, tagsLen, pSkipList, "tm1\0", 1, 20.5, "def", 1100, -10500, -30, 2.0, false, 8);
//  addOneNode(pSchema, tagsLen, pSkipList, "tm2\0", 2, 30.5, "ghi", 1200, -11000, -40, 3.0, true, 8);
//  addOneNode(pSchema, tagsLen, pSkipList, "tm3\0", 3, 40.5, "jkl", 1300, -11500, -50, 4.0, false, 8);
//  addOneNode(pSchema, tagsLen, pSkipList, "tm4\0", 4, 50.5, "mno", 1400, -12000, -60, 5.0, true, 8);
//  addOneNode(pSchema, tagsLen, pSkipList, "tm5\0", 5, 60.5, "pqr", 1500, -12500, -70, 6.0, false, 8);
//  addOneNode(pSchema, tagsLen, pSkipList, "tm6\0", 6, 70.5, "stu", 1600, -13000, -80, 7.0, true, 8);
//
//  return pSkipList;
//}
//
H
hzcheng 已提交
220
// static SSkipList *createSkipList_binary(SSchema *pSchema, int32_t numOfTags) {
221 222 223 224 225
//  int32_t tagsLen = 0;
//  for (int32_t i = 0; i < numOfTags; ++i) {
//    tagsLen += pSchema[i].bytes;
//  }
//
H
hzcheng 已提交
226
//  SSkipList *pSkipList = SSkipListCreate(10, pSchema[0].type, 4);
227 228 229 230 231 232 233 234 235 236 237 238
//
//  addOneNode_binary(pSchema, tagsLen, pSkipList, "tm0\0", 0, 10.5, "abc", 1000, -10000, -20, 1.0, true, 8);
//  addOneNode_binary(pSchema, tagsLen, pSkipList, "tm1\0", 1, 20.5, "def", 1100, -10500, -30, 2.0, false, 8);
//  addOneNode_binary(pSchema, tagsLen, pSkipList, "tm2\0", 2, 30.5, "ghi", 1200, -11000, -40, 3.0, true, 8);
//  addOneNode_binary(pSchema, tagsLen, pSkipList, "tm3\0", 3, 40.5, "jkl", 1300, -11500, -50, 4.0, false, 8);
//  addOneNode_binary(pSchema, tagsLen, pSkipList, "tm4\0", 4, 50.5, "mno", 1400, -12000, -60, 5.0, true, 8);
//  addOneNode_binary(pSchema, tagsLen, pSkipList, "tm5\0", 5, 60.5, "pqr", 1500, -12500, -70, 6.0, false, 8);
//  addOneNode_binary(pSchema, tagsLen, pSkipList, "tm6\0", 6, 70.5, "stu", 1600, -13000, -80, 7.0, true, 8);
//
//  return pSkipList;
//}

H
Haojun Liao 已提交
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
//static void testQueryStr(SSchema *schema, int32_t numOfCols, char *sql, SSkipList *pSkipList, ResultObj *pResult) {
//  tExprNode *pExpr = NULL;
//  tSQLBinaryExprFromString(&pExpr, schema, numOfCols, sql, strlen(sql));
//
//  char    str[512] = {0};
//  int32_t len = 0;
//  if (pExpr == NULL) {
//    printf("-----error in parse syntax:%s\n\n", sql);
//    assert(pResult == NULL);
//    return;
//  }
//
//  tSQLBinaryExprToString(pExpr, str, &len);
//  printf("expr is: %s\n", str);
//
//  SArray *result = NULL;
//  //  tExprTreeTraverse(pExpr, pSkipList, result, SSkipListNodeFilterCallback, &result);
//  //  printf("the result is:%lld\n", result.num);
//  //
//  //  bool findResult = false;
//  //  for (int32_t i = 0; i < result.num; ++i) {
//  //    STabObj *pm = (STabObj *)result.pRes[i];
//  //    printf("meterid:%s,\t", pm->meterId);
//  //
//  //    for (int32_t j = 0; j < pResult->numOfResult; ++j) {
//  //      if (strcmp(pm->meterId, pResult->resultName[j]) == 0) {
//  //        findResult = true;
//  //        break;
//  //      }
//  //    }
//  //    assert(findResult == true);
//  //    findResult = false;
//  //  }
//
//  printf("\n\n");
//  tExprTreeDestroy(&pExpr, NULL);
//}
276

H
Haojun Liao 已提交
277
#if 0
H
hzcheng 已提交
278
static void Left2RightTest(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList) {
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
  char str[256] = {0};

  char *t0[1] = {"tm0"};
  char *t1[1] = {"tm1"};
  char *sql = NULL;

  ResultObj res = {1, {"tm1"}};
  testQueryStr(schema, numOfCols, "a=1", pSkipList, &res);

  char *tt[1] = {"tm6"};
  setValue(&res, 1, tt);
  testQueryStr(schema, numOfCols, "a>=6", pSkipList, &res);

  setValue(&res, 1, t0);
  testQueryStr(schema, numOfCols, "b<=10.6", pSkipList, &res);

  strcpy(str, "c<>'pqr'");
  char *t2[6] = {"tm0", "tm1", "tm2", "tm3", "tm4", "tm6"};
  setValue(&res, 6, t2);
  testQueryStr(schema, numOfCols, str, pSkipList, &res);

  strcpy(str, "c='abc'");
  setValue(&res, 1, t0);
  testQueryStr(schema, numOfCols, str, pSkipList, &res);

  char *t3[6] = {"tm1", "tm2", "tm3", "tm4", "tm5", "tm6"};
  setValue(&res, 6, t3);
  testQueryStr(schema, numOfCols, "d>1050", pSkipList, &res);

  char *t4[3] = {"tm4", "tm5", "tm6"};
  setValue(&res, 3, t4);
  testQueryStr(schema, numOfCols, "g>4.5980765", pSkipList, &res);

  char *t5[4] = {"tm0", "tm2", "tm4", "tm6"};
  setValue(&res, 4, t5);
  testQueryStr(schema, numOfCols, "h=true", pSkipList, &res);

  char *t6[3] = {"tm1", "tm3", "tm5"};
  setValue(&res, 3, t6);
  testQueryStr(schema, numOfCols, "h=0", pSkipList, &res);

  sql = "(((b<40)))\0";
  char *t7[3] = {"tm0", "tm1", "tm2"};
  setValue(&res, 3, t7);
  testQueryStr(schema, numOfCols, sql, pSkipList, &res);

  sql = "((a=1) or (a=10)) or ((b=12))";
  setValue(&res, 1, t1);
  testQueryStr(schema, numOfCols, sql, pSkipList, &res);

  sql = "((((((a>0 and a<2))) or a=6) or a=3) or (b=50.5)) and h=0";
  char *t8[2] = {"tm1", "tm3"};
  setValue(&res, 2, t8);
  testQueryStr(schema, numOfCols, sql, pSkipList, &res);

  char *tf[1] = {"tm6"};
  setValue(&res, 1, tf);
  testQueryStr(schema, numOfCols, "e = -13000", pSkipList, &res);

  char *ft[5] = {"tm0", "tm1", "tm2", "tm3", "tm4"};
  setValue(&res, 5, ft);
  testQueryStr(schema, numOfCols, "f > -65", pSkipList, &res);
}

H
hzcheng 已提交
343
void Right2LeftTest(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList) {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  ResultObj res = {1, {"tm1"}};
  testQueryStr(schema, numOfCols, "((1=a))", pSkipList, &res);

  char *t9[2] = {"tm0", "tm1"};
  setValue(&res, 2, t9);
  testQueryStr(schema, numOfCols, "1>=a", pSkipList, &res);

  char *t0[1] = {"tm0"};
  setValue(&res, 1, t0);
  testQueryStr(schema, numOfCols, "10.6>=b", pSkipList, &res);

  char *t10[3] = {"tm1", "tm3", "tm5"};
  setValue(&res, 3, t10);
  testQueryStr(schema, numOfCols, "0=h", pSkipList, &res);
}

H
hzcheng 已提交
360
static void IllegalExprTest(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList) {
361 362 363 364 365 366 367 368 369 370 371 372 373 374
  testQueryStr(schema, numOfCols, "h=", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "h<", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "a=1 and ", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "and or", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "and a = 1 or", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "(())", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "(", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "(a", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "(a)", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "())", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "a===1", pSkipList, NULL);
  testQueryStr(schema, numOfCols, "a=1 and ", pSkipList, NULL);
}

H
hzcheng 已提交
375
static void Left2RightTest_binary(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList) {
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  char  str[256] = {0};
  char *sql = NULL;

  char *t0[1] = {"tm0"};
  char *t1[1] = {"tm1"};

  ResultObj res = {1, {"tm0"}};
  strcpy(str, "a='abc'");
  testQueryStr(schema, numOfCols, str, pSkipList, &res);

  char *tt[1] = {"tm6"};
  setValue(&res, 1, tt);
  testQueryStr(schema, numOfCols, "c>=6", pSkipList, &res);

  setValue(&res, 1, t0);
  testQueryStr(schema, numOfCols, "b<=10.6", pSkipList, &res);

  strcpy(str, "a<>'pqr'");
  char *t2[6] = {"tm0", "tm1", "tm2", "tm3", "tm4", "tm6"};
  setValue(&res, 6, t2);
  testQueryStr(schema, numOfCols, str, pSkipList, &res);

  strcpy(str, "a='abc'");
  setValue(&res, 1, t0);
  testQueryStr(schema, numOfCols, str, pSkipList, &res);

  char *t3[6] = {"tm1", "tm2", "tm3", "tm4", "tm5", "tm6"};
  setValue(&res, 6, t3);
  testQueryStr(schema, numOfCols, "d>1050", pSkipList, &res);

  char *t4[3] = {"tm4", "tm5", "tm6"};
  setValue(&res, 3, t4);
  testQueryStr(schema, numOfCols, "g>4.5980765", pSkipList, &res);

  char *t5[4] = {"tm0", "tm2", "tm4", "tm6"};
  setValue(&res, 4, t5);
  testQueryStr(schema, numOfCols, "h=true", pSkipList, &res);

  char *t6[3] = {"tm1", "tm3", "tm5"};
  setValue(&res, 3, t6);
  testQueryStr(schema, numOfCols, "h=0", pSkipList, &res);

  sql = "(((b<40)))\0";
  char *t7[3] = {"tm0", "tm1", "tm2"};
  setValue(&res, 3, t7);
  testQueryStr(schema, numOfCols, sql, pSkipList, &res);

  sql = "((c=1) or (c=10)) or ((b=12))\0";
  setValue(&res, 1, t1);
  testQueryStr(schema, numOfCols, sql, pSkipList, &res);

  sql = "((((((c>0 and c<2))) or c=6) or c=3) or (b=50.5)) and h=false\0";
  char *t8[2] = {"tm1", "tm3"};
  setValue(&res, 2, t8);
  testQueryStr(schema, numOfCols, sql, pSkipList, &res);
}

H
hzcheng 已提交
433
static void Right2LeftTest_binary(SSchema *schema, int32_t numOfCols, SSkipList *pSkipList) {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  char  str[256] = {0};
  char *sql = NULL;

  char *t0[1] = {"tm0"};
  char *t1[1] = {"tm1"};

  ResultObj res = {1, {"tm0"}};
  strcpy(str, "'abc'=a");
  testQueryStr(schema, numOfCols, str, pSkipList, &res);

  char *tt[1] = {"tm6"};
  setValue(&res, 1, tt);
  testQueryStr(schema, numOfCols, "6<=c", pSkipList, &res);

  setValue(&res, 1, t0);
  testQueryStr(schema, numOfCols, "10.6>=b", pSkipList, &res);

  strcpy(str, "'pqr'<>a");
  char *t2[6] = {"tm0", "tm1", "tm2", "tm3", "tm4", "tm6"};
  setValue(&res, 6, t2);
  testQueryStr(schema, numOfCols, str, pSkipList, &res);
}

namespace {
// two level expression tree
tExprNode *createExpr1() {
  auto *pLeft = (tExprNode*) calloc(1, sizeof(tExprNode));
  pLeft->nodeType = TSQL_NODE_COL;
  pLeft->pSchema = (SSchema*) calloc(1, sizeof(SSchema));
  
  strcpy(pLeft->pSchema->name, "col_a");
  pLeft->pSchema->type = TSDB_DATA_TYPE_INT;
  pLeft->pSchema->bytes = sizeof(int32_t);
  pLeft->pSchema->colId = 1;
  
  auto *pRight = (tExprNode*) calloc(1, sizeof(tExprNode));
  pRight->nodeType = TSQL_NODE_VALUE;
  pRight->pVal = (tVariant*) calloc(1, sizeof(tVariant));
  
  pRight->pVal->nType = TSDB_DATA_TYPE_INT;
  pRight->pVal->i64Key = 12;
  
  auto *pRoot = (tExprNode*) calloc(1, sizeof(tExprNode));
  pRoot->nodeType = TSQL_NODE_EXPR;
  
  pRoot->_node.optr = TSDB_RELATION_EQUAL;
  pRoot->_node.pLeft = pLeft;
  pRoot->_node.pRight = pRight;
  pRoot->_node.hasPK = true;
  
  return pRoot;
}

// thress level expression tree
tExprNode* createExpr2() {
  auto *pLeft2 = (tExprNode*) calloc(1, sizeof(tExprNode));
  pLeft2->nodeType = TSQL_NODE_COL;
  pLeft2->pSchema = (SSchema*) calloc(1, sizeof(SSchema));
  
  strcpy(pLeft2->pSchema->name, "col_a");
  pLeft2->pSchema->type = TSDB_DATA_TYPE_BINARY;
  pLeft2->pSchema->bytes = 20;
  pLeft2->pSchema->colId = 1;
  
  auto *pRight2 = (tExprNode*) calloc(1, sizeof(tExprNode));
  pRight2->nodeType = TSQL_NODE_VALUE;
  pRight2->pVal = (tVariant*) calloc(1, sizeof(tVariant));
  
  pRight2->pVal->nType = TSDB_DATA_TYPE_BINARY;
  const char* v = "hello world!";
  pRight2->pVal->pz = strdup(v);
  pRight2->pVal->nLen = strlen(v);
  
  auto *p1 = (tExprNode*) calloc(1, sizeof(tExprNode));
  p1->nodeType = TSQL_NODE_EXPR;
  
  p1->_node.optr = TSDB_RELATION_LIKE;
  p1->_node.pLeft = pLeft2;
  p1->_node.pRight = pRight2;
  p1->_node.hasPK = false;
  
  auto *pLeft1 = (tExprNode*) calloc(1, sizeof(tExprNode));
  pLeft1->nodeType = TSQL_NODE_COL;
  pLeft1->pSchema = (SSchema*) calloc(1, sizeof(SSchema));
  
  strcpy(pLeft1->pSchema->name, "col_b");
  pLeft1->pSchema->type = TSDB_DATA_TYPE_DOUBLE;
  pLeft1->pSchema->bytes = 8;
  pLeft1->pSchema->colId = 99;
  
  auto *pRight1 = (tExprNode*) calloc(1, sizeof(tExprNode));
  pRight1->nodeType = TSQL_NODE_VALUE;
  pRight1->pVal = (tVariant*) calloc(1, sizeof(tVariant));
  
  pRight1->pVal->nType = TSDB_DATA_TYPE_DOUBLE;
  pRight1->pVal->dKey = 91.99;
  
  auto *p2 = (tExprNode*) calloc(1, sizeof(tExprNode));
  p2->nodeType = TSQL_NODE_EXPR;
  
H
hjxilinx 已提交
534
  p2->_node.optr = TSDB_RELATION_GREATER_EQUAL;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  p2->_node.pLeft = pLeft1;
  p2->_node.pRight = pRight1;
  p2->_node.hasPK = false;
  
  auto *pRoot = (tExprNode*) calloc(1, sizeof(tExprNode));
  pRoot->nodeType = TSQL_NODE_EXPR;
  
  pRoot->_node.optr = TSDB_RELATION_OR;
  pRoot->_node.pLeft = p1;
  pRoot->_node.pRight = p2;
  pRoot->_node.hasPK = true;
  return pRoot;
}

void exprSerializeTest1() {
  tExprNode* p1 = createExpr1();
551 552
  SBufferWriter bw = tbufInitWriter(NULL, false);
  exprTreeToBinary(&bw, p1);
553
  
554
  size_t size = tbufTell(&bw);
555
  ASSERT_TRUE(size > 0);
556
  char* b = tbufGetData(&bw, false);
557
  
H
hjxilinx 已提交
558
  tExprNode* p2 = exprTreeFromBinary(b, size);
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  ASSERT_EQ(p1->nodeType, p2->nodeType);
  
  ASSERT_EQ(p2->_node.optr, p1->_node.optr);
  ASSERT_EQ(p2->_node.pLeft->nodeType, p1->_node.pLeft->nodeType);
  ASSERT_EQ(p2->_node.pRight->nodeType, p1->_node.pRight->nodeType);
  
  SSchema* s1 = p1->_node.pLeft->pSchema;
  SSchema* s2 = p2->_node.pLeft->pSchema;
  
  ASSERT_EQ(s2->colId, s1->colId);
  ASSERT_EQ(s2->type, s1->type);
  ASSERT_EQ(s2->bytes, s1->bytes);
  ASSERT_STRCASEEQ(s2->name, s1->name);
  
  tVariant* v1 = p1->_node.pRight->pVal;
  tVariant* v2 = p2->_node.pRight->pVal;
  
  ASSERT_EQ(v1->nType, v2->nType);
  ASSERT_EQ(v1->i64Key, v2->i64Key);
  ASSERT_EQ(p1->_node.hasPK, p2->_node.hasPK);
  
  tExprTreeDestroy(&p1, nullptr);
  tExprTreeDestroy(&p2, nullptr);
  
H
TD-166  
hzcheng 已提交
583
  // tbufClose(&bw);
584 585 586 587
}

void exprSerializeTest2() {
  tExprNode* p1 = createExpr2();
588 589
  SBufferWriter bw = tbufInitWriter(NULL, false);
  exprTreeToBinary(&bw, p1);
590
  
591
  size_t size = tbufTell(&bw);
592
  ASSERT_TRUE(size > 0);
593
  char* b = tbufGetData(&bw, false);
594
  
H
hjxilinx 已提交
595
  tExprNode* p2 = exprTreeFromBinary(b, size);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  ASSERT_EQ(p1->nodeType, p2->nodeType);
  
  ASSERT_EQ(p2->_node.optr, p1->_node.optr);
  ASSERT_EQ(p2->_node.pLeft->nodeType, p1->_node.pLeft->nodeType);
  ASSERT_EQ(p2->_node.pRight->nodeType, p1->_node.pRight->nodeType);
  
  tExprNode* c1Left = p1->_node.pLeft;
  tExprNode* c2Left = p2->_node.pLeft;
  
  ASSERT_EQ(c1Left->nodeType, c2Left->nodeType);
  
  ASSERT_EQ(c2Left->nodeType, TSQL_NODE_EXPR);
  ASSERT_EQ(c2Left->_node.optr, TSDB_RELATION_LIKE);
  
  ASSERT_STRCASEEQ(c2Left->_node.pLeft->pSchema->name, "col_a");
  ASSERT_EQ(c2Left->_node.pRight->nodeType, TSQL_NODE_VALUE);
  
  ASSERT_STRCASEEQ(c2Left->_node.pRight->pVal->pz, "hello world!");
  
  tExprNode* c1Right = p1->_node.pRight;
  tExprNode* c2Right = p2->_node.pRight;
  
  ASSERT_EQ(c1Right->nodeType, c2Right->nodeType);
  ASSERT_EQ(c2Right->nodeType, TSQL_NODE_EXPR);
H
hjxilinx 已提交
620
  ASSERT_EQ(c2Right->_node.optr, TSDB_RELATION_GREATER_EQUAL);
621 622 623 624 625 626 627
  ASSERT_EQ(c2Right->_node.pRight->pVal->dKey, 91.99);
  
  ASSERT_EQ(p2->_node.hasPK, true);
  
  tExprTreeDestroy(&p1, nullptr);
  tExprTreeDestroy(&p2, nullptr);

H
TD-166  
hzcheng 已提交
628
  // tbufClose(&bw);
629 630 631
}
}  // namespace
TEST(testCase, astTest) {
H
Haojun Liao 已提交
632
//  exprSerializeTest2();
H
Haojun Liao 已提交
633 634
}
#endif