tokenizerTest.cpp 21.4 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2
#if 0

3 4 5
#include <gtest/gtest.h>
#include <iostream>

S
Shengliang Guan 已提交
6 7
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
8 9 10 11 12 13 14 15 16 17 18
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "os.h"

#include "taos.h"
#include "tvariant.h"
#include "tdef.h"
#include "ttoken.h"
#include "astGenerator.h"
#include "parserUtil.h"
19
#include "parserInt.h"
20 21 22 23 24 25 26 27 28

namespace {
int32_t testValidateName(char* name) {
  SToken token = {0};
  token.z = name;
  token.n = strlen(name);
  token.type = 0;

  tGetToken(name, &token.type);
29
  return parserValidateIdToken(&token);
30 31 32 33 34 35 36 37 38 39 40 41 42
}

SToken createToken(char* s) {
  SToken t = {0};

  t.type = TK_STRING;
  t.z = s;
  t.n = strlen(s);
  return t;
}
}  // namespace

static void _init_tvariant_bool(SVariant* t) {
43
  t->i = TSDB_FALSE;
44 45 46 47
  t->nType = TSDB_DATA_TYPE_BOOL;
}

static void _init_tvariant_tinyint(SVariant* t) {
48
  t->i = -27;
49 50 51 52
  t->nType = TSDB_DATA_TYPE_TINYINT;
}

static void _init_tvariant_int(SVariant* t) {
53
  t->i = -23997659;
54 55 56 57
  t->nType = TSDB_DATA_TYPE_INT;
}

static void _init_tvariant_bigint(SVariant* t) {
58
  t->i = -3333333333333;
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
  t->nType = TSDB_DATA_TYPE_BIGINT;
}

static void _init_tvariant_float(SVariant* t) {
  t->d = -8991212199.8987878776;
  t->nType = TSDB_DATA_TYPE_FLOAT;
}

static void _init_tvariant_binary(SVariant* t) {
  taosVariantDestroy(t);

  t->pz = (char*)calloc(1, 20);  //"2e3");
  t->nType = TSDB_DATA_TYPE_BINARY;
  strcpy(t->pz, "2e5");
  t->nLen = strlen(t->pz);
}

static void _init_tvariant_nchar(SVariant* t) {
  taosVariantDestroy(t);

  t->wpz = (wchar_t*)calloc(1, 20 * TSDB_NCHAR_SIZE);
  t->nType = TSDB_DATA_TYPE_NCHAR;
  wcscpy(t->wpz, L"-2000000.8765");
  t->nLen = twcslen(t->wpz);
}

TEST(testCase, validateToken_test) {
  char t01[] = "abc";
  EXPECT_EQ(testValidateName(t01), TSDB_CODE_SUCCESS);

89 90 91
  char t110[] = "`1233abc.911`";
  EXPECT_EQ(testValidateName(t110), TSDB_CODE_SUCCESS);

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
  char t02[] = "'abc'";
  EXPECT_EQ(testValidateName(t02), TSDB_CODE_TSC_INVALID_OPERATION);

  char t1[] = "abc.def";
  EXPECT_EQ(testValidateName(t1), TSDB_CODE_SUCCESS);
  printf("%s\n", t1);

  char t98[] = "abc.DeF";
  EXPECT_EQ(testValidateName(t98), TSDB_CODE_SUCCESS);
  EXPECT_STREQ(t98, "abc.def");
  printf("%s\n", t98);

  char t97[] = "257.abc";
  EXPECT_EQ(testValidateName(t97), TSDB_CODE_TSC_INVALID_OPERATION);
  printf("%s\n", t97);

  char t96[] = "_257.aBc";
  EXPECT_EQ(testValidateName(t96), TSDB_CODE_SUCCESS);
  EXPECT_STREQ(t96, "_257.abc");
  printf("%s\n", t96);

  char t99[] = "abc . def";
  EXPECT_EQ(testValidateName(t99), TSDB_CODE_TSC_INVALID_OPERATION);
  printf("%s\n", t99);

  char t2[] = "'abc.def'";
  EXPECT_EQ(testValidateName(t2), TSDB_CODE_TSC_INVALID_OPERATION);
  printf("%s\n", t2);

  char t3[] = "'abc'.def";
  EXPECT_EQ(testValidateName(t3), TSDB_CODE_TSC_INVALID_OPERATION);
  printf("%s\n", t3);

  char t4[] = "'abc'.'def'";
  EXPECT_EQ(testValidateName(t4), TSDB_CODE_TSC_INVALID_OPERATION);

  char t5[] = "table.'def'";
  EXPECT_EQ(testValidateName(t5), TSDB_CODE_TSC_INVALID_OPERATION);

  char t6[] = "'table'.'def'";
  EXPECT_EQ(testValidateName(t6), TSDB_CODE_TSC_INVALID_OPERATION);

  char t7[] = "'_ab1234'.'def'";
  EXPECT_EQ(testValidateName(t7), TSDB_CODE_TSC_INVALID_OPERATION);
  printf("%s\n", t7);

  char t8[] = "'_ab&^%1234'.'def'";
  EXPECT_EQ(testValidateName(t8), TSDB_CODE_TSC_INVALID_OPERATION);

  char t9[] = "'_123'.'gtest中文'";
  EXPECT_EQ(testValidateName(t9), TSDB_CODE_TSC_INVALID_OPERATION);

  char t10[] = "abc.'gtest中文'";
  EXPECT_EQ(testValidateName(t10), TSDB_CODE_TSC_INVALID_OPERATION);

  char t10_1[] = "abc.'中文gtest'";
  EXPECT_EQ(testValidateName(t10_1), TSDB_CODE_TSC_INVALID_OPERATION);

  char t11[] = "'192.168.0.1'.abc";
  EXPECT_EQ(testValidateName(t11), TSDB_CODE_TSC_INVALID_OPERATION);

  char t12[] = "192.168.0.1.abc";
  EXPECT_EQ(testValidateName(t12), TSDB_CODE_TSC_INVALID_OPERATION);

  char t13[] = "abc.";
  EXPECT_EQ(testValidateName(t13), TSDB_CODE_TSC_INVALID_OPERATION);

  char t14[] = ".abc";
  EXPECT_EQ(testValidateName(t14), TSDB_CODE_TSC_INVALID_OPERATION);

  char t15[] = ".'abc'";
  EXPECT_EQ(testValidateName(t15), TSDB_CODE_TSC_INVALID_OPERATION);

  char t16[] = ".abc'";
  EXPECT_EQ(testValidateName(t16), TSDB_CODE_TSC_INVALID_OPERATION);

  char t17[] = "123a.\"abc\"";
  EXPECT_EQ(testValidateName(t17), TSDB_CODE_TSC_INVALID_OPERATION);
  printf("%s\n", t17);

  char t18[] = "a.\"abc\"";
  EXPECT_EQ(testValidateName(t18), TSDB_CODE_TSC_INVALID_OPERATION);
  printf("%s\n", t18);

  char t19[] = "'_ab1234'.'def'.'ab123'";
  EXPECT_EQ(testValidateName(t19), TSDB_CODE_TSC_INVALID_OPERATION);

  char t20[] = "'_ab1234*&^'";
  EXPECT_EQ(testValidateName(t20), TSDB_CODE_TSC_INVALID_OPERATION);

  char t21[] = "'1234_abc'";
  EXPECT_EQ(testValidateName(t21), TSDB_CODE_TSC_INVALID_OPERATION);

  // =======Containing capital letters=================
  char t30[] = "ABC";
  EXPECT_EQ(testValidateName(t30), TSDB_CODE_SUCCESS);

  char t31[] = "'ABC'";
  EXPECT_EQ(testValidateName(t31), TSDB_CODE_TSC_INVALID_OPERATION);

  char t32[] = "ABC.def";
  EXPECT_EQ(testValidateName(t32), TSDB_CODE_SUCCESS);

  char t33[] = "'ABC.def";
  EXPECT_EQ(testValidateName(t33), TSDB_CODE_TSC_INVALID_OPERATION);

  char t33_0[] = "abc.DEF'";
  EXPECT_EQ(testValidateName(t33_0), TSDB_CODE_TSC_INVALID_OPERATION);

  char t34[] = "'ABC.def'";
  // int32_t tmp0 = testValidateName(t34);
  EXPECT_EQ(testValidateName(t34), TSDB_CODE_TSC_INVALID_OPERATION);

  char t35[] = "'ABC'.def";
  EXPECT_EQ(testValidateName(t35), TSDB_CODE_TSC_INVALID_OPERATION);

  char t36[] = "ABC.DEF";
  EXPECT_EQ(testValidateName(t36), TSDB_CODE_SUCCESS);

  char t37[] = "abc.DEF";
  EXPECT_EQ(testValidateName(t37), TSDB_CODE_SUCCESS);

  char t37_1[] = "abc._123DEF";
  EXPECT_EQ(testValidateName(t37_1), TSDB_CODE_SUCCESS);

  char t38[] = "'abc'.\"DEF\"";
  EXPECT_EQ(testValidateName(t38), TSDB_CODE_TSC_INVALID_OPERATION);

  // do not use key words
  char t39[] = "table.'DEF'";
  EXPECT_EQ(testValidateName(t39), TSDB_CODE_TSC_INVALID_OPERATION);

  char t40[] = "'table'.'DEF'";
  EXPECT_EQ(testValidateName(t40), TSDB_CODE_TSC_INVALID_OPERATION);

  char t41[] = "'_abXYZ1234'.'deFF'";
  EXPECT_EQ(testValidateName(t41), TSDB_CODE_TSC_INVALID_OPERATION);

  char t42[] = "'_abDEF&^%1234'.'DIef'";
  EXPECT_EQ(testValidateName(t42), TSDB_CODE_TSC_INVALID_OPERATION);

  char t43[] = "'_123'.'Gtest中文'";
  EXPECT_EQ(testValidateName(t43), TSDB_CODE_TSC_INVALID_OPERATION);

  char t44[] = "'aABC'.'Gtest中文'";
  EXPECT_EQ(testValidateName(t44), TSDB_CODE_TSC_INVALID_OPERATION);

  char t45[] = "'ABC'.";
  EXPECT_EQ(testValidateName(t45), TSDB_CODE_TSC_INVALID_OPERATION);

  char t46[] = ".'ABC'";
  EXPECT_EQ(testValidateName(t46), TSDB_CODE_TSC_INVALID_OPERATION);

  char t47[] = "a.\"aTWc\"";
  EXPECT_EQ(testValidateName(t47), TSDB_CODE_TSC_INVALID_OPERATION);

  // ================has space =================
  char t60[] = " ABC ";
  EXPECT_EQ(testValidateName(t60), TSDB_CODE_TSC_INVALID_OPERATION);

  char t60_1[] = "   ABC ";
  EXPECT_EQ(testValidateName(t60_1), TSDB_CODE_TSC_INVALID_OPERATION);

  char t61[] = "' ABC '";
  EXPECT_EQ(testValidateName(t61), TSDB_CODE_TSC_INVALID_OPERATION);

  char t61_1[] = "'  ABC '";
  EXPECT_EQ(testValidateName(t61_1), TSDB_CODE_TSC_INVALID_OPERATION);

  char t62[] = " ABC . def ";
  EXPECT_EQ(testValidateName(t62), TSDB_CODE_TSC_INVALID_OPERATION);

  char t63[] = "' ABC . def ";
  EXPECT_EQ(testValidateName(t63), TSDB_CODE_TSC_INVALID_OPERATION);

  char t63_0[] = "  abc . DEF ' ";
  EXPECT_EQ(testValidateName(t63_0), TSDB_CODE_TSC_INVALID_OPERATION);

  char t64[] = " '  ABC .  def ' ";
  // int32_t tmp1 = testValidateName(t64);
  EXPECT_EQ(testValidateName(t64), TSDB_CODE_TSC_INVALID_OPERATION);

  char t65[] = " ' ABC  '. def ";
  EXPECT_EQ(testValidateName(t65), TSDB_CODE_TSC_INVALID_OPERATION);

  char t66[] = "' ABC '.'  DEF '";
  EXPECT_EQ(testValidateName(t66), TSDB_CODE_TSC_INVALID_OPERATION);

  char t67[] = "abc . '  DEF  '";
  EXPECT_EQ(testValidateName(t67), TSDB_CODE_TSC_INVALID_OPERATION);

  char t68[] = "'  abc '.'   DEF '";
  EXPECT_EQ(testValidateName(t68), TSDB_CODE_TSC_INVALID_OPERATION);

  // do not use key words
  char t69[] = "table.'DEF'";
  EXPECT_EQ(testValidateName(t69), TSDB_CODE_TSC_INVALID_OPERATION);

  char t70[] = "'table'.'DEF'";
  EXPECT_EQ(testValidateName(t70), TSDB_CODE_TSC_INVALID_OPERATION);

  char t71[] = "'_abXYZ1234  '.' deFF  '";
  EXPECT_EQ(testValidateName(t71), TSDB_CODE_TSC_INVALID_OPERATION);

  char t72[] = "'_abDEF&^%1234'.'  DIef'";
  EXPECT_EQ(testValidateName(t72), TSDB_CODE_TSC_INVALID_OPERATION);

  char t73[] = "'_123'.'  Gtest中文'";
  EXPECT_EQ(testValidateName(t73), TSDB_CODE_TSC_INVALID_OPERATION);

  char t74[] = "' aABC'.'Gtest中文'";
  EXPECT_EQ(testValidateName(t74), TSDB_CODE_TSC_INVALID_OPERATION);

  char t75[] = "' ABC '.";
  EXPECT_EQ(testValidateName(t75), TSDB_CODE_TSC_INVALID_OPERATION);

  char t76[] = ".' ABC'";
  EXPECT_EQ(testValidateName(t76), TSDB_CODE_TSC_INVALID_OPERATION);

  char t77[] = " a . \"aTWc\" ";
  EXPECT_EQ(testValidateName(t77), TSDB_CODE_TSC_INVALID_OPERATION);

  char t78[] = "  a.\"aTWc  \"";
  EXPECT_EQ(testValidateName(t78), TSDB_CODE_TSC_INVALID_OPERATION);

  // ===============muti string by space ===================
  // There's no such case.
  // char t160[] = "A BC";
  // EXPECT_EQ(testValidateName(t160), TSDB_CODE_TSC_INVALID_OPERATION);
  // printf("end:%s\n", t160);

  // There's no such case.
  // char t161[] = "' A BC '";
  // EXPECT_EQ(testValidateName(t161), TSDB_CODE_TSC_INVALID_OPERATION);

  char t162[] = " AB C . de f ";
  EXPECT_EQ(testValidateName(t162), TSDB_CODE_TSC_INVALID_OPERATION);

  char t163[] = "' AB C . de f ";
  EXPECT_EQ(testValidateName(t163), TSDB_CODE_TSC_INVALID_OPERATION);

  char t163_0[] = "  ab c . DE F ' ";
  EXPECT_EQ(testValidateName(t163_0), TSDB_CODE_TSC_INVALID_OPERATION);

  char t164[] = " '  AB C .  de f ' ";
  // int32_t tmp2 = testValidateName(t164);
  EXPECT_EQ(testValidateName(t164), TSDB_CODE_TSC_INVALID_OPERATION);

  char t165[] = " ' A BC  '. de f ";
  EXPECT_EQ(testValidateName(t165), TSDB_CODE_TSC_INVALID_OPERATION);

  char t166[] = "' AB C '.'  DE  F '";
  EXPECT_EQ(testValidateName(t166), TSDB_CODE_TSC_INVALID_OPERATION);

  char t167[] = "ab  c . '  D  EF  '";
  EXPECT_EQ(testValidateName(t167), TSDB_CODE_TSC_INVALID_OPERATION);

  char t168[] = "'  a bc '.'   DE  F '";
  EXPECT_EQ(testValidateName(t168), TSDB_CODE_TSC_INVALID_OPERATION);
}

#if 0
TEST(testCase, tvariant_convert) {
  // 1. bool data to all other data types
  SVariant t = {0};
  _init_tvariant_bool(&t);

  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
360
  EXPECT_EQ(t.i, 0);
361 362 363

  _init_tvariant_bool(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);
364
  EXPECT_EQ(t.i, 0);
365 366 367

  _init_tvariant_bool(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);
368
  EXPECT_EQ(t.i, 0);
369 370 371

  _init_tvariant_bool(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
372
  EXPECT_EQ(t.i, 0);
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

  _init_tvariant_bool(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.d, 0);

  _init_tvariant_bool(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.d, 0);

  _init_tvariant_bool(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "FALSE");
  taosVariantDestroy(&t);

  _init_tvariant_bool(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"FALSE");
  taosVariantDestroy(&t);

  // 2. tinyint to other data types
  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
395
  EXPECT_EQ(t.i, 1);
396 397 398

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);
399
  EXPECT_EQ(t.i, -27);
400 401 402

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);
403
  EXPECT_EQ(t.i, -27);
404 405 406

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_INT), 0);
407
  EXPECT_EQ(t.i, -27);
408 409 410

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
411
  EXPECT_EQ(t.i, -27);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.d, -27);

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.d, -27);

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-27");
  taosVariantDestroy(&t);

  _init_tvariant_tinyint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-27");
  taosVariantDestroy(&t);

  // 3. int to other data
  // types//////////////////////////////////////////////////////////////////
  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
435
  EXPECT_EQ(t.i, 1);
436 437 438 439 440 441 442 443 444

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_INT), 0);
445
  EXPECT_EQ(t.i, -23997659);
446 447 448

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
449
  EXPECT_EQ(t.i, -23997659);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.d, -23997659);

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.d, -23997659);

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-23997659");
  taosVariantDestroy(&t);

  _init_tvariant_int(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-23997659");
  taosVariantDestroy(&t);

  // 4. bigint to other data
  // type//////////////////////////////////////////////////////////////////////////////
  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
473
  EXPECT_EQ(t.i, 1);
474 475 476 477 478 479 480 481 482 483 484 485

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_TINYINT), 0);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_SMALLINT), 0);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_INT), 0);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
486
  EXPECT_EQ(t.i, -3333333333333);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_EQ(t.d, -3333333333333);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_EQ(t.d, -3333333333333);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-3333333333333");
  taosVariantDestroy(&t);

  _init_tvariant_bigint(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-3333333333333");
  taosVariantDestroy(&t);

  // 5. float to other data
  // types////////////////////////////////////////////////////////////////////////
  _init_tvariant_float(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
510
  EXPECT_EQ(t.i, 1);
511 512 513

  _init_tvariant_float(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
514
  EXPECT_EQ(t.i, -8991212199);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

  _init_tvariant_float(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_DOUBLE_EQ(t.d, -8991212199.8987885);

  _init_tvariant_float(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_DOUBLE_EQ(t.d, -8991212199.8987885);

  _init_tvariant_float(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-8991212199.898788");
  taosVariantDestroy(&t);

  _init_tvariant_float(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-8991212199.898788");
  taosVariantDestroy(&t);

  // 6. binary to other data types
  // //////////////////////////////////////////////////////////////////
  t.pz = "true";
  t.nLen = strlen(t.pz);
  t.nType = TSDB_DATA_TYPE_BINARY;
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
540
  EXPECT_EQ(t.i, 1);
541 542 543 544 545 546

  _init_tvariant_binary(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), -1);

  _init_tvariant_binary(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
547
  EXPECT_EQ(t.i, 200000);
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

  _init_tvariant_binary(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_DOUBLE_EQ(t.d, 200000);

  _init_tvariant_binary(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_DOUBLE_EQ(t.d, 200000);

  _init_tvariant_binary(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "2e5");
  taosVariantDestroy(&t);

  _init_tvariant_binary(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"2e5");
  taosVariantDestroy(&t);

  // 7. nchar to other data types
  // //////////////////////////////////////////////////////////////////
  t.wpz = L"FALSE";
  t.nLen = wcslen(t.wpz);
  t.nType = TSDB_DATA_TYPE_NCHAR;
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);
573
  EXPECT_EQ(t.i, 0);
574 575 576 577 578 579

  _init_tvariant_nchar(&t);
  EXPECT_LE(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BOOL), 0);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BIGINT), 0);
580
  EXPECT_EQ(t.i, -2000000);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

  _init_tvariant_nchar(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_FLOAT), 0);
  EXPECT_DOUBLE_EQ(t.d, -2000000.8765);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_DOUBLE), 0);
  EXPECT_DOUBLE_EQ(t.d, -2000000.8765);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_BINARY), 0);
  EXPECT_STREQ(t.pz, "-2000000.8765");
  taosVariantDestroy(&t);

  _init_tvariant_nchar(&t);
  EXPECT_EQ(taosVariantTypeSetType(&t, TSDB_DATA_TYPE_NCHAR), 0);
  EXPECT_STREQ(t.wpz, L"-2000000.8765");
  taosVariantDestroy(&t);
}
#endif

TEST(testCase, tGetToken_Test) {
  char*    s = ".123 ";
  uint32_t type = 0;

  int32_t len = tGetToken(s, &type);
  EXPECT_EQ(type, TK_FLOAT);
  EXPECT_EQ(len, strlen(s) - 1);

  char s1[] = "1.123e10 ";
  len = tGetToken(s1, &type);
  EXPECT_EQ(type, TK_FLOAT);
  EXPECT_EQ(len, strlen(s1) - 1);

  char s4[] = "0xff ";
  len = tGetToken(s4, &type);
  EXPECT_EQ(type, TK_HEX);
  EXPECT_EQ(len, strlen(s4) - 1);

  // invalid data type
  char s2[] = "e10 ";
  len = tGetToken(s2, &type);
  EXPECT_FALSE(type == TK_FLOAT);

  char s3[] = "1.1.1.1";
  len = tGetToken(s3, &type);
  EXPECT_EQ(type, TK_IPTOKEN);
  EXPECT_EQ(len, strlen(s3));

  char s5[] = "0x ";
  len = tGetToken(s5, &type);
  EXPECT_FALSE(type == TK_HEX);
}

TEST(testCase, isValidNumber_test) {
  SToken t1 = createToken("123abc");

  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);

  t1 = createToken("0xabc");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_HEX);

  t1 = createToken("0b11101");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_BIN);

  t1 = createToken(".134abc");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);

  t1 = createToken("1e1 ");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);

  t1 = createToken("1+2");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_ILLEGAL);

  t1 = createToken("-0x123");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_HEX);

  t1 = createToken("-1");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_INTEGER);

  t1 = createToken("-0b1110");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_BIN);

  t1 = createToken("-.234");
  EXPECT_EQ(tGetNumericStringType(&t1), TK_FLOAT);
}

668 669 670 671 672 673 674 675 676 677 678 679 680
TEST(testCase, generateAST_test) {
  SSqlInfo info = doGenerateAST("select * from t1 where ts < now");
  ASSERT_EQ(info.valid, true);

  SSqlInfo info1 = doGenerateAST("select * from `t.1abc` where ts<now+2h  and col < 20+99");
  ASSERT_EQ(info1.valid, true);

  char msg[128] = {0};

  SMsgBuf msgBuf = {0};
  msgBuf.buf = msg;
  msgBuf.len = 128;

681
  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0);
682 683 684 685
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &msgBuf);
  ASSERT_EQ(code, 0);

  SSqlInfo info2 = doGenerateAST("select * from abc where ts<now+2");
686
  SSqlNode* pNode2 = (SSqlNode*) taosArrayGetP(((SArray*)info2.sub.node), 0);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
  code = evaluateSqlNode(pNode2, TSDB_TIME_PRECISION_MILLI, &msgBuf);
  ASSERT_NE(code, 0);

  destroySqlInfo(&info);
  destroySqlInfo(&info1);
  destroySqlInfo(&info2);
}

TEST(testCase, evaluateAST_test) {
  SSqlInfo info1 = doGenerateAST("select a, b+22 from `t.1abc` where ts<now+2h and `col` < 20 + 99");
  ASSERT_EQ(info1.valid, true);

  char msg[128] = {0};
  SMsgBuf msgBuf = {0};
  msgBuf.buf = msg;
  msgBuf.len = 128;

704
  SSqlNode* pNode = (SSqlNode*) taosArrayGetP(((SArray*)info1.sub.node), 0);
705 706 707 708 709 710 711 712 713 714
  int32_t code = evaluateSqlNode(pNode, TSDB_TIME_PRECISION_NANO, &msgBuf);
  ASSERT_EQ(code, 0);
  destroySqlInfo(&info1);
}

TEST(testCase, extractMeta_test) {
  SSqlInfo info1 = doGenerateAST("select a, b+22 from `t.1abc` where ts<now+2h and `col` < 20 + 99");
  ASSERT_EQ(info1.valid, true);

  char msg[128] = {0};
D
dapan1121 已提交
715
  SCatalogReq req  = {0};
H
Haojun Liao 已提交
716

H
Haojun Liao 已提交
717
  SParseContext ctx = {0};
H
Haojun Liao 已提交
718 719 720
  ctx.db = "db1";
  ctx.acctId = 1;
  int32_t ret = qParserExtractRequestedMetaInfo(&info1, &req, &ctx, msg, 128);
721 722 723
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(taosArrayGetSize(req.pTableName), 1);

724
  qParserCleanupMetaRequestInfo(&req);
725 726
  destroySqlInfo(&info1);
}
727

X
Xiaoyu Wang 已提交
728 729 730
#pragma GCC diagnostic pop

#endif