• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4984

13 Mar 2026 03:38AM UTC coverage: 68.643% (-0.01%) from 68.653%
#4984

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

3048 existing lines in 150 files now uncovered.

212713 of 309883 relevant lines covered (68.64%)

135561814.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#include "meta.h"
17

18
extern int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry);
19
extern int32_t metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, int64_t ownerId,
20
                                 STableMetaRsp *pMetaRsp);
21
extern int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
22
                                    int64_t ownerId, STableMetaRsp *pMetaRsp, int8_t tableType);
23
extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
24
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
25
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
26
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
27
extern int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema);
28
extern int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum);
29

30
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,177,603✔
31
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,177,603✔
32
  void     *value = NULL;
5,203,409✔
33
  int32_t   valueSize = 0;
5,204,472✔
34
  SMetaInfo info;
5,203,515✔
35

36
  // check name
37
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
5,203,659✔
38
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
9,332✔
39
              pReq->name, version);
40
    return TSDB_CODE_INVALID_MSG;
×
41
  }
42

43
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
5,185,645✔
44
  if (r == 0) {  // name exists, check uid and type
5,192,020✔
45
    int64_t uid = *(tb_uid_t *)value;
4,858✔
46
    tdbFree(value);
4,858✔
47

48
    if (pReq->suid != uid) {
4,858✔
49
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
2,232✔
50
                " version:%" PRId64,
51
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
52
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
2,232✔
53
    }
54

55
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
2,626✔
56
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
57
                " not found, this is an internal error in meta, version:%" PRId64,
58
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
59
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
60
    }
61

62
    if (info.uid == info.suid) {
2,626✔
63
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
2,626✔
64
    } else {
65
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
66
                " already exists but not a super table, version:%" PRId64,
67
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
68
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
69
    }
70
  }
71

72
  // check suid
73
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
5,187,162✔
74
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
×
75
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
76
    return TSDB_CODE_INVALID_MSG;
×
77
  }
78

79
  return TSDB_CODE_SUCCESS;
5,195,557✔
80
}
81

82
static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,503,129✔
83
  int32_t   code = TSDB_CODE_SUCCESS;
1,503,129✔
84
  void     *value = NULL;
1,503,129✔
85
  int32_t   valueSize = 0;
1,503,129✔
86
  SMetaInfo info;
1,503,129✔
87

88
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,503,129✔
89
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
90
              __FILE__, __LINE__, pReq->name, version);
91
    return TSDB_CODE_INVALID_MSG;
×
92
  }
93

94
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
1,503,129✔
95
  if (TSDB_CODE_SUCCESS != code) {
1,503,129✔
96
    if (pReq->igNotExists) {
×
97
      metaTrace("vgId:%d, %s success since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
98
                pReq->name, version);
99
    } else {
100
      metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
101
                __func__, __FILE__, __LINE__, pReq->name, version);
102
    }
103
    return TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
104
  }
105
  pReq->uid = *(tb_uid_t *)value;
1,503,129✔
106
  tdbFreeClear(value);
1,503,129✔
107

108
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
1,503,129✔
109
  if (TSDB_CODE_SUCCESS != code) {
1,503,129✔
110
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
111
              " not found, this is an internal error, version:%" PRId64,
112
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, version);
113
    code = TSDB_CODE_INTERNAL_ERROR;
×
114
    return code;
×
115
  }
116
  pReq->suid = info.suid;
1,503,129✔
117

118
  return code;
1,503,129✔
119
}
120

121
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
911,091✔
122
  int32_t   code = TSDB_CODE_SUCCESS;
911,091✔
123
  void     *value = NULL;
911,091✔
124
  int32_t   valueSize = 0;
911,517✔
125
  SMetaInfo info;
911,517✔
126

127
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
911,517✔
128
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
129
              __FILE__, __LINE__, pReq->name, version);
130
    return TSDB_CODE_INVALID_MSG;
×
131
  }
132

133
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
911,517✔
134
  if (code) {
908,063✔
135
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
744✔
136
              __FILE__, __LINE__, pReq->name, version);
137
    return TSDB_CODE_TDB_STB_NOT_EXIST;
744✔
138
  } else {
139
    int64_t uid = *(int64_t *)value;
907,319✔
140
    tdbFreeClear(value);
908,446✔
141

142
    if (uid != pReq->suid) {
910,186✔
143
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " not match, version:%" PRId64,
744✔
144
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
145
      return TSDB_CODE_TDB_STB_NOT_EXIST;
744✔
146
    }
147
  }
148

149
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
904,335✔
150
  if (code) {
907,886✔
151
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
152
              " not found, this is an internal error, version:%" PRId64,
153
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
154
    return TSDB_CODE_INTERNAL_ERROR;
×
155
  }
156
  if (info.suid != info.uid) {
907,886✔
157
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a super table, version:%" PRId64,
×
158
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
159
    return TSDB_CODE_INVALID_MSG;
×
160
  }
161
  return code;
907,886✔
162
}
163

164
// Create Super Table
165
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,185,215✔
166
  int32_t code = TSDB_CODE_SUCCESS;
5,185,215✔
167

168
  // check request
169
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,185,215✔
170
  if (code != TSDB_CODE_SUCCESS) {
5,198,631✔
171
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
4,858✔
172
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
2,626✔
173
               pReq->name, pReq->suid, version);
174
      TAOS_RETURN(TSDB_CODE_SUCCESS);
2,626✔
175
    } else {
176
      TAOS_RETURN(code);
2,232✔
177
    }
178
  }
179

180
  // handle entry
181
  SMetaEntry entry = {
10,369,235✔
182
      .version = version,
183
      .type = TSDB_SUPER_TABLE,
184
      .uid = pReq->suid,
5,190,329✔
185
      .name = pReq->name,
5,185,431✔
186
      .stbEntry.schemaRow = pReq->schemaRow,
187
      .stbEntry.schemaTag = pReq->schemaTag,
188
      .stbEntry.keep = pReq->keep,
5,178,182✔
189
      .stbEntry.ownerId = pReq->ownerId,
5,176,724✔
190
  };
191
  if (pReq->rollup) {
5,186,513✔
192
    TABLE_SET_ROLLUP(entry.flags);
×
193
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
194
  }
195
  if (pReq->colCmpred) {
5,173,509✔
196
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,191,695✔
197
    entry.colCmpr = pReq->colCmpr;
5,191,695✔
198
  }
199

200
  entry.pExtSchemas = pReq->pExtSchemas;
5,159,379✔
201

202
  if (pReq->virtualStb) {
5,172,350✔
203
    TABLE_SET_VIRTUAL(entry.flags);
78,469✔
204
  }
205

206
  code = metaHandleEntry2(pMeta, &entry);
5,171,777✔
207
  if (TSDB_CODE_SUCCESS == code) {
5,202,975✔
208
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
5,202,975✔
209
             pReq->suid, version);
210
  } else {
211
    metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
212
              pReq->suid, tstrerror(code));
213
  }
214
  TAOS_RETURN(code);
5,202,975✔
215
}
216

217
// Drop Super Table
218
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
911,517✔
219
  int32_t code = TSDB_CODE_SUCCESS;
911,517✔
220

221
  // check request
222
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
911,517✔
223
  if (code) {
911,052✔
224
    TAOS_RETURN(code);
1,488✔
225
  }
226

227
  // handle entry
228
  SMetaEntry entry = {
909,564✔
229
      .version = verison,
230
      .type = -TSDB_SUPER_TABLE,
231
      .uid = pReq->suid,
908,627✔
232
  };
233
  code = metaHandleEntry2(pMeta, &entry);
905,107✔
234
  if (code) {
909,084✔
235
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
236
              tstrerror(code));
237
  } else {
238
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
909,084✔
239
             pReq->suid, verison);
240
  }
241
  TAOS_RETURN(code);
910,029✔
242
}
243

244
// Alter Super Table
245

246
// Create Child Table
247
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
76,769,280✔
248
  int32_t   code = TSDB_CODE_SUCCESS;
76,769,280✔
249
  void     *value = NULL;
76,769,280✔
250
  int32_t   valueSize = 0;
76,770,827✔
251
  SMetaInfo info;
76,767,268✔
252

253
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
76,758,271✔
254
      pReq->ctb.suid == 0) {
76,761,166✔
255
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
202✔
256
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
257
    return TSDB_CODE_INVALID_MSG;
×
258
  }
259

260
  // check table existence
261
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
76,767,669✔
262
    pReq->uid = *(int64_t *)value;
329,933✔
263
    tdbFreeClear(value);
329,971✔
264

265
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
329,971✔
266
      metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
267
                ", which is an internal error, version:%" PRId64,
268
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
269
      return TSDB_CODE_INTERNAL_ERROR;
×
270
    }
271

272
    // check table type
273
    if (info.suid == info.uid || info.suid == 0) {
329,895✔
274
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
1,790✔
275
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
276
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
1,790✔
277
    }
278

279
    // check suid
280
    if (info.suid != pReq->ctb.suid) {
328,105✔
281
      metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " exists in another stable with uid %" PRId64
×
282
                " instead of stable with uid %" PRId64 " version:%" PRId64,
283
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, info.suid, pReq->ctb.suid,
284
                version);
285
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
286
    }
287

288
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
328,143✔
289
  }
290

291
  // check super table existence
292
  SMetaEntry *pStbEntry = NULL;
76,436,264✔
293
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
76,436,192✔
294
  if (code) {
76,419,973✔
295
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
296
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
297
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
298
  }
299

300
  if (pStbEntry->type != TSDB_SUPER_TABLE) {
76,419,973✔
301
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64,
×
302
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
303
    metaFetchEntryFree(&pStbEntry);
×
304
    return TSDB_CODE_INVALID_MSG;
×
305
  }
306

307
  if (pStbEntry->uid != pReq->ctb.suid) {
76,413,937✔
308
    metaError("vgId:%d, %s failed at %s:%d since super table %s uid %" PRId64 " does not match request uid %" PRId64
×
309
              ", version:%" PRId64,
310
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, pStbEntry->uid, pReq->ctb.suid,
311
              version);
312
    metaFetchEntryFree(&pStbEntry);
×
313
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
314
  }
315

316
  // Check tag value
317
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
76,407,906✔
318
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
76,429,856✔
319
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
76,414,687✔
320
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
316,329,634✔
321
      STagVal tagVal = {
240,213,368✔
322
          .cid = pTagSchema->pSchema[i].colId,
240,195,755✔
323
      };
324

325
      if (tTagGet(pTag, &tagVal)) {
240,213,507✔
326
        if (pTagSchema->pSchema[i].type != tagVal.type) {
197,939,412✔
327
          metaError("vgId:%d, %s failed at %s:%d since child table %s tag type does not match the expected type, version:%" PRId64,
×
328
                    TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, version);
329
          metaFetchEntryFree(&pStbEntry);
×
330
          return TSDB_CODE_INVALID_MSG;
×
331
        }
332
      }
333
    }
334
  }
335

336
  metaFetchEntryFree(&pStbEntry);
76,412,643✔
337

338
  // check grant
339
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
76,418,101✔
340
    code = grantCheck(TSDB_GRANT_TIMESERIES);
76,424,322✔
341
    if (TSDB_CODE_SUCCESS != code) {
76,405,019✔
342
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
343
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
344
    }
345
  }
346
  return code;
76,406,654✔
347
}
348

349
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
76,176,361✔
350
  int32_t code = TSDB_CODE_SUCCESS;
76,176,361✔
351

352
  if (NULL == ppRsp) {
76,176,361✔
353
    return code;
×
354
  }
355

356
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
76,176,361✔
357
  if (NULL == ppRsp) {
76,155,364✔
358
    return terrno;
×
359
  }
360

361
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
76,155,364✔
362
  (*ppRsp)->tuid = pEntry->uid;
76,172,658✔
363
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
76,175,917✔
364
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
76,184,064✔
365

366
  return code;
76,201,948✔
367
}
368

369
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
76,535,638✔
370
  int32_t code = TSDB_CODE_SUCCESS;
76,535,638✔
371

372
  // check request
373
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
76,535,638✔
374
  if (code) {
76,499,274✔
375
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
329,895✔
376
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,790✔
377
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
378
    }
379
    return code;
329,857✔
380
  }
381

382
  SMetaEntry entry = {
76,169,379✔
383
      .version = version,
384
      .type = TSDB_CHILD_TABLE,
385
      .uid = pReq->uid,
76,176,422✔
386
      .name = pReq->name,
76,177,035✔
387
      .ctbEntry.btime = pReq->btime,
76,154,639✔
388
      .ctbEntry.ttlDays = pReq->ttl,
76,164,418✔
389
      .ctbEntry.commentLen = pReq->commentLen,
76,196,031✔
390
      .ctbEntry.comment = pReq->comment,
76,175,344✔
391
      .ctbEntry.suid = pReq->ctb.suid,
76,158,955✔
392
      .ctbEntry.pTags = pReq->ctb.pTag,
76,160,653✔
393
  };
394

395
  // build response
396
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
76,191,740✔
397
  if (code) {
76,200,772✔
398
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
399
              tstrerror(code));
400
  }
401

402
  // handle entry
403
  code = metaHandleEntry2(pMeta, &entry);
76,200,772✔
404
  if (TSDB_CODE_SUCCESS == code) {
76,201,147✔
405
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
76,201,964✔
406
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
407
  } else {
408
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
409
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
410
              pReq->ctb.suid, version);
411
  }
412
  return code;
76,215,326✔
413
}
414

415
// Drop Child Table
416

417
// Alter Child Table
418

419
// Create Normal Table
420
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
7,179,055✔
421
  int32_t code = 0;
7,179,055✔
422
  void   *value = NULL;
7,179,055✔
423
  int32_t valueSize = 0;
7,179,055✔
424

425
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,179,055✔
426
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
427
              __FILE__, __LINE__, pReq->name, version);
428
    return TSDB_CODE_INVALID_MSG;
×
429
  }
430

431
  // check name
432
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
7,179,055✔
433
    // for auto create table, we return the uid of the existing table
434
    pReq->uid = *(tb_uid_t *)value;
37,083✔
435
    tdbFree(value);
37,083✔
436
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
37,083✔
437
  }
438

439
  // grant check
440
  code = grantCheck(TSDB_GRANT_TIMESERIES);
7,141,972✔
441
  if (code) {
7,141,972✔
442
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
443
              __FILE__, __LINE__, tstrerror(code), version, pReq->name);
444
  }
445
  return code;
7,141,972✔
446
}
447

448
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
6,992,462✔
449
  int32_t code = TSDB_CODE_SUCCESS;
6,992,462✔
450

451
  if (NULL == ppRsp) {
6,992,462✔
452
    return code;
×
453
  }
454

455
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
6,992,462✔
456
  if (NULL == *ppRsp) {
6,992,462✔
457
    return terrno;
×
458
  }
459

460
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, pEntry->ntbEntry.ownerId, *ppRsp);
6,992,462✔
461
  if (code) {
6,992,462✔
462
    taosMemoryFreeClear(*ppRsp);
×
463
    return code;
×
464
  }
465

466
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
80,053,154✔
467
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
73,060,692✔
468
    (*ppRsp)->pSchemaExt[i].colId = p->id;
73,060,692✔
469
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
73,060,692✔
470
    if (pEntry->pExtSchemas) {
73,060,692✔
471
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
457,647✔
472
    }
473
  }
474

475
  return code;
6,992,462✔
476
}
477

478
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
7,029,545✔
479
  int32_t code = TSDB_CODE_SUCCESS;
7,029,545✔
480

481
  // check request
482
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
7,029,545✔
483
  if (code) {
7,029,545✔
484
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
37,083✔
485
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
486
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
487
    }
488
    TAOS_RETURN(code);
37,083✔
489
  }
490

491
  SMetaEntry entry = {
13,984,619✔
492
      .version = version,
493
      .type = TSDB_NORMAL_TABLE,
494
      .uid = pReq->uid,
6,992,462✔
495
      .name = pReq->name,
6,992,462✔
496
      .ntbEntry.btime = pReq->btime,
6,992,462✔
497
      .ntbEntry.ttlDays = pReq->ttl,
6,992,462✔
498
      .ntbEntry.commentLen = pReq->commentLen,
6,992,462✔
499
      .ntbEntry.comment = pReq->comment,
6,992,462✔
500
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
501
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
6,992,462✔
502
      .ntbEntry.ownerId = pReq->ntb.userId,
6,992,462✔
503
      .colCmpr = pReq->colCmpr,
504
      .pExtSchemas = pReq->pExtSchemas,
6,992,462✔
505
  };
506
  TABLE_SET_COL_COMPRESSED(entry.flags);
6,992,462✔
507

508
  // build response
509
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
6,992,462✔
510
  if (code) {
6,992,462✔
511
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
512
              tstrerror(code));
513
  }
514

515
  // handle entry
516
  code = metaHandleEntry2(pMeta, &entry);
6,992,462✔
517
  if (TSDB_CODE_SUCCESS == code) {
6,992,462✔
518
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
6,992,462✔
519
             pReq->name, pReq->uid);
520
  } else {
521
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
522
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
523
  }
524
  TAOS_RETURN(code);
6,992,462✔
525
}
526

527
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
149,510✔
528
  int32_t code = TSDB_CODE_SUCCESS;
149,510✔
529

530
  if (NULL == ppRsp) {
149,510✔
531
    return code;
×
532
  }
533

534
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
149,510✔
535
  if (NULL == *ppRsp) {
149,510✔
536
    return terrno;
×
537
  }
538

539
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, pEntry->ntbEntry.ownerId, *ppRsp,
149,510✔
540
                              TSDB_VIRTUAL_NORMAL_TABLE);
541
  if (code) {
149,510✔
542
    taosMemoryFreeClear(*ppRsp);
×
543
    return code;
×
544
  }
545

546
  return code;
149,510✔
547
}
548

549
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
149,510✔
550
  // check request
551
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
149,510✔
552
  if (code) {
149,510✔
553
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
554
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
555
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
556
    }
557
    TAOS_RETURN(code);
×
558
  }
559

560
  SMetaEntry entry = {.version = version,
299,020✔
561
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
562
                      .uid = pReq->uid,
149,510✔
563
                      .name = pReq->name,
149,510✔
564
                      .ntbEntry.btime = pReq->btime,
149,510✔
565
                      .ntbEntry.ttlDays = pReq->ttl,
149,510✔
566
                      .ntbEntry.commentLen = pReq->commentLen,
149,510✔
567
                      .ntbEntry.comment = pReq->comment,
149,510✔
568
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
569
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
149,510✔
570
                      .ntbEntry.ownerId = pReq->ntb.userId,
149,510✔
571
                      .colRef = pReq->colRef};
572

573
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
149,510✔
574
  if (code) {
149,510✔
575
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
576
              tstrerror(code));
577
    TAOS_RETURN(code);
×
578
  }
579

580
  // handle entry
581
  code = metaHandleEntry2(pMeta, &entry);
149,510✔
582
  if (TSDB_CODE_SUCCESS == code) {
149,510✔
583
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
149,510✔
584
             version, pReq->name, pReq->uid);
585
  } else {
586
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
587
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
588
  }
589
  TAOS_RETURN(code);
149,510✔
590
#if 0
591
  metaTimeSeriesNotifyCheck(pMeta);
592
#endif
593
}
594

595
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
233,607✔
596
  int32_t code = TSDB_CODE_SUCCESS;
233,607✔
597

598
  if (NULL == ppRsp) {
233,607✔
599
    return code;
×
600
  }
601

602
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
233,607✔
603
  if (NULL == *ppRsp) {
233,607✔
604
    return terrno;
×
605
  }
606

607
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, 0, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
233,607✔
608
  if (code) {
233,607✔
609
    taosMemoryFreeClear(*ppRsp);
×
610
    return code;
×
611
  }
612
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
233,607✔
613

614
  return code;
233,607✔
615
}
616

617
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
233,607✔
618
  // check request
619
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
233,607✔
620
  if (code) {
233,607✔
621
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
622
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
623
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
624
    }
625
    TAOS_RETURN(code);
×
626
  }
627

628
  SMetaEntry entry = {.version = version,
467,214✔
629
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
630
                      .uid = pReq->uid,
233,607✔
631
                      .name = pReq->name,
233,607✔
632
                      .ctbEntry.btime = pReq->btime,
233,607✔
633
                      .ctbEntry.ttlDays = pReq->ttl,
233,607✔
634
                      .ctbEntry.commentLen = pReq->commentLen,
233,607✔
635
                      .ctbEntry.comment = pReq->comment,
233,607✔
636
                      .ctbEntry.suid = pReq->ctb.suid,
233,607✔
637
                      .ctbEntry.pTags = pReq->ctb.pTag,
233,607✔
638
                      .colRef = pReq->colRef};
639

640
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
233,607✔
641
  if (code) {
233,607✔
642
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
643
              tstrerror(code));
644
    TAOS_RETURN(code);
×
645
  }
646

647
  // handle entry
648
  code = metaHandleEntry2(pMeta, &entry);
233,607✔
649
  if (TSDB_CODE_SUCCESS == code) {
233,607✔
650
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
233,607✔
651
             version, pReq->name, pReq->uid);
652
  } else {
653
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
654
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
655
  }
656
  TAOS_RETURN(code);
233,607✔
657
#if 0
658
  metaTimeSeriesNotifyCheck(pMeta);
659
#endif
660
}
661

662
// Drop Normal Table
663

664
// Alter Normal Table
665

666
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
83,948,105✔
667
  int32_t code = TSDB_CODE_SUCCESS;
83,948,105✔
668
  if (TSDB_CHILD_TABLE == pReq->type) {
83,948,105✔
669
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
76,537,234✔
670
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
7,412,662✔
671
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
7,029,545✔
672
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
383,117✔
673
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
149,510✔
674
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
233,607✔
675
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
233,607✔
676
  } else {
677
    code = TSDB_CODE_INVALID_MSG;
×
678
  }
679
  TAOS_RETURN(code);
83,957,179✔
680
}
681

682
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,503,129✔
683
  int32_t code = TSDB_CODE_SUCCESS;
1,503,129✔
684

685
  // check request
686
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,503,129✔
687
  if (code) {
1,503,129✔
688
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
689
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
690
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
691
    }
692
    TAOS_RETURN(code);
×
693
  }
694

695
  if (pReq->suid == pReq->uid) {
1,503,129✔
696
    code = TSDB_CODE_INVALID_PARA;
×
697
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
698
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
699
    TAOS_RETURN(code);
×
700
  }
701

702
  SMetaEntry entry = {
1,503,129✔
703
      .version = version,
704
      .uid = pReq->uid,
1,503,129✔
705
  };
706

707
  if (pReq->isVirtual) {
1,503,129✔
708
    if (pReq->suid == 0) {
55,130✔
709
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
27,790✔
710
    } else {
711
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
27,340✔
712
    }
713
  } else {
714
    if (pReq->suid == 0) {
1,447,999✔
715
      entry.type = -TSDB_NORMAL_TABLE;
782,275✔
716
    } else {
717
      entry.type = -TSDB_CHILD_TABLE;
665,724✔
718
    }
719
  }
720
  code = metaHandleEntry2(pMeta, &entry);
1,503,129✔
721
  if (code) {
1,503,129✔
722
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
723
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
724
  } else {
725
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,503,129✔
726
             pReq->uid, version);
727
  }
728
  TAOS_RETURN(code);
1,503,129✔
729
}
730

731
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,327,049✔
732
  int32_t code = 0;
4,327,049✔
733

734
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,327,049✔
735
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
736
              __func__, __FILE__, __LINE__, pReq->colName, version);
737
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
738
  }
739

740
  // check name
741
  void   *value = NULL;
4,327,049✔
742
  int32_t valueSize = 0;
4,327,049✔
743
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,327,049✔
744
  if (code) {
4,327,049✔
745
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
746
              __FILE__, __LINE__, pReq->tbName, version);
747
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
748
    TAOS_RETURN(code);
×
749
  }
750
  int64_t uid = *(int64_t *)value;
4,327,049✔
751
  tdbFreeClear(value);
4,327,049✔
752

753
  // check table type
754
  SMetaInfo info;
4,327,049✔
755
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,327,049✔
756
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
757
              " not found, this is an internal error in meta, version:%" PRId64,
758
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
759
    code = TSDB_CODE_INTERNAL_ERROR;
×
760
    TAOS_RETURN(code);
×
761
  }
762
  if (info.suid != 0 && pReq->action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF &&
4,327,049✔
763
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
28,982✔
764
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
765
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
766
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
767
    TAOS_RETURN(code);
×
768
  }
769

770
  // check grant
771
  code = grantCheck(TSDB_GRANT_TIMESERIES);
4,327,049✔
772
  if (code) {
4,327,049✔
773
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
774
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
775
    TAOS_RETURN(code);
×
776
  }
777
  TAOS_RETURN(code);
4,327,049✔
778
}
779

780
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,562,018✔
781
  int32_t code = TSDB_CODE_SUCCESS;
3,562,018✔
782

783
  // check request
784
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,562,018✔
785
  if (code) {
3,562,018✔
786
    TAOS_RETURN(code);
×
787
  }
788

789
  // fetch old entry
790
  SMetaEntry *pEntry = NULL;
3,562,018✔
791
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,562,018✔
792
  if (code) {
3,562,018✔
793
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
794
              __FILE__, __LINE__, pReq->tbName, version);
795
    TAOS_RETURN(code);
×
796
  }
797
  if (pEntry->version >= version) {
3,562,018✔
798
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
799
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
800
    metaFetchEntryFree(&pEntry);
×
801
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
802
  }
803

804
  // do add column
805
  int32_t         rowSize = 0;
3,562,018✔
806
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,562,018✔
807
  SSchema        *pColumn;
808
  SExtSchema      extSchema = {0};
3,562,018✔
809
  pEntry->version = version;
3,562,018✔
810
  for (int32_t i = 0; i < pSchema->nCols; i++) {
2,147,483,647✔
811
    pColumn = &pSchema->pSchema[i];
2,147,483,647✔
812
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
2,147,483,647✔
813
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
316,695✔
814
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
815
      metaFetchEntryFree(&pEntry);
316,695✔
816
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
316,695✔
817
    }
818
    rowSize += pColumn->bytes;
2,147,483,647✔
819
  }
820

821
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
3,245,323✔
822
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,245,323✔
823
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,450✔
824
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
825
    metaFetchEntryFree(&pEntry);
6,450✔
826
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,450✔
827
  }
828

829
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,238,873✔
830
  if (pSchema->nCols + 1 > maxCols) {
3,238,873✔
831
    metaError("vgId:%d, %s failed at %s:%d since column count %d + 1 > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
832
              __func__, __FILE__, __LINE__, pSchema->nCols, maxCols, version);
833
    metaFetchEntryFree(&pEntry);
×
834
    TAOS_RETURN(TSDB_CODE_PAR_TOO_MANY_COLUMNS);
×
835
  }
836

837
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
3,238,873✔
838
  if (NULL == pNewSchema) {
3,238,873✔
839
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
840
              __LINE__, tstrerror(terrno), version);
841
    metaFetchEntryFree(&pEntry);
×
842
    TAOS_RETURN(terrno);
×
843
  }
844
  pSchema->pSchema = pNewSchema;
3,238,873✔
845
  pSchema->version++;
3,238,873✔
846
  pSchema->nCols++;
3,238,873✔
847
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,238,873✔
848
  pColumn->bytes = pReq->bytes;
3,238,873✔
849
  pColumn->type = pReq->type;
3,238,873✔
850
  pColumn->flags = pReq->flags;
3,238,873✔
851
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,238,873✔
852
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
536✔
853
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
854
              version);
855
    metaFetchEntryFree(&pEntry);
536✔
856
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
536✔
857
  }
858
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,238,337✔
859
  extSchema.typeMod = pReq->typeMod;
3,238,337✔
860
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,238,337✔
861
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,238,337✔
862
    SColRef tmpRef;
53,881✔
863
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
53,881✔
864
      tmpRef.hasRef = false;
30,848✔
865
      tmpRef.id = pColumn->colId;
30,848✔
866
    } else {
867
      tmpRef.hasRef = true;
23,033✔
868
      tmpRef.id = pColumn->colId;
23,033✔
869
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
23,033✔
870
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
23,033✔
871
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
23,033✔
872
    }
873
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
53,881✔
874
    if (code) {
53,881✔
875
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
876
                __LINE__, tstrerror(code), version);
877
      metaFetchEntryFree(&pEntry);
×
878
      TAOS_RETURN(code);
×
879
    }
880
  } else {
881
    uint32_t compress;
882
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
3,184,456✔
883
      compress = createDefaultColCmprByType(pColumn->type);
3,174,894✔
884
    } else {
885
      compress = pReq->compress;
9,562✔
886
    }
887
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
3,184,456✔
888
    if (code) {
3,184,456✔
889
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
890
                __LINE__, tstrerror(code), version);
891
      metaFetchEntryFree(&pEntry);
×
892
      TAOS_RETURN(code);
×
893
    }
894
  }
895
  code = addTableExtSchema(pEntry, pColumn, pSchema->nCols, &extSchema);
3,238,337✔
896
  if (code) {
3,238,337✔
897
    metaError("vgId:%d, %s failed to add ext schema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
898
              __func__, __FILE__, __LINE__, tstrerror(code), version);
899
    metaFetchEntryFree(&pEntry);
×
900
    TAOS_RETURN(code);
×
901
  }
902

903
  // do handle entry
904
  code = metaHandleEntry2(pMeta, pEntry);
3,238,337✔
905
  if (code) {
3,238,337✔
906
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
907
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
908
    metaFetchEntryFree(&pEntry);
×
909
    TAOS_RETURN(code);
×
910
  } else {
911
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,238,337✔
912
             pEntry->uid, version);
913
  }
914

915
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,238,337✔
916
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
53,881✔
917
                                pEntry->type);
53,881✔
918
    if (code) {
53,881✔
919
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
920
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
921
    } else {
922
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
17,959,945✔
923
        SColRef *p = &pEntry->colRef.pColRef[i];
17,906,064✔
924
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,906,064✔
925
        pRsp->pColRefs[i].id = p->id;
17,906,064✔
926
        if (p->hasRef) {
17,906,064✔
927
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
173,010✔
928
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
173,010✔
929
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
173,010✔
930
        }
931
      }
932
    }
933
  } else {
934
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,184,456✔
935
    if (code) {
3,184,456✔
936
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
937
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
938
    } else {
939
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
940
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
941
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
942
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
943
      }
944
    }
945
  }
946

947
  metaFetchEntryFree(&pEntry);
3,238,337✔
948
  TAOS_RETURN(code);
3,238,337✔
949
}
950

951
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
92,383✔
952
  int32_t code = TSDB_CODE_SUCCESS;
92,383✔
953

954
  // check request
955
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
92,383✔
956
  if (code) {
92,383✔
957
    TAOS_RETURN(code);
×
958
  }
959

960
  // fetch old entry
961
  SMetaEntry *pEntry = NULL;
92,383✔
962
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
92,383✔
963
  if (code) {
92,383✔
964
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
965
              __FILE__, __LINE__, pReq->tbName, version);
966
    TAOS_RETURN(code);
×
967
  }
968

969
  if (pEntry->version >= version) {
92,383✔
970
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
971
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
972
    metaFetchEntryFree(&pEntry);
×
973
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
974
  }
975

976
  // search the column to drop
977
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
92,383✔
978
  SSchema        *pColumn = NULL;
92,383✔
979
  SSchema         tColumn;
92,383✔
980
  int32_t         iColumn = 0;
92,383✔
981
  for (; iColumn < pSchema->nCols; iColumn++) {
65,287,651✔
982
    pColumn = &pSchema->pSchema[iColumn];
65,287,651✔
983
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
65,287,651✔
984
      break;
92,383✔
985
    }
986
  }
987

988
  if (iColumn == pSchema->nCols) {
92,383✔
989
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
990
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
991
    metaFetchEntryFree(&pEntry);
×
992
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
993
  }
994

995
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
92,383✔
996
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
997
              __func__, __FILE__, __LINE__, pReq->colName, version);
998
    metaFetchEntryFree(&pEntry);
×
999
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1000
  }
1001

1002
  tColumn = *pColumn;
92,383✔
1003

1004
  // do drop column
1005
  pEntry->version = version;
92,383✔
1006
  if (pSchema->nCols - iColumn - 1 > 0) {
92,383✔
1007
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
68,209✔
1008
  }
1009
  pSchema->nCols--;
92,383✔
1010
  pSchema->version++;
92,383✔
1011
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
92,383✔
1012
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
29,765✔
1013
    if (code) {
29,765✔
1014
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1015
                __LINE__, tstrerror(code), version);
1016
      metaFetchEntryFree(&pEntry);
×
1017
      TAOS_RETURN(code);
×
1018
    }
1019
    if (pEntry->colRef.nCols != pSchema->nCols) {
29,765✔
1020
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1021
                __func__, __FILE__, __LINE__, version);
1022
      metaFetchEntryFree(&pEntry);
×
1023
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1024
    }
1025
  } else {
1026
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
62,618✔
1027
    if (code) {
62,618✔
1028
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1029
                __LINE__, tstrerror(code), version);
1030
      metaFetchEntryFree(&pEntry);
×
1031
      TAOS_RETURN(code);
×
1032
    }
1033
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
62,618✔
1034
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1035
                __func__, __FILE__, __LINE__, version);
1036
      metaFetchEntryFree(&pEntry);
×
1037
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1038
    }
1039
  }
1040

1041
  // update column extschema
1042
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
92,383✔
1043
  if (code) {
92,383✔
1044
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1045
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1046
    metaFetchEntryFree(&pEntry);
×
1047
    TAOS_RETURN(code);
×
1048
  }
1049

1050
  // do handle entry
1051
  code = metaHandleEntry2(pMeta, pEntry);
92,383✔
1052
  if (code) {
92,383✔
1053
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1054
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1055
    metaFetchEntryFree(&pEntry);
×
1056
    TAOS_RETURN(code);
×
1057
  } else {
1058
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
92,383✔
1059
             pEntry->uid, version);
1060
  }
1061

1062
  // build response
1063
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
92,383✔
1064
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
29,765✔
1065
                                pEntry->type);
29,765✔
1066
    if (code) {
29,765✔
1067
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1068
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1069
    } else {
1070
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
35,315,405✔
1071
        SColRef *p = &pEntry->colRef.pColRef[i];
35,285,640✔
1072
        pRsp->pColRefs[i].hasRef = p->hasRef;
35,285,640✔
1073
        pRsp->pColRefs[i].id = p->id;
35,285,640✔
1074
        if (p->hasRef) {
35,285,640✔
1075
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
17,655,448✔
1076
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
17,655,448✔
1077
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
17,655,448✔
1078
        }
1079
      }
1080
    }
1081
  } else {
1082
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
62,618✔
1083
    if (code) {
62,618✔
1084
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1085
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1086
    } else {
1087
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
53,074,270✔
1088
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
53,011,652✔
1089
        pRsp->pSchemaExt[i].colId = p->id;
53,011,652✔
1090
        pRsp->pSchemaExt[i].compress = p->alg;
53,011,652✔
1091
      }
1092
    }
1093
  }
1094

1095
  metaFetchEntryFree(&pEntry);
92,383✔
1096
  TAOS_RETURN(code);
92,383✔
1097
}
1098

1099
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
45,409✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
45,409✔
1101

1102
  // check request
1103
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
45,409✔
1104
  if (code) {
45,409✔
1105
    TAOS_RETURN(code);
×
1106
  }
1107

1108
  if (NULL == pReq->colNewName) {
45,409✔
1109
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1110
              __func__, __FILE__, __LINE__, version);
1111
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1112
  }
1113

1114
  // fetch old entry
1115
  SMetaEntry *pEntry = NULL;
45,409✔
1116
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
45,409✔
1117
  if (code) {
45,409✔
1118
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1119
              __FILE__, __LINE__, pReq->tbName, version);
1120
    TAOS_RETURN(code);
×
1121
  }
1122

1123
  if (pEntry->version >= version) {
45,409✔
1124
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1125
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1126
    metaFetchEntryFree(&pEntry);
×
1127
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1128
  }
1129

1130
  // search the column to update
1131
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
45,409✔
1132
  SSchema        *pColumn = NULL;
45,409✔
1133
  int32_t         iColumn = 0;
45,409✔
1134
  for (int32_t i = 0; i < pSchema->nCols; i++) {
207,362✔
1135
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
207,362✔
1136
      pColumn = &pSchema->pSchema[i];
45,409✔
1137
      iColumn = i;
45,409✔
1138
      break;
45,409✔
1139
    }
1140
  }
1141

1142
  if (NULL == pColumn) {
45,409✔
1143
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1144
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1145
    metaFetchEntryFree(&pEntry);
×
1146
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1147
  }
1148

1149

1150
  // do update column name
1151
  pEntry->version = version;
45,409✔
1152
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
45,409✔
1153
  pSchema->version++;
45,409✔
1154

1155
  // do handle entry
1156
  code = metaHandleEntry2(pMeta, pEntry);
45,409✔
1157
  if (code) {
45,409✔
1158
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1159
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1160
    metaFetchEntryFree(&pEntry);
×
1161
    TAOS_RETURN(code);
×
1162
  } else {
1163
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
45,409✔
1164
             pEntry->uid, version);
1165
  }
1166

1167
  // build response
1168
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
45,409✔
1169
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
27,743✔
1170
                                pEntry->type);
27,743✔
1171
    if (code) {
27,743✔
1172
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1173
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1174
    } else {
1175
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
196,048✔
1176
        SColRef *p = &pEntry->colRef.pColRef[i];
168,305✔
1177
        pRsp->pColRefs[i].hasRef = p->hasRef;
168,305✔
1178
        pRsp->pColRefs[i].id = p->id;
168,305✔
1179
        if (p->hasRef) {
168,305✔
1180
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
103,853✔
1181
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
103,853✔
1182
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
103,853✔
1183
        }
1184
      }
1185
    }
1186
  } else {
1187
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
17,666✔
1188
    if (code) {
17,666✔
1189
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1190
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1191
    } else {
1192
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
185,142✔
1193
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
167,476✔
1194
        pRsp->pSchemaExt[i].colId = p->id;
167,476✔
1195
        pRsp->pSchemaExt[i].compress = p->alg;
167,476✔
1196
      }
1197
    }
1198
  }
1199

1200
  metaFetchEntryFree(&pEntry);
45,409✔
1201
  TAOS_RETURN(code);
45,409✔
1202
}
1203

1204
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
491,572✔
1205
  int32_t code = TSDB_CODE_SUCCESS;
491,572✔
1206

1207
  // check request
1208
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
491,572✔
1209
  if (code) {
491,572✔
1210
    TAOS_RETURN(code);
×
1211
  }
1212

1213
  // fetch old entry
1214
  SMetaEntry *pEntry = NULL;
491,572✔
1215
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
491,572✔
1216
  if (code) {
491,572✔
1217
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1218
              __FILE__, __LINE__, pReq->tbName, version);
1219
    TAOS_RETURN(code);
×
1220
  }
1221

1222
  if (pEntry->version >= version) {
491,572✔
1223
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1224
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1225
    metaFetchEntryFree(&pEntry);
×
1226
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1227
  }
1228

1229
  // search the column to update
1230
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
491,572✔
1231
  SSchema        *pColumn = NULL;
491,572✔
1232
  int32_t         iColumn = 0;
491,572✔
1233
  int32_t         rowSize = 0;
491,572✔
1234
  for (int32_t i = 0; i < pSchema->nCols; i++) {
36,855,076✔
1235
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
36,363,504✔
1236
      pColumn = &pSchema->pSchema[i];
491,572✔
1237
      iColumn = i;
491,572✔
1238
    }
1239
    rowSize += pSchema->pSchema[i].bytes;
36,363,504✔
1240
  }
1241

1242
  if (NULL == pColumn) {
491,572✔
1243
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1244
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1245
    metaFetchEntryFree(&pEntry);
×
1246
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1247
  }
1248

1249
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
491,572✔
1250
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
195,435✔
1251
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1252
              version);
1253
    metaFetchEntryFree(&pEntry);
195,435✔
1254
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
195,435✔
1255
  }
1256

1257
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
296,137✔
1258
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
296,137✔
1259
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
45,150✔
1260
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1261
              version);
1262
    metaFetchEntryFree(&pEntry);
45,150✔
1263
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
45,150✔
1264
  }
1265

1266
  // do change the column bytes
1267
  pEntry->version = version;
250,987✔
1268
  pSchema->version++;
250,987✔
1269
  pColumn->bytes = pReq->colModBytes;
250,987✔
1270

1271
  // do handle entry
1272
  code = metaHandleEntry2(pMeta, pEntry);
250,987✔
1273
  if (code) {
250,987✔
1274
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1275
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1276
    metaFetchEntryFree(&pEntry);
×
1277
    TAOS_RETURN(code);
×
1278
  } else {
1279
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
250,987✔
1280
             pEntry->uid, version);
1281
  }
1282

1283
  // build response
1284
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
250,987✔
1285
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
26,941✔
1286
                                pEntry->type);
26,941✔
1287
    if (code) {
26,941✔
1288
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1289
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1290
    } else {
1291
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
17,747,110✔
1292
        SColRef *p = &pEntry->colRef.pColRef[i];
17,720,169✔
1293
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,720,169✔
1294
        pRsp->pColRefs[i].id = p->id;
17,720,169✔
1295
        if (p->hasRef) {
17,720,169✔
1296
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
78,586✔
1297
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
78,586✔
1298
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
78,586✔
1299
        }
1300
      }
1301
    }
1302
  } else {
1303
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
224,046✔
1304
    if (code) {
224,046✔
1305
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1306
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1307
    } else {
1308
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,003,396✔
1309
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,779,350✔
1310
        pRsp->pSchemaExt[i].colId = p->id;
8,779,350✔
1311
        pRsp->pSchemaExt[i].compress = p->alg;
8,779,350✔
1312
      }
1313
    }
1314
  }
1315

1316
  metaFetchEntryFree(&pEntry);
250,987✔
1317
  TAOS_RETURN(code);
250,987✔
1318
}
1319

1320
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,752,735✔
1321
  int32_t code = 0;
7,752,735✔
1322

1323
  // check tag name
1324
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,752,735✔
1325
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1326
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1327
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1328
  }
1329

1330
  // check name
1331
  void   *value = NULL;
7,752,735✔
1332
  int32_t valueSize = 0;
7,752,735✔
1333
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,752,735✔
1334
  if (code) {
7,752,735✔
1335
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
271✔
1336
              __FILE__, __LINE__, pReq->tbName, version);
1337
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
271✔
1338
    TAOS_RETURN(code);
271✔
1339
  }
1340
  tdbFreeClear(value);
7,752,464✔
1341

1342
  TAOS_RETURN(code);
7,752,464✔
1343
}
1344

1345
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1346

1347
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,653,045✔
1348
  if (isNull) {
7,653,045✔
1349
    if (!tTagGet(pOldTag, &value)) {
47,384✔
1350
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
20,403✔
1351
      return true;
20,403✔
1352
    }
1353
    return false;
26,981✔
1354
  }
1355
  if (!tTagGet(pOldTag, &value)){
7,605,661✔
1356
    return false;
183,200✔
1357
  }
1358
  if (IS_VAR_DATA_TYPE(value.type)) {
7,422,461✔
1359
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
59,182✔
1360
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
37,583✔
1361
      return true;
37,583✔
1362
    }
1363
  } else {
1364
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,363,279✔
1365
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
91,145✔
1366
      return true;
91,145✔
1367
    }
1368
  }
1369
  return false;
7,293,733✔
1370
}
1371

1372
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,752,735✔
1373
  int32_t code = TSDB_CODE_SUCCESS;
7,752,735✔
1374

1375
  // check request
1376
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,752,735✔
1377
  if (code) {
7,752,735✔
1378
    TAOS_RETURN(code);
271✔
1379
  }
1380

1381
  // fetch child entry
1382
  SMetaEntry *pChild = NULL;
7,752,464✔
1383
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,752,464✔
1384
  if (code) {
7,752,464✔
1385
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1386
              __FILE__, __LINE__, pReq->tbName, version);
1387
    TAOS_RETURN(code);
×
1388
  }
1389

1390
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
7,752,464✔
1391
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1392
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1393
    metaFetchEntryFree(&pChild);
×
1394
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1395
  }
1396

1397
  // fetch super entry
1398
  SMetaEntry *pSuper = NULL;
7,752,464✔
1399
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,752,464✔
1400
  if (code) {
7,752,464✔
1401
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1402
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1403
    metaFetchEntryFree(&pChild);
×
1404
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1405
  }
1406

1407
  // search the tag to update
1408
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,752,464✔
1409
  SSchema        *pColumn = NULL;
7,752,464✔
1410
  int32_t         iColumn = 0;
7,752,464✔
1411
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,431,460✔
1412
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,431,460✔
1413
      pColumn = &pTagSchema->pSchema[i];
7,752,464✔
1414
      iColumn = i;
7,752,464✔
1415
      break;
7,752,464✔
1416
    }
1417
  }
1418

1419
  if (NULL == pColumn) {
7,752,464✔
1420
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1421
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1422
    metaFetchEntryFree(&pChild);
×
1423
    metaFetchEntryFree(&pSuper);
×
1424
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1425
  }
1426

1427
  // do change tag value
1428
  pChild->version = version;
7,752,464✔
1429
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,752,464✔
1430
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
120,069✔
1431
    if (NULL == pNewTag) {
120,069✔
1432
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1433
                __LINE__, tstrerror(terrno), version);
1434
      metaFetchEntryFree(&pChild);
×
1435
      metaFetchEntryFree(&pSuper);
×
1436
      TAOS_RETURN(terrno);
×
1437
    }
1438
    pChild->ctbEntry.pTags = pNewTag;
120,069✔
1439
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
120,069✔
1440
  } else {
1441
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,632,395✔
1442

1443
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,632,395✔
1444
    if (NULL == pTagArray) {
7,632,395✔
1445
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1446
                __LINE__, tstrerror(terrno), version);
1447
      metaFetchEntryFree(&pChild);
×
1448
      metaFetchEntryFree(&pSuper);
×
1449
      TAOS_RETURN(terrno);
×
1450
    }
1451

1452
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
16,223,774✔
1453
      STagVal value = {
8,731,200✔
1454
          .type = pTagSchema->pSchema[i].type,
8,731,200✔
1455
          .cid = pTagSchema->pSchema[i].colId,
8,731,200✔
1456
      };
1457

1458
      if (iColumn == i) {
8,731,200✔
1459
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,632,395✔
1460
          taosArrayDestroy(pTagArray);
139,821✔
1461
          metaFetchEntryFree(&pChild);
139,821✔
1462
          metaFetchEntryFree(&pSuper);
139,821✔
1463
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
139,821✔
1464
        }
1465
        if (pReq->isNull) {
7,492,574✔
1466
          continue;
24,986✔
1467
        }
1468
        if (IS_VAR_DATA_TYPE(value.type)) {
7,467,588✔
1469
          value.pData = pReq->pTagVal;
103,123✔
1470
          value.nData = pReq->nTagVal;
103,123✔
1471
        } else {
1472
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,364,465✔
1473
        }
1474
      } else if (!tTagGet(pOldTag, &value)) {
1,098,805✔
1475
        continue;
299,087✔
1476
      }
1477

1478
      if (NULL == taosArrayPush(pTagArray, &value)) {
8,267,306✔
1479
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1480
                  __LINE__, tstrerror(terrno), version);
1481
        taosArrayDestroy(pTagArray);
×
1482
        metaFetchEntryFree(&pChild);
×
1483
        metaFetchEntryFree(&pSuper);
×
1484
        TAOS_RETURN(terrno);
×
1485
      }
1486
    }
1487

1488
    STag *pNewTag = NULL;
7,492,574✔
1489
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,492,574✔
1490
    if (code) {
7,492,574✔
1491
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1492
                __LINE__, tstrerror(code), version);
1493
      taosArrayDestroy(pTagArray);
×
1494
      metaFetchEntryFree(&pChild);
×
1495
      metaFetchEntryFree(&pSuper);
×
1496
      TAOS_RETURN(code);
×
1497
    }
1498
    taosArrayDestroy(pTagArray);
7,492,574✔
1499
    taosMemoryFree(pChild->ctbEntry.pTags);
7,492,574✔
1500
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,492,574✔
1501
  }
1502

1503
  // do handle entry
1504
  code = metaHandleEntry2(pMeta, pChild);
7,612,643✔
1505
  if (code) {
7,612,643✔
1506
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1507
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1508
    metaFetchEntryFree(&pChild);
×
1509
    metaFetchEntryFree(&pSuper);
×
1510
    TAOS_RETURN(code);
×
1511
  } else {
1512
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
7,612,643✔
1513
             pChild->uid, version);
1514
  }
1515

1516
  // free resource and return
1517
  metaFetchEntryFree(&pChild);
7,612,643✔
1518
  metaFetchEntryFree(&pSuper);
7,612,643✔
1519
  TAOS_RETURN(code);
7,612,643✔
1520
}
1521

1522
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,675✔
1523
  int32_t code = 0;
3,675✔
1524

1525
  // check tag name
1526
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,675✔
1527
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1528
              __FILE__, __LINE__, version);
1529
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1530
  }
1531

1532
  // check name
1533
  void   *value = NULL;
3,675✔
1534
  int32_t valueSize = 0;
3,675✔
1535
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,675✔
1536
  if (code) {
3,675✔
1537
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1538
              __FILE__, __LINE__, pReq->tbName, version);
1539
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1540
    TAOS_RETURN(code);
×
1541
  }
1542
  tdbFreeClear(value);
3,675✔
1543

1544
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,675✔
1545
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1546
              __FILE__, __LINE__, version);
1547
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1548
  }
1549

1550
  TAOS_RETURN(code);
3,675✔
1551
}
1552

1553
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,675✔
1554
  int32_t code = TSDB_CODE_SUCCESS;
3,675✔
1555

1556
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,675✔
1557
  if (code) {
3,675✔
1558
    TAOS_RETURN(code);
×
1559
  }
1560

1561
  // fetch child entry
1562
  SMetaEntry *pChild = NULL;
3,675✔
1563
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,675✔
1564
  if (code) {
3,675✔
1565
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1566
              __FILE__, __LINE__, pReq->tbName, version);
1567
    TAOS_RETURN(code);
×
1568
  }
1569

1570
  if (pChild->type != TSDB_CHILD_TABLE) {
3,675✔
1571
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1572
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1573
    metaFetchEntryFree(&pChild);
×
1574
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1575
  }
1576

1577
  // fetch super entry
1578
  SMetaEntry *pSuper = NULL;
3,675✔
1579
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,675✔
1580
  if (code) {
3,675✔
1581
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1582
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1583
    metaFetchEntryFree(&pChild);
×
1584
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1585
  }
1586

1587
  // search the tags to update
1588
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,675✔
1589

1590
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,675✔
1591
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1592
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1593
    metaFetchEntryFree(&pChild);
×
1594
    metaFetchEntryFree(&pSuper);
×
1595
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1596
  }
1597

1598
  // do check if tag name exists
1599
  SHashObj *pTagTable =
1600
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
3,675✔
1601
  if (pTagTable == NULL) {
3,675✔
1602
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1603
              __LINE__, tstrerror(terrno), version);
1604
    metaFetchEntryFree(&pChild);
×
1605
    metaFetchEntryFree(&pSuper);
×
1606
    TAOS_RETURN(terrno);
×
1607
  }
1608

1609
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
24,325✔
1610
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
20,650✔
1611
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
20,650✔
1612
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1613
                __LINE__, tstrerror(terrno), version);
1614
      taosHashCleanup(pTagTable);
×
1615
      metaFetchEntryFree(&pChild);
×
1616
      metaFetchEntryFree(&pSuper);
×
1617
      TAOS_RETURN(terrno);
×
1618
    }
1619
  }
1620

1621
  int32_t numOfChangedTags = 0;
3,675✔
1622
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,650✔
1623
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
23,975✔
1624
        ? numOfChangedTags++
20,650✔
1625
        : 0;
23,975✔
1626
  }
1627
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,675✔
1628
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1629
              __FILE__, __LINE__, version);
1630
    taosHashCleanup(pTagTable);
×
1631
    metaFetchEntryFree(&pChild);
×
1632
    metaFetchEntryFree(&pSuper);
×
1633
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1634
  }
1635

1636
  // do change tag value
1637
  pChild->version = version;
3,675✔
1638
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,675✔
1639
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,675✔
1640
  if (NULL == pTagArray) {
3,675✔
1641
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1642
              __LINE__, tstrerror(terrno), version);
1643
    taosHashCleanup(pTagTable);
×
1644
    metaFetchEntryFree(&pChild);
×
1645
    metaFetchEntryFree(&pSuper);
×
1646
    TAOS_RETURN(terrno);
×
1647
  }
1648

1649
  bool allSame = true;
3,675✔
1650

1651
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,650✔
1652
    SSchema *pCol = &pTagSchema->pSchema[i];
23,975✔
1653
    STagVal  value = {
23,975✔
1654
         .cid = pCol->colId,
23,975✔
1655
    };
1656

1657
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
23,975✔
1658
    if (pTagVal == NULL) {
23,975✔
1659
      if (!tTagGet(pOldTag, &value)) {
3,325✔
1660
        continue;
×
1661
      }
1662
    } else {
1663
      value.type = pCol->type;
20,650✔
1664
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
20,650✔
1665
        allSame = false;
11,340✔
1666
      }
1667
      if (pTagVal->isNull) {
20,650✔
1668
        continue;
3,990✔
1669
      }
1670

1671
      if (IS_VAR_DATA_TYPE(pCol->type)) {
16,660✔
1672
        value.pData = pTagVal->pTagVal;
2,660✔
1673
        value.nData = pTagVal->nTagVal;
2,660✔
1674
      } else {
1675
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
14,000✔
1676
      }
1677
    }
1678

1679
    if (taosArrayPush(pTagArray, &value) == NULL) {
19,985✔
1680
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1681
                __LINE__, tstrerror(terrno), version);
1682
      taosHashCleanup(pTagTable);
×
1683
      taosArrayDestroy(pTagArray);
×
1684
      metaFetchEntryFree(&pChild);
×
1685
      metaFetchEntryFree(&pSuper);
×
1686
      TAOS_RETURN(terrno);
×
1687
    }
1688
  }
1689

1690
  if (allSame) {
3,675✔
1691
    metaWarn("vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
665✔
1692
             __LINE__, version);
1693
    taosHashCleanup(pTagTable);
665✔
1694
    taosArrayDestroy(pTagArray);
665✔
1695
    metaFetchEntryFree(&pChild);
665✔
1696
    metaFetchEntryFree(&pSuper);
665✔
1697
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
665✔
1698
  } 
1699
  STag *pNewTag = NULL;
3,010✔
1700
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
3,010✔
1701
  if (code) {
3,010✔
1702
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1703
              __LINE__, tstrerror(code), version);
1704
    taosHashCleanup(pTagTable);
×
1705
    taosArrayDestroy(pTagArray);
×
1706
    metaFetchEntryFree(&pChild);
×
1707
    metaFetchEntryFree(&pSuper);
×
1708
    TAOS_RETURN(code);
×
1709
  }
1710
  taosArrayDestroy(pTagArray);
3,010✔
1711
  taosMemoryFree(pChild->ctbEntry.pTags);
3,010✔
1712
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
3,010✔
1713

1714
  // do handle entry
1715
  code = metaHandleEntry2(pMeta, pChild);
3,010✔
1716
  if (code) {
3,010✔
1717
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1718
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1719
    taosHashCleanup(pTagTable);
×
1720
    metaFetchEntryFree(&pChild);
×
1721
    metaFetchEntryFree(&pSuper);
×
1722
    TAOS_RETURN(code);
×
1723
  } else {
1724
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,010✔
1725
             pChild->uid, version);
1726
  }
1727

1728
  taosHashCleanup(pTagTable);
3,010✔
1729
  metaFetchEntryFree(&pChild);
3,010✔
1730
  metaFetchEntryFree(&pSuper);
3,010✔
1731
  TAOS_RETURN(code);
3,010✔
1732
}
1733

1734
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
25,130✔
1735
  int32_t code = TSDB_CODE_SUCCESS;
25,130✔
1736

1737
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
25,130✔
1738
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1739
              __FILE__, __LINE__, version);
1740
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1741
  }
1742

1743
  return code;
25,130✔
1744
}
1745

1746
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
25,130✔
1747
  int32_t code = 0;
25,130✔
1748

1749
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
25,130✔
1750
  if (code) {
25,130✔
1751
    TAOS_RETURN(code);
×
1752
  }
1753

1754
  // fetch entry
1755
  SMetaEntry *pEntry = NULL;
25,130✔
1756
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
25,130✔
1757
  if (code) {
25,130✔
1758
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1759
              __FILE__, __LINE__, pReq->tbName, version);
1760
    TAOS_RETURN(code);
×
1761
  }
1762

1763
  // do change the entry
1764
  pEntry->version = version;
25,130✔
1765
  if (pEntry->type == TSDB_CHILD_TABLE) {
25,130✔
1766
    if (pReq->updateTTL) {
11,810✔
1767
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,569✔
1768
    }
1769
    if (pReq->newCommentLen >= 0) {
11,810✔
1770
      char *pNewComment = NULL;
7,241✔
1771
      if (pReq->newCommentLen) {
7,241✔
1772
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,621✔
1773
        if (NULL == pNewComment) {
4,621✔
1774
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1775
                    __LINE__, tstrerror(terrno), version);
1776
          metaFetchEntryFree(&pEntry);
×
1777
          TAOS_RETURN(terrno);
×
1778
        }
1779
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,621✔
1780
      } else {
1781
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,620✔
1782
      }
1783
      pEntry->ctbEntry.comment = pNewComment;
7,241✔
1784
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
7,241✔
1785
    }
1786
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
13,320✔
1787
    if (pReq->updateTTL) {
13,320✔
1788
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,703✔
1789
    }
1790
    if (pReq->newCommentLen >= 0) {
13,320✔
1791
      char *pNewComment = NULL;
8,617✔
1792
      if (pReq->newCommentLen > 0) {
8,617✔
1793
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
5,997✔
1794
        if (NULL == pNewComment) {
5,997✔
1795
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1796
                    __LINE__, tstrerror(terrno), version);
1797
          metaFetchEntryFree(&pEntry);
×
1798
          TAOS_RETURN(terrno);
×
1799
        }
1800
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
5,997✔
1801
      } else {
1802
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,620✔
1803
      }
1804
      pEntry->ntbEntry.comment = pNewComment;
8,617✔
1805
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,617✔
1806
    }
1807
  } else {
1808
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1809
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1810
    metaFetchEntryFree(&pEntry);
×
1811
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1812
  }
1813

1814
  // do handle entry
1815
  code = metaHandleEntry2(pMeta, pEntry);
25,130✔
1816
  if (code) {
25,130✔
1817
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1818
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1819
    metaFetchEntryFree(&pEntry);
×
1820
    TAOS_RETURN(code);
×
1821
  } else {
1822
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
25,130✔
1823
             pEntry->uid, version);
1824
  }
1825

1826
  metaFetchEntryFree(&pEntry);
25,130✔
1827
  TAOS_RETURN(code);
25,130✔
1828
}
1829

1830
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,045✔
1831
  int32_t code = TSDB_CODE_SUCCESS;
6,045✔
1832

1833
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,045✔
1834
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1835
              __FILE__, __LINE__, version);
1836
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1837
  }
1838

1839
  SMetaEntry *pEntry = NULL;
6,045✔
1840
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,045✔
1841
  if (code) {
6,045✔
1842
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1843
              __FILE__, __LINE__, pReq->tbName, version);
1844
    TAOS_RETURN(code);
×
1845
  }
1846

1847
  if (pEntry->version >= version) {
6,045✔
1848
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1849
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1850
    metaFetchEntryFree(&pEntry);
×
1851
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1852
  }
1853

1854
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,045✔
1855
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1856
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1857
    metaFetchEntryFree(&pEntry);
×
1858
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1859
  }
1860

1861
  // do change the entry
1862
  int8_t           updated = 0;
6,045✔
1863
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,045✔
1864
  for (int32_t i = 0; i < wp->nCols; i++) {
48,360✔
1865
    SColCmpr *p = &wp->pColCmpr[i];
42,315✔
1866
    if (p->id == pReq->colId) {
42,315✔
1867
      uint32_t dst = 0;
6,045✔
1868
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,045✔
1869
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1870
      if (updated > 0) {
6,045✔
1871
        p->alg = dst;
6,045✔
1872
      }
1873
    }
1874
  }
1875

1876
  if (updated == 0) {
6,045✔
1877
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1878
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1879
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1880
    metaFetchEntryFree(&pEntry);
×
1881
    TAOS_RETURN(code);
×
1882
  } else if (updated < 0) {
6,045✔
1883
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1884
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1885
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1886
    metaFetchEntryFree(&pEntry);
×
1887
    TAOS_RETURN(code);
×
1888
  }
1889

1890
  pEntry->version = version;
6,045✔
1891

1892
  // do handle entry
1893
  code = metaHandleEntry2(pMeta, pEntry);
6,045✔
1894
  if (code) {
6,045✔
1895
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1896
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1897
    metaFetchEntryFree(&pEntry);
×
1898
    TAOS_RETURN(code);
×
1899
  } else {
1900
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
6,045✔
1901
             pEntry->uid, version);
1902
  }
1903

1904
  metaFetchEntryFree(&pEntry);
6,045✔
1905
  TAOS_RETURN(code);
6,045✔
1906
}
1907

1908
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
77,529✔
1909
  int32_t code = TSDB_CODE_SUCCESS;
77,529✔
1910

1911
  // check request
1912
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
77,529✔
1913
  if (code) {
77,529✔
1914
    TAOS_RETURN(code);
×
1915
  }
1916

1917
  if (NULL == pReq->refDbName) {
77,529✔
1918
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1919
              __func__, __FILE__, __LINE__, version);
1920
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1921
  }
1922

1923
  if (NULL == pReq->refTbName) {
77,529✔
1924
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1925
              __func__, __FILE__, __LINE__, version);
1926
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1927
  }
1928

1929
  if (NULL == pReq->refColName) {
77,529✔
1930
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1931
              __func__, __FILE__, __LINE__, version);
1932
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1933
  }
1934

1935
  // fetch old entry
1936
  SMetaEntry *pEntry = NULL;
77,529✔
1937
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
77,529✔
1938
  if (code) {
77,529✔
1939
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1940
              __FILE__, __LINE__, pReq->tbName, version);
1941
    TAOS_RETURN(code);
×
1942
  }
1943

1944
  if (pEntry->version >= version) {
77,529✔
1945
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1946
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1947
    metaFetchEntryFree(&pEntry);
×
1948
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1949
  }
1950

1951
  // fetch super entry
1952
  SMetaEntry *pSuper = NULL;
77,529✔
1953
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
77,529✔
1954
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
28,877✔
1955
    if (code) {
28,877✔
1956
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1957
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1958
      metaFetchEntryFree(&pEntry);
×
1959
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1960
    }
1961
  }
1962

1963
  // search the column to update
1964
  SSchemaWrapper *pSchema =
77,529✔
1965
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
77,529✔
1966
  SColRef *pColRef = NULL;
77,529✔
1967
  int32_t  iColumn = 0;
77,529✔
1968
  for (int32_t i = 0; i < pSchema->nCols; i++) {
381,093✔
1969
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
381,093✔
1970
      pColRef = &pEntry->colRef.pColRef[i];
77,529✔
1971
      iColumn = i;
77,529✔
1972
      break;
77,529✔
1973
    }
1974
  }
1975

1976
  if (NULL == pColRef) {
77,529✔
1977
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1978
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1979
    metaFetchEntryFree(&pEntry);
×
1980
    metaFetchEntryFree(&pSuper);
×
1981
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1982
  }
1983

1984
  // do update column name
1985
  pEntry->version = version;
77,529✔
1986
  pColRef->hasRef = true;
77,529✔
1987
  pColRef->id = pSchema->pSchema[iColumn].colId;
77,529✔
1988
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
77,529✔
1989
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
77,529✔
1990
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
77,529✔
1991
  pSchema->version++;
77,529✔
1992
  pEntry->colRef.version++;
77,529✔
1993

1994
  // do handle entry
1995
  code = metaHandleEntry2(pMeta, pEntry);
77,529✔
1996
  if (code) {
77,529✔
1997
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1998
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1999
    metaFetchEntryFree(&pEntry);
×
2000
    metaFetchEntryFree(&pSuper);
×
2001
    TAOS_RETURN(code);
×
2002
  } else {
2003
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
77,529✔
2004
             pEntry->uid, version);
2005
  }
2006

2007
  // build response
2008
  code = metaUpdateVtbMetaRsp(
155,058✔
2009
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
77,529✔
2010
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
77,529✔
2011
      pEntry->type);
77,529✔
2012
  if (code) {
77,529✔
2013
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2014
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2015
  } else {
2016
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
572,921✔
2017
      SColRef *p = &pEntry->colRef.pColRef[i];
495,392✔
2018
      pRsp->pColRefs[i].hasRef = p->hasRef;
495,392✔
2019
      pRsp->pColRefs[i].id = p->id;
495,392✔
2020
      if (p->hasRef) {
495,392✔
2021
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
341,133✔
2022
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
341,133✔
2023
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
341,133✔
2024
      }
2025
    }
2026
  }
2027

2028
  metaFetchEntryFree(&pEntry);
77,529✔
2029
  metaFetchEntryFree(&pSuper);
77,529✔
2030
  TAOS_RETURN(code);
77,529✔
2031
}
2032

2033
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
58,138✔
2034
  int32_t code = TSDB_CODE_SUCCESS;
58,138✔
2035

2036
  // check request
2037
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
58,138✔
2038
  if (code) {
58,138✔
2039
    TAOS_RETURN(code);
×
2040
  }
2041

2042
  // fetch old entry
2043
  SMetaEntry *pEntry = NULL;
58,138✔
2044
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
58,138✔
2045
  if (code) {
58,138✔
2046
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2047
              __FILE__, __LINE__, pReq->tbName, version);
2048
    TAOS_RETURN(code);
×
2049
  }
2050

2051
  if (pEntry->version >= version) {
58,138✔
2052
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2053
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2054
    metaFetchEntryFree(&pEntry);
×
2055
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2056
  }
2057

2058
  // fetch super entry
2059
  SMetaEntry *pSuper = NULL;
58,138✔
2060
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
58,138✔
2061
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
28,982✔
2062
    if (code) {
28,982✔
2063
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2064
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2065
      metaFetchEntryFree(&pEntry);
×
2066
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2067
    }
2068
  }
2069

2070
  // search the column to update
2071
  SSchemaWrapper *pSchema =
58,138✔
2072
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
58,138✔
2073
  SColRef *pColRef = NULL;
58,138✔
2074
  int32_t  iColumn = 0;
58,138✔
2075
  for (int32_t i = 0; i < pSchema->nCols; i++) {
232,399✔
2076
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
232,399✔
2077
      pColRef = &pEntry->colRef.pColRef[i];
58,138✔
2078
      iColumn = i;
58,138✔
2079
      break;
58,138✔
2080
    }
2081
  }
2082

2083
  if (NULL == pColRef) {
58,138✔
2084
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2085
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2086
    metaFetchEntryFree(&pEntry);
×
2087
    metaFetchEntryFree(&pSuper);
×
2088
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2089
  }
2090

2091
  // do update column name
2092
  pEntry->version = version;
58,138✔
2093
  pColRef->hasRef = false;
58,138✔
2094
  pColRef->id = pSchema->pSchema[iColumn].colId;
58,138✔
2095
  pSchema->version++;
58,138✔
2096
  pEntry->colRef.version++;
58,138✔
2097

2098
  // do handle entry
2099
  code = metaHandleEntry2(pMeta, pEntry);
58,138✔
2100
  if (code) {
58,138✔
2101
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2102
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2103
    metaFetchEntryFree(&pEntry);
×
2104
    metaFetchEntryFree(&pSuper);
×
2105
    TAOS_RETURN(code);
×
2106
  } else {
2107
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
58,138✔
2108
             pEntry->uid, version);
2109
  }
2110

2111
  // build response
2112
  code = metaUpdateVtbMetaRsp(
116,276✔
2113
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
58,138✔
2114
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
58,138✔
2115
      pEntry->type);
58,138✔
2116
  if (code) {
58,138✔
2117
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2118
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2119
  } else {
2120
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
361,496✔
2121
      SColRef *p = &pEntry->colRef.pColRef[i];
303,358✔
2122
      pRsp->pColRefs[i].hasRef = p->hasRef;
303,358✔
2123
      pRsp->pColRefs[i].id = p->id;
303,358✔
2124
      if (p->hasRef) {
303,358✔
2125
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
152,096✔
2126
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
152,096✔
2127
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
152,096✔
2128
      }
2129
    }
2130
  }
2131

2132
  metaFetchEntryFree(&pEntry);
58,138✔
2133
  metaFetchEntryFree(&pSuper);
58,138✔
2134
  TAOS_RETURN(code);
58,138✔
2135
}
2136

2137
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
17,829✔
2138
  int32_t code = TSDB_CODE_SUCCESS;
17,829✔
2139

2140
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
17,829✔
2141
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2142
              __FILE__, __LINE__, version);
2143
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2144
  }
2145

2146
  SMetaEntry *pEntry = NULL;
17,829✔
2147
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
17,829✔
2148
  if (code) {
17,829✔
2149
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2150
              __FILE__, __LINE__, pReq->name, version);
2151
    TAOS_RETURN(code);
×
2152
  }
2153

2154
  if (pEntry->type != TSDB_SUPER_TABLE) {
17,829✔
2155
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2156
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2157
    metaFetchEntryFree(&pEntry);
×
2158
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2159
  }
2160

2161
  if (pEntry->uid != pReq->suid) {
17,829✔
2162
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2163
              ", version:%" PRId64,
2164
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2165
    metaFetchEntryFree(&pEntry);
×
2166
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2167
  }
2168

2169
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2170
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2171
  //   PRId64,
2172
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2173
  //             pReq->schemaTag.version, version);
2174
  //   metaFetchEntryFree(&pEntry);
2175
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2176
  // }
2177

2178
  // do change the entry
2179
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
17,829✔
2180
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
17,829✔
2181
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
17,829✔
2182
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2183
              __func__, __FILE__, __LINE__, pReq->name, version);
2184
    metaFetchEntryFree(&pEntry);
×
2185
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2186
  }
2187

2188
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
17,829✔
2189
    metaError(
×
2190
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2191
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2192
        version);
2193
    metaFetchEntryFree(&pEntry);
×
2194
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2195
  }
2196

2197
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2198
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2199
  //   PRId64,
2200
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2201
  //             pNewTagSchema->version, version);
2202
  //   metaFetchEntryFree(&pEntry);
2203
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2204
  // }
2205

2206
  int32_t numOfChangedTags = 0;
17,829✔
2207
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
192,043✔
2208
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
174,214✔
2209
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
174,214✔
2210

2211
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
174,214✔
2212
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
174,214✔
2213
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2214
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2215
      metaFetchEntryFree(&pEntry);
×
2216
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2217
    }
2218

2219
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
174,214✔
2220
      numOfChangedTags++;
17,829✔
2221
      SSCHMEA_SET_IDX_ON(pOldColumn);
17,829✔
2222
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
156,385✔
2223
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2224
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2225
      metaFetchEntryFree(&pEntry);
×
2226
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2227
    }
2228
  }
2229

2230
  if (numOfChangedTags != 1) {
17,829✔
2231
    metaError(
×
2232
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2233
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2234
    metaFetchEntryFree(&pEntry);
×
2235
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2236
  }
2237

2238
  pEntry->version = version;
17,829✔
2239
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
17,829✔
2240

2241
  // do handle the entry
2242
  code = metaHandleEntry2(pMeta, pEntry);
17,829✔
2243
  if (code) {
17,829✔
2244
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2245
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2246
    metaFetchEntryFree(&pEntry);
×
2247
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2248
  } else {
2249
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
17,829✔
2250
             pEntry->uid, version);
2251
  }
2252

2253
  metaFetchEntryFree(&pEntry);
17,829✔
2254
  TAOS_RETURN(code);
17,829✔
2255
}
2256

2257
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
14,512✔
2258
  int32_t code = TSDB_CODE_SUCCESS;
14,512✔
2259

2260
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
14,512✔
2261
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2262
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2263
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2264
  }
2265

2266
  SMetaEntry *pEntry = NULL;
14,512✔
2267
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
14,512✔
2268
  if (code) {
14,512✔
2269
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2270
              __FILE__, __LINE__, pReq->stb, version);
2271
    TAOS_RETURN(code);
×
2272
  }
2273

2274
  if (TSDB_SUPER_TABLE != pEntry->type) {
14,512✔
2275
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2276
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2277
    metaFetchEntryFree(&pEntry);
×
2278
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2279
  }
2280

2281
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
14,512✔
2282
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
14,512✔
2283
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2284
              __func__, __FILE__, __LINE__, pReq->stb, version);
2285
    metaFetchEntryFree(&pEntry);
×
2286
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2287
  }
2288

2289
  // search and set the tag index off
2290
  int32_t numOfChangedTags = 0;
14,512✔
2291
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
89,918✔
2292
    SSchema *pCol = pTagSchema->pSchema + i;
89,918✔
2293
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
89,918✔
2294
      if (!IS_IDX_ON(pCol)) {
14,512✔
2295
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2296
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2297
        metaFetchEntryFree(&pEntry);
×
2298
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2299
      }
2300
      numOfChangedTags++;
14,512✔
2301
      SSCHMEA_SET_IDX_OFF(pCol);
14,512✔
2302
      break;
14,512✔
2303
    }
2304
  }
2305

2306
  if (numOfChangedTags != 1) {
14,512✔
2307
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2308
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2309
    metaFetchEntryFree(&pEntry);
×
2310
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2311
  }
2312

2313
  // do handle the entry
2314
  pEntry->version = version;
14,512✔
2315
  pTagSchema->version++;
14,512✔
2316
  code = metaHandleEntry2(pMeta, pEntry);
14,512✔
2317
  if (code) {
14,512✔
2318
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2319
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2320
    metaFetchEntryFree(&pEntry);
×
2321
    TAOS_RETURN(code);
×
2322
  } else {
2323
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
14,512✔
2324
             pEntry->uid, version);
2325
  }
2326

2327
  metaFetchEntryFree(&pEntry);
14,512✔
2328
  TAOS_RETURN(code);
14,512✔
2329
}
2330

2331
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,487,117✔
2332
  int32_t code = TSDB_CODE_SUCCESS;
7,487,117✔
2333

2334
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,487,117✔
2335
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
7,862✔
2336
              __FILE__, __LINE__, version);
2337
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
7,862✔
2338
  }
2339

2340
  SMetaEntry *pEntry = NULL;
7,481,746✔
2341
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,485,616✔
2342
  if (code) {
7,480,923✔
2343
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2344
              __FILE__, __LINE__, pReq->name, version);
2345
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2346
  }
2347

2348
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,480,923✔
2349
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2350
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2351
    metaFetchEntryFree(&pEntry);
×
2352
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2353
  }
2354

2355
  SMetaEntry entry = {
22,408,451✔
2356
      .version = version,
2357
      .type = TSDB_SUPER_TABLE,
2358
      .uid = pReq->suid,
7,476,708✔
2359
      .name = pReq->name,
7,478,543✔
2360
      .stbEntry.schemaRow = pReq->schemaRow,
2361
      .stbEntry.schemaTag = pReq->schemaTag,
2362
      .stbEntry.keep = pReq->keep,
7,479,263✔
2363
      .stbEntry.ownerId = pReq->ownerId,
7,464,914✔
2364
      .colCmpr = pReq->colCmpr,
2365
      .pExtSchemas = pReq->pExtSchemas,
7,469,230✔
2366
  };
2367
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,477,301✔
2368
  if (pReq->virtualStb) {
7,477,301✔
2369
    TABLE_SET_VIRTUAL(entry.flags);
20,872✔
2370
  }
2371
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,462,492✔
2372
    TABLE_SET_ROLLUP(entry.flags);
4,374✔
2373
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,374✔
2374
  }
2375

2376
  // do handle the entry
2377
  code = metaHandleEntry2(pMeta, &entry);
7,465,923✔
2378
  if (code) {
7,476,953✔
2379
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2380
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2381
    metaFetchEntryFree(&pEntry);
×
2382
    TAOS_RETURN(code);
×
2383
  } else {
2384
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,476,953✔
2385
             pReq->suid, version);
2386
  }
2387

2388
  metaFetchEntryFree(&pEntry);
7,492,688✔
2389
  TAOS_RETURN(code);
7,492,839✔
2390
}
2391

2392
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2393
  int32_t code = 0;
×
2394

2395
  if (taosArrayGetSize(uidArray) == 0) {
×
2396
    return TSDB_CODE_SUCCESS;
×
2397
  }
2398

2399
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2400
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2401
    SMetaInfo info;
×
2402
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2403
    if (code) {
×
2404
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2405
                __func__, __FILE__, __LINE__, uid, code);
2406
      return code;
×
2407
    }
2408

2409
    SMetaEntry entry = {
×
2410
        .version = version,
2411
        .uid = uid,
2412
    };
2413

2414
    if (info.suid == 0) {
×
2415
      entry.type = -TSDB_NORMAL_TABLE;
×
2416
    } else if (info.suid == uid) {
×
2417
      entry.type = -TSDB_SUPER_TABLE;
×
2418
    } else {
2419
      entry.type = -TSDB_CHILD_TABLE;
×
2420
    }
2421
    code = metaHandleEntry2(pMeta, &entry);
×
2422
    if (code) {
×
2423
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2424
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2425
      return code;
×
2426
    }
2427
  }
2428
  return code;
×
2429
}
2430

2431
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
37,995✔
2432
  int32_t code = TSDB_CODE_SUCCESS;
37,995✔
2433

2434
  if (NULL == pReq->name || pReq->name[0] == 0) {
37,995✔
2435
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2436
              __LINE__, version);
2437
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2438
  }
2439

2440
  SMetaEntry *pEntry = NULL;
38,724✔
2441
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
38,724✔
2442
  if (code) {
38,724✔
2443
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2444
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2445
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2446
  }
2447

2448
  if (pEntry->type != TSDB_SUPER_TABLE) {
38,724✔
2449
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2450
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2451
    metaFetchEntryFree(&pEntry);
×
2452
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2453
  }
2454

2455
  if (pEntry->uid != pReq->tbUid) {
38,724✔
2456
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2457
              ", version:%" PRId64,
2458
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2459
    metaFetchEntryFree(&pEntry);
×
2460
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2461
  }
2462

2463
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
38,724✔
2464
    // overwrite the old rsma definition if exists
2465
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2466
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2467
  } else {
2468
    TABLE_SET_ROLLUP(pEntry->flags);
37,995✔
2469
  }
2470

2471
  SMetaEntry entry = *pEntry;
38,724✔
2472
  entry.version = version;
38,724✔
2473
  entry.stbEntry.rsmaParam.name = pReq->name;
38,724✔
2474
  entry.stbEntry.rsmaParam.uid = pReq->uid;
38,724✔
2475
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
38,724✔
2476
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
37,995✔
2477
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
37,995✔
2478
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
38,724✔
2479
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
37,995✔
2480
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
38,724✔
2481

2482
  // do handle the entry
2483
  code = metaHandleEntry2(pMeta, &entry);
37,995✔
2484
  if (code) {
38,724✔
2485
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2486
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2487
    metaFetchEntryFree(&pEntry);
×
2488
    TAOS_RETURN(code);
×
2489
  } else {
2490
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
38,724✔
2491
    pMeta->pVnode->config.isRsma = 1;
38,724✔
2492
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
38,724✔
2493
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2494
  }
2495

2496
  metaFetchEntryFree(&pEntry);
38,724✔
2497
  TAOS_RETURN(code);
38,724✔
2498
}
2499

2500
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
19,498✔
2501
  int32_t code = TSDB_CODE_SUCCESS;
19,498✔
2502

2503
  if (NULL == pReq->name || pReq->name[0] == 0) {
19,498✔
2504
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2505
              __LINE__, version);
2506
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2507
  }
2508

2509
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
19,498✔
2510
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2511
              __LINE__, version);
2512
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2513
  }
2514

2515
  SMetaEntry *pEntry = NULL;
19,498✔
2516
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
19,498✔
2517
  if (code) {
19,498✔
2518
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2519
             __func__, __LINE__, pReq->name, pReq->tbName, version);
2520
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2521
  }
2522

2523
  if (pEntry->type != pReq->tbType) {
19,498✔
2524
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2525
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2526
    metaFetchEntryFree(&pEntry);
×
2527
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2528
  }
2529

2530
  if (pEntry->uid != pReq->tbUid) {
19,498✔
2531
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2532
              ", version:%" PRId64,
2533
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2534
    metaFetchEntryFree(&pEntry);
×
2535
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2536
  }
2537

2538
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
19,498✔
2539
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
19,498✔
2540
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
19,498✔
UNCOV
2541
      metaError(
×
2542
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2543
          "uid:%" PRIi64 ", version:%" PRId64,
2544
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2545
          pReq->uid, version);
UNCOV
2546
      metaFetchEntryFree(&pEntry);
×
2547
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2548
    }
2549
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
19,498✔
2550
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
18,769✔
2551
  } else {
2552
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2553
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
2554
    metaFetchEntryFree(&pEntry);
×
2555
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2556
  }
2557

2558
  SMetaEntry entry = *pEntry;
18,769✔
2559
  entry.version = version;
19,498✔
2560
  TABLE_RESET_ROLLUP(entry.flags);
19,498✔
2561
  entry.stbEntry.rsmaParam.uid = 0;
19,498✔
2562
  entry.stbEntry.rsmaParam.name = NULL;
19,498✔
2563
  entry.stbEntry.rsmaParam.nFuncs = 0;
19,498✔
2564
  entry.stbEntry.rsmaParam.funcColIds = NULL;
19,498✔
2565
  entry.stbEntry.rsmaParam.funcIds = NULL;
19,498✔
2566

2567
  // do handle the entry
2568
  code = metaHandleEntry2(pMeta, &entry);
19,498✔
2569
  if (code) {
19,498✔
2570
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2571
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
2572
    metaFetchEntryFree(&pEntry);
×
2573
    TAOS_RETURN(code);
×
2574
  } else {
2575
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
19,498✔
2576
      pMeta->pVnode->config.isRsma = 0;
×
2577
    }
2578
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
19,498✔
2579
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2580
  }
2581

2582
  metaFetchEntryFree(&pEntry);
19,498✔
2583
  TAOS_RETURN(code);
19,498✔
2584
}
2585

2586
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,936✔
2587
  int32_t code = TSDB_CODE_SUCCESS;
11,936✔
2588

2589
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,936✔
2590
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2591
              __LINE__, version);
2592
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2593
  }
2594

2595
  SMetaEntry *pEntry = NULL;
11,936✔
2596
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,936✔
2597
  if (code) {
11,936✔
2598
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2599
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2600
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2601
  }
2602

2603
  if (pEntry->uid != pReq->tbUid) {
11,936✔
2604
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2605
              ", version:%" PRId64,
2606
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2607
    metaFetchEntryFree(&pEntry);
×
2608
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2609
  }
2610

2611
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
11,936✔
2612
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
11,936✔
2613
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
11,936✔
2614
  } else {
2615
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2616
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2617
    metaFetchEntryFree(&pEntry);
×
2618
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2619
  }
2620

2621
  SMetaEntry entry = *pEntry;
11,936✔
2622
  entry.version = version;
11,936✔
2623
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
11,936✔
2624
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
11,936✔
2625
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
11,936✔
2626
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
11,936✔
2627
  }
2628
  // do handle the entry
2629
  code = metaHandleEntry2(pMeta, &entry);
11,936✔
2630
  if (code) {
11,936✔
2631
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2632
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2633
    metaFetchEntryFree(&pEntry);
×
2634
    TAOS_RETURN(code);
×
2635
  } else {
2636
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
11,936✔
2637
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2638
  }
2639

2640
  metaFetchEntryFree(&pEntry);
11,936✔
2641
  TAOS_RETURN(code);
11,936✔
2642
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc