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

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndStb.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndDnode.h"
S
Shengliang Guan 已提交
20
#include "mndInfoSchema.h"
S
Shengliang Guan 已提交
21
#include "mndMnode.h"
H
Hongze Cheng 已提交
22
#include "mndPerfSchema.h"
23
#include "mndPrivilege.h"
C
Cary Xu 已提交
24
#include "mndScheduler.h"
S
Shengliang Guan 已提交
25
#include "mndShow.h"
26
#include "mndSma.h"
27
#include "mndTopic.h"
S
Shengliang Guan 已提交
28 29
#include "mndTrans.h"
#include "mndUser.h"
30
#include "mndVgroup.h"
S
Shengliang Guan 已提交
31
#include "tname.h"
S
Shengliang Guan 已提交
32

S
Shengliang Guan 已提交
33 34
#define STB_VER_NUMBER   1
#define STB_RESERVE_SIZE 64
S
Shengliang Guan 已提交
35 36 37 38

static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw);
static int32_t  mndStbActionInsert(SSdb *pSdb, SStbObj *pStb);
static int32_t  mndStbActionDelete(SSdb *pSdb, SStbObj *pStb);
S
Shengliang Guan 已提交
39
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
S
Shengliang Guan 已提交
40
static int32_t  mndProcessTtlTimer(SRpcMsg *pReq);
41 42 43
static int32_t  mndProcessCreateStbReq(SRpcMsg *pReq);
static int32_t  mndProcessAlterStbReq(SRpcMsg *pReq);
static int32_t  mndProcessDropStbReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
44 45
static int32_t  mndProcessTableMetaReq(SRpcMsg *pReq);
static int32_t  mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
46
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);
D
dapan1121 已提交
47
static int32_t  mndProcessTableCfgReq(SRpcMsg *pReq);
48 49 50
static int32_t  mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                               void *alterOriData, int32_t alterOriDataLen);
static int32_t  mndCheckColAndTagModifiable(SMnode *pMnode, const char *stbname, int64_t suid, col_id_t colId);
S
Shengliang Guan 已提交
51 52

int32_t mndInitStb(SMnode *pMnode) {
S
Shengliang Guan 已提交
53 54 55 56 57 58 59 60 61
  SSdbTable table = {
      .sdbType = SDB_STB,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndStbActionEncode,
      .decodeFp = (SdbDecodeFp)mndStbActionDecode,
      .insertFp = (SdbInsertFp)mndStbActionInsert,
      .updateFp = (SdbUpdateFp)mndStbActionUpdate,
      .deleteFp = (SdbDeleteFp)mndStbActionDelete,
  };
S
Shengliang Guan 已提交
62

63 64 65
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessCreateStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessAlterStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessDropStbReq);
66 67 68
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndTransProcessRsp);
D
dapan1121 已提交
69
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_META, mndProcessTableMetaReq);
S
Shengliang Guan 已提交
70
  mndSetMsgHandle(pMnode, TDMT_MND_TTL_TIMER, mndProcessTtlTimer);
D
dapan1121 已提交
71
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_CFG, mndProcessTableCfgReq);
S
Shengliang Guan 已提交
72 73 74

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STB, mndRetrieveStb);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STB, mndCancelGetNextStb);
S
Shengliang Guan 已提交
75 76

  return sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
77 78
}

S
Shengliang Guan 已提交
79
void mndCleanupStb(SMnode *pMnode) {}
S
Shengliang Guan 已提交
80

81
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
82 83
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
84
  int32_t size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags) * sizeof(SSchema) + pStb->commentLen +
D
dapan1121 已提交
85
                 pStb->ast1Len + pStb->ast2Len + STB_RESERVE_SIZE + taosArrayGetSize(pStb->pFuncs) * TSDB_FUNC_NAME_LEN;
S
Shengliang Guan 已提交
86
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STB, STB_VER_NUMBER, size);
87
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
88 89

  int32_t dataPos = 0;
90 91 92 93 94 95
  SDB_SET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->updateTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->uid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->dbUid, _OVER)
96 97
  SDB_SET_INT32(pRaw, dataPos, pStb->tagVer, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->colVer, _OVER)
S
Shengliang Guan 已提交
98
  SDB_SET_INT32(pRaw, dataPos, pStb->smaVer, _OVER)
99
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, _OVER)
100 101 102 103
  SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[0], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[1], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->watermark[0], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->watermark[1], _OVER)
104 105 106
  SDB_SET_INT32(pRaw, dataPos, pStb->ttl, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, _OVER)
S
Shengliang Guan 已提交
107
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfFuncs, _OVER)
108
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, _OVER)
109 110
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
111

S
Shengliang Guan 已提交
112 113
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
114
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
115
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
116 117 118
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
119 120 121 122
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
123
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
124
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
125 126 127
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
128 129
  }

S
Shengliang Guan 已提交
130 131 132 133 134
  for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
    char *func = taosArrayGet(pStb->pFuncs, i);
    SDB_SET_BINARY(pRaw, dataPos, func, TSDB_FUNC_NAME_LEN, _OVER)
  }

S
sma  
Shengliang Guan 已提交
135
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
136
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
137
  }
S
Shengliang Guan 已提交
138

139 140 141
  if (pStb->ast1Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
S
Shengliang Guan 已提交
142

143 144 145
  if (pStb->ast2Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147
  SDB_SET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
148
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
149 150 151

  terrno = 0;

152
_OVER:
153 154 155 156 157
  if (terrno != 0) {
    mError("stb:%s, failed to encode to raw:%p since %s", pStb->name, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
158

159
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
160 161 162
  return pRaw;
}

S
Shengliang Guan 已提交
163
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
164 165
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
166
  int8_t sver = 0;
167
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169
  if (sver != STB_VER_NUMBER) {
S
Shengliang Guan 已提交
170
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
171
    goto _OVER;
S
Shengliang Guan 已提交
172 173
  }

S
Shengliang 已提交
174
  SSdbRow *pRow = sdbAllocRow(sizeof(SStbObj));
175
  if (pRow == NULL) goto _OVER;
176

S
Shengliang Guan 已提交
177
  SStbObj *pStb = sdbGetRowObj(pRow);
178
  if (pStb == NULL) goto _OVER;
S
Shengliang Guan 已提交
179 180

  int32_t dataPos = 0;
181 182 183 184 185 186
  SDB_GET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->updateTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->uid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->dbUid, _OVER)
187 188
  SDB_GET_INT32(pRaw, dataPos, &pStb->tagVer, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->colVer, _OVER)
S
Shengliang Guan 已提交
189
  SDB_GET_INT32(pRaw, dataPos, &pStb->smaVer, _OVER)
190
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
191 192 193 194
  SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[0], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[1], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[0], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[1], _OVER)
195 196 197
  SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, _OVER)
S
Shengliang Guan 已提交
198
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfFuncs, _OVER)
199
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, _OVER)
200 201
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
202

wafwerar's avatar
wafwerar 已提交
203 204
  pStb->pColumns = taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = taosMemoryCalloc(pStb->numOfTags, sizeof(SSchema));
S
Shengliang Guan 已提交
205
  pStb->pFuncs = taosArrayInit(pStb->numOfFuncs, TSDB_FUNC_NAME_LEN);
S
Shengliang Guan 已提交
206
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pFuncs == NULL) {
207
    goto _OVER;
S
Shengliang 已提交
208
  }
S
Shengliang Guan 已提交
209

S
Shengliang Guan 已提交
210 211
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
212
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
213
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
214 215 216
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
217 218 219 220
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
221
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
222
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
223 224 225
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
226 227
  }

S
Shengliang Guan 已提交
228 229 230 231 232 233
  for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
    char funcName[TSDB_FUNC_NAME_LEN] = {0};
    SDB_GET_BINARY(pRaw, dataPos, funcName, TSDB_FUNC_NAME_LEN, _OVER)
    taosArrayPush(pStb->pFuncs, funcName);
  }

S
sma  
Shengliang Guan 已提交
234
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
235
    pStb->comment = taosMemoryCalloc(pStb->commentLen + 1, 1);
236
    if (pStb->comment == NULL) goto _OVER;
wmmhello's avatar
wmmhello 已提交
237
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
238
  }
S
Shengliang Guan 已提交
239

240 241 242 243 244
  if (pStb->ast1Len > 0) {
    pStb->pAst1 = taosMemoryCalloc(pStb->ast1Len, 1);
    if (pStb->pAst1 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
S
Shengliang Guan 已提交
245

246 247 248 249 250
  if (pStb->ast2Len > 0) {
    pStb->pAst2 = taosMemoryCalloc(pStb->ast2Len, 1);
    if (pStb->pAst2 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
S
Shengliang Guan 已提交
251
  SDB_GET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
252 253 254

  terrno = 0;

255
_OVER:
256 257
  if (terrno != 0) {
    mError("stb:%s, failed to decode from raw:%p since %s", pStb->name, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
258 259 260 261
    taosMemoryFreeClear(pStb->pColumns);
    taosMemoryFreeClear(pStb->pTags);
    taosMemoryFreeClear(pStb->comment);
    taosMemoryFreeClear(pRow);
262 263
    return NULL;
  }
S
Shengliang Guan 已提交
264

265
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
266 267 268
  return pRow;
}

L
Liu Jicong 已提交
269 270 271 272 273 274 275 276 277
void mndFreeStb(SStbObj *pStb) {
  taosArrayDestroy(pStb->pFuncs);
  taosMemoryFreeClear(pStb->pColumns);
  taosMemoryFreeClear(pStb->pTags);
  taosMemoryFreeClear(pStb->comment);
  taosMemoryFreeClear(pStb->pAst1);
  taosMemoryFreeClear(pStb->pAst2);
}

S
Shengliang Guan 已提交
278
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
279
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
280 281 282
  return 0;
}

S
Shengliang Guan 已提交
283
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
284
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
L
Liu Jicong 已提交
285
  mndFreeStb(pStb);
S
Shengliang Guan 已提交
286 287 288
  return 0;
}

S
Shengliang Guan 已提交
289 290
static int32_t mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew) {
  mTrace("stb:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
S
Shengliang Guan 已提交
291

S
Shengliang Guan 已提交
292
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
293 294

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
295
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
296
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
297
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
298
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
299 300 301 302 303 304 305 306
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->numOfTags < pNew->numOfTags) {
wafwerar's avatar
wafwerar 已提交
307
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
308
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
309
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
310
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
311 312 313 314
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
315
    }
S
Shengliang Guan 已提交
316 317
  }

318
  if (pOld->commentLen < pNew->commentLen && pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
319
    void *comment = taosMemoryMalloc(pNew->commentLen + 1);
S
Shengliang Guan 已提交
320
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
321
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
322 323 324 325 326 327 328
      pOld->comment = comment;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }
S
Shengliang Guan 已提交
329
  pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
330

331
  if (pOld->ast1Len < pNew->ast1Len) {
S
Shengliang Guan 已提交
332
    void *pAst1 = taosMemoryMalloc(pNew->ast1Len + 1);
333 334 335 336 337 338 339 340 341 342 343
    if (pAst1 != NULL) {
      taosMemoryFree(pOld->pAst1);
      pOld->pAst1 = pAst1;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->ast2Len < pNew->ast2Len) {
S
Shengliang Guan 已提交
344
    void *pAst2 = taosMemoryMalloc(pNew->ast2Len + 1);
345 346 347 348 349 350 351 352 353 354
    if (pAst2 != NULL) {
      taosMemoryFree(pOld->pAst2);
      pOld->pAst2 = pAst2;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

S
Shengliang Guan 已提交
355
  pOld->updateTime = pNew->updateTime;
356 357
  pOld->tagVer = pNew->tagVer;
  pOld->colVer = pNew->colVer;
358
  pOld->smaVer = pNew->smaVer;
359
  pOld->nextColId = pNew->nextColId;
S
Shengliang 已提交
360
  pOld->ttl = pNew->ttl;
361 362 363 364 365 366 367 368
  if (pNew->numOfColumns > 0) {
    pOld->numOfColumns = pNew->numOfColumns;
    memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  }
  if (pNew->numOfTags > 0) {
    pOld->numOfTags = pNew->numOfTags;
    memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
  }
369
  if (pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
370
    memcpy(pOld->comment, pNew->comment, pNew->commentLen + 1);
S
Shengliang Guan 已提交
371
    pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
372
  }
373 374
  if (pNew->ast1Len != 0) {
    memcpy(pOld->pAst1, pNew->pAst1, pNew->ast1Len);
S
Shengliang Guan 已提交
375
    pOld->ast1Len = pNew->ast1Len;
376 377 378
  }
  if (pNew->ast2Len != 0) {
    memcpy(pOld->pAst2, pNew->pAst2, pNew->ast2Len);
S
Shengliang Guan 已提交
379
    pOld->ast2Len = pNew->ast2Len;
380
  }
S
Shengliang Guan 已提交
381
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
382 383 384
  return 0;
}

S
Shengliang Guan 已提交
385
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
386
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
387
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
388
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
389 390 391
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
392 393
}

S
Shengliang Guan 已提交
394
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
395 396 397 398
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

399
SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
400 401
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
402

S
Shengliang Guan 已提交
403 404
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
405

S
Shengliang Guan 已提交
406 407
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
408

409
static FORCE_INLINE int32_t schemaExColIdCompare(const void *colId, const void *pSchema) {
H
Hongze Cheng 已提交
410
  if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
411
    return -1;
H
Hongze Cheng 已提交
412
  } else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
413 414 415 416 417
    return 1;
  }
  return 0;
}

418 419
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen,
                                   void *alterOriData, int32_t alterOriDataLen) {
H
Hongze Cheng 已提交
420
  SEncoder       encoder = {0};
H
Hongze Cheng 已提交
421 422 423
  int32_t        contLen;
  SName          name = {0};
  SVCreateStbReq req = {0};
H
Hongze Cheng 已提交
424

S
Shengliang Guan 已提交
425
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
X
Xiaoyu Wang 已提交
426 427
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFName);
428

S
Shengliang Guan 已提交
429
  req.name = (char *)tNameGetTableName(&name);
H
Hongze Cheng 已提交
430
  req.suid = pStb->uid;
431
  req.rollup = pStb->ast1Len > 0 ? 1 : 0;
wmmhello's avatar
wmmhello 已提交
432 433
  req.alterOriData = alterOriData;
  req.alterOriDataLen = alterOriDataLen;
434 435
  // todo
  req.schemaRow.nCols = pStb->numOfColumns;
436
  req.schemaRow.version = pStb->colVer;
437
  req.schemaRow.pSchema = pStb->pColumns;
H
Hongze Cheng 已提交
438
  req.schemaTag.nCols = pStb->numOfTags;
439
  req.schemaTag.version = pStb->tagVer;
H
Hongze Cheng 已提交
440
  req.schemaTag.pSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
441

C
Cary Xu 已提交
442
  if (req.rollup) {
C
Cary Xu 已提交
443 444
    req.rsmaParam.maxdelay[0] = pStb->maxdelay[0];
    req.rsmaParam.maxdelay[1] = pStb->maxdelay[1];
C
Cary Xu 已提交
445 446
    req.rsmaParam.watermark[0] = pStb->watermark[0];
    req.rsmaParam.watermark[1] = pStb->watermark[1];
C
Cary Xu 已提交
447
    if (pStb->ast1Len > 0) {
C
Cary Xu 已提交
448 449
      if (mndConvertRsmaTask(&req.rsmaParam.qmsg[0], &req.rsmaParam.qmsgLen[0], pStb->pAst1, pStb->uid,
                             STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[0]) < 0) {
450
        goto _err;
C
Cary Xu 已提交
451
      }
C
Cary Xu 已提交
452 453
    }
    if (pStb->ast2Len > 0) {
C
Cary Xu 已提交
454 455
      if (mndConvertRsmaTask(&req.rsmaParam.qmsg[1], &req.rsmaParam.qmsgLen[1], pStb->pAst2, pStb->uid,
                             STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[1]) < 0) {
456
        goto _err;
C
Cary Xu 已提交
457
      }
C
Cary Xu 已提交
458
    }
H
Hongze Cheng 已提交
459
  }
H
Hongze Cheng 已提交
460
  // get length
wafwerar's avatar
wafwerar 已提交
461 462 463
  int32_t ret = 0;
  tEncodeSize(tEncodeSVCreateStbReq, &req, contLen, ret);
  if (ret < 0) {
464
    goto _err;
H
Hongze Cheng 已提交
465 466
  }

H
Hongze Cheng 已提交
467
  contLen += sizeof(SMsgHead);
C
Cary Xu 已提交
468

wafwerar's avatar
wafwerar 已提交
469
  SMsgHead *pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
470
  if (pHead == NULL) {
H
more  
Hongze Cheng 已提交
471
    terrno = TSDB_CODE_OUT_OF_MEMORY;
472
    goto _err;
H
more  
Hongze Cheng 已提交
473 474
  }

S
Shengliang Guan 已提交
475 476
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
477

S
Shengliang Guan 已提交
478
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
479 480
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  if (tEncodeSVCreateStbReq(&encoder, &req) < 0) {
481 482
    taosMemoryFreeClear(pHead);
    tEncoderClear(&encoder);
483
    goto _err;
H
Hongze Cheng 已提交
484
  }
H
Hongze Cheng 已提交
485
  tEncoderClear(&encoder);
H
more  
Hongze Cheng 已提交
486

S
Shengliang Guan 已提交
487
  *pContLen = contLen;
C
Cary Xu 已提交
488 489
  taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
  taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
S
Shengliang Guan 已提交
490
  return pHead;
491
_err:
C
Cary Xu 已提交
492 493
  taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
  taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
494
  return NULL;
495 496
}

S
Shengliang Guan 已提交
497
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
498 499 500 501 502
  SName        name = {0};
  SVDropStbReq req = {0};
  int32_t      contLen = 0;
  int32_t      ret = 0;
  SMsgHead    *pHead = NULL;
H
Hongze Cheng 已提交
503
  SEncoder     encoder = {0};
H
Hongze Cheng 已提交
504

S
Shengliang Guan 已提交
505 506 507 508
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  req.name = (char *)tNameGetTableName(&name);
  req.suid = pStb->uid;
509

H
Hongze Cheng 已提交
510 511 512 513 514
  tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
  if (ret < 0) return NULL;

  contLen += sizeof(SMsgHead);
  pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
515
  if (pHead == NULL) {
516 517 518 519
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
520 521 522 523
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
524

H
Hongze Cheng 已提交
525 526 527
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  tEncodeSVDropStbReq(&encoder, &req);
  tEncoderClear(&encoder);
528

S
Shengliang Guan 已提交
529 530
  *pContLen = contLen;
  return pHead;
531 532
}

533
int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
534
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
535
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
536 537
    return -1;
  }
S
Shengliang Guan 已提交
538

S
Shengliang Guan 已提交
539
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
5
54liuyao 已提交
540
    terrno = TSDB_CODE_PAR_INVALID_COLUMNS_NUM;
S
Shengliang Guan 已提交
541 542
    return -1;
  }
S
Shengliang Guan 已提交
543

S
Shengliang Guan 已提交
544
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
545
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
546 547
    return -1;
  }
S
Shengliang Guan 已提交
548

S
Shengliang Guan 已提交
549
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
550
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
5
54liuyao 已提交
551
    terrno = TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
S
Shengliang Guan 已提交
552 553 554
    return -1;
  }

S
Shengliang Guan 已提交
555
  for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
556
    SField *pField1 = taosArrayGet(pCreate->pColumns, i);
S
Shengliang Guan 已提交
557
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
558 559 560
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
561
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
562 563 564
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
565
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
566 567 568 569 570 571
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
  }

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
572 573
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
    if (pField1->type < 0) {
S
Shengliang Guan 已提交
574
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
575 576
      return -1;
    }
577
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
578
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
579 580
      return -1;
    }
581
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
582
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
583 584 585
      return -1;
    }
  }
S
Shengliang Guan 已提交
586

S
Shengliang Guan 已提交
587 588 589
  return 0;
}

590
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
591 592
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
593 594 595 596
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
597 598 599 600 601
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;

  return 0;
}

602
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
603 604
  SSdbRaw *pUndoRaw = mndStbActionEncode(pStb);
  if (pUndoRaw == NULL) return -1;
S
Shengliang Guan 已提交
605 606 607 608
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
    sdbFreeRaw(pUndoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
609 610 611 612 613
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

614
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
615 616
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
617 618 619 620
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
621 622 623 624 625
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

626
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
627
  SSdb   *pSdb = pMnode->pSdb;
628
  SVgObj *pVgroup = NULL;
629
  void   *pIter = NULL;
S
Shengliang Guan 已提交
630
  int32_t contLen;
631 632 633 634

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
635
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
636 637 638 639
      sdbRelease(pSdb, pVgroup);
      continue;
    }

wmmhello's avatar
wmmhello 已提交
640
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, NULL, 0);
S
Shengliang Guan 已提交
641
    if (pReq == NULL) {
642 643 644 645
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
646

647 648
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
649
    action.pCont = pReq;
S
Shengliang Guan 已提交
650
    action.contLen = contLen;
H
Hongze Cheng 已提交
651
    action.msgType = TDMT_VND_CREATE_STB;
S
Shengliang Guan 已提交
652
    action.acceptableCode = TSDB_CODE_TDB_STB_ALREADY_EXIST;
653
    action.retryCode = TSDB_CODE_TDB_STB_NOT_EXIST;
654
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
655
      taosMemoryFree(pReq);
656 657 658 659 660 661
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
662 663 664 665

  return 0;
}

666
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
667
  SSdb   *pSdb = pMnode->pSdb;
668
  SVgObj *pVgroup = NULL;
669
  void   *pIter = NULL;
670 671 672 673

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
674
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
675 676 677 678
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
679
    int32_t contLen = 0;
S
Shengliang Guan 已提交
680
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
681
    if (pReq == NULL) {
682 683 684 685 686
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
687

688 689
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
690
    action.pCont = pReq;
S
Shengliang Guan 已提交
691
    action.contLen = contLen;
H
Hongze Cheng 已提交
692
    action.msgType = TDMT_VND_DROP_STB;
S
Shengliang Guan 已提交
693
    action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
694
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
695
      taosMemoryFree(pReq);
696 697 698 699 700 701
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
702 703 704 705

  return 0;
}

S
sma  
Shengliang Guan 已提交
706
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
707
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
708
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
709
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
710 711 712 713 714 715
      return pSchema;
    }
  }
  return NULL;
}

716 717 718 719 720
int32_t mndBuildStbFromReq(SMnode *pMnode, SStbObj *pDst, SMCreateStbReq *pCreate, SDbObj *pDb) {
  memcpy(pDst->name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(pDst->db, pDb->name, TSDB_DB_FNAME_LEN);
  pDst->createdTime = taosGetTimestampMs();
  pDst->updateTime = pDst->createdTime;
721 722
  pDst->uid =
      (pCreate->source == TD_REQ_FROM_TAOX) ? pCreate->suid : mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
723
  pDst->dbUid = pDb->uid;
724 725
  pDst->tagVer = 1;
  pDst->colVer = 1;
S
Shengliang Guan 已提交
726
  pDst->smaVer = 1;
727
  pDst->nextColId = 1;
728 729 730 731
  pDst->maxdelay[0] = pCreate->delay1;
  pDst->maxdelay[1] = pCreate->delay2;
  pDst->watermark[0] = pCreate->watermark1;
  pDst->watermark[1] = pCreate->watermark2;
732 733 734
  pDst->ttl = pCreate->ttl;
  pDst->numOfColumns = pCreate->numOfColumns;
  pDst->numOfTags = pCreate->numOfTags;
S
Shengliang Guan 已提交
735
  pDst->numOfFuncs = pCreate->numOfFuncs;
736
  pDst->commentLen = pCreate->commentLen;
D
dapan1121 已提交
737 738
  pDst->pFuncs = pCreate->pFuncs;
  pCreate->pFuncs = NULL;
739

740
  if (pDst->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
741
    pDst->comment = taosMemoryCalloc(pDst->commentLen + 1, 1);
742
    if (pDst->comment == NULL) {
S
sma  
Shengliang Guan 已提交
743 744 745
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
746
    memcpy(pDst->comment, pCreate->pComment, pDst->commentLen + 1);
S
sma  
Shengliang Guan 已提交
747
  }
S
Shengliang Guan 已提交
748

749 750 751 752
  pDst->ast1Len = pCreate->ast1Len;
  if (pDst->ast1Len > 0) {
    pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
    if (pDst->pAst1 == NULL) {
753 754 755
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
756
    memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
757 758
  }

759 760 761 762
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
763 764 765
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
766
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
767 768
  }

769 770 771
  pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema));
  pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema));
  if (pDst->pColumns == NULL || pDst->pTags == NULL) {
S
Shengliang Guan 已提交
772 773 774
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
775

776
  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
S
Shengliang Guan 已提交
777
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
778
    SSchema *pSchema = &pDst->pColumns[i];
S
Shengliang Guan 已提交
779 780
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
781
    pSchema->flags = pField->flags;
S
Shengliang Guan 已提交
782
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
783 784
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
785 786
  }

787
  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
S
Shengliang Guan 已提交
788
    SField  *pField = taosArrayGet(pCreate->pTags, i);
789
    SSchema *pSchema = &pDst->pTags[i];
S
Shengliang Guan 已提交
790 791 792
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
793 794
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
795
  }
796 797 798
  return 0;
}

S
Shengliang Guan 已提交
799
static int32_t mndCreateStb(SMnode *pMnode, SRpcMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
800
  SStbObj stbObj = {0};
S
Shengliang 已提交
801
  int32_t code = -1;
802

803
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq);
804
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
805

S
Shengliang Guan 已提交
806
  mDebug("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
807
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
808
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;
809
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
810 811
  code = 0;

812
_OVER:
S
Shengliang Guan 已提交
813
  mndTransDrop(pTrans);
814
  mndStbActionDelete(pMnode->pSdb, &stbObj);
S
Shengliang Guan 已提交
815
  return code;
S
Shengliang Guan 已提交
816
}
S
Shengliang Guan 已提交
817

818
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
819
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
820 821 822 823 824 825 826
  if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
  if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
  return 0;
}
S
Shengliang Guan 已提交
827

S
Shengliang Guan 已提交
828
static int32_t mndProcessTtlTimer(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
829 830 831 832 833 834 835
  SMnode           *pMnode = pReq->info.node;
  SSdb             *pSdb = pMnode->pSdb;
  SVgObj           *pVgroup = NULL;
  void             *pIter = NULL;
  SVDropTtlTableReq ttlReq = {.timestamp = taosGetTimestampSec()};
  int32_t           reqLen = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
  int32_t           contLen = reqLen + sizeof(SMsgHead);
wmmhello's avatar
wmmhello 已提交
836 837 838 839 840

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
841
    SMsgHead *pHead = rpcMallocCont(contLen);
wmmhello's avatar
wmmhello 已提交
842
    if (pHead == NULL) {
S
Shengliang Guan 已提交
843
      sdbCancelFetch(pSdb, pVgroup);
wmmhello's avatar
wmmhello 已提交
844 845 846 847 848
      sdbRelease(pSdb, pVgroup);
      continue;
    }
    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(pVgroup->vgId);
S
Shengliang Guan 已提交
849
    tSerializeSVDropTtlTableReq((char *)pHead + sizeof(SMsgHead), contLen, &ttlReq);
wmmhello's avatar
wmmhello 已提交
850 851

    SRpcMsg rpcMsg = {.msgType = TDMT_VND_DROP_TTL_TABLE, .pCont = pHead, .contLen = contLen};
S
Shengliang Guan 已提交
852
    SEpSet  epSet = mndGetVgroupEpset(pMnode, pVgroup);
wmmhello's avatar
wmmhello 已提交
853
    int32_t code = tmsgSendReq(&epSet, &rpcMsg);
S
Shengliang Guan 已提交
854
    if (code != 0) {
S
Shengliang Guan 已提交
855
      mError("vgId:%d, failed to send drop ttl table request to vnode since 0x%x", pVgroup->vgId, code);
S
Shengliang Guan 已提交
856
    } else {
S
Shengliang Guan 已提交
857
      mDebug("vgId:%d, send drop ttl table request to vnode, time:%d", pVgroup->vgId, ttlReq.timestamp);
wmmhello's avatar
wmmhello 已提交
858 859 860
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
861

wmmhello's avatar
wmmhello 已提交
862 863 864
  return 0;
}

wmmhello's avatar
wmmhello 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
    if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) {
      return tag;
    }
  }

  return -1;
}

static int32_t mndFindSuperTableColumnIndex(const SStbObj *pStb, const char *colName) {
  for (int32_t col = 0; col < pStb->numOfColumns; col++) {
    if (strcasecmp(pStb->pColumns[col].name, colName) == 0) {
      return col;
    }
  }

  return -1;
}

static int32_t mndBuildStbFromAlter(SStbObj *pStb, SStbObj *pDst, SMCreateStbReq *createReq) {
  taosRLockLatch(&pStb->lock);
  memcpy(pDst, pStb, sizeof(SStbObj));
  taosRUnLockLatch(&pStb->lock);

  pDst->updateTime = taosGetTimestampMs();
  pDst->numOfColumns = createReq->numOfColumns;
  pDst->numOfTags = createReq->numOfTags;
  pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema));
  pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema));
  if (pDst->pColumns == NULL || pDst->pTags == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
    SField  *pField = taosArrayGet(createReq->pColumns, i);
    SSchema *pSchema = &pDst->pColumns[i];
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    pSchema->flags = pField->flags;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
    int32_t cIndex = mndFindSuperTableColumnIndex(pStb, pField->name);
908
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
909
      pSchema->colId = pStb->pColumns[cIndex].colId;
910
    } else {
wmmhello's avatar
wmmhello 已提交
911 912 913 914 915 916 917 918 919 920 921
      pSchema->colId = pDst->nextColId++;
    }
  }

  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
    SField  *pField = taosArrayGet(createReq->pTags, i);
    SSchema *pSchema = &pDst->pTags[i];
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
    int32_t cIndex = mndFindSuperTableTagIndex(pStb, pField->name);
922
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
923
      pSchema->colId = pStb->pTags[cIndex].colId;
924
    } else {
wmmhello's avatar
wmmhello 已提交
925 926 927 928 929 930 931 932
      pSchema->colId = pDst->nextColId++;
    }
  }
  pDst->tagVer = createReq->tagVer;
  pDst->colVer = createReq->colVer;
  return TSDB_CODE_SUCCESS;
}

933
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
934
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
935 936 937 938
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
  SMCreateStbReq createReq = {0};
939
  bool           isAlter = false;
S
Shengliang Guan 已提交
940

S
Shengliang Guan 已提交
941
  if (tDeserializeSMCreateStbReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
942
    terrno = TSDB_CODE_INVALID_MSG;
943
    goto _OVER;
S
Shengliang Guan 已提交
944
  }
S
Shengliang Guan 已提交
945

S
Shengliang Guan 已提交
946
  mDebug("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
947 948
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
949
    goto _OVER;
S
Shengliang Guan 已提交
950
  }
S
Shengliang Guan 已提交
951

S
Shengliang Guan 已提交
952
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
953
  if (pStb != NULL) {
S
Shengliang Guan 已提交
954
    if (createReq.igExists) {
955 956 957 958 959 960 961 962 963 964
      if (createReq.source == TD_REQ_FROM_APP) {
        mDebug("stb:%s, already exist, ignore exist is set", createReq.name);
        code = 0;
        goto _OVER;
      } else if (pStb->uid != createReq.suid) {
        mError("stb:%s, already exist while create, input suid:%" PRId64 " not match with exist suid:%" PRId64,
               createReq.name, createReq.suid, pStb->uid);
        terrno = TSDB_CODE_MND_STABLE_UID_NOT_MATCH;
        goto _OVER;
      } else if (createReq.tagVer > 0 || createReq.colVer > 0) {
wmmhello's avatar
wmmhello 已提交
965 966 967
        int32_t tagDelta = createReq.tagVer - pStb->tagVer;
        int32_t colDelta = createReq.colVer - pStb->colVer;
        int32_t verDelta = tagDelta + colDelta;
968 969 970 971 972 973 974
        mInfo("stb:%s, already exist while create, input tagVer:%d colVer:%d, exist tagVer:%d colVer:%d",
              createReq.name, createReq.tagVer, createReq.colVer, pStb->tagVer, pStb->colVer);
        if (tagDelta <= 0 && colDelta <= 0) {
          mInfo("stb:%s, schema version is not incremented and nothing needs to be done", createReq.name);
          code = 0;
          goto _OVER;
        } else if ((tagDelta == 1 || colDelta == 1) && (verDelta == 1)) {
975
          isAlter = true;
976 977 978 979 980 981 982 983 984 985 986 987
          mInfo("stb:%s, schema version is only increased by 1 number, do alter operation", createReq.name);
        } else {
          mError("stb:%s, schema version increase more than 1 number, error is returned", createReq.name);
          terrno = TSDB_CODE_MND_INVALID_SCHEMA_VER;
          goto _OVER;
        }
      } else {
        mError("stb:%s, already exist while create, input tagVer:%d colVer:%d is invalid", createReq.name,
               createReq.tagVer, createReq.colVer, pStb->tagVer, pStb->colVer);
        terrno = TSDB_CODE_MND_INVALID_SCHEMA_VER;
        goto _OVER;
      }
S
Shengliang Guan 已提交
988 989
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
990
      goto _OVER;
S
Shengliang Guan 已提交
991
    }
S
Shengliang Guan 已提交
992
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
993
    goto _OVER;
994
  } else if (createReq.source == TD_REQ_FROM_TAOX && (createReq.tagVer != 1 || createReq.colVer != 1)) {
995 996 997
    mInfo("stb:%s, alter table does not need to be done, because table is deleted", createReq.name);
    code = 0;
    goto _OVER;
S
Shengliang Guan 已提交
998 999
  }

S
Shengliang Guan 已提交
1000
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
1001 1002
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1003
    goto _OVER;
S
Shengliang Guan 已提交
1004 1005
  }

1006
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1007
    goto _OVER;
S
Shengliang Guan 已提交
1008 1009
  }

1010
  int32_t numOfStbs = -1;
S
Shengliang Guan 已提交
1011 1012 1013 1014
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
    goto _OVER;
  }

L
Liu Jicong 已提交
1015
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
1016 1017 1018 1019
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

wafwerar's avatar
wafwerar 已提交
1020 1021 1022 1023 1024
  if ((terrno = grantCheck(TSDB_GRANT_STABLE)) < 0) {
    code = -1;
    goto _OVER;
  }

1025
  if (isAlter) {
1026
    bool    needRsp = false;
wmmhello's avatar
wmmhello 已提交
1027
    SStbObj pDst = {0};
wmmhello's avatar
wmmhello 已提交
1028 1029 1030
    if (mndBuildStbFromAlter(pStb, &pDst, &createReq) != 0) {
      taosMemoryFreeClear(pDst.pTags);
      taosMemoryFreeClear(pDst.pColumns);
wmmhello's avatar
wmmhello 已提交
1031 1032 1033 1034 1035 1036
      goto _OVER;
    }

    code = mndAlterStbImp(pMnode, pReq, pDb, &pDst, needRsp, NULL, 0);
    taosMemoryFreeClear(pDst.pTags);
    taosMemoryFreeClear(pDst.pColumns);
1037 1038 1039
  } else {
    code = mndCreateStb(pMnode, pReq, &createReq, pDb);
  }
S
Shengliang Guan 已提交
1040
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1041

1042
_OVER:
S
Shengliang Guan 已提交
1043
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1044
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
1045 1046
  }

S
Shengliang Guan 已提交
1047 1048
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1049
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
1050 1051

  return code;
S
Shengliang Guan 已提交
1052 1053
}

S
Shengliang Guan 已提交
1054
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
1055 1056
  if (pAlter->commentLen >= 0) return 0;
  if (pAlter->ttl != 0) return 0;
S
Shengliang 已提交
1057

S
Shengliang Guan 已提交
1058 1059 1060 1061
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
1062

S
Shengliang Guan 已提交
1063 1064 1065
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
1066 1067 1068
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
1069 1070 1071 1072 1073
  }

  return 0;
}

S
Shengliang Guan 已提交
1074
static int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) {
wafwerar's avatar
wafwerar 已提交
1075 1076
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
  if (pNew->pTags == NULL || pNew->pColumns == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  memcpy(pNew->pColumns, pOld->pColumns, sizeof(SSchema) * pOld->numOfColumns);
  memcpy(pNew->pTags, pOld->pTags, sizeof(SSchema) * pOld->numOfTags);
  return 0;
}

S
Shengliang 已提交
1087 1088
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
                                         int32_t ttl) {
S
Shengliang 已提交
1089 1090
  if (commentLen > 0) {
    pNew->commentLen = commentLen;
wmmhello's avatar
wmmhello 已提交
1091
    pNew->comment = taosMemoryCalloc(1, commentLen + 1);
S
Shengliang 已提交
1092 1093 1094 1095
    if (pNew->comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
wmmhello's avatar
wmmhello 已提交
1096
    memcpy(pNew->comment, pComment, commentLen + 1);
1097
  } else if (commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1098
    pNew->commentLen = 0;
S
Shengliang Guan 已提交
1099
  } else {
S
Shengliang 已提交
1100
  }
wmmhello's avatar
wmmhello 已提交
1101

S
Shengliang 已提交
1102 1103 1104
  if (ttl >= 0) {
    pNew->ttl = ttl;
  }
S
Shengliang 已提交
1105 1106 1107 1108 1109 1110 1111

  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }
  return 0;
}

S
Shengliang Guan 已提交
1112
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
  if (pOld->numOfTags + ntags > TSDB_MAX_TAGS) {
    terrno = TSDB_CODE_MND_TOO_MANY_TAGS;
    return -1;
  }

  if (pOld->numOfColumns + ntags + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1123 1124 1125 1126 1127
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1128
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
1129
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1130
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1131
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1132 1133 1134
      return -1;
    }

S
Shengliang 已提交
1135
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1136
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1137 1138 1139
      return -1;
    }

S
Shengliang Guan 已提交
1140 1141 1142 1143
    SSchema *pSchema = &pNew->pTags[pOld->numOfTags + i];
    pSchema->bytes = pField->bytes;
    pSchema->type = pField->type;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
S
Shengliang Guan 已提交
1144 1145
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1146 1147

    mDebug("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1148 1149
  }

1150
  pNew->tagVer++;
S
Shengliang Guan 已提交
1151 1152 1153
  return 0;
}

1154
static int32_t mndCheckAlterColForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
1155 1156 1157 1158 1159 1160 1161 1162
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SMqTopicObj *pTopic = NULL;
    pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
    if (pIter == NULL) break;

    mDebug("topic:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, subType:%d sql:%s",
1163
           pTopic->name, stbFullName, suid, colId, pTopic->subType, pTopic->sql);
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
      sdbRelease(pSdb, pTopic);
      continue;
    }

    SNode *pAst = NULL;
    if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
      ASSERT(0);
      return -1;
    }

    SNodeList *pNodeList = NULL;
    nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
    SNode *pNode = NULL;
    FOREACH(pNode, pNodeList) {
      SColumnNode *pCol = (SColumnNode *)pNode;
      mDebug("topic:%s, check colId:%d tableId:%" PRId64 " ctbStbUid:%" PRId64, pTopic->name, pCol->colId,
             pCol->tableId, pTopic->ctbStbUid);

      if (pCol->tableId != suid && pTopic->ctbStbUid != suid) {
        mDebug("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
        goto NEXT;
      }
      if (pCol->colId > 0 && pCol->colId == colId) {
        sdbRelease(pSdb, pTopic);
        nodesDestroyNode(pAst);
        terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TOPIC;
        mError("topic:%s, check colId:%d conflicted", pTopic->name, pCol->colId);
        return -1;
      }
      mDebug("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
    }

  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
  }
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
  return 0;
}

static int32_t mndCheckAlterColForStream(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SStreamObj *pStream = NULL;
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
    if (pIter == NULL) break;

    SNode *pAst = NULL;
    if (nodesStringToNode(pStream->ast, &pAst) != 0) {
      ASSERT(0);
      return -1;
    }

    SNodeList *pNodeList = NULL;
    nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
    SNode *pNode = NULL;
    FOREACH(pNode, pNodeList) {
      SColumnNode *pCol = (SColumnNode *)pNode;

      if (pCol->tableId != suid) {
        mDebug("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
        goto NEXT;
      }
      if (pCol->colId > 0 && pCol->colId == colId) {
        sdbRelease(pSdb, pStream);
        nodesDestroyNode(pAst);
        terrno = TSDB_CODE_MND_STREAM_MUST_BE_DELETED;
        mError("stream:%s, check colId:%d conflicted", pStream->name, pCol->colId);
        return -1;
      }
      mDebug("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
    }

  NEXT:
    sdbRelease(pSdb, pStream);
    nodesDestroyNode(pAst);
  }
  return 0;
}
1244

1245 1246 1247
static int32_t mndCheckAlterColForTSma(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
1248 1249 1250 1251 1252
  while (1) {
    SSmaObj *pSma = NULL;
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
    if (pIter == NULL) break;

1253 1254
    mDebug("tsma:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, sql:%s", pSma->name,
           stbFullName, suid, colId, pSma->sql);
1255 1256 1257 1258 1259

    SNode *pAst = NULL;
    if (nodesStringToNode(pSma->ast, &pAst) != 0) {
      terrno = TSDB_CODE_SDB_INVALID_DATA_CONTENT;
      mError("tsma:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d failed since parse AST err",
1260
             pSma->name, stbFullName, suid, colId);
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
      return -1;
    }

    SNodeList *pNodeList = NULL;
    nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
    SNode *pNode = NULL;
    FOREACH(pNode, pNodeList) {
      SColumnNode *pCol = (SColumnNode *)pNode;
      mDebug("tsma:%s, check colId:%d tableId:%" PRId64, pSma->name, pCol->colId, pCol->tableId);

      if ((pCol->tableId != suid) && (pSma->stbUid != suid)) {
        mDebug("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
1273
        goto NEXT;
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
      }
      if ((pCol->colId) > 0 && (pCol->colId == colId)) {
        sdbRelease(pSdb, pSma);
        nodesDestroyNode(pAst);
        terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TSMA;
        mError("tsma:%s, check colId:%d conflicted", pSma->name, pCol->colId);
        return -1;
      }
      mDebug("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
    }

1285
  NEXT:
1286 1287 1288
    sdbRelease(pSdb, pSma);
    nodesDestroyNode(pAst);
  }
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
  return 0;
}

int32_t mndCheckColAndTagModifiable(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
  if (mndCheckAlterColForTopic(pMnode, stbFullName, suid, colId) < 0) {
    return -1;
  }
  if (mndCheckAlterColForStream(pMnode, stbFullName, suid, colId) < 0) {
    return -1;
  }
1299

1300 1301 1302
  if (mndCheckAlterColForTSma(pMnode, stbFullName, suid, colId) < 0) {
    return -1;
  }
1303 1304 1305
  return 0;
}

1306
static int32_t mndDropSuperTableTag(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
S
Shengliang Guan 已提交
1307 1308 1309 1310 1311 1312
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1313
  col_id_t colId = pOld->pTags[tag].colId;
1314
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1315 1316 1317
    return -1;
  }

S
Shengliang Guan 已提交
1318 1319 1320 1321 1322
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pTags + tag, pNew->pTags + tag + 1, sizeof(SSchema) * (pNew->numOfTags - tag - 1));
S
Shengliang Guan 已提交
1323
  pNew->numOfTags--;
S
Shengliang Guan 已提交
1324

1325
  pNew->tagVer++;
S
Shengliang Guan 已提交
1326 1327 1328 1329
  mDebug("stb:%s, start to drop tag %s", pNew->name, tagName);
  return 0;
}

1330
static int32_t mndAlterStbTagName(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
S
Shengliang Guan 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
  if ((int32_t)taosArrayGetSize(pFields) != 2) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

  SField *pField0 = taosArrayGet(pFields, 0);
  SField *pField1 = taosArrayGet(pFields, 1);

  const char *oldTagName = pField0->name;
  const char *newTagName = pField1->name;

S
Shengliang Guan 已提交
1342 1343 1344 1345 1346 1347
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1348
  col_id_t colId = pOld->pTags[tag].colId;
1349
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1350 1351 1352
    return -1;
  }

S
Shengliang Guan 已提交
1353
  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
S
Shengliang Guan 已提交
1354
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1355 1356 1357
    return -1;
  }

S
Shengliang Guan 已提交
1358 1359
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
    return -1;
  }

  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pSchema = (SSchema *)(pNew->pTags + tag);
  memcpy(pSchema->name, newTagName, TSDB_COL_NAME_LEN);

1370
  pNew->tagVer++;
S
Shengliang Guan 已提交
1371 1372 1373 1374
  mDebug("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
  return 0;
}

1375
static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1376
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1377 1378 1379 1380 1381
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1382
  col_id_t colId = pOld->pTags[tag].colId;
1383
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1384 1385 1386
    return -1;
  }

S
Shengliang Guan 已提交
1387 1388 1389 1390 1391 1392
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pTag = pNew->pTags + tag;

S
Shengliang Guan 已提交
1393 1394 1395 1396 1397
  if (!(pTag->type == TSDB_DATA_TYPE_BINARY || pTag->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
1398
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1399 1400 1401 1402
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1403
  pTag->bytes = pField->bytes;
1404
  pNew->tagVer++;
S
Shengliang Guan 已提交
1405

S
Shengliang Guan 已提交
1406
  mDebug("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1407 1408 1409
  return 0;
}

S
Shengliang Guan 已提交
1410
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1411 1412 1413 1414 1415
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1416 1417 1418 1419 1420
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1421
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1422
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1423
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1424
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1425 1426 1427
      return -1;
    }

S
Shengliang 已提交
1428
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1429
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1430 1431 1432
      return -1;
    }

S
Shengliang Guan 已提交
1433 1434 1435 1436
    SSchema *pSchema = &pNew->pColumns[pOld->numOfColumns + i];
    pSchema->bytes = pField->bytes;
    pSchema->type = pField->type;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
S
Shengliang Guan 已提交
1437 1438
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1439 1440

    mDebug("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1441 1442
  }

1443
  pNew->colVer++;
S
Shengliang Guan 已提交
1444 1445 1446
  return 0;
}

1447
static int32_t mndDropSuperTableColumn(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *colName) {
S
Shengliang Guan 已提交
1448
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1449
  if (col < 0) {
S
Shengliang Guan 已提交
1450 1451 1452 1453
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
  if (col == 0) {
    terrno = TSDB_CODE_MND_INVALID_STB_ALTER_OPTION;
    return -1;
  }

  if (pOld->numOfColumns == 2) {
    terrno = TSDB_CODE_MND_INVALID_STB_ALTER_OPTION;
    return -1;
  }

1464
  col_id_t colId = pOld->pColumns[col].colId;
1465
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1466 1467 1468
    return -1;
  }

S
Shengliang Guan 已提交
1469 1470 1471 1472 1473
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
S
Shengliang Guan 已提交
1474
  pNew->numOfColumns--;
S
Shengliang Guan 已提交
1475

1476
  pNew->colVer++;
S
Shengliang Guan 已提交
1477 1478 1479 1480
  mDebug("stb:%s, start to drop col %s", pNew->name, colName);
  return 0;
}

1481
static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1482
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1483 1484 1485 1486 1487 1488 1489
  if (col < 0) {
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

  uint32_t nLen = 0;
  for (int32_t i = 0; i < pOld->numOfColumns; ++i) {
S
Shengliang Guan 已提交
1490
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1491 1492 1493 1494 1495 1496 1497
  }

  if (nLen > TSDB_MAX_BYTES_PER_ROW) {
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

1498
  col_id_t colId = pOld->pColumns[col].colId;
1499
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1500 1501 1502
    return -1;
  }

S
Shengliang Guan 已提交
1503 1504 1505 1506 1507
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pCol = pNew->pColumns + col;
S
Shengliang Guan 已提交
1508 1509
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1510 1511 1512
    return -1;
  }

S
Shengliang Guan 已提交
1513
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1514
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1515 1516 1517
    return -1;
  }

S
Shengliang Guan 已提交
1518
  pCol->bytes = pField->bytes;
1519
  pNew->colVer++;
S
Shengliang Guan 已提交
1520

S
Shengliang Guan 已提交
1521
  mDebug("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1522 1523 1524
  return 0;
}

S
Shengliang Guan 已提交
1525
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1526 1527
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
1528 1529 1530 1531
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1532
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1533 1534 1535 1536

  return 0;
}

S
Shengliang Guan 已提交
1537
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1538 1539
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
1540 1541 1542 1543
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1544 1545 1546 1547 1548
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

1549 1550
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, void *alterOriData,
                                         int32_t alterOriDataLen) {
S
Shengliang Guan 已提交
1551 1552 1553 1554 1555 1556 1557 1558
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
  int32_t contLen;

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1559
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
1560 1561 1562 1563
      sdbRelease(pSdb, pVgroup);
      continue;
    }

wmmhello's avatar
wmmhello 已提交
1564
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, alterOriData, alterOriDataLen);
S
Shengliang Guan 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
S
Shengliang Guan 已提交
1574
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1575
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1576
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

D
dapan1121 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
static int32_t mndBuildStbSchemaImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, STableMetaRsp *pRsp) {
  taosRLockLatch(&pStb->lock);

  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
  if (pRsp->pSchemas == NULL) {
    taosRUnLockLatch(&pStb->lock);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  strcpy(pRsp->dbFName, pStb->db);
  strcpy(pRsp->tbName, tbName);
  strcpy(pRsp->stbName, tbName);
  pRsp->dbId = pDb->uid;
  pRsp->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->precision = pDb->cfg.precision;
  pRsp->tableType = TSDB_SUPER_TABLE;
  pRsp->sversion = pStb->colVer;
  pRsp->tversion = pStb->tagVer;
  pRsp->suid = pStb->uid;
  pRsp->tuid = pStb->uid;

  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pRsp->pSchemas[i];
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
    SSchema *pSrcSchema = &pStb->pTags[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

  taosRUnLockLatch(&pStb->lock);
  return 0;
}

D
dapan1121 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
static int32_t mndBuildStbCfgImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, STableCfgRsp *pRsp) {
  taosRLockLatch(&pStb->lock);

  int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
  pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
  if (pRsp->pSchemas == NULL) {
    taosRUnLockLatch(&pStb->lock);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  strcpy(pRsp->dbFName, pStb->db);
  strcpy(pRsp->tbName, tbName);
  strcpy(pRsp->stbName, tbName);
  pRsp->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->tableType = TSDB_SUPER_TABLE;
D
dapan1121 已提交
1650 1651 1652 1653
  pRsp->delay1 = pStb->maxdelay[0];
  pRsp->delay2 = pStb->maxdelay[1];
  pRsp->watermark1 = pStb->watermark[0];
  pRsp->watermark2 = pStb->watermark[1];
D
dapan1121 已提交
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
  pRsp->ttl = pStb->ttl;
  pRsp->commentLen = pStb->commentLen;
  if (pStb->commentLen > 0) {
    pRsp->pComment = strdup(pStb->comment);
  }

  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pRsp->pSchemas[i];
    SSchema *pSrcSchema = &pStb->pColumns[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
    SSchema *pSrcSchema = &pStb->pTags[i];
    memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
    pSchema->type = pSrcSchema->type;
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

S
Shengliang Guan 已提交
1678
  if (pStb->numOfFuncs > 0) {
D
dapan1121 已提交
1679 1680
    pRsp->pFuncs = taosArrayDup(pStb->pFuncs);
  }
1681

D
dapan1121 已提交
1682 1683 1684 1685
  taosRUnLockLatch(&pStb->lock);
  return 0;
}

L
Liu Jicong 已提交
1686 1687
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp,
                                 int32_t *smaVer) {
D
dapan1121 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
  char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
  snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, tbName);

  SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
D
dapan1121 已提交
1700
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
D
dapan1121 已提交
1701 1702 1703
    return -1;
  }

D
dapan1121 已提交
1704 1705 1706 1707
  if (smaVer) {
    *smaVer = pStb->smaVer;
  }

D
dapan1121 已提交
1708 1709 1710 1711 1712 1713
  int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}

D
dapan1121 已提交
1714
static int32_t mndBuildStbCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
  char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
  snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, tbName);

  SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

  SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
  if (pStb == NULL) {
    mndReleaseDb(pMnode, pDb);
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
    return -1;
  }
D
dapan1121 已提交
1730

1731
  int32_t code = mndBuildStbCfgImp(pDb, pStb, tbName, pRsp);
D
dapan1121 已提交
1732

1733 1734 1735 1736
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}
D
dapan1121 已提交
1737

1738
static int32_t mndBuildSMAlterStbRsp(SDbObj *pDb, SStbObj *pObj, void **pCont, int32_t *pLen) {
1739
  int32_t       ret;
D
dapan1121 已提交
1740
  SEncoder      ec = {0};
1741
  uint32_t      contLen = 0;
D
dapan1121 已提交
1742
  SMAlterStbRsp alterRsp = {0};
1743
  SName         name = {0};
1744
  tNameFromString(&name, pObj->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
D
dapan1121 已提交
1745 1746 1747 1748 1749 1750

  alterRsp.pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
  if (NULL == alterRsp.pMeta) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
1751

D
dapan1121 已提交
1752
  ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, alterRsp.pMeta);
D
dapan1121 已提交
1753 1754 1755 1756
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }
1757

D
dapan1121 已提交
1758 1759 1760 1761 1762 1763
  tEncodeSize(tEncodeSMAlterStbRsp, &alterRsp, contLen, ret);
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }

1764
  void *cont = taosMemoryMalloc(contLen);
D
dapan1121 已提交
1765 1766 1767 1768 1769 1770 1771 1772
  tEncoderInit(&ec, cont, contLen);
  tEncodeSMAlterStbRsp(&ec, &alterRsp);
  tEncoderClear(&ec);

  tFreeSMAlterStbRsp(&alterRsp);

  *pCont = cont;
  *pLen = contLen;
1773

D
dapan1121 已提交
1774 1775 1776
  return 0;
}

1777 1778
static int32_t mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                              void *alterOriData, int32_t alterOriDataLen) {
1779
  int32_t code = -1;
1780
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq);
1781 1782 1783
  if (pTrans == NULL) goto _OVER;

  mDebug("trans:%d, used to alter stb:%s", pTrans->id, pStb->name);
1784
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794

  if (needRsp) {
    void   *pCont = NULL;
    int32_t contLen = 0;
    if (mndBuildSMAlterStbRsp(pDb, pStb, &pCont, &contLen) != 0) goto _OVER;
    mndTransSetRpcRsp(pTrans, pCont, contLen);
  }

  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
wmmhello's avatar
wmmhello 已提交
1795
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, pStb, alterOriData, alterOriDataLen) != 0) goto _OVER;
1796 1797 1798 1799 1800 1801 1802 1803 1804
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  code = 0;

_OVER:
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
1805
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
1806 1807 1808 1809
  bool    needRsp = true;
  int32_t code = -1;
  SField *pField0 = NULL;

S
Shengliang Guan 已提交
1810 1811 1812
  SStbObj stbObj = {0};
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
1813
  taosRUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
1814 1815 1816
  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
D
dapan1121 已提交
1817
  stbObj.lock = 0;
S
Shengliang Guan 已提交
1818

S
Shengliang Guan 已提交
1819
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
1820
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
1821
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1822
      break;
S
Shengliang Guan 已提交
1823
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang 已提交
1824
      pField0 = taosArrayGet(pAlter->pFields, 0);
1825
      code = mndDropSuperTableTag(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1826
      break;
S
Shengliang Guan 已提交
1827
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
1828
      code = mndAlterStbTagName(pMnode, pOld, &stbObj, pAlter->pFields);
S
Shengliang Guan 已提交
1829
      break;
S
Shengliang Guan 已提交
1830
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang 已提交
1831
      pField0 = taosArrayGet(pAlter->pFields, 0);
1832
      code = mndAlterStbTagBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1833 1834
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
1835
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
1836 1837
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang 已提交
1838
      pField0 = taosArrayGet(pAlter->pFields, 0);
1839
      code = mndDropSuperTableColumn(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
1840
      break;
S
Shengliang Guan 已提交
1841
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang 已提交
1842
      pField0 = taosArrayGet(pAlter->pFields, 0);
1843
      code = mndAlterStbColumnBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
1844
      break;
S
Shengliang 已提交
1845
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
D
dapan1121 已提交
1846
      needRsp = false;
S
Shengliang 已提交
1847
      code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
S
Shengliang 已提交
1848
      break;
S
Shengliang Guan 已提交
1849
    default:
D
dapan1121 已提交
1850
      needRsp = false;
S
Shengliang 已提交
1851
      terrno = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
1852 1853 1854
      break;
  }

1855
  if (code != 0) goto _OVER;
wmmhello's avatar
wmmhello 已提交
1856
  code = mndAlterStbImp(pMnode, pReq, pDb, &stbObj, needRsp, pReq->pCont, pReq->contLen);
S
Shengliang Guan 已提交
1857

1858
_OVER:
wafwerar's avatar
wafwerar 已提交
1859 1860
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
1861 1862 1863
  if (pAlter->commentLen > 0) {
    taosMemoryFreeClear(stbObj.comment);
  }
S
Shengliang Guan 已提交
1864 1865
  return code;
}
S
Shengliang Guan 已提交
1866

1867
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
1868
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1869 1870 1871 1872
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
1873

S
Shengliang Guan 已提交
1874
  if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
1875
    terrno = TSDB_CODE_INVALID_MSG;
1876
    goto _OVER;
S
Shengliang Guan 已提交
1877
  }
S
Shengliang Guan 已提交
1878

S
Shengliang Guan 已提交
1879
  mDebug("stb:%s, start to alter", alterReq.name);
1880
  if (mndCheckAlterStbReq(&alterReq) != 0) goto _OVER;
S
Shengliang Guan 已提交
1881

S
Shengliang Guan 已提交
1882
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1883 1884
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
1885
    goto _OVER;
S
Shengliang Guan 已提交
1886 1887
  }

S
Shengliang Guan 已提交
1888
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
1889 1890
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
1891
    goto _OVER;
S
Shengliang Guan 已提交
1892
  }
S
Shengliang Guan 已提交
1893

1894
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1895
    goto _OVER;
S
Shengliang Guan 已提交
1896 1897
  }

S
Shengliang Guan 已提交
1898
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
1899
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1900

1901
_OVER:
S
Shengliang Guan 已提交
1902
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1903
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
1904 1905
  }

S
Shengliang Guan 已提交
1906 1907
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
1908
  tFreeSMAltertbReq(&alterReq);
S
Shengliang Guan 已提交
1909 1910

  return code;
S
Shengliang Guan 已提交
1911
}
S
Shengliang Guan 已提交
1912

S
Shengliang Guan 已提交
1913 1914 1915
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
1916 1917 1918 1919
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1920 1921 1922 1923 1924 1925 1926 1927
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING) != 0) return -1;

  return 0;
}

static int32_t mndSetDropStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
1928 1929 1930 1931
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1932 1933 1934 1935 1936
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
1937 1938 1939 1940
static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
  SSdb   *pSdb = pMnode->pSdb;
  SVgObj *pVgroup = NULL;
  void   *pIter = NULL;
S
Shengliang Guan 已提交
1941

S
Shengliang Guan 已提交
1942 1943 1944
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
1945
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
1946 1947 1948 1949
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
1950
    int32_t contLen = 0;
S
Shengliang Guan 已提交
1951
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
    if (pReq == NULL) {
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
    action.pCont = pReq;
    action.contLen = contLen;
    action.msgType = TDMT_VND_DROP_STB;
    action.acceptableCode = TSDB_CODE_VND_TB_NOT_EXIST;
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1966
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
1977
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1978
  int32_t code = -1;
1979
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq);
1980
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
1981

S
Shengliang Guan 已提交
1982
  mDebug("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);
1983
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
S
Shengliang Guan 已提交
1984

1985 1986 1987
  if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
  if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
  if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
1988
  if (mndDropSmasByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
1989
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
1990

S
Shengliang Guan 已提交
1991 1992
  code = 0;

1993
_OVER:
S
Shengliang Guan 已提交
1994
  mndTransDrop(pTrans);
S
Shengliang 已提交
1995
  return code;
S
Shengliang Guan 已提交
1996 1997
}

1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
static int32_t mndCheckDropStbForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SMqTopicObj *pTopic = NULL;
    pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
    if (pIter == NULL) break;

    if (pTopic->subType == TOPIC_SUB_TYPE__TABLE) {
      if (pTopic->stbUid == suid) {
        sdbRelease(pSdb, pTopic);
        return -1;
      }
    }

    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
      sdbRelease(pSdb, pTopic);
      continue;
    }

    SNode *pAst = NULL;
    if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
      ASSERT(0);
      return -1;
    }

    SNodeList *pNodeList = NULL;
    nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
    SNode *pNode = NULL;
    FOREACH(pNode, pNodeList) {
      SColumnNode *pCol = (SColumnNode *)pNode;

L
Liu Jicong 已提交
2030
      if (pCol->tableId == suid) {
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
        sdbRelease(pSdb, pTopic);
        nodesDestroyNode(pAst);
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
  }
  return 0;
}

static int32_t mndCheckDropStbForStream(SMnode *pMnode, const char *stbFullName, int64_t suid) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SStreamObj *pStream = NULL;
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
    if (pIter == NULL) break;

L
Liu Jicong 已提交
2053 2054 2055 2056 2057 2058
    if (pStream->smaId != 0) {
      sdbRelease(pSdb, pStream);
      continue;
    }

    if (pStream->targetStbUid == suid) {
L
Liu Jicong 已提交
2059 2060 2061 2062
      sdbRelease(pSdb, pStream);
      return -1;
    }

2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
    SNode *pAst = NULL;
    if (nodesStringToNode(pStream->ast, &pAst) != 0) {
      ASSERT(0);
      return -1;
    }

    SNodeList *pNodeList = NULL;
    nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
    SNode *pNode = NULL;
    FOREACH(pNode, pNodeList) {
      SColumnNode *pCol = (SColumnNode *)pNode;

L
Liu Jicong 已提交
2075
      if (pCol->tableId == suid) {
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
        sdbRelease(pSdb, pStream);
        nodesDestroyNode(pAst);
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pStream);
    nodesDestroyNode(pAst);
  }
  return 0;
}

2090
static int32_t mndProcessDropStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
2091
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2092 2093 2094
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
2095
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
2096

S
Shengliang Guan 已提交
2097
  if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
2098
    terrno = TSDB_CODE_INVALID_MSG;
2099
    goto _OVER;
S
Shengliang Guan 已提交
2100
  }
S
Shengliang Guan 已提交
2101

S
Shengliang Guan 已提交
2102
  mDebug("stb:%s, start to drop", dropReq.name);
S
Shengliang Guan 已提交
2103

S
Shengliang Guan 已提交
2104
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2105
  if (pStb == NULL) {
S
Shengliang Guan 已提交
2106 2107
    if (dropReq.igNotExists) {
      mDebug("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
2108
      code = 0;
2109
      goto _OVER;
S
Shengliang Guan 已提交
2110 2111
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
2112
      goto _OVER;
S
Shengliang Guan 已提交
2113 2114 2115
    }
  }

wmmhello's avatar
wmmhello 已提交
2116 2117
  if (dropReq.source == TD_REQ_FROM_TAOX && pStb->uid != dropReq.suid) {
    code = 0;
wmmhello's avatar
wmmhello 已提交
2118 2119 2120
    goto _OVER;
  }

S
Shengliang Guan 已提交
2121
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2122 2123
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
2124
    goto _OVER;
S
Shengliang Guan 已提交
2125 2126
  }

2127
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
2128
    goto _OVER;
S
Shengliang Guan 已提交
2129 2130
  }

2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
  if (mndCheckDropStbForTopic(pMnode, dropReq.name, pStb->uid) < 0) {
    terrno = TSDB_CODE_MND_TOPIC_MUST_BE_DELETED;
    goto _OVER;
  }

  if (mndCheckDropStbForStream(pMnode, dropReq.name, pStb->uid) < 0) {
    terrno = TSDB_CODE_MND_STREAM_MUST_BE_DELETED;
    goto _OVER;
  }

S
Shengliang Guan 已提交
2141
  code = mndDropStb(pMnode, pReq, pDb, pStb);
S
Shengliang Guan 已提交
2142
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
2143

2144
_OVER:
S
Shengliang Guan 已提交
2145
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2146
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
2147 2148
  }

S
Shengliang Guan 已提交
2149 2150 2151
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
S
Shengliang Guan 已提交
2152
}
S
Shengliang Guan 已提交
2153

S
Shengliang Guan 已提交
2154 2155
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2156 2157 2158
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
2159

S
Shengliang Guan 已提交
2160
  if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
2161
    terrno = TSDB_CODE_INVALID_MSG;
2162
    goto _OVER;
S
Shengliang Guan 已提交
2163
  }
D
dapan 已提交
2164

D
dapan1121 已提交
2165 2166 2167
  if (0 == strcmp(infoReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
    mDebug("information_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildInsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
2168
      goto _OVER;
D
dapan1121 已提交
2169
    }
D
dapan1121 已提交
2170 2171 2172
  } else if (0 == strcmp(infoReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
    mDebug("performance_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
    if (mndBuildPerfsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
2173
      goto _OVER;
D
dapan1121 已提交
2174
    }
D
dapan1121 已提交
2175 2176
  } else {
    mDebug("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
2177
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp, NULL) != 0) {
2178
      goto _OVER;
D
dapan1121 已提交
2179
    }
S
Shengliang Guan 已提交
2180
  }
S
Shengliang Guan 已提交
2181

S
Shengliang Guan 已提交
2182 2183 2184
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
2185
    goto _OVER;
S
Shengliang Guan 已提交
2186
  }
S
Shengliang Guan 已提交
2187

S
Shengliang Guan 已提交
2188
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
2189 2190
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2191
    goto _OVER;
S
Shengliang Guan 已提交
2192
  }
D
dapan 已提交
2193

S
Shengliang Guan 已提交
2194
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
2195 2196
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
S
Shengliang Guan 已提交
2197
  code = 0;
S
Shengliang Guan 已提交
2198

2199
  mTrace("%s.%s, meta is retrieved", infoReq.dbFName, infoReq.tbName);
D
dapan 已提交
2200

2201
_OVER:
S
Shengliang Guan 已提交
2202 2203 2204
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
2205

S
Shengliang Guan 已提交
2206 2207 2208
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
2209

D
dapan1121 已提交
2210
static int32_t mndProcessTableCfgReq(SRpcMsg *pReq) {
2211 2212 2213 2214
  SMnode      *pMnode = pReq->info.node;
  int32_t      code = -1;
  STableCfgReq cfgReq = {0};
  STableCfgRsp cfgRsp = {0};
D
dapan1121 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265

  if (tDeserializeSTableCfgReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  if (0 == strcmp(cfgReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
    mDebug("information_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
    if (mndBuildInsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else if (0 == strcmp(cfgReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
    mDebug("performance_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
    if (mndBuildPerfsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else {
    mDebug("stb:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
    if (mndBuildStbCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  }

  int32_t rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }

  void *pRsp = rpcMallocCont(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
  code = 0;

  mTrace("%s.%s, cfg is retrieved", cfgReq.dbFName, cfgReq.tbName);

_OVER:
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve cfg since %s", cfgReq.dbFName, cfgReq.tbName, terrstr());
  }

  tFreeSTableCfgRsp(&cfgRsp);
  return code;
}

D
dapan1121 已提交
2266
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
S
Shengliang Guan 已提交
2267
                           int32_t *pRspLen) {
D
dapan1121 已提交
2268
  SSTbHbRsp hbRsp = {0};
D
dapan1121 已提交
2269 2270
  hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (hbRsp.pMetaRsp == NULL) {
S
Shengliang Guan 已提交
2271 2272 2273
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
2274

D
dapan1121 已提交
2275
  hbRsp.pIndexRsp = taosArrayInit(numOfStbs, sizeof(STableIndexRsp));
D
dapan1121 已提交
2276
  if (NULL == hbRsp.pIndexRsp) {
D
dapan1121 已提交
2277
    taosArrayDestroy(hbRsp.pMetaRsp);
D
dapan1121 已提交
2278 2279 2280
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
2281

S
Shengliang Guan 已提交
2282
  for (int32_t i = 0; i < numOfStbs; ++i) {
D
dapan1121 已提交
2283
    SSTableVersion *pStbVersion = &pStbVersions[i];
S
Shengliang Guan 已提交
2284 2285 2286
    pStbVersion->suid = be64toh(pStbVersion->suid);
    pStbVersion->sversion = ntohs(pStbVersion->sversion);
    pStbVersion->tversion = ntohs(pStbVersion->tversion);
D
dapan1121 已提交
2287
    pStbVersion->smaVer = ntohl(pStbVersion->smaVer);
S
Shengliang Guan 已提交
2288

S
Shengliang Guan 已提交
2289
    STableMetaRsp metaRsp = {0};
L
Liu Jicong 已提交
2290
    int32_t       smaVer = 0;
S
Shengliang Guan 已提交
2291
    mDebug("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2292
    if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp, &smaVer) != 0) {
S
Shengliang Guan 已提交
2293 2294
      metaRsp.numOfColumns = -1;
      metaRsp.suid = pStbVersion->suid;
D
dapan1121 已提交
2295
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2296
      continue;
D
dapan 已提交
2297
    }
S
Shengliang Guan 已提交
2298

D
dapan1121 已提交
2299
    if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
D
dapan1121 已提交
2300
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2301 2302
    } else {
      tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
2303
    }
D
dapan1121 已提交
2304

D
dapan1121 已提交
2305
    if (pStbVersion->smaVer && pStbVersion->smaVer != smaVer) {
L
Liu Jicong 已提交
2306 2307
      bool           exist = false;
      char           tbFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
2308
      STableIndexRsp indexRsp = {0};
D
dapan1121 已提交
2309 2310 2311 2312 2313
      indexRsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
      if (NULL == indexRsp.pIndex) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
L
Liu Jicong 已提交
2314

D
dapan1121 已提交
2315
      sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2316
      int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist);
D
dapan1121 已提交
2317
      if (code || !exist) {
D
dapan1121 已提交
2318 2319 2320
        indexRsp.suid = pStbVersion->suid;
        indexRsp.version = -1;
        indexRsp.pIndex = NULL;
D
dapan1121 已提交
2321
      }
D
dapan1121 已提交
2322

D
dapan1121 已提交
2323 2324
      strcpy(indexRsp.dbFName, pStbVersion->dbFName);
      strcpy(indexRsp.tbName, pStbVersion->stbName);
D
dapan1121 已提交
2325 2326

      taosArrayPush(hbRsp.pIndexRsp, &indexRsp);
D
dapan1121 已提交
2327
    }
S
Shengliang Guan 已提交
2328
  }
S
Shengliang Guan 已提交
2329

D
dapan1121 已提交
2330
  int32_t rspLen = tSerializeSSTbHbRsp(NULL, 0, &hbRsp);
S
Shengliang Guan 已提交
2331
  if (rspLen < 0) {
D
dapan1121 已提交
2332
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2333 2334
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
2335 2336
  }

wafwerar's avatar
wafwerar 已提交
2337
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
2338
  if (pRsp == NULL) {
D
dapan1121 已提交
2339
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2340 2341
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
2342 2343
  }

D
dapan1121 已提交
2344 2345
  tSerializeSSTbHbRsp(pRsp, rspLen, &hbRsp);
  tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2346 2347
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
2348 2349 2350
  return 0;
}

2351
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
2352
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2353 2354 2355 2356 2357 2358
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
2359
  int32_t numOfStbs = 0;
2360
  void   *pIter = NULL;
S
Shengliang Guan 已提交
2361
  while (1) {
S
Shengliang Guan 已提交
2362
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
2363
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2364 2365
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
2366
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
2367
      numOfStbs++;
S
Shengliang Guan 已提交
2368 2369
    }

S
Shengliang Guan 已提交
2370
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2371 2372
  }

S
Shengliang Guan 已提交
2373
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
2374
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
2375 2376 2377
  return 0;
}

L
Liu Jicong 已提交
2378 2379 2380 2381 2382 2383 2384 2385
void mndExtractDbNameFromStbFullName(const char *stbFullName, char *dst) {
  SName name = {0};
  tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  tNameGetFullDbName(&name, dst);
}

void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t dstSize) {
S
Shengliang Guan 已提交
2386 2387
  int32_t pos = -1;
  int32_t num = 0;
L
Liu Jicong 已提交
2388 2389
  for (pos = 0; stbFullName[pos] != 0; ++pos) {
    if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
2390 2391 2392 2393
    if (num == 2) break;
  }

  if (num == 2) {
L
Liu Jicong 已提交
2394
    tstrncpy(dst, stbFullName + pos + 1, dstSize);
S
Shengliang Guan 已提交
2395 2396 2397
  }
}

S
Shengliang Guan 已提交
2398 2399
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2400
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2401 2402 2403
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
2404

H
Hongze Cheng 已提交
2405
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
2406 2407
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
2408
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
2409
  }
S
Shengliang Guan 已提交
2410

S
Shengliang Guan 已提交
2411
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
2412
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2413 2414
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
2415
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
2416
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2417 2418 2419 2420 2421
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
2422
    SName name = {0};
H
Hongze Cheng 已提交
2423
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
2424
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
2425
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
2426

H
Hongze Cheng 已提交
2427 2428
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)stbName, false);
2429

H
Hongze Cheng 已提交
2430 2431
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
2432 2433 2434
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

2435
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
H
Hongze Cheng 已提交
2436
    colDataAppend(pColInfo, numOfRows, (const char *)db, false);
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->createdTime, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfColumns, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfTags, false);

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pStb->updateTime, false);  // number of tables

2450
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
2451 2452 2453 2454
    if (pStb->commentLen > 0) {
      char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, pStb->comment);
      colDataAppend(pColInfo, numOfRows, comment, false);
2455
    } else if (pStb->commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
2456 2457 2458 2459 2460
      char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, "");
      colDataAppend(pColInfo, numOfRows, comment, false);
    } else {
      colDataAppendNULL(pColInfo, numOfRows);
S
Shengliang Guan 已提交
2461
    }
H
Haojun Liao 已提交
2462

2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
    char watermark[64 + VARSTR_HEADER_SIZE] = {0};
    sprintf(varDataVal(watermark), "%" PRId64 "a,%" PRId64 "a", pStb->watermark[0], pStb->watermark[1]);
    varDataSetLen(watermark, strlen(varDataVal(watermark)));

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)watermark, false);

    char maxDelay[64 + VARSTR_HEADER_SIZE] = {0};
    sprintf(varDataVal(maxDelay), "%" PRId64 "a,%" PRId64 "a", pStb->maxdelay[0], pStb->maxdelay[1]);
    varDataSetLen(maxDelay, strlen(varDataVal(maxDelay)));

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)maxDelay, false);

2477
    char    rollup[128 + VARSTR_HEADER_SIZE] = {0};
2478 2479 2480 2481
    int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs);
    for (int32_t i = 0; i < rollupNum; ++i) {
      char *funcName = taosArrayGet(pStb->pFuncs, i);
      if (i) {
2482
        strcat(varDataVal(rollup), ", ");
2483 2484 2485 2486 2487 2488 2489 2490
      }
      strcat(varDataVal(rollup), funcName);
    }
    varDataSetLen(rollup, strlen(varDataVal(rollup)));

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)rollup, false);

S
Shengliang Guan 已提交
2491
    numOfRows++;
S
Shengliang Guan 已提交
2492
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2493 2494
  }

H
Haojun Liao 已提交
2495 2496 2497 2498
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

2499
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
2500 2501 2502
  return numOfRows;
}

S
Shengliang Guan 已提交
2503
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
2504 2505
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
2506
}