dCDAstProcess.c 34.1 KB
Newer Older
H
Haojun Liao 已提交
1 2
#include "tmsg.h"
#include "tglobal.h"
3
#include "parserInt.h"
H
Haojun Liao 已提交
4 5 6
#include "ttime.h"
#include "astToMsg.h"
#include "astGenerator.h"
H
Haojun Liao 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "parserUtil.h"
#include "queryInfoUtil.h"

/* is contained in pFieldList or not */
static bool has(SArray* pFieldList, int32_t startIndex, const char* name) {
  size_t numOfCols = taosArrayGetSize(pFieldList);
  for (int32_t j = startIndex; j < numOfCols; ++j) {
    TAOS_FIELD* field = taosArrayGet(pFieldList, j);
    if (strncasecmp(name, field->name, sizeof(field->name) - 1) == 0) return true;
  }

  return false;
}

H
Haojun Liao 已提交
21
static int32_t setShowInfo(SShowInfo* pShowInfo, SParseContext* pCtx, void** output, int32_t* outputLen,
22
                           SEpSet* pEpSet, void** pExtension, SMsgBuf* pMsgBuf) {
H
Haojun Liao 已提交
23 24 25 26 27 28
  const char* msg1 = "invalid name";
  const char* msg2 = "wildcard string should be less than %d characters";
  const char* msg3 = "database name too long";
  const char* msg4 = "pattern is invalid";
  const char* msg5 = "database name is empty";
  const char* msg6 = "pattern string is empty";
H
Haojun Liao 已提交
29
  const char* msg7 = "db is not specified";
H
Haojun Liao 已提交
30 31 32 33
  /*
   * database prefix in pInfo->pMiscInfo->a[0]
   * wildcard in like clause in pInfo->pMiscInfo->a[1]
   */
H
Hongze Cheng 已提交
34
  int16_t showType = pShowInfo->showType;
H
Haojun Liao 已提交
35
  if (showType == TSDB_MGMT_TABLE_TABLE) {
36
    SArray* array = NULL;
H
Hongze Cheng 已提交
37
    SName   name = {0};
H
Haojun Liao 已提交
38 39 40 41 42 43 44 45 46 47 48

    if (pCtx->db == NULL && pShowInfo->prefix.n == 0) {
      return buildInvalidOperationMsg(pMsgBuf, msg7);
    }

    SVShowTablesReq* pShowReq = calloc(1, sizeof(SVShowTablesReq));
    if (pShowInfo->prefix.n > 0) {
      tNameSetDbName(&name, pCtx->acctId, pShowInfo->prefix.z, pShowInfo->prefix.n);
    } else {
      tNameSetDbName(&name, pCtx->acctId, pCtx->db, strlen(pCtx->db));
    }
49 50 51 52

    char dbFname[TSDB_DB_FNAME_LEN] = {0};
    tNameGetFullDbName(&name, dbFname);

53
    catalogGetDBVgroup(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, dbFname, false, &array);
54 55 56 57 58 59

    SVgroupInfo* info = taosArrayGet(array, 0);
    pShowReq->head.vgId = htonl(info->vgId);
    pEpSet->numOfEps = info->numOfEps;
    pEpSet->inUse = info->inUse;

H
Hongze Cheng 已提交
60
    for (int32_t i = 0; i < pEpSet->numOfEps; ++i) {
61 62 63 64
      strncpy(pEpSet->fqdn[i], info->epAddr[i].fqdn, tListLen(pEpSet->fqdn[i]));
      pEpSet->port[i] = info->epAddr[i].port;
    }

H
Haojun Liao 已提交
65 66
    *outputLen  = sizeof(SVShowTablesReq);
    *output     = pShowReq;
67
    *pExtension = array;
H
Haojun Liao 已提交
68 69 70 71 72 73 74
  } else {
    if (showType == TSDB_MGMT_TABLE_STB || showType == TSDB_MGMT_TABLE_VGROUP) {
      SToken* pDbPrefixToken = &pShowInfo->prefix;
      if (pDbPrefixToken->type != 0) {
        if (pDbPrefixToken->n >= TSDB_DB_NAME_LEN) {  // db name is too long
          return buildInvalidOperationMsg(pMsgBuf, msg3);
        }
H
Haojun Liao 已提交
75

H
Haojun Liao 已提交
76 77 78
        if (pDbPrefixToken->n <= 0) {
          return buildInvalidOperationMsg(pMsgBuf, msg5);
        }
H
Haojun Liao 已提交
79

H
Haojun Liao 已提交
80 81 82 83 84 85 86 87
        if (parserValidateIdToken(pDbPrefixToken) != TSDB_CODE_SUCCESS) {
          return buildInvalidOperationMsg(pMsgBuf, msg1);
        }

        //      int32_t ret = tNameSetDbName(&pTableMetaInfo->name, getAccountId(pRequest->pTsc), pDbPrefixToken);
        //      if (ret != TSDB_CODE_SUCCESS) {
        //        return buildInvalidOperationMsg(pMsgBuf, msg1);
        //      }
H
Haojun Liao 已提交
88 89
      }

H
Haojun Liao 已提交
90 91 92 93 94 95
      // show table/stable like 'xxxx', set the like pattern for show tables
      SToken* pPattern = &pShowInfo->pattern;
      if (pPattern->type != 0) {
        if (pPattern->type == TK_ID && pPattern->z[0] == TS_ESCAPE_CHAR) {
          return buildInvalidOperationMsg(pMsgBuf, msg4);
        }
H
Haojun Liao 已提交
96

H
Haojun Liao 已提交
97 98 99 100
        pPattern->n = strdequote(pPattern->z);
        if (pPattern->n <= 0) {
          return buildInvalidOperationMsg(pMsgBuf, msg6);
        }
H
Haojun Liao 已提交
101

H
Haojun Liao 已提交
102 103 104 105 106 107 108 109 110
        if (pPattern->n > tsMaxWildCardsLen) {
          char tmp[64] = {0};
          sprintf(tmp, msg2, tsMaxWildCardsLen);
          return buildInvalidOperationMsg(pMsgBuf, tmp);
        }
      }
    } else if (showType == TSDB_MGMT_TABLE_VNODES) {
      if (pShowInfo->prefix.type == 0) {
        return buildInvalidOperationMsg(pMsgBuf, "No specified dnode ep");
H
Haojun Liao 已提交
111 112
      }

H
Haojun Liao 已提交
113 114
      if (pShowInfo->prefix.type == TK_STRING) {
        pShowInfo->prefix.n = strdequote(pShowInfo->prefix.z);
H
Haojun Liao 已提交
115 116 117
      }
    }

H
Haojun Liao 已提交
118
    *pEpSet = pCtx->mgmtEpSet;
119 120 121 122 123
    *output = buildShowMsg(pShowInfo, pCtx, pMsgBuf);
    if (*output == NULL) {
      return terrno;
    }

S
Shengliang Guan 已提交
124
    *outputLen = sizeof(SShowReq) /* + htons(pShowMsg->payloadLen)*/;
H
Haojun Liao 已提交
125 126 127 128 129 130
  }

  return TSDB_CODE_SUCCESS;
}

// can only perform the parameters based on the macro definitation
S
Shengliang Guan 已提交
131
static int32_t doCheckDbOptions(SCreateDbReq* pCreate, SMsgBuf* pMsgBuf) {
H
Haojun Liao 已提交
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
  char msg[512] = {0};

  if (pCreate->walLevel != -1 && (pCreate->walLevel < TSDB_MIN_WAL_LEVEL || pCreate->walLevel > TSDB_MAX_WAL_LEVEL)) {
    snprintf(msg, tListLen(msg), "invalid db option walLevel: %d, only 1-2 allowed", pCreate->walLevel);
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  if (pCreate->replications != -1 &&
      (pCreate->replications < TSDB_MIN_DB_REPLICA_OPTION || pCreate->replications > TSDB_MAX_DB_REPLICA_OPTION)) {
    snprintf(msg, tListLen(msg), "invalid db option replications: %d valid range: [%d, %d]", pCreate->replications,
             TSDB_MIN_DB_REPLICA_OPTION, TSDB_MAX_DB_REPLICA_OPTION);
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  int32_t blocks = ntohl(pCreate->totalBlocks);
  if (blocks != -1 && (blocks < TSDB_MIN_TOTAL_BLOCKS || blocks > TSDB_MAX_TOTAL_BLOCKS)) {
    snprintf(msg, tListLen(msg), "invalid db option totalBlocks: %d valid range: [%d, %d]", blocks,
             TSDB_MIN_TOTAL_BLOCKS, TSDB_MAX_TOTAL_BLOCKS);
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  if (pCreate->quorum != -1 &&
      (pCreate->quorum < TSDB_MIN_DB_QUORUM_OPTION || pCreate->quorum > TSDB_MAX_DB_QUORUM_OPTION)) {
    snprintf(msg, tListLen(msg), "invalid db option quorum: %d valid range: [%d, %d]", pCreate->quorum,
             TSDB_MIN_DB_QUORUM_OPTION, TSDB_MAX_DB_QUORUM_OPTION);
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  int32_t val = htonl(pCreate->daysPerFile);
  if (val != -1 && (val < TSDB_MIN_DAYS_PER_FILE || val > TSDB_MAX_DAYS_PER_FILE)) {
H
Hongze Cheng 已提交
162 163
    snprintf(msg, tListLen(msg), "invalid db option daysPerFile: %d valid range: [%d, %d]", val, TSDB_MIN_DAYS_PER_FILE,
             TSDB_MAX_DAYS_PER_FILE);
H
Haojun Liao 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  val = htonl(pCreate->cacheBlockSize);
  if (val != -1 && (val < TSDB_MIN_CACHE_BLOCK_SIZE || val > TSDB_MAX_CACHE_BLOCK_SIZE)) {
    snprintf(msg, tListLen(msg), "invalid db option cacheBlockSize: %d valid range: [%d, %d]", val,
             TSDB_MIN_CACHE_BLOCK_SIZE, TSDB_MAX_CACHE_BLOCK_SIZE);
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO &&
      pCreate->precision != TSDB_TIME_PRECISION_NANO) {
    snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d, %d]", pCreate->precision,
             TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO, TSDB_TIME_PRECISION_NANO);
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  val = htonl(pCreate->commitTime);
  if (val != -1 && (val < TSDB_MIN_COMMIT_TIME || val > TSDB_MAX_COMMIT_TIME)) {
H
Hongze Cheng 已提交
183 184
    snprintf(msg, tListLen(msg), "invalid db option commitTime: %d valid range: [%d, %d]", val, TSDB_MIN_COMMIT_TIME,
             TSDB_MAX_COMMIT_TIME);
H
Haojun Liao 已提交
185 186 187 188 189
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  val = htonl(pCreate->fsyncPeriod);
  if (val != -1 && (val < TSDB_MIN_FSYNC_PERIOD || val > TSDB_MAX_FSYNC_PERIOD)) {
H
Hongze Cheng 已提交
190 191
    snprintf(msg, tListLen(msg), "invalid db option fsyncPeriod: %d valid range: [%d, %d]", val, TSDB_MIN_FSYNC_PERIOD,
             TSDB_MAX_FSYNC_PERIOD);
H
Haojun Liao 已提交
192 193 194 195 196 197 198 199 200 201
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

  if (pCreate->compression != -1 &&
      (pCreate->compression < TSDB_MIN_COMP_LEVEL || pCreate->compression > TSDB_MAX_COMP_LEVEL)) {
    snprintf(msg, tListLen(msg), "invalid db option compression: %d valid range: [%d, %d]", pCreate->compression,
             TSDB_MIN_COMP_LEVEL, TSDB_MAX_COMP_LEVEL);
    return buildInvalidOperationMsg(pMsgBuf, msg);
  }

H
Haojun Liao 已提交
202 203 204
  val = htonl(pCreate->numOfVgroups);
  if (val < TSDB_MIN_VNODES_PER_DB || val > TSDB_MAX_VNODES_PER_DB) {
    snprintf(msg, tListLen(msg), "invalid number of vgroups for DB:%d valid range: [%d, %d]", val,
H
Hongze Cheng 已提交
205
             TSDB_MIN_VNODES_PER_DB, TSDB_MAX_VNODES_PER_DB);
H
Haojun Liao 已提交
206 207
  }

H
Haojun Liao 已提交
208 209 210 211 212 213 214 215 216 217 218 219
  val = htonl(pCreate->maxRows);
  if (val < TSDB_MIN_MAX_ROW_FBLOCK || val > TSDB_MAX_MAX_ROW_FBLOCK) {
    snprintf(msg, tListLen(msg), "invalid number of max rows in file block for DB:%d valid range: [%d, %d]", val,
             TSDB_MIN_MAX_ROW_FBLOCK, TSDB_MAX_MAX_ROW_FBLOCK);
  }

  val = htonl(pCreate->minRows);
  if (val < TSDB_MIN_MIN_ROW_FBLOCK || val > TSDB_MAX_MIN_ROW_FBLOCK) {
    snprintf(msg, tListLen(msg), "invalid number of min rows in file block for DB:%d valid range: [%d, %d]", val,
             TSDB_MIN_MIN_ROW_FBLOCK, TSDB_MAX_MIN_ROW_FBLOCK);
  }

H
Haojun Liao 已提交
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
  return TSDB_CODE_SUCCESS;
}

static int32_t validateTableColumns(SArray* pFieldList, int32_t maxRowLength, int32_t maxColumns, SMsgBuf* pMsgBuf) {
  const char* msg2 = "row length exceeds max length";
  const char* msg3 = "duplicated column names";
  const char* msg4 = "invalid data type";
  const char* msg5 = "invalid binary/nchar column length";
  const char* msg6 = "invalid column name";
  const char* msg7 = "too many columns";
  const char* msg8 = "illegal number of columns";

  size_t numOfCols = taosArrayGetSize(pFieldList);
  if (numOfCols > maxColumns) {
    return buildInvalidOperationMsg(pMsgBuf, msg7);
  }

  int32_t rowLen = 0;
  for (int32_t i = 0; i < numOfCols; ++i) {
    TAOS_FIELD* pField = taosArrayGet(pFieldList, i);
    if (!isValidDataType(pField->type)) {
      return buildInvalidOperationMsg(pMsgBuf, msg4);
    }

    if (pField->bytes == 0) {
      return buildInvalidOperationMsg(pMsgBuf, msg5);
    }

    if ((pField->type == TSDB_DATA_TYPE_BINARY && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_BINARY_LEN)) ||
        (pField->type == TSDB_DATA_TYPE_NCHAR && (pField->bytes <= 0 || pField->bytes > TSDB_MAX_NCHAR_LEN))) {
      return buildInvalidOperationMsg(pMsgBuf, msg5);
    }

    SToken nameToken = {.z = pField->name, .n = strlen(pField->name), .type = TK_ID};
    if (parserValidateNameToken(&nameToken) != TSDB_CODE_SUCCESS) {
      return buildInvalidOperationMsg(pMsgBuf, msg6);
    }

    // field name must be unique
    if (has(pFieldList, i + 1, pField->name) == true) {
      return buildInvalidOperationMsg(pMsgBuf, msg3);
    }

    rowLen += pField->bytes;
  }

  // max row length must be less than TSDB_MAX_BYTES_PER_ROW
  if (rowLen > maxRowLength) {
    return buildInvalidOperationMsg(pMsgBuf, msg2);
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t validateTableColumnInfo(SArray* pFieldList, SMsgBuf* pMsgBuf) {
275
  assert(pFieldList != NULL && pMsgBuf != NULL);
H
Haojun Liao 已提交
276 277

  const char* msg1 = "first column must be timestamp";
278
  const char* msg2 = "illegal number of columns";
H
Haojun Liao 已提交
279 280 281 282 283 284 285 286 287 288

  // first column must be timestamp
  SField* pField = taosArrayGet(pFieldList, 0);
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
    return buildInvalidOperationMsg(pMsgBuf, msg1);
  }

  // number of fields no less than 2
  size_t numOfCols = taosArrayGetSize(pFieldList);
  if (numOfCols <= 1) {
289
    return buildInvalidOperationMsg(pMsgBuf, msg2);
H
Haojun Liao 已提交
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
  }

  return validateTableColumns(pFieldList, TSDB_MAX_BYTES_PER_ROW, TSDB_MAX_COLUMNS, pMsgBuf);
}

static int32_t validateTagParams(SArray* pTagsList, SArray* pFieldList, SMsgBuf* pMsgBuf) {
  assert(pTagsList != NULL);

  const char* msg1 = "invalid number of tag columns";
  const char* msg3 = "duplicated column names";

  // number of fields at least 1
  size_t numOfTags = taosArrayGetSize(pTagsList);
  if (numOfTags < 1) {
    return buildInvalidOperationMsg(pMsgBuf, msg1);
  }

  // field name must be unique
  for (int32_t i = 0; i < numOfTags; ++i) {
    SField* p = taosArrayGet(pTagsList, i);
    if (has(pFieldList, 0, p->name) == true) {
      return buildInvalidOperationMsg(pMsgBuf, msg3);
    }
  }

  return validateTableColumns(pFieldList, TSDB_MAX_TAGS_LEN, TSDB_MAX_TAGS, pMsgBuf);
}

318
int32_t doCheckForCreateTable(SCreateTableSql* pCreateTable, SMsgBuf* pMsgBuf) {
H
Haojun Liao 已提交
319 320 321 322 323 324 325
  const char* msg1 = "invalid table name";

  SArray* pFieldList = pCreateTable->colInfo.pColumns;
  SArray* pTagList = pCreateTable->colInfo.pTagColumns;
  assert(pFieldList != NULL);

  // if sql specifies db, use it, otherwise use default db
326
  SToken* pNameToken = &(pCreateTable->name);
H
Haojun Liao 已提交
327

328
  if (parserValidateIdToken(pNameToken) != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
329 330 331 332 333 334 335 336 337 338 339
    return buildInvalidOperationMsg(pMsgBuf, msg1);
  }

  if (validateTableColumnInfo(pFieldList, pMsgBuf) != TSDB_CODE_SUCCESS ||
      (pTagList != NULL && validateTagParams(pTagList, pFieldList, pMsgBuf) != TSDB_CODE_SUCCESS)) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

  return TSDB_CODE_SUCCESS;
}

340 341 342 343 344
typedef struct SVgroupTablesBatch {
  SVCreateTbBatchReq req;
  SVgroupInfo        info;
} SVgroupTablesBatch;

345 346
static SArray* doSerializeVgroupCreateTableInfo(SHashObj* pVgroupHashmap);

H
Haojun Liao 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
static int32_t doParseSerializeTagValue(SSchema* pTagSchema, int32_t numOfInputTag, SKVRowBuilder* pKvRowBuilder,
                                        SArray* pTagValList, int32_t tsPrecision, SMsgBuf* pMsgBuf) {
  const char* msg1 = "illegal value or data overflow";
  int32_t     code = TSDB_CODE_SUCCESS;

  for (int32_t i = 0; i < numOfInputTag; ++i) {
    SSchema* pSchema = &pTagSchema[i];

    char* endPtr = NULL;
    char  tmpTokenBuf[TSDB_MAX_TAGS_LEN] = {0};
    SKvParam param = {.builder = pKvRowBuilder, .schema = pSchema};

    SToken* pItem = taosArrayGet(pTagValList, i);
    code = parseValueToken(&endPtr, pItem, pSchema, tsPrecision, tmpTokenBuf, KvRowAppend, &param, pMsgBuf);

    if (code != TSDB_CODE_SUCCESS) {
      return buildInvalidOperationMsg(pMsgBuf, msg1);
    }
  }

  return code;
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
static void addCreateTbReqIntoVgroup(SHashObj* pVgroupHashmap, const SName* pTableName, SKVRow row, uint64_t suid, SVgroupInfo* pVgInfo) {
  struct SVCreateTbReq req = {0};
  req.type        = TD_CHILD_TABLE;
  req.name        = strdup(tNameGetTableName(pTableName));
  req.ctbCfg.suid = suid;
  req.ctbCfg.pTag = row;

  SVgroupTablesBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId));
  if (pTableBatch == NULL) {
    SVgroupTablesBatch tBatch = {0};
    tBatch.info = *pVgInfo;

    tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq));
    taosArrayPush(tBatch.req.pArray, &req);

    taosHashPut(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId), &tBatch, sizeof(tBatch));
  } else {  // add to the correct vgroup
    assert(pVgInfo->vgId == pTableBatch->info.vgId);
    taosArrayPush(pTableBatch->req.pArray, &req);
  }
}

static void destroyCreateTbReqBatch(SVgroupTablesBatch* pTbBatch) {
  size_t size = taosArrayGetSize(pTbBatch->req.pArray);
  for(int32_t i = 0; i < size; ++i) {
    SVCreateTbReq* pTableReq = taosArrayGet(pTbBatch->req.pArray, i);
    tfree(pTableReq->name);
397 398 399 400 401 402 403 404

    if (pTableReq->type == TSDB_NORMAL_TABLE) {
      tfree(pTableReq->ntbCfg.pSchema);
    } else if (pTableReq->type == TSDB_CHILD_TABLE) {
      tfree(pTableReq->ctbCfg.pTag);
    } else {
      assert(0);
    }
405 406 407 408 409
  }

  taosArrayDestroy(pTbBatch->req.pArray);
}

H
Haojun Liao 已提交
410
static int32_t doCheckAndBuildCreateCTableReq(SCreateTableSql* pCreateTable, SParseContext* pCtx, SMsgBuf* pMsgBuf, SArray** pBufArray) {
H
Haojun Liao 已提交
411 412 413 414 415
  const char* msg1 = "invalid table name";
  const char* msg2 = "tags number not matched";
  const char* msg3 = "tag value too long";
  const char* msg4 = "illegal value or data overflow";

416 417 418
  int32_t code = 0;
  STableMeta* pSuperTableMeta = NULL;

419 420
  SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);

H
Haojun Liao 已提交
421
  // super table name, create table by using dst
422
  size_t numOfTables = taosArrayGetSize(pCreateTable->childTableInfo);
H
Hongze Cheng 已提交
423
  for (int32_t j = 0; j < numOfTables; ++j) {
H
Haojun Liao 已提交
424 425 426
    SCreatedTableInfo* pCreateTableInfo = taosArrayGet(pCreateTable->childTableInfo, j);

    SToken* pSTableNameToken = &pCreateTableInfo->stbName;
427
    code = parserValidateNameToken(pSTableNameToken);
H
Haojun Liao 已提交
428
    if (code != TSDB_CODE_SUCCESS) {
429 430
      code = buildInvalidOperationMsg(pMsgBuf, msg1);
      goto _error;
H
Haojun Liao 已提交
431 432 433 434 435
    }

    SName name = {0};
    code = createSName(&name, pSTableNameToken, pCtx, pMsgBuf);
    if (code != TSDB_CODE_SUCCESS) {
436
      goto _error;
H
Haojun Liao 已提交
437 438
    }

H
Haojun Liao 已提交
439 440
    SKVRowBuilder kvRowBuilder = {0};
    if (tdInitKVRowBuilder(&kvRowBuilder) < 0) {
441 442
      code = TSDB_CODE_TSC_OUT_OF_MEMORY;
      goto _error;
H
Haojun Liao 已提交
443 444
    }

H
Haojun Liao 已提交
445
    SArray* pValList = pCreateTableInfo->pTagVals;
H
Haojun Liao 已提交
446
    size_t  numOfInputTag = taosArrayGetSize(pValList);
H
Haojun Liao 已提交
447

448 449
    code = catalogGetTableMeta(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, &name, &pSuperTableMeta);
    if (code != TSDB_CODE_SUCCESS) {
450
      goto _error;
451 452
    }

453
    assert(pSuperTableMeta != NULL);
454

H
Haojun Liao 已提交
455
    // too long tag values will return invalid sql, not be truncated automatically
H
Hongze Cheng 已提交
456
    SSchema*      pTagSchema = getTableTagSchema(pSuperTableMeta);
H
Haojun Liao 已提交
457 458 459
    STableComInfo tinfo = getTableInfo(pSuperTableMeta);

    SArray* pNameList = NULL;
H
Haojun Liao 已提交
460
    size_t  numOfBoundTags = 0;
H
Haojun Liao 已提交
461 462 463 464
    int32_t schemaSize = getNumOfTags(pSuperTableMeta);

    if (pCreateTableInfo->pTagNames) {
      pNameList = pCreateTableInfo->pTagNames;
H
Haojun Liao 已提交
465
      numOfBoundTags = taosArrayGetSize(pNameList);
H
Haojun Liao 已提交
466

H
Haojun Liao 已提交
467
      if (numOfInputTag != numOfBoundTags || schemaSize < numOfInputTag) {
H
Haojun Liao 已提交
468
        tdDestroyKVRowBuilder(&kvRowBuilder);
469 470
        code = buildInvalidOperationMsg(pMsgBuf, msg2);
        goto _error;
H
Haojun Liao 已提交
471 472 473
      }

      bool findColumnIndex = false;
H
Haojun Liao 已提交
474
      for (int32_t i = 0; i < numOfBoundTags; ++i) {
H
Haojun Liao 已提交
475 476
        SToken* sToken = taosArrayGet(pNameList, i);

H
Hongze Cheng 已提交
477
        char tmpTokenBuf[TSDB_MAX_BYTES_PER_ROW] = {0};  // create tmp buf to avoid alter orginal sqlstr
H
Haojun Liao 已提交
478 479 480
        strncpy(tmpTokenBuf, sToken->z, sToken->n);
        sToken->z = tmpTokenBuf;

H
Hongze Cheng 已提交
481 482 483
        //        if (TK_STRING == sToken->type) {
        //          tscDequoteAndTrimToken(sToken);
        //        }
H
Haojun Liao 已提交
484

H
Hongze Cheng 已提交
485 486 487
        //        if (TK_ID == sToken->type) {
        //          tscRmEscapeAndTrimToken(sToken);
        //        }
H
Haojun Liao 已提交
488

489
        SListItem* pItem = taosArrayGet(pValList, i);
H
Haojun Liao 已提交
490 491 492 493 494 495

        findColumnIndex = false;

        // todo speedup by using hash list
        for (int32_t t = 0; t < schemaSize; ++t) {
          if (strncmp(sToken->z, pTagSchema[t].name, sToken->n) == 0 && strlen(pTagSchema[t].name) == sToken->n) {
H
Hongze Cheng 已提交
496
            SSchema* pSchema = &pTagSchema[t];
H
Haojun Liao 已提交
497 498 499 500 501

            char tagVal[TSDB_MAX_TAGS_LEN] = {0};
            if (pSchema->type == TSDB_DATA_TYPE_BINARY || pSchema->type == TSDB_DATA_TYPE_NCHAR) {
              if (pItem->pVar.nLen > pSchema->bytes) {
                tdDestroyKVRowBuilder(&kvRowBuilder);
502 503
                code = buildInvalidOperationMsg(pMsgBuf, msg3);
                goto _error;
H
Haojun Liao 已提交
504 505 506
              }
            } else if (pSchema->type == TSDB_DATA_TYPE_TIMESTAMP) {
              if (pItem->pVar.nType == TSDB_DATA_TYPE_BINARY) {
H
Hongze Cheng 已提交
507 508 509 510
                //                code = convertTimestampStrToInt64(&(pItem->pVar), tinfo.precision);
                //                if (code != TSDB_CODE_SUCCESS) {
                //                  return buildInvalidOperationMsg(pMsgBuf, msg4);
                //                }
H
Haojun Liao 已提交
511
              } else if (pItem->pVar.nType == TSDB_DATA_TYPE_TIMESTAMP) {
512
                pItem->pVar.i = convertTimePrecision(pItem->pVar.i, TSDB_TIME_PRECISION_NANO, tinfo.precision);
H
Haojun Liao 已提交
513 514 515
              }
            }

516
            code = taosVariantDump(&(pItem->pVar), tagVal, pSchema->type, true);
H
Haojun Liao 已提交
517 518

            // check again after the convert since it may be converted from binary to nchar.
519
            if (IS_VAR_DATA_TYPE(pSchema->type)) {
H
Haojun Liao 已提交
520 521 522
              int16_t len = varDataTLen(tagVal);
              if (len > pSchema->bytes) {
                tdDestroyKVRowBuilder(&kvRowBuilder);
523 524
                code = buildInvalidOperationMsg(pMsgBuf, msg3);
                goto _error;
H
Haojun Liao 已提交
525 526 527 528 529
              }
            }

            if (code != TSDB_CODE_SUCCESS) {
              tdDestroyKVRowBuilder(&kvRowBuilder);
530 531
              code = buildInvalidOperationMsg(pMsgBuf, msg4);
              goto _error;
H
Haojun Liao 已提交
532 533 534 535 536 537 538 539 540 541 542
            }

            tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal);

            findColumnIndex = true;
            break;
          }
        }

        if (!findColumnIndex) {
          tdDestroyKVRowBuilder(&kvRowBuilder);
H
Hongze Cheng 已提交
543
          //          return buildInvalidOperationMsg(pMsgBuf, "invalid tag name", sToken->z);
H
Haojun Liao 已提交
544 545 546
        }
      }
    } else {
547
      if (schemaSize != numOfInputTag) {
H
Haojun Liao 已提交
548
        tdDestroyKVRowBuilder(&kvRowBuilder);
549 550
        code = buildInvalidOperationMsg(pMsgBuf, msg2);
        goto _error;
H
Haojun Liao 已提交
551 552
      }

H
Haojun Liao 已提交
553 554
      code = doParseSerializeTagValue(pTagSchema, numOfInputTag, &kvRowBuilder, pValList, tinfo.precision, pMsgBuf);
      if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
555
        tdDestroyKVRowBuilder(&kvRowBuilder);
556
        goto _error;
H
Haojun Liao 已提交
557 558 559 560 561 562
      }
    }

    SKVRow row = tdGetKVRowFromBuilder(&kvRowBuilder);
    tdDestroyKVRowBuilder(&kvRowBuilder);
    if (row == NULL) {
563 564
      code = TSDB_CODE_QRY_OUT_OF_MEMORY;
      goto _error;
H
Haojun Liao 已提交
565
    }
566

H
Haojun Liao 已提交
567 568
    tdSortKVRowByColIdx(row);

569 570 571
    SName tableName = {0};
    code = createSName(&tableName, &pCreateTableInfo->name, pCtx, pMsgBuf);
    if (code != TSDB_CODE_SUCCESS) {
572
      goto _error;
H
Haojun Liao 已提交
573 574
    }

575
    // Find a appropriate vgroup to accommodate this table , according to the table name
576
    SVgroupInfo info = {0};
577 578 579 580
    code = catalogGetTableHashVgroup(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, &tableName, &info);
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }
581

582 583 584
    addCreateTbReqIntoVgroup(pVgroupHashmap, &tableName, row, pSuperTableMeta->uid, &info);
    tfree(pSuperTableMeta);
  }
585

586
  *pBufArray = doSerializeVgroupCreateTableInfo(pVgroupHashmap);
587 588 589 590
  if (*pBufArray == NULL) {
    code = terrno;
    goto _error;
  }
591 592 593

  taosHashCleanup(pVgroupHashmap);
  return TSDB_CODE_SUCCESS;
594 595 596 597 598 599

  _error:
  taosHashCleanup(pVgroupHashmap);
  tfree(pSuperTableMeta);
  terrno = code;
  return code;
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
}

static int32_t serializeVgroupTablesBatchImpl(SVgroupTablesBatch* pTbBatch, SArray* pBufArray) {
  int   tlen = sizeof(SMsgHead) + tSVCreateTbBatchReqSerialize(NULL, &(pTbBatch->req));
  void* buf = malloc(tlen);
  if (buf == NULL) {
    // TODO: handle error
  }

  ((SMsgHead*)buf)->vgId = htonl(pTbBatch->info.vgId);
  ((SMsgHead*)buf)->contLen = htonl(tlen);

  void* pBuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
  tSVCreateTbBatchReqSerialize(&pBuf, &(pTbBatch->req));

  SVgDataBlocks* pVgData = calloc(1, sizeof(SVgDataBlocks));
  pVgData->vg    = pTbBatch->info;
  pVgData->pData = buf;
  pVgData->size  = tlen;
  pVgData->numOfTables = (int32_t) taosArrayGetSize(pTbBatch->req.pArray);

  taosArrayPush(pBufArray, &pVgData);
}

static int32_t doBuildSingleTableBatchReq(SName* pTableName, SArray* pColumns, SVgroupInfo* pVgroupInfo, SVgroupTablesBatch* pBatch) {
  struct SVCreateTbReq req = {0};
  req.type = TD_NORMAL_TABLE;
  req.name = strdup(tNameGetTableName(pTableName));

  req.ntbCfg.nCols = taosArrayGetSize(pColumns);
  int32_t num = req.ntbCfg.nCols;

  req.ntbCfg.pSchema = calloc(num, sizeof(SSchema));
  for(int32_t i = 0; i < num; ++i) {
    SSchema* pSchema = taosArrayGet(pColumns, i);
    memcpy(&req.ntbCfg.pSchema[i], pSchema, sizeof(SSchema));
  }

  pBatch->info = *pVgroupInfo;
  pBatch->req.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
  if (pBatch->req.pArray == NULL) {
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }

  taosArrayPush(pBatch->req.pArray, &req);
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
648
int32_t doCheckAndBuildCreateTableReq(SCreateTableSql* pCreateTable, SParseContext* pCtx, SMsgBuf* pMsgBuf, char** pOutput, int32_t* len) {
649
  SArray* pBufArray = NULL;
H
Haojun Liao 已提交
650
  int32_t code = 0;
651 652 653 654

  // it is a sql statement to create a normal table
  if (pCreateTable->childTableInfo == NULL) {
    assert(taosArrayGetSize(pCreateTable->colInfo.pColumns) > 0 && pCreateTable->colInfo.pTagColumns == NULL);
H
Haojun Liao 已提交
655
    code = doCheckForCreateTable(pCreateTable, pMsgBuf);
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    SName tableName = {0};
    code = createSName(&tableName, &pCreateTable->name, pCtx, pMsgBuf);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    SVgroupInfo info = {0};
    catalogGetTableHashVgroup(pCtx->pCatalog, pCtx->pTransporter, &pCtx->mgmtEpSet, &tableName, &info);

    SVgroupTablesBatch tbatch = {0};
    code = doBuildSingleTableBatchReq(&tableName, pCreateTable->colInfo.pColumns, &info, &tbatch);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    pBufArray = taosArrayInit(1, POINTER_BYTES);
    if (pBufArray == NULL) {
      return TSDB_CODE_QRY_OUT_OF_MEMORY;
    }

    serializeVgroupTablesBatchImpl(&tbatch, pBufArray);
    destroyCreateTbReqBatch(&tbatch);
  } else { // it is a child table, created according to a super table
H
Haojun Liao 已提交
683
    code = doCheckAndBuildCreateCTableReq(pCreateTable, pCtx, pMsgBuf, &pBufArray);
684
    if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
685 686
      return code;
    }
687
  }
688

689 690 691
  SVnodeModifOpStmtInfo* pStmtInfo = calloc(1, sizeof(SVnodeModifOpStmtInfo));
  pStmtInfo->nodeType    = TSDB_SQL_CREATE_TABLE;
  pStmtInfo->pDataBlocks = pBufArray;
692

693 694
  *pOutput = (char*) pStmtInfo;
  *len     = sizeof(SVnodeModifOpStmtInfo);
H
Haojun Liao 已提交
695

696 697
  return TSDB_CODE_SUCCESS;
}
H
Haojun Liao 已提交
698

699
SArray* doSerializeVgroupCreateTableInfo(SHashObj* pVgroupHashmap) {
700 701 702
  SArray* pBufArray = taosArrayInit(taosHashGetSize(pVgroupHashmap), sizeof(void*));

  SVgroupTablesBatch* pTbBatch = NULL;
H
Hongze Cheng 已提交
703
  do {
704
    pTbBatch = taosHashIterate(pVgroupHashmap, pTbBatch);
705 706 707
    if (pTbBatch == NULL) {
      break;
    }
H
Hongze Cheng 已提交
708

709
    /*int32_t code = */serializeVgroupTablesBatchImpl(pTbBatch, pBufArray);
710
    destroyCreateTbReqBatch(pTbBatch);
H
Hongze Cheng 已提交
711
  } while (true);
712

713
  return pBufArray;
H
Haojun Liao 已提交
714 715
}

H
Haojun Liao 已提交
716
SDclStmtInfo* qParserValidateDclSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, char* msgBuf, int32_t msgBufLen) {
H
Haojun Liao 已提交
717 718
  int32_t code = 0;

719 720
  SDclStmtInfo* pDcl = calloc(1, sizeof(SDclStmtInfo));

H
Hongze Cheng 已提交
721 722
  SMsgBuf  m = {.buf = msgBuf, .len = msgBufLen};
  SMsgBuf* pMsgBuf = &m;
H
Haojun Liao 已提交
723

H
Haojun Liao 已提交
724 725
  pDcl->epSet = pCtx->mgmtEpSet;

H
Haojun Liao 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738
  switch (pInfo->type) {
    case TSDB_SQL_CREATE_USER:
    case TSDB_SQL_ALTER_USER: {
      const char* msg1 = "not support options";
      const char* msg2 = "invalid user/account name";
      const char* msg3 = "name too long";
      const char* msg4 = "invalid user rights";

      SUserInfo* pUser = &pInfo->pMiscInfo->user;
      SToken*    pName = &pUser->user;
      SToken*    pPwd = &pUser->passwd;

      if (pName->n >= TSDB_USER_LEN) {
739 740
        code = buildInvalidOperationMsg(pMsgBuf, msg3);
        goto _error;
H
Haojun Liao 已提交
741 742 743
      }

      if (parserValidateIdToken(pName) != TSDB_CODE_SUCCESS) {
744 745
        code = buildInvalidOperationMsg(pMsgBuf, msg2);
        goto _error;
H
Haojun Liao 已提交
746 747 748 749
      }

      if (pInfo->type == TSDB_SQL_CREATE_USER) {
        if (parserValidatePassword(pPwd, pMsgBuf) != TSDB_CODE_SUCCESS) {
750 751
          code = TSDB_CODE_TSC_INVALID_OPERATION;
          goto _error;
H
Haojun Liao 已提交
752 753 754 755
        }
      } else {
        if (pUser->type == TSDB_ALTER_USER_PASSWD) {
          if (parserValidatePassword(pPwd, pMsgBuf) != TSDB_CODE_SUCCESS) {
756 757
            code = TSDB_CODE_TSC_INVALID_OPERATION;
            goto _error;
H
Haojun Liao 已提交
758 759 760 761 762 763 764 765 766 767
          }
        } else if (pUser->type == TSDB_ALTER_USER_PRIVILEGES) {
          assert(pPwd->type == TSDB_DATA_TYPE_NULL);

          SToken* pPrivilege = &pUser->privilege;
          if (strncasecmp(pPrivilege->z, "super", 5) == 0 && pPrivilege->n == 5) {
            //            pCmd->count = 1;
          } else if (strncasecmp(pPrivilege->z, "normal", 4) == 0 && pPrivilege->n == 4) {
            //            pCmd->count = 2;
          } else {
768 769
            code = buildInvalidOperationMsg(pMsgBuf, msg4);
            goto _error;
H
Haojun Liao 已提交
770 771
          }
        } else {
772 773
          code = buildInvalidOperationMsg(pMsgBuf, msg1);
          goto _error;
H
Haojun Liao 已提交
774 775 776 777
        }
      }

      pDcl->pMsg = (char*)buildUserManipulationMsg(pInfo, &pDcl->msgLen, pCtx->requestId, msgBuf, msgBufLen);
H
Hongze Cheng 已提交
778
      pDcl->msgType = (pInfo->type == TSDB_SQL_CREATE_USER) ? TDMT_MND_CREATE_USER : TDMT_MND_ALTER_USER;
H
Haojun Liao 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791
      break;
    }

    case TSDB_SQL_CREATE_ACCT:
    case TSDB_SQL_ALTER_ACCT: {
      const char* msg1 = "invalid state option, available options[no, r, w, all]";
      const char* msg2 = "invalid user/account name";
      const char* msg3 = "name too long";

      SToken* pName = &pInfo->pMiscInfo->user.user;
      SToken* pPwd = &pInfo->pMiscInfo->user.passwd;

      if (parserValidatePassword(pPwd, pMsgBuf) != TSDB_CODE_SUCCESS) {
792 793
        code = TSDB_CODE_TSC_INVALID_OPERATION;
        goto _error;
H
Haojun Liao 已提交
794 795 796
      }

      if (pName->n >= TSDB_USER_LEN) {
797 798
        code = buildInvalidOperationMsg(pMsgBuf, msg3);
        goto _error;
H
Haojun Liao 已提交
799 800 801
      }

      if (parserValidateNameToken(pName) != TSDB_CODE_SUCCESS) {
802 803
        code = buildInvalidOperationMsg(pMsgBuf, msg2);
        goto _error;
H
Haojun Liao 已提交
804 805 806 807 808 809 810 811 812
      }

      SCreateAcctInfo* pAcctOpt = &pInfo->pMiscInfo->acctOpt;
      if (pAcctOpt->stat.n > 0) {
        if (pAcctOpt->stat.z[0] == 'r' && pAcctOpt->stat.n == 1) {
        } else if (pAcctOpt->stat.z[0] == 'w' && pAcctOpt->stat.n == 1) {
        } else if (strncmp(pAcctOpt->stat.z, "all", 3) == 0 && pAcctOpt->stat.n == 3) {
        } else if (strncmp(pAcctOpt->stat.z, "no", 2) == 0 && pAcctOpt->stat.n == 2) {
        } else {
813 814
          code = buildInvalidOperationMsg(pMsgBuf, msg1);
          goto _error;
H
Haojun Liao 已提交
815 816 817 818
        }
      }

      pDcl->pMsg = (char*)buildAcctManipulationMsg(pInfo, &pDcl->msgLen, pCtx->requestId, msgBuf, msgBufLen);
H
Hongze Cheng 已提交
819
      pDcl->msgType = (pInfo->type == TSDB_SQL_CREATE_ACCT) ? TDMT_MND_CREATE_ACCT : TDMT_MND_ALTER_ACCT;
H
Haojun Liao 已提交
820 821 822 823 824 825
      break;
    }

    case TSDB_SQL_DROP_ACCT:
    case TSDB_SQL_DROP_USER: {
      pDcl->pMsg = (char*)buildDropUserMsg(pInfo, &pDcl->msgLen, pCtx->requestId, msgBuf, msgBufLen);
H
Hongze Cheng 已提交
826
      pDcl->msgType = (pInfo->type == TSDB_SQL_DROP_ACCT) ? TDMT_MND_DROP_ACCT : TDMT_MND_DROP_USER;
H
Haojun Liao 已提交
827 828 829 830
      break;
    }

    case TSDB_SQL_SHOW: {
H
Haojun Liao 已提交
831
      SShowInfo* pShowInfo = &pInfo->pMiscInfo->showOpt;
832
      code = setShowInfo(pShowInfo, pCtx, (void**)&pDcl->pMsg, &pDcl->msgLen, &pDcl->epSet, &pDcl->pExtension, pMsgBuf);
833 834 835 836
      if (code != TSDB_CODE_SUCCESS) {
        goto _error;
      }
      
H
Hongze Cheng 已提交
837
      pDcl->msgType = (pShowInfo->showType == TSDB_MGMT_TABLE_TABLE) ? TDMT_VND_SHOW_TABLES : TDMT_MND_SHOW;
H
Haojun Liao 已提交
838 839 840 841 842 843 844 845
      break;
    }

    case TSDB_SQL_USE_DB: {
      const char* msg = "invalid db name";

      SToken* pToken = taosArrayGet(pInfo->pMiscInfo->a, 0);
      if (parserValidateNameToken(pToken) != TSDB_CODE_SUCCESS) {
846 847
        code = buildInvalidOperationMsg(pMsgBuf, msg);
        goto _error;
H
Haojun Liao 已提交
848 849
      }

H
Hongze Cheng 已提交
850
      SName   n = {0};
H
Haojun Liao 已提交
851 852
      int32_t ret = tNameSetDbName(&n, pCtx->acctId, pToken->z, pToken->n);
      if (ret != TSDB_CODE_SUCCESS) {
853 854
        code = buildInvalidOperationMsg(pMsgBuf, msg);
        goto _error;
H
Haojun Liao 已提交
855 856
      }

S
Shengliang Guan 已提交
857
      SUseDbReq* pUseDbMsg = (SUseDbReq*)calloc(1, sizeof(SUseDbReq));
H
Haojun Liao 已提交
858 859 860
      tNameExtractFullName(&n, pUseDbMsg->db);

      pDcl->pMsg = (char*)pUseDbMsg;
S
Shengliang Guan 已提交
861
      pDcl->msgLen = sizeof(SUseDbReq);
H
Haojun Liao 已提交
862 863 864 865 866 867 868 869 870 871 872
      pDcl->msgType = TDMT_MND_USE_DB;
      break;
    }

    case TSDB_SQL_ALTER_DB:
    case TSDB_SQL_CREATE_DB: {
      const char* msg1 = "invalid db name";
      const char* msg2 = "name too long";

      SCreateDbInfo* pCreateDB = &(pInfo->pMiscInfo->dbOpt);
      if (pCreateDB->dbname.n >= TSDB_DB_NAME_LEN) {
873 874
        code = buildInvalidOperationMsg(pMsgBuf, msg2);
        goto _error;
H
Haojun Liao 已提交
875 876
      }

H
Hongze Cheng 已提交
877
      char   buf[TSDB_DB_NAME_LEN] = {0};
H
Haojun Liao 已提交
878 879 880
      SToken token = taosTokenDup(&pCreateDB->dbname, buf, tListLen(buf));

      if (parserValidateNameToken(&token) != TSDB_CODE_SUCCESS) {
881 882
        code = buildInvalidOperationMsg(pMsgBuf, msg1);
        goto _error;
H
Haojun Liao 已提交
883 884
      }

S
Shengliang Guan 已提交
885
      SCreateDbReq* pCreateMsg = buildCreateDbMsg(pCreateDB, pCtx, pMsgBuf);
H
Haojun Liao 已提交
886
      if (doCheckDbOptions(pCreateMsg, pMsgBuf) != TSDB_CODE_SUCCESS) {
887 888
        code = TSDB_CODE_TSC_INVALID_OPERATION;
        goto _error;
H
Haojun Liao 已提交
889 890 891
      }

      pDcl->pMsg = (char*)pCreateMsg;
S
Shengliang Guan 已提交
892
      pDcl->msgLen = sizeof(SCreateDbReq);
H
Hongze Cheng 已提交
893
      pDcl->msgType = (pInfo->type == TSDB_SQL_CREATE_DB) ? TDMT_MND_CREATE_DB : TDMT_MND_ALTER_DB;
H
Haojun Liao 已提交
894 895 896 897 898 899 900 901 902 903 904 905
      break;
    }

    case TSDB_SQL_DROP_DB: {
      const char* msg1 = "invalid database name";

      assert(taosArrayGetSize(pInfo->pMiscInfo->a) == 1);
      SToken* dbName = taosArrayGet(pInfo->pMiscInfo->a, 0);

      SName name = {0};
      code = tNameSetDbName(&name, pCtx->acctId, dbName->z, dbName->n);
      if (code != TSDB_CODE_SUCCESS) {
906 907
        code = buildInvalidOperationMsg(pMsgBuf, msg1);
        goto _error;
H
Haojun Liao 已提交
908 909
      }

S
Shengliang Guan 已提交
910
      SDropDbReq* pDropDbMsg = (SDropDbReq*)calloc(1, sizeof(SDropDbReq));
H
Haojun Liao 已提交
911 912 913 914 915 916

      code = tNameExtractFullName(&name, pDropDbMsg->db);
      pDropDbMsg->ignoreNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0;
      assert(code == TSDB_CODE_SUCCESS && name.type == TSDB_DB_NAME_T);

      pDcl->msgType = TDMT_MND_DROP_DB;
S
Shengliang Guan 已提交
917
      pDcl->msgLen = sizeof(SDropDbReq);
H
Haojun Liao 已提交
918
      pDcl->pMsg = (char*)pDropDbMsg;
919
      break;
H
Haojun Liao 已提交
920 921
    }

H
Haojun Liao 已提交
922
    case TSDB_SQL_CREATE_STABLE: {
H
Haojun Liao 已提交
923
      SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo;
924 925 926
      if ((code = doCheckForCreateTable(pCreateTable, pMsgBuf)) != TSDB_CODE_SUCCESS) {
        terrno = code;
        goto _error;
H
Haojun Liao 已提交
927 928
      }

929 930
      pDcl->pMsg = (char*)buildCreateStbMsg(pCreateTable, &pDcl->msgLen, pCtx, pMsgBuf);
      pDcl->msgType = TDMT_MND_CREATE_STB;
H
Haojun Liao 已提交
931 932 933 934
      break;
    }

    case TSDB_SQL_DROP_TABLE: {
H
Haojun Liao 已提交
935
      pDcl->pMsg = (char*)buildDropStableMsg(pInfo, &pDcl->msgLen, pCtx, pMsgBuf);
H
Haojun Liao 已提交
936
      if (pDcl->pMsg == NULL) {
937
        goto _error;
H
Haojun Liao 已提交
938 939 940
      }

      pDcl->msgType = TDMT_MND_DROP_STB;
941 942 943 944
      break;
    }

    case TSDB_SQL_CREATE_DNODE: {
H
Hongze Cheng 已提交
945
      pDcl->pMsg = (char*)buildCreateDnodeMsg(pInfo, &pDcl->msgLen, pMsgBuf);
946
      if (pDcl->pMsg == NULL) {
947
        goto _error;
948 949 950 951 952 953 954
      }

      pDcl->msgType = TDMT_MND_CREATE_DNODE;
      break;
    }

    case TSDB_SQL_DROP_DNODE: {
H
Hongze Cheng 已提交
955
      pDcl->pMsg = (char*)buildDropDnodeMsg(pInfo, &pDcl->msgLen, pMsgBuf);
956
      if (pDcl->pMsg == NULL) {
957
        goto _error;
958 959 960
      }

      pDcl->msgType = TDMT_MND_DROP_DNODE;
H
Haojun Liao 已提交
961 962 963 964 965 966 967
      break;
    }

    default:
      break;
  }

968 969 970 971 972 973
  return pDcl;

  _error:
    terrno = code;
    tfree(pDcl);
    return NULL;
H
Haojun Liao 已提交
974
}
975

H
Haojun Liao 已提交
976
SVnodeModifOpStmtInfo* qParserValidateCreateTbSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, char* msgBuf, int32_t msgBufLen) {
977
  SCreateTableSql* pCreateTable = pInfo->pCreateTableInfo;
978
  assert(pCreateTable->type == TSDB_SQL_CREATE_TABLE);
979 980 981 982

  SMsgBuf  m = {.buf = msgBuf, .len = msgBufLen};
  SMsgBuf* pMsgBuf = &m;

983
  SVnodeModifOpStmtInfo* pModifSqlStmt = NULL;
984 985

  int32_t msgLen = 0;
986
  int32_t code = doCheckAndBuildCreateTableReq(pCreateTable, pCtx, pMsgBuf, (char**) &pModifSqlStmt, &msgLen);
987
  if (code != TSDB_CODE_SUCCESS) {
988
    terrno = code;
989
    tfree(pModifSqlStmt);
990 991 992
    return NULL;
  }

993
  return pModifSqlStmt;
994
}