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

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

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

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

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

S
Shengliang Guan 已提交
59
int32_t mndInitStb(SMnode *pMnode) {
S
Shengliang Guan 已提交
60 61 62 63 64 65 66 67 68
  SSdbTable table = {
      .sdbType = SDB_STB,
      .keyType = SDB_KEY_BINARY,
      .encodeFp = (SdbEncodeFp)mndStbActionEncode,
      .decodeFp = (SdbDecodeFp)mndStbActionDecode,
      .insertFp = (SdbInsertFp)mndStbActionInsert,
      .updateFp = (SdbUpdateFp)mndStbActionUpdate,
      .deleteFp = (SdbDeleteFp)mndStbActionDelete,
  };
S
Shengliang Guan 已提交
69

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

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

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

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

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

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

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

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

  int32_t dataPos = 0;
107 108 109 110 111 112
  SDB_SET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->updateTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->uid, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->dbUid, _OVER)
113 114
  SDB_SET_INT32(pRaw, dataPos, pStb->tagVer, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->colVer, _OVER)
S
Shengliang Guan 已提交
115
  SDB_SET_INT32(pRaw, dataPos, pStb->smaVer, _OVER)
116
  SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, _OVER)
117 118 119 120
  SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[0], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[1], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->watermark[0], _OVER)
  SDB_SET_INT64(pRaw, dataPos, pStb->watermark[1], _OVER)
121 122 123
  SDB_SET_INT32(pRaw, dataPos, pStb->ttl, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, _OVER)
S
Shengliang Guan 已提交
124
  SDB_SET_INT32(pRaw, dataPos, pStb->numOfFuncs, _OVER)
125
  SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, _OVER)
126 127
  SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
  SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129 130
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
131
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
132
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
133 134 135
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
136 137 138 139
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
140
    SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
S
Shengliang Guan 已提交
141
    SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
142 143 144
    SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
    SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
    SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
145 146
  }

S
Shengliang Guan 已提交
147 148 149 150 151
  for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
    char *func = taosArrayGet(pStb->pFuncs, i);
    SDB_SET_BINARY(pRaw, dataPos, func, TSDB_FUNC_NAME_LEN, _OVER)
  }

S
sma  
Shengliang Guan 已提交
152
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
153
    SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
154
  }
S
Shengliang Guan 已提交
155

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

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

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

  terrno = 0;

169
_OVER:
170 171 172 173 174
  if (terrno != 0) {
    mError("stb:%s, failed to encode to raw:%p since %s", pStb->name, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
175

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

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

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

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

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

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

  int32_t dataPos = 0;
200 201 202 203 204 205
  SDB_GET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->updateTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->uid, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->dbUid, _OVER)
206 207
  SDB_GET_INT32(pRaw, dataPos, &pStb->tagVer, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->colVer, _OVER)
S
Shengliang Guan 已提交
208
  SDB_GET_INT32(pRaw, dataPos, &pStb->smaVer, _OVER)
209
  SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
210 211 212 213
  SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[0], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[1], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[0], _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[1], _OVER)
214 215 216
  SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, _OVER)
S
Shengliang Guan 已提交
217
  SDB_GET_INT32(pRaw, dataPos, &pStb->numOfFuncs, _OVER)
218
  SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, _OVER)
219 220
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
S
Shengliang Guan 已提交
221

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

S
Shengliang Guan 已提交
229 230
  for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
    SSchema *pSchema = &pStb->pColumns[i];
231
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
232
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
233 234 235
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
236 237 238 239
  }

  for (int32_t i = 0; i < pStb->numOfTags; ++i) {
    SSchema *pSchema = &pStb->pTags[i];
240
    SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
S
Shengliang Guan 已提交
241
    SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
242 243 244
    SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
    SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
S
Shengliang Guan 已提交
245 246
  }

S
Shengliang Guan 已提交
247 248 249 250 251 252
  for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
    char funcName[TSDB_FUNC_NAME_LEN] = {0};
    SDB_GET_BINARY(pRaw, dataPos, funcName, TSDB_FUNC_NAME_LEN, _OVER)
    taosArrayPush(pStb->pFuncs, funcName);
  }

S
sma  
Shengliang Guan 已提交
253
  if (pStb->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
254
    pStb->comment = taosMemoryCalloc(pStb->commentLen + 1, 1);
255
    if (pStb->comment == NULL) goto _OVER;
wmmhello's avatar
wmmhello 已提交
256
    SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
S
sma  
Shengliang Guan 已提交
257
  }
S
Shengliang Guan 已提交
258

259 260 261 262 263
  if (pStb->ast1Len > 0) {
    pStb->pAst1 = taosMemoryCalloc(pStb->ast1Len, 1);
    if (pStb->pAst1 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
  }
S
Shengliang Guan 已提交
264

265 266 267 268 269
  if (pStb->ast2Len > 0) {
    pStb->pAst2 = taosMemoryCalloc(pStb->ast2Len, 1);
    if (pStb->pAst2 == NULL) goto _OVER;
    SDB_GET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
  }
S
Shengliang Guan 已提交
270
  SDB_GET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
271 272 273

  terrno = 0;

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

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

L
Liu Jicong 已提交
290 291 292 293 294 295 296 297 298
void mndFreeStb(SStbObj *pStb) {
  taosArrayDestroy(pStb->pFuncs);
  taosMemoryFreeClear(pStb->pColumns);
  taosMemoryFreeClear(pStb->pTags);
  taosMemoryFreeClear(pStb->comment);
  taosMemoryFreeClear(pStb->pAst1);
  taosMemoryFreeClear(pStb->pAst2);
}

S
Shengliang Guan 已提交
299
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
300
  mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
S
Shengliang Guan 已提交
301 302 303
  return 0;
}

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

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

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

  if (pOld->numOfColumns < pNew->numOfColumns) {
wafwerar's avatar
wafwerar 已提交
316
    void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
S
Shengliang Guan 已提交
317
    if (pColumns != NULL) {
wafwerar's avatar
wafwerar 已提交
318
      taosMemoryFree(pOld->pColumns);
S
Shengliang Guan 已提交
319
      pOld->pColumns = pColumns;
S
Shengliang Guan 已提交
320 321 322 323 324 325 326 327
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->numOfTags < pNew->numOfTags) {
wafwerar's avatar
wafwerar 已提交
328
    void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
S
Shengliang Guan 已提交
329
    if (pTags != NULL) {
wafwerar's avatar
wafwerar 已提交
330
      taosMemoryFree(pOld->pTags);
S
Shengliang Guan 已提交
331
      pOld->pTags = pTags;
S
Shengliang Guan 已提交
332 333 334 335
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
336
    }
S
Shengliang Guan 已提交
337 338
  }

339
  if (pOld->commentLen < pNew->commentLen && pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
340
    void *comment = taosMemoryMalloc(pNew->commentLen + 1);
S
Shengliang Guan 已提交
341
    if (comment != NULL) {
wafwerar's avatar
wafwerar 已提交
342
      taosMemoryFree(pOld->comment);
S
Shengliang Guan 已提交
343 344 345 346 347 348 349
      pOld->comment = comment;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }
S
Shengliang Guan 已提交
350
  pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
351

352
  if (pOld->ast1Len < pNew->ast1Len) {
S
Shengliang Guan 已提交
353
    void *pAst1 = taosMemoryMalloc(pNew->ast1Len + 1);
354 355 356 357 358 359 360 361 362 363 364
    if (pAst1 != NULL) {
      taosMemoryFree(pOld->pAst1);
      pOld->pAst1 = pAst1;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

  if (pOld->ast2Len < pNew->ast2Len) {
S
Shengliang Guan 已提交
365
    void *pAst2 = taosMemoryMalloc(pNew->ast2Len + 1);
366 367 368 369 370 371 372 373 374 375
    if (pAst2 != NULL) {
      taosMemoryFree(pOld->pAst2);
      pOld->pAst2 = pAst2;
    } else {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
      taosWUnLockLatch(&pOld->lock);
    }
  }

S
Shengliang Guan 已提交
376
  pOld->updateTime = pNew->updateTime;
377 378
  pOld->tagVer = pNew->tagVer;
  pOld->colVer = pNew->colVer;
379
  pOld->smaVer = pNew->smaVer;
380
  pOld->nextColId = pNew->nextColId;
S
Shengliang 已提交
381
  pOld->ttl = pNew->ttl;
382 383 384 385 386 387 388 389
  if (pNew->numOfColumns > 0) {
    pOld->numOfColumns = pNew->numOfColumns;
    memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
  }
  if (pNew->numOfTags > 0) {
    pOld->numOfTags = pNew->numOfTags;
    memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
  }
390
  if (pNew->commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
391
    memcpy(pOld->comment, pNew->comment, pNew->commentLen + 1);
S
Shengliang Guan 已提交
392
    pOld->commentLen = pNew->commentLen;
S
Shengliang Guan 已提交
393
  }
394 395
  if (pNew->ast1Len != 0) {
    memcpy(pOld->pAst1, pNew->pAst1, pNew->ast1Len);
S
Shengliang Guan 已提交
396
    pOld->ast1Len = pNew->ast1Len;
397 398 399
  }
  if (pNew->ast2Len != 0) {
    memcpy(pOld->pAst2, pNew->pAst2, pNew->ast2Len);
S
Shengliang Guan 已提交
400
    pOld->ast2Len = pNew->ast2Len;
401
  }
S
Shengliang Guan 已提交
402
  taosWUnLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
403 404 405
  return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}

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

  return 0;
}

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

  return 0;
}

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

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

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

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

  return 0;
}

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

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

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

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

  return 0;
}

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

740 741 742 743 744
int32_t mndBuildStbFromReq(SMnode *pMnode, SStbObj *pDst, SMCreateStbReq *pCreate, SDbObj *pDb) {
  memcpy(pDst->name, pCreate->name, TSDB_TABLE_FNAME_LEN);
  memcpy(pDst->db, pDb->name, TSDB_DB_FNAME_LEN);
  pDst->createdTime = taosGetTimestampMs();
  pDst->updateTime = pDst->createdTime;
745 746
  pDst->uid =
      (pCreate->source == TD_REQ_FROM_TAOX) ? pCreate->suid : mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
747
  pDst->dbUid = pDb->uid;
748 749
  pDst->tagVer = 1;
  pDst->colVer = 1;
S
Shengliang Guan 已提交
750
  pDst->smaVer = 1;
751
  pDst->nextColId = 1;
752 753 754 755
  pDst->maxdelay[0] = pCreate->delay1;
  pDst->maxdelay[1] = pCreate->delay2;
  pDst->watermark[0] = pCreate->watermark1;
  pDst->watermark[1] = pCreate->watermark2;
756 757 758
  pDst->ttl = pCreate->ttl;
  pDst->numOfColumns = pCreate->numOfColumns;
  pDst->numOfTags = pCreate->numOfTags;
S
Shengliang Guan 已提交
759
  pDst->numOfFuncs = pCreate->numOfFuncs;
760
  pDst->commentLen = pCreate->commentLen;
D
dapan1121 已提交
761 762
  pDst->pFuncs = pCreate->pFuncs;
  pCreate->pFuncs = NULL;
763

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

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

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

793 794 795
  pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema));
  pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema));
  if (pDst->pColumns == NULL || pDst->pTags == NULL) {
S
Shengliang Guan 已提交
796 797 798
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
799

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

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

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

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

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

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

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

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

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

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

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

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

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

wmmhello's avatar
wmmhello 已提交
895 896
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
  for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
X
Xiaoyu Wang 已提交
897
    if (strcmp(pStb->pTags[tag].name, tagName) == 0) {
wmmhello's avatar
wmmhello 已提交
898 899 900 901 902 903 904 905 906
      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 已提交
907
    if (strcmp(pStb->pColumns[col].name, colName) == 0) {
wmmhello's avatar
wmmhello 已提交
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 937
      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);
938
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
939
      pSchema->colId = pStb->pColumns[cIndex].colId;
940
    } else {
wmmhello's avatar
wmmhello 已提交
941 942 943 944 945 946 947 948 949 950 951
      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);
952
    if (cIndex >= 0) {
wmmhello's avatar
wmmhello 已提交
953
      pSchema->colId = pStb->pTags[cIndex].colId;
954
    } else {
wmmhello's avatar
wmmhello 已提交
955 956 957 958 959 960 961 962
      pSchema->colId = pDst->nextColId++;
    }
  }
  pDst->tagVer = createReq->tagVer;
  pDst->colVer = createReq->colVer;
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  SSchema *pTag = pNew->pTags + tag;

S
Shengliang Guan 已提交
1436 1437 1438 1439 1440
  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 已提交
1441
  if (pField->bytes <= pTag->bytes) {
S
Shengliang Guan 已提交
1442 1443 1444 1445
    terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
    return -1;
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}

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

  return 0;
}

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

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

  return 0;
}

dengyihao's avatar
dengyihao 已提交
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 1666
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 已提交
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
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 已提交
1678 1679 1680
  tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
  tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
  tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
D
dapan1121 已提交
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
  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 已提交
1705
    pSchema->flags = pSrcSchema->flags;
D
dapan1121 已提交
1706 1707 1708 1709 1710 1711 1712 1713
    pSchema->colId = pSrcSchema->colId;
    pSchema->bytes = pSrcSchema->bytes;
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  tFreeSMAlterStbRsp(&alterRsp);

  *pCont = cont;
  *pLen = contLen;
1854

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

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

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

  SEncoder       ec = {0};
  uint32_t       contLen = 0;
1872
  SMCreateStbRsp stbRsp = {0};
L
Liu Jicong 已提交
1873
  SName          name = {0};
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 1904
  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 已提交
1905

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

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

  return ret;
}

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

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

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

  code = 0;

_OVER:
  mndTransDrop(pTrans);
  return code;
}

dengyihao's avatar
dengyihao 已提交
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
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 已提交
1958 1959 1960 1961 1962 1963 1964
  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 已提交
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 2014
  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 已提交
2015
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
2016 2017 2018 2019
  bool    needRsp = true;
  int32_t code = -1;
  SField *pField0 = NULL;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return 0;
}

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

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

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

  return 0;
}

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

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

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

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

2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
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 已提交
2237 2238 2239
      terrno = TSDB_CODE_MND_INVALID_TOPIC_OPTION;
      mError("topic:%s, create ast error", pTopic->name);
      sdbRelease(pSdb, pTopic);
2240 2241 2242 2243 2244 2245 2246 2247 2248
      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 已提交
2249
      if (pCol->tableId == suid) {
2250 2251
        sdbRelease(pSdb, pTopic);
        nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2252
        nodesDestroyList(pNodeList);
2253 2254 2255 2256 2257 2258 2259 2260
        return -1;
      } else {
        goto NEXT;
      }
    }
  NEXT:
    sdbRelease(pSdb, pTopic);
    nodesDestroyNode(pAst);
L
Liu Jicong 已提交
2261
    nodesDestroyList(pNodeList);
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
  }
  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 已提交
2274 2275 2276 2277 2278 2279
    if (pStream->smaId != 0) {
      sdbRelease(pSdb, pStream);
      continue;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  if (0 == strcmp(cfgReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2454
    mInfo("information_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2455 2456 2457 2458
    if (mndBuildInsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else if (0 == strcmp(cfgReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2459
    mInfo("performance_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
2460 2461 2462 2463
    if (mndBuildPerfsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
      goto _OVER;
    }
  } else {
2464
    mInfo("stb:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
D
dapan1121 已提交
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 2497
    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 已提交
2498
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
S
Shengliang Guan 已提交
2499
                           int32_t *pRspLen) {
D
dapan1121 已提交
2500
  SSTbHbRsp hbRsp = {0};
D
dapan1121 已提交
2501 2502
  hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
  if (hbRsp.pMetaRsp == NULL) {
S
Shengliang Guan 已提交
2503 2504 2505
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
2506

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

L
Liu Jicong 已提交
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);
}

C
cademfly 已提交
2617 2618 2619 2620 2621 2622 2623
void mndExtractShortDbNameFromStbFullName(const char *stbFullName, char *dst) {
  SName name = {0};
  tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

  tNameGetDbName(&name, dst);
}

L
Liu Jicong 已提交
2624
void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t dstSize) {
S
Shengliang Guan 已提交
2625 2626
  int32_t pos = -1;
  int32_t num = 0;
L
Liu Jicong 已提交
2627 2628
  for (pos = 0; stbFullName[pos] != 0; ++pos) {
    if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
S
Shengliang Guan 已提交
2629 2630 2631 2632
    if (num == 2) break;
  }

  if (num == 2) {
L
Liu Jicong 已提交
2633
    tstrncpy(dst, stbFullName + pos + 1, dstSize);
S
Shengliang Guan 已提交
2634 2635 2636
  }
}

dengyihao's avatar
dengyihao 已提交
2637 2638 2639 2640 2641 2642
// 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 已提交
2643
//
dengyihao's avatar
dengyihao 已提交
2644 2645 2646 2647 2648
//   SRetrieveTableReq retrieveReq = {0};
//   if (tDeserializeSRetrieveTableReq(pReq->pCont, pReq->contLen, &retrieveReq) != 0) {
//     terrno = TSDB_CODE_INVALID_MSG;
//     return -1;
//   }
wmmhello's avatar
wmmhello 已提交
2649
//
dengyihao's avatar
dengyihao 已提交
2650 2651 2652 2653 2654
//   SMnode    *pMnode = pReq->info.node;
//   SSdb      *pSdb = pMnode->pSdb;
//   int32_t    numOfRows = 0;
//   SDbObj    *pDb = NULL;
//   ESdbStatus objStatus = 0;
wmmhello's avatar
wmmhello 已提交
2655
//
dengyihao's avatar
dengyihao 已提交
2656 2657 2658
//   SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user);
//   if (pUser == NULL) return 0;
//   bool sysinfo = pUser->sysInfo;
wmmhello's avatar
wmmhello 已提交
2659
//
dengyihao's avatar
dengyihao 已提交
2660
//   // Append the information_schema database into the result.
wmmhello's avatar
wmmhello 已提交
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 2746 2747 2748 2749 2750 2751 2752 2753
////  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 已提交
2754
//        colDataSetNULL(pColInfo, numOfRows);
wmmhello's avatar
wmmhello 已提交
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 2905 2906 2907 2908 2909 2910 2911 2912
//      }
//
//      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 已提交
2913 2914
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode  *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
2915
  SSdb    *pSdb = pMnode->pSdb;
S
Shengliang Guan 已提交
2916 2917 2918
  int32_t  numOfRows = 0;
  SStbObj *pStb = NULL;
  int32_t  cols = 0;
S
Shengliang Guan 已提交
2919

H
Hongze Cheng 已提交
2920
  SDbObj *pDb = NULL;
H
Haojun Liao 已提交
2921 2922
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
D
dapan1121 已提交
2923
    if (pDb == NULL) return terrno;
H
Haojun Liao 已提交
2924
  }
S
Shengliang Guan 已提交
2925

S
Shengliang Guan 已提交
2926
  while (numOfRows < rows) {
S
Shengliang Guan 已提交
2927
    pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
S
Shengliang Guan 已提交
2928 2929
    if (pShow->pIter == NULL) break;

H
Haojun Liao 已提交
2930
    if (pDb != NULL && pStb->dbUid != pDb->uid) {
S
Shengliang Guan 已提交
2931
      sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
2932 2933 2934 2935 2936
      continue;
    }

    cols = 0;

H
Haojun Liao 已提交
2937
    SName name = {0};
H
Hongze Cheng 已提交
2938
    char  stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
L
Liu Jicong 已提交
2939
    mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
2940
    varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
S
Shengliang Guan 已提交
2941

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

H
Hongze Cheng 已提交
2945 2946
    char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
    tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
2947 2948 2949
    tNameGetDbName(&name, varDataVal(db));
    varDataSetLen(db, strlen(varDataVal(db)));

2950
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2951
    colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
2952 2953

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

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

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

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

2965
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
2966 2967 2968
    if (pStb->commentLen > 0) {
      char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, pStb->comment);
2969
      colDataSetVal(pColInfo, numOfRows, comment, false);
2970
    } else if (pStb->commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
2971 2972
      char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
      STR_TO_VARSTR(comment, "");
2973
      colDataSetVal(pColInfo, numOfRows, comment, false);
wmmhello's avatar
wmmhello 已提交
2974
    } else {
2975
      colDataSetNULL(pColInfo, numOfRows);
S
Shengliang Guan 已提交
2976
    }
H
Haojun Liao 已提交
2977

2978 2979 2980 2981 2982
    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++);
2983
    colDataSetVal(pColInfo, numOfRows, (const char *)watermark, false);
2984 2985 2986 2987 2988 2989

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

S
Shengliang Guan 已提交
2992
    char    rollup[160 + VARSTR_HEADER_SIZE] = {0};
2993
    int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs);
D
dapan1121 已提交
2994 2995
    char   *sep = ", ";
    int32_t sepLen = strlen(sep);
D
dapan1121 已提交
2996
    int32_t rollupLen = sizeof(rollup) - VARSTR_HEADER_SIZE - 2;
2997 2998 2999
    for (int32_t i = 0; i < rollupNum; ++i) {
      char *funcName = taosArrayGet(pStb->pFuncs, i);
      if (i) {
D
dapan1121 已提交
3000 3001
        strncat(varDataVal(rollup), sep, rollupLen);
        rollupLen -= sepLen;
3002
      }
D
dapan1121 已提交
3003 3004
      strncat(varDataVal(rollup), funcName, rollupLen);
      rollupLen -= strlen(funcName);
3005 3006 3007 3008
    }
    varDataSetLen(rollup, strlen(varDataVal(rollup)));

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

S
Shengliang Guan 已提交
3011
    numOfRows++;
S
Shengliang Guan 已提交
3012
    sdbRelease(pSdb, pStb);
S
Shengliang Guan 已提交
3013 3014
  }

H
Haojun Liao 已提交
3015 3016 3017 3018
  if (pDb != NULL) {
    mndReleaseDb(pMnode, pDb);
  }

3019
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
3020 3021 3022
  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3023 3024
static int32_t buildDbColsInfoBlock(const SSDataBlock *p, const SSysTableMeta *pSysDbTableMeta, size_t size,
                                    const char *dbName, const char *tbName) {
wmmhello's avatar
wmmhello 已提交
3025 3026 3027 3028 3029 3030 3031 3032 3033
  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 已提交
3034 3035 3036 3037 3038
    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 已提交
3039 3040 3041
      continue;
    }

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

dengyihao's avatar
dengyihao 已提交
3044
    for (int32_t j = 0; j < pm->colNum; j++) {
wmmhello's avatar
wmmhello 已提交
3045
      // table name
dengyihao's avatar
dengyihao 已提交
3046
      SColumnInfoData *pColInfoData = taosArrayGet(p->pDataBlock, 0);
wmmhello's avatar
wmmhello 已提交
3047 3048
      colDataAppend(pColInfoData, numOfRows, tName, false);

wmmhello's avatar
wmmhello 已提交
3049
      // database name
wmmhello's avatar
wmmhello 已提交
3050
      pColInfoData = taosArrayGet(p->pDataBlock, 1);
wmmhello's avatar
wmmhello 已提交
3051
      colDataAppend(pColInfoData, numOfRows, dName, false);
wmmhello's avatar
wmmhello 已提交
3052 3053

      pColInfoData = taosArrayGet(p->pDataBlock, 2);
wmmhello's avatar
wmmhello 已提交
3054
      colDataAppend(pColInfoData, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067

      // 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 已提交
3068 3069
        colTypeLen +=
            sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)", (int32_t)(pm->schema[j].bytes - VARSTR_HEADER_SIZE));
wmmhello's avatar
wmmhello 已提交
3070
      } else if (colType == TSDB_DATA_TYPE_NCHAR) {
dengyihao's avatar
dengyihao 已提交
3071 3072
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)((pm->schema[j].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
wmmhello's avatar
wmmhello 已提交
3073 3074
      }
      varDataSetLen(colTypeStr, colTypeLen);
dengyihao's avatar
dengyihao 已提交
3075
      colDataAppend(pColInfoData, numOfRows, (char *)colTypeStr, false);
wmmhello's avatar
wmmhello 已提交
3076 3077

      pColInfoData = taosArrayGet(p->pDataBlock, 5);
dengyihao's avatar
dengyihao 已提交
3078
      colDataAppend(pColInfoData, numOfRows, (const char *)&pm->schema[j].bytes, false);
wmmhello's avatar
wmmhello 已提交
3079 3080
      for (int32_t k = 6; k <= 8; ++k) {
        pColInfoData = taosArrayGet(p->pDataBlock, k);
X
Xiaoyu Wang 已提交
3081
        colDataSetNULL(pColInfoData, numOfRows);
wmmhello's avatar
wmmhello 已提交
3082 3083 3084 3085 3086 3087 3088 3089 3090
      }

      numOfRows += 1;
    }
  }

  return numOfRows;
}

dengyihao's avatar
dengyihao 已提交
3091
static int32_t buildSysDbColsInfo(SSDataBlock *p, char *db, char *tb) {
wmmhello's avatar
wmmhello 已提交
3092
  size_t               size = 0;
dengyihao's avatar
dengyihao 已提交
3093
  const SSysTableMeta *pSysDbTableMeta = NULL;
wmmhello's avatar
wmmhello 已提交
3094

dengyihao's avatar
dengyihao 已提交
3095 3096
  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 已提交
3097 3098 3099
    return p->info.rows;
  }

wmmhello's avatar
wmmhello 已提交
3100
  getInfosDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3101
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_INFORMATION_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3102 3103

  getPerfDbMeta(&pSysDbTableMeta, &size);
wmmhello's avatar
wmmhello 已提交
3104
  p->info.rows = buildDbColsInfoBlock(p, pSysDbTableMeta, size, TSDB_PERFORMANCE_SCHEMA_DB, tb);
wmmhello's avatar
wmmhello 已提交
3105 3106 3107 3108 3109 3110 3111 3112 3113

  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 已提交
3114
  int32_t numOfRows = buildSysDbColsInfo(pBlock, pShow->db, pShow->filterTb);
wmmhello's avatar
wmmhello 已提交
3115
  mDebug("mndRetrieveStbCol get system table cols, rows:%d, db:%s", numOfRows, pShow->db);
wmmhello's avatar
wmmhello 已提交
3116 3117 3118 3119 3120 3121
  SDbObj *pDb = NULL;
  if (strlen(pShow->db) > 0) {
    pDb = mndAcquireDb(pMnode, pShow->db);
    if (pDb == NULL) return terrno;
  }

dengyihao's avatar
dengyihao 已提交
3122
  char typeName[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
wmmhello's avatar
wmmhello 已提交
3123
  STR_TO_VARSTR(typeName, "SUPER_TABLE");
D
dapan1121 已提交
3124 3125
  bool fetch = pShow->restore ? false : true;
  pShow->restore = false;
wmmhello's avatar
wmmhello 已提交
3126
  while (numOfRows < rows) {
D
dapan1121 已提交
3127 3128 3129 3130 3131 3132 3133 3134 3135
    if (fetch) {
      pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
      if (pShow->pIter == NULL) break;
    } else {
      fetch = true;
      void  *pKey = taosHashGetKey(pShow->pIter, NULL);
      pStb = sdbAcquire(pSdb, SDB_STB, pKey);
      if (!pStb) continue;
    }
wmmhello's avatar
wmmhello 已提交
3136 3137 3138 3139 3140 3141 3142 3143 3144

    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 已提交
3145
    if (pShow->filterTb[0] && strncmp(pShow->filterTb, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN) != 0) {
wmmhello's avatar
wmmhello 已提交
3146 3147 3148
      sdbRelease(pSdb, pStb);
      continue;
    }
D
dapan1121 已提交
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159

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

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

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

wmmhello's avatar
wmmhello 已提交
3164 3165 3166 3167 3168
    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 已提交
3169 3170
    for (int i = 0; i < pStb->numOfColumns; i++) {
      int32_t          cols = 0;
wmmhello's avatar
wmmhello 已提交
3171 3172 3173 3174
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataAppend(pColInfo, numOfRows, (const char *)stbName, false);

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

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
wmmhello's avatar
wmmhello 已提交
3178
      colDataAppend(pColInfo, numOfRows, typeName, false);
wmmhello's avatar
wmmhello 已提交
3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194

      // 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 已提交
3195 3196
        colTypeLen += sprintf(varDataVal(colTypeStr) + colTypeLen, "(%d)",
                              (int32_t)((pStb->pColumns[i].bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
wmmhello's avatar
wmmhello 已提交
3197 3198
      }
      varDataSetLen(colTypeStr, colTypeLen);
dengyihao's avatar
dengyihao 已提交
3199
      colDataAppend(pColInfo, numOfRows, (char *)colTypeStr, false);
wmmhello's avatar
wmmhello 已提交
3200 3201

      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
dengyihao's avatar
dengyihao 已提交
3202 3203
      colDataAppend(pColInfo, numOfRows, (const char *)&pStb->pColumns[i].bytes, false);
      while (cols < pShow->numOfColumns) {
wmmhello's avatar
wmmhello 已提交
3204
        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
X
Xiaoyu Wang 已提交
3205
        colDataSetNULL(pColInfo, numOfRows);
wmmhello's avatar
wmmhello 已提交
3206
      }
wmmhello's avatar
wmmhello 已提交
3207
      numOfRows++;
wmmhello's avatar
wmmhello 已提交
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217
    }

    sdbRelease(pSdb, pStb);
  }

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

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

wmmhello's avatar
wmmhello 已提交
3220 3221 3222
  return numOfRows;
}

S
Shengliang Guan 已提交
3223
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
S
Shengliang Guan 已提交
3224 3225
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
D
dapan1121 已提交
3226
}
S
Shengliang Guan 已提交
3227 3228 3229 3230 3231 3232 3233 3234 3235 3236

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 已提交
3237
}
dengyihao's avatar
dengyihao 已提交
3238 3239 3240 3241 3242 3243

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

dengyihao's avatar
dengyihao 已提交
3244
/*int32_t mndAddIndexImpl(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp, void *sql,
dengyihao's avatar
dengyihao 已提交
3245 3246
                        int32_t len) {
  // impl later
dengyihao's avatar
dengyihao 已提交
3247
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
3248 3249 3250 3251 3252 3253 3254
  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 已提交
3255 3256
  if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
  if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3257
  if (mndSetAlterStbRedoActions2(pMnode, pTrans, pDb, pStb, sql, len) != 0) goto _OVER;
dengyihao's avatar
dengyihao 已提交
3258 3259
  if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;

dengyihao's avatar
dengyihao 已提交
3260 3261 3262 3263 3264
  return code;

_OVER:
  mndTransDrop(pTrans);
  return code;
dengyihao's avatar
dengyihao 已提交
3265
}
dengyihao's avatar
dengyihao 已提交
3266 3267 3268 3269 3270
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 已提交
3271 3272 3273
  SStbObj  stbObj = {0};
  SStbObj *pNew = &stbObj;

dengyihao's avatar
dengyihao 已提交
3274 3275 3276 3277 3278 3279 3280 3281 3282
  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 已提交
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297
  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 已提交
3298
    terrno = TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST;
dengyihao's avatar
dengyihao 已提交
3299 3300 3301 3302 3303 3304
    return -1;
  } else {
    pTag->flags |= COL_IDX_ON;
  }
  pNew->tagVer++;

dengyihao's avatar
dengyihao 已提交
3305 3306 3307
  code = mndAddIndexImpl(pMnode, pReq, pDb, pNew, needRsp, pReq->pCont, pReq->contLen);

  return code;
dengyihao's avatar
dengyihao 已提交
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 3334 3335 3336 3337 3338 3339 3340 3341 3342
}
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 已提交
3343 3344 3345
  if (terrno == TSDB_CODE_MND_TAG_INDEX_ALREADY_EXIST || terrno == TSDB_CODE_MND_TAG_NOT_EXIST) {
    return terrno;
  } else {
dengyihao's avatar
dengyihao 已提交
3346
    if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
dengyihao's avatar
dengyihao 已提交
3347
  }
dengyihao's avatar
dengyihao 已提交
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369
_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 已提交
3370
}*/