parserAstTest.cpp 20.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16 17 18
#include <algorithm>
#include <string>

19 20
#include <gtest/gtest.h>

X
Xiaoyu Wang 已提交
21
#include "parInt.h"
X
Xiaoyu Wang 已提交
22
#include "parserTestUtil.h"
23 24 25 26

using namespace std;
using namespace testing;

X
Xiaoyu Wang 已提交
27
class ParserTest : public Test {
X
Xiaoyu Wang 已提交
28
 protected:
29 30 31 32 33 34 35 36
  void setDatabase(const string& acctId, const string& db) {
    acctId_ = acctId;
    db_ = db;
  }

  void bind(const char* sql) {
    reset();
    cxt_.acctId = atoi(acctId_.c_str());
37 38 39
    cxt_.db = db_.c_str();
    sqlBuf_ = string(sql);
    transform(sqlBuf_.begin(), sqlBuf_.end(), sqlBuf_.begin(), ::tolower);
40
    cxt_.sqlLen = strlen(sql);
41
    cxt_.pSql = sqlBuf_.c_str();
42 43
  }

44
  bool run(int32_t parseCode = TSDB_CODE_SUCCESS, int32_t translateCode = TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
45 46 47
    query_ = nullptr;
    bool res = runImpl(parseCode, translateCode);
    qDestroyQuery(query_);
48
    if (!res || g_isDump) {
X
Xiaoyu Wang 已提交
49 50
      dump();
    }
X
Xiaoyu Wang 已提交
51 52 53
    return res;
  }

X
Xiaoyu Wang 已提交
54
 private:
X
Xiaoyu Wang 已提交
55 56 57
  static const int max_err_len = 1024;

  bool runImpl(int32_t parseCode, int32_t translateCode) {
X
Xiaoyu Wang 已提交
58
    int32_t code = parse(&cxt_, &query_);
59
    if (code != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
60
      parseErrStr_ = string("code:") + tstrerror(code) + string(", msg:") + errMagBuf_;
61
      return (code == parseCode);
62 63 64 65
    }
    if (TSDB_CODE_SUCCESS != parseCode) {
      return false;
    }
X
Xiaoyu Wang 已提交
66
    parsedAstStr_ = toString(query_->pRoot);
X
Xiaoyu Wang 已提交
67
    code = translate(&cxt_, query_);
68
    if (code != TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
69
      translateErrStr_ = string("code:") + tstrerror(code) + string(", msg:") + errMagBuf_;
70
      return (code == translateCode);
71
    }
X
Xiaoyu Wang 已提交
72
    translatedAstStr_ = toString(query_->pRoot);
73 74 75 76 77 78
    code = calculateConstant(&cxt_, query_);
    if (code != TSDB_CODE_SUCCESS) {
      calcConstErrStr_ = string("code:") + tstrerror(code) + string(", msg:") + errMagBuf_;
      return false;
    }
    calcConstAstStr_ = toString(query_->pRoot);
79
    return (TSDB_CODE_SUCCESS == translateCode);
80 81
  }

X
Xiaoyu Wang 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  void dump() {
    cout << "input sql : [" << cxt_.pSql << "]" << endl;
    if (!parseErrStr_.empty()) {
      cout << "parse error: " << parseErrStr_ << endl;
    }
    if (!parsedAstStr_.empty()) {
      cout << "parse output: " << endl;
      cout << parsedAstStr_ << endl;
    }
    if (!translateErrStr_.empty()) {
      cout << "translate error: " << translateErrStr_ << endl;
    }
    if (!translatedAstStr_.empty()) {
      cout << "translate output: " << endl;
      cout << translatedAstStr_ << endl;
    }
98 99 100 101 102 103 104
    if (!calcConstErrStr_.empty()) {
      cout << "calculateConstant error: " << calcConstErrStr_ << endl;
    }
    if (!calcConstAstStr_.empty()) {
      cout << "calculateConstant output: " << endl;
      cout << calcConstAstStr_ << endl;
    }
X
Xiaoyu Wang 已提交
105 106
  }

107
  string toString(const SNode* pRoot, bool format = false) {
X
Xiaoyu Wang 已提交
108
    char*   pStr = NULL;
109 110 111 112 113
    int32_t len = 0;
    int32_t code = nodesNodeToString(pRoot, format, &pStr, &len);
    if (code != TSDB_CODE_SUCCESS) {
      cout << "sql:[" << cxt_.pSql << "] toString code:" << code << ", strerror:" << tstrerror(code) << endl;
      throw "nodesNodeToString failed!";
114
    }
115
    string str(pStr);
wafwerar's avatar
wafwerar 已提交
116
    taosMemoryFreeClear(pStr);
117
    return str;
118 119 120 121 122 123 124
  }

  void reset() {
    memset(&cxt_, 0, sizeof(cxt_));
    memset(errMagBuf_, 0, max_err_len);
    cxt_.pMsg = errMagBuf_;
    cxt_.msgLen = max_err_len;
X
Xiaoyu Wang 已提交
125 126 127 128
    parseErrStr_.clear();
    parsedAstStr_.clear();
    translateErrStr_.clear();
    translatedAstStr_.clear();
129 130
    calcConstErrStr_.clear();
    calcConstAstStr_.clear();
131 132
  }

X
Xiaoyu Wang 已提交
133 134 135 136
  string        acctId_;
  string        db_;
  char          errMagBuf_[max_err_len];
  string        sqlBuf_;
137
  SParseContext cxt_;
X
Xiaoyu Wang 已提交
138 139 140 141 142 143 144
  SQuery*       query_;
  string        parseErrStr_;
  string        parsedAstStr_;
  string        translateErrStr_;
  string        translatedAstStr_;
  string        calcConstErrStr_;
  string        calcConstAstStr_;
145 146
};

X
Xiaoyu Wang 已提交
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
TEST_F(ParserTest, createAccount) {
  setDatabase("root", "test");

  bind("create account ac_wxy pass '123456'");
  ASSERT_TRUE(run(TSDB_CODE_PAR_EXPRIE_STATEMENT));
}

TEST_F(ParserTest, alterAccount) {
  setDatabase("root", "test");

  bind("alter account ac_wxy pass '123456'");
  ASSERT_TRUE(run(TSDB_CODE_PAR_EXPRIE_STATEMENT));
}

TEST_F(ParserTest, createUser) {
  setDatabase("root", "test");

  bind("create user wxy pass '123456'");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, alterUser) {
  setDatabase("root", "test");

  bind("alter user wxy pass '123456'");
  ASSERT_TRUE(run());

  bind("alter user wxy privilege 'write'");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, dropUser) {
  setDatabase("root", "test");

  bind("drop user wxy");
  ASSERT_TRUE(run());
}

X
Xiaoyu Wang 已提交
185
TEST_F(ParserTest, selectSimple) {
186 187 188
  setDatabase("root", "test");

  bind("SELECT * FROM t1");
189
  ASSERT_TRUE(run());
190 191

  bind("SELECT * FROM test.t1");
192
  ASSERT_TRUE(run());
193

194
  bind("SELECT ts, c1 FROM t1");
195 196
  ASSERT_TRUE(run());

197 198 199 200
  bind("SELECT ts, t.c1 FROM (SELECT * FROM t1) t");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1");
201
  ASSERT_TRUE(run());
202 203
}

X
Xiaoyu Wang 已提交
204
TEST_F(ParserTest, selectConstant) {
205 206 207 208 209
  setDatabase("root", "test");

  bind("SELECT 123, 20.4, 'abc', \"wxy\", TIMESTAMP '2022-02-09 17:30:20', true, false, 10s FROM t1");
  ASSERT_TRUE(run());

X
Xiaoyu Wang 已提交
210 211 212
  bind(
      "SELECT 1234567890123456789012345678901234567890, 20.1234567890123456789012345678901234567890, 'abc', \"wxy\", "
      "TIMESTAMP '2022-02-09 17:30:20', true, false, 15s FROM t1");
213
  ASSERT_TRUE(run());
214 215 216

  bind("SELECT 123 + 45 FROM t1 where 2 - 1");
  ASSERT_TRUE(run());
217 218
}

X
Xiaoyu Wang 已提交
219
TEST_F(ParserTest, selectExpression) {
220 221
  setDatabase("root", "test");

222
  bind("SELECT ts + 10s, c1 + 10, concat(c2, 'abc') FROM t1");
223
  ASSERT_TRUE(run());
224 225 226 227 228 229

  bind("SELECT ts > 0, c1 < 20 AND c2 = 'qaz' FROM t1");
  ASSERT_TRUE(run());

  bind("SELECT ts > 0, c1 BETWEEN 10 AND 20 AND c2 = 'qaz' FROM t1");
  ASSERT_TRUE(run());
230 231
}

232 233 234 235 236 237 238 239 240 241
TEST_F(ParserTest, selectCondition) {
  setDatabase("root", "test");

  bind("SELECT c1 FROM t1 where ts in (true, false)");
  ASSERT_TRUE(run());

  bind("SELECT * FROM t1 where c1 > 10 and c1 is not null");
  ASSERT_TRUE(run());
}

242 243 244 245 246 247 248
TEST_F(ParserTest, selectPseudoColumn) {
  setDatabase("root", "test");

  bind("SELECT _wstartts, _wendts, count(*) FROM t1 interval(10s)");
  ASSERT_TRUE(run());
}

249 250 251 252 253 254 255 256 257 258 259 260 261
TEST_F(ParserTest, selectMultiResFunc) {
  setDatabase("root", "test");

  // bind("SELECT last(*), first(*), last_row(*) FROM t1");
  // ASSERT_TRUE(run());

  bind("SELECT last(c1, c2), first(t1.*), last_row(c3) FROM t1");
  ASSERT_TRUE(run());

  bind("SELECT last(t2.*), first(t1.c1, t2.*), last_row(t1.*, t2.*) FROM st1s1 t1, st1s2 t2 where t1.ts = t2.ts");
  ASSERT_TRUE(run());
}

X
Xiaoyu Wang 已提交
262
TEST_F(ParserTest, selectClause) {
263 264
  setDatabase("root", "test");

265
  // GROUP BY clause
266
  bind("SELECT count(*) cnt FROM t1 WHERE c1 > 0");
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  ASSERT_TRUE(run());

  bind("SELECT count(*), c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c2");
  ASSERT_TRUE(run());

  bind("SELECT count(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 HAVING count(c1) > 10");
  ASSERT_TRUE(run());

  bind("SELECT count(*), c1, c2 + 10, c1 + c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c2, c1");
  ASSERT_TRUE(run());

  bind("SELECT count(*), c1 + 10, c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c1 + 10, c2");
  ASSERT_TRUE(run());

  // ORDER BY clause
282 283 284 285 286
  bind("SELECT count(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 ORDER BY cnt");
  ASSERT_TRUE(run());

  bind("SELECT count(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 ORDER BY 1");
  ASSERT_TRUE(run());
287 288 289 290 291 292 293 294 295 296

  // DISTINCT clause
  bind("SELECT DISTINCT c1, c2 FROM t1 WHERE c1 > 0 ORDER BY c1");
  ASSERT_TRUE(run());

  bind("SELECT DISTINCT c1 + 10, c2 FROM t1 WHERE c1 > 0 ORDER BY c1 + 10, c2");
  ASSERT_TRUE(run());

  bind("SELECT DISTINCT c1 + 10 cc1, c2 cc2 FROM t1 WHERE c1 > 0 ORDER BY cc1, c2");
  ASSERT_TRUE(run());
297 298 299

  bind("SELECT DISTINCT count(c2) FROM t1 WHERE c1 > 0 GROUP BY c1 ORDER BY count(c2)");
  ASSERT_TRUE(run());
300 301
}

X
Xiaoyu Wang 已提交
302 303 304 305 306 307 308
TEST_F(ParserTest, selectWindow) {
  setDatabase("root", "test");

  bind("SELECT count(*) FROM t1 interval(10s)");
  ASSERT_TRUE(run());
}

X
Xiaoyu Wang 已提交
309
TEST_F(ParserTest, selectSyntaxError) {
310 311 312 313 314 315 316 317 318 319 320 321 322
  setDatabase("root", "test");

  bind("SELECTT * FROM t1");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));

  bind("SELECT *");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));

  bind("SELECT *, * FROM test.t1");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));

  bind("SELECT * FROM test.t1 t WHER");
  ASSERT_TRUE(run(TSDB_CODE_FAILED));
323
}
324

X
Xiaoyu Wang 已提交
325
TEST_F(ParserTest, selectSemanticError) {
326 327
  setDatabase("root", "test");

328
  // TSDB_CODE_PAR_INVALID_COLUMN
329
  bind("SELECT c1, cc1 FROM t1");
330 331
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_INVALID_COLUMN));

332
  bind("SELECT t1.c1, t1.cc1 FROM t1");
333 334 335
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_INVALID_COLUMN));

  // TSDB_CODE_PAR_TABLE_NOT_EXIST
336
  bind("SELECT * FROM t10");
337
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_TABLE_NOT_EXIST));
338

339 340
  bind("SELECT * FROM test.t10");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_TABLE_NOT_EXIST));
341

342 343 344
  bind("SELECT t2.c1 FROM t1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_TABLE_NOT_EXIST));

345
  // TSDB_CODE_PAR_AMBIGUOUS_COLUMN
346
  bind("SELECT c2 FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1");
347 348 349 350 351 352 353 354 355 356 357
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_AMBIGUOUS_COLUMN));

  // TSDB_CODE_PAR_WRONG_VALUE_TYPE
  bind("SELECT 10n FROM t1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_WRONG_VALUE_TYPE));

  bind("SELECT TIMESTAMP '2010' FROM t1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_WRONG_VALUE_TYPE));

  // TSDB_CODE_PAR_INVALID_FUNTION
  bind("SELECT cnt(*) FROM t1");
358
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_FUNC_INVALID_FUNTION));
359 360 361 362 363 364 365

  // TSDB_CODE_PAR_FUNTION_PARA_NUM
  // TSDB_CODE_PAR_FUNTION_PARA_TYPE

  // TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION
  bind("SELECT c2 FROM t1 tt1 JOIN t1 tt2 ON count(*) > 0");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION));
366 367

  bind("SELECT c2 FROM t1 where count(*) > 0");
368 369 370 371 372 373 374 375 376 377 378 379
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION));

  bind("SELECT c2 FROM t1 GROUP BY count(*)");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION));

  // TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT
  bind("SELECT c2 FROM t1 ORDER BY 0");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT));

  bind("SELECT c2 FROM t1 ORDER BY 2");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT));

380
  // TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION
381 382 383 384 385 386 387 388 389 390 391
  bind("SELECT count(*) cnt FROM t1 HAVING c1 > 0");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION));

  bind("SELECT count(*) cnt FROM t1 GROUP BY c2 HAVING c1 > 0");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION));

  bind("SELECT count(*), c1 cnt FROM t1 GROUP BY c2 HAVING c2 > 0");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION));

  bind("SELECT count(*) cnt FROM t1 GROUP BY c2 HAVING c2 > 0 ORDER BY c1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION));
392 393 394 395 396 397 398 399 400 401 402 403 404 405

  // TSDB_CODE_PAR_NOT_SINGLE_GROUP
  bind("SELECT count(*), c1 FROM t1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SINGLE_GROUP));

  bind("SELECT count(*) FROM t1 ORDER BY c1");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SINGLE_GROUP));

  bind("SELECT c1 FROM t1 ORDER BY count(*)");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SINGLE_GROUP));

  // TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION
  bind("SELECT DISTINCT c1, c2 FROM t1 WHERE c1 > 0 ORDER BY ts");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION));
406 407 408 409 410 411

  bind("SELECT DISTINCT c1 FROM t1 WHERE c1 > 0 ORDER BY count(c2)");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION));

  bind("SELECT DISTINCT c2 FROM t1 WHERE c1 > 0 ORDER BY count(c2)");
  ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION));
412
}
413

X
Xiaoyu Wang 已提交
414 415 416 417 418 419 420
TEST_F(ParserTest, showUsers) {
  setDatabase("root", "test");

  bind("show users");
  ASSERT_TRUE(run());
}

421 422 423 424 425 426 427 428 429 430
TEST_F(ParserTest, createDnode) {
  setDatabase("root", "test");

  bind("create dnode abc1 port 7000");
  ASSERT_TRUE(run());

  bind("create dnode 1.1.1.1 port 9000");
  ASSERT_TRUE(run());
}

X
Xiaoyu Wang 已提交
431 432 433 434 435 436 437
TEST_F(ParserTest, showDnodes) {
  setDatabase("root", "test");

  bind("show dnodes");
  ASSERT_TRUE(run());
}

438 439 440 441 442 443 444 445 446 447
TEST_F(ParserTest, alterDnode) {
  setDatabase("root", "test");

  bind("alter dnode 1 'resetLog'");
  ASSERT_TRUE(run());

  bind("alter dnode 1 'debugFlag' '134'");
  ASSERT_TRUE(run());
}

448
TEST_F(ParserTest, createDatabase) {
449 450
  setDatabase("root", "test");

451 452
  bind("create database wxy_db");
  ASSERT_TRUE(run());
453

X
Xiaoyu Wang 已提交
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
  bind(
      "create database if not exists wxy_db "
      "BLOCKS 100 "
      "CACHE 100 "
      "CACHELAST 2 "
      "COMP 1 "
      "DAYS 100 "
      "FSYNC 100 "
      "MAXROWS 1000 "
      "MINROWS 100 "
      "KEEP 100 "
      "PRECISION 'ms' "
      "QUORUM 1 "
      "REPLICA 3 "
      "TTL 100 "
      "WAL 2 "
      "VGROUPS 100 "
      "SINGLE_STABLE 0 "
      "STREAM_MODE 1 "
      "RETENTIONS '15s:7d,1m:21d,15m:5y'");
  ASSERT_TRUE(run());

  bind(
      "create database if not exists wxy_db "
      "DAYS 100m "
      "KEEP 200m,300h,400d ");
X
Xiaoyu Wang 已提交
480
  ASSERT_TRUE(run());
481 482
}

483 484 485 486 487 488
TEST_F(ParserTest, alterDatabase) {
  setDatabase("root", "test");

  bind("alter database wxy_db BLOCKS 200");
  ASSERT_TRUE(run());

X
Xiaoyu Wang 已提交
489 490 491 492 493 494 495 496
  bind(
      "alter database wxy_db "
      "BLOCKS 200 "
      "CACHELAST 1 "
      "FSYNC 200 "
      "KEEP 200 "
      "QUORUM 2 "
      "WAL 1 ");
497 498 499
  ASSERT_TRUE(run());
}

500
TEST_F(ParserTest, showDatabase) {
501 502
  setDatabase("root", "test");

503 504 505 506 507
  bind("show databases");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, useDatabase) {
508 509
  setDatabase("root", "test");

510 511 512 513 514
  bind("use wxy_db");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, createTable) {
515 516
  setDatabase("root", "test");

517 518
  bind("create table t1(ts timestamp, c1 int)");
  ASSERT_TRUE(run());
519

X
Xiaoyu Wang 已提交
520 521 522 523 524 525 526
  bind(
      "create table if not exists test.t1("
      "ts TIMESTAMP, c1 INT, c2 INT UNSIGNED, c3 BIGINT, c4 BIGINT UNSIGNED, c5 FLOAT, c6 DOUBLE, c7 BINARY(20), c8 "
      "SMALLINT, "
      "c9 SMALLINT UNSIGNED COMMENT 'test column comment', c10 TINYINT, c11 TINYINT UNSIGNED, c12 BOOL, c13 NCHAR(30), "
      "c14 JSON, c15 VARCHAR(50)) "
      "KEEP 100 TTL 100 COMMENT 'test create table' SMA(c1, c2, c3)");
527
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
528 529 530 531 532 533 534 535 536 537 538 539

  bind(
      "create table if not exists test.t1("
      "ts TIMESTAMP, c1 INT, c2 INT UNSIGNED, c3 BIGINT, c4 BIGINT UNSIGNED, c5 FLOAT, c6 DOUBLE, c7 BINARY(20), c8 "
      "SMALLINT, "
      "c9 SMALLINT UNSIGNED COMMENT 'test column comment', c10 TINYINT, c11 TINYINT UNSIGNED, c12 BOOL, c13 NCHAR(30), "
      "c14 JSON, c15 VARCHAR(50)) "
      "TAGS (tsa TIMESTAMP, a1 INT, a2 INT UNSIGNED, a3 BIGINT, a4 BIGINT UNSIGNED, a5 FLOAT, a6 DOUBLE, a7 "
      "BINARY(20), a8 SMALLINT, "
      "a9 SMALLINT UNSIGNED COMMENT 'test column comment', a10 TINYINT, a11 TINYINT UNSIGNED, a12 BOOL, a13 NCHAR(30), "
      "a14 JSON, a15 VARCHAR(50)) "
      "KEEP 100 TTL 100 COMMENT 'test create table' SMA(c1, c2, c3) ROLLUP (min) FILE_FACTOR 0.1 DELAY 2");
540
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
541

542
  bind("create table if not exists t1 using st1 tags(1, 'wxy')");
543 544
  ASSERT_TRUE(run());

X
Xiaoyu Wang 已提交
545 546 547 548 549 550 551 552
  bind(
      "create table "
      "if not exists test.t1 using test.st1 (tag1, tag2) tags(1, 'abc') "
      "if not exists test.t2 using test.st1 (tag1, tag2) tags(2, 'abc') "
      "if not exists test.t3 using test.st1 (tag1, tag2) tags(3, 'abc') "
      "if not exists test.t4 using test.st1 (tag1, tag2) tags(3, null) "
      "if not exists test.t5 using test.st1 (tag1, tag2) tags(null, 'abc') "
      "if not exists test.t6 using test.st1 (tag1, tag2) tags(null, null)");
553
  ASSERT_TRUE(run());
554 555 556 557

  bind("create stable t1(ts timestamp, c1 int) TAGS(id int)");
  ASSERT_TRUE(run());

X
Xiaoyu Wang 已提交
558 559 560 561 562 563 564 565 566 567 568
  bind(
      "create stable if not exists test.t1("
      "ts TIMESTAMP, c1 INT, c2 INT UNSIGNED, c3 BIGINT, c4 BIGINT UNSIGNED, c5 FLOAT, c6 DOUBLE, c7 BINARY(20), c8 "
      "SMALLINT, "
      "c9 SMALLINT UNSIGNED COMMENT 'test column comment', c10 TINYINT, c11 TINYINT UNSIGNED, c12 BOOL, c13 NCHAR(30), "
      "c14 JSON, c15 VARCHAR(50)) "
      "TAGS (tsa TIMESTAMP, a1 INT, a2 INT UNSIGNED, a3 BIGINT, a4 BIGINT UNSIGNED, a5 FLOAT, a6 DOUBLE, a7 "
      "BINARY(20), a8 SMALLINT, "
      "a9 SMALLINT UNSIGNED COMMENT 'test column comment', a10 TINYINT, a11 TINYINT UNSIGNED, a12 BOOL, a13 NCHAR(30), "
      "a14 JSON, a15 VARCHAR(50)) "
      "KEEP 100 TTL 100 COMMENT 'test create table' SMA(c1, c2, c3) ROLLUP (min) FILE_FACTOR 0.1 DELAY 2");
569
  ASSERT_TRUE(run());
570
}
X
Xiaoyu Wang 已提交
571 572 573 574

TEST_F(ParserTest, showTables) {
  setDatabase("root", "test");

X
Xiaoyu Wang 已提交
575 576
  bind("show tables");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
577

X
Xiaoyu Wang 已提交
578 579
  bind("show test.tables");
  ASSERT_TRUE(run());
X
Xiaoyu Wang 已提交
580 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

  bind("show tables like 'c%'");
  ASSERT_TRUE(run());

  bind("show test.tables like 'c%'");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showStables) {
  setDatabase("root", "test");

  bind("show stables");
  ASSERT_TRUE(run());

  bind("show test.stables");
  ASSERT_TRUE(run());

  bind("show stables like 'c%'");
  ASSERT_TRUE(run());

  bind("show test.stables like 'c%'");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showVgroups) {
  setDatabase("root", "test");

  bind("show vgroups");
  ASSERT_TRUE(run());

  bind("show test.vgroups");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showMnodes) {
  setDatabase("root", "test");

  bind("show mnodes");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showModules) {
  setDatabase("root", "test");

  bind("show modules");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showQnodes) {
  setDatabase("root", "test");

  bind("show qnodes");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showFunctions) {
  setDatabase("root", "test");

  bind("show functions");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showIndexes) {
  setDatabase("root", "test");

  bind("show indexes from t1");
  ASSERT_TRUE(run());

  bind("show indexes from t1 from test");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, showStreams) {
  setDatabase("root", "test");

  bind("show streams");
  ASSERT_TRUE(run());
}
X
Xiaoyu Wang 已提交
658

X
Xiaoyu Wang 已提交
659 660 661
TEST_F(ParserTest, createSmaIndex) {
  setDatabase("root", "test");

662
  bind("create sma index index1 on t1 function(max(c1), min(c3 + 10), sum(c4)) INTERVAL(10s)");
X
Xiaoyu Wang 已提交
663 664
  ASSERT_TRUE(run());
}
X
Xiaoyu Wang 已提交
665

666 667 668 669 670 671 672
TEST_F(ParserTest, dropIndex) {
  setDatabase("root", "test");

  bind("drop index index1 on t1");
  ASSERT_TRUE(run());
}

X
Xiaoyu Wang 已提交
673 674 675 676 677 678
TEST_F(ParserTest, createQnode) {
  setDatabase("root", "test");

  bind("create qnode on dnode 1");
  ASSERT_TRUE(run());
}
679 680 681 682 683 684 685 686

TEST_F(ParserTest, dropQnode) {
  setDatabase("root", "test");

  bind("drop qnode on dnode 1");
  ASSERT_TRUE(run());
}

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
TEST_F(ParserTest, createBnode) {
  setDatabase("root", "test");

  bind("create bnode on dnode 1");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, dropBnode) {
  setDatabase("root", "test");

  bind("drop bnode on dnode 1");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, createSnode) {
  setDatabase("root", "test");

  bind("create snode on dnode 1");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, dropSnode) {
  setDatabase("root", "test");

  bind("drop snode on dnode 1");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, createMnode) {
  setDatabase("root", "test");

  bind("create mnode on dnode 1");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, dropMnode) {
  setDatabase("root", "test");

  bind("drop mnode on dnode 1");
  ASSERT_TRUE(run());
}

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
TEST_F(ParserTest, createTopic) {
  setDatabase("root", "test");

  bind("create topic tp1 as select * from t1");
  ASSERT_TRUE(run());

  bind("create topic if not exists tp1 as select * from t1");
  ASSERT_TRUE(run());

  bind("create topic tp1 as test");
  ASSERT_TRUE(run());

  bind("create topic if not exists tp1 as test");
  ASSERT_TRUE(run());
}

TEST_F(ParserTest, dropTopic) {
  setDatabase("root", "test");

  bind("drop topic tp1");
  ASSERT_TRUE(run());

  bind("drop topic if exists tp1");
  ASSERT_TRUE(run());
}
754

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
TEST_F(ParserTest, createStream) {
  setDatabase("root", "test");

  bind("create stream s1 as select * from t1");
  ASSERT_TRUE(run());

  bind("create stream if not exists s1 as select * from t1");
  ASSERT_TRUE(run());

  bind("create stream s1 into st1 as select * from t1");
  ASSERT_TRUE(run());

  bind("create stream if not exists s1 trigger window_close watermark 10s into st1 as select * from t1");
  ASSERT_TRUE(run());
}

771 772 773 774 775 776 777 778 779 780 781 782
TEST_F(ParserTest, explain) {
  setDatabase("root", "test");

  bind("explain SELECT * FROM t1");
  ASSERT_TRUE(run());

  bind("explain analyze SELECT * FROM t1");
  ASSERT_TRUE(run());

  bind("explain analyze verbose true ratio 0.01 SELECT * FROM t1");
  ASSERT_TRUE(run());
}