parInitialCTest.cpp 46.7 KB
Newer Older
1 2 3 4 5
/*
 * 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
X
Xiaoyu Wang 已提交
6
 * or later ("AGPL"), AS published by the Free Software Foundation.
7 8 9 10 11 12 13 14 15
 *
 * 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
#include <fstream>

18 19 20 21 22 23
#include "parTestUtil.h"

using namespace std;

namespace ParserTest {

X
Xiaoyu Wang 已提交
24
class ParserInitialCTest : public ParserDdlTest {};
25

X
Xiaoyu Wang 已提交
26
/*
27
 * COMPACT DATABASE db_name [START WITH start_time] [END WITH END_time]
X
Xiaoyu Wang 已提交
28
 */
X
Xiaoyu Wang 已提交
29 30 31
TEST_F(ParserInitialCTest, compact) {
  SCompactDbReq expect = {0};

32 33 34 35 36
  auto setCompactDbReq = [&](const char* pDb, int64_t start = INT64_MIN, int64_t end = INT64_MAX) {
    snprintf(expect.db, sizeof(expect.db), "0.%s", pDb);
    expect.timeRange.skey = start;
    expect.timeRange.ekey = end;
  };
X
Xiaoyu Wang 已提交
37 38 39 40 41 42 43

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_COMPACT_DATABASE_STMT);
    ASSERT_EQ(pQuery->pCmdMsg->msgType, TDMT_MND_COMPACT_DB);
    SCompactDbReq req = {0};
    ASSERT_EQ(tDeserializeSCompactDbReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req), TSDB_CODE_SUCCESS);
    ASSERT_EQ(std::string(req.db), std::string(expect.db));
44 45
    ASSERT_EQ(req.timeRange.skey, expect.timeRange.skey);
    ASSERT_EQ(req.timeRange.ekey, expect.timeRange.ekey);
X
Xiaoyu Wang 已提交
46 47
  });

48 49 50 51 52 53 54 55 56 57 58
  setCompactDbReq("test");
  run("COMPACT DATABASE test");

  setCompactDbReq("test", 1678168883000, 1678255283000);
  run("COMPACT DATABASE test START WITH '2023-03-07 14:01:23' END WITH '2023-03-08 14:01:23'");

  setCompactDbReq("testus", 1673071283000000000);
  run("COMPACT DATABASE testus START WITH TIMESTAMP '2023-01-07 14:01:23'");

  setCompactDbReq("testus", INT64_MIN, 1675749683000000000);
  run("COMPACT DATABASE testus END WITH 1675749683000000000");
X
Xiaoyu Wang 已提交
59 60
}

X
Xiaoyu Wang 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/*
 * CREATE ACCOUNT account_name PASS value [create_account_options]
 *
 * create_account_options:
 *     create_account_option ...
 *
 * create_account_option: {
 *     PPS value
 *   | TSERIES value
 *   | STORAGE value
 *   | STREAMS value
 *   | QTIME value
 *   | DBS value
 *   | USERS value
 *   | CONNS value
 *   | STATE value
 * }
 */
79 80 81
TEST_F(ParserInitialCTest, createAccount) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
82
  run("CREATE ACCOUNT ac_wxy PASS '123456'", TSDB_CODE_PAR_EXPRIE_STATEMENT, PARSER_STAGE_PARSE);
83 84
}

X
Xiaoyu Wang 已提交
85 86 87 88 89
/*
 * CREATE BNODE ON DNODE dnode_id
 * the server does not support it temporarily
 */

X
Xiaoyu Wang 已提交
90 91 92 93 94 95 96 97
/*
 * CREATE DATABASE [IF NOT EXISTS] db_name [database_options]
 *
 * database_options:
 *     database_option ...
 *
 * database_option: {
 *     BUFFER value
98 99
 *   | CACHEMODEL {'none' | 'last_row' | 'last_value' | 'both'}
 *   | CACHESIZE value
X
Xiaoyu Wang 已提交
100
 *   | COMP {0 | 1 | 2}
X
Xiaoyu Wang 已提交
101
 *   | DURATION value
X
Xiaoyu Wang 已提交
102
 *   | WAL_FSYNC_PERIOD value
X
Xiaoyu Wang 已提交
103 104 105 106 107 108 109 110
 *   | MAXROWS value
 *   | MINROWS value
 *   | KEEP value
 *   | PAGES value
 *   | PAGESIZE  value
 *   | PRECISION {'ms' | 'us' | 'ns'}
 *   | REPLICA value
 *   | RETENTIONS ingestion_duration:keep_duration ...
111
 *   | STRICT {'off' | 'on'}  // not support
X
Xiaoyu Wang 已提交
112
 *   | WAL_LEVEL value
X
Xiaoyu Wang 已提交
113 114
 *   | VGROUPS value
 *   | SINGLE_STABLE {0 | 1}
X
Xiaoyu Wang 已提交
115 116 117 118
 *   | WAL_RETENTION_PERIOD value
 *   | WAL_ROLL_PERIOD value
 *   | WAL_RETENTION_SIZE value
 *   | WAL_SEGMENT_SIZE value
X
Xiaoyu Wang 已提交
119 120
 * }
 */
121 122 123
TEST_F(ParserInitialCTest, createDatabase) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
124 125
  SCreateDbReq expect = {0};

126 127
  auto clearCreateDbReq = [&]() {
    tFreeSCreateDbReq(&expect);
X
Xiaoyu Wang 已提交
128
    memset(&expect, 0, sizeof(SCreateDbReq));
129 130
  };

X
Xiaoyu Wang 已提交
131
  auto setCreateDbReq = [&](const char* pDbname, int8_t igExists = 0) {
X
Xiaoyu Wang 已提交
132 133 134 135
    int32_t len = snprintf(expect.db, sizeof(expect.db), "0.%s", pDbname);
    expect.db[len] = '\0';
    expect.ignoreExist = igExists;
    expect.buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
136 137
    expect.cacheLast = TSDB_DEFAULT_CACHE_MODEL;
    expect.cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
X
Xiaoyu Wang 已提交
138 139
    expect.compression = TSDB_DEFAULT_COMP_LEVEL;
    expect.daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
140
    expect.walFsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
X
Xiaoyu Wang 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
    expect.maxRows = TSDB_DEFAULT_MAXROWS_FBLOCK;
    expect.minRows = TSDB_DEFAULT_MINROWS_FBLOCK;
    expect.daysToKeep0 = TSDB_DEFAULT_KEEP;
    expect.daysToKeep1 = TSDB_DEFAULT_KEEP;
    expect.daysToKeep2 = TSDB_DEFAULT_KEEP;
    expect.pages = TSDB_DEFAULT_PAGES_PER_VNODE;
    expect.pageSize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
    expect.precision = TSDB_DEFAULT_PRECISION;
    expect.replications = TSDB_DEFAULT_DB_REPLICA;
    expect.strict = TSDB_DEFAULT_DB_STRICT;
    expect.walLevel = TSDB_DEFAULT_WAL_LEVEL;
    expect.numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
    expect.numOfStables = TSDB_DEFAULT_DB_SINGLE_STABLE;
X
Xiaoyu Wang 已提交
154
    expect.schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
155 156 157
    expect.walRetentionPeriod = TSDB_REP_DEF_DB_WAL_RET_PERIOD;
    expect.walRetentionSize = TSDB_REP_DEF_DB_WAL_RET_SIZE;
    expect.walRollPeriod = TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
158
    expect.walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
159
    expect.sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
160 161
    expect.hashPrefix = TSDB_DEFAULT_HASH_PREFIX;
    expect.hashSuffix = TSDB_DEFAULT_HASH_SUFFIX;
162
    expect.tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
X
Xiaoyu Wang 已提交
163 164
  };

X
Xiaoyu Wang 已提交
165 166
  auto setDbBuffer = [&](int32_t buffer) { expect.buffer = buffer; };
  auto setDbCachelast = [&](int8_t cachelast) { expect.cacheLast = cachelast; };
167
  auto setDbCachelastSize = [&](int8_t cachelastSize) { expect.cacheLastSize = cachelastSize; };
X
Xiaoyu Wang 已提交
168 169 170 171 172 173
  auto setDbCompression = [&](int8_t compressionLevel) { expect.compression = compressionLevel; };
  auto setDbDays = [&](int32_t daysPerFile) { expect.daysPerFile = daysPerFile; };
  auto setDbFsync = [&](int32_t fsyncPeriod) { expect.walFsyncPeriod = fsyncPeriod; };
  auto setDbMaxRows = [&](int32_t maxRowsPerBlock) { expect.maxRows = maxRowsPerBlock; };
  auto setDbMinRows = [&](int32_t minRowsPerBlock) { expect.minRows = minRowsPerBlock; };
  auto setDbKeep = [&](int32_t keep0, int32_t keep1 = 0, int32_t keep2 = 0) {
X
Xiaoyu Wang 已提交
174 175 176 177
    expect.daysToKeep0 = keep0;
    expect.daysToKeep1 = 0 == keep1 ? expect.daysToKeep0 : keep1;
    expect.daysToKeep2 = 0 == keep2 ? expect.daysToKeep1 : keep2;
  };
X
Xiaoyu Wang 已提交
178 179 180 181 182 183 184 185 186
  auto setDbPages = [&](int32_t pages) { expect.pages = pages; };
  auto setDbPageSize = [&](int32_t pagesize) { expect.pageSize = pagesize; };
  auto setDbPrecision = [&](int8_t precision) { expect.precision = precision; };
  auto setDbReplica = [&](int8_t replica) { expect.replications = replica; };
  auto setDbStricta = [&](int8_t strict) { expect.strict = strict; };
  auto setDbWalLevel = [&](int8_t walLevel) { expect.walLevel = walLevel; };
  auto setDbVgroups = [&](int32_t numOfVgroups) { expect.numOfVgroups = numOfVgroups; };
  auto setDbSingleStable = [&](int8_t singleStable) { expect.numOfStables = singleStable; };
  auto addDbRetention = [&](int64_t freq, int64_t keep, int8_t freqUnit, int8_t keepUnit) {
X
Xiaoyu Wang 已提交
187 188 189 190 191 192 193 194 195 196 197
    SRetention retention = {0};
    retention.freq = freq;
    retention.keep = keep;
    retention.freqUnit = freqUnit;
    retention.keepUnit = keepUnit;
    if (NULL == expect.pRetensions) {
      expect.pRetensions = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SRetention));
    }
    taosArrayPush(expect.pRetensions, &retention);
    ++expect.numOfRetensions;
  };
X
Xiaoyu Wang 已提交
198
  auto setDbSchemaless = [&](int8_t schemaless) { expect.schemaless = schemaless; };
X
Xiaoyu Wang 已提交
199 200 201 202
  auto setDbWalRetentionPeriod = [&](int32_t walRetentionPeriod) { expect.walRetentionPeriod = walRetentionPeriod; };
  auto setDbWalRetentionSize = [&](int32_t walRetentionSize) { expect.walRetentionSize = walRetentionSize; };
  auto setDbWalRollPeriod = [&](int32_t walRollPeriod) { expect.walRollPeriod = walRollPeriod; };
  auto setDbWalSegmentSize = [&](int32_t walSegmentSize) { expect.walSegmentSize = walSegmentSize; };
203
  auto setDbSstTrigger = [&](int32_t sstTrigger) { expect.sstTrigger = sstTrigger; };
204 205
  auto setDbHashPrefix = [&](int32_t hashPrefix) { expect.hashPrefix = hashPrefix; };
  auto setDbHashSuffix = [&](int32_t hashSuffix) { expect.hashSuffix = hashSuffix; };
206
  auto setDbTsdbPageSize = [&](int32_t tsdbPageSize) { expect.tsdbPageSize = tsdbPageSize; };
X
Xiaoyu Wang 已提交
207 208 209

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_DATABASE_STMT);
X
Xiaoyu Wang 已提交
210
    ASSERT_EQ(pQuery->pCmdMsg->msgType, TDMT_MND_CREATE_DB);
X
Xiaoyu Wang 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    SCreateDbReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSCreateDbReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.db), std::string(expect.db));
    ASSERT_EQ(req.numOfVgroups, expect.numOfVgroups);
    ASSERT_EQ(req.numOfStables, expect.numOfStables);
    ASSERT_EQ(req.buffer, expect.buffer);
    ASSERT_EQ(req.pageSize, expect.pageSize);
    ASSERT_EQ(req.pages, expect.pages);
    ASSERT_EQ(req.daysPerFile, expect.daysPerFile);
    ASSERT_EQ(req.daysToKeep0, expect.daysToKeep0);
    ASSERT_EQ(req.daysToKeep1, expect.daysToKeep1);
    ASSERT_EQ(req.daysToKeep2, expect.daysToKeep2);
    ASSERT_EQ(req.minRows, expect.minRows);
    ASSERT_EQ(req.maxRows, expect.maxRows);
226
    ASSERT_EQ(req.walFsyncPeriod, expect.walFsyncPeriod);
X
Xiaoyu Wang 已提交
227 228 229 230 231
    ASSERT_EQ(req.walLevel, expect.walLevel);
    ASSERT_EQ(req.precision, expect.precision);
    ASSERT_EQ(req.compression, expect.compression);
    ASSERT_EQ(req.replications, expect.replications);
    ASSERT_EQ(req.strict, expect.strict);
232 233
    ASSERT_EQ(req.cacheLast, expect.cacheLast);
    ASSERT_EQ(req.cacheLastSize, expect.cacheLastSize);
X
Xiaoyu Wang 已提交
234 235 236 237
    ASSERT_EQ(req.walRetentionPeriod, expect.walRetentionPeriod);
    ASSERT_EQ(req.walRetentionSize, expect.walRetentionSize);
    ASSERT_EQ(req.walRollPeriod, expect.walRollPeriod);
    ASSERT_EQ(req.walSegmentSize, expect.walSegmentSize);
238
    ASSERT_EQ(req.sstTrigger, expect.sstTrigger);
239 240
    ASSERT_EQ(req.hashPrefix, expect.hashPrefix);
    ASSERT_EQ(req.hashSuffix, expect.hashSuffix);
241
    ASSERT_EQ(req.tsdbPageSize, expect.tsdbPageSize);
X
Xiaoyu Wang 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255
    ASSERT_EQ(req.ignoreExist, expect.ignoreExist);
    ASSERT_EQ(req.numOfRetensions, expect.numOfRetensions);
    if (expect.numOfRetensions > 0) {
      ASSERT_EQ(taosArrayGetSize(req.pRetensions), expect.numOfRetensions);
      ASSERT_EQ(taosArrayGetSize(req.pRetensions), taosArrayGetSize(expect.pRetensions));
      for (int32_t i = 0; i < expect.numOfRetensions; ++i) {
        SRetention* pReten = (SRetention*)taosArrayGet(req.pRetensions, i);
        SRetention* pExpectReten = (SRetention*)taosArrayGet(expect.pRetensions, i);
        ASSERT_EQ(pReten->freq, pExpectReten->freq);
        ASSERT_EQ(pReten->keep, pExpectReten->keep);
        ASSERT_EQ(pReten->freqUnit, pExpectReten->freqUnit);
        ASSERT_EQ(pReten->keepUnit, pExpectReten->keepUnit);
      }
    }
256
    tFreeSCreateDbReq(&req);
X
Xiaoyu Wang 已提交
257 258
  });

X
Xiaoyu Wang 已提交
259
  setCreateDbReq("wxy_db");
X
Xiaoyu Wang 已提交
260
  run("CREATE DATABASE wxy_db");
261
  clearCreateDbReq();
X
Xiaoyu Wang 已提交
262

X
Xiaoyu Wang 已提交
263 264 265
  setCreateDbReq("wxy_db", 1);
  setDbBuffer(64);
  setDbCachelast(2);
266
  setDbCachelastSize(20);
X
Xiaoyu Wang 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  setDbCompression(1);
  setDbDays(100 * 1440);
  setDbFsync(100);
  setDbMaxRows(1000);
  setDbMinRows(100);
  setDbKeep(1440 * 1440);
  setDbPages(96);
  setDbPageSize(8);
  setDbPrecision(TSDB_TIME_PRECISION_NANO);
  setDbReplica(3);
  addDbRetention(15 * MILLISECOND_PER_SECOND, 7 * MILLISECOND_PER_DAY, TIME_UNIT_SECOND, TIME_UNIT_DAY);
  addDbRetention(1 * MILLISECOND_PER_MINUTE, 21 * MILLISECOND_PER_DAY, TIME_UNIT_MINUTE, TIME_UNIT_DAY);
  addDbRetention(15 * MILLISECOND_PER_MINUTE, 500 * MILLISECOND_PER_DAY, TIME_UNIT_MINUTE, TIME_UNIT_DAY);
  // setDbStricta(1);
  setDbWalLevel(2);
  setDbVgroups(100);
  setDbSingleStable(1);
  setDbSchemaless(1);
X
Xiaoyu Wang 已提交
285 286 287 288
  setDbWalRetentionPeriod(-1);
  setDbWalRetentionSize(-1);
  setDbWalRollPeriod(10);
  setDbWalSegmentSize(20);
289
  setDbSstTrigger(16);
290 291
  setDbHashPrefix(3);
  setDbHashSuffix(4);
292
  setDbTsdbPageSize(32);
X
Xiaoyu Wang 已提交
293 294
  run("CREATE DATABASE IF NOT EXISTS wxy_db "
      "BUFFER 64 "
295 296
      "CACHEMODEL 'last_value' "
      "CACHESIZE 20 "
X
Xiaoyu Wang 已提交
297
      "COMP 1 "
X
Xiaoyu Wang 已提交
298
      "DURATION 100 "
X
Xiaoyu Wang 已提交
299
      "WAL_FSYNC_PERIOD 100 "
X
Xiaoyu Wang 已提交
300 301 302 303 304 305 306
      "MAXROWS 1000 "
      "MINROWS 100 "
      "KEEP 1440 "
      "PAGES 96 "
      "PAGESIZE 8 "
      "PRECISION 'ns' "
      "REPLICA 3 "
X
Xiaoyu Wang 已提交
307
      "RETENTIONS 15s:7d,1m:21d,15m:500d "
308
      //      "STRICT 'on' "
X
Xiaoyu Wang 已提交
309
      "WAL_LEVEL 2 "
X
Xiaoyu Wang 已提交
310
      "VGROUPS 100 "
X
Xiaoyu Wang 已提交
311
      "SINGLE_STABLE 1 "
X
Xiaoyu Wang 已提交
312 313 314 315
      "SCHEMALESS 1 "
      "WAL_RETENTION_PERIOD -1 "
      "WAL_RETENTION_SIZE -1 "
      "WAL_ROLL_PERIOD 10 "
316
      "WAL_SEGMENT_SIZE 20 "
317 318 319 320
      "STT_TRIGGER 16 "
      "TABLE_PREFIX 3 "
      "TABLE_SUFFIX 4 "
      "TSDB_PAGESIZE 32");
321
  clearCreateDbReq();
X
Xiaoyu Wang 已提交
322

X
Xiaoyu Wang 已提交
323 324 325
  setCreateDbReq("wxy_db", 1);
  setDbDays(100);
  setDbKeep(1440, 300 * 60, 400 * 1440);
X
Xiaoyu Wang 已提交
326
  run("CREATE DATABASE IF NOT EXISTS wxy_db "
X
Xiaoyu Wang 已提交
327
      "DURATION 100m "
X
Xiaoyu Wang 已提交
328
      "KEEP 1440m,300h,400d ");
329
  clearCreateDbReq();
330

X
Xiaoyu Wang 已提交
331 332
  setCreateDbReq("wxy_db", 1);
  setDbReplica(3);
333 334 335 336 337
  setDbWalRetentionPeriod(TSDB_REPS_DEF_DB_WAL_RET_PERIOD);
  setDbWalRetentionSize(TSDB_REPS_DEF_DB_WAL_RET_SIZE);
  setDbWalRollPeriod(TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD);
  run("CREATE DATABASE IF NOT EXISTS wxy_db REPLICA 3");
  clearCreateDbReq();
338 339
}

X
Xiaoyu Wang 已提交
340 341 342
TEST_F(ParserInitialCTest, createDatabaseSemanticCheck) {
  useDb("root", "test");

343 344 345 346 347 348
  run("create database db2 retentions 0s:1d", TSDB_CODE_PAR_INVALID_DB_OPTION);
  run("create database db2 retentions 10s:0d", TSDB_CODE_PAR_INVALID_DB_OPTION);
  run("create database db2 retentions 1w:1d", TSDB_CODE_PAR_INVALID_DB_OPTION);
  run("create database db2 retentions 1w:1n", TSDB_CODE_PAR_INVALID_DB_OPTION);
  run("create database db2 retentions 15s:7d,15m:21d,10m:500d", TSDB_CODE_PAR_INVALID_DB_OPTION);
  run("create database db2 retentions 15s:7d,5m:21d,10m:10d", TSDB_CODE_PAR_INVALID_DB_OPTION);
X
Xiaoyu Wang 已提交
349 350
}

X
Xiaoyu Wang 已提交
351 352 353
/*
 * CREATE DNODE {dnode_endpoint | dnode_host_name PORT port_val}
 */
354 355 356
TEST_F(ParserInitialCTest, createDnode) {
  useDb("root", "test");

357
  SCreateDnodeReq expect = {0};
358

359 360
  auto clearCreateDnodeReq = [&]() { memset(&expect, 0, sizeof(SCreateDnodeReq)); };

X
Xiaoyu Wang 已提交
361
  auto setCreateDnodeReq = [&](const char* pFqdn, int32_t port = tsServerPort) {
362 363 364 365 366 367 368 369 370 371 372 373 374
    strcpy(expect.fqdn, pFqdn);
    expect.port = port;
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_DNODE_STMT);
    SCreateDnodeReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSCreateDnodeReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.fqdn), std::string(expect.fqdn));
    ASSERT_EQ(req.port, expect.port);
  });

X
Xiaoyu Wang 已提交
375
  setCreateDnodeReq("abc1", 7030);
376 377 378
  run("CREATE DNODE 'abc1' PORT 7030");
  clearCreateDnodeReq();

X
Xiaoyu Wang 已提交
379
  setCreateDnodeReq("1.1.1.1", 8030);
380 381 382
  run("CREATE DNODE 1.1.1.1 PORT 8030");
  clearCreateDnodeReq();

X
Xiaoyu Wang 已提交
383
  setCreateDnodeReq("host1", 9030);
384 385 386
  run("CREATE DNODE host1 PORT 9030");
  clearCreateDnodeReq();

X
Xiaoyu Wang 已提交
387
  setCreateDnodeReq("abc2", 7040);
388 389 390
  run("CREATE DNODE 'abc2:7040'");
  clearCreateDnodeReq();

X
Xiaoyu Wang 已提交
391
  setCreateDnodeReq("1.1.1.2");
392 393 394
  run("CREATE DNODE 1.1.1.2");
  clearCreateDnodeReq();

X
Xiaoyu Wang 已提交
395
  setCreateDnodeReq("host2");
396 397
  run("CREATE DNODE host2");
  clearCreateDnodeReq();
398 399
}

X
Xiaoyu Wang 已提交
400
/*
401
 * CREATE [OR REPLACE] [AGGREGATE] FUNCTION [IF NOT EXISTS] func_name
402
 *   AS library_path OUTPUTTYPE type_name [BUFSIZE value] [LANGUAGE value]
X
Xiaoyu Wang 已提交
403
 */
404 405 406 407 408
TEST_F(ParserInitialCTest, createFunction) {
  useDb("root", "test");

  SCreateFuncReq expect = {0};

X
Xiaoyu Wang 已提交
409
  auto setCreateFuncReq = [&](const char* pUdfName, int8_t outputType, int32_t outputBytes = 0,
410
                              int8_t funcType = TSDB_FUNC_TYPE_SCALAR, int8_t igExists = 0, int32_t bufSize = 0,
411
                              int8_t language = TSDB_FUNC_SCRIPT_BIN_LIB, int8_t orReplace = 0) {
412 413
    memset(&expect, 0, sizeof(SCreateFuncReq));
    strcpy(expect.name, pUdfName);
414
    expect.orReplace = orReplace;
415 416
    expect.igExists = igExists;
    expect.funcType = funcType;
417
    expect.scriptType = language;
418 419 420 421 422 423 424 425 426 427 428
    expect.outputType = outputType;
    expect.outputLen = outputBytes > 0 ? outputBytes : tDataTypes[outputType].bytes;
    expect.bufSize = bufSize;
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_FUNCTION_STMT);
    SCreateFuncReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSCreateFuncReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.name), std::string(expect.name));
429
    ASSERT_EQ(req.orReplace, expect.orReplace);
430 431 432 433 434 435
    ASSERT_EQ(req.igExists, expect.igExists);
    ASSERT_EQ(req.funcType, expect.funcType);
    ASSERT_EQ(req.scriptType, expect.scriptType);
    ASSERT_EQ(req.outputType, expect.outputType);
    ASSERT_EQ(req.outputLen, expect.outputLen);
    ASSERT_EQ(req.bufSize, expect.bufSize);
436 437

    tFreeSCreateFuncReq(&req);
438 439
  });

440 441 442 443 444 445 446 447 448 449
  struct udfFile {
    udfFile(const std::string& filename) : path_(filename) {
      std::ofstream file(filename, std::ios::binary);
      file << 123 << "abc" << '\n';
      file.close();
    }
    ~udfFile() { remove(path_.c_str()); }
    std::string path_;
  } udffile("udf");

X
Xiaoyu Wang 已提交
450
  setCreateFuncReq("udf1", TSDB_DATA_TYPE_INT);
451
  run("CREATE FUNCTION udf1 AS 'udf' OUTPUTTYPE INT");
452

453 454
  setCreateFuncReq("udf2", TSDB_DATA_TYPE_DOUBLE, 0, TSDB_FUNC_TYPE_AGGREGATE, 1, 8, TSDB_FUNC_SCRIPT_PYTHON, 1);
  run("CREATE OR REPLACE AGGREGATE FUNCTION IF NOT EXISTS udf2 AS 'udf' OUTPUTTYPE DOUBLE BUFSIZE 8 LANGUAGE 'python'");
455
}
456

X
Xiaoyu Wang 已提交
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
/*
 * CREATE MNODE ON DNODE dnode_id
 */
TEST_F(ParserInitialCTest, createMnode) {
  useDb("root", "test");

  SMCreateMnodeReq expect = {0};

  auto setCreateMnodeReq = [&](int32_t dnodeId) { expect.dnodeId = dnodeId; };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_MNODE_STMT);
    SMCreateMnodeReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS ==
                tDeserializeSCreateDropMQSNodeReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(req.dnodeId, expect.dnodeId);
  });

  setCreateMnodeReq(1);
  run("CREATE MNODE ON DNODE 1");
}

/*
 * CREATE QNODE ON DNODE dnode_id
 */
TEST_F(ParserInitialCTest, createQnode) {
  useDb("root", "test");

  SMCreateQnodeReq expect = {0};

  auto setCreateQnodeReq = [&](int32_t dnodeId) { expect.dnodeId = dnodeId; };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_QNODE_STMT);
    SMCreateQnodeReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS ==
                tDeserializeSCreateDropMQSNodeReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(req.dnodeId, expect.dnodeId);
  });

  setCreateQnodeReq(1);
  run("CREATE QNODE ON DNODE 1");
}

/*
 * CREATE SMA INDEX index_name ON tb_name index_option
 *
 * index_option:
 *     FUNCTION(functions) INTERVAL(interval_val [, interval_offset]) [SLIDING(sliding_val)]
 *         [WATERMARK(watermark_val)] [MAX_DELAY(max_delay_val)]
 *
 * functions:
 *     function [, function] ...
 */
513
TEST_F(ParserInitialCTest, createSmaIndex) {
514 515
  useDb("root", "test");

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  SMCreateSmaReq expect = {0};

  auto setCreateSmacReq = [&](const char* pIndexName, const char* pStbName, int64_t interval, int8_t intervalUnit,
                              int64_t offset = 0, int64_t sliding = -1, int8_t slidingUnit = -1, int8_t igExists = 0) {
    memset(&expect, 0, sizeof(SMCreateSmaReq));
    strcpy(expect.name, pIndexName);
    strcpy(expect.stb, pStbName);
    expect.igExists = igExists;
    expect.intervalUnit = intervalUnit;
    expect.slidingUnit = slidingUnit < 0 ? intervalUnit : slidingUnit;
    expect.timezone = 0;
    expect.dstVgId = 1;
    expect.interval = interval;
    expect.offset = offset;
    expect.sliding = sliding < 0 ? interval : sliding;
    expect.maxDelay = -1;
    expect.watermark = TSDB_DEFAULT_ROLLUP_WATERMARK;
    expect.deleteMark = TSDB_DEFAULT_ROLLUP_DELETE_MARK;
  };

  auto setOptionsForCreateSmacReq = [&](int64_t maxDelay, int64_t watermark, int64_t deleteMark) {
    expect.maxDelay = maxDelay;
    expect.watermark = watermark;
    expect.deleteMark = deleteMark;
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_INDEX_STMT);
    SMCreateSmaReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSMCreateSmaReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.name), std::string(expect.name));
    ASSERT_EQ(std::string(req.stb), std::string(expect.stb));
    ASSERT_EQ(req.igExists, expect.igExists);
    ASSERT_EQ(req.intervalUnit, expect.intervalUnit);
    ASSERT_EQ(req.slidingUnit, expect.slidingUnit);
    ASSERT_EQ(req.timezone, expect.timezone);
    ASSERT_EQ(req.dstVgId, expect.dstVgId);
    ASSERT_EQ(req.interval, expect.interval);
    ASSERT_EQ(req.offset, expect.offset);
    ASSERT_EQ(req.sliding, expect.sliding);
    ASSERT_EQ(req.maxDelay, expect.maxDelay);
    ASSERT_EQ(req.watermark, expect.watermark);
    ASSERT_EQ(req.deleteMark, expect.deleteMark);
    ASSERT_GT(req.exprLen, 0);
    ASSERT_EQ(req.tagsFilterLen, 0);
    ASSERT_GT(req.sqlLen, 0);
    ASSERT_GT(req.astLen, 0);
    ASSERT_NE(req.expr, nullptr);
    ASSERT_EQ(req.tagsFilter, nullptr);
    ASSERT_NE(req.sql, nullptr);
    ASSERT_NE(req.ast, nullptr);
    tFreeSMCreateSmaReq(&req);
  });

  setCreateSmacReq("0.test.index1", "0.test.t1", 10 * MILLISECOND_PER_SECOND, 's');
X
Xiaoyu Wang 已提交
572
  run("CREATE SMA INDEX index1 ON t1 FUNCTION(MAX(c1), MIN(c3 + 10), SUM(c4)) INTERVAL(10s)");
573

574 575 576 577
  setCreateSmacReq("0.test.index2", "0.test.st1", 5 * MILLISECOND_PER_SECOND, 's');
  setOptionsForCreateSmacReq(10 * MILLISECOND_PER_SECOND, 20 * MILLISECOND_PER_SECOND, 1000 * MILLISECOND_PER_SECOND);
  run("CREATE SMA INDEX index2 ON st1 FUNCTION(MAX(c1), MIN(tag1)) INTERVAL(5s) WATERMARK 20s MAX_DELAY 10s "
      "DELETE_MARK 1000s");
578 579
}

X
Xiaoyu Wang 已提交
580 581 582 583
/*
 * CREATE SNODE ON DNODE dnode_id
 */
TEST_F(ParserInitialCTest, createSnode) {
584 585
  useDb("root", "test");

X
Xiaoyu Wang 已提交
586
  SMCreateSnodeReq expect = {0};
587

X
Xiaoyu Wang 已提交
588
  auto setCreateSnodeReq = [&](int32_t dnodeId) { expect.dnodeId = dnodeId; };
589

X
Xiaoyu Wang 已提交
590 591 592 593 594
  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_SNODE_STMT);
    SMCreateSnodeReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS ==
                tDeserializeSCreateDropMQSNodeReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));
595

X
Xiaoyu Wang 已提交
596 597
    ASSERT_EQ(req.dnodeId, expect.dnodeId);
  });
598

X
Xiaoyu Wang 已提交
599
  setCreateSnodeReq(1);
X
Xiaoyu Wang 已提交
600
  run("CREATE SNODE ON DNODE 1");
601 602
}

X
Xiaoyu Wang 已提交
603 604 605 606 607 608 609 610 611 612
/*
 * CREATE STABLE [IF NOT EXISTS] stb_name (create_definition [, create_definitionn] ...)
 *     TAGS (create_definition [, create_definition] ...) [table_options]
 *
 * create_definition:
 *     col_name column_definition
 *
 * column_definition:
 *     type_name [COMMENT 'string_value']
 */
613 614 615
TEST_F(ParserInitialCTest, createStable) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
616 617
  SMCreateStbReq expect = {0};

618 619 620 621 622
  auto clearCreateStbReq = [&]() {
    tFreeSMCreateStbReq(&expect);
    memset(&expect, 0, sizeof(SMCreateStbReq));
  };

X
Xiaoyu Wang 已提交
623
  auto setCreateStbReq =
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
      [&](const char* pDbName, const char* pTbName, int8_t igExists = 0, int64_t delay1 = -1, int64_t delay2 = -1,
          int64_t watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK, int64_t watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK,
          int64_t deleteMark1 = TSDB_DEFAULT_ROLLUP_DELETE_MARK, int64_t deleteMark2 = TSDB_DEFAULT_ROLLUP_DELETE_MARK,
          int32_t ttl = TSDB_DEFAULT_TABLE_TTL, const char* pComment = nullptr) {
        int32_t len = snprintf(expect.name, sizeof(expect.name), "0.%s.%s", pDbName, pTbName);
        expect.name[len] = '\0';
        expect.igExists = igExists;
        expect.delay1 = delay1;
        expect.delay2 = delay2;
        expect.watermark1 = watermark1;
        expect.watermark2 = watermark2;
        expect.deleteMark1 = deleteMark1;
        expect.deleteMark2 = deleteMark2;
        // expect.ttl = ttl;
        if (nullptr != pComment) {
639
          expect.pComment = taosStrdup(pComment);
640 641 642
          expect.commentLen = strlen(pComment);
        }
      };
X
Xiaoyu Wang 已提交
643

X
Xiaoyu Wang 已提交
644 645
  auto addFieldToCreateStbReq = [&](bool col, const char* pFieldName, uint8_t type, int32_t bytes = 0,
                                    int8_t flags = COL_SMA_ON) {
X
Xiaoyu Wang 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    SField field = {0};
    strcpy(field.name, pFieldName);
    field.type = type;
    field.bytes = bytes > 0 ? bytes : tDataTypes[type].bytes;
    field.flags = flags;

    if (col) {
      if (NULL == expect.pColumns) {
        expect.pColumns = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SField));
      }
      taosArrayPush(expect.pColumns, &field);
      expect.numOfColumns += 1;
    } else {
      if (NULL == expect.pTags) {
        expect.pTags = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SField));
      }
      taosArrayPush(expect.pTags, &field);
      expect.numOfTags += 1;
    }
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_TABLE_STMT);
    SMCreateStbReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSMCreateStbReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.name), std::string(expect.name));
    ASSERT_EQ(req.igExists, expect.igExists);
X
Xiaoyu Wang 已提交
674 675 676 677
    ASSERT_EQ(req.delay1, expect.delay1);
    ASSERT_EQ(req.delay2, expect.delay2);
    ASSERT_EQ(req.watermark1, expect.watermark1);
    ASSERT_EQ(req.watermark2, expect.watermark2);
X
Xiaoyu Wang 已提交
678 679 680
    ASSERT_EQ(req.ttl, expect.ttl);
    ASSERT_EQ(req.numOfColumns, expect.numOfColumns);
    ASSERT_EQ(req.numOfTags, expect.numOfTags);
X
Xiaoyu Wang 已提交
681
    //    ASSERT_EQ(req.commentLen, expect.commentLen);
X
Xiaoyu Wang 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    ASSERT_EQ(req.ast1Len, expect.ast1Len);
    ASSERT_EQ(req.ast2Len, expect.ast2Len);

    if (expect.numOfColumns > 0) {
      ASSERT_EQ(taosArrayGetSize(req.pColumns), expect.numOfColumns);
      ASSERT_EQ(taosArrayGetSize(req.pColumns), taosArrayGetSize(expect.pColumns));
      for (int32_t i = 0; i < expect.numOfColumns; ++i) {
        SField* pField = (SField*)taosArrayGet(req.pColumns, i);
        SField* pExpectField = (SField*)taosArrayGet(expect.pColumns, i);
        ASSERT_EQ(std::string(pField->name), std::string(pExpectField->name));
        ASSERT_EQ(pField->type, pExpectField->type);
        ASSERT_EQ(pField->bytes, pExpectField->bytes);
        ASSERT_EQ(pField->flags, pExpectField->flags);
      }
    }
    if (expect.numOfTags > 0) {
      ASSERT_EQ(taosArrayGetSize(req.pTags), expect.numOfTags);
      ASSERT_EQ(taosArrayGetSize(req.pTags), taosArrayGetSize(expect.pTags));
      for (int32_t i = 0; i < expect.numOfTags; ++i) {
        SField* pField = (SField*)taosArrayGet(req.pTags, i);
        SField* pExpectField = (SField*)taosArrayGet(expect.pTags, i);
        ASSERT_EQ(std::string(pField->name), std::string(pExpectField->name));
        ASSERT_EQ(pField->type, pExpectField->type);
        ASSERT_EQ(pField->bytes, pExpectField->bytes);
        ASSERT_EQ(pField->flags, pExpectField->flags);
      }
    }
    if (expect.commentLen > 0) {
S
Shengliang Guan 已提交
710
      ASSERT_EQ(std::string(req.pComment), std::string(expect.pComment));
X
Xiaoyu Wang 已提交
711 712 713 714 715 716 717
    }
    if (expect.ast1Len > 0) {
      ASSERT_EQ(std::string(req.pAst1), std::string(expect.pAst1));
    }
    if (expect.ast2Len > 0) {
      ASSERT_EQ(std::string(req.pAst2), std::string(expect.pAst2));
    }
718
    tFreeSMCreateStbReq(&req);
X
Xiaoyu Wang 已提交
719 720
  });

X
Xiaoyu Wang 已提交
721 722 723 724
  setCreateStbReq("test", "t1");
  addFieldToCreateStbReq(true, "ts", TSDB_DATA_TYPE_TIMESTAMP);
  addFieldToCreateStbReq(true, "c1", TSDB_DATA_TYPE_INT);
  addFieldToCreateStbReq(false, "id", TSDB_DATA_TYPE_INT);
X
Xiaoyu Wang 已提交
725
  run("CREATE STABLE t1(ts TIMESTAMP, c1 INT) TAGS(id INT)");
726
  clearCreateStbReq();
727

X
Xiaoyu Wang 已提交
728 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 754 755 756 757 758 759 760
  setCreateStbReq("rollup_db", "t1", 1, 100 * MILLISECOND_PER_SECOND, 10 * MILLISECOND_PER_MINUTE, 10,
                  1 * MILLISECOND_PER_MINUTE, 1000 * MILLISECOND_PER_SECOND, 200 * MILLISECOND_PER_MINUTE, 100,
                  "test create table");
  addFieldToCreateStbReq(true, "ts", TSDB_DATA_TYPE_TIMESTAMP, 0, 0);
  addFieldToCreateStbReq(true, "c1", TSDB_DATA_TYPE_INT);
  addFieldToCreateStbReq(true, "c2", TSDB_DATA_TYPE_UINT);
  addFieldToCreateStbReq(true, "c3", TSDB_DATA_TYPE_BIGINT);
  addFieldToCreateStbReq(true, "c4", TSDB_DATA_TYPE_UBIGINT, 0, 0);
  addFieldToCreateStbReq(true, "c5", TSDB_DATA_TYPE_FLOAT, 0, 0);
  addFieldToCreateStbReq(true, "c6", TSDB_DATA_TYPE_DOUBLE, 0, 0);
  addFieldToCreateStbReq(true, "c7", TSDB_DATA_TYPE_BINARY, 20 + VARSTR_HEADER_SIZE, 0);
  addFieldToCreateStbReq(true, "c8", TSDB_DATA_TYPE_SMALLINT, 0, 0);
  addFieldToCreateStbReq(true, "c9", TSDB_DATA_TYPE_USMALLINT, 0, 0);
  addFieldToCreateStbReq(true, "c10", TSDB_DATA_TYPE_TINYINT, 0, 0);
  addFieldToCreateStbReq(true, "c11", TSDB_DATA_TYPE_UTINYINT, 0, 0);
  addFieldToCreateStbReq(true, "c12", TSDB_DATA_TYPE_BOOL, 0, 0);
  addFieldToCreateStbReq(true, "c13", TSDB_DATA_TYPE_NCHAR, 30 * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE, 0);
  addFieldToCreateStbReq(true, "c14", TSDB_DATA_TYPE_VARCHAR, 50 + VARSTR_HEADER_SIZE, 0);
  addFieldToCreateStbReq(false, "a1", TSDB_DATA_TYPE_TIMESTAMP);
  addFieldToCreateStbReq(false, "a2", TSDB_DATA_TYPE_INT);
  addFieldToCreateStbReq(false, "a3", TSDB_DATA_TYPE_UINT);
  addFieldToCreateStbReq(false, "a4", TSDB_DATA_TYPE_BIGINT);
  addFieldToCreateStbReq(false, "a5", TSDB_DATA_TYPE_UBIGINT);
  addFieldToCreateStbReq(false, "a6", TSDB_DATA_TYPE_FLOAT);
  addFieldToCreateStbReq(false, "a7", TSDB_DATA_TYPE_DOUBLE);
  addFieldToCreateStbReq(false, "a8", TSDB_DATA_TYPE_BINARY, 20 + VARSTR_HEADER_SIZE);
  addFieldToCreateStbReq(false, "a9", TSDB_DATA_TYPE_SMALLINT);
  addFieldToCreateStbReq(false, "a10", TSDB_DATA_TYPE_USMALLINT);
  addFieldToCreateStbReq(false, "a11", TSDB_DATA_TYPE_TINYINT);
  addFieldToCreateStbReq(false, "a12", TSDB_DATA_TYPE_UTINYINT);
  addFieldToCreateStbReq(false, "a13", TSDB_DATA_TYPE_BOOL);
  addFieldToCreateStbReq(false, "a14", TSDB_DATA_TYPE_NCHAR, 30 * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE);
  addFieldToCreateStbReq(false, "a15", TSDB_DATA_TYPE_VARCHAR, 50 + VARSTR_HEADER_SIZE);
761
  run("CREATE STABLE IF NOT EXISTS rollup_db.t1("
X
Xiaoyu Wang 已提交
762
      "ts TIMESTAMP, c1 INT, c2 INT UNSIGNED, c3 BIGINT, c4 BIGINT UNSIGNED, c5 FLOAT, c6 DOUBLE, c7 BINARY(20), "
763
      "c8 SMALLINT, c9 SMALLINT UNSIGNED, c10 TINYINT, c11 TINYINT UNSIGNED, c12 BOOL, "
X
Xiaoyu Wang 已提交
764 765
      "c13 NCHAR(30), c14 VARCHAR(50)) "
      "TAGS (a1 TIMESTAMP, a2 INT, a3 INT UNSIGNED, a4 BIGINT, a5 BIGINT UNSIGNED, a6 FLOAT, a7 DOUBLE, "
766
      "a8 BINARY(20), a9 SMALLINT, a10 SMALLINT UNSIGNED, a11 TINYINT, "
X
Xiaoyu Wang 已提交
767
      "a12 TINYINT UNSIGNED, a13 BOOL, a14 NCHAR(30), a15 VARCHAR(50)) "
768 769
      "TTL 100 COMMENT 'test create table' SMA(c1, c2, c3) ROLLUP (MIN) MAX_DELAY 100s,10m WATERMARK 10a,1m "
      "DELETE_MARK 1000s,200m");
770
  clearCreateStbReq();
771 772
}

X
Xiaoyu Wang 已提交
773 774 775
TEST_F(ParserInitialCTest, createStableSemanticCheck) {
  useDb("root", "test");

776
  run("CREATE STABLE rollup_db.stb2 (ts TIMESTAMP, c1 INT) TAGS (tag1 INT) ROLLUP(CEIL)",
777
      TSDB_CODE_PAR_INVALID_TABLE_OPTION);
X
Xiaoyu Wang 已提交
778

779
  run("CREATE STABLE rollup_db.stb2 (ts TIMESTAMP, c1 INT) TAGS (tag1 INT) ROLLUP(MAX) MAX_DELAY 0s WATERMARK 1m",
780
      TSDB_CODE_PAR_INVALID_TABLE_OPTION);
X
Xiaoyu Wang 已提交
781

782
  run("CREATE STABLE rollup_db.stb2 (ts TIMESTAMP, c1 INT) TAGS (tag1 INT) ROLLUP(MAX) MAX_DELAY 10s WATERMARK 18m",
783
      TSDB_CODE_PAR_INVALID_TABLE_OPTION);
X
Xiaoyu Wang 已提交
784 785
}

X
Xiaoyu Wang 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799
/*
 * CREATE STREAM [IF NOT EXISTS] stream_name [stream_options]
 *     INTO stb_name [TAGS (create_definition [, create_definition] ...)] [SUBTABLE (expr)] AS subquery
 *
 * stream_options:
 *     stream_option ...
 *
 * stream_option: {
 *     TRIGGER    [AT_ONCE | WINDOW_CLOSE | MAX_DELAY time]
 *   | WATERMARK   time
 *   | IGNORE EXPIRED value
 *   | FILL_HISTORY value
 * }
 */
800 801 802
TEST_F(ParserInitialCTest, createStream) {
  useDb("root", "test");

803 804 805 806 807 808 809
  SCMCreateStreamReq expect = {0};

  auto clearCreateStreamReq = [&]() {
    tFreeSCMCreateStreamReq(&expect);
    memset(&expect, 0, sizeof(SCMCreateStreamReq));
  };

810
  auto setCreateStreamReq = [&](const char* pStream, const char* pSrcDb, const char* pSql, const char* pDstStb,
X
Xiaoyu Wang 已提交
811
                                int8_t igExists = 0) {
812 813
    snprintf(expect.name, sizeof(expect.name), "0.%s", pStream);
    snprintf(expect.sourceDB, sizeof(expect.sourceDB), "0.%s", pSrcDb);
814
    snprintf(expect.targetStbFullName, sizeof(expect.targetStbFullName), "0.test.%s", pDstStb);
815
    expect.igExists = igExists;
816
    expect.sql = taosStrdup(pSql);
817 818
  };

X
Xiaoyu Wang 已提交
819
  auto setStreamOptions =
5
54liuyao 已提交
820
      [&](int8_t createStb = STREAM_CREATE_STABLE_TRUE, int8_t triggerType = STREAM_TRIGGER_WINDOW_CLOSE,
X
Xiaoyu Wang 已提交
821 822 823 824 825 826 827 828 829 830
          int64_t maxDelay = 0, int64_t watermark = 0, int8_t igExpired = STREAM_DEFAULT_IGNORE_EXPIRED,
          int8_t fillHistory = STREAM_DEFAULT_FILL_HISTORY, int8_t igUpdate = STREAM_DEFAULT_IGNORE_UPDATE) {
        expect.createStb = createStb;
        expect.triggerType = triggerType;
        expect.maxDelay = maxDelay;
        expect.watermark = watermark;
        expect.fillHistory = fillHistory;
        expect.igExpired = igExpired;
        expect.igUpdate = igUpdate;
      };
831

832 833 834 835 836 837 838 839 840 841 842 843 844 845
  auto addTag = [&](const char* pFieldName, uint8_t type, int32_t bytes = 0) {
    SField field = {0};
    strcpy(field.name, pFieldName);
    field.type = type;
    field.bytes = bytes > 0 ? bytes : tDataTypes[type].bytes;
    field.flags |= COL_SMA_ON;

    if (NULL == expect.pTags) {
      expect.pTags = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SField));
    }
    taosArrayPush(expect.pTags, &field);
    expect.numOfTags += 1;
  };

846 847 848 849 850 851 852 853 854 855 856 857 858 859
  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_STREAM_STMT);
    SCMCreateStreamReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS ==
                tDeserializeSCMCreateStreamReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.name), std::string(expect.name));
    ASSERT_EQ(std::string(req.sourceDB), std::string(expect.sourceDB));
    ASSERT_EQ(std::string(req.targetStbFullName), std::string(expect.targetStbFullName));
    ASSERT_EQ(req.igExists, expect.igExists);
    ASSERT_EQ(std::string(req.sql), std::string(expect.sql));
    ASSERT_EQ(req.triggerType, expect.triggerType);
    ASSERT_EQ(req.maxDelay, expect.maxDelay);
    ASSERT_EQ(req.watermark, expect.watermark);
860
    ASSERT_EQ(req.fillHistory, expect.fillHistory);
861
    ASSERT_EQ(req.igExpired, expect.igExpired);
862 863 864 865 866 867 868 869 870 871 872 873 874
    ASSERT_EQ(req.numOfTags, expect.numOfTags);
    if (expect.numOfTags > 0) {
      ASSERT_EQ(taosArrayGetSize(req.pTags), expect.numOfTags);
      ASSERT_EQ(taosArrayGetSize(req.pTags), taosArrayGetSize(expect.pTags));
      for (int32_t i = 0; i < expect.numOfTags; ++i) {
        SField* pField = (SField*)taosArrayGet(req.pTags, i);
        SField* pExpectField = (SField*)taosArrayGet(expect.pTags, i);
        ASSERT_EQ(std::string(pField->name), std::string(pExpectField->name));
        ASSERT_EQ(pField->type, pExpectField->type);
        ASSERT_EQ(pField->bytes, pExpectField->bytes);
        ASSERT_EQ(pField->flags, pExpectField->flags);
      }
    }
875 876
    ASSERT_EQ(req.checkpointFreq, expect.checkpointFreq);
    ASSERT_EQ(req.createStb, expect.createStb);
877
    ASSERT_EQ(req.igUpdate, expect.igUpdate);
878 879 880
    tFreeSCMCreateStreamReq(&req);
  });

881
  setCreateStreamReq("s1", "test", "create stream s1 into st3 as select count(*) from t1 interval(10s)", "st3");
5
54liuyao 已提交
882
  setStreamOptions();
883
  run("CREATE STREAM s1 INTO st3 AS SELECT COUNT(*) FROM t1 INTERVAL(10S)");
884
  clearCreateStreamReq();
885

886 887
  setCreateStreamReq(
      "s1", "test",
888
      "create stream if not exists s1 trigger max_delay 20s watermark 10s ignore expired 0 fill_history 1 ignore "
X
Xiaoyu Wang 已提交
889 890 891 892
      "update 1 into st3 as select count(*) from t1 interval(10s)",
      "st3", 1);
  setStreamOptions(STREAM_CREATE_STABLE_TRUE, STREAM_TRIGGER_MAX_DELAY, 20 * MILLISECOND_PER_SECOND,
                   10 * MILLISECOND_PER_SECOND, 0, 1, 1);
893
  run("CREATE STREAM IF NOT EXISTS s1 TRIGGER MAX_DELAY 20s WATERMARK 10s IGNORE EXPIRED 0 FILL_HISTORY 1 IGNORE "
X
Xiaoyu Wang 已提交
894
      "UPDATE 1 INTO st3 AS SELECT COUNT(*) FROM t1 INTERVAL(10S)");
895
  clearCreateStreamReq();
896

897 898 899 900
  setCreateStreamReq("s1", "test",
                     "create stream s1 into st3 tags(tname varchar(10), id int) subtable(concat('new-', tname)) as "
                     "select _wstart wstart, count(*) cnt from st1 partition by tbname tname, tag1 id interval(10s)",
                     "st3");
901 902
  addTag("tname", TSDB_DATA_TYPE_VARCHAR, 10 + VARSTR_HEADER_SIZE);
  addTag("id", TSDB_DATA_TYPE_INT);
5
54liuyao 已提交
903
  setStreamOptions();
904 905 906
  run("CREATE STREAM s1 INTO st3 TAGS(tname VARCHAR(10), id INT) SUBTABLE(CONCAT('new-', tname)) "
      "AS SELECT _WSTART wstart, COUNT(*) cnt FROM st1 PARTITION BY TBNAME tname, tag1 id INTERVAL(10S)");
  clearCreateStreamReq();
907

908 909 910 911
  // st1 already exists
  setCreateStreamReq(
      "s1", "test",
      "create stream s1 into st1 tags(tag2) as select max(c1), c2 from t1 partition by tbname tag2 interval(10s)",
X
Xiaoyu Wang 已提交
912 913
      "st1");
  setStreamOptions(STREAM_CREATE_STABLE_FALSE);
914
  run("CREATE STREAM s1 INTO st1 TAGS(tag2) AS SELECT MAX(c1), c2 FROM t1 PARTITION BY TBNAME tag2 INTERVAL(10S)");
915
  clearCreateStreamReq();
916 917
}

918 919 920
TEST_F(ParserInitialCTest, createStreamSemanticCheck) {
  useDb("root", "test");

921 922
  run("CREATE STREAM s1 INTO st1 AS SELECT PERCENTILE(c1, 30) FROM t1 INTERVAL(10S)",
      TSDB_CODE_PAR_STREAM_NOT_ALLOWED_FUNC);
S
shenglian zhou 已提交
923
  run("CREATE STREAM s2 INTO st1 AS SELECT ts, to_json('{c1:1}') FROM st1 PARTITION BY TBNAME",
924
      TSDB_CODE_PAR_INVALID_STREAM_QUERY);
925
  run("CREATE STREAM s3 INTO st3 TAGS(tname VARCHAR(10), id INT) SUBTABLE(CONCAT('new-', tbname)) "
926
      "AS SELECT _WSTART wstart, COUNT(*) cnt FROM st1 INTERVAL(10S)", TSDB_CODE_PAR_INVALID_STREAM_QUERY);
927 928
}

X
Xiaoyu Wang 已提交
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
/*
 * CREATE TABLE [IF NOT EXISTS] [db_name.]tb_name (create_definition [, create_definitionn] ...) [table_options]
 *
 * CREATE TABLE create_subtable_clause
 *
 * CREATE TABLE [IF NOT EXISTS] [db_name.]tb_name (create_definition [, create_definitionn] ...)
 *     [TAGS (create_definition [, create_definitionn] ...)]
 *     [table_options]
 *
 * create_subtable_clause: {
 *     create_subtable_clause [create_subtable_clause] ...
 *   | [IF NOT EXISTS] [db_name.]tb_name USING [db_name.]stb_name [(tag_name [, tag_name] ...)]
 *         TAGS (tag_value [, tag_value] ...)
 * }
 *
 * create_definition:
 *     col_name column_definition
 *
 * column_definition:
 *     type_name [comment 'string_value']
 *
 * table_options:
 *     table_option ...
 *
 * table_option: {
 *     COMMENT 'string_value'
 *   | WATERMARK duration[,duration]
 *   | MAX_DELAY duration[,duration]
 *   | ROLLUP(func_name [, func_name] ...)
 *   | SMA(col_name [, col_name] ...)
 *   | TTL value
 * }
 */
962 963 964
TEST_F(ParserInitialCTest, createTable) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
965 966 967 968 969
  SVCreateTbBatchReq expect = {0};

  auto addCreateTbReq = [&](const char* pName, bool ignoreExists = false, int32_t ttl = TSDB_DEFAULT_TABLE_TTL,
                            const char* pComment = nullptr) {
    SVCreateTbReq req = {0};
970
    req.name = taosStrdup(pName);
X
Xiaoyu Wang 已提交
971 972 973 974 975
    if (ignoreExists) {
      req.flags |= TD_CREATE_IF_NOT_EXISTS;
    }
    req.ttl = ttl;
    if (nullptr != pComment) {
976
      req.comment = taosStrdup(pComment);
X
Xiaoyu Wang 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
      req.commentLen = strlen(pComment);
    }
    ++expect.nReqs;
    if (nullptr == expect.pArray) {
      expect.pArray = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVCreateTbReq));
    }
    taosArrayPush(expect.pArray, &req);
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    return;  // todo
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_VNODE_MODIFY_STMT);
    SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pQuery->pRoot;

    ASSERT_EQ(pStmt->sqlNodeType, QUERY_NODE_CREATE_TABLE_STMT);
    ASSERT_NE(pStmt->pDataBlocks, nullptr);
    int32_t numOfBlocks = taosArrayGetSize(pStmt->pDataBlocks);
    for (int32_t i = 0; i < numOfBlocks; ++i) {
      SVgDataBlocks*     pVgData = (SVgDataBlocks*)taosArrayGetP(pStmt->pDataBlocks, i);
      void*              pBuf = POINTER_SHIFT(pVgData->pData, sizeof(SMsgHead));
      SVCreateTbBatchReq req = {0};
      SDecoder           coder = {0};
      tDecoderInit(&coder, (uint8_t*)pBuf, pVgData->size);
      ASSERT_EQ(tDecodeSVCreateTbBatchReq(&coder, &req), TSDB_CODE_SUCCESS);
      ASSERT_EQ(req.nReqs, expect.nReqs);
      for (int32_t j = 0; j < req.nReqs; ++j) {
        SVCreateTbReq* pReq = req.pReqs + j;
        SVCreateTbReq* pExpect = (SVCreateTbReq*)taosArrayGet(expect.pArray, j);
        ASSERT_EQ(pReq->flags, pExpect->flags);
        ASSERT_EQ(std::string(pReq->name), std::string(pExpect->name));
        ASSERT_EQ(pReq->uid, pExpect->uid);
        ASSERT_EQ(pReq->ctime, pExpect->ctime);
        ASSERT_EQ(pReq->ttl, pExpect->ttl);
        ASSERT_EQ(pReq->commentLen, pExpect->commentLen);
        ASSERT_EQ(std::string(pReq->comment), std::string(pExpect->comment));
        ASSERT_EQ(pReq->type, pExpect->type);
        if (TD_NORMAL_TABLE == pExpect->type) {
          ASSERT_EQ(pReq->ntb.schemaRow.version, pExpect->ntb.schemaRow.version);
          ASSERT_EQ(pReq->ntb.schemaRow.nCols, pExpect->ntb.schemaRow.nCols);
        } else if (TD_CHILD_TABLE == pExpect->type) {
          ASSERT_EQ(std::string(pReq->ctb.stbName), std::string(pExpect->ctb.stbName));
          ASSERT_EQ(pReq->ctb.tagNum, pExpect->ctb.tagNum);
          ASSERT_EQ(pReq->ctb.suid, pExpect->ctb.suid);
        }
      }
      tDecoderClear(&coder);
    }
  });

X
Xiaoyu Wang 已提交
1026
  run("CREATE TABLE t1(ts TIMESTAMP, c1 INT)");
1027

X
Xiaoyu Wang 已提交
1028 1029
  run("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), "
1030
      "c8 SMALLINT, c9 SMALLINT UNSIGNED, c10 TINYINT, c11 TINYINT UNSIGNED, c12 BOOL, "
X
Xiaoyu Wang 已提交
1031
      "c13 NCHAR(30), c15 VARCHAR(50)) "
1032 1033
      "TTL 100 COMMENT 'test create table' SMA(c1, c2, c3)");

1034
  run("CREATE TABLE IF NOT EXISTS rollup_db.t1("
X
Xiaoyu Wang 已提交
1035
      "ts TIMESTAMP, c1 INT, c2 INT UNSIGNED, c3 BIGINT, c4 BIGINT UNSIGNED, c5 FLOAT, c6 DOUBLE, c7 BINARY(20), "
1036
      "c8 SMALLINT, c9 SMALLINT UNSIGNED, c10 TINYINT, c11 TINYINT UNSIGNED, c12 BOOL, "
X
Xiaoyu Wang 已提交
1037 1038
      "c13 NCHAR(30), c14 VARCHAR(50)) "
      "TAGS (a1 TIMESTAMP, a2 INT, a3 INT UNSIGNED, a4 BIGINT, a5 BIGINT UNSIGNED, a6 FLOAT, a7 DOUBLE, a8 BINARY(20), "
1039
      "a9 SMALLINT, a10 SMALLINT UNSIGNED, a11 TINYINT, a12 TINYINT UNSIGNED, a13 BOOL, "
X
Xiaoyu Wang 已提交
1040
      "a14 NCHAR(30), a15 VARCHAR(50)) "
X
Xiaoyu Wang 已提交
1041
      "TTL 100 COMMENT 'test create table' SMA(c1, c2, c3) ROLLUP (MIN)");
X
Xiaoyu Wang 已提交
1042

X
Xiaoyu Wang 已提交
1043
  run("CREATE TABLE IF NOT EXISTS t1 USING st1 TAGS(1, 'wxy', NOW)");
X
Xiaoyu Wang 已提交
1044 1045 1046 1047 1048

  run("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') ");
X
Xiaoyu Wang 已提交
1049

X
Xiaoyu Wang 已提交
1050
  run("CREATE TABLE IF NOT EXISTS t1 USING st1 TAGS(1, 'wxy', NOW + 1S)");
1051 1052
}

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
TEST_F(ParserInitialCTest, createTableSemanticCheck) {
  useDb("root", "test");

  string sql = "CREATE TABLE st1(ts TIMESTAMP, ";
  for (int32_t i = 1; i < 4096; ++i) {
    if (i > 1) {
      sql.append(", ");
    }
    sql.append("c" + to_string(i) + " INT");
  }
  sql.append(") TAGS (t1 int)");

  run(sql, TSDB_CODE_PAR_TOO_MANY_COLUMNS);
}

X
Xiaoyu Wang 已提交
1068 1069 1070 1071 1072
/*
 * CREATE TOPIC [IF NOT EXISTS] topic_name AS subquery
 *
 * CREATE TOPIC [IF NOT EXISTS] topic_name [WITH META] AS {DATABASE db_name | STABLE stb_name }
 */
1073 1074 1075
TEST_F(ParserInitialCTest, createTopic) {
  useDb("root", "test");

1076 1077
  SCMCreateTopicReq expect = {0};

1078 1079
  auto clearCreateTopicReq = [&]() { memset(&expect, 0, sizeof(SCMCreateTopicReq)); };

X
Xiaoyu Wang 已提交
1080 1081
  auto setCreateTopicReq = [&](const char* pTopicName, int8_t igExists, const char* pSql, const char* pAst,
                               const char* pDbName = nullptr, const char* pTbname = nullptr, int8_t withMeta = 0) {
1082 1083 1084
    snprintf(expect.name, sizeof(expect.name), "0.%s", pTopicName);
    expect.igExists = igExists;
    expect.sql = (char*)pSql;
X
Xiaoyu Wang 已提交
1085
    expect.withMeta = withMeta;
1086 1087 1088 1089 1090 1091 1092 1093
    if (nullptr != pTbname) {
      expect.subType = TOPIC_SUB_TYPE__TABLE;
      snprintf(expect.subStbName, sizeof(expect.subStbName), "0.%s.%s", pDbName, pTbname);
    } else if (nullptr != pAst) {
      expect.subType = TOPIC_SUB_TYPE__COLUMN;
      expect.ast = (char*)pAst;
    } else {
      expect.subType = TOPIC_SUB_TYPE__DB;
L
Liu Jicong 已提交
1094
      snprintf(expect.subDbName, sizeof(expect.subDbName), "0.%s", pDbName);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    }
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_TOPIC_STMT);
    SCMCreateTopicReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS ==
                tDeserializeSCMCreateTopicReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.name), std::string(expect.name));
    ASSERT_EQ(req.igExists, expect.igExists);
    ASSERT_EQ(req.subType, expect.subType);
    ASSERT_EQ(std::string(req.sql), std::string(expect.sql));
X
Xiaoyu Wang 已提交
1108
    ASSERT_EQ(req.withMeta, expect.withMeta);
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    switch (expect.subType) {
      case TOPIC_SUB_TYPE__DB:
        ASSERT_EQ(std::string(req.subDbName), std::string(expect.subDbName));
        break;
      case TOPIC_SUB_TYPE__TABLE:
        ASSERT_EQ(std::string(req.subStbName), std::string(expect.subStbName));
        break;
      case TOPIC_SUB_TYPE__COLUMN:
        ASSERT_NE(req.ast, nullptr);
        break;
      default:
        ASSERT_TRUE(false);
    }
1122
    tFreeSCMCreateTopicReq(&req);
1123 1124
  });

X
Xiaoyu Wang 已提交
1125
  setCreateTopicReq("tp1", 0, "create topic tp1 as select * from t1", "ast");
X
Xiaoyu Wang 已提交
1126
  run("CREATE TOPIC tp1 AS SELECT * FROM t1");
1127
  clearCreateTopicReq();
1128

X
Xiaoyu Wang 已提交
1129
  setCreateTopicReq("tp1", 1, "create topic if not exists tp1 as select ts, ceil(c1) from t1", "ast");
1130
  run("CREATE TOPIC IF NOT EXISTS tp1 AS SELECT ts, CEIL(c1) FROM t1");
1131
  clearCreateTopicReq();
1132

X
Xiaoyu Wang 已提交
1133
  setCreateTopicReq("tp1", 0, "create topic tp1 as database test", nullptr, "test");
1134
  run("CREATE TOPIC tp1 AS DATABASE test");
1135
  clearCreateTopicReq();
1136

X
Xiaoyu Wang 已提交
1137
  setCreateTopicReq("tp1", 0, "create topic tp1 with meta as database test", nullptr, "test", nullptr, 1);
X
Xiaoyu Wang 已提交
1138 1139 1140
  run("CREATE TOPIC tp1 WITH META AS DATABASE test");
  clearCreateTopicReq();

X
Xiaoyu Wang 已提交
1141
  setCreateTopicReq("tp1", 1, "create topic if not exists tp1 as stable st1", nullptr, "test", "st1");
1142
  run("CREATE TOPIC IF NOT EXISTS tp1 AS STABLE st1");
1143
  clearCreateTopicReq();
X
Xiaoyu Wang 已提交
1144

X
Xiaoyu Wang 已提交
1145
  setCreateTopicReq("tp1", 1, "create topic if not exists tp1 with meta as stable st1", nullptr, "test", "st1", 1);
X
Xiaoyu Wang 已提交
1146 1147
  run("CREATE TOPIC IF NOT EXISTS tp1 WITH META AS STABLE st1");
  clearCreateTopicReq();
1148 1149
}

X
Xiaoyu Wang 已提交
1150 1151 1152
/*
 * CREATE USER use_name PASS password [SYSINFO value]
 */
1153 1154 1155
TEST_F(ParserInitialCTest, createUser) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  SCreateUserReq expect = {0};

  auto clearCreateUserReq = [&]() { memset(&expect, 0, sizeof(SCreateUserReq)); };

  auto setCreateUserReq = [&](const char* pUser, const char* pPass, int8_t sysInfo = 1) {
    strcpy(expect.user, pUser);
    strcpy(expect.pass, pPass);
    expect.createType = 0;
    expect.superUser = 0;
    expect.sysInfo = sysInfo;
    expect.enable = 1;
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_CREATE_USER_STMT);
    SCreateUserReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSCreateUserReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(req.createType, expect.createType);
    ASSERT_EQ(req.superUser, expect.superUser);
    ASSERT_EQ(req.sysInfo, expect.sysInfo);
    ASSERT_EQ(req.enable, expect.enable);
    ASSERT_EQ(std::string(req.user), std::string(expect.user));
    ASSERT_EQ(std::string(req.pass), std::string(expect.pass));
  });

  setCreateUserReq("wxy", "123456");
X
Xiaoyu Wang 已提交
1183
  run("CREATE USER wxy PASS '123456'");
X
Xiaoyu Wang 已提交
1184 1185 1186 1187 1188
  clearCreateUserReq();

  setCreateUserReq("wxy1", "a123456", 1);
  run("CREATE USER wxy1 PASS 'a123456' SYSINFO 1");
  clearCreateUserReq();
1189 1190 1191
}

}  // namespace ParserTest