mndStb.c 107.8 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"
dengyihao's avatar
dengyihao 已提交
20
#include "mndIndex.h"
S
Shengliang Guan 已提交
21
#include "mndInfoSchema.h"
S
Shengliang Guan 已提交
22
#include "mndMnode.h"
H
Hongze Cheng 已提交
23
#include "mndPerfSchema.h"
24
#include "mndPrivilege.h"
C
Cary Xu 已提交
25
#include "mndScheduler.h"
S
Shengliang Guan 已提交
26
#include "mndShow.h"
27
#include "mndSma.h"
28
#include "mndTopic.h"
S
Shengliang Guan 已提交
29 30
#include "mndTrans.h"
#include "mndUser.h"
31
#include "mndVgroup.h"
S
Shengliang Guan 已提交
32
#include "tname.h"
S
Shengliang Guan 已提交
33

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

static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw);
static int32_t  mndStbActionInsert(SSdb *pSdb, SStbObj *pStb);
static int32_t  mndStbActionDelete(SSdb *pSdb, SStbObj *pStb);
S
Shengliang Guan 已提交
40
static int32_t  mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
S
Shengliang Guan 已提交
41
static int32_t  mndProcessTtlTimer(SRpcMsg *pReq);
42 43 44
static int32_t  mndProcessCreateStbReq(SRpcMsg *pReq);
static int32_t  mndProcessAlterStbReq(SRpcMsg *pReq);
static int32_t  mndProcessDropStbReq(SRpcMsg *pReq);
wmmhello's avatar
wmmhello 已提交
45
static int32_t  mndProcessDropTtltbReq(SRpcMsg *pReq);
S
Shengliang Guan 已提交
46 47
static int32_t  mndProcessTableMetaReq(SRpcMsg *pReq);
static int32_t  mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
wmmhello's avatar
wmmhello 已提交
48
static int32_t  mndRetrieveStbCol(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
49
static void     mndCancelGetNextStb(SMnode *pMnode, void *pIter);
D
dapan1121 已提交
50
static int32_t  mndProcessTableCfgReq(SRpcMsg *pReq);
51 52
static int32_t  mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                               void *alterOriData, int32_t alterOriDataLen);
dengyihao's avatar
dengyihao 已提交
53 54
static int32_t  mndAlterStbAndUpdateTagIdxImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                                              void *alterOriData, int32_t alterOriDataLen, const SMAlterStbReq *pAlter);
S
Shengliang Guan 已提交
55

dengyihao's avatar
dengyihao 已提交
56 57 58
static int32_t mndProcessCreateIndexReq(SRpcMsg *pReq);
static int32_t mndProcessDropIndexReq(SRpcMsg *pReq);

S
Shengliang Guan 已提交
59
int32_t mndInitStb(SMnode *pMnode) {
S
Shengliang Guan 已提交
60 61 62 63 64 65 66 67 68
  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 已提交
69

70 71 72
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessCreateStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessAlterStbReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessDropStbReq);
73
  mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndTransProcessRsp);
wmmhello's avatar
wmmhello 已提交
74
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_TTL_TABLE_RSP, mndProcessDropTtltbReq);
75 76
  mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndTransProcessRsp);
  mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndTransProcessRsp);
D
dapan1121 已提交
77
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_META, mndProcessTableMetaReq);
S
Shengliang Guan 已提交
78
  mndSetMsgHandle(pMnode, TDMT_MND_TTL_TIMER, mndProcessTtlTimer);
D
dapan1121 已提交
79
  mndSetMsgHandle(pMnode, TDMT_MND_TABLE_CFG, mndProcessTableCfgReq);
dengyihao's avatar
dengyihao 已提交
80
  //  mndSetMsgHandle(pMnode, TDMT_MND_SYSTABLE_RETRIEVE, mndProcessRetrieveStbReq);
S
Shengliang Guan 已提交
81

dengyihao's avatar
dengyihao 已提交
82 83 84 85
  // mndSetMsgHandle(pMnode, TDMT_MND_CREATE_INDEX, mndProcessCreateIndexReq);
  // mndSetMsgHandle(pMnode, TDMT_MND_DROP_INDEX, mndProcessDropIndexReq);
  // mndSetMsgHandle(pMnode, TDMT_VND_CREATE_INDEX_RSP, mndTransProcessRsp);
  // mndSetMsgHandle(pMnode, TDMT_VND_DROP_INDEX_RSP, mndTransProcessRsp);
dengyihao's avatar
dengyihao 已提交
86

S
Shengliang Guan 已提交
87 88
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STB, mndRetrieveStb);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STB, mndCancelGetNextStb);
S
Shengliang Guan 已提交
89

wmmhello's avatar
wmmhello 已提交
90 91 92
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_COL, mndRetrieveStbCol);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_COL, mndCancelGetNextStb);

S
Shengliang Guan 已提交
93
  return sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
94 95
}

S
Shengliang Guan 已提交
96
void mndCleanupStb(SMnode *pMnode) {}
S
Shengliang Guan 已提交
97

98
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
99 100
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
101
  int32_t size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags) * sizeof(SSchema) + pStb->commentLen +
D
dapan1121 已提交
102
                 pStb->ast1Len + pStb->ast2Len + STB_RESERVE_SIZE + taosArrayGetSize(pStb->pFuncs) * TSDB_FUNC_NAME_LEN;
S
Shengliang Guan 已提交
103
  SSdbRaw *pRaw = sdbAllocRaw(SDB_STB, STB_VER_NUMBER, size);
104
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
105 106

  int32_t dataPos = 0;
107 108 109 110 111 112
  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)
113 114
  SDB_SET_INT32(pRaw, dataPos, pStb->tagVer, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->colVer, _OVER)
S
Shengliang Guan 已提交
115
  SDB_SET_INT32(pRaw, dataPos, pStb->smaVer, _OVER)
116
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, _OVER)
117 118 119 120
  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)
121 122 123
  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 已提交
124
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfFuncs, _OVER)
125
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, _OVER)
126 127
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129 130
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
131
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
132
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
133 134 135
    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 已提交
136 137 138 139
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
140
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
141
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
142 143 144
    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 已提交
145 146
  }

S
Shengliang Guan 已提交
147 148 149 150 151
  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 已提交
152
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
153
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
154
  }
S
Shengliang Guan 已提交
155

156 157 158
  if (pStb->ast1Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
S
Shengliang Guan 已提交
159

160 161 162
  if (pStb->ast2Len > 0) {
    SDB_SET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
S
Shengliang Guan 已提交
163

S
Shengliang Guan 已提交
164
  SDB_SET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
165
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
166 167 168

  terrno = 0;

169
_OVER:
170 171 172 173 174
  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 已提交
175

176
  mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
177 178 179
  return pRaw;
}

S
Shengliang Guan 已提交
180
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
181
  terrno = TSDB_CODE_OUT_OF_MEMORY;
182 183
  SSdbRow *pRow = NULL;
  SStbObj *pStb = NULL;
184

S
Shengliang Guan 已提交
185
  int8_t sver = 0;
186
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
187

S
Shengliang Guan 已提交
188
  if (sver != STB_VER_NUMBER) {
S
Shengliang Guan 已提交
189
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
190
    goto _OVER;
S
Shengliang Guan 已提交
191 192
  }

193
  pRow = sdbAllocRow(sizeof(SStbObj));
194
  if (pRow == NULL) goto _OVER;
195

196
  pStb = sdbGetRowObj(pRow);
197
  if (pStb == NULL) goto _OVER;
S
Shengliang Guan 已提交
198 199

  int32_t dataPos = 0;
200 201 202 203 204 205
  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)
206 207
  SDB_GET_INT32(pRaw, dataPos, &pStb->tagVer, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->colVer, _OVER)
S
Shengliang Guan 已提交
208
  SDB_GET_INT32(pRaw, dataPos, &pStb->smaVer, _OVER)
209
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
210 211 212 213
  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)
214 215 216
  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 已提交
217
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfFuncs, _OVER)
218
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, _OVER)
219 220
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
221

wafwerar's avatar
wafwerar 已提交
222 223
  pStb->pColumns = taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
  pStb->pTags = taosMemoryCalloc(pStb->numOfTags, sizeof(SSchema));
S
Shengliang Guan 已提交
224
  pStb->pFuncs = taosArrayInit(pStb->numOfFuncs, TSDB_FUNC_NAME_LEN);
S
Shengliang Guan 已提交
225
  if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pFuncs == NULL) {
226
    goto _OVER;
S
Shengliang 已提交
227
  }
S
Shengliang Guan 已提交
228

S
Shengliang Guan 已提交
229 230
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
231
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
232
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
233 234 235
    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 已提交
236 237 238 239
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
240
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
241
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
242 243 244
    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 已提交
245 246
  }

S
Shengliang Guan 已提交
247 248 249 250 251 252
  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 已提交
253
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
254
    pStb->comment = taosMemoryCalloc(pStb->commentLen + 1, 1);
255
    if (pStb->comment == NULL) goto _OVER;
wmmhello's avatar
wmmhello 已提交
256
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
257
  }
S
Shengliang Guan 已提交
258

259 260 261 262 263
  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 已提交
264

265 266 267 268 269
  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 已提交
270
  SDB_GET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
271 272 273

  terrno = 0;

274
_OVER:
275
  if (terrno != 0) {
276 277 278 279 280 281
    mError("stb:%s, failed to decode from raw:%p since %s", pStb == NULL ? "null" : pStb->name, pRaw, terrstr());
    if (pStb != NULL) {
      taosMemoryFreeClear(pStb->pColumns);
      taosMemoryFreeClear(pStb->pTags);
      taosMemoryFreeClear(pStb->comment);
    }
wafwerar's avatar
wafwerar 已提交
282
    taosMemoryFreeClear(pRow);
283 284
    return NULL;
  }
S
Shengliang Guan 已提交
285

286
  mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
S
Shengliang Guan 已提交
287 288 289
  return pRow;
}

L
Liu Jicong 已提交
290 291 292 293 294 295 296 297 298
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 已提交
299
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
300
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
301 302 303
  return 0;
}

S
Shengliang Guan 已提交
304
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
305
  mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
L
Liu Jicong 已提交
306
  mndFreeStb(pStb);
S
Shengliang Guan 已提交
307 308 309
  return 0;
}

S
Shengliang Guan 已提交
310 311
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 已提交
312

S
Shengliang Guan 已提交
313
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
314 315

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
316
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
317
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
318
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
319
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
320 321 322 323 324 325 326 327
    } 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 已提交
328
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
329
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
330
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
331
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
332 333 334 335
    } 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 已提交
336
    }
S
Shengliang Guan 已提交
337 338
  }

339
  if (pOld->commentLen < pNew->commentLen && pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
340
    void *comment = taosMemoryMalloc(pNew->commentLen + 1);
S
Shengliang Guan 已提交
341
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
342
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
343 344 345 346 347 348 349
      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 已提交
350
  pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
351

352
  if (pOld->ast1Len < pNew->ast1Len) {
S
Shengliang Guan 已提交
353
    void *pAst1 = taosMemoryMalloc(pNew->ast1Len + 1);
354 355 356 357 358 359 360 361 362 363 364
    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 已提交
365
    void *pAst2 = taosMemoryMalloc(pNew->ast2Len + 1);
366 367 368 369 370 371 372 373 374 375
    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 已提交
376
  pOld->updateTime = pNew->updateTime;
377 378
  pOld->tagVer = pNew->tagVer;
  pOld->colVer = pNew->colVer;
379
  pOld->smaVer = pNew->smaVer;
380
  pOld->nextColId = pNew->nextColId;
S
Shengliang 已提交
381
  pOld->ttl = pNew->ttl;
382 383 384 385 386 387 388 389
  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));
  }
390
  if (pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
391
    memcpy(pOld->comment, pNew->comment, pNew->commentLen + 1);
S
Shengliang Guan 已提交
392
    pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
393
  }
394 395
  if (pNew->ast1Len != 0) {
    memcpy(pOld->pAst1, pNew->pAst1, pNew->ast1Len);
S
Shengliang Guan 已提交
396
    pOld->ast1Len = pNew->ast1Len;
397 398 399
  }
  if (pNew->ast2Len != 0) {
    memcpy(pOld->pAst2, pNew->pAst2, pNew->ast2Len);
S
Shengliang Guan 已提交
400
    pOld->ast2Len = pNew->ast2Len;
401
  }
S
Shengliang Guan 已提交
402
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
403 404 405
  return 0;
}

S
Shengliang Guan 已提交
406
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
407
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
408
  SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
S
Shengliang Guan 已提交
409
  if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
S
Shengliang Guan 已提交
410 411 412
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
  }
  return pStb;
S
Shengliang Guan 已提交
413 414
}

S
Shengliang Guan 已提交
415
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
S
Shengliang Guan 已提交
416 417 418 419
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pStb);
}

420
SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
S
Shengliang Guan 已提交
421 422
  SName name = {0};
  tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
S
Shengliang Guan 已提交
423

S
Shengliang Guan 已提交
424 425
  char db[TSDB_TABLE_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, db);
S
Shengliang Guan 已提交
426

S
Shengliang Guan 已提交
427 428
  return mndAcquireDb(pMnode, db);
}
S
Shengliang Guan 已提交
429

430
static FORCE_INLINE int32_t schemaExColIdCompare(const void *colId, const void *pSchema) {
H
Hongze Cheng 已提交
431
  if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
432
    return -1;
H
Hongze Cheng 已提交
433
  } else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
C
Cary Xu 已提交
434 435 436 437 438
    return 1;
  }
  return 0;
}

dengyihao's avatar
dengyihao 已提交
439 440
void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen, void *alterOriData,
                            int32_t alterOriDataLen) {
H
Hongze Cheng 已提交
441
  SEncoder       encoder = {0};
H
Hongze Cheng 已提交
442 443 444
  int32_t        contLen;
  SName          name = {0};
  SVCreateStbReq req = {0};
H
Hongze Cheng 已提交
445

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

S
Shengliang Guan 已提交
450
  req.name = (char *)tNameGetTableName(&name);
H
Hongze Cheng 已提交
451
  req.suid = pStb->uid;
452
  req.rollup = pStb->ast1Len > 0 ? 1 : 0;
wmmhello's avatar
wmmhello 已提交
453 454
  req.alterOriData = alterOriData;
  req.alterOriDataLen = alterOriDataLen;
455 456
  // todo
  req.schemaRow.nCols = pStb->numOfColumns;
457
  req.schemaRow.version = pStb->colVer;
458
  req.schemaRow.pSchema = pStb->pColumns;
H
Hongze Cheng 已提交
459
  req.schemaTag.nCols = pStb->numOfTags;
460
  req.schemaTag.version = pStb->tagVer;
H
Hongze Cheng 已提交
461
  req.schemaTag.pSchema = pStb->pTags;
H
more  
Hongze Cheng 已提交
462

C
Cary Xu 已提交
463
  if (req.rollup) {
C
Cary Xu 已提交
464 465
    req.rsmaParam.maxdelay[0] = pStb->maxdelay[0];
    req.rsmaParam.maxdelay[1] = pStb->maxdelay[1];
C
Cary Xu 已提交
466 467
    req.rsmaParam.watermark[0] = pStb->watermark[0];
    req.rsmaParam.watermark[1] = pStb->watermark[1];
C
Cary Xu 已提交
468
    if (pStb->ast1Len > 0) {
C
Cary Xu 已提交
469
      if (mndConvertRsmaTask(&req.rsmaParam.qmsg[0], &req.rsmaParam.qmsgLen[0], pStb->pAst1, pStb->uid,
470 471
                             STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[0],
                             req.rsmaParam.deleteMark[0]) < 0) {
472
        goto _err;
C
Cary Xu 已提交
473
      }
C
Cary Xu 已提交
474 475
    }
    if (pStb->ast2Len > 0) {
C
Cary Xu 已提交
476
      if (mndConvertRsmaTask(&req.rsmaParam.qmsg[1], &req.rsmaParam.qmsgLen[1], pStb->pAst2, pStb->uid,
477 478
                             STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[1],
                             req.rsmaParam.deleteMark[1]) < 0) {
479
        goto _err;
C
Cary Xu 已提交
480
      }
C
Cary Xu 已提交
481
    }
H
Hongze Cheng 已提交
482
  }
H
Hongze Cheng 已提交
483
  // get length
wafwerar's avatar
wafwerar 已提交
484 485 486
  int32_t ret = 0;
  tEncodeSize(tEncodeSVCreateStbReq, &req, contLen, ret);
  if (ret < 0) {
487
    goto _err;
H
Hongze Cheng 已提交
488 489
  }

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

D
dapan1121 已提交
492
  SMsgHead *pHead = taosMemoryCalloc(1, contLen);
S
Shengliang Guan 已提交
493
  if (pHead == NULL) {
H
more  
Hongze Cheng 已提交
494
    terrno = TSDB_CODE_OUT_OF_MEMORY;
495
    goto _err;
H
more  
Hongze Cheng 已提交
496 497
  }

S
Shengliang Guan 已提交
498 499
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);
H
more  
Hongze Cheng 已提交
500

S
Shengliang Guan 已提交
501
  void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
H
Hongze Cheng 已提交
502 503
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  if (tEncodeSVCreateStbReq(&encoder, &req) < 0) {
504 505
    taosMemoryFreeClear(pHead);
    tEncoderClear(&encoder);
506
    goto _err;
H
Hongze Cheng 已提交
507
  }
H
Hongze Cheng 已提交
508
  tEncoderClear(&encoder);
H
more  
Hongze Cheng 已提交
509

S
Shengliang Guan 已提交
510
  *pContLen = contLen;
C
Cary Xu 已提交
511 512
  taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
  taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
S
Shengliang Guan 已提交
513
  return pHead;
514
_err:
C
Cary Xu 已提交
515 516
  taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
  taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
517
  return NULL;
518 519
}

S
Shengliang Guan 已提交
520
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
H
Hongze Cheng 已提交
521 522 523 524 525
  SName        name = {0};
  SVDropStbReq req = {0};
  int32_t      contLen = 0;
  int32_t      ret = 0;
  SMsgHead    *pHead = NULL;
H
Hongze Cheng 已提交
526
  SEncoder     encoder = {0};
H
Hongze Cheng 已提交
527

S
Shengliang Guan 已提交
528 529 530 531
  tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

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

H
Hongze Cheng 已提交
533 534 535 536 537
  tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
  if (ret < 0) return NULL;

  contLen += sizeof(SMsgHead);
  pHead = taosMemoryMalloc(contLen);
S
Shengliang Guan 已提交
538
  if (pHead == NULL) {
539 540 541 542
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
543 544 545 546
  pHead->contLen = htonl(contLen);
  pHead->vgId = htonl(pVgroup->vgId);

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

H
Hongze Cheng 已提交
548 549 550
  tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
  tEncodeSVDropStbReq(&encoder, &req);
  tEncoderClear(&encoder);
551

S
Shengliang Guan 已提交
552 553
  *pContLen = contLen;
  return pHead;
554 555
}

556
int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
S
Shengliang Guan 已提交
557
  if (pCreate->igExists < 0 || pCreate->igExists > 1) {
S
Shengliang Guan 已提交
558
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
559 560
    return -1;
  }
S
Shengliang Guan 已提交
561

562
  if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfTags + pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
5
54liuyao 已提交
563
    terrno = TSDB_CODE_PAR_INVALID_COLUMNS_NUM;
S
Shengliang Guan 已提交
564 565
    return -1;
  }
S
Shengliang Guan 已提交
566

S
Shengliang Guan 已提交
567
  if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
S
Shengliang Guan 已提交
568
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
569 570
    return -1;
  }
S
Shengliang Guan 已提交
571

S
Shengliang Guan 已提交
572
  SField *pField = taosArrayGet(pCreate->pColumns, 0);
S
Shengliang Guan 已提交
573
  if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
5
54liuyao 已提交
574
    terrno = TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
S
Shengliang Guan 已提交
575 576 577
    return -1;
  }

S
Shengliang Guan 已提交
578
  for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
579
    SField *pField1 = taosArrayGet(pCreate->pColumns, i);
S
Shengliang Guan 已提交
580
    if (pField1->type >= TSDB_DATA_TYPE_MAX) {
S
Shengliang Guan 已提交
581 582 583
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
584
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
585 586 587
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
588
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
589 590 591 592 593 594
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
  }

  for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
595
    SField *pField1 = taosArrayGet(pCreate->pTags, i);
S
Shengliang Guan 已提交
596
    if (pField1->type >= TSDB_DATA_TYPE_MAX) {
S
Shengliang Guan 已提交
597
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
598 599
      return -1;
    }
600
    if (pField1->bytes <= 0) {
S
Shengliang Guan 已提交
601
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
602 603
      return -1;
    }
604
    if (pField1->name[0] == 0) {
S
Shengliang Guan 已提交
605
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
606 607 608
      return -1;
    }
  }
S
Shengliang Guan 已提交
609

S
Shengliang Guan 已提交
610 611 612
  return 0;
}

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

  return 0;
}

625
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
626 627
  SSdbRaw *pUndoRaw = mndStbActionEncode(pStb);
  if (pUndoRaw == NULL) return -1;
S
Shengliang Guan 已提交
628 629 630 631
  if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
    sdbFreeRaw(pUndoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
632 633 634 635 636
  if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

637
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
638 639
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
640 641 642 643
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
644 645 646 647 648
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

649
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
650
  SSdb   *pSdb = pMnode->pSdb;
651
  SVgObj *pVgroup = NULL;
652
  void   *pIter = NULL;
S
Shengliang Guan 已提交
653
  int32_t contLen;
654 655 656 657

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
658
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
659 660 661 662
      sdbRelease(pSdb, pVgroup);
      continue;
    }

wmmhello's avatar
wmmhello 已提交
663
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, NULL, 0);
S
Shengliang Guan 已提交
664
    if (pReq == NULL) {
665 666 667 668
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
S
Shengliang Guan 已提交
669

670
    STransAction action = {0};
dengyihao's avatar
dengyihao 已提交
671
    action.mTraceId = pTrans->mTraceId;
672
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
673
    action.pCont = pReq;
S
Shengliang Guan 已提交
674
    action.contLen = contLen;
H
Hongze Cheng 已提交
675
    action.msgType = TDMT_VND_CREATE_STB;
S
Shengliang Guan 已提交
676
    action.acceptableCode = TSDB_CODE_TDB_STB_ALREADY_EXIST;
677
    action.retryCode = TSDB_CODE_TDB_STB_NOT_EXIST;
678
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
679
      taosMemoryFree(pReq);
680 681 682 683 684 685
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
686 687 688 689

  return 0;
}

690
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
691
  SSdb   *pSdb = pMnode->pSdb;
692
  SVgObj *pVgroup = NULL;
693
  void   *pIter = NULL;
694 695 696 697

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
698
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
699 700 701 702
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
703
    int32_t contLen = 0;
S
Shengliang Guan 已提交
704
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
705
    if (pReq == NULL) {
706 707 708 709 710
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
711

712 713
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
714
    action.pCont = pReq;
S
Shengliang Guan 已提交
715
    action.contLen = contLen;
H
Hongze Cheng 已提交
716
    action.msgType = TDMT_VND_DROP_STB;
S
Shengliang Guan 已提交
717
    action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
718
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
719
      taosMemoryFree(pReq);
720 721 722 723 724 725
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
726 727 728 729

  return 0;
}

S
sma  
Shengliang Guan 已提交
730
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
731
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
732
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
733
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
734 735 736 737 738 739
      return pSchema;
    }
  }
  return NULL;
}

740 741 742 743 744
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;
745 746
  pDst->uid =
      (pCreate->source == TD_REQ_FROM_TAOX) ? pCreate->suid : mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
747
  pDst->dbUid = pDb->uid;
748 749
  pDst->tagVer = 1;
  pDst->colVer = 1;
S
Shengliang Guan 已提交
750
  pDst->smaVer = 1;
751
  pDst->nextColId = 1;
752 753 754 755
  pDst->maxdelay[0] = pCreate->delay1;
  pDst->maxdelay[1] = pCreate->delay2;
  pDst->watermark[0] = pCreate->watermark1;
  pDst->watermark[1] = pCreate->watermark2;
756 757 758
  pDst->ttl = pCreate->ttl;
  pDst->numOfColumns = pCreate->numOfColumns;
  pDst->numOfTags = pCreate->numOfTags;
S
Shengliang Guan 已提交
759
  pDst->numOfFuncs = pCreate->numOfFuncs;
760
  pDst->commentLen = pCreate->commentLen;
D
dapan1121 已提交
761 762
  pDst->pFuncs = pCreate->pFuncs;
  pCreate->pFuncs = NULL;
763

764
  if (pDst->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
765
    pDst->comment = taosMemoryCalloc(pDst->commentLen + 1, 1);
766
    if (pDst->comment == NULL) {
S
sma  
Shengliang Guan 已提交
767 768 769
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
770
    memcpy(pDst->comment, pCreate->pComment, pDst->commentLen + 1);
S
sma  
Shengliang Guan 已提交
771
  }
S
Shengliang Guan 已提交
772

773 774 775 776
  pDst->ast1Len = pCreate->ast1Len;
  if (pDst->ast1Len > 0) {
    pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
    if (pDst->pAst1 == NULL) {
777 778 779
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
780
    memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
781 782
  }

783 784 785 786
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
787 788 789
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
790
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
791 792
  }

793 794 795
  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 已提交
796 797 798
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
799

D
dmchen 已提交
800 801 802 803 804
  if(pDst->nextColId > 0 && pDst->nextColId < 0x7fff - pDst->numOfColumns - pDst->numOfTags){
    terrno = TSDB_CODE_MND_BIG_FIELD_VALUE;
    return -1;
  }

805
  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
S
Shengliang Guan 已提交
806
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
807
    SSchema *pSchema = &pDst->pColumns[i];
S
Shengliang Guan 已提交
808 809
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
810
    pSchema->flags = pField->flags;
S
Shengliang Guan 已提交
811
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
812 813
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
814 815
  }

816
  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
S
Shengliang Guan 已提交
817
    SField  *pField = taosArrayGet(pCreate->pTags, i);
818
    SSchema *pSchema = &pDst->pTags[i];
S
Shengliang Guan 已提交
819 820
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
dengyihao's avatar
dengyihao 已提交
821
    if (i == 0) {
dengyihao's avatar
dengyihao 已提交
822
      SSCHMEA_SET_IDX_ON(pSchema);
dengyihao's avatar
dengyihao 已提交
823
    }
S
Shengliang Guan 已提交
824
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
825 826
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
827
  }
828 829 830
  return 0;
}

S
Shengliang Guan 已提交
831
static int32_t mndCreateStb(SMnode *pMnode, SRpcMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
832
  SStbObj stbObj = {0};
S
Shengliang 已提交
833
  int32_t code = -1;
834

835
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq, "create-stb");
836
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
837

838
  mInfo("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
839
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
840
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;
841
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
842 843
  code = 0;

844
_OVER:
S
Shengliang Guan 已提交
845
  mndTransDrop(pTrans);
846
  mndStbActionDelete(pMnode->pSdb, &stbObj);
S
Shengliang Guan 已提交
847
  return code;
S
Shengliang Guan 已提交
848
}
S
Shengliang Guan 已提交
849

850
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
851
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
852
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) return -1;
853 854 855 856 857 858 859
  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 已提交
860

S
Shengliang Guan 已提交
861
static int32_t mndProcessTtlTimer(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
862 863 864 865 866 867 868
  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 已提交
869

870 871
  mInfo("start to process ttl timer");

wmmhello's avatar
wmmhello 已提交
872 873 874 875
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
876
    SMsgHead *pHead = rpcMallocCont(contLen);
wmmhello's avatar
wmmhello 已提交
877
    if (pHead == NULL) {
S
Shengliang Guan 已提交
878
      sdbCancelFetch(pSdb, pVgroup);
wmmhello's avatar
wmmhello 已提交
879 880 881 882 883
      sdbRelease(pSdb, pVgroup);
      continue;
    }
    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(pVgroup->vgId);
S
Shengliang Guan 已提交
884
    tSerializeSVDropTtlTableReq((char *)pHead + sizeof(SMsgHead), contLen, &ttlReq);
wmmhello's avatar
wmmhello 已提交
885

dengyihao's avatar
dengyihao 已提交
886
    SRpcMsg rpcMsg = {.msgType = TDMT_VND_DROP_TTL_TABLE, .pCont = pHead, .contLen = contLen, .info = pReq->info};
S
Shengliang Guan 已提交
887
    SEpSet  epSet = mndGetVgroupEpset(pMnode, pVgroup);
wmmhello's avatar
wmmhello 已提交
888
    int32_t code = tmsgSendReq(&epSet, &rpcMsg);
S
Shengliang Guan 已提交
889
    if (code != 0) {
S
Shengliang Guan 已提交
890
      mError("vgId:%d, failed to send drop ttl table request to vnode since 0x%x", pVgroup->vgId, code);
S
Shengliang Guan 已提交
891
    } else {
892
      mInfo("vgId:%d, send drop ttl table request to vnode, time:%d", pVgroup->vgId, ttlReq.timestamp);
wmmhello's avatar
wmmhello 已提交
893 894 895
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
896

wmmhello's avatar
wmmhello 已提交
897 898 899
  return 0;
}

wmmhello's avatar
wmmhello 已提交
900 901
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
X
Xiaoyu Wang 已提交
902
    if (strcmp(pStb->pTags[tag].name, tagName) == 0) {
wmmhello's avatar
wmmhello 已提交
903 904 905 906 907 908 909 910 911
      return tag;
    }
  }

  return -1;
}

static int32_t mndFindSuperTableColumnIndex(const SStbObj *pStb, const char *colName) {
  for (int32_t col = 0; col < pStb->numOfColumns; col++) {
X
Xiaoyu Wang 已提交
912
    if (strcmp(pStb->pColumns[col].name, colName) == 0) {
wmmhello's avatar
wmmhello 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
      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;
  }

D
dmchen 已提交
935 936 937 938 939
  if(pDst->nextColId > 0 && pDst->nextColId < 0x7fff - pDst->numOfColumns - pDst->numOfTags){
    terrno = TSDB_CODE_MND_BIG_FIELD_VALUE;
    return -1;
  }

wmmhello's avatar
wmmhello 已提交
940 941 942 943 944 945 946 947
  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);
948
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
949
      pSchema->colId = pStb->pColumns[cIndex].colId;
950
    } else {
wmmhello's avatar
wmmhello 已提交
951 952 953 954 955 956 957 958 959 960 961
      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);
962
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
963
      pSchema->colId = pStb->pTags[cIndex].colId;
964
    } else {
wmmhello's avatar
wmmhello 已提交
965 966 967 968 969 970 971 972
      pSchema->colId = pDst->nextColId++;
    }
  }
  pDst->tagVer = createReq->tagVer;
  pDst->colVer = createReq->colVer;
  return TSDB_CODE_SUCCESS;
}

973
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
974
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
975 976 977 978
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
  SMCreateStbReq createReq = {0};
979
  bool           isAlter = false;
S
Shengliang Guan 已提交
980

S
Shengliang Guan 已提交
981
  if (tDeserializeSMCreateStbReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
982
    terrno = TSDB_CODE_INVALID_MSG;
983
    goto _OVER;
S
Shengliang Guan 已提交
984
  }
S
Shengliang Guan 已提交
985

986
  mInfo("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
987 988
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
989
    goto _OVER;
S
Shengliang Guan 已提交
990
  }
S
Shengliang Guan 已提交
991

S
Shengliang Guan 已提交
992
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
993
  if (pStb != NULL) {
S
Shengliang Guan 已提交
994
    if (createReq.igExists) {
995
      if (createReq.source == TD_REQ_FROM_APP) {
996
        mInfo("stb:%s, already exist, ignore exist is set", createReq.name);
997 998 999
        code = 0;
        goto _OVER;
      } else if (pStb->uid != createReq.suid) {
1000 1001
        mInfo("stb:%s, alter table does not need to be done, because table is deleted", createReq.name);
        code = 0;
1002 1003
        goto _OVER;
      } else if (createReq.tagVer > 0 || createReq.colVer > 0) {
wmmhello's avatar
wmmhello 已提交
1004 1005 1006
        int32_t tagDelta = createReq.tagVer - pStb->tagVer;
        int32_t colDelta = createReq.colVer - pStb->colVer;
        int32_t verDelta = tagDelta + colDelta;
1007 1008 1009 1010 1011 1012 1013
        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)) {
1014
          isAlter = true;
1015 1016 1017 1018 1019 1020 1021
          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 {
S
Shengliang Guan 已提交
1022 1023
        mError("stb:%s, already exist while create, input tagVer:%d colVer:%d is invalid, origin tagVer:%d colVer:%d",
               createReq.name, createReq.tagVer, createReq.colVer, pStb->tagVer, pStb->colVer);
1024 1025 1026
        terrno = TSDB_CODE_MND_INVALID_SCHEMA_VER;
        goto _OVER;
      }
S
Shengliang Guan 已提交
1027 1028
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
1029
      goto _OVER;
S
Shengliang Guan 已提交
1030
    }
S
Shengliang Guan 已提交
1031
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
1032
    goto _OVER;
1033
  } else if (createReq.source == TD_REQ_FROM_TAOX && (createReq.tagVer != 1 || createReq.colVer != 1)) {
1034 1035 1036
    mInfo("stb:%s, alter table does not need to be done, because table is deleted", createReq.name);
    code = 0;
    goto _OVER;
S
Shengliang Guan 已提交
1037 1038
  }

S
Shengliang Guan 已提交
1039
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
1040 1041
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1042
    goto _OVER;
S
Shengliang Guan 已提交
1043 1044
  }

1045
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1046
    goto _OVER;
S
Shengliang Guan 已提交
1047 1048
  }

1049
  int32_t numOfStbs = -1;
S
Shengliang Guan 已提交
1050 1051 1052 1053
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
    goto _OVER;
  }

L
Liu Jicong 已提交
1054
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
1055 1056 1057 1058
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

wafwerar's avatar
wafwerar 已提交
1059 1060 1061 1062 1063
  if ((terrno = grantCheck(TSDB_GRANT_STABLE)) < 0) {
    code = -1;
    goto _OVER;
  }

1064
  if (isAlter) {
1065
    bool    needRsp = false;
wmmhello's avatar
wmmhello 已提交
1066
    SStbObj pDst = {0};
wmmhello's avatar
wmmhello 已提交
1067 1068 1069
    if (mndBuildStbFromAlter(pStb, &pDst, &createReq) != 0) {
      taosMemoryFreeClear(pDst.pTags);
      taosMemoryFreeClear(pDst.pColumns);
wmmhello's avatar
wmmhello 已提交
1070 1071 1072 1073 1074 1075
      goto _OVER;
    }

    code = mndAlterStbImp(pMnode, pReq, pDb, &pDst, needRsp, NULL, 0);
    taosMemoryFreeClear(pDst.pTags);
    taosMemoryFreeClear(pDst.pColumns);
1076 1077 1078
  } else {
    code = mndCreateStb(pMnode, pReq, &createReq, pDb);
  }
S
Shengliang Guan 已提交
1079
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1080

1081
_OVER:
S
Shengliang Guan 已提交
1082
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1083
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
1084 1085
  }

S
Shengliang Guan 已提交
1086 1087
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1088
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
1089 1090

  return code;
S
Shengliang Guan 已提交
1091 1092
}

S
Shengliang Guan 已提交
1093
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
1094 1095
  if (pAlter->commentLen >= 0) return 0;
  if (pAlter->ttl != 0) return 0;
S
Shengliang 已提交
1096

S
Shengliang Guan 已提交
1097 1098 1099 1100
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
1101

S
Shengliang Guan 已提交
1102 1103 1104
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
1105 1106 1107
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
1108 1109 1110 1111 1112
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
1113
int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) {
wafwerar's avatar
wafwerar 已提交
1114 1115
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
  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 已提交
1126 1127
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
                                         int32_t ttl) {
S
Shengliang 已提交
1128 1129
  if (commentLen > 0) {
    pNew->commentLen = commentLen;
wmmhello's avatar
wmmhello 已提交
1130
    pNew->comment = taosMemoryCalloc(1, commentLen + 1);
S
Shengliang 已提交
1131 1132 1133 1134
    if (pNew->comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
wmmhello's avatar
wmmhello 已提交
1135
    memcpy(pNew->comment, pComment, commentLen + 1);
1136
  } else if (commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1137
    pNew->commentLen = 0;
S
Shengliang Guan 已提交
1138
  } else {
S
Shengliang 已提交
1139
  }
wmmhello's avatar
wmmhello 已提交
1140

S
Shengliang 已提交
1141 1142 1143
  if (ttl >= 0) {
    pNew->ttl = ttl;
  }
S
Shengliang 已提交
1144 1145 1146 1147 1148 1149 1150

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

S
Shengliang Guan 已提交
1151
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  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 已提交
1162 1163 1164 1165 1166
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

D
dmchen 已提交
1167 1168 1169 1170 1171
  if(pNew->nextColId > 0 && pNew->nextColId < 0x7fff - ntags){
    terrno = TSDB_CODE_MND_BIG_FIELD_VALUE;
    return -1;
  }

S
Shengliang Guan 已提交
1172
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
1173
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1174
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1175
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1176 1177 1178
      return -1;
    }

S
Shengliang 已提交
1179
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1180
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1181 1182 1183
      return -1;
    }

S
Shengliang Guan 已提交
1184 1185 1186 1187
    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 已提交
1188 1189
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1190

1191
    mInfo("stb:%s, start to add tag %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1192 1193
  }

1194
  pNew->tagVer++;
S
Shengliang Guan 已提交
1195 1196 1197
  return 0;
}

1198
static int32_t mndCheckAlterColForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
1199 1200 1201 1202 1203 1204 1205
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SMqTopicObj *pTopic = NULL;
    pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
    if (pIter == NULL) break;

1206
    mInfo("topic:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, subType:%d sql:%s",
H
Hongze Cheng 已提交
1207
          pTopic->name, stbFullName, suid, colId, pTopic->subType, pTopic->sql);
1208 1209 1210 1211 1212 1213 1214
    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
      sdbRelease(pSdb, pTopic);
      continue;
    }

    SNode *pAst = NULL;
    if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
S
Shengliang Guan 已提交
1215 1216 1217
      terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TOPIC;
      mError("topic:%s, create ast error", pTopic->name);
      sdbRelease(pSdb, pTopic);
1218 1219 1220 1221 1222 1223 1224 1225
      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;
H
Hongze Cheng 已提交
1226 1227
      mInfo("topic:%s, check colId:%d tableId:%" PRId64 " ctbStbUid:%" PRId64, pTopic->name, pCol->colId, pCol->tableId,
            pTopic->ctbStbUid);
1228 1229

      if (pCol->tableId != suid && pTopic->ctbStbUid != suid) {
1230
        mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
1231 1232 1233 1234 1235
        goto NEXT;
      }
      if (pCol->colId > 0 && pCol->colId == colId) {
        terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TOPIC;
        mError("topic:%s, check colId:%d conflicted", pTopic->name, pCol->colId);
L
Liu Jicong 已提交
1236 1237 1238
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pTopic);
1239 1240
        return -1;
      }
1241
      mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
1242 1243 1244 1245 1246
    }

  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1247
    nodesDestroyList(pNodeList);
1248
  }
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  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) {
S
Shengliang Guan 已提交
1262 1263 1264
      terrno = TSDB_CODE_MND_INVALID_STREAM_OPTION;
      mError("stream:%s, create ast error", pStream->name);
      sdbRelease(pSdb, pStream);
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
      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) {
1275
        mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
1276 1277 1278 1279 1280
        goto NEXT;
      }
      if (pCol->colId > 0 && pCol->colId == colId) {
        terrno = TSDB_CODE_MND_STREAM_MUST_BE_DELETED;
        mError("stream:%s, check colId:%d conflicted", pStream->name, pCol->colId);
L
Liu Jicong 已提交
1281 1282 1283
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pStream);
1284 1285
        return -1;
      }
1286
      mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
1287 1288 1289 1290 1291
    }

  NEXT:
    sdbRelease(pSdb, pStream);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1292
    nodesDestroyList(pNodeList);
1293 1294 1295
  }
  return 0;
}
1296

1297 1298 1299
static int32_t mndCheckAlterColForTSma(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
1300 1301 1302 1303 1304
  while (1) {
    SSmaObj *pSma = NULL;
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
    if (pIter == NULL) break;

H
Hongze Cheng 已提交
1305 1306
    mInfo("tsma:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, sql:%s", pSma->name, stbFullName,
          suid, colId, pSma->sql);
1307 1308 1309 1310 1311

    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",
1312
             pSma->name, stbFullName, suid, colId);
1313 1314 1315 1316 1317 1318 1319 1320
      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;
1321
      mInfo("tsma:%s, check colId:%d tableId:%" PRId64, pSma->name, pCol->colId, pCol->tableId);
1322 1323

      if ((pCol->tableId != suid) && (pSma->stbUid != suid)) {
1324
        mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
1325
        goto NEXT;
1326 1327 1328 1329
      }
      if ((pCol->colId) > 0 && (pCol->colId == colId)) {
        terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TSMA;
        mError("tsma:%s, check colId:%d conflicted", pSma->name, pCol->colId);
L
Liu Jicong 已提交
1330 1331 1332
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pSma);
1333 1334
        return -1;
      }
1335
      mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
1336 1337
    }

1338
  NEXT:
1339 1340
    sdbRelease(pSdb, pSma);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1341
    nodesDestroyList(pNodeList);
1342
  }
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
  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;
  }
1353

1354 1355 1356
  if (mndCheckAlterColForTSma(pMnode, stbFullName, suid, colId) < 0) {
    return -1;
  }
1357 1358 1359
  return 0;
}

1360
static int32_t mndDropSuperTableTag(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
S
Shengliang Guan 已提交
1361 1362 1363 1364 1365 1366
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1367
  col_id_t colId = pOld->pTags[tag].colId;
1368
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1369 1370 1371
    return -1;
  }

S
Shengliang Guan 已提交
1372 1373 1374 1375 1376
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1379
  pNew->tagVer++;
dengyihao's avatar
dengyihao 已提交
1380 1381 1382 1383

  // if (mndDropIndexByTag(pMnode, pOld, tagName) != 0) {
  //   return -1;
  // }
1384
  mInfo("stb:%s, start to drop tag %s", pNew->name, tagName);
S
Shengliang Guan 已提交
1385 1386 1387
  return 0;
}

1388
static int32_t mndAlterStbTagName(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
S
Shengliang Guan 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
  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 已提交
1400 1401 1402 1403 1404 1405
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1406
  col_id_t colId = pOld->pTags[tag].colId;
1407
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1408 1409 1410
    return -1;
  }

S
Shengliang Guan 已提交
1411
  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
S
Shengliang Guan 已提交
1412
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1413 1414 1415
    return -1;
  }

S
Shengliang Guan 已提交
1416 1417
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    return -1;
  }

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

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

1428
  pNew->tagVer++;
1429
  mInfo("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
S
Shengliang Guan 已提交
1430 1431 1432
  return 0;
}

1433
static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1434
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1435 1436 1437 1438 1439
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1440
  col_id_t colId = pOld->pTags[tag].colId;
1441
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1442 1443 1444
    return -1;
  }

S
Shengliang Guan 已提交
1445 1446 1447 1448 1449 1450
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pTag = pNew->pTags + tag;

S
Shengliang Guan 已提交
1451 1452 1453 1454 1455
  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 已提交
1456
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1457 1458 1459 1460
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1461
  pTag->bytes = pField->bytes;
1462
  pNew->tagVer++;
S
Shengliang Guan 已提交
1463

1464
  mInfo("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1465 1466 1467
  return 0;
}

S
Shengliang Guan 已提交
1468
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1469 1470 1471 1472 1473
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1474 1475 1476 1477 1478
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

D
dmchen 已提交
1479 1480 1481 1482 1483
  if(pNew->nextColId > 0 && pNew->nextColId < 0x7fff - ncols){
    terrno = TSDB_CODE_MND_BIG_FIELD_VALUE;
    return -1;
  }

S
Shengliang Guan 已提交
1484
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1485
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1486
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1487
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1488 1489 1490
      return -1;
    }

S
Shengliang 已提交
1491
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1492
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1493 1494 1495
      return -1;
    }

S
Shengliang Guan 已提交
1496 1497 1498 1499
    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 已提交
1500 1501
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1502

1503
    mInfo("stb:%s, start to add column %s", pNew->name, pSchema->name);
S
Shengliang Guan 已提交
1504 1505
  }

1506
  pNew->colVer++;
S
Shengliang Guan 已提交
1507 1508 1509
  return 0;
}

1510
static int32_t mndDropSuperTableColumn(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *colName) {
S
Shengliang Guan 已提交
1511
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1512
  if (col < 0) {
S
Shengliang Guan 已提交
1513 1514 1515 1516
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
  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;
  }

1527
  col_id_t colId = pOld->pColumns[col].colId;
1528
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1529 1530 1531
    return -1;
  }

S
Shengliang Guan 已提交
1532 1533 1534 1535 1536
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1539
  pNew->colVer++;
1540
  mInfo("stb:%s, start to drop col %s", pNew->name, colName);
S
Shengliang Guan 已提交
1541 1542 1543
  return 0;
}

1544
static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1545
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1546 1547 1548 1549 1550 1551 1552
  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 已提交
1553
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1554 1555 1556 1557 1558 1559 1560
  }

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

1561
  col_id_t colId = pOld->pColumns[col].colId;
1562
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1563 1564 1565
    return -1;
  }

S
Shengliang Guan 已提交
1566 1567 1568 1569 1570
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pCol = pNew->pColumns + col;
S
Shengliang Guan 已提交
1571 1572
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1573 1574 1575
    return -1;
  }

S
Shengliang Guan 已提交
1576
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1577
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1578 1579 1580
    return -1;
  }

S
Shengliang Guan 已提交
1581
  pCol->bytes = pField->bytes;
1582
  pNew->colVer++;
S
Shengliang Guan 已提交
1583

1584
  mInfo("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1585 1586 1587
  return 0;
}

S
Shengliang Guan 已提交
1588
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1589 1590
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
1591 1592 1593 1594
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1595
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1596 1597 1598 1599

  return 0;
}

S
Shengliang Guan 已提交
1600
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1601 1602
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
1603 1604 1605 1606
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1607 1608 1609 1610 1611
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

1612 1613
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, void *alterOriData,
                                         int32_t alterOriDataLen) {
S
Shengliang Guan 已提交
1614 1615 1616 1617 1618 1619 1620 1621
  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 已提交
1622
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
1623 1624 1625 1626
      sdbRelease(pSdb, pVgroup);
      continue;
    }

wmmhello's avatar
wmmhello 已提交
1627
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, alterOriData, alterOriDataLen);
S
Shengliang Guan 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636
    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 已提交
1637
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1638
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1639
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
static int32_t mndSetAlterStbRedoActions2(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb,
                                          void *alterOriData, int32_t alterOriDataLen) {
  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;
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
      sdbRelease(pSdb, pVgroup);
      continue;
    }

    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, alterOriData, alterOriDataLen);
    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;
    action.msgType = TDMT_VND_CREATE_INDEX;
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
      taosMemoryFree(pReq);
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}
D
dapan1121 已提交
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
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;
  }

S
Shengliang Guan 已提交
1698 1699 1700
  tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
  tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
  tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
D
dapan1121 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
  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;
dengyihao's avatar
dengyihao 已提交
1725
    pSchema->flags = pSrcSchema->flags;
D
dapan1121 已提交
1726 1727 1728 1729 1730 1731 1732 1733
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

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

D
dapan1121 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
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;
  }

S
Shengliang Guan 已提交
1745 1746 1747
  tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
  tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
  tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
D
dapan1121 已提交
1748 1749 1750
  pRsp->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->tableType = TSDB_SUPER_TABLE;
D
dapan1121 已提交
1751 1752 1753 1754
  pRsp->delay1 = pStb->maxdelay[0];
  pRsp->delay2 = pStb->maxdelay[1];
  pRsp->watermark1 = pStb->watermark[0];
  pRsp->watermark2 = pStb->watermark[1];
D
dapan1121 已提交
1755 1756 1757
  pRsp->ttl = pStb->ttl;
  pRsp->commentLen = pStb->commentLen;
  if (pStb->commentLen > 0) {
1758
    pRsp->pComment = taosStrdup(pStb->comment);
D
dapan1121 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
  }

  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 已提交
1779
  if (pStb->numOfFuncs > 0) {
H
Haojun Liao 已提交
1780
    pRsp->pFuncs = taosArrayDup(pStb->pFuncs, NULL);
D
dapan1121 已提交
1781
  }
1782

D
dapan1121 已提交
1783 1784 1785 1786
  taosRUnLockLatch(&pStb->lock);
  return 0;
}

L
Liu Jicong 已提交
1787 1788
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp,
                                 int32_t *smaVer) {
D
dapan1121 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
  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 已提交
1801
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
D
dapan1121 已提交
1802 1803 1804
    return -1;
  }

D
dapan1121 已提交
1805 1806 1807 1808
  if (smaVer) {
    *smaVer = pStb->smaVer;
  }

D
dapan1121 已提交
1809 1810 1811 1812 1813 1814
  int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}

D
dapan1121 已提交
1815
static int32_t mndBuildStbCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
  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 已提交
1831

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

1834 1835 1836 1837
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}
D
dapan1121 已提交
1838

1839
static int32_t mndBuildSMAlterStbRsp(SDbObj *pDb, SStbObj *pObj, void **pCont, int32_t *pLen) {
1840
  int32_t       ret;
D
dapan1121 已提交
1841
  SEncoder      ec = {0};
1842
  uint32_t      contLen = 0;
D
dapan1121 已提交
1843
  SMAlterStbRsp alterRsp = {0};
1844
  SName         name = {0};
1845
  tNameFromString(&name, pObj->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
D
dapan1121 已提交
1846 1847 1848 1849 1850 1851

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

D
dapan1121 已提交
1853
  ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, alterRsp.pMeta);
D
dapan1121 已提交
1854 1855 1856 1857
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }
1858

D
dapan1121 已提交
1859 1860 1861 1862 1863 1864
  tEncodeSize(tEncodeSMAlterStbRsp, &alterRsp, contLen, ret);
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }

1865
  void *cont = taosMemoryMalloc(contLen);
D
dapan1121 已提交
1866 1867 1868 1869 1870 1871 1872 1873
  tEncoderInit(&ec, cont, contLen);
  tEncodeSMAlterStbRsp(&ec, &alterRsp);
  tEncoderClear(&ec);

  tFreeSMAlterStbRsp(&alterRsp);

  *pCont = cont;
  *pLen = contLen;
1874

D
dapan1121 已提交
1875 1876 1877
  return 0;
}

L
Liu Jicong 已提交
1878 1879
int32_t mndBuildSMCreateStbRsp(SMnode *pMnode, char *dbFName, char *stbFName, void **pCont, int32_t *pLen) {
  int32_t ret = -1;
1880 1881 1882 1883 1884 1885 1886 1887 1888
  SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
  if (NULL == pDb) {
    return -1;
  }

  SStbObj *pObj = mndAcquireStb(pMnode, stbFName);
  if (NULL == pObj) {
    goto _OVER;
  }
L
Liu Jicong 已提交
1889 1890 1891

  SEncoder       ec = {0};
  uint32_t       contLen = 0;
1892
  SMCreateStbRsp stbRsp = {0};
L
Liu Jicong 已提交
1893
  SName          name = {0};
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
  tNameFromString(&name, pObj->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  stbRsp.pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
  if (NULL == stbRsp.pMeta) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _OVER;
  }

  ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, stbRsp.pMeta);
  if (ret) {
    tFreeSMCreateStbRsp(&stbRsp);
    goto _OVER;
  }

  tEncodeSize(tEncodeSMCreateStbRsp, &stbRsp, contLen, ret);
  if (ret) {
    tFreeSMCreateStbRsp(&stbRsp);
    goto _OVER;
  }

  void *cont = taosMemoryMalloc(contLen);
  tEncoderInit(&ec, cont, contLen);
  tEncodeSMCreateStbRsp(&ec, &stbRsp);
  tEncoderClear(&ec);

  tFreeSMCreateStbRsp(&stbRsp);

  *pCont = cont;
  *pLen = contLen;

  ret = 0;
L
Liu Jicong 已提交
1925

1926 1927 1928 1929
_OVER:
  if (pObj) {
    mndReleaseStb(pMnode, pObj);
  }
L
Liu Jicong 已提交
1930

1931 1932 1933 1934 1935 1936 1937
  if (pDb) {
    mndReleaseDb(pMnode, pDb);
  }

  return ret;
}

1938 1939
static int32_t mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                              void *alterOriData, int32_t alterOriDataLen) {
1940
  int32_t code = -1;
1941
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "alter-stb");
1942 1943
  if (pTrans == NULL) goto _OVER;

1944
  mInfo("trans:%d, used to alter stb:%s", pTrans->id, pStb->name);
1945
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
1946
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956

  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 已提交
1957
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, pStb, alterOriData, alterOriDataLen) != 0) goto _OVER;
1958 1959 1960 1961 1962 1963 1964 1965 1966
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  code = 0;

_OVER:
  mndTransDrop(pTrans);
  return code;
}

dengyihao's avatar
dengyihao 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
static int32_t mndAlterStbAndUpdateTagIdxImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                                             void *alterOriData, int32_t alterOriDataLen, const SMAlterStbReq *pAlter) {
  int32_t code = -1;
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "alter-stb");
  if (pTrans == NULL) goto _OVER;

  mInfo("trans:%d, used to alter stb:%s", pTrans->id, pStb->name);
  mndTransSetDbName(pTrans, pDb->name, pStb->name);

  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
1978 1979 1980 1981 1982 1983 1984
  if (needRsp) {
    void   *pCont = NULL;
    int32_t contLen = 0;
    if (mndBuildSMAlterStbRsp(pDb, pStb, &pCont, &contLen) != 0) goto _OVER;
    mndTransSetRpcRsp(pTrans, pCont, contLen);
  }

dengyihao's avatar
dengyihao 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 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 2030 2031 2032 2033 2034
  if (pAlter->alterType == TSDB_ALTER_TABLE_DROP_TAG) {
    SIdxObj idxObj = {0};
    SField *pField0 = taosArrayGet(pAlter->pFields, 0);
    bool    exist = false;
    if (mndGetIdxsByTagName(pMnode, pStb, pField0->name, &idxObj) == 0) {
      exist = true;
    }
    if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
    if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;

    if (exist == true) {
      if (mndSetDropIdxRedoLogs(pMnode, pTrans, &idxObj) != 0) goto _OVER;
      if (mndSetDropIdxCommitLogs(pMnode, pTrans, &idxObj) != 0) goto _OVER;
    }

    if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, pStb, alterOriData, alterOriDataLen) != 0) goto _OVER;
    if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  } else if (pAlter->alterType == TSDB_ALTER_TABLE_UPDATE_TAG_NAME) {
    SIdxObj     idxObj = {0};
    SField     *pField0 = taosArrayGet(pAlter->pFields, 0);
    SField     *pField1 = taosArrayGet(pAlter->pFields, 1);
    const char *oTagName = pField0->name;
    const char *nTagName = pField1->name;
    bool        exist = false;

    if (mndGetIdxsByTagName(pMnode, pStb, pField0->name, &idxObj) == 0) {
      exist = true;
    }

    if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
    if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;

    if (exist == true) {
      memcpy(idxObj.colName, nTagName, strlen(nTagName));
      idxObj.colName[strlen(nTagName)] = 0;
      if (mndSetAlterIdxRedoLogs(pMnode, pTrans, &idxObj) != 0) goto _OVER;
      if (mndSetAlterIdxCommitLogs(pMnode, pTrans, &idxObj) != 0) goto _OVER;
    }

    if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, pStb, alterOriData, alterOriDataLen) != 0) goto _OVER;
    if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
  }
  code = 0;

_OVER:
  mndTransDrop(pTrans);
  return code;
}

S
Shengliang Guan 已提交
2035
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
2036 2037 2038 2039
  bool    needRsp = true;
  int32_t code = -1;
  SField *pField0 = NULL;

S
Shengliang Guan 已提交
2040 2041 2042
  SStbObj stbObj = {0};
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
2043
  taosRUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
2044 2045 2046
  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
D
dapan1121 已提交
2047
  stbObj.lock = 0;
dengyihao's avatar
dengyihao 已提交
2048
  bool updateTagIndex = false;
S
Shengliang Guan 已提交
2049
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
2050
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
2051
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
2052
      break;
S
Shengliang Guan 已提交
2053
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang 已提交
2054
      pField0 = taosArrayGet(pAlter->pFields, 0);
2055
      code = mndDropSuperTableTag(pMnode, pOld, &stbObj, pField0->name);
dengyihao's avatar
dengyihao 已提交
2056
      updateTagIndex = true;
S
Shengliang Guan 已提交
2057
      break;
S
Shengliang Guan 已提交
2058
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
2059
      code = mndAlterStbTagName(pMnode, pOld, &stbObj, pAlter->pFields);
dengyihao's avatar
dengyihao 已提交
2060
      updateTagIndex = true;
S
Shengliang Guan 已提交
2061
      break;
S
Shengliang Guan 已提交
2062
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang 已提交
2063
      pField0 = taosArrayGet(pAlter->pFields, 0);
2064
      code = mndAlterStbTagBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
2065 2066
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
2067
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
2068 2069
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang 已提交
2070
      pField0 = taosArrayGet(pAlter->pFields, 0);
2071
      code = mndDropSuperTableColumn(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
2072
      break;
S
Shengliang Guan 已提交
2073
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang 已提交
2074
      pField0 = taosArrayGet(pAlter->pFields, 0);
2075
      code = mndAlterStbColumnBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
2076
      break;
S
Shengliang 已提交
2077
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
D
dapan1121 已提交
2078
      needRsp = false;
S
Shengliang 已提交
2079
      code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
S
Shengliang 已提交
2080
      break;
S
Shengliang Guan 已提交
2081
    default:
D
dapan1121 已提交
2082
      needRsp = false;
S
Shengliang 已提交
2083
      terrno = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
2084 2085 2086
      break;
  }

2087
  if (code != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
2088 2089 2090 2091 2092
  if (updateTagIndex == false) {
    code = mndAlterStbImp(pMnode, pReq, pDb, &stbObj, needRsp, pReq->pCont, pReq->contLen);
  } else {
    code = mndAlterStbAndUpdateTagIdxImp(pMnode, pReq, pDb, &stbObj, needRsp, pReq->pCont, pReq->contLen, pAlter);
  }
S
Shengliang Guan 已提交
2093

2094
_OVER:
wafwerar's avatar
wafwerar 已提交
2095 2096
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
2097 2098 2099
  if (pAlter->commentLen > 0) {
    taosMemoryFreeClear(stbObj.comment);
  }
S
Shengliang Guan 已提交
2100 2101
  return code;
}
S
Shengliang Guan 已提交
2102

2103
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
2104
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2105 2106 2107 2108
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
2109

S
Shengliang Guan 已提交
2110
  if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
2111
    terrno = TSDB_CODE_INVALID_MSG;
2112
    goto _OVER;
S
Shengliang Guan 已提交
2113
  }
S
Shengliang Guan 已提交
2114

2115
  mInfo("stb:%s, start to alter", alterReq.name);
2116
  if (mndCheckAlterStbReq(&alterReq) != 0) goto _OVER;
S
Shengliang Guan 已提交
2117

S
Shengliang Guan 已提交
2118
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
2119 2120
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
2121
    goto _OVER;
S
Shengliang Guan 已提交
2122 2123
  }

S
Shengliang Guan 已提交
2124
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
2125 2126
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
2127
    goto _OVER;
S
Shengliang Guan 已提交
2128
  }
S
Shengliang Guan 已提交
2129

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

S
Shengliang Guan 已提交
2134
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
2135
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
2136

2137
_OVER:
S
Shengliang Guan 已提交
2138
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2139
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
2140 2141
  }

S
Shengliang Guan 已提交
2142 2143
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
2144
  tFreeSMAltertbReq(&alterReq);
S
Shengliang Guan 已提交
2145 2146

  return code;
S
Shengliang Guan 已提交
2147
}
S
Shengliang Guan 已提交
2148

S
Shengliang Guan 已提交
2149 2150 2151
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
2152 2153 2154 2155
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
2156 2157 2158 2159 2160 2161 2162 2163
  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 已提交
2164 2165 2166 2167
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
2168 2169 2170 2171 2172
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
2173 2174 2175 2176
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 已提交
2177

S
Shengliang Guan 已提交
2178 2179 2180
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
2181
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
2182 2183 2184 2185
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
2186
    int32_t contLen = 0;
S
Shengliang Guan 已提交
2187
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    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;
S
Shengliang Guan 已提交
2200
    action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
S
Shengliang Guan 已提交
2201
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
2202
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
2213
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
2214
  int32_t code = -1;
2215
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "drop-stb");
2216
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
2217

2218
  mInfo("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);
2219
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
2220
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
2221

2222 2223 2224
  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;
dengyihao's avatar
dengyihao 已提交
2225
  if (mndDropIdxsByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2226
  if (mndDropSmasByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2227
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
2228 2229
  code = 0;

2230
_OVER:
S
Shengliang Guan 已提交
2231
  mndTransDrop(pTrans);
S
Shengliang 已提交
2232
  return code;
S
Shengliang Guan 已提交
2233 2234
}

2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
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) {
S
Shengliang Guan 已提交
2257 2258 2259
      terrno = TSDB_CODE_MND_INVALID_TOPIC_OPTION;
      mError("topic:%s, create ast error", pTopic->name);
      sdbRelease(pSdb, pTopic);
2260 2261 2262 2263 2264 2265 2266 2267 2268
      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 已提交
2269
      if (pCol->tableId == suid) {
2270 2271
        sdbRelease(pSdb, pTopic);
        nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2272
        nodesDestroyList(pNodeList);
2273 2274 2275 2276 2277 2278 2279 2280
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2281
    nodesDestroyList(pNodeList);
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
  }
  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 已提交
2294 2295 2296 2297 2298 2299
    if (pStream->smaId != 0) {
      sdbRelease(pSdb, pStream);
      continue;
    }

    if (pStream->targetStbUid == suid) {
L
Liu Jicong 已提交
2300 2301 2302 2303
      sdbRelease(pSdb, pStream);
      return -1;
    }

2304 2305
    SNode *pAst = NULL;
    if (nodesStringToNode(pStream->ast, &pAst) != 0) {
S
Shengliang Guan 已提交
2306 2307 2308
      terrno = TSDB_CODE_MND_INVALID_STREAM_OPTION;
      mError("stream:%s, create ast error", pStream->name);
      sdbRelease(pSdb, pStream);
2309 2310 2311 2312 2313 2314 2315 2316 2317
      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 已提交
2318
      if (pCol->tableId == suid) {
2319 2320
        sdbRelease(pSdb, pStream);
        nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2321
        nodesDestroyList(pNodeList);
2322 2323 2324 2325 2326 2327 2328 2329
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pStream);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2330
    nodesDestroyList(pNodeList);
2331 2332 2333 2334
  }
  return 0;
}

dengyihao's avatar
dengyihao 已提交
2335
static int32_t mndProcessDropTtltbReq(SRpcMsg *pRsp) { return 0; }
wmmhello's avatar
wmmhello 已提交
2336

2337
static int32_t mndProcessDropStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
2338
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2339 2340 2341
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
2342
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
2343

S
Shengliang Guan 已提交
2344
  if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
2345
    terrno = TSDB_CODE_INVALID_MSG;
2346
    goto _OVER;
S
Shengliang Guan 已提交
2347
  }
S
Shengliang Guan 已提交
2348

2349
  mInfo("stb:%s, start to drop", dropReq.name);
S
Shengliang Guan 已提交
2350

S
Shengliang Guan 已提交
2351
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2352
  if (pStb == NULL) {
S
Shengliang Guan 已提交
2353
    if (dropReq.igNotExists) {
2354
      mInfo("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
2355
      code = 0;
2356
      goto _OVER;
S
Shengliang Guan 已提交
2357 2358
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
2359
      goto _OVER;
S
Shengliang Guan 已提交
2360 2361 2362
    }
  }

wmmhello's avatar
wmmhello 已提交
2363 2364
  if (dropReq.source == TD_REQ_FROM_TAOX && pStb->uid != dropReq.suid) {
    code = 0;
wmmhello's avatar
wmmhello 已提交
2365 2366 2367
    goto _OVER;
  }

S
Shengliang Guan 已提交
2368
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2369 2370
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
2371
    goto _OVER;
S
Shengliang Guan 已提交
2372 2373
  }

2374
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
2375
    goto _OVER;
S
Shengliang Guan 已提交
2376 2377
  }

2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
  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 已提交
2388
  code = mndDropStb(pMnode, pReq, pDb, pStb);
S
Shengliang Guan 已提交
2389
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
2390

2391
_OVER:
S
Shengliang Guan 已提交
2392
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2393
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
2394 2395
  }

S
Shengliang Guan 已提交
2396 2397 2398
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
S
Shengliang Guan 已提交
2399
}
S
Shengliang Guan 已提交
2400

S
Shengliang Guan 已提交
2401 2402
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2403 2404 2405
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
2406

2407 2408 2409 2410
  SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user);
  if (pUser == NULL) return 0;
  bool sysinfo = pUser->sysInfo;

S
Shengliang Guan 已提交
2411
  if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
2412
    terrno = TSDB_CODE_INVALID_MSG;
2413
    goto _OVER;
S
Shengliang Guan 已提交
2414
  }
D
dapan 已提交
2415

D
dapan1121 已提交
2416
  if (0 == strcmp(infoReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2417
    mInfo("information_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
2418
    if (mndBuildInsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, sysinfo, &metaRsp) != 0) {
2419
      goto _OVER;
D
dapan1121 已提交
2420
    }
D
dapan1121 已提交
2421
  } else if (0 == strcmp(infoReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2422
    mInfo("performance_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
2423
    if (mndBuildPerfsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
2424
      goto _OVER;
D
dapan1121 已提交
2425
    }
D
dapan1121 已提交
2426
  } else {
2427
    mInfo("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
2428
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp, NULL) != 0) {
2429
      goto _OVER;
D
dapan1121 已提交
2430
    }
S
Shengliang Guan 已提交
2431
  }
S
Shengliang Guan 已提交
2432

S
Shengliang Guan 已提交
2433 2434 2435
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
2436
    goto _OVER;
S
Shengliang Guan 已提交
2437
  }
S
Shengliang Guan 已提交
2438

S
Shengliang Guan 已提交
2439
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
2440 2441
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2442
    goto _OVER;
S
Shengliang Guan 已提交
2443
  }
D
dapan 已提交
2444

S
Shengliang Guan 已提交
2445
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
2446 2447
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
S
Shengliang Guan 已提交
2448
  code = 0;
S
Shengliang Guan 已提交
2449

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

2452
_OVER:
S
Shengliang Guan 已提交
2453 2454 2455
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
2456

2457
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
2458 2459 2460
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
2461

D
dapan1121 已提交
2462
static int32_t mndProcessTableCfgReq(SRpcMsg *pReq) {
2463 2464 2465 2466
  SMnode      *pMnode = pReq->info.node;
  int32_t      code = -1;
  STableCfgReq cfgReq = {0};
  STableCfgRsp cfgRsp = {0};
D
dapan1121 已提交
2467 2468 2469 2470 2471 2472 2473

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

  if (0 == strcmp(cfgReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2474
    mInfo("information_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2475 2476 2477 2478
    if (mndBuildInsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else if (0 == strcmp(cfgReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2479
    mInfo("performance_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2480 2481 2482 2483
    if (mndBuildPerfsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else {
2484
    mInfo("stb:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
    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 已提交
2518
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
S
Shengliang Guan 已提交
2519
                           int32_t *pRspLen) {
D
dapan1121 已提交
2520
  SSTbHbRsp hbRsp = {0};
D
dapan1121 已提交
2521 2522
  hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (hbRsp.pMetaRsp == NULL) {
S
Shengliang Guan 已提交
2523 2524 2525
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
2526

D
dapan1121 已提交
2527
  hbRsp.pIndexRsp = taosArrayInit(numOfStbs, sizeof(STableIndexRsp));
D
dapan1121 已提交
2528
  if (NULL == hbRsp.pIndexRsp) {
D
dapan1121 已提交
2529
    taosArrayDestroy(hbRsp.pMetaRsp);
D
dapan1121 已提交
2530 2531 2532
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
2533

S
Shengliang Guan 已提交
2534
  for (int32_t i = 0; i < numOfStbs; ++i) {
D
dapan1121 已提交
2535
    SSTableVersion *pStbVersion = &pStbVersions[i];
S
Shengliang Guan 已提交
2536 2537 2538
    pStbVersion->suid = be64toh(pStbVersion->suid);
    pStbVersion->sversion = ntohs(pStbVersion->sversion);
    pStbVersion->tversion = ntohs(pStbVersion->tversion);
D
dapan1121 已提交
2539
    pStbVersion->smaVer = ntohl(pStbVersion->smaVer);
S
Shengliang Guan 已提交
2540

S
Shengliang Guan 已提交
2541
    STableMetaRsp metaRsp = {0};
L
Liu Jicong 已提交
2542
    int32_t       smaVer = 0;
2543
    mInfo("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2544
    if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp, &smaVer) != 0) {
S
Shengliang Guan 已提交
2545 2546
      metaRsp.numOfColumns = -1;
      metaRsp.suid = pStbVersion->suid;
D
dapan1121 已提交
2547
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2548
      continue;
D
dapan 已提交
2549
    }
S
Shengliang Guan 已提交
2550

D
dapan1121 已提交
2551
    if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
D
dapan1121 已提交
2552
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2553 2554
    } else {
      tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
2555
    }
D
dapan1121 已提交
2556

D
dapan1121 已提交
2557
    if (pStbVersion->smaVer && pStbVersion->smaVer != smaVer) {
L
Liu Jicong 已提交
2558 2559
      bool           exist = false;
      char           tbFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
2560
      STableIndexRsp indexRsp = {0};
D
dapan1121 已提交
2561 2562 2563 2564 2565
      indexRsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
      if (NULL == indexRsp.pIndex) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
L
Liu Jicong 已提交
2566

D
dapan1121 已提交
2567
      sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2568
      int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist);
D
dapan1121 已提交
2569
      if (code || !exist) {
D
dapan1121 已提交
2570 2571 2572
        indexRsp.suid = pStbVersion->suid;
        indexRsp.version = -1;
        indexRsp.pIndex = NULL;
D
dapan1121 已提交
2573
      }
D
dapan1121 已提交
2574

D
dapan1121 已提交
2575 2576
      strcpy(indexRsp.dbFName, pStbVersion->dbFName);
      strcpy(indexRsp.tbName, pStbVersion->stbName);
D
dapan1121 已提交
2577 2578

      taosArrayPush(hbRsp.pIndexRsp, &indexRsp);
D
dapan1121 已提交
2579
    }
S
Shengliang Guan 已提交
2580
  }
S
Shengliang Guan 已提交
2581

D
dapan1121 已提交
2582
  int32_t rspLen = tSerializeSSTbHbRsp(NULL, 0, &hbRsp);
S
Shengliang Guan 已提交
2583
  if (rspLen < 0) {
D
dapan1121 已提交
2584
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2585 2586
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
2587 2588
  }

wafwerar's avatar
wafwerar 已提交
2589
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
2590
  if (pRsp == NULL) {
D
dapan1121 已提交
2591
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2592 2593
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
2594 2595
  }

D
dapan1121 已提交
2596 2597
  tSerializeSSTbHbRsp(pRsp, rspLen, &hbRsp);
  tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2598 2599
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
2600 2601 2602
  return 0;
}

2603
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
2604
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2605 2606 2607 2608 2609 2610
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
2611
  int32_t numOfStbs = 0;
2612
  void   *pIter = NULL;
S
Shengliang Guan 已提交
2613
  while (1) {
S
Shengliang Guan 已提交
2614
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
2615
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2616 2617
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
2618
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
2619
      numOfStbs++;
S
Shengliang Guan 已提交
2620 2621
    }

S
Shengliang Guan 已提交
2622
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2623 2624
  }

S
Shengliang Guan 已提交
2625
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
2626
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
2627 2628 2629
  return 0;
}

L
Liu Jicong 已提交
2630 2631 2632 2633 2634 2635 2636
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);
}

C
cademfly 已提交
2637 2638 2639 2640 2641 2642 2643
void mndExtractShortDbNameFromStbFullName(const char *stbFullName, char *dst) {
  SName name = {0};
  tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  tNameGetDbName(&name, dst);
}

L
Liu Jicong 已提交
2644
void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t dstSize) {
S
Shengliang Guan 已提交
2645 2646
  int32_t pos = -1;
  int32_t num = 0;
L
Liu Jicong 已提交
2647 2648
  for (pos = 0; stbFullName[pos] != 0; ++pos) {
    if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
2649 2650 2651 2652
    if (num == 2) break;
  }

  if (num == 2) {
L
Liu Jicong 已提交
2653
    tstrncpy(dst, stbFullName + pos + 1, dstSize);
S
Shengliang Guan 已提交
2654 2655 2656
  }
}

dengyihao's avatar
dengyihao 已提交
2657 2658 2659 2660 2661 2662
// static int32_t mndProcessRetrieveStbReq(SRpcMsg *pReq) {
//   SMnode    *pMnode = pReq->info.node;
//   SShowMgmt *pMgmt = &pMnode->showMgmt;
//   SShowObj  *pShow = NULL;
//   int32_t    rowsToRead = SHOW_STEP_SIZE;
//   int32_t    rowsRead = 0;
wmmhello's avatar
wmmhello 已提交
2663
//
dengyihao's avatar
dengyihao 已提交
2664 2665 2666 2667 2668
//   SRetrieveTableReq retrieveReq = {0};
//   if (tDeserializeSRetrieveTableReq(pReq->pCont, pReq->contLen, &retrieveReq) != 0) {
//     terrno = TSDB_CODE_INVALID_MSG;
//     return -1;
//   }
wmmhello's avatar
wmmhello 已提交
2669
//
dengyihao's avatar
dengyihao 已提交
2670 2671 2672 2673 2674
//   SMnode    *pMnode = pReq->info.node;
//   SSdb      *pSdb = pMnode->pSdb;
//   int32_t    numOfRows = 0;
//   SDbObj    *pDb = NULL;
//   ESdbStatus objStatus = 0;
wmmhello's avatar
wmmhello 已提交
2675
//
dengyihao's avatar
dengyihao 已提交
2676 2677 2678
//   SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user);
//   if (pUser == NULL) return 0;
//   bool sysinfo = pUser->sysInfo;
wmmhello's avatar
wmmhello 已提交
2679
//
dengyihao's avatar
dengyihao 已提交
2680
//   // Append the information_schema database into the result.
wmmhello's avatar
wmmhello 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
////  if (!pShow->sysDbRsp) {
////    SDbObj infoschemaDb = {0};
////    setInformationSchemaDbCfg(pMnode, &infoschemaDb);
////    size_t numOfTables = 0;
////    getVisibleInfosTablesNum(sysinfo, &numOfTables);
////    mndDumpDbInfoData(pMnode, pBlock, &infoschemaDb, pShow, numOfRows, numOfTables, true, 0, 1);
////
////    numOfRows += 1;
////
////    SDbObj perfschemaDb = {0};
////    setPerfSchemaDbCfg(pMnode, &perfschemaDb);
////    numOfTables = 0;
////    getPerfDbMeta(NULL, &numOfTables);
////    mndDumpDbInfoData(pMnode, pBlock, &perfschemaDb, pShow, numOfRows, numOfTables, true, 0, 1);
////
////    numOfRows += 1;
////    pShow->sysDbRsp = true;
////  }
//
//  SSDataBlock* p = buildInfoSchemaTableMetaBlock(TSDB_INS_TABLE_COLS);
//  blockDataEnsureCapacity(p, rowsToRead);
//
//  size_t               size = 0;
//  const SSysTableMeta* pSysDbTableMeta = NULL;
//
//  getInfosDbMeta(&pSysDbTableMeta, &size);
//  p->info.rows = buildDbColsInfoBlock(sysinfo, p, pSysDbTableMeta, size, TSDB_INFORMATION_SCHEMA_DB);
//
//  getPerfDbMeta(&pSysDbTableMeta, &size);
//  p->info.rows = buildDbColsInfoBlock(sysinfo, p, pSysDbTableMeta, size, TSDB_PERFORMANCE_SCHEMA_DB);
//
//  blockDataDestroy(p);
//
//
//  while (numOfRows < rowsToRead) {
//    pShow->pIter = sdbFetchAll(pSdb, SDB_DB, pShow->pIter, (void **)&pDb, &objStatus, true);
//    if (pShow->pIter == NULL) break;
//    if (strncmp(retrieveReq.db, pDb->name, strlen(retrieveReq.db)) != 0){
//      continue;
//    }
//    if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_READ_OR_WRITE_DB, pDb) != 0) {
//      continue;
//    }
//
//    while (numOfRows < rowsToRead) {
//      pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
//      if (pShow->pIter == NULL) break;
//
//      if (pDb != NULL && pStb->dbUid != pDb->uid) {
//        sdbRelease(pSdb, pStb);
//        continue;
//      }
//
//      cols = 0;
//
//      SName name = {0};
//      char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
//      mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
//      varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
//
//      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
//      colDataAppend(pColInfo, numOfRows, (const char *)stbName, false);
//
//      char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
//      tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
//      tNameGetDbName(&name, varDataVal(db));
//      varDataSetLen(db, strlen(varDataVal(db)));
//
//      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
//      colDataAppend(pColInfo, numOfRows, (const char *)db, false);
//
//      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
//
//      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
//      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);
//      } else if (pStb->commentLen == 0) {
//        char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
//        STR_TO_VARSTR(comment, "");
//        colDataAppend(pColInfo, numOfRows, comment, false);
//      } else {
X
Xiaoyu Wang 已提交
2774
//        colDataSetNULL(pColInfo, numOfRows);
wmmhello's avatar
wmmhello 已提交
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
//      }
//
//      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);
//
//      char    rollup[160 + VARSTR_HEADER_SIZE] = {0};
//      int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs);
//      char   *sep = ", ";
//      int32_t sepLen = strlen(sep);
//      int32_t rollupLen = sizeof(rollup) - VARSTR_HEADER_SIZE - 2;
//      for (int32_t i = 0; i < rollupNum; ++i) {
//        char *funcName = taosArrayGet(pStb->pFuncs, i);
//        if (i) {
//          strncat(varDataVal(rollup), sep, rollupLen);
//          rollupLen -= sepLen;
//        }
//        strncat(varDataVal(rollup), funcName, rollupLen);
//        rollupLen -= strlen(funcName);
//      }
//      varDataSetLen(rollup, strlen(varDataVal(rollup)));
//
//      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
//      colDataAppend(pColInfo, numOfRows, (const char *)rollup, false);
//
//      numOfRows++;
//      sdbRelease(pSdb, pStb);
//    }
//
//    if (pDb != NULL) {
//      mndReleaseDb(pMnode, pDb);
//    }
//
//    sdbRelease(pSdb, pDb);
//  }
//
//  pShow->numOfRows += numOfRows;
//  mndReleaseUser(pMnode, pUser);
//
//
//
//
//
//
//
//
//  ShowRetrieveFp retrieveFp = pMgmt->retrieveFps[pShow->type];
//  if (retrieveFp == NULL) {
//    mndReleaseShowObj(pShow, false);
//    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
//    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
//    return -1;
//  }
//
//  mDebug("show:0x%" PRIx64 ", start retrieve data, type:%d", pShow->id, pShow->type);
//  if (retrieveReq.user[0] != 0) {
//    memcpy(pReq->info.conn.user, retrieveReq.user, TSDB_USER_LEN);
//  } else {
//    memcpy(pReq->info.conn.user, TSDB_DEFAULT_USER, strlen(TSDB_DEFAULT_USER) + 1);
//  }
//  if (retrieveReq.db[0] && mndCheckShowPrivilege(pMnode, pReq->info.conn.user, pShow->type, retrieveReq.db) != 0) {
//    return -1;
//  }
//
//  int32_t numOfCols = pShow->pMeta->numOfColumns;
//
//  SSDataBlock *pBlock = createDataBlock();
//  for (int32_t i = 0; i < numOfCols; ++i) {
//    SColumnInfoData idata = {0};
//
//    SSchema *p = &pShow->pMeta->pSchemas[i];
//
//    idata.info.bytes = p->bytes;
//    idata.info.type = p->type;
//    idata.info.colId = p->colId;
//    blockDataAppendColInfo(pBlock, &idata);
//  }
//
//  blockDataEnsureCapacity(pBlock, rowsToRead);
//
//  if (mndCheckRetrieveFinished(pShow)) {
//    mDebug("show:0x%" PRIx64 ", read finished, numOfRows:%d", pShow->id, pShow->numOfRows);
//    rowsRead = 0;
//  } else {
//    rowsRead = (*retrieveFp)(pReq, pShow, pBlock, rowsToRead);
//    if (rowsRead < 0) {
//      terrno = rowsRead;
//      mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
//      mndReleaseShowObj(pShow, true);
//      blockDataDestroy(pBlock);
//      return -1;
//    }
//
//    pBlock->info.rows = rowsRead;
//    mDebug("show:0x%" PRIx64 ", stop retrieve data, rowsRead:%d numOfRows:%d", pShow->id, rowsRead, pShow->numOfRows);
//  }
//
//  size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * pShow->pMeta->numOfColumns +
//         blockDataGetSize(pBlock) + blockDataGetSerialMetaSize(taosArrayGetSize(pBlock->pDataBlock));
//
//  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
//  if (pRsp == NULL) {
//    mndReleaseShowObj(pShow, false);
//    terrno = TSDB_CODE_OUT_OF_MEMORY;
//    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
//    blockDataDestroy(pBlock);
//    return -1;
//  }
//
//  pRsp->handle = htobe64(pShow->id);
//
//  if (rowsRead > 0) {
//    char    *pStart = pRsp->data;
//    SSchema *ps = pShow->pMeta->pSchemas;
//
//    *(int32_t *)pStart = htonl(pShow->pMeta->numOfColumns);
//    pStart += sizeof(int32_t);  // number of columns
//
//    for (int32_t i = 0; i < pShow->pMeta->numOfColumns; ++i) {
//      SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
//      pSchema->bytes = htonl(ps[i].bytes);
//      pSchema->colId = htons(ps[i].colId);
//      pSchema->type = ps[i].type;
//
//      pStart += sizeof(SSysTableSchema);
//    }
//
//    int32_t len = blockEncode(pBlock, pStart, pShow->pMeta->numOfColumns);
//  }
//
//  pRsp->numOfRows = htonl(rowsRead);
//  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
//  pReq->info.rsp = pRsp;
//  pReq->info.rspLen = size;
//
//  if (rowsRead == 0 || rowsRead < rowsToRead) {
//    pRsp->completed = 1;
//    mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
//    mndReleaseShowObj(pShow, true);
//  } else {
//    mDebug("show:0x%" PRIx64 ", retrieve not completed yet", pShow->id);
//    mndReleaseShowObj(pShow, false);
//  }
//
//  blockDataDestroy(pBlock);
//  return TSDB_CODE_SUCCESS;
//}

S
Shengliang Guan 已提交
2933 2934
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2935
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2936 2937 2938
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
2939

H
Hongze Cheng 已提交
2940
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
2941 2942
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
2943
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
2944
  }
S
Shengliang Guan 已提交
2945

S
Shengliang Guan 已提交
2946
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
2947
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2948 2949
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
2950
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
2951
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2952 2953 2954 2955 2956
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
2957
    SName name = {0};
H
Hongze Cheng 已提交
2958
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
2959
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
2960
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
2961

H
Hongze Cheng 已提交
2962
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2963
    colDataSetVal(pColInfo, numOfRows, (const char *)stbName, false);
2964

H
Hongze Cheng 已提交
2965 2966
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
2967 2968 2969
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

2970
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2971
    colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
2972 2973

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

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

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

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

2985
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
2986 2987 2988
    if (pStb->commentLen > 0) {
      char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, pStb->comment);
2989
      colDataSetVal(pColInfo, numOfRows, comment, false);
2990
    } else if (pStb->commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
2991 2992
      char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, "");
2993
      colDataSetVal(pColInfo, numOfRows, comment, false);
wmmhello's avatar
wmmhello 已提交
2994
    } else {
2995
      colDataSetNULL(pColInfo, numOfRows);
S
Shengliang Guan 已提交
2996
    }
H
Haojun Liao 已提交
2997

2998 2999 3000 3001 3002
    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++);
3003
    colDataSetVal(pColInfo, numOfRows, (const char *)watermark, false);
3004 3005 3006 3007 3008 3009

    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++);
3010
    colDataSetVal(pColInfo, numOfRows, (const char *)maxDelay, false);
3011

S
Shengliang Guan 已提交
3012
    char    rollup[160 + VARSTR_HEADER_SIZE] = {0};
3013
    int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs);
D
dapan1121 已提交
3014 3015
    char   *sep = ", ";
    int32_t sepLen = strlen(sep);
D
dapan1121 已提交
3016
    int32_t rollupLen = sizeof(rollup) - VARSTR_HEADER_SIZE - 2;
3017 3018 3019
    for (int32_t i = 0; i < rollupNum; ++i) {
      char *funcName = taosArrayGet(pStb->pFuncs, i);
      if (i) {
D
dapan1121 已提交
3020 3021
        strncat(varDataVal(rollup), sep, rollupLen);
        rollupLen -= sepLen;
3022
      }
D
dapan1121 已提交
3023 3024
      strncat(varDataVal(rollup), funcName, rollupLen);
      rollupLen -= strlen(funcName);
3025 3026 3027 3028
    }
    varDataSetLen(rollup, strlen(varDataVal(rollup)));

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

S
Shengliang Guan 已提交
3031
    numOfRows++;
S
Shengliang Guan 已提交
3032
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
3033 3034
  }

H
Haojun Liao 已提交
3035 3036 3037 3038
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

3039
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
3040 3041 3042
  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3043 3044
static int32_t buildDbColsInfoBlock(const SSDataBlock *p, const SSysTableMeta *pSysDbTableMeta, size_t size,
                                    const char *dbName, const char *tbName) {
wmmhello's avatar
wmmhello 已提交
3045 3046 3047 3048 3049 3050 3051 3052 3053
  char    tName[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
  char    dName[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
  char    typeName[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
  int32_t numOfRows = p->info.rows;

  STR_TO_VARSTR(dName, dbName);
  STR_TO_VARSTR(typeName, "SYSTEM_TABLE");

  for (int32_t i = 0; i < size; ++i) {
dengyihao's avatar
dengyihao 已提交
3054 3055 3056 3057 3058
    const SSysTableMeta *pm = &pSysDbTableMeta[i];
    //    if (pm->sysInfo) {
    //      continue;
    //    }
    if (tbName[0] && strncmp(tbName, pm->name, TSDB_TABLE_NAME_LEN) != 0) {
wmmhello's avatar
wmmhello 已提交
3059 3060 3061
      continue;
    }

wmmhello's avatar
wmmhello 已提交
3062 3063
    STR_TO_VARSTR(tName, pm->name);

dengyihao's avatar
dengyihao 已提交
3064
    for (int32_t j = 0; j < pm->colNum; j++) {
wmmhello's avatar
wmmhello 已提交
3065
      // table name
dengyihao's avatar
dengyihao 已提交
3066
      SColumnInfoData *pColInfoData = taosArrayGet(p->pDataBlock, 0);
wmmhello's avatar
wmmhello 已提交
3067 3068
      colDataAppend(pColInfoData, numOfRows, tName, false);

wmmhello's avatar
wmmhello 已提交
3069
      // database name
wmmhello's avatar
wmmhello 已提交
3070
      pColInfoData = taosArrayGet(p->pDataBlock, 1);
wmmhello's avatar
wmmhello 已提交
3071
      colDataAppend(pColInfoData, numOfRows, dName, false);
wmmhello's avatar
wmmhello 已提交
3072 3073

      pColInfoData = taosArrayGet(p->pDataBlock, 2);
wmmhello's avatar
wmmhello 已提交
3074
      colDataAppend(pColInfoData, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087

      // col name
      char colName[TSDB_COL_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(colName, pm->schema[j].name);
      pColInfoData = taosArrayGet(p->pDataBlock, 3);
      colDataAppend(pColInfoData, numOfRows, colName, false);

      // col type
      int8_t colType = pm->schema[j].type;
      pColInfoData = taosArrayGet(p->pDataBlock, 4);
      char colTypeStr[VARSTR_HEADER_SIZE + 32];
      int  colTypeLen = sprintf(varDataVal(colTypeStr), "%s", tDataTypes[colType].name);
      if (colType == TSDB_DATA_TYPE_VARCHAR) {
dengyihao's avatar
dengyihao 已提交
3088 3089
        colTypeLen +=
            sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", (int32_t)(pm->schema[j].bytes - VARSTR_HEADER_SIZE));
wmmhello's avatar
wmmhello 已提交
3090
      } else if (colType == TSDB_DATA_TYPE_NCHAR) {
dengyihao's avatar
dengyihao 已提交
3091 3092
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)((pm->schema[j].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
wmmhello's avatar
wmmhello 已提交
3093 3094
      }
      varDataSetLen(colTypeStr, colTypeLen);
dengyihao's avatar
dengyihao 已提交
3095
      colDataAppend(pColInfoData, numOfRows, (char *)colTypeStr, false);
wmmhello's avatar
wmmhello 已提交
3096 3097

      pColInfoData = taosArrayGet(p->pDataBlock, 5);
dengyihao's avatar
dengyihao 已提交
3098
      colDataAppend(pColInfoData, numOfRows, (const char *)&pm->schema[j].bytes, false);
wmmhello's avatar
wmmhello 已提交
3099 3100
      for (int32_t k = 6; k <= 8; ++k) {
        pColInfoData = taosArrayGet(p->pDataBlock, k);
X
Xiaoyu Wang 已提交
3101
        colDataSetNULL(pColInfoData, numOfRows);
wmmhello's avatar
wmmhello 已提交
3102 3103 3104 3105 3106 3107 3108 3109 3110
      }

      numOfRows += 1;
    }
  }

  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3111
static int32_t buildSysDbColsInfo(SSDataBlock *p, char *db, char *tb) {
wmmhello's avatar
wmmhello 已提交
3112
  size_t               size = 0;
dengyihao's avatar
dengyihao 已提交
3113
  const SSysTableMeta *pSysDbTableMeta = NULL;
wmmhello's avatar
wmmhello 已提交
3114

dengyihao's avatar
dengyihao 已提交
3115 3116
  if (db[0] && strncmp(db, TSDB_INFORMATION_SCHEMA_DB, TSDB_DB_FNAME_LEN) != 0 &&
      strncmp(db, TSDB_PERFORMANCE_SCHEMA_DB, TSDB_DB_FNAME_LEN) != 0) {
wmmhello's avatar
wmmhello 已提交
3117 3118 3119
    return p->info.rows;
  }

wmmhello's avatar
wmmhello 已提交
3120
  getInfosDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3121
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_INFORMATION_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3122 3123

  getPerfDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3124
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_PERFORMANCE_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3125 3126 3127 3128 3129 3130 3131 3132 3133

  return p->info.rows;
}

static int32_t mndRetrieveStbCol(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
  SSdb    *pSdb = pMnode->pSdb;
  SStbObj *pStb = NULL;

wmmhello's avatar
wmmhello 已提交
3134
  int32_t numOfRows = buildSysDbColsInfo(pBlock, pShow->db, pShow->filterTb);
wmmhello's avatar
wmmhello 已提交
3135
  mDebug("mndRetrieveStbCol get system table cols, rows:%d, db:%s", numOfRows, pShow->db);
wmmhello's avatar
wmmhello 已提交
3136 3137 3138 3139 3140 3141
  SDbObj *pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
    if (pDb == NULL) return terrno;
  }

dengyihao's avatar
dengyihao 已提交
3142
  char typeName[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
wmmhello's avatar
wmmhello 已提交
3143
  STR_TO_VARSTR(typeName, "SUPER_TABLE");
D
dapan1121 已提交
3144 3145
  bool fetch = pShow->restore ? false : true;
  pShow->restore = false;
wmmhello's avatar
wmmhello 已提交
3146
  while (numOfRows < rows) {
D
dapan1121 已提交
3147 3148 3149 3150 3151 3152 3153 3154 3155
    if (fetch) {
      pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
      if (pShow->pIter == NULL) break;
    } else {
      fetch = true;
      void  *pKey = taosHashGetKey(pShow->pIter, NULL);
      pStb = sdbAcquire(pSdb, SDB_STB, pKey);
      if (!pStb) continue;
    }
wmmhello's avatar
wmmhello 已提交
3156 3157 3158 3159 3160 3161 3162 3163 3164

    if (pDb != NULL && pStb->dbUid != pDb->uid) {
      sdbRelease(pSdb, pStb);
      continue;
    }

    SName name = {0};
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
dengyihao's avatar
dengyihao 已提交
3165
    if (pShow->filterTb[0] && strncmp(pShow->filterTb, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN) != 0) {
wmmhello's avatar
wmmhello 已提交
3166 3167 3168
      sdbRelease(pSdb, pStb);
      continue;
    }
D
dapan1121 已提交
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179

    if ((numOfRows + pStb->numOfColumns) > rows) {
      pShow->restore = true;
      if (numOfRows == 0) {
        mError("mndRetrieveStbCol failed to get stable cols since buf:%d less than result:%d, stable name:%s, db:%s",
               rows, pStb->numOfColumns, pStb->name, pStb->db);
      }
      sdbRelease(pSdb, pStb);
      break;
    }

wmmhello's avatar
wmmhello 已提交
3180 3181
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));

wmmhello's avatar
wmmhello 已提交
3182 3183
    mDebug("mndRetrieveStbCol get stable cols, stable name:%s, db:%s", pStb->name, pStb->db);

wmmhello's avatar
wmmhello 已提交
3184 3185 3186 3187 3188
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

dengyihao's avatar
dengyihao 已提交
3189 3190
    for (int i = 0; i < pStb->numOfColumns; i++) {
      int32_t          cols = 0;
wmmhello's avatar
wmmhello 已提交
3191 3192 3193 3194
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataAppend(pColInfo, numOfRows, (const char *)stbName, false);

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
3195
      colDataAppend(pColInfo, numOfRows, (const char *)db, false);
wmmhello's avatar
wmmhello 已提交
3196 3197

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
3198
      colDataAppend(pColInfo, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214

      // col name
      char colName[TSDB_COL_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(colName, pStb->pColumns[i].name);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataAppend(pColInfo, numOfRows, colName, false);

      // col type
      int8_t colType = pStb->pColumns[i].type;
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      char colTypeStr[VARSTR_HEADER_SIZE + 32];
      int  colTypeLen = sprintf(varDataVal(colTypeStr), "%s", tDataTypes[colType].name);
      if (colType == TSDB_DATA_TYPE_VARCHAR) {
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)(pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE));
      } else if (colType == TSDB_DATA_TYPE_NCHAR) {
dengyihao's avatar
dengyihao 已提交
3215 3216
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)((pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
wmmhello's avatar
wmmhello 已提交
3217 3218
      }
      varDataSetLen(colTypeStr, colTypeLen);
dengyihao's avatar
dengyihao 已提交
3219
      colDataAppend(pColInfo, numOfRows, (char *)colTypeStr, false);
wmmhello's avatar
wmmhello 已提交
3220 3221

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
dengyihao's avatar
dengyihao 已提交
3222 3223
      colDataAppend(pColInfo, numOfRows, (const char *)&pStb->pColumns[i].bytes, false);
      while (cols < pShow->numOfColumns) {
wmmhello's avatar
wmmhello 已提交
3224
        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
X
Xiaoyu Wang 已提交
3225
        colDataSetNULL(pColInfo, numOfRows);
wmmhello's avatar
wmmhello 已提交
3226
      }
wmmhello's avatar
wmmhello 已提交
3227
      numOfRows++;
wmmhello's avatar
wmmhello 已提交
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
    }

    sdbRelease(pSdb, pStb);
  }

  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

  pShow->numOfRows += numOfRows;
wmmhello's avatar
wmmhello 已提交
3238 3239
  mDebug("mndRetrieveStbCol success, rows:%d, pShow->numOfRows:%d", numOfRows, pShow->numOfRows);

wmmhello's avatar
wmmhello 已提交
3240 3241 3242
  return numOfRows;
}

S
Shengliang Guan 已提交
3243
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
3244 3245
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
3246
}
S
Shengliang Guan 已提交
3247 3248 3249 3250 3251 3252 3253 3254 3255 3256

const char *mndGetStbStr(const char *src) {
  char *posDb = strstr(src, TS_PATH_DELIMITER);
  if (posDb != NULL) ++posDb;
  if (posDb == NULL) return src;

  char *posStb = strstr(posDb, TS_PATH_DELIMITER);
  if (posStb != NULL) ++posStb;
  if (posStb == NULL) return posDb;
  return posStb;
D
dapan1121 已提交
3257
}
dengyihao's avatar
dengyihao 已提交
3258 3259 3260 3261 3262 3263

static int32_t mndCheckIndexReq(SCreateTagIndexReq *pReq) {
  // impl
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
3264
/*int32_t mndAddIndexImpl(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp, void *sql,
dengyihao's avatar
dengyihao 已提交
3265 3266
                        int32_t len) {
  // impl later
dengyihao's avatar
dengyihao 已提交
3267
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
3268 3269 3270 3271 3272 3273 3274
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "create-stb-index");
  if (pTrans == NULL) goto _OVER;

  mInfo("trans:%d, used to add index to stb:%s", pTrans->id, pStb->name);
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
3275 3276
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3277
  if (mndSetAlterStbRedoActions2(pMnode, pTrans, pDb, pStb, sql, len) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3278 3279
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
3280 3281 3282 3283 3284
  return code;

_OVER:
  mndTransDrop(pTrans);
  return code;
dengyihao's avatar
dengyihao 已提交
3285
}
dengyihao's avatar
dengyihao 已提交
3286 3287 3288 3289 3290
static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *tagIdxReq, SDbObj *pDb, SStbObj *pOld) {
  bool    needRsp = true;
  int32_t code = -1;
  SField *pField0 = NULL;

dengyihao's avatar
dengyihao 已提交
3291 3292 3293
  SStbObj  stbObj = {0};
  SStbObj *pNew = &stbObj;

dengyihao's avatar
dengyihao 已提交
3294 3295 3296 3297 3298 3299 3300 3301 3302
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
  taosRUnLockLatch(&pOld->lock);

  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
  stbObj.lock = 0;

dengyihao's avatar
dengyihao 已提交
3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagIdxReq->colName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }
  col_id_t colId = pOld->pTags[tag].colId;
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
    return -1;
  }
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pTag = pNew->pTags + tag;
  if (IS_IDX_ON(pTag)) {
dengyihao's avatar
dengyihao 已提交
3318
    terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
dengyihao's avatar
dengyihao 已提交
3319 3320 3321 3322 3323 3324
    return -1;
  } else {
    pTag->flags |= COL_IDX_ON;
  }
  pNew->tagVer++;

dengyihao's avatar
dengyihao 已提交
3325 3326 3327
  code = mndAddIndexImpl(pMnode, pReq, pDb, pNew, needRsp, pReq->pCont, pReq->contLen);

  return code;
dengyihao's avatar
dengyihao 已提交
3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
}
static int32_t mndProcessCreateIndexReq(SRpcMsg *pReq) {
  SMnode            *pMnode = pReq->info.node;
  int32_t            code = -1;
  SDbObj            *pDb = NULL;
  SStbObj           *pStb = NULL;
  SCreateTagIndexReq tagIdxReq = {0};

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

  mInfo("stb:%s, start to alter", tagIdxReq.stbName);

  if (mndCheckIndexReq(&tagIdxReq) != TSDB_CODE_SUCCESS) {
    goto _OVER;
  }

  pDb = mndAcquireDbByStb(pMnode, tagIdxReq.dbFName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
    goto _OVER;
  }

  pStb = mndAcquireStb(pMnode, tagIdxReq.stbName);
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
    goto _OVER;
  }
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
    goto _OVER;
  }

  code = mndAddIndex(pMnode, pReq, &tagIdxReq, pDb, pStb);
dengyihao's avatar
dengyihao 已提交
3363 3364 3365
  if (terrno == TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST || terrno == TSDB_CODE_MND_TAG_NOT_EXIST) {
    return terrno;
  } else {
dengyihao's avatar
dengyihao 已提交
3366
    if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
dengyihao's avatar
dengyihao 已提交
3367
  }
dengyihao's avatar
dengyihao 已提交
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
_OVER:
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
    mError("stb:%s, failed to create index since %s", tagIdxReq.stbName, terrstr());
  }
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
  return code;
}
static int32_t mndProcessDropIndexReq(SRpcMsg *pReq) {
  SMnode          *pMnode = pReq->info.node;
  int32_t          code = -1;
  SDbObj          *pDb = NULL;
  SStbObj         *pStb = NULL;
  SDropTagIndexReq dropReq = {0};
  if (tDeserializeSDropTagIdxReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    goto _OVER;
  }
  //
  return TSDB_CODE_SUCCESS;
_OVER:
  return code;
dengyihao's avatar
dengyihao 已提交
3390
}*/