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

  return 0;
}

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

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

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

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

  return 0;
}

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

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

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

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

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

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

799
  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
S
Shengliang Guan 已提交
800
    SField  *pField = taosArrayGet(pCreate->pColumns, i);
801
    SSchema *pSchema = &pDst->pColumns[i];
S
Shengliang Guan 已提交
802 803
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
804
    pSchema->flags = pField->flags;
S
Shengliang Guan 已提交
805
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
806 807
    pSchema->colId = pDst->nextColId;
    pDst->nextColId++;
S
Shengliang Guan 已提交
808 809
  }

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

S
Shengliang Guan 已提交
825
static int32_t mndCreateStb(SMnode *pMnode, SRpcMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
826
  SStbObj stbObj = {0};
S
Shengliang 已提交
827
  int32_t code = -1;
828

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

832
  mInfo("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
833
  if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
834
  if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;
835
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
836 837
  code = 0;

838
_OVER:
S
Shengliang Guan 已提交
839
  mndTransDrop(pTrans);
840
  mndStbActionDelete(pMnode->pSdb, &stbObj);
S
Shengliang Guan 已提交
841
  return code;
S
Shengliang Guan 已提交
842
}
S
Shengliang Guan 已提交
843

844
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
845
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
846
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) return -1;
847 848 849 850 851 852 853
  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 已提交
854

S
Shengliang Guan 已提交
855
static int32_t mndProcessTtlTimer(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
856 857 858 859 860 861 862
  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 已提交
863

864 865
  mInfo("start to process ttl timer");

wmmhello's avatar
wmmhello 已提交
866 867 868 869
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;

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

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

wmmhello's avatar
wmmhello 已提交
891 892 893
  return 0;
}

wmmhello's avatar
wmmhello 已提交
894 895
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
X
Xiaoyu Wang 已提交
896
    if (strcmp(pStb->pTags[tag].name, tagName) == 0) {
wmmhello's avatar
wmmhello 已提交
897 898 899 900 901 902 903 904 905
      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 已提交
906
    if (strcmp(pStb->pColumns[col].name, colName) == 0) {
wmmhello's avatar
wmmhello 已提交
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
      return col;
    }
  }

  return -1;
}

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

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

  for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
    SField  *pField = taosArrayGet(createReq->pColumns, i);
    SSchema *pSchema = &pDst->pColumns[i];
    pSchema->type = pField->type;
    pSchema->bytes = pField->bytes;
    pSchema->flags = pField->flags;
    memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
    int32_t cIndex = mndFindSuperTableColumnIndex(pStb, pField->name);
937
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
938
      pSchema->colId = pStb->pColumns[cIndex].colId;
939
    } else {
wmmhello's avatar
wmmhello 已提交
940 941 942 943 944 945 946 947 948 949 950
      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);
951
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
952
      pSchema->colId = pStb->pTags[cIndex].colId;
953
    } else {
wmmhello's avatar
wmmhello 已提交
954 955 956 957 958 959 960 961
      pSchema->colId = pDst->nextColId++;
    }
  }
  pDst->tagVer = createReq->tagVer;
  pDst->colVer = createReq->colVer;
  return TSDB_CODE_SUCCESS;
}

962
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
963
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
964 965 966 967
  int32_t        code = -1;
  SStbObj       *pStb = NULL;
  SDbObj        *pDb = NULL;
  SMCreateStbReq createReq = {0};
968
  bool           isAlter = false;
S
Shengliang Guan 已提交
969

S
Shengliang Guan 已提交
970
  if (tDeserializeSMCreateStbReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
971
    terrno = TSDB_CODE_INVALID_MSG;
972
    goto _OVER;
S
Shengliang Guan 已提交
973
  }
S
Shengliang Guan 已提交
974

975
  mInfo("stb:%s, start to create", createReq.name);
S
Shengliang Guan 已提交
976 977
  if (mndCheckCreateStbReq(&createReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
978
    goto _OVER;
S
Shengliang Guan 已提交
979
  }
S
Shengliang Guan 已提交
980

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

S
Shengliang Guan 已提交
1028
  pDb = mndAcquireDbByStb(pMnode, createReq.name);
S
Shengliang Guan 已提交
1029 1030
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
1031
    goto _OVER;
S
Shengliang Guan 已提交
1032 1033
  }

1034
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
1035
    goto _OVER;
S
Shengliang Guan 已提交
1036 1037
  }

1038
  int32_t numOfStbs = -1;
S
Shengliang Guan 已提交
1039 1040 1041 1042
  if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
    goto _OVER;
  }

L
Liu Jicong 已提交
1043
  if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
1044 1045 1046 1047
    terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
    goto _OVER;
  }

wafwerar's avatar
wafwerar 已提交
1048 1049 1050 1051 1052
  if ((terrno = grantCheck(TSDB_GRANT_STABLE)) < 0) {
    code = -1;
    goto _OVER;
  }

1053
  if (isAlter) {
1054
    bool    needRsp = false;
wmmhello's avatar
wmmhello 已提交
1055
    SStbObj pDst = {0};
wmmhello's avatar
wmmhello 已提交
1056 1057 1058
    if (mndBuildStbFromAlter(pStb, &pDst, &createReq) != 0) {
      taosMemoryFreeClear(pDst.pTags);
      taosMemoryFreeClear(pDst.pColumns);
wmmhello's avatar
wmmhello 已提交
1059 1060 1061 1062 1063 1064
      goto _OVER;
    }

    code = mndAlterStbImp(pMnode, pReq, pDb, &pDst, needRsp, NULL, 0);
    taosMemoryFreeClear(pDst.pTags);
    taosMemoryFreeClear(pDst.pColumns);
1065 1066 1067
  } else {
    code = mndCreateStb(pMnode, pReq, &createReq, pDb);
  }
S
Shengliang Guan 已提交
1068
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
1069

1070
_OVER:
S
Shengliang Guan 已提交
1071
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
1072
    mError("stb:%s, failed to create since %s", createReq.name, terrstr());
S
Shengliang Guan 已提交
1073 1074
  }

S
Shengliang Guan 已提交
1075 1076
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
1077
  tFreeSMCreateStbReq(&createReq);
S
Shengliang Guan 已提交
1078 1079

  return code;
S
Shengliang Guan 已提交
1080 1081
}

S
Shengliang Guan 已提交
1082
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
S
Shengliang Guan 已提交
1083 1084
  if (pAlter->commentLen >= 0) return 0;
  if (pAlter->ttl != 0) return 0;
S
Shengliang 已提交
1085

S
Shengliang Guan 已提交
1086 1087 1088 1089
  if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }
S
Shengliang Guan 已提交
1090

S
Shengliang Guan 已提交
1091 1092 1093
  for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
    SField *pField = taosArrayGet(pAlter->pFields, i);
    if (pField->name[0] == 0) {
S
Shengliang Guan 已提交
1094 1095 1096
      terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
      return -1;
    }
S
Shengliang Guan 已提交
1097 1098 1099 1100 1101
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
1102
int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) {
wafwerar's avatar
wafwerar 已提交
1103 1104
  pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
  pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
S
Shengliang Guan 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  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 已提交
1115 1116
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
                                         int32_t ttl) {
S
Shengliang 已提交
1117 1118
  if (commentLen > 0) {
    pNew->commentLen = commentLen;
wmmhello's avatar
wmmhello 已提交
1119
    pNew->comment = taosMemoryCalloc(1, commentLen + 1);
S
Shengliang 已提交
1120 1121 1122 1123
    if (pNew->comment == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
wmmhello's avatar
wmmhello 已提交
1124
    memcpy(pNew->comment, pComment, commentLen + 1);
1125
  } else if (commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1126
    pNew->commentLen = 0;
S
Shengliang Guan 已提交
1127
  } else {
S
Shengliang 已提交
1128
  }
wmmhello's avatar
wmmhello 已提交
1129

S
Shengliang 已提交
1130 1131 1132
  if (ttl >= 0) {
    pNew->ttl = ttl;
  }
S
Shengliang 已提交
1133 1134 1135 1136 1137 1138 1139

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

S
Shengliang Guan 已提交
1140
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
S
Shengliang Guan 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
  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 已提交
1151 1152 1153 1154 1155
  pNew->numOfTags = pNew->numOfTags + ntags;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1156
  for (int32_t i = 0; i < ntags; i++) {
S
Shengliang Guan 已提交
1157
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1158
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1159
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1160 1161 1162
      return -1;
    }

S
Shengliang 已提交
1163
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1164
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1165 1166 1167
      return -1;
    }

S
Shengliang Guan 已提交
1168 1169 1170 1171
    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 已提交
1172 1173
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1174

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

1178
  pNew->tagVer++;
S
Shengliang Guan 已提交
1179 1180 1181
  return 0;
}

1182
static int32_t mndCheckAlterColForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
1183 1184 1185 1186 1187 1188 1189
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SMqTopicObj *pTopic = NULL;
    pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
    if (pIter == NULL) break;

1190
    mInfo("topic:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, subType:%d sql:%s",
H
Hongze Cheng 已提交
1191
          pTopic->name, stbFullName, suid, colId, pTopic->subType, pTopic->sql);
1192 1193 1194 1195 1196 1197 1198
    if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
      sdbRelease(pSdb, pTopic);
      continue;
    }

    SNode *pAst = NULL;
    if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
S
Shengliang Guan 已提交
1199 1200 1201
      terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TOPIC;
      mError("topic:%s, create ast error", pTopic->name);
      sdbRelease(pSdb, pTopic);
1202 1203 1204 1205 1206 1207 1208 1209
      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 已提交
1210 1211
      mInfo("topic:%s, check colId:%d tableId:%" PRId64 " ctbStbUid:%" PRId64, pTopic->name, pCol->colId, pCol->tableId,
            pTopic->ctbStbUid);
1212 1213

      if (pCol->tableId != suid && pTopic->ctbStbUid != suid) {
1214
        mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
1215 1216 1217 1218 1219
        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 已提交
1220 1221 1222
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pTopic);
1223 1224
        return -1;
      }
1225
      mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
1226 1227 1228 1229 1230
    }

  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1231
    nodesDestroyList(pNodeList);
1232
  }
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  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 已提交
1246 1247 1248
      terrno = TSDB_CODE_MND_INVALID_STREAM_OPTION;
      mError("stream:%s, create ast error", pStream->name);
      sdbRelease(pSdb, pStream);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
      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) {
1259
        mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
1260 1261 1262 1263 1264
        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 已提交
1265 1266 1267
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pStream);
1268 1269
        return -1;
      }
1270
      mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
1271 1272 1273 1274 1275
    }

  NEXT:
    sdbRelease(pSdb, pStream);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1276
    nodesDestroyList(pNodeList);
1277 1278 1279
  }
  return 0;
}
1280

1281 1282 1283
static int32_t mndCheckAlterColForTSma(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
1284 1285 1286 1287 1288
  while (1) {
    SSmaObj *pSma = NULL;
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
    if (pIter == NULL) break;

H
Hongze Cheng 已提交
1289 1290
    mInfo("tsma:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, sql:%s", pSma->name, stbFullName,
          suid, colId, pSma->sql);
1291 1292 1293 1294 1295

    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",
1296
             pSma->name, stbFullName, suid, colId);
1297 1298 1299 1300 1301 1302 1303 1304
      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;
1305
      mInfo("tsma:%s, check colId:%d tableId:%" PRId64, pSma->name, pCol->colId, pCol->tableId);
1306 1307

      if ((pCol->tableId != suid) && (pSma->stbUid != suid)) {
1308
        mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
1309
        goto NEXT;
1310 1311 1312 1313
      }
      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 已提交
1314 1315 1316
        nodesDestroyNode(pAst);
        nodesDestroyList(pNodeList);
        sdbRelease(pSdb, pSma);
1317 1318
        return -1;
      }
1319
      mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
1320 1321
    }

1322
  NEXT:
1323 1324
    sdbRelease(pSdb, pSma);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
1325
    nodesDestroyList(pNodeList);
1326
  }
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
  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;
  }
1337

1338 1339 1340
  if (mndCheckAlterColForTSma(pMnode, stbFullName, suid, colId) < 0) {
    return -1;
  }
1341 1342 1343
  return 0;
}

1344
static int32_t mndDropSuperTableTag(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
S
Shengliang Guan 已提交
1345 1346 1347 1348 1349 1350
  int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

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

S
Shengliang Guan 已提交
1356 1357 1358 1359 1360
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1363
  pNew->tagVer++;
dengyihao's avatar
dengyihao 已提交
1364 1365 1366 1367

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

1372
static int32_t mndAlterStbTagName(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
S
Shengliang Guan 已提交
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
  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 已提交
1384 1385 1386 1387 1388 1389
  int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

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

S
Shengliang Guan 已提交
1395
  if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
S
Shengliang Guan 已提交
1396
    terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1397 1398 1399
    return -1;
  }

S
Shengliang Guan 已提交
1400 1401
  if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
    terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
    return -1;
  }

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

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

1412
  pNew->tagVer++;
1413
  mInfo("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
S
Shengliang Guan 已提交
1414 1415 1416
  return 0;
}

1417
static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1418
  int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1419 1420 1421 1422 1423
  if (tag < 0) {
    terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
    return -1;
  }

1424
  col_id_t colId = pOld->pTags[tag].colId;
1425
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1426 1427 1428
    return -1;
  }

S
Shengliang Guan 已提交
1429 1430 1431 1432 1433 1434
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pTag = pNew->pTags + tag;

S
Shengliang Guan 已提交
1435 1436 1437 1438 1439
  if (!(pTag->type == TSDB_DATA_TYPE_BINARY || pTag->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
    return -1;
  }

S
Shengliang Guan 已提交
1440
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1441 1442 1443 1444
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

S
Shengliang Guan 已提交
1445
  pTag->bytes = pField->bytes;
1446
  pNew->tagVer++;
S
Shengliang Guan 已提交
1447

1448
  mInfo("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1449 1450 1451
  return 0;
}

S
Shengliang Guan 已提交
1452
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
S
Shengliang Guan 已提交
1453 1454 1455 1456 1457
  if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
    terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
    return -1;
  }

S
Shengliang Guan 已提交
1458 1459 1460 1461 1462
  pNew->numOfColumns = pNew->numOfColumns + ncols;
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
1463
  for (int32_t i = 0; i < ncols; i++) {
S
Shengliang Guan 已提交
1464
    SField *pField = taosArrayGet(pFields, i);
S
Shengliang 已提交
1465
    if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1466
      terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
S
Shengliang Guan 已提交
1467 1468 1469
      return -1;
    }

S
Shengliang 已提交
1470
    if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
S
Shengliang Guan 已提交
1471
      terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
S
Shengliang Guan 已提交
1472 1473 1474
      return -1;
    }

S
Shengliang Guan 已提交
1475 1476 1477 1478
    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 已提交
1479 1480
    pSchema->colId = pNew->nextColId;
    pNew->nextColId++;
S
Shengliang Guan 已提交
1481

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

1485
  pNew->colVer++;
S
Shengliang Guan 已提交
1486 1487 1488
  return 0;
}

1489
static int32_t mndDropSuperTableColumn(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *colName) {
S
Shengliang Guan 已提交
1490
  int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
S
Shengliang Guan 已提交
1491
  if (col < 0) {
S
Shengliang Guan 已提交
1492 1493 1494 1495
    terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
    return -1;
  }

S
Shengliang Guan 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
  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;
  }

1506
  col_id_t colId = pOld->pColumns[col].colId;
1507
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1508 1509 1510
    return -1;
  }

S
Shengliang Guan 已提交
1511 1512 1513 1514 1515
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

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

1518
  pNew->colVer++;
1519
  mInfo("stb:%s, start to drop col %s", pNew->name, colName);
S
Shengliang Guan 已提交
1520 1521 1522
  return 0;
}

1523
static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
S
Shengliang Guan 已提交
1524
  int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
S
Shengliang Guan 已提交
1525 1526 1527 1528 1529 1530 1531
  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 已提交
1532
    nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
S
Shengliang Guan 已提交
1533 1534 1535 1536 1537 1538 1539
  }

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

1540
  col_id_t colId = pOld->pColumns[col].colId;
1541
  if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
1542 1543 1544
    return -1;
  }

S
Shengliang Guan 已提交
1545 1546 1547 1548 1549
  if (mndAllocStbSchemas(pOld, pNew) != 0) {
    return -1;
  }

  SSchema *pCol = pNew->pColumns + col;
S
Shengliang Guan 已提交
1550 1551
  if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
    terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
S
Shengliang Guan 已提交
1552 1553 1554
    return -1;
  }

S
Shengliang Guan 已提交
1555
  if (pField->bytes <= pCol->bytes) {
S
Shengliang Guan 已提交
1556
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
S
Shengliang Guan 已提交
1557 1558 1559
    return -1;
  }

S
Shengliang Guan 已提交
1560
  pCol->bytes = pField->bytes;
1561
  pNew->colVer++;
S
Shengliang Guan 已提交
1562

1563
  mInfo("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
S
Shengliang Guan 已提交
1564 1565 1566
  return 0;
}

S
Shengliang Guan 已提交
1567
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1568 1569
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
1570 1571 1572 1573
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1574
  if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
S
Shengliang Guan 已提交
1575 1576 1577 1578

  return 0;
}

S
Shengliang Guan 已提交
1579
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
1580 1581
  SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
  if (pCommitRaw == NULL) return -1;
S
Shengliang Guan 已提交
1582 1583 1584 1585
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
1586 1587 1588 1589 1590
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;

  return 0;
}

1591 1592
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, void *alterOriData,
                                         int32_t alterOriDataLen) {
S
Shengliang Guan 已提交
1593 1594 1595 1596 1597 1598 1599 1600
  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 已提交
1601
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
1602 1603 1604 1605
      sdbRelease(pSdb, pVgroup);
      continue;
    }

wmmhello's avatar
wmmhello 已提交
1606
    void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, alterOriData, alterOriDataLen);
S
Shengliang Guan 已提交
1607 1608 1609 1610 1611 1612 1613 1614 1615
    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 已提交
1616
    action.msgType = TDMT_VND_ALTER_STB;
S
Shengliang Guan 已提交
1617
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
1618
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
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 已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
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 已提交
1677 1678 1679
  tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
  tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
  tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
D
dapan1121 已提交
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
  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 已提交
1704
    pSchema->flags = pSrcSchema->flags;
D
dapan1121 已提交
1705 1706 1707 1708 1709 1710 1711 1712
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

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

D
dapan1121 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
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 已提交
1724 1725 1726
  tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
  tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
  tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
D
dapan1121 已提交
1727 1728 1729
  pRsp->numOfTags = pStb->numOfTags;
  pRsp->numOfColumns = pStb->numOfColumns;
  pRsp->tableType = TSDB_SUPER_TABLE;
D
dapan1121 已提交
1730 1731 1732 1733
  pRsp->delay1 = pStb->maxdelay[0];
  pRsp->delay2 = pStb->maxdelay[1];
  pRsp->watermark1 = pStb->watermark[0];
  pRsp->watermark2 = pStb->watermark[1];
D
dapan1121 已提交
1734 1735 1736
  pRsp->ttl = pStb->ttl;
  pRsp->commentLen = pStb->commentLen;
  if (pStb->commentLen > 0) {
1737
    pRsp->pComment = taosStrdup(pStb->comment);
D
dapan1121 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
  }

  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 已提交
1758
  if (pStb->numOfFuncs > 0) {
H
Haojun Liao 已提交
1759
    pRsp->pFuncs = taosArrayDup(pStb->pFuncs, NULL);
D
dapan1121 已提交
1760
  }
1761

D
dapan1121 已提交
1762 1763 1764 1765
  taosRUnLockLatch(&pStb->lock);
  return 0;
}

L
Liu Jicong 已提交
1766 1767
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp,
                                 int32_t *smaVer) {
D
dapan1121 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
  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 已提交
1780
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
D
dapan1121 已提交
1781 1782 1783
    return -1;
  }

D
dapan1121 已提交
1784 1785 1786 1787
  if (smaVer) {
    *smaVer = pStb->smaVer;
  }

D
dapan1121 已提交
1788 1789 1790 1791 1792 1793
  int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}

D
dapan1121 已提交
1794
static int32_t mndBuildStbCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
  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 已提交
1810

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

1813 1814 1815 1816
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
}
D
dapan1121 已提交
1817

1818
static int32_t mndBuildSMAlterStbRsp(SDbObj *pDb, SStbObj *pObj, void **pCont, int32_t *pLen) {
1819
  int32_t       ret;
D
dapan1121 已提交
1820
  SEncoder      ec = {0};
1821
  uint32_t      contLen = 0;
D
dapan1121 已提交
1822
  SMAlterStbRsp alterRsp = {0};
1823
  SName         name = {0};
1824
  tNameFromString(&name, pObj->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
D
dapan1121 已提交
1825 1826 1827 1828 1829 1830

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

D
dapan1121 已提交
1832
  ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, alterRsp.pMeta);
D
dapan1121 已提交
1833 1834 1835 1836
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }
1837

D
dapan1121 已提交
1838 1839 1840 1841 1842 1843
  tEncodeSize(tEncodeSMAlterStbRsp, &alterRsp, contLen, ret);
  if (ret) {
    tFreeSMAlterStbRsp(&alterRsp);
    return ret;
  }

1844
  void *cont = taosMemoryMalloc(contLen);
D
dapan1121 已提交
1845 1846 1847 1848 1849 1850 1851 1852
  tEncoderInit(&ec, cont, contLen);
  tEncodeSMAlterStbRsp(&ec, &alterRsp);
  tEncoderClear(&ec);

  tFreeSMAlterStbRsp(&alterRsp);

  *pCont = cont;
  *pLen = contLen;
1853

D
dapan1121 已提交
1854 1855 1856
  return 0;
}

L
Liu Jicong 已提交
1857 1858
int32_t mndBuildSMCreateStbRsp(SMnode *pMnode, char *dbFName, char *stbFName, void **pCont, int32_t *pLen) {
  int32_t ret = -1;
1859 1860 1861 1862 1863 1864 1865 1866 1867
  SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
  if (NULL == pDb) {
    return -1;
  }

  SStbObj *pObj = mndAcquireStb(pMnode, stbFName);
  if (NULL == pObj) {
    goto _OVER;
  }
L
Liu Jicong 已提交
1868 1869 1870

  SEncoder       ec = {0};
  uint32_t       contLen = 0;
1871
  SMCreateStbRsp stbRsp = {0};
L
Liu Jicong 已提交
1872
  SName          name = {0};
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
  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 已提交
1904

1905 1906 1907 1908
_OVER:
  if (pObj) {
    mndReleaseStb(pMnode, pObj);
  }
L
Liu Jicong 已提交
1909

1910 1911 1912 1913 1914 1915 1916
  if (pDb) {
    mndReleaseDb(pMnode, pDb);
  }

  return ret;
}

1917 1918
static int32_t mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
                              void *alterOriData, int32_t alterOriDataLen) {
1919
  int32_t code = -1;
1920
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "alter-stb");
1921 1922
  if (pTrans == NULL) goto _OVER;

1923
  mInfo("trans:%d, used to alter stb:%s", pTrans->id, pStb->name);
1924
  mndTransSetDbName(pTrans, pDb->name, pStb->name);
1925
  if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935

  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 已提交
1936
  if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, pStb, alterOriData, alterOriDataLen) != 0) goto _OVER;
1937 1938 1939 1940 1941 1942 1943 1944 1945
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

  code = 0;

_OVER:
  mndTransDrop(pTrans);
  return code;
}

dengyihao's avatar
dengyihao 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
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 已提交
1957 1958 1959 1960 1961 1962 1963
  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 已提交
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
  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 已提交
2014
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
2015 2016 2017 2018
  bool    needRsp = true;
  int32_t code = -1;
  SField *pField0 = NULL;

S
Shengliang Guan 已提交
2019 2020 2021
  SStbObj stbObj = {0};
  taosRLockLatch(&pOld->lock);
  memcpy(&stbObj, pOld, sizeof(SStbObj));
2022
  taosRUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
2023 2024 2025
  stbObj.pColumns = NULL;
  stbObj.pTags = NULL;
  stbObj.updateTime = taosGetTimestampMs();
D
dapan1121 已提交
2026
  stbObj.lock = 0;
dengyihao's avatar
dengyihao 已提交
2027
  bool updateTagIndex = false;
S
Shengliang Guan 已提交
2028
  switch (pAlter->alterType) {
S
Shengliang Guan 已提交
2029
    case TSDB_ALTER_TABLE_ADD_TAG:
S
Shengliang Guan 已提交
2030
      code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
2031
      break;
S
Shengliang Guan 已提交
2032
    case TSDB_ALTER_TABLE_DROP_TAG:
S
Shengliang 已提交
2033
      pField0 = taosArrayGet(pAlter->pFields, 0);
2034
      code = mndDropSuperTableTag(pMnode, pOld, &stbObj, pField0->name);
dengyihao's avatar
dengyihao 已提交
2035
      updateTagIndex = true;
S
Shengliang Guan 已提交
2036
      break;
S
Shengliang Guan 已提交
2037
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
2038
      code = mndAlterStbTagName(pMnode, pOld, &stbObj, pAlter->pFields);
dengyihao's avatar
dengyihao 已提交
2039
      updateTagIndex = true;
S
Shengliang Guan 已提交
2040
      break;
S
Shengliang Guan 已提交
2041
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
S
Shengliang 已提交
2042
      pField0 = taosArrayGet(pAlter->pFields, 0);
2043
      code = mndAlterStbTagBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
2044 2045
      break;
    case TSDB_ALTER_TABLE_ADD_COLUMN:
S
Shengliang Guan 已提交
2046
      code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
S
Shengliang Guan 已提交
2047 2048
      break;
    case TSDB_ALTER_TABLE_DROP_COLUMN:
S
Shengliang 已提交
2049
      pField0 = taosArrayGet(pAlter->pFields, 0);
2050
      code = mndDropSuperTableColumn(pMnode, pOld, &stbObj, pField0->name);
S
Shengliang Guan 已提交
2051
      break;
S
Shengliang Guan 已提交
2052
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
S
Shengliang 已提交
2053
      pField0 = taosArrayGet(pAlter->pFields, 0);
2054
      code = mndAlterStbColumnBytes(pMnode, pOld, &stbObj, pField0);
S
Shengliang Guan 已提交
2055
      break;
S
Shengliang 已提交
2056
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
D
dapan1121 已提交
2057
      needRsp = false;
S
Shengliang 已提交
2058
      code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
S
Shengliang 已提交
2059
      break;
S
Shengliang Guan 已提交
2060
    default:
D
dapan1121 已提交
2061
      needRsp = false;
S
Shengliang 已提交
2062
      terrno = TSDB_CODE_OPS_NOT_SUPPORT;
S
Shengliang Guan 已提交
2063 2064 2065
      break;
  }

2066
  if (code != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
2067 2068 2069 2070 2071
  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 已提交
2072

2073
_OVER:
wafwerar's avatar
wafwerar 已提交
2074 2075
  taosMemoryFreeClear(stbObj.pTags);
  taosMemoryFreeClear(stbObj.pColumns);
2076 2077 2078
  if (pAlter->commentLen > 0) {
    taosMemoryFreeClear(stbObj.comment);
  }
S
Shengliang Guan 已提交
2079 2080
  return code;
}
S
Shengliang Guan 已提交
2081

2082
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
2083
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2084 2085 2086 2087
  int32_t       code = -1;
  SDbObj       *pDb = NULL;
  SStbObj      *pStb = NULL;
  SMAlterStbReq alterReq = {0};
S
Shengliang Guan 已提交
2088

S
Shengliang Guan 已提交
2089
  if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
2090
    terrno = TSDB_CODE_INVALID_MSG;
2091
    goto _OVER;
S
Shengliang Guan 已提交
2092
  }
S
Shengliang Guan 已提交
2093

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

S
Shengliang Guan 已提交
2097
  pDb = mndAcquireDbByStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
2098 2099
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_INVALID_DB;
2100
    goto _OVER;
S
Shengliang Guan 已提交
2101 2102
  }

S
Shengliang Guan 已提交
2103
  pStb = mndAcquireStb(pMnode, alterReq.name);
S
Shengliang Guan 已提交
2104 2105
  if (pStb == NULL) {
    terrno = TSDB_CODE_MND_STB_NOT_EXIST;
2106
    goto _OVER;
S
Shengliang Guan 已提交
2107
  }
S
Shengliang Guan 已提交
2108

2109
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
2110
    goto _OVER;
S
Shengliang Guan 已提交
2111 2112
  }

S
Shengliang Guan 已提交
2113
  code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
S
Shengliang Guan 已提交
2114
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
2115

2116
_OVER:
S
Shengliang Guan 已提交
2117
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2118
    mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
S
Shengliang Guan 已提交
2119 2120
  }

S
Shengliang Guan 已提交
2121 2122
  mndReleaseStb(pMnode, pStb);
  mndReleaseDb(pMnode, pDb);
2123
  tFreeSMAltertbReq(&alterReq);
S
Shengliang Guan 已提交
2124 2125

  return code;
S
Shengliang Guan 已提交
2126
}
S
Shengliang Guan 已提交
2127

S
Shengliang Guan 已提交
2128 2129 2130
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
  SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
  if (pRedoRaw == NULL) return -1;
S
Shengliang Guan 已提交
2131 2132 2133 2134
  if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
    sdbFreeRaw(pRedoRaw);
    return -1;
  }
S
Shengliang Guan 已提交
2135 2136 2137 2138 2139 2140 2141 2142
  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 已提交
2143 2144 2145 2146
  if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    sdbFreeRaw(pCommitRaw);
    return -1;
  }
S
Shengliang Guan 已提交
2147 2148 2149 2150 2151
  if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;

  return 0;
}

S
Shengliang Guan 已提交
2152 2153 2154 2155
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 已提交
2156

S
Shengliang Guan 已提交
2157 2158 2159
  while (1) {
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
    if (pIter == NULL) break;
S
Shengliang Guan 已提交
2160
    if (!mndVgroupInDb(pVgroup, pDb->uid)) {
S
Shengliang Guan 已提交
2161 2162 2163 2164
      sdbRelease(pSdb, pVgroup);
      continue;
    }

S
Shengliang Guan 已提交
2165
    int32_t contLen = 0;
S
Shengliang Guan 已提交
2166
    void   *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
S
Shengliang Guan 已提交
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
    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 已提交
2179
    action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
S
Shengliang Guan 已提交
2180
    if (mndTransAppendRedoAction(pTrans, &action) != 0) {
wafwerar's avatar
wafwerar 已提交
2181
      taosMemoryFree(pReq);
S
Shengliang Guan 已提交
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
      sdbCancelFetch(pSdb, pIter);
      sdbRelease(pSdb, pVgroup);
      return -1;
    }
    sdbRelease(pSdb, pVgroup);
  }

  return 0;
}

S
Shengliang Guan 已提交
2192
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
S
Shengliang Guan 已提交
2193
  int32_t code = -1;
2194
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "drop-stb");
2195
  if (pTrans == NULL) goto _OVER;
S
Shengliang Guan 已提交
2196

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

2201 2202 2203
  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 已提交
2204
  if (mndDropIdxsByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2205
  if (mndDropSmasByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2206
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
S
Shengliang Guan 已提交
2207 2208
  code = 0;

2209
_OVER:
S
Shengliang Guan 已提交
2210
  mndTransDrop(pTrans);
S
Shengliang 已提交
2211
  return code;
S
Shengliang Guan 已提交
2212 2213
}

2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
static int32_t mndCheckDropStbForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
  while (1) {
    SMqTopicObj *pTopic = NULL;
    pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
    if (pIter == NULL) break;

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

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

    SNode *pAst = NULL;
    if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
S
Shengliang Guan 已提交
2236 2237 2238
      terrno = TSDB_CODE_MND_INVALID_TOPIC_OPTION;
      mError("topic:%s, create ast error", pTopic->name);
      sdbRelease(pSdb, pTopic);
2239 2240 2241 2242 2243 2244 2245 2246 2247
      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 已提交
2248
      if (pCol->tableId == suid) {
2249 2250
        sdbRelease(pSdb, pTopic);
        nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2251
        nodesDestroyList(pNodeList);
2252 2253 2254 2255 2256 2257 2258 2259
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2260
    nodesDestroyList(pNodeList);
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
  }
  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 已提交
2273 2274 2275 2276 2277 2278
    if (pStream->smaId != 0) {
      sdbRelease(pSdb, pStream);
      continue;
    }

    if (pStream->targetStbUid == suid) {
L
Liu Jicong 已提交
2279 2280 2281 2282
      sdbRelease(pSdb, pStream);
      return -1;
    }

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

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

2316
static int32_t mndProcessDropStbReq(SRpcMsg *pReq) {
S
Shengliang Guan 已提交
2317
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2318 2319 2320
  int32_t      code = -1;
  SDbObj      *pDb = NULL;
  SStbObj     *pStb = NULL;
S
Shengliang Guan 已提交
2321
  SMDropStbReq dropReq = {0};
S
Shengliang Guan 已提交
2322

S
Shengliang Guan 已提交
2323
  if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
2324
    terrno = TSDB_CODE_INVALID_MSG;
2325
    goto _OVER;
S
Shengliang Guan 已提交
2326
  }
S
Shengliang Guan 已提交
2327

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

S
Shengliang Guan 已提交
2330
  pStb = mndAcquireStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2331
  if (pStb == NULL) {
S
Shengliang Guan 已提交
2332
    if (dropReq.igNotExists) {
2333
      mInfo("stb:%s, not exist, ignore not exist is set", dropReq.name);
S
Shengliang Guan 已提交
2334
      code = 0;
2335
      goto _OVER;
S
Shengliang Guan 已提交
2336 2337
    } else {
      terrno = TSDB_CODE_MND_STB_NOT_EXIST;
2338
      goto _OVER;
S
Shengliang Guan 已提交
2339 2340 2341
    }
  }

wmmhello's avatar
wmmhello 已提交
2342 2343
  if (dropReq.source == TD_REQ_FROM_TAOX && pStb->uid != dropReq.suid) {
    code = 0;
wmmhello's avatar
wmmhello 已提交
2344 2345 2346
    goto _OVER;
  }

S
Shengliang Guan 已提交
2347
  pDb = mndAcquireDbByStb(pMnode, dropReq.name);
S
Shengliang Guan 已提交
2348 2349
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
2350
    goto _OVER;
S
Shengliang Guan 已提交
2351 2352
  }

2353
  if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
2354
    goto _OVER;
S
Shengliang Guan 已提交
2355 2356
  }

2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
  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 已提交
2367
  code = mndDropStb(pMnode, pReq, pDb, pStb);
S
Shengliang Guan 已提交
2368
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
2369

2370
_OVER:
S
Shengliang Guan 已提交
2371
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
2372
    mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
S
Shengliang Guan 已提交
2373 2374
  }

S
Shengliang Guan 已提交
2375 2376 2377
  mndReleaseDb(pMnode, pDb);
  mndReleaseStb(pMnode, pStb);
  return code;
S
Shengliang Guan 已提交
2378
}
S
Shengliang Guan 已提交
2379

S
Shengliang Guan 已提交
2380 2381
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2382 2383 2384
  int32_t       code = -1;
  STableInfoReq infoReq = {0};
  STableMetaRsp metaRsp = {0};
D
dapan 已提交
2385

2386 2387 2388 2389
  SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user);
  if (pUser == NULL) return 0;
  bool sysinfo = pUser->sysInfo;

S
Shengliang Guan 已提交
2390
  if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
S
Shengliang Guan 已提交
2391
    terrno = TSDB_CODE_INVALID_MSG;
2392
    goto _OVER;
S
Shengliang Guan 已提交
2393
  }
D
dapan 已提交
2394

D
dapan1121 已提交
2395
  if (0 == strcmp(infoReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2396
    mInfo("information_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
2397
    if (mndBuildInsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, sysinfo, &metaRsp) != 0) {
2398
      goto _OVER;
D
dapan1121 已提交
2399
    }
D
dapan1121 已提交
2400
  } else if (0 == strcmp(infoReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2401
    mInfo("performance_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
2402
    if (mndBuildPerfsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
2403
      goto _OVER;
D
dapan1121 已提交
2404
    }
D
dapan1121 已提交
2405
  } else {
2406
    mInfo("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
D
dapan1121 已提交
2407
    if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp, NULL) != 0) {
2408
      goto _OVER;
D
dapan1121 已提交
2409
    }
S
Shengliang Guan 已提交
2410
  }
S
Shengliang Guan 已提交
2411

S
Shengliang Guan 已提交
2412 2413 2414
  int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
  if (rspLen < 0) {
    terrno = TSDB_CODE_INVALID_MSG;
2415
    goto _OVER;
S
Shengliang Guan 已提交
2416
  }
S
Shengliang Guan 已提交
2417

S
Shengliang Guan 已提交
2418
  void *pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
2419 2420
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2421
    goto _OVER;
S
Shengliang Guan 已提交
2422
  }
D
dapan 已提交
2423

S
Shengliang Guan 已提交
2424
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
S
Shengliang Guan 已提交
2425 2426
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = rspLen;
S
Shengliang Guan 已提交
2427
  code = 0;
S
Shengliang Guan 已提交
2428

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

2431
_OVER:
S
Shengliang Guan 已提交
2432 2433 2434
  if (code != 0) {
    mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
  }
S
Shengliang Guan 已提交
2435

2436
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
2437 2438 2439
  tFreeSTableMetaRsp(&metaRsp);
  return code;
}
S
Shengliang Guan 已提交
2440

D
dapan1121 已提交
2441
static int32_t mndProcessTableCfgReq(SRpcMsg *pReq) {
2442 2443 2444 2445
  SMnode      *pMnode = pReq->info.node;
  int32_t      code = -1;
  STableCfgReq cfgReq = {0};
  STableCfgRsp cfgRsp = {0};
D
dapan1121 已提交
2446 2447 2448 2449 2450 2451 2452

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

  if (0 == strcmp(cfgReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2453
    mInfo("information_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2454 2455 2456 2457
    if (mndBuildInsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else if (0 == strcmp(cfgReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2458
    mInfo("performance_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2459 2460 2461 2462
    if (mndBuildPerfsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else {
2463
    mInfo("stb:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
    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 已提交
2497
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
S
Shengliang Guan 已提交
2498
                           int32_t *pRspLen) {
D
dapan1121 已提交
2499
  SSTbHbRsp hbRsp = {0};
D
dapan1121 已提交
2500 2501
  hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (hbRsp.pMetaRsp == NULL) {
S
Shengliang Guan 已提交
2502 2503 2504
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
2505

D
dapan1121 已提交
2506
  hbRsp.pIndexRsp = taosArrayInit(numOfStbs, sizeof(STableIndexRsp));
D
dapan1121 已提交
2507
  if (NULL == hbRsp.pIndexRsp) {
D
dapan1121 已提交
2508
    taosArrayDestroy(hbRsp.pMetaRsp);
D
dapan1121 已提交
2509 2510 2511
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
L
Liu Jicong 已提交
2512

S
Shengliang Guan 已提交
2513
  for (int32_t i = 0; i < numOfStbs; ++i) {
D
dapan1121 已提交
2514
    SSTableVersion *pStbVersion = &pStbVersions[i];
S
Shengliang Guan 已提交
2515 2516 2517
    pStbVersion->suid = be64toh(pStbVersion->suid);
    pStbVersion->sversion = ntohs(pStbVersion->sversion);
    pStbVersion->tversion = ntohs(pStbVersion->tversion);
D
dapan1121 已提交
2518
    pStbVersion->smaVer = ntohl(pStbVersion->smaVer);
S
Shengliang Guan 已提交
2519

S
Shengliang Guan 已提交
2520
    STableMetaRsp metaRsp = {0};
L
Liu Jicong 已提交
2521
    int32_t       smaVer = 0;
2522
    mInfo("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2523
    if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp, &smaVer) != 0) {
S
Shengliang Guan 已提交
2524 2525
      metaRsp.numOfColumns = -1;
      metaRsp.suid = pStbVersion->suid;
D
dapan1121 已提交
2526
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2527
      continue;
D
dapan 已提交
2528
    }
S
Shengliang Guan 已提交
2529

D
dapan1121 已提交
2530
    if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
D
dapan1121 已提交
2531
      taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
D
dapan1121 已提交
2532 2533
    } else {
      tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
2534
    }
D
dapan1121 已提交
2535

D
dapan1121 已提交
2536
    if (pStbVersion->smaVer && pStbVersion->smaVer != smaVer) {
L
Liu Jicong 已提交
2537 2538
      bool           exist = false;
      char           tbFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
2539
      STableIndexRsp indexRsp = {0};
D
dapan1121 已提交
2540 2541 2542 2543 2544
      indexRsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
      if (NULL == indexRsp.pIndex) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return -1;
      }
L
Liu Jicong 已提交
2545

D
dapan1121 已提交
2546
      sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName);
D
dapan1121 已提交
2547
      int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist);
D
dapan1121 已提交
2548
      if (code || !exist) {
D
dapan1121 已提交
2549 2550 2551
        indexRsp.suid = pStbVersion->suid;
        indexRsp.version = -1;
        indexRsp.pIndex = NULL;
D
dapan1121 已提交
2552
      }
D
dapan1121 已提交
2553

D
dapan1121 已提交
2554 2555
      strcpy(indexRsp.dbFName, pStbVersion->dbFName);
      strcpy(indexRsp.tbName, pStbVersion->stbName);
D
dapan1121 已提交
2556 2557

      taosArrayPush(hbRsp.pIndexRsp, &indexRsp);
D
dapan1121 已提交
2558
    }
S
Shengliang Guan 已提交
2559
  }
S
Shengliang Guan 已提交
2560

D
dapan1121 已提交
2561
  int32_t rspLen = tSerializeSSTbHbRsp(NULL, 0, &hbRsp);
S
Shengliang Guan 已提交
2562
  if (rspLen < 0) {
D
dapan1121 已提交
2563
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2564 2565
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
D
dapan 已提交
2566 2567
  }

wafwerar's avatar
wafwerar 已提交
2568
  void *pRsp = taosMemoryMalloc(rspLen);
S
Shengliang Guan 已提交
2569
  if (pRsp == NULL) {
D
dapan1121 已提交
2570
    tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2571 2572
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
D
dapan 已提交
2573 2574
  }

D
dapan1121 已提交
2575 2576
  tSerializeSSTbHbRsp(pRsp, rspLen, &hbRsp);
  tFreeSSTbHbRsp(&hbRsp);
S
Shengliang Guan 已提交
2577 2578
  *ppRsp = pRsp;
  *pRspLen = rspLen;
D
dapan 已提交
2579 2580 2581
  return 0;
}

2582
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
S
Shengliang Guan 已提交
2583
  SSdb   *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2584 2585 2586 2587 2588 2589
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
  if (pDb == NULL) {
    terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
    return -1;
  }

S
Shengliang Guan 已提交
2590
  int32_t numOfStbs = 0;
2591
  void   *pIter = NULL;
S
Shengliang Guan 已提交
2592
  while (1) {
S
Shengliang Guan 已提交
2593
    SStbObj *pStb = NULL;
S
Shengliang Guan 已提交
2594
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2595 2596
    if (pIter == NULL) break;

S
Shengliang Guan 已提交
2597
    if (pStb->dbUid == pDb->uid) {
S
Shengliang Guan 已提交
2598
      numOfStbs++;
S
Shengliang Guan 已提交
2599 2600
    }

S
Shengliang Guan 已提交
2601
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2602 2603
  }

S
Shengliang Guan 已提交
2604
  *pNumOfStbs = numOfStbs;
S
Shengliang Guan 已提交
2605
  mndReleaseDb(pMnode, pDb);
S
Shengliang Guan 已提交
2606 2607 2608
  return 0;
}

L
Liu Jicong 已提交
2609 2610 2611 2612 2613 2614 2615 2616
void mndExtractDbNameFromStbFullName(const char *stbFullName, char *dst) {
  SName name = {0};
  tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  tNameGetFullDbName(&name, dst);
}

void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t dstSize) {
S
Shengliang Guan 已提交
2617 2618
  int32_t pos = -1;
  int32_t num = 0;
L
Liu Jicong 已提交
2619 2620
  for (pos = 0; stbFullName[pos] != 0; ++pos) {
    if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
2621 2622 2623 2624
    if (num == 2) break;
  }

  if (num == 2) {
L
Liu Jicong 已提交
2625
    tstrncpy(dst, stbFullName + pos + 1, dstSize);
S
Shengliang Guan 已提交
2626 2627 2628
  }
}

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

S
Shengliang Guan 已提交
2905 2906
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2907
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2908 2909 2910
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
2911

H
Hongze Cheng 已提交
2912
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
2913 2914
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
2915
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
2916
  }
S
Shengliang Guan 已提交
2917

S
Shengliang Guan 已提交
2918
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
2919
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2920 2921
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
2922
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
2923
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2924 2925 2926 2927 2928
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
2929
    SName name = {0};
H
Hongze Cheng 已提交
2930
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
2931
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
2932
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
2933

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

H
Hongze Cheng 已提交
2937 2938
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
2939 2940 2941
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

2942
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2943
    colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
2944 2945

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

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

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

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

2957
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
2958 2959 2960
    if (pStb->commentLen > 0) {
      char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, pStb->comment);
2961
      colDataSetVal(pColInfo, numOfRows, comment, false);
2962
    } else if (pStb->commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
2963 2964
      char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, "");
2965
      colDataSetVal(pColInfo, numOfRows, comment, false);
wmmhello's avatar
wmmhello 已提交
2966
    } else {
2967
      colDataSetNULL(pColInfo, numOfRows);
S
Shengliang Guan 已提交
2968
    }
H
Haojun Liao 已提交
2969

2970 2971 2972 2973 2974
    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++);
2975
    colDataSetVal(pColInfo, numOfRows, (const char *)watermark, false);
2976 2977 2978 2979 2980 2981

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

S
Shengliang Guan 已提交
2984
    char    rollup[160 + VARSTR_HEADER_SIZE] = {0};
2985
    int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs);
D
dapan1121 已提交
2986 2987
    char   *sep = ", ";
    int32_t sepLen = strlen(sep);
D
dapan1121 已提交
2988
    int32_t rollupLen = sizeof(rollup) - VARSTR_HEADER_SIZE - 2;
2989 2990 2991
    for (int32_t i = 0; i < rollupNum; ++i) {
      char *funcName = taosArrayGet(pStb->pFuncs, i);
      if (i) {
D
dapan1121 已提交
2992 2993
        strncat(varDataVal(rollup), sep, rollupLen);
        rollupLen -= sepLen;
2994
      }
D
dapan1121 已提交
2995 2996
      strncat(varDataVal(rollup), funcName, rollupLen);
      rollupLen -= strlen(funcName);
2997 2998 2999 3000
    }
    varDataSetLen(rollup, strlen(varDataVal(rollup)));

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

S
Shengliang Guan 已提交
3003
    numOfRows++;
S
Shengliang Guan 已提交
3004
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
3005 3006
  }

H
Haojun Liao 已提交
3007 3008 3009 3010
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

3011
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
3012 3013 3014
  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3015 3016
static int32_t buildDbColsInfoBlock(const SSDataBlock *p, const SSysTableMeta *pSysDbTableMeta, size_t size,
                                    const char *dbName, const char *tbName) {
wmmhello's avatar
wmmhello 已提交
3017 3018 3019 3020 3021 3022 3023 3024 3025
  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 已提交
3026 3027 3028 3029 3030
    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 已提交
3031 3032 3033
      continue;
    }

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

dengyihao's avatar
dengyihao 已提交
3036
    for (int32_t j = 0; j < pm->colNum; j++) {
wmmhello's avatar
wmmhello 已提交
3037
      // table name
dengyihao's avatar
dengyihao 已提交
3038
      SColumnInfoData *pColInfoData = taosArrayGet(p->pDataBlock, 0);
wmmhello's avatar
wmmhello 已提交
3039 3040
      colDataAppend(pColInfoData, numOfRows, tName, false);

wmmhello's avatar
wmmhello 已提交
3041
      // database name
wmmhello's avatar
wmmhello 已提交
3042
      pColInfoData = taosArrayGet(p->pDataBlock, 1);
wmmhello's avatar
wmmhello 已提交
3043
      colDataAppend(pColInfoData, numOfRows, dName, false);
wmmhello's avatar
wmmhello 已提交
3044 3045

      pColInfoData = taosArrayGet(p->pDataBlock, 2);
wmmhello's avatar
wmmhello 已提交
3046
      colDataAppend(pColInfoData, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059

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

      // col type
      int8_t colType = pm->schema[j].type;
      pColInfoData = taosArrayGet(p->pDataBlock, 4);
      char colTypeStr[VARSTR_HEADER_SIZE + 32];
      int  colTypeLen = sprintf(varDataVal(colTypeStr), "%s", tDataTypes[colType].name);
      if (colType == TSDB_DATA_TYPE_VARCHAR) {
dengyihao's avatar
dengyihao 已提交
3060 3061
        colTypeLen +=
            sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", (int32_t)(pm->schema[j].bytes - VARSTR_HEADER_SIZE));
wmmhello's avatar
wmmhello 已提交
3062
      } else if (colType == TSDB_DATA_TYPE_NCHAR) {
dengyihao's avatar
dengyihao 已提交
3063 3064
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)((pm->schema[j].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
wmmhello's avatar
wmmhello 已提交
3065 3066
      }
      varDataSetLen(colTypeStr, colTypeLen);
dengyihao's avatar
dengyihao 已提交
3067
      colDataAppend(pColInfoData, numOfRows, (char *)colTypeStr, false);
wmmhello's avatar
wmmhello 已提交
3068 3069

      pColInfoData = taosArrayGet(p->pDataBlock, 5);
dengyihao's avatar
dengyihao 已提交
3070
      colDataAppend(pColInfoData, numOfRows, (const char *)&pm->schema[j].bytes, false);
wmmhello's avatar
wmmhello 已提交
3071 3072
      for (int32_t k = 6; k <= 8; ++k) {
        pColInfoData = taosArrayGet(p->pDataBlock, k);
X
Xiaoyu Wang 已提交
3073
        colDataSetNULL(pColInfoData, numOfRows);
wmmhello's avatar
wmmhello 已提交
3074 3075 3076 3077 3078 3079 3080 3081 3082
      }

      numOfRows += 1;
    }
  }

  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3083
static int32_t buildSysDbColsInfo(SSDataBlock *p, char *db, char *tb) {
wmmhello's avatar
wmmhello 已提交
3084
  size_t               size = 0;
dengyihao's avatar
dengyihao 已提交
3085
  const SSysTableMeta *pSysDbTableMeta = NULL;
wmmhello's avatar
wmmhello 已提交
3086

dengyihao's avatar
dengyihao 已提交
3087 3088
  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 已提交
3089 3090 3091
    return p->info.rows;
  }

wmmhello's avatar
wmmhello 已提交
3092
  getInfosDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3093
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_INFORMATION_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3094 3095

  getPerfDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3096
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_PERFORMANCE_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3097 3098 3099 3100 3101 3102 3103 3104 3105

  return p->info.rows;
}

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

wmmhello's avatar
wmmhello 已提交
3106
  int32_t numOfRows = buildSysDbColsInfo(pBlock, pShow->db, pShow->filterTb);
wmmhello's avatar
wmmhello 已提交
3107
  mDebug("mndRetrieveStbCol get system table cols, rows:%d, db:%s", numOfRows, pShow->db);
wmmhello's avatar
wmmhello 已提交
3108 3109 3110 3111 3112 3113
  SDbObj *pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
    if (pDb == NULL) return terrno;
  }

dengyihao's avatar
dengyihao 已提交
3114
  char typeName[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
wmmhello's avatar
wmmhello 已提交
3115
  STR_TO_VARSTR(typeName, "SUPER_TABLE");
K
kailixu 已提交
3116 3117
  bool fetch = pShow->restore ? false : true;
  pShow->restore = false;
wmmhello's avatar
wmmhello 已提交
3118
  while (numOfRows < rows) {
K
kailixu 已提交
3119 3120 3121 3122 3123 3124 3125 3126
    if (fetch) {
      pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
      if (pShow->pIter == NULL) break;
    } else {
      fetch = true;
      sdbGet(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
      if (!pStb) continue;
    }
wmmhello's avatar
wmmhello 已提交
3127 3128 3129 3130 3131 3132 3133 3134 3135

    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 已提交
3136
    if (pShow->filterTb[0] && strncmp(pShow->filterTb, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN) != 0) {
wmmhello's avatar
wmmhello 已提交
3137 3138 3139
      sdbRelease(pSdb, pStb);
      continue;
    }
K
kailixu 已提交
3140 3141

    if ((numOfRows + pStb->numOfColumns) > rows) {
K
kailixu 已提交
3142
      pShow->restore = true;
K
kailixu 已提交
3143 3144 3145 3146 3147 3148 3149 3150
      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 已提交
3151 3152
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));

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

wmmhello's avatar
wmmhello 已提交
3155 3156 3157 3158 3159
    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 已提交
3160 3161
    for (int i = 0; i < pStb->numOfColumns; i++) {
      int32_t          cols = 0;
wmmhello's avatar
wmmhello 已提交
3162 3163 3164 3165
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataAppend(pColInfo, numOfRows, (const char *)stbName, false);

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

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
3169
      colDataAppend(pColInfo, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185

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

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

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
dengyihao's avatar
dengyihao 已提交
3193 3194
      colDataAppend(pColInfo, numOfRows, (const char *)&pStb->pColumns[i].bytes, false);
      while (cols < pShow->numOfColumns) {
wmmhello's avatar
wmmhello 已提交
3195
        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
X
Xiaoyu Wang 已提交
3196
        colDataSetNULL(pColInfo, numOfRows);
wmmhello's avatar
wmmhello 已提交
3197
      }
wmmhello's avatar
wmmhello 已提交
3198
      numOfRows++;
wmmhello's avatar
wmmhello 已提交
3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
    }

    sdbRelease(pSdb, pStb);
  }

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

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

wmmhello's avatar
wmmhello 已提交
3211 3212 3213
  return numOfRows;
}

S
Shengliang Guan 已提交
3214
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
3215 3216
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
3217
}
S
Shengliang Guan 已提交
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227

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 已提交
3228
}
dengyihao's avatar
dengyihao 已提交
3229 3230 3231 3232 3233 3234

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

dengyihao's avatar
dengyihao 已提交
3235
/*int32_t mndAddIndexImpl(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp, void *sql,
dengyihao's avatar
dengyihao 已提交
3236 3237
                        int32_t len) {
  // impl later
dengyihao's avatar
dengyihao 已提交
3238
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
3239 3240 3241 3242 3243 3244 3245
  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 已提交
3246 3247
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3248
  if (mndSetAlterStbRedoActions2(pMnode, pTrans, pDb, pStb, sql, len) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3249 3250
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
3251 3252 3253 3254 3255
  return code;

_OVER:
  mndTransDrop(pTrans);
  return code;
dengyihao's avatar
dengyihao 已提交
3256
}
dengyihao's avatar
dengyihao 已提交
3257 3258 3259 3260 3261
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 已提交
3262 3263 3264
  SStbObj  stbObj = {0};
  SStbObj *pNew = &stbObj;

dengyihao's avatar
dengyihao 已提交
3265 3266 3267 3268 3269 3270 3271 3272 3273
  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 已提交
3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
  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 已提交
3289
    terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
dengyihao's avatar
dengyihao 已提交
3290 3291 3292 3293 3294 3295
    return -1;
  } else {
    pTag->flags |= COL_IDX_ON;
  }
  pNew->tagVer++;

dengyihao's avatar
dengyihao 已提交
3296 3297 3298
  code = mndAddIndexImpl(pMnode, pReq, pDb, pNew, needRsp, pReq->pCont, pReq->contLen);

  return code;
dengyihao's avatar
dengyihao 已提交
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333
}
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 已提交
3334 3335 3336
  if (terrno == TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST || terrno == TSDB_CODE_MND_TAG_NOT_EXIST) {
    return terrno;
  } else {
dengyihao's avatar
dengyihao 已提交
3337
    if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
dengyihao's avatar
dengyihao 已提交
3338
  }
dengyihao's avatar
dengyihao 已提交
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360
_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 已提交
3361
}*/