mndStb.c 108.9 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;
}

C
cadem 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
int32_t mndSetForceDropCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup, SStbObj *pStb) {
  SSdb   *pSdb = pMnode->pSdb;
  int32_t contLen;

  void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, NULL, 0);
  if (pReq == NULL) {
    return -1;
  }

  STransAction action = {0};
  action.mTraceId = pTrans->mTraceId;
  action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
  action.pCont = pReq;
  action.contLen = contLen;
  action.msgType = TDMT_VND_CREATE_STB;
  action.acceptableCode = TSDB_CODE_TDB_STB_ALREADY_EXIST;
  action.retryCode = TSDB_CODE_TDB_STB_NOT_EXIST;
  if (mndTransAppendRedoAction(pTrans, &action) != 0) {
    taosMemoryFree(pReq);
    return -1;
  }

  return 0;
}

715
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
716
  SSdb   *pSdb = pMnode->pSdb;
717
  SVgObj *pVgroup = NULL;
718
  void   *pIter = NULL;
719 720 721 722

  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
723
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
724 725 726 727
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
728
    int32_t contLen = 0;
S
Shengliang Guan 已提交
729
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
730
    if (pReq == NULL) {
731 732 733 734 735
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
736

737 738
    STransAction action = {0};
    action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
S
Shengliang Guan 已提交
739
    action.pCont = pReq;
S
Shengliang Guan 已提交
740
    action.contLen = contLen;
H
Hongze Cheng 已提交
741
    action.msgType = TDMT_VND_DROP_STB;
S
Shengliang Guan 已提交
742
    action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
743
    if (mndTransAppendUndoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
744
      taosMemoryFree(pReq);
745 746 747 748 749 750
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
751 752 753 754

  return 0;
}

S
sma  
Shengliang Guan 已提交
755
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
C
Cary Xu 已提交
756
  for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
S
sma  
Shengliang Guan 已提交
757
    SSchema *pSchema = &pStb->pColumns[col];
C
Cary Xu 已提交
758
    if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
S
sma  
Shengliang Guan 已提交
759 760 761 762 763 764
      return pSchema;
    }
  }
  return NULL;
}

765 766 767 768 769
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;
770 771
  pDst->uid =
      (pCreate->source == TD_REQ_FROM_TAOX) ? pCreate->suid : mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
772
  pDst->dbUid = pDb->uid;
773 774
  pDst->tagVer = 1;
  pDst->colVer = 1;
S
Shengliang Guan 已提交
775
  pDst->smaVer = 1;
776
  pDst->nextColId = 1;
777 778 779 780
  pDst->maxdelay[0] = pCreate->delay1;
  pDst->maxdelay[1] = pCreate->delay2;
  pDst->watermark[0] = pCreate->watermark1;
  pDst->watermark[1] = pCreate->watermark2;
781 782 783
  pDst->ttl = pCreate->ttl;
  pDst->numOfColumns = pCreate->numOfColumns;
  pDst->numOfTags = pCreate->numOfTags;
S
Shengliang Guan 已提交
784
  pDst->numOfFuncs = pCreate->numOfFuncs;
785
  pDst->commentLen = pCreate->commentLen;
D
dapan1121 已提交
786 787
  pDst->pFuncs = pCreate->pFuncs;
  pCreate->pFuncs = NULL;
788

789
  if (pDst->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
790
    pDst->comment = taosMemoryCalloc(pDst->commentLen + 1, 1);
791
    if (pDst->comment == NULL) {
S
sma  
Shengliang Guan 已提交
792 793 794
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
S
Shengliang Guan 已提交
795
    memcpy(pDst->comment, pCreate->pComment, pDst->commentLen + 1);
S
sma  
Shengliang Guan 已提交
796
  }
S
Shengliang Guan 已提交
797

798 799 800 801
  pDst->ast1Len = pCreate->ast1Len;
  if (pDst->ast1Len > 0) {
    pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
    if (pDst->pAst1 == NULL) {
802 803 804
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
805
    memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
806 807
  }

808 809 810 811
  pDst->ast2Len = pCreate->ast2Len;
  if (pDst->ast2Len > 0) {
    pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
    if (pDst->pAst2 == NULL) {
812 813 814
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
815
    memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
816 817
  }

818 819 820
  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 已提交
821 822 823
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
824

D
dmchen 已提交
825
  if(pDst->nextColId < 0 || pDst->nextColId >= 0x7fff - pDst->numOfColumns - pDst->numOfTags){
D
dm chen 已提交
826
    terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
D
dmchen 已提交
827 828 829
    return -1;
  }

830
  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
S
Shengliang Guan 已提交
831
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
832
    SSchema *pSchema = &pDst->pColumns[i];
S
Shengliang Guan 已提交
833 834
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
835
    pSchema->flags = pField->flags;
S
Shengliang Guan 已提交
836
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
837 838
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
839 840
  }

841
  for (int32_t i = 0; i < pDst->numOfTags; ++i) {
S
Shengliang Guan 已提交
842
    SField  *pField = taosArrayGet(pCreate->pTags, i);
843
    SSchema *pSchema = &pDst->pTags[i];
S
Shengliang Guan 已提交
844 845
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
dengyihao's avatar
dengyihao 已提交
846
    if (i == 0) {
dengyihao's avatar
dengyihao 已提交
847
      SSCHMEA_SET_IDX_ON(pSchema);
dengyihao's avatar
dengyihao 已提交
848
    }
S
Shengliang Guan 已提交
849
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
850 851
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
852
  }
853 854 855
  return 0;
}

S
Shengliang Guan 已提交
856
static int32_t mndCreateStb(SMnode *pMnode, SRpcMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
857
  SStbObj stbObj = {0};
S
Shengliang 已提交
858
  int32_t code = -1;
859

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

863
  mInfo("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
864
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
865
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;
866
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
867 868
  code = 0;

869
_OVER:
S
Shengliang Guan 已提交
870
  mndTransDrop(pTrans);
871
  mndStbActionDelete(pMnode->pSdb, &stbObj);
S
Shengliang Guan 已提交
872
  return code;
S
Shengliang Guan 已提交
873
}
S
Shengliang Guan 已提交
874

875
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
876
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
877
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) return -1;
878 879 880 881 882 883 884
  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 已提交
885

S
Shengliang Guan 已提交
886
static int32_t mndProcessTtlTimer(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
887 888 889 890 891 892 893
  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 已提交
894

895 896
  mInfo("start to process ttl timer");

wmmhello's avatar
wmmhello 已提交
897 898 899 900
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
901
    SMsgHead *pHead = rpcMallocCont(contLen);
wmmhello's avatar
wmmhello 已提交
902
    if (pHead == NULL) {
S
Shengliang Guan 已提交
903
      sdbCancelFetch(pSdb, pVgroup);
wmmhello's avatar
wmmhello 已提交
904 905 906 907 908
      sdbRelease(pSdb, pVgroup);
      continue;
    }
    pHead->contLen = htonl(contLen);
    pHead->vgId = htonl(pVgroup->vgId);
S
Shengliang Guan 已提交
909
    tSerializeSVDropTtlTableReq((char *)pHead + sizeof(SMsgHead), contLen, &ttlReq);
wmmhello's avatar
wmmhello 已提交
910

dengyihao's avatar
dengyihao 已提交
911
    SRpcMsg rpcMsg = {.msgType = TDMT_VND_DROP_TTL_TABLE, .pCont = pHead, .contLen = contLen, .info = pReq->info};
S
Shengliang Guan 已提交
912
    SEpSet  epSet = mndGetVgroupEpset(pMnode, pVgroup);
wmmhello's avatar
wmmhello 已提交
913
    int32_t code = tmsgSendReq(&epSet, &rpcMsg);
S
Shengliang Guan 已提交
914
    if (code != 0) {
S
Shengliang Guan 已提交
915
      mError("vgId:%d, failed to send drop ttl table request to vnode since 0x%x", pVgroup->vgId, code);
S
Shengliang Guan 已提交
916
    } else {
917
      mInfo("vgId:%d, send drop ttl table request to vnode, time:%d", pVgroup->vgId, ttlReq.timestamp);
wmmhello's avatar
wmmhello 已提交
918 919 920
    }
    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
921

wmmhello's avatar
wmmhello 已提交
922 923 924
  return 0;
}

wmmhello's avatar
wmmhello 已提交
925 926
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
X
Xiaoyu Wang 已提交
927
    if (strcmp(pStb->pTags[tag].name, tagName) == 0) {
wmmhello's avatar
wmmhello 已提交
928 929 930 931 932 933 934 935 936
      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 已提交
937
    if (strcmp(pStb->pColumns[col].name, colName) == 0) {
wmmhello's avatar
wmmhello 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
      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 已提交
960
  if(pDst->nextColId < 0 || pDst->nextColId >= 0x7fff - pDst->numOfColumns - pDst->numOfTags){
D
dm chen 已提交
961
    terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
D
dmchen 已提交
962 963 964
    return -1;
  }

wmmhello's avatar
wmmhello 已提交
965 966 967 968 969 970 971 972
  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);
973
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
974
      pSchema->colId = pStb->pColumns[cIndex].colId;
975
    } else {
wmmhello's avatar
wmmhello 已提交
976 977 978 979 980 981 982 983 984 985 986
      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);
987
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
988
      pSchema->colId = pStb->pTags[cIndex].colId;
989
    } else {
wmmhello's avatar
wmmhello 已提交
990 991 992 993 994 995 996 997
      pSchema->colId = pDst->nextColId++;
    }
  }
  pDst->tagVer = createReq->tagVer;
  pDst->colVer = createReq->colVer;
  return TSDB_CODE_SUCCESS;
}

998
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
999
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
1000 1001 1002 1003
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
  SMCreateStbReq createReq = {0};
1004
  bool           isAlter = false;
S
Shengliang Guan 已提交
1005

S
Shengliang Guan 已提交
1006
  if (tDeserializeSMCreateStbReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
1007
    terrno = TSDB_CODE_INVALID_MSG;
1008
    goto _OVER;
S
Shengliang Guan 已提交
1009
  }
S
Shengliang Guan 已提交
1010

1011
  mInfo("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
1012 1013
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
1014
    goto _OVER;
S
Shengliang Guan 已提交
1015
  }
S
Shengliang Guan 已提交
1016

S
Shengliang Guan 已提交
1017
  pStb = mndAcquireStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
1018
  if (pStb != NULL) {
S
Shengliang Guan 已提交
1019
    if (createReq.igExists) {
1020
      if (createReq.source == TD_REQ_FROM_APP) {
1021
        mInfo("stb:%s, already exist, ignore exist is set", createReq.name);
1022 1023 1024
        code = 0;
        goto _OVER;
      } else if (pStb->uid != createReq.suid) {
1025 1026
        mInfo("stb:%s, alter table does not need to be done, because table is deleted", createReq.name);
        code = 0;
1027 1028
        goto _OVER;
      } else if (createReq.tagVer > 0 || createReq.colVer > 0) {
wmmhello's avatar
wmmhello 已提交
1029 1030 1031
        int32_t tagDelta = createReq.tagVer - pStb->tagVer;
        int32_t colDelta = createReq.colVer - pStb->colVer;
        int32_t verDelta = tagDelta + colDelta;
1032 1033 1034 1035 1036 1037 1038
        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)) {
1039
          isAlter = true;
1040 1041 1042 1043 1044 1045 1046
          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 已提交
1047 1048
        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);
1049 1050 1051
        terrno = TSDB_CODE_MND_INVALID_SCHEMA_VER;
        goto _OVER;
      }
S
Shengliang Guan 已提交
1052 1053
    } else {
      terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
1054
      goto _OVER;
S
Shengliang Guan 已提交
1055
    }
S
Shengliang Guan 已提交
1056
  } else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
1057
    goto _OVER;
1058
  } else if (createReq.source == TD_REQ_FROM_TAOX && (createReq.tagVer != 1 || createReq.colVer != 1)) {
1059 1060 1061
    mInfo("stb:%s, alter table does not need to be done, because table is deleted", createReq.name);
    code = 0;
    goto _OVER;
S
Shengliang Guan 已提交
1062 1063
  }

S
Shengliang Guan 已提交
1064
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
1065 1066
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1067
    goto _OVER;
S
Shengliang Guan 已提交
1068 1069
  }

1070
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1071
    goto _OVER;
S
Shengliang Guan 已提交
1072 1073
  }

1074
  int32_t numOfStbs = -1;
S
Shengliang Guan 已提交
1075 1076 1077 1078
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
    goto _OVER;
  }

L
Liu Jicong 已提交
1079
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
1080 1081 1082 1083
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

wafwerar's avatar
wafwerar 已提交
1084 1085 1086 1087 1088
  if ((terrno = grantCheck(TSDB_GRANT_STABLE)) < 0) {
    code = -1;
    goto _OVER;
  }

1089
  if (isAlter) {
1090
    bool    needRsp = false;
wmmhello's avatar
wmmhello 已提交
1091
    SStbObj pDst = {0};
wmmhello's avatar
wmmhello 已提交
1092 1093 1094
    if (mndBuildStbFromAlter(pStb, &pDst, &createReq) != 0) {
      taosMemoryFreeClear(pDst.pTags);
      taosMemoryFreeClear(pDst.pColumns);
wmmhello's avatar
wmmhello 已提交
1095 1096 1097 1098 1099 1100
      goto _OVER;
    }

    code = mndAlterStbImp(pMnode, pReq, pDb, &pDst, needRsp, NULL, 0);
    taosMemoryFreeClear(pDst.pTags);
    taosMemoryFreeClear(pDst.pColumns);
1101 1102 1103
  } else {
    code = mndCreateStb(pMnode, pReq, &createReq, pDb);
  }
S
Shengliang Guan 已提交
1104
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1105

1106
_OVER:
S
Shengliang Guan 已提交
1107
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1108
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
1109 1110
  }

S
Shengliang Guan 已提交
1111 1112
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1113
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
1114 1115

  return code;
S
Shengliang Guan 已提交
1116 1117
}

S
Shengliang Guan 已提交
1118
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
1119 1120
  if (pAlter->commentLen >= 0) return 0;
  if (pAlter->ttl != 0) return 0;
S
Shengliang 已提交
1121

S
Shengliang Guan 已提交
1122 1123 1124 1125
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
1126

S
Shengliang Guan 已提交
1127 1128 1129
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
1130 1131 1132
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
1133 1134 1135 1136 1137
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
1138
int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) {
wafwerar's avatar
wafwerar 已提交
1139 1140
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
  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 已提交
1151 1152
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
                                         int32_t ttl) {
S
Shengliang 已提交
1153 1154
  if (commentLen > 0) {
    pNew->commentLen = commentLen;
wmmhello's avatar
wmmhello 已提交
1155
    pNew->comment = taosMemoryCalloc(1, commentLen + 1);
S
Shengliang 已提交
1156 1157 1158 1159
    if (pNew->comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
wmmhello's avatar
wmmhello 已提交
1160
    memcpy(pNew->comment, pComment, commentLen + 1);
1161
  } else if (commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1162
    pNew->commentLen = 0;
S
Shengliang Guan 已提交
1163
  } else {
S
Shengliang 已提交
1164
  }
wmmhello's avatar
wmmhello 已提交
1165

S
Shengliang 已提交
1166 1167 1168
  if (ttl >= 0) {
    pNew->ttl = ttl;
  }
S
Shengliang 已提交
1169 1170 1171 1172 1173 1174 1175

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

S
Shengliang Guan 已提交
1176
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
  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 已提交
1187 1188 1189 1190
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }
D
dmchen 已提交
1191 1192
 
  if(pNew->nextColId < 0 || pNew->nextColId >= 0x7fff - ntags){
D
dm chen 已提交
1193
    terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
D
dmchen 已提交
1194 1195 1196
    return -1;
  }

S
Shengliang Guan 已提交
1197
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
1198
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1199
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1200
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1201 1202 1203
      return -1;
    }

S
Shengliang 已提交
1204
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1205
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1206 1207 1208
      return -1;
    }

S
Shengliang Guan 已提交
1209 1210 1211 1212
    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 已提交
1213 1214
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1215

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

1219
  pNew->tagVer++;
S
Shengliang Guan 已提交
1220 1221 1222
  return 0;
}

1223
static int32_t mndCheckAlterColForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
1224 1225 1226 1227 1228 1229 1230
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SMqTopicObj *pTopic = NULL;
    pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
    if (pIter == NULL) break;

1231
    mInfo("topic:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, subType:%d sql:%s",
H
Hongze Cheng 已提交
1232
          pTopic->name, stbFullName, suid, colId, pTopic->subType, pTopic->sql);
1233
    if (pTopic->ast == NULL) {
1234 1235 1236 1237 1238 1239
      sdbRelease(pSdb, pTopic);
      continue;
    }

    SNode *pAst = NULL;
    if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
S
Shengliang Guan 已提交
1240 1241 1242
      terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TOPIC;
      mError("topic:%s, create ast error", pTopic->name);
      sdbRelease(pSdb, pTopic);
1243 1244 1245 1246 1247 1248 1249 1250
      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 已提交
1251 1252
      mInfo("topic:%s, check colId:%d tableId:%" PRId64 " ctbStbUid:%" PRId64, pTopic->name, pCol->colId, pCol->tableId,
            pTopic->ctbStbUid);
1253 1254

      if (pCol->tableId != suid && pTopic->ctbStbUid != suid) {
1255
        mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
1256 1257 1258 1259 1260
        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 已提交
1261 1262 1263
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pTopic);
1264 1265
        return -1;
      }
1266
      mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
1267 1268 1269 1270 1271
    }

  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1272
    nodesDestroyList(pNodeList);
1273
  }
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
  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 已提交
1287 1288 1289
      terrno = TSDB_CODE_MND_INVALID_STREAM_OPTION;
      mError("stream:%s, create ast error", pStream->name);
      sdbRelease(pSdb, pStream);
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
      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) {
1300
        mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
1301 1302 1303 1304 1305
        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 已提交
1306 1307 1308
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pStream);
1309 1310
        return -1;
      }
1311
      mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
1312 1313 1314 1315 1316
    }

  NEXT:
    sdbRelease(pSdb, pStream);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1317
    nodesDestroyList(pNodeList);
1318 1319 1320
  }
  return 0;
}
1321

1322 1323 1324
static int32_t mndCheckAlterColForTSma(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
1325 1326 1327 1328 1329
  while (1) {
    SSmaObj *pSma = NULL;
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
    if (pIter == NULL) break;

H
Hongze Cheng 已提交
1330 1331
    mInfo("tsma:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, sql:%s", pSma->name, stbFullName,
          suid, colId, pSma->sql);
1332 1333 1334 1335 1336

    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",
1337
             pSma->name, stbFullName, suid, colId);
1338 1339 1340 1341 1342 1343 1344 1345
      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;
1346
      mInfo("tsma:%s, check colId:%d tableId:%" PRId64, pSma->name, pCol->colId, pCol->tableId);
1347 1348

      if ((pCol->tableId != suid) && (pSma->stbUid != suid)) {
1349
        mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
1350
        goto NEXT;
1351 1352 1353 1354
      }
      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 已提交
1355 1356 1357
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pSma);
1358 1359
        return -1;
      }
1360
      mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
1361 1362
    }

1363
  NEXT:
1364 1365
    sdbRelease(pSdb, pSma);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1366
    nodesDestroyList(pNodeList);
1367
  }
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
  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;
  }
1378

1379 1380 1381
  if (mndCheckAlterColForTSma(pMnode, stbFullName, suid, colId) < 0) {
    return -1;
  }
1382 1383 1384
  return 0;
}

1385
static int32_t mndDropSuperTableTag(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
S
Shengliang Guan 已提交
1386 1387 1388 1389 1390 1391
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1392
  col_id_t colId = pOld->pTags[tag].colId;
1393
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1394 1395 1396
    return -1;
  }

S
Shengliang Guan 已提交
1397 1398 1399 1400 1401
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1404
  pNew->tagVer++;
dengyihao's avatar
dengyihao 已提交
1405 1406 1407 1408

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

1413
static int32_t mndAlterStbTagName(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
S
Shengliang Guan 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
  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 已提交
1425 1426 1427 1428 1429 1430
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1431
  col_id_t colId = pOld->pTags[tag].colId;
1432
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1433 1434 1435
    return -1;
  }

S
Shengliang Guan 已提交
1436
  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
S
Shengliang Guan 已提交
1437
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1438 1439 1440
    return -1;
  }

S
Shengliang Guan 已提交
1441 1442
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
    return -1;
  }

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

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

1453
  pNew->tagVer++;
1454
  mInfo("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
S
Shengliang Guan 已提交
1455 1456 1457
  return 0;
}

1458
static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1459
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1460 1461 1462 1463 1464
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

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

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

  SSchema *pTag = pNew->pTags + tag;

D
Dingle Zhang 已提交
1476
  if (!(pTag->type == TSDB_DATA_TYPE_BINARY || pTag->type == TSDB_DATA_TYPE_NCHAR || pTag->type == TSDB_DATA_TYPE_GEOMETRY)) {
S
Shengliang Guan 已提交
1477 1478 1479 1480
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
1481
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1482 1483 1484 1485
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1486
  pTag->bytes = pField->bytes;
1487
  pNew->tagVer++;
S
Shengliang Guan 已提交
1488

1489
  mInfo("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1490 1491 1492
  return 0;
}

S
Shengliang Guan 已提交
1493
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1494 1495 1496 1497 1498
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1499 1500 1501 1502 1503
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

D
dmchen 已提交
1504
  if(pNew->nextColId < 0 || pNew->nextColId >= 0x7fff - ncols){
D
dm chen 已提交
1505
    terrno = TSDB_CODE_MND_FIELD_VALUE_OVERFLOW;
D
dmchen 已提交
1506 1507 1508
    return -1;
  }

S
Shengliang Guan 已提交
1509
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1510
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1511
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1512
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1513 1514 1515
      return -1;
    }

S
Shengliang 已提交
1516
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1517
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1518 1519 1520
      return -1;
    }

S
Shengliang Guan 已提交
1521 1522 1523 1524
    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 已提交
1525 1526
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1527

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

1531
  pNew->colVer++;
S
Shengliang Guan 已提交
1532 1533 1534
  return 0;
}

1535
static int32_t mndDropSuperTableColumn(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *colName) {
S
Shengliang Guan 已提交
1536
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1537
  if (col < 0) {
S
Shengliang Guan 已提交
1538 1539 1540 1541
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
  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;
  }

1552
  col_id_t colId = pOld->pColumns[col].colId;
1553
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1554 1555 1556
    return -1;
  }

S
Shengliang Guan 已提交
1557 1558 1559 1560 1561
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1564
  pNew->colVer++;
1565
  mInfo("stb:%s, start to drop col %s", pNew->name, colName);
S
Shengliang Guan 已提交
1566 1567 1568
  return 0;
}

1569
static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1570
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1571 1572 1573 1574 1575 1576 1577
  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 已提交
1578
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1579 1580 1581 1582 1583 1584 1585
  }

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

1586
  col_id_t colId = pOld->pColumns[col].colId;
1587
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1588 1589 1590
    return -1;
  }

S
Shengliang Guan 已提交
1591 1592 1593 1594 1595
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pCol = pNew->pColumns + col;
D
Dingle Zhang 已提交
1596
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR || pCol->type == TSDB_DATA_TYPE_GEOMETRY)) {
S
Shengliang Guan 已提交
1597
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1598 1599 1600
    return -1;
  }

S
Shengliang Guan 已提交
1601
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1602
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1603 1604 1605
    return -1;
  }

S
Shengliang Guan 已提交
1606
  pCol->bytes = pField->bytes;
1607
  pNew->colVer++;
S
Shengliang Guan 已提交
1608

1609
  mInfo("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1610 1611 1612
  return 0;
}

S
Shengliang Guan 已提交
1613
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1614 1615
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
1616 1617 1618 1619
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1620
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1621 1622 1623 1624

  return 0;
}

S
Shengliang Guan 已提交
1625
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1626 1627
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
1628 1629 1630 1631
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1632 1633 1634 1635 1636
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

1637 1638
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, void *alterOriData,
                                         int32_t alterOriDataLen) {
S
Shengliang Guan 已提交
1639 1640 1641 1642 1643 1644 1645 1646
  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 已提交
1647
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
1648 1649 1650 1651
      sdbRelease(pSdb, pVgroup);
      continue;
    }

wmmhello's avatar
wmmhello 已提交
1652
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, alterOriData, alterOriDataLen);
S
Shengliang Guan 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661
    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 已提交
1662
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1663
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1664
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
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 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
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 已提交
1723 1724 1725
  tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
  tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
  tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
D
dapan1121 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
  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 已提交
1750
    pSchema->flags = pSrcSchema->flags;
D
dapan1121 已提交
1751 1752 1753 1754 1755 1756 1757 1758
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

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

D
dapan1121 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
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 已提交
1770 1771 1772
  tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
  tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
  tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
D
dapan1121 已提交
1773 1774 1775
  pRsp->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->tableType = TSDB_SUPER_TABLE;
D
dapan1121 已提交
1776 1777 1778 1779
  pRsp->delay1 = pStb->maxdelay[0];
  pRsp->delay2 = pStb->maxdelay[1];
  pRsp->watermark1 = pStb->watermark[0];
  pRsp->watermark2 = pStb->watermark[1];
D
dapan1121 已提交
1780 1781 1782
  pRsp->ttl = pStb->ttl;
  pRsp->commentLen = pStb->commentLen;
  if (pStb->commentLen > 0) {
1783
    pRsp->pComment = taosStrdup(pStb->comment);
D
dapan1121 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
  }

  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 已提交
1804
  if (pStb->numOfFuncs > 0) {
H
Haojun Liao 已提交
1805
    pRsp->pFuncs = taosArrayDup(pStb->pFuncs, NULL);
D
dapan1121 已提交
1806
  }
1807

D
dapan1121 已提交
1808 1809 1810 1811
  taosRUnLockLatch(&pStb->lock);
  return 0;
}

L
Liu Jicong 已提交
1812 1813
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp,
                                 int32_t *smaVer) {
D
dapan1121 已提交
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
  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 已提交
1826
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
D
dapan1121 已提交
1827 1828 1829
    return -1;
  }

D
dapan1121 已提交
1830 1831 1832 1833
  if (smaVer) {
    *smaVer = pStb->smaVer;
  }

D
dapan1121 已提交
1834 1835 1836 1837 1838 1839
  int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}

D
dapan1121 已提交
1840
static int32_t mndBuildStbCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
  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 已提交
1856

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

1859 1860 1861 1862
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}
D
dapan1121 已提交
1863

1864
static int32_t mndBuildSMAlterStbRsp(SDbObj *pDb, SStbObj *pObj, void **pCont, int32_t *pLen) {
1865
  int32_t       ret;
D
dapan1121 已提交
1866
  SEncoder      ec = {0};
1867
  uint32_t      contLen = 0;
D
dapan1121 已提交
1868
  SMAlterStbRsp alterRsp = {0};
1869
  SName         name = {0};
1870
  tNameFromString(&name, pObj->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
D
dapan1121 已提交
1871 1872 1873 1874 1875 1876

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

D
dapan1121 已提交
1878
  ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, alterRsp.pMeta);
D
dapan1121 已提交
1879 1880 1881 1882
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }
1883

D
dapan1121 已提交
1884 1885 1886 1887 1888 1889
  tEncodeSize(tEncodeSMAlterStbRsp, &alterRsp, contLen, ret);
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }

1890
  void *cont = taosMemoryMalloc(contLen);
D
dapan1121 已提交
1891 1892 1893 1894 1895 1896 1897 1898
  tEncoderInit(&ec, cont, contLen);
  tEncodeSMAlterStbRsp(&ec, &alterRsp);
  tEncoderClear(&ec);

  tFreeSMAlterStbRsp(&alterRsp);

  *pCont = cont;
  *pLen = contLen;
1899

D
dapan1121 已提交
1900 1901 1902
  return 0;
}

L
Liu Jicong 已提交
1903 1904
int32_t mndBuildSMCreateStbRsp(SMnode *pMnode, char *dbFName, char *stbFName, void **pCont, int32_t *pLen) {
  int32_t ret = -1;
1905 1906 1907 1908 1909 1910 1911 1912 1913
  SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
  if (NULL == pDb) {
    return -1;
  }

  SStbObj *pObj = mndAcquireStb(pMnode, stbFName);
  if (NULL == pObj) {
    goto _OVER;
  }
L
Liu Jicong 已提交
1914 1915 1916

  SEncoder       ec = {0};
  uint32_t       contLen = 0;
1917
  SMCreateStbRsp stbRsp = {0};
L
Liu Jicong 已提交
1918
  SName          name = {0};
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
  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 已提交
1950

1951 1952 1953 1954
_OVER:
  if (pObj) {
    mndReleaseStb(pMnode, pObj);
  }
L
Liu Jicong 已提交
1955

1956 1957 1958 1959 1960 1961 1962
  if (pDb) {
    mndReleaseDb(pMnode, pDb);
  }

  return ret;
}

1963 1964
static int32_t mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                              void *alterOriData, int32_t alterOriDataLen) {
1965
  int32_t code = -1;
1966
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "alter-stb");
1967 1968
  if (pTrans == NULL) goto _OVER;

1969
  mInfo("trans:%d, used to alter stb:%s", pTrans->id, pStb->name);
1970
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
1971
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981

  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 已提交
1982
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, pStb, alterOriData, alterOriDataLen) != 0) goto _OVER;
1983 1984 1985 1986 1987 1988 1989 1990 1991
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  code = 0;

_OVER:
  mndTransDrop(pTrans);
  return code;
}

dengyihao's avatar
dengyihao 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
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 已提交
2003 2004 2005 2006 2007 2008 2009
  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 已提交
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 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
  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 已提交
2060
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
2061 2062 2063 2064
  bool    needRsp = true;
  int32_t code = -1;
  SField *pField0 = NULL;

S
Shengliang Guan 已提交
2065 2066 2067
  SStbObj stbObj = {0};
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
2068
  taosRUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
2069 2070 2071
  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
D
dapan1121 已提交
2072
  stbObj.lock = 0;
dengyihao's avatar
dengyihao 已提交
2073
  bool updateTagIndex = false;
S
Shengliang Guan 已提交
2074
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
2075
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
2076
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
2077
      break;
S
Shengliang Guan 已提交
2078
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang 已提交
2079
      pField0 = taosArrayGet(pAlter->pFields, 0);
2080
      code = mndDropSuperTableTag(pMnode, pOld, &stbObj, pField0->name);
dengyihao's avatar
dengyihao 已提交
2081
      updateTagIndex = true;
S
Shengliang Guan 已提交
2082
      break;
S
Shengliang Guan 已提交
2083
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
2084
      code = mndAlterStbTagName(pMnode, pOld, &stbObj, pAlter->pFields);
dengyihao's avatar
dengyihao 已提交
2085
      updateTagIndex = true;
S
Shengliang Guan 已提交
2086
      break;
S
Shengliang Guan 已提交
2087
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang 已提交
2088
      pField0 = taosArrayGet(pAlter->pFields, 0);
2089
      code = mndAlterStbTagBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
2090 2091
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
2092
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
2093 2094
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang 已提交
2095
      pField0 = taosArrayGet(pAlter->pFields, 0);
2096
      code = mndDropSuperTableColumn(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
2097
      break;
S
Shengliang Guan 已提交
2098
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang 已提交
2099
      pField0 = taosArrayGet(pAlter->pFields, 0);
2100
      code = mndAlterStbColumnBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
2101
      break;
S
Shengliang 已提交
2102
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
D
dapan1121 已提交
2103
      needRsp = false;
S
Shengliang 已提交
2104
      code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
S
Shengliang 已提交
2105
      break;
S
Shengliang Guan 已提交
2106
    default:
D
dapan1121 已提交
2107
      needRsp = false;
S
Shengliang 已提交
2108
      terrno = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
2109 2110 2111
      break;
  }

2112
  if (code != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
2113 2114 2115 2116 2117
  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 已提交
2118

2119
_OVER:
wafwerar's avatar
wafwerar 已提交
2120 2121
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
2122 2123 2124
  if (pAlter->commentLen > 0) {
    taosMemoryFreeClear(stbObj.comment);
  }
S
Shengliang Guan 已提交
2125 2126
  return code;
}
S
Shengliang Guan 已提交
2127

2128
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
2129
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2130 2131 2132 2133
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
2134

S
Shengliang Guan 已提交
2135
  if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
2136
    terrno = TSDB_CODE_INVALID_MSG;
2137
    goto _OVER;
S
Shengliang Guan 已提交
2138
  }
S
Shengliang Guan 已提交
2139

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

S
Shengliang Guan 已提交
2143
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
2144 2145
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
2146
    goto _OVER;
S
Shengliang Guan 已提交
2147 2148
  }

S
Shengliang Guan 已提交
2149
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
2150 2151
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
2152
    goto _OVER;
S
Shengliang Guan 已提交
2153
  }
S
Shengliang Guan 已提交
2154

2155
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
2156
    goto _OVER;
S
Shengliang Guan 已提交
2157 2158
  }

S
Shengliang Guan 已提交
2159
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
2160
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
2161

2162
_OVER:
S
Shengliang Guan 已提交
2163
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2164
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
2165 2166
  }

S
Shengliang Guan 已提交
2167 2168
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
2169
  tFreeSMAltertbReq(&alterReq);
S
Shengliang Guan 已提交
2170 2171

  return code;
S
Shengliang Guan 已提交
2172
}
S
Shengliang Guan 已提交
2173

S
Shengliang Guan 已提交
2174 2175 2176
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
2177 2178 2179 2180
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
2181 2182 2183 2184 2185 2186 2187 2188
  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 已提交
2189 2190 2191 2192
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
2193 2194 2195 2196 2197
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
2198 2199 2200 2201
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 已提交
2202

S
Shengliang Guan 已提交
2203 2204 2205
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
2206
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
2207 2208 2209 2210
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
2211
    int32_t contLen = 0;
S
Shengliang Guan 已提交
2212
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
    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 已提交
2225
    action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
S
Shengliang Guan 已提交
2226
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
2227
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
2238
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
2239
  int32_t code = -1;
2240
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "drop-stb");
2241
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
2242

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

2247 2248 2249
  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 已提交
2250
  if (mndDropIdxsByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2251
  if (mndDropSmasByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2252
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
2253 2254
  code = 0;

2255
_OVER:
S
Shengliang Guan 已提交
2256
  mndTransDrop(pTrans);
S
Shengliang 已提交
2257
  return code;
S
Shengliang Guan 已提交
2258 2259
}

2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
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;
      }
    }

2275
    if (pTopic->ast == NULL) {
2276 2277 2278 2279 2280 2281
      sdbRelease(pSdb, pTopic);
      continue;
    }

    SNode *pAst = NULL;
    if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
S
Shengliang Guan 已提交
2282 2283 2284
      terrno = TSDB_CODE_MND_INVALID_TOPIC_OPTION;
      mError("topic:%s, create ast error", pTopic->name);
      sdbRelease(pSdb, pTopic);
2285 2286 2287 2288 2289 2290 2291 2292 2293
      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 已提交
2294
      if (pCol->tableId == suid) {
2295 2296
        sdbRelease(pSdb, pTopic);
        nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2297
        nodesDestroyList(pNodeList);
2298 2299 2300 2301 2302 2303 2304 2305
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2306
    nodesDestroyList(pNodeList);
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
  }
  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 已提交
2319 2320 2321 2322 2323 2324
    if (pStream->smaId != 0) {
      sdbRelease(pSdb, pStream);
      continue;
    }

    if (pStream->targetStbUid == suid) {
L
Liu Jicong 已提交
2325 2326 2327 2328
      sdbRelease(pSdb, pStream);
      return -1;
    }

2329 2330
    SNode *pAst = NULL;
    if (nodesStringToNode(pStream->ast, &pAst) != 0) {
S
Shengliang Guan 已提交
2331 2332 2333
      terrno = TSDB_CODE_MND_INVALID_STREAM_OPTION;
      mError("stream:%s, create ast error", pStream->name);
      sdbRelease(pSdb, pStream);
2334 2335 2336 2337 2338 2339 2340 2341 2342
      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 已提交
2343
      if (pCol->tableId == suid) {
2344 2345
        sdbRelease(pSdb, pStream);
        nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2346
        nodesDestroyList(pNodeList);
2347 2348 2349 2350 2351 2352 2353 2354
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pStream);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2355
    nodesDestroyList(pNodeList);
2356 2357 2358 2359
  }
  return 0;
}

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

2362
static int32_t mndProcessDropStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
2363
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2364 2365 2366
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
2367
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
2368

S
Shengliang Guan 已提交
2369
  if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
2370
    terrno = TSDB_CODE_INVALID_MSG;
2371
    goto _OVER;
S
Shengliang Guan 已提交
2372
  }
S
Shengliang Guan 已提交
2373

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

S
Shengliang Guan 已提交
2376
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2377
  if (pStb == NULL) {
S
Shengliang Guan 已提交
2378
    if (dropReq.igNotExists) {
2379
      mInfo("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
2380
      code = 0;
2381
      goto _OVER;
S
Shengliang Guan 已提交
2382 2383
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
2384
      goto _OVER;
S
Shengliang Guan 已提交
2385 2386 2387
    }
  }

wmmhello's avatar
wmmhello 已提交
2388 2389
  if (dropReq.source == TD_REQ_FROM_TAOX && pStb->uid != dropReq.suid) {
    code = 0;
wmmhello's avatar
wmmhello 已提交
2390 2391 2392
    goto _OVER;
  }

S
Shengliang Guan 已提交
2393
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2394 2395
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
2396
    goto _OVER;
S
Shengliang Guan 已提交
2397 2398
  }

2399
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
2400
    goto _OVER;
S
Shengliang Guan 已提交
2401 2402
  }

2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
  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 已提交
2413
  code = mndDropStb(pMnode, pReq, pDb, pStb);
S
Shengliang Guan 已提交
2414
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
2415

2416
_OVER:
S
Shengliang Guan 已提交
2417
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2418
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
2419 2420
  }

S
Shengliang Guan 已提交
2421 2422 2423
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
S
Shengliang Guan 已提交
2424
}
S
Shengliang Guan 已提交
2425

S
Shengliang Guan 已提交
2426 2427
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2428 2429 2430
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
2431

2432 2433 2434 2435
  SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user);
  if (pUser == NULL) return 0;
  bool sysinfo = pUser->sysInfo;

S
Shengliang Guan 已提交
2436
  if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
2437
    terrno = TSDB_CODE_INVALID_MSG;
2438
    goto _OVER;
S
Shengliang Guan 已提交
2439
  }
D
dapan 已提交
2440

D
dapan1121 已提交
2441
  if (0 == strcmp(infoReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2442
    mInfo("information_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
2443
    if (mndBuildInsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, sysinfo, &metaRsp) != 0) {
2444
      goto _OVER;
D
dapan1121 已提交
2445
    }
D
dapan1121 已提交
2446
  } else if (0 == strcmp(infoReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2447
    mInfo("performance_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
2448
    if (mndBuildPerfsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
2449
      goto _OVER;
D
dapan1121 已提交
2450
    }
D
dapan1121 已提交
2451
  } else {
2452
    mInfo("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
2453
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp, NULL) != 0) {
2454
      goto _OVER;
D
dapan1121 已提交
2455
    }
S
Shengliang Guan 已提交
2456
  }
S
Shengliang Guan 已提交
2457

S
Shengliang Guan 已提交
2458 2459 2460
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
2461
    goto _OVER;
S
Shengliang Guan 已提交
2462
  }
S
Shengliang Guan 已提交
2463

S
Shengliang Guan 已提交
2464
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
2465 2466
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2467
    goto _OVER;
S
Shengliang Guan 已提交
2468
  }
D
dapan 已提交
2469

S
Shengliang Guan 已提交
2470
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
2471 2472
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
S
Shengliang Guan 已提交
2473
  code = 0;
S
Shengliang Guan 已提交
2474

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

2477
_OVER:
S
Shengliang Guan 已提交
2478 2479 2480
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
2481

2482
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
2483 2484 2485
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
2486

D
dapan1121 已提交
2487
static int32_t mndProcessTableCfgReq(SRpcMsg *pReq) {
2488 2489 2490 2491
  SMnode      *pMnode = pReq->info.node;
  int32_t      code = -1;
  STableCfgReq cfgReq = {0};
  STableCfgRsp cfgRsp = {0};
D
dapan1121 已提交
2492 2493 2494 2495 2496 2497 2498

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

  if (0 == strcmp(cfgReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2499
    mInfo("information_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2500 2501 2502 2503
    if (mndBuildInsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else if (0 == strcmp(cfgReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2504
    mInfo("performance_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2505 2506 2507 2508
    if (mndBuildPerfsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else {
2509
    mInfo("stb:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
    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 已提交
2543
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
S
Shengliang Guan 已提交
2544
                           int32_t *pRspLen) {
D
dapan1121 已提交
2545
  SSTbHbRsp hbRsp = {0};
D
dapan1121 已提交
2546 2547
  hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (hbRsp.pMetaRsp == NULL) {
S
Shengliang Guan 已提交
2548 2549 2550
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
2551

D
dapan1121 已提交
2552
  hbRsp.pIndexRsp = taosArrayInit(numOfStbs, sizeof(STableIndexRsp));
D
dapan1121 已提交
2553
  if (NULL == hbRsp.pIndexRsp) {
D
dapan1121 已提交
2554
    taosArrayDestroy(hbRsp.pMetaRsp);
D
dapan1121 已提交
2555 2556 2557
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
2558

S
Shengliang Guan 已提交
2559
  for (int32_t i = 0; i < numOfStbs; ++i) {
D
dapan1121 已提交
2560
    SSTableVersion *pStbVersion = &pStbVersions[i];
S
Shengliang Guan 已提交
2561 2562 2563
    pStbVersion->suid = be64toh(pStbVersion->suid);
    pStbVersion->sversion = ntohs(pStbVersion->sversion);
    pStbVersion->tversion = ntohs(pStbVersion->tversion);
D
dapan1121 已提交
2564
    pStbVersion->smaVer = ntohl(pStbVersion->smaVer);
S
Shengliang Guan 已提交
2565

S
Shengliang Guan 已提交
2566
    STableMetaRsp metaRsp = {0};
L
Liu Jicong 已提交
2567
    int32_t       smaVer = 0;
2568
    mInfo("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2569
    if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp, &smaVer) != 0) {
S
Shengliang Guan 已提交
2570 2571
      metaRsp.numOfColumns = -1;
      metaRsp.suid = pStbVersion->suid;
2572 2573 2574
      tstrncpy(metaRsp.dbFName, pStbVersion->dbFName, sizeof(metaRsp.dbFName));
      tstrncpy(metaRsp.tbName, pStbVersion->stbName, sizeof(metaRsp.tbName));
      tstrncpy(metaRsp.stbName, pStbVersion->stbName, sizeof(metaRsp.stbName));
D
dapan1121 已提交
2575
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2576
      continue;
D
dapan 已提交
2577
    }
S
Shengliang Guan 已提交
2578

D
dapan1121 已提交
2579
    if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
D
dapan1121 已提交
2580
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2581 2582
    } else {
      tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
2583
    }
D
dapan1121 已提交
2584

D
dapan1121 已提交
2585
    if (pStbVersion->smaVer && pStbVersion->smaVer != smaVer) {
L
Liu Jicong 已提交
2586 2587
      bool           exist = false;
      char           tbFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
2588
      STableIndexRsp indexRsp = {0};
D
dapan1121 已提交
2589 2590 2591 2592 2593
      indexRsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
      if (NULL == indexRsp.pIndex) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
L
Liu Jicong 已提交
2594

D
dapan1121 已提交
2595
      sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2596
      int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist);
D
dapan1121 已提交
2597
      if (code || !exist) {
D
dapan1121 已提交
2598 2599 2600
        indexRsp.suid = pStbVersion->suid;
        indexRsp.version = -1;
        indexRsp.pIndex = NULL;
D
dapan1121 已提交
2601
      }
D
dapan1121 已提交
2602

D
dapan1121 已提交
2603 2604
      strcpy(indexRsp.dbFName, pStbVersion->dbFName);
      strcpy(indexRsp.tbName, pStbVersion->stbName);
D
dapan1121 已提交
2605 2606

      taosArrayPush(hbRsp.pIndexRsp, &indexRsp);
D
dapan1121 已提交
2607
    }
S
Shengliang Guan 已提交
2608
  }
S
Shengliang Guan 已提交
2609

D
dapan1121 已提交
2610
  int32_t rspLen = tSerializeSSTbHbRsp(NULL, 0, &hbRsp);
S
Shengliang Guan 已提交
2611
  if (rspLen < 0) {
D
dapan1121 已提交
2612
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2613 2614
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
2615 2616
  }

wafwerar's avatar
wafwerar 已提交
2617
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
2618
  if (pRsp == NULL) {
D
dapan1121 已提交
2619
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2620 2621
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
2622 2623
  }

D
dapan1121 已提交
2624 2625
  tSerializeSSTbHbRsp(pRsp, rspLen, &hbRsp);
  tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2626 2627
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
2628 2629 2630
  return 0;
}

2631
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
2632
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2633 2634 2635 2636 2637 2638
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
2639
  int32_t numOfStbs = 0;
2640
  void   *pIter = NULL;
S
Shengliang Guan 已提交
2641
  while (1) {
S
Shengliang Guan 已提交
2642
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
2643
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2644 2645
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
2646
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
2647
      numOfStbs++;
S
Shengliang Guan 已提交
2648 2649
    }

S
Shengliang Guan 已提交
2650
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2651 2652
  }

S
Shengliang Guan 已提交
2653
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
2654
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
2655 2656 2657
  return 0;
}

L
Liu Jicong 已提交
2658 2659 2660 2661 2662 2663 2664
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 已提交
2665 2666 2667 2668 2669 2670 2671
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 已提交
2672
void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t dstSize) {
S
Shengliang Guan 已提交
2673 2674
  int32_t pos = -1;
  int32_t num = 0;
L
Liu Jicong 已提交
2675 2676
  for (pos = 0; stbFullName[pos] != 0; ++pos) {
    if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
2677 2678 2679 2680
    if (num == 2) break;
  }

  if (num == 2) {
L
Liu Jicong 已提交
2681
    tstrncpy(dst, stbFullName + pos + 1, dstSize);
S
Shengliang Guan 已提交
2682 2683 2684
  }
}

dengyihao's avatar
dengyihao 已提交
2685 2686 2687 2688 2689 2690
// 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 已提交
2691
//
dengyihao's avatar
dengyihao 已提交
2692 2693 2694 2695 2696
//   SRetrieveTableReq retrieveReq = {0};
//   if (tDeserializeSRetrieveTableReq(pReq->pCont, pReq->contLen, &retrieveReq) != 0) {
//     terrno = TSDB_CODE_INVALID_MSG;
//     return -1;
//   }
wmmhello's avatar
wmmhello 已提交
2697
//
dengyihao's avatar
dengyihao 已提交
2698 2699 2700 2701 2702
//   SMnode    *pMnode = pReq->info.node;
//   SSdb      *pSdb = pMnode->pSdb;
//   int32_t    numOfRows = 0;
//   SDbObj    *pDb = NULL;
//   ESdbStatus objStatus = 0;
wmmhello's avatar
wmmhello 已提交
2703
//
dengyihao's avatar
dengyihao 已提交
2704 2705 2706
//   SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user);
//   if (pUser == NULL) return 0;
//   bool sysinfo = pUser->sysInfo;
wmmhello's avatar
wmmhello 已提交
2707
//
dengyihao's avatar
dengyihao 已提交
2708
//   // Append the information_schema database into the result.
wmmhello's avatar
wmmhello 已提交
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
////  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++);
2770
//      colDataSetVal(pColInfo, numOfRows, (const char *)stbName, false);
wmmhello's avatar
wmmhello 已提交
2771 2772 2773 2774 2775 2776 2777
//
//      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++);
2778
//      colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
wmmhello's avatar
wmmhello 已提交
2779 2780
//
//      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2781
//      colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->createdTime, false);
wmmhello's avatar
wmmhello 已提交
2782 2783
//
//      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2784
//      colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->numOfColumns, false);
wmmhello's avatar
wmmhello 已提交
2785 2786
//
//      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2787
//      colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->numOfTags, false);
wmmhello's avatar
wmmhello 已提交
2788 2789
//
//      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2790
//      colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->updateTime, false);  // number of tables
wmmhello's avatar
wmmhello 已提交
2791 2792 2793 2794 2795
//
//      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);
2796
//        colDataSetVal(pColInfo, numOfRows, comment, false);
wmmhello's avatar
wmmhello 已提交
2797 2798 2799
//      } else if (pStb->commentLen == 0) {
//        char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
//        STR_TO_VARSTR(comment, "");
2800
//        colDataSetVal(pColInfo, numOfRows, comment, false);
wmmhello's avatar
wmmhello 已提交
2801
//      } else {
X
Xiaoyu Wang 已提交
2802
//        colDataSetNULL(pColInfo, numOfRows);
wmmhello's avatar
wmmhello 已提交
2803 2804 2805 2806 2807 2808 2809
//      }
//
//      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++);
2810
//      colDataSetVal(pColInfo, numOfRows, (const char *)watermark, false);
wmmhello's avatar
wmmhello 已提交
2811 2812 2813 2814 2815 2816
//
//      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++);
2817
//      colDataSetVal(pColInfo, numOfRows, (const char *)maxDelay, false);
wmmhello's avatar
wmmhello 已提交
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
//
//      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++);
2836
//      colDataSetVal(pColInfo, numOfRows, (const char *)rollup, false);
wmmhello's avatar
wmmhello 已提交
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 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
//
//      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 已提交
2961 2962
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2963
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2964 2965 2966
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
2967

H
Hongze Cheng 已提交
2968
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
2969 2970
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
2971
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
2972
  }
S
Shengliang Guan 已提交
2973

S
Shengliang Guan 已提交
2974
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
2975
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2976 2977
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
2978
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
2979
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2980 2981 2982 2983 2984
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
2985
    SName name = {0};
H
Hongze Cheng 已提交
2986
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
2987
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
2988
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
2989

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

H
Hongze Cheng 已提交
2993 2994
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
2995 2996 2997
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

2998
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2999
    colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
3000 3001

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

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

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

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

3013
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
3014 3015 3016
    if (pStb->commentLen > 0) {
      char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, pStb->comment);
3017
      colDataSetVal(pColInfo, numOfRows, comment, false);
3018
    } else if (pStb->commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
3019 3020
      char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, "");
3021
      colDataSetVal(pColInfo, numOfRows, comment, false);
wmmhello's avatar
wmmhello 已提交
3022
    } else {
3023
      colDataSetNULL(pColInfo, numOfRows);
S
Shengliang Guan 已提交
3024
    }
H
Haojun Liao 已提交
3025

3026 3027 3028 3029 3030
    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++);
3031
    colDataSetVal(pColInfo, numOfRows, (const char *)watermark, false);
3032 3033 3034 3035 3036 3037

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

S
Shengliang Guan 已提交
3040
    char    rollup[160 + VARSTR_HEADER_SIZE] = {0};
3041
    int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs);
D
dapan1121 已提交
3042 3043
    char   *sep = ", ";
    int32_t sepLen = strlen(sep);
D
dapan1121 已提交
3044
    int32_t rollupLen = sizeof(rollup) - VARSTR_HEADER_SIZE - 2;
3045 3046 3047
    for (int32_t i = 0; i < rollupNum; ++i) {
      char *funcName = taosArrayGet(pStb->pFuncs, i);
      if (i) {
D
dapan1121 已提交
3048 3049
        strncat(varDataVal(rollup), sep, rollupLen);
        rollupLen -= sepLen;
3050
      }
D
dapan1121 已提交
3051 3052
      strncat(varDataVal(rollup), funcName, rollupLen);
      rollupLen -= strlen(funcName);
3053 3054 3055 3056
    }
    varDataSetLen(rollup, strlen(varDataVal(rollup)));

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

S
Shengliang Guan 已提交
3059
    numOfRows++;
S
Shengliang Guan 已提交
3060
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
3061 3062
  }

H
Haojun Liao 已提交
3063 3064 3065 3066
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

3067
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
3068 3069 3070
  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3071 3072
static int32_t buildDbColsInfoBlock(const SSDataBlock *p, const SSysTableMeta *pSysDbTableMeta, size_t size,
                                    const char *dbName, const char *tbName) {
wmmhello's avatar
wmmhello 已提交
3073 3074 3075 3076 3077 3078 3079 3080 3081
  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 已提交
3082 3083 3084 3085 3086
    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 已提交
3087 3088 3089
      continue;
    }

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

dengyihao's avatar
dengyihao 已提交
3092
    for (int32_t j = 0; j < pm->colNum; j++) {
wmmhello's avatar
wmmhello 已提交
3093
      // table name
dengyihao's avatar
dengyihao 已提交
3094
      SColumnInfoData *pColInfoData = taosArrayGet(p->pDataBlock, 0);
3095
      colDataSetVal(pColInfoData, numOfRows, tName, false);
wmmhello's avatar
wmmhello 已提交
3096

wmmhello's avatar
wmmhello 已提交
3097
      // database name
wmmhello's avatar
wmmhello 已提交
3098
      pColInfoData = taosArrayGet(p->pDataBlock, 1);
3099
      colDataSetVal(pColInfoData, numOfRows, dName, false);
wmmhello's avatar
wmmhello 已提交
3100 3101

      pColInfoData = taosArrayGet(p->pDataBlock, 2);
3102
      colDataSetVal(pColInfoData, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3103 3104 3105 3106 3107

      // 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);
3108
      colDataSetVal(pColInfoData, numOfRows, colName, false);
wmmhello's avatar
wmmhello 已提交
3109 3110 3111 3112 3113 3114 3115

      // 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 已提交
3116 3117
        colTypeLen +=
            sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", (int32_t)(pm->schema[j].bytes - VARSTR_HEADER_SIZE));
wmmhello's avatar
wmmhello 已提交
3118
      } else if (colType == TSDB_DATA_TYPE_NCHAR) {
dengyihao's avatar
dengyihao 已提交
3119 3120
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)((pm->schema[j].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
wmmhello's avatar
wmmhello 已提交
3121 3122
      }
      varDataSetLen(colTypeStr, colTypeLen);
3123
      colDataSetVal(pColInfoData, numOfRows, (char *)colTypeStr, false);
wmmhello's avatar
wmmhello 已提交
3124 3125

      pColInfoData = taosArrayGet(p->pDataBlock, 5);
3126
      colDataSetVal(pColInfoData, numOfRows, (const char *)&pm->schema[j].bytes, false);
wmmhello's avatar
wmmhello 已提交
3127 3128
      for (int32_t k = 6; k <= 8; ++k) {
        pColInfoData = taosArrayGet(p->pDataBlock, k);
X
Xiaoyu Wang 已提交
3129
        colDataSetNULL(pColInfoData, numOfRows);
wmmhello's avatar
wmmhello 已提交
3130 3131 3132 3133 3134 3135 3136 3137 3138
      }

      numOfRows += 1;
    }
  }

  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3139
static int32_t buildSysDbColsInfo(SSDataBlock *p, char *db, char *tb) {
wmmhello's avatar
wmmhello 已提交
3140
  size_t               size = 0;
dengyihao's avatar
dengyihao 已提交
3141
  const SSysTableMeta *pSysDbTableMeta = NULL;
wmmhello's avatar
wmmhello 已提交
3142

dengyihao's avatar
dengyihao 已提交
3143 3144
  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 已提交
3145 3146 3147
    return p->info.rows;
  }

wmmhello's avatar
wmmhello 已提交
3148
  getInfosDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3149
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_INFORMATION_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3150 3151

  getPerfDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3152
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_PERFORMANCE_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3153 3154 3155 3156 3157 3158 3159 3160 3161

  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;

3162 3163 3164 3165 3166 3167 3168 3169

  int32_t numOfRows = 0;
  if (!pShow->sysDbRsp) {
    numOfRows = buildSysDbColsInfo(pBlock, pShow->db, pShow->filterTb);
    mDebug("mndRetrieveStbCol get system table cols, rows:%d, db:%s", numOfRows, pShow->db);
    pShow->sysDbRsp = true;
  }

wmmhello's avatar
wmmhello 已提交
3170 3171 3172 3173 3174 3175
  SDbObj *pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
    if (pDb == NULL) return terrno;
  }

dengyihao's avatar
dengyihao 已提交
3176
  char typeName[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
wmmhello's avatar
wmmhello 已提交
3177
  STR_TO_VARSTR(typeName, "SUPER_TABLE");
D
dapan1121 已提交
3178 3179
  bool fetch = pShow->restore ? false : true;
  pShow->restore = false;
wmmhello's avatar
wmmhello 已提交
3180
  while (numOfRows < rows) {
D
dapan1121 已提交
3181 3182 3183 3184 3185 3186 3187 3188 3189
    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 已提交
3190 3191 3192 3193 3194 3195 3196 3197 3198

    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 已提交
3199
    if (pShow->filterTb[0] && strncmp(pShow->filterTb, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN) != 0) {
wmmhello's avatar
wmmhello 已提交
3200 3201 3202
      sdbRelease(pSdb, pStb);
      continue;
    }
D
dapan1121 已提交
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213

    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 已提交
3214 3215
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));

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

wmmhello's avatar
wmmhello 已提交
3218 3219 3220 3221 3222
    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 已提交
3223 3224
    for (int i = 0; i < pStb->numOfColumns; i++) {
      int32_t          cols = 0;
wmmhello's avatar
wmmhello 已提交
3225
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3226
      colDataSetVal(pColInfo, numOfRows, (const char *)stbName, false);
wmmhello's avatar
wmmhello 已提交
3227 3228

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3229
      colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
wmmhello's avatar
wmmhello 已提交
3230 3231

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3232
      colDataSetVal(pColInfo, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3233 3234 3235 3236 3237

      // 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++);
3238
      colDataSetVal(pColInfo, numOfRows, colName, false);
wmmhello's avatar
wmmhello 已提交
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248

      // 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 已提交
3249 3250
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)((pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
wmmhello's avatar
wmmhello 已提交
3251 3252
      }
      varDataSetLen(colTypeStr, colTypeLen);
3253
      colDataSetVal(pColInfo, numOfRows, (char *)colTypeStr, false);
wmmhello's avatar
wmmhello 已提交
3254 3255

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
3256
      colDataSetVal(pColInfo, numOfRows, (const char *)&pStb->pColumns[i].bytes, false);
dengyihao's avatar
dengyihao 已提交
3257
      while (cols < pShow->numOfColumns) {
wmmhello's avatar
wmmhello 已提交
3258
        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
X
Xiaoyu Wang 已提交
3259
        colDataSetNULL(pColInfo, numOfRows);
wmmhello's avatar
wmmhello 已提交
3260
      }
wmmhello's avatar
wmmhello 已提交
3261
      numOfRows++;
wmmhello's avatar
wmmhello 已提交
3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
    }

    sdbRelease(pSdb, pStb);
  }

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

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

wmmhello's avatar
wmmhello 已提交
3274 3275 3276
  return numOfRows;
}

S
Shengliang Guan 已提交
3277
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
3278 3279
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
3280
}
S
Shengliang Guan 已提交
3281 3282 3283 3284 3285 3286 3287 3288 3289 3290

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 已提交
3291
}
dengyihao's avatar
dengyihao 已提交
3292 3293 3294 3295 3296 3297

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

dengyihao's avatar
dengyihao 已提交
3298
/*int32_t mndAddIndexImpl(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp, void *sql,
dengyihao's avatar
dengyihao 已提交
3299 3300
                        int32_t len) {
  // impl later
dengyihao's avatar
dengyihao 已提交
3301
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
3302 3303 3304 3305 3306 3307 3308
  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 已提交
3309 3310
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3311
  if (mndSetAlterStbRedoActions2(pMnode, pTrans, pDb, pStb, sql, len) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3312 3313
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
3314 3315 3316 3317 3318
  return code;

_OVER:
  mndTransDrop(pTrans);
  return code;
dengyihao's avatar
dengyihao 已提交
3319
}
dengyihao's avatar
dengyihao 已提交
3320 3321 3322 3323 3324
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 已提交
3325 3326 3327
  SStbObj  stbObj = {0};
  SStbObj *pNew = &stbObj;

dengyihao's avatar
dengyihao 已提交
3328 3329 3330 3331 3332 3333 3334 3335 3336
  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 已提交
3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351
  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 已提交
3352
    terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
dengyihao's avatar
dengyihao 已提交
3353 3354 3355 3356 3357 3358
    return -1;
  } else {
    pTag->flags |= COL_IDX_ON;
  }
  pNew->tagVer++;

dengyihao's avatar
dengyihao 已提交
3359 3360 3361
  code = mndAddIndexImpl(pMnode, pReq, pDb, pNew, needRsp, pReq->pCont, pReq->contLen);

  return code;
dengyihao's avatar
dengyihao 已提交
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396
}
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 已提交
3397 3398 3399
  if (terrno == TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST || terrno == TSDB_CODE_MND_TAG_NOT_EXIST) {
    return terrno;
  } else {
dengyihao's avatar
dengyihao 已提交
3400
    if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
dengyihao's avatar
dengyihao 已提交
3401
  }
dengyihao's avatar
dengyihao 已提交
3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
_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 已提交
3424
}*/