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

taosdata / TDengine / #4903

27 Dec 2025 02:36PM UTC coverage: 65.73% (+0.09%) from 65.642%
#4903

push

travis-ci

web-flow
fix: ci errors (#34079)

192966 of 293572 relevant lines covered (65.73%)

118504973.46 hits per line

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

67.83
/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, STableMetaRsp *pMetaRsp);
20
extern int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
21
                                    STableMetaRsp *pMetaRsp, int8_t tableType);
22
extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
23
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
24
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
25
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
26
extern int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema);
27
extern int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum);
28

29
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,506,489✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,506,489✔
31
  void     *value = NULL;
4,509,606✔
32
  int32_t   valueSize = 0;
4,510,433✔
33
  SMetaInfo info;
4,509,344✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,509,820✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
1,586✔
38
              pReq->name, version);
39
    return TSDB_CODE_INVALID_MSG;
×
40
  }
41

42
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
4,504,041✔
43
  if (r == 0) {  // name exists, check uid and type
4,498,943✔
44
    int64_t uid = *(tb_uid_t *)value;
2,840✔
45
    tdbFree(value);
2,840✔
46

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

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

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

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

78
  return TSDB_CODE_SUCCESS;
4,499,206✔
79
}
80

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

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

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

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

117
  return code;
1,307,443✔
118
}
119

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

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

132
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
848,193✔
133
  if (code) {
847,254✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
710✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
710✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
846,544✔
139
    tdbFreeClear(value);
847,428✔
140

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

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

163
// Create Super Table
164
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,491,180✔
165
  int32_t code = TSDB_CODE_SUCCESS;
4,491,180✔
166

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
4,491,180✔
169
  if (code != TSDB_CODE_SUCCESS) {
4,497,583✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
2,840✔
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
×
172
               pReq->name, pReq->suid, version);
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
×
174
    } else {
175
      TAOS_RETURN(code);
2,840✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
8,985,269✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,498,179✔
184
      .name = pReq->name,
4,495,696✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,493,610✔
188
  };
189
  if (pReq->rollup) {
4,487,650✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
4,494,031✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,487,701✔
195
    entry.colCmpr = pReq->colCmpr;
4,487,701✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
4,475,063✔
199

200
  if (pReq->virtualStb) {
4,493,775✔
201
    TABLE_SET_VIRTUAL(entry.flags);
41,756✔
202
  }
203

204
  code = metaHandleEntry2(pMeta, &entry);
4,483,533✔
205
  if (TSDB_CODE_SUCCESS == code) {
4,509,447✔
206
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,509,622✔
207
             pReq->suid, version);
208
  } else {
209
    metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
210
              pReq->suid, tstrerror(code));
211
  }
212
  TAOS_RETURN(code);
4,509,447✔
213
}
214

215
// Drop Super Table
216
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
848,741✔
217
  int32_t code = TSDB_CODE_SUCCESS;
848,741✔
218

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
848,741✔
221
  if (code) {
846,296✔
222
    TAOS_RETURN(code);
1,420✔
223
  }
224

225
  // handle entry
226
  SMetaEntry entry = {
844,876✔
227
      .version = verison,
228
      .type = -TSDB_SUPER_TABLE,
229
      .uid = pReq->suid,
845,344✔
230
  };
231
  code = metaHandleEntry2(pMeta, &entry);
844,683✔
232
  if (code) {
847,321✔
233
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
234
              tstrerror(code));
235
  } else {
236
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
847,321✔
237
             pReq->suid, verison);
238
  }
239
  TAOS_RETURN(code);
847,321✔
240
}
241

242
// Alter Super Table
243

244
// Create Child Table
245
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
50,954,982✔
246
  int32_t   code = TSDB_CODE_SUCCESS;
50,954,982✔
247
  void     *value = NULL;
50,954,982✔
248
  int32_t   valueSize = 0;
50,954,189✔
249
  SMetaInfo info;
50,956,345✔
250

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
50,956,670✔
252
      pReq->ctb.suid == 0) {
50,954,534✔
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
299✔
254
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
255
    return TSDB_CODE_INVALID_MSG;
×
256
  }
257

258
  // check table existence
259
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
50,956,265✔
260
    pReq->uid = *(int64_t *)value;
310,019✔
261
    tdbFreeClear(value);
310,019✔
262

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

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

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

286
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
308,303✔
287
  }
288

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
50,650,559✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
50,649,964✔
292
  if (code) {
50,638,804✔
293
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
294
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
295
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
296
  }
297

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

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

314
  // Check tag value
315
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
50,636,257✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
50,645,800✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
50,648,204✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
230,257,172✔
319
      STagVal tagVal = {
179,855,554✔
320
          .cid = pTagSchema->pSchema[i].colId,
179,840,680✔
321
      };
322

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

334
  metaFetchEntryFree(&pStbEntry);
50,634,853✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
50,621,239✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
50,633,742✔
339
    if (TSDB_CODE_SUCCESS != code) {
50,618,064✔
340
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
341
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
342
    }
343
  }
344
  return code;
50,616,140✔
345
}
346

347
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
50,479,707✔
348
  int32_t code = TSDB_CODE_SUCCESS;
50,479,707✔
349

350
  if (NULL == ppRsp) {
50,479,707✔
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
50,479,707✔
355
  if (NULL == ppRsp) {
50,464,030✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
50,464,030✔
360
  (*ppRsp)->tuid = pEntry->uid;
50,480,866✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
50,481,146✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
50,489,425✔
363

364
  return code;
50,499,686✔
365
}
366

367
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
50,809,368✔
368
  int32_t code = TSDB_CODE_SUCCESS;
50,809,368✔
369

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
50,809,368✔
372
  if (code) {
50,780,619✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
310,019✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,716✔
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
310,019✔
378
  }
379

380
  SMetaEntry entry = {
50,470,600✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
50,468,017✔
384
      .name = pReq->name,
50,471,612✔
385
      .ctbEntry.btime = pReq->btime,
50,459,245✔
386
      .ctbEntry.ttlDays = pReq->ttl,
50,456,412✔
387
      .ctbEntry.commentLen = pReq->commentLen,
50,484,038✔
388
      .ctbEntry.comment = pReq->comment,
50,468,282✔
389
      .ctbEntry.suid = pReq->ctb.suid,
50,459,081✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
50,457,122✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
50,482,683✔
395
  if (code) {
50,497,990✔
396
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
397
              tstrerror(code));
398
  }
399

400
  // handle entry
401
  code = metaHandleEntry2(pMeta, &entry);
50,497,990✔
402
  if (TSDB_CODE_SUCCESS == code) {
50,501,200✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
50,505,371✔
404
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
405
  } else {
406
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
407
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
408
              pReq->ctb.suid, version);
409
  }
410
  return code;
50,514,264✔
411
}
412

413
// Drop Child Table
414

415
// Alter Child Table
416

417
// Create Normal Table
418
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
13,136,902✔
419
  int32_t code = 0;
13,136,902✔
420
  void   *value = NULL;
13,136,902✔
421
  int32_t valueSize = 0;
13,136,902✔
422

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

429
  // check name
430
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
13,136,902✔
431
    // for auto create table, we return the uid of the existing table
432
    pReq->uid = *(tb_uid_t *)value;
28,430✔
433
    tdbFree(value);
28,430✔
434
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
28,430✔
435
  }
436

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

446
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
13,011,112✔
447
  int32_t code = TSDB_CODE_SUCCESS;
13,011,112✔
448

449
  if (NULL == ppRsp) {
13,011,112✔
450
    return code;
×
451
  }
452

453
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
13,011,112✔
454
  if (NULL == *ppRsp) {
13,011,112✔
455
    return terrno;
×
456
  }
457

458
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp);
13,011,112✔
459
  if (code) {
13,011,112✔
460
    taosMemoryFreeClear(*ppRsp);
×
461
    return code;
×
462
  }
463

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
78,990,931✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
65,979,819✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
65,979,819✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
65,979,819✔
468
    if (pEntry->pExtSchemas) {
65,979,819✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
412,681✔
470
    }
471
  }
472

473
  return code;
13,011,112✔
474
}
475

476
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
13,039,542✔
477
  int32_t code = TSDB_CODE_SUCCESS;
13,039,542✔
478

479
  // check request
480
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
13,039,542✔
481
  if (code) {
13,039,542✔
482
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
28,430✔
483
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
484
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
485
    }
486
    TAOS_RETURN(code);
28,430✔
487
  }
488

489
  SMetaEntry entry = {
26,021,920✔
490
      .version = version,
491
      .type = TSDB_NORMAL_TABLE,
492
      .uid = pReq->uid,
13,011,112✔
493
      .name = pReq->name,
13,011,112✔
494
      .ntbEntry.btime = pReq->btime,
13,011,112✔
495
      .ntbEntry.ttlDays = pReq->ttl,
13,011,112✔
496
      .ntbEntry.commentLen = pReq->commentLen,
13,011,112✔
497
      .ntbEntry.comment = pReq->comment,
13,011,112✔
498
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
499
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
13,011,112✔
500
      .colCmpr = pReq->colCmpr,
501
      .pExtSchemas = pReq->pExtSchemas,
13,011,112✔
502
  };
503
  TABLE_SET_COL_COMPRESSED(entry.flags);
13,011,112✔
504

505
  // build response
506
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
13,011,112✔
507
  if (code) {
13,011,112✔
508
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
509
              tstrerror(code));
510
  }
511

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

524
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
97,360✔
525
  int32_t code = TSDB_CODE_SUCCESS;
97,360✔
526

527
  if (NULL == ppRsp) {
97,360✔
528
    return code;
×
529
  }
530

531
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
97,360✔
532
  if (NULL == *ppRsp) {
97,360✔
533
    return terrno;
×
534
  }
535

536
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, *ppRsp,
97,360✔
537
                              TSDB_VIRTUAL_NORMAL_TABLE);
538
  if (code) {
97,360✔
539
    taosMemoryFreeClear(*ppRsp);
287✔
540
    return code;
287✔
541
  }
542

543
  return code;
97,073✔
544
}
545

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

557
  SMetaEntry entry = {.version = version,
194,720✔
558
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
559
                      .uid = pReq->uid,
97,360✔
560
                      .name = pReq->name,
97,360✔
561
                      .ntbEntry.btime = pReq->btime,
97,360✔
562
                      .ntbEntry.ttlDays = pReq->ttl,
97,360✔
563
                      .ntbEntry.commentLen = pReq->commentLen,
97,360✔
564
                      .ntbEntry.comment = pReq->comment,
97,360✔
565
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
566
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
97,360✔
567
                      .colRef = pReq->colRef};
568

569
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
97,360✔
570
  if (code) {
97,360✔
571
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
287✔
572
              tstrerror(code));
573
    TAOS_RETURN(code);
287✔
574
  }
575

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

591
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
147,187✔
592
  int32_t code = TSDB_CODE_SUCCESS;
147,187✔
593

594
  if (NULL == ppRsp) {
147,187✔
595
    return code;
×
596
  }
597

598
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
147,187✔
599
  if (NULL == *ppRsp) {
147,187✔
600
    return terrno;
×
601
  }
602

603
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
147,187✔
604
  if (code) {
147,187✔
605
    taosMemoryFreeClear(*ppRsp);
287✔
606
    return code;
287✔
607
  }
608
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
146,900✔
609

610
  return code;
146,900✔
611
}
612

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

624
  SMetaEntry entry = {.version = version,
294,374✔
625
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
626
                      .uid = pReq->uid,
147,187✔
627
                      .name = pReq->name,
147,187✔
628
                      .ctbEntry.btime = pReq->btime,
147,187✔
629
                      .ctbEntry.ttlDays = pReq->ttl,
147,187✔
630
                      .ctbEntry.commentLen = pReq->commentLen,
147,187✔
631
                      .ctbEntry.comment = pReq->comment,
147,187✔
632
                      .ctbEntry.suid = pReq->ctb.suid,
147,187✔
633
                      .ctbEntry.pTags = pReq->ctb.pTag,
147,187✔
634
                      .colRef = pReq->colRef};
635

636
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
147,187✔
637
  if (code) {
147,187✔
638
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
287✔
639
              tstrerror(code));
640
    TAOS_RETURN(code);
287✔
641
  }
642

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

658
// Drop Normal Table
659

660
// Alter Normal Table
661

662
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
64,090,313✔
663
  int32_t code = TSDB_CODE_SUCCESS;
64,090,313✔
664
  if (TSDB_CHILD_TABLE == pReq->type) {
64,090,313✔
665
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
50,818,814✔
666
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
13,284,089✔
667
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
13,039,542✔
668
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
244,547✔
669
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
97,360✔
670
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
147,187✔
671
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
147,187✔
672
  } else {
673
    code = TSDB_CODE_INVALID_MSG;
×
674
  }
675
  TAOS_RETURN(code);
64,106,931✔
676
}
677

678
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,307,443✔
679
  int32_t code = TSDB_CODE_SUCCESS;
1,307,443✔
680

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

691
  if (pReq->suid == pReq->uid) {
1,307,443✔
692
    code = TSDB_CODE_INVALID_PARA;
×
693
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
694
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
695
    TAOS_RETURN(code);
×
696
  }
697

698
  SMetaEntry entry = {
1,307,443✔
699
      .version = version,
700
      .uid = pReq->uid,
1,307,443✔
701
  };
702

703
  if (pReq->isVirtual) {
1,307,443✔
704
    if (pReq->suid == 0) {
50,206✔
705
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,341✔
706
    } else {
707
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
23,865✔
708
    }
709
  } else {
710
    if (pReq->suid == 0) {
1,257,237✔
711
      entry.type = -TSDB_NORMAL_TABLE;
722,904✔
712
    } else {
713
      entry.type = -TSDB_CHILD_TABLE;
534,333✔
714
    }
715
  }
716
  code = metaHandleEntry2(pMeta, &entry);
1,307,443✔
717
  if (code) {
1,307,443✔
718
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
719
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
720
  } else {
721
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,307,443✔
722
             pReq->uid, version);
723
  }
724
  TAOS_RETURN(code);
1,307,443✔
725
}
726

727
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,060,751✔
728
  int32_t code = 0;
4,060,751✔
729

730
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,060,751✔
731
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
732
              __func__, __FILE__, __LINE__, pReq->colName, version);
733
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
734
  }
735

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

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

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

776
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,351,657✔
777
  int32_t code = TSDB_CODE_SUCCESS;
3,351,657✔
778

779
  // check request
780
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,351,657✔
781
  if (code) {
3,351,657✔
782
    TAOS_RETURN(code);
×
783
  }
784

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

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

817
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
3,053,737✔
818
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,053,737✔
819
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,080✔
820
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
821
    metaFetchEntryFree(&pEntry);
6,080✔
822
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,080✔
823
  }
824

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

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

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

911
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,047,370✔
912
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
49,172✔
913
    if (code) {
49,172✔
914
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
915
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
916
    } else {
917
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
9,774,304✔
918
        SColRef *p = &pEntry->colRef.pColRef[i];
9,725,132✔
919
        pRsp->pColRefs[i].hasRef = p->hasRef;
9,725,132✔
920
        pRsp->pColRefs[i].id = p->id;
9,725,132✔
921
        if (p->hasRef) {
9,725,132✔
922
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
159,198✔
923
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
159,198✔
924
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
159,198✔
925
        }
926
      }
927
    }
928
  } else {
929
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
2,998,198✔
930
    if (code) {
2,998,198✔
931
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
932
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
933
    } else {
934
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
935
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
936
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
937
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
938
      }
939
    }
940
  }
941

942
  metaFetchEntryFree(&pEntry);
3,047,370✔
943
  TAOS_RETURN(code);
3,047,370✔
944
}
945

946
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
84,734✔
947
  int32_t code = TSDB_CODE_SUCCESS;
84,734✔
948

949
  // check request
950
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
84,734✔
951
  if (code) {
84,734✔
952
    TAOS_RETURN(code);
×
953
  }
954

955
  // fetch old entry
956
  SMetaEntry *pEntry = NULL;
84,734✔
957
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
84,734✔
958
  if (code) {
84,734✔
959
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
960
              __FILE__, __LINE__, pReq->tbName, version);
961
    TAOS_RETURN(code);
×
962
  }
963

964
  if (pEntry->version >= version) {
84,734✔
965
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
966
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
967
    metaFetchEntryFree(&pEntry);
×
968
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
969
  }
970

971
  // search the column to drop
972
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
84,734✔
973
  SSchema        *pColumn = NULL;
84,734✔
974
  SSchema         tColumn;
84,734✔
975
  int32_t         iColumn = 0;
84,734✔
976
  for (; iColumn < pSchema->nCols; iColumn++) {
56,847,368✔
977
    pColumn = &pSchema->pSchema[iColumn];
56,847,368✔
978
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
56,847,368✔
979
      break;
84,734✔
980
    }
981
  }
982

983
  if (iColumn == pSchema->nCols) {
84,734✔
984
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
985
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
986
    metaFetchEntryFree(&pEntry);
×
987
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
988
  }
989

990
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
84,734✔
991
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
992
              __func__, __FILE__, __LINE__, pReq->colName, version);
993
    metaFetchEntryFree(&pEntry);
×
994
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
995
  }
996

997
  tColumn = *pColumn;
84,734✔
998

999
  // do drop column
1000
  pEntry->version = version;
84,734✔
1001
  if (pSchema->nCols - iColumn - 1 > 0) {
84,734✔
1002
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
64,271✔
1003
  }
1004
  pSchema->nCols--;
84,734✔
1005
  pSchema->version++;
84,734✔
1006
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
84,734✔
1007
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
27,747✔
1008
    if (code) {
27,747✔
1009
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1010
                __LINE__, tstrerror(code), version);
1011
      metaFetchEntryFree(&pEntry);
×
1012
      TAOS_RETURN(code);
×
1013
    }
1014
    if (pEntry->colRef.nCols != pSchema->nCols) {
27,747✔
1015
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1016
                __func__, __FILE__, __LINE__, version);
1017
      metaFetchEntryFree(&pEntry);
×
1018
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1019
    }
1020
  } else {
1021
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
56,987✔
1022
    if (code) {
56,987✔
1023
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1024
                __LINE__, tstrerror(code), version);
1025
      metaFetchEntryFree(&pEntry);
×
1026
      TAOS_RETURN(code);
×
1027
    }
1028
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
56,987✔
1029
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1030
                __func__, __FILE__, __LINE__, version);
1031
      metaFetchEntryFree(&pEntry);
×
1032
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1033
    }
1034
  }
1035

1036
  // update column extschema
1037
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
84,734✔
1038
  if (code) {
84,734✔
1039
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1040
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1041
    metaFetchEntryFree(&pEntry);
×
1042
    TAOS_RETURN(code);
×
1043
  }
1044

1045
  // do handle entry
1046
  code = metaHandleEntry2(pMeta, pEntry);
84,734✔
1047
  if (code) {
84,734✔
1048
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1049
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1050
    metaFetchEntryFree(&pEntry);
×
1051
  } else {
1052
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
84,734✔
1053
             pEntry->uid, version);
1054
  }
1055

1056
  // build response
1057
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
84,734✔
1058
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
27,747✔
1059
    if (code) {
27,747✔
1060
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1061
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1062
    } else {
1063
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,989,854✔
1064
        SColRef *p = &pEntry->colRef.pColRef[i];
18,962,107✔
1065
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,962,107✔
1066
        pRsp->pColRefs[i].id = p->id;
18,962,107✔
1067
        if (p->hasRef) {
18,962,107✔
1068
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
9,492,041✔
1069
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
9,492,041✔
1070
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
9,492,041✔
1071
        }
1072
      }
1073
    }
1074
  } else {
1075
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
56,987✔
1076
    if (code) {
56,987✔
1077
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1078
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1079
    } else {
1080
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
60,335,346✔
1081
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
60,278,359✔
1082
        pRsp->pSchemaExt[i].colId = p->id;
60,278,359✔
1083
        pRsp->pSchemaExt[i].compress = p->alg;
60,278,359✔
1084
      }
1085
    }
1086
  }
1087

1088
  metaFetchEntryFree(&pEntry);
84,734✔
1089
  TAOS_RETURN(code);
84,734✔
1090
}
1091

1092
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
42,326✔
1093
  int32_t code = TSDB_CODE_SUCCESS;
42,326✔
1094

1095
  // check request
1096
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
42,326✔
1097
  if (code) {
42,326✔
1098
    TAOS_RETURN(code);
×
1099
  }
1100

1101
  if (NULL == pReq->colNewName) {
42,326✔
1102
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1103
              __func__, __FILE__, __LINE__, version);
1104
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1105
  }
1106

1107
  // fetch old entry
1108
  SMetaEntry *pEntry = NULL;
42,326✔
1109
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
42,326✔
1110
  if (code) {
42,326✔
1111
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1112
              __FILE__, __LINE__, pReq->tbName, version);
1113
    TAOS_RETURN(code);
×
1114
  }
1115

1116
  if (pEntry->version >= version) {
42,326✔
1117
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1118
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1119
    metaFetchEntryFree(&pEntry);
×
1120
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1121
  }
1122

1123
  // search the column to update
1124
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
42,326✔
1125
  SSchema        *pColumn = NULL;
42,326✔
1126
  int32_t         iColumn = 0;
42,326✔
1127
  for (int32_t i = 0; i < pSchema->nCols; i++) {
193,762✔
1128
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
193,762✔
1129
      pColumn = &pSchema->pSchema[i];
42,326✔
1130
      iColumn = i;
42,326✔
1131
      break;
42,326✔
1132
    }
1133
  }
1134

1135
  if (NULL == pColumn) {
42,326✔
1136
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1137
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1138
    metaFetchEntryFree(&pEntry);
×
1139
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1140
  }
1141

1142

1143
  // do update column name
1144
  pEntry->version = version;
42,326✔
1145
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
42,326✔
1146
  pSchema->version++;
42,326✔
1147

1148
  // do handle entry
1149
  code = metaHandleEntry2(pMeta, pEntry);
42,326✔
1150
  if (code) {
42,326✔
1151
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1152
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1153
    metaFetchEntryFree(&pEntry);
×
1154
    TAOS_RETURN(code);
×
1155
  } else {
1156
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
42,326✔
1157
             pEntry->uid, version);
1158
  }
1159

1160
  // build response
1161
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
42,326✔
1162
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,197✔
1163
    if (code) {
26,197✔
1164
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1165
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1166
    } else {
1167
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
185,364✔
1168
        SColRef *p = &pEntry->colRef.pColRef[i];
159,167✔
1169
        pRsp->pColRefs[i].hasRef = p->hasRef;
159,167✔
1170
        pRsp->pColRefs[i].id = p->id;
159,167✔
1171
        if (p->hasRef) {
159,167✔
1172
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
98,143✔
1173
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
98,143✔
1174
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
98,143✔
1175
        }
1176
      }
1177
    }
1178
  } else {
1179
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,129✔
1180
    if (code) {
16,129✔
1181
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1182
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1183
    } else {
1184
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
171,632✔
1185
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
155,503✔
1186
        pRsp->pSchemaExt[i].colId = p->id;
155,503✔
1187
        pRsp->pSchemaExt[i].compress = p->alg;
155,503✔
1188
      }
1189
    }
1190
  }
1191

1192
  metaFetchEntryFree(&pEntry);
42,326✔
1193
  TAOS_RETURN(code);
42,326✔
1194
}
1195

1196
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
462,310✔
1197
  int32_t code = TSDB_CODE_SUCCESS;
462,310✔
1198

1199
  // check request
1200
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
462,310✔
1201
  if (code) {
462,310✔
1202
    TAOS_RETURN(code);
×
1203
  }
1204

1205
  // fetch old entry
1206
  SMetaEntry *pEntry = NULL;
462,310✔
1207
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
462,310✔
1208
  if (code) {
462,310✔
1209
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1210
              __FILE__, __LINE__, pReq->tbName, version);
1211
    TAOS_RETURN(code);
×
1212
  }
1213

1214
  if (pEntry->version >= version) {
462,310✔
1215
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1216
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1217
    metaFetchEntryFree(&pEntry);
×
1218
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1219
  }
1220

1221
  // search the column to update
1222
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
462,310✔
1223
  SSchema        *pColumn = NULL;
462,310✔
1224
  int32_t         iColumn = 0;
462,310✔
1225
  int32_t         rowSize = 0;
462,310✔
1226
  for (int32_t i = 0; i < pSchema->nCols; i++) {
27,565,309✔
1227
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
27,102,999✔
1228
      pColumn = &pSchema->pSchema[i];
462,310✔
1229
      iColumn = i;
462,310✔
1230
    }
1231
    rowSize += pSchema->pSchema[i].bytes;
27,102,999✔
1232
  }
1233

1234
  if (NULL == pColumn) {
462,310✔
1235
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1236
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1237
    metaFetchEntryFree(&pEntry);
×
1238
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1239
  }
1240

1241
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
462,310✔
1242
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
183,616✔
1243
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1244
              version);
1245
    metaFetchEntryFree(&pEntry);
183,616✔
1246
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
183,616✔
1247
  }
1248

1249
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
278,694✔
1250
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
278,694✔
1251
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
42,560✔
1252
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1253
              version);
1254
    metaFetchEntryFree(&pEntry);
42,560✔
1255
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
42,560✔
1256
  }
1257

1258
  // do change the column bytes
1259
  pEntry->version = version;
236,134✔
1260
  pSchema->version++;
236,134✔
1261
  pColumn->bytes = pReq->colModBytes;
236,134✔
1262

1263
  // do handle entry
1264
  code = metaHandleEntry2(pMeta, pEntry);
236,134✔
1265
  if (code) {
236,134✔
1266
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1267
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1268
    metaFetchEntryFree(&pEntry);
×
1269
    TAOS_RETURN(code);
×
1270
  } else {
1271
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
236,134✔
1272
             pEntry->uid, version);
1273
  }
1274

1275
  // build response
1276
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
236,134✔
1277
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,338✔
1278
    if (code) {
25,338✔
1279
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1280
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1281
    } else {
1282
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
9,580,893✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
9,555,555✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
9,555,555✔
1285
        pRsp->pColRefs[i].id = p->id;
9,555,555✔
1286
        if (p->hasRef) {
9,555,555✔
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
74,542✔
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
74,542✔
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
74,542✔
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
210,796✔
1295
    if (code) {
210,796✔
1296
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1297
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1298
    } else {
1299
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,485,024✔
1300
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,274,228✔
1301
        pRsp->pSchemaExt[i].colId = p->id;
8,274,228✔
1302
        pRsp->pSchemaExt[i].compress = p->alg;
8,274,228✔
1303
      }
1304
    }
1305
  }
1306

1307
  metaFetchEntryFree(&pEntry);
236,134✔
1308
  TAOS_RETURN(code);
236,134✔
1309
}
1310

1311
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,404,533✔
1312
  int32_t code = 0;
7,404,533✔
1313

1314
  // check tag name
1315
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,404,533✔
1316
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1317
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1318
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1319
  }
1320

1321
  // check name
1322
  void   *value = NULL;
7,404,533✔
1323
  int32_t valueSize = 0;
7,404,533✔
1324
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,404,533✔
1325
  if (code) {
7,404,533✔
1326
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
243✔
1327
              __FILE__, __LINE__, pReq->tbName, version);
1328
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
243✔
1329
    TAOS_RETURN(code);
243✔
1330
  }
1331
  tdbFreeClear(value);
7,404,290✔
1332

1333
  TAOS_RETURN(code);
7,404,290✔
1334
}
1335

1336
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1337

1338
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,310,091✔
1339
  if (isNull) {
7,310,091✔
1340
    if (!tTagGet(pOldTag, &value)) {
44,748✔
1341
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,416✔
1342
      return true;
19,416✔
1343
    }
1344
    return false;
25,332✔
1345
  }
1346
  if (!tTagGet(pOldTag, &value)){
7,265,343✔
1347
    return false;
171,832✔
1348
  }
1349
  if (IS_VAR_DATA_TYPE(value.type)) {
7,093,511✔
1350
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
56,024✔
1351
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
35,819✔
1352
      return true;
35,819✔
1353
    }
1354
  } else {
1355
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,037,487✔
1356
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
87,352✔
1357
      return true;
87,352✔
1358
    }
1359
  }
1360
  return false;
6,970,340✔
1361
}
1362

1363
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,404,533✔
1364
  int32_t code = TSDB_CODE_SUCCESS;
7,404,533✔
1365

1366
  // check request
1367
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,404,533✔
1368
  if (code) {
7,404,533✔
1369
    TAOS_RETURN(code);
243✔
1370
  }
1371

1372
  // fetch child entry
1373
  SMetaEntry *pChild = NULL;
7,404,290✔
1374
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,404,290✔
1375
  if (code) {
7,404,290✔
1376
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1377
              __FILE__, __LINE__, pReq->tbName, version);
1378
    TAOS_RETURN(code);
×
1379
  }
1380

1381
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
7,404,290✔
1382
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1383
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1384
    metaFetchEntryFree(&pChild);
×
1385
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1386
  }
1387

1388
  // fetch super entry
1389
  SMetaEntry *pSuper = NULL;
7,404,290✔
1390
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,404,290✔
1391
  if (code) {
7,404,290✔
1392
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1393
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1394
    metaFetchEntryFree(&pChild);
×
1395
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1396
  }
1397

1398
  // search the tag to update
1399
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,404,290✔
1400
  SSchema        *pColumn = NULL;
7,404,290✔
1401
  int32_t         iColumn = 0;
7,404,290✔
1402
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,040,772✔
1403
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,040,772✔
1404
      pColumn = &pTagSchema->pSchema[i];
7,404,290✔
1405
      iColumn = i;
7,404,290✔
1406
      break;
7,404,290✔
1407
    }
1408
  }
1409

1410
  if (NULL == pColumn) {
7,404,290✔
1411
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1412
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1413
    metaFetchEntryFree(&pChild);
×
1414
    metaFetchEntryFree(&pSuper);
×
1415
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1416
  }
1417

1418
  // do change tag value
1419
  pChild->version = version;
7,404,290✔
1420
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,404,290✔
1421
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
112,919✔
1422
    if (NULL == pNewTag) {
112,919✔
1423
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1424
                __LINE__, tstrerror(terrno), version);
1425
      metaFetchEntryFree(&pChild);
×
1426
      metaFetchEntryFree(&pSuper);
×
1427
      TAOS_RETURN(terrno);
×
1428
    }
1429
    pChild->ctbEntry.pTags = pNewTag;
112,919✔
1430
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
112,919✔
1431
  } else {
1432
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,291,371✔
1433

1434
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,291,371✔
1435
    if (NULL == pTagArray) {
7,291,371✔
1436
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1437
                __LINE__, tstrerror(terrno), version);
1438
      metaFetchEntryFree(&pChild);
×
1439
      metaFetchEntryFree(&pSuper);
×
1440
      TAOS_RETURN(terrno);
×
1441
    }
1442

1443
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,476,995✔
1444
      STagVal value = {
8,319,475✔
1445
          .type = pTagSchema->pSchema[i].type,
8,319,475✔
1446
          .cid = pTagSchema->pSchema[i].colId,
8,319,475✔
1447
      };
1448

1449
      if (iColumn == i) {
8,319,475✔
1450
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,291,371✔
1451
          taosArrayDestroy(pTagArray);
133,851✔
1452
          metaFetchEntryFree(&pChild);
133,851✔
1453
          metaFetchEntryFree(&pSuper);
133,851✔
1454
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
133,851✔
1455
        }
1456
        if (pReq->isNull) {
7,157,520✔
1457
          continue;
23,460✔
1458
        }
1459
        if (IS_VAR_DATA_TYPE(value.type)) {
7,134,060✔
1460
          value.pData = pReq->pTagVal;
96,650✔
1461
          value.nData = pReq->nTagVal;
96,650✔
1462
        } else {
1463
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,037,410✔
1464
        }
1465
      } else if (!tTagGet(pOldTag, &value)) {
1,028,104✔
1466
        continue;
281,212✔
1467
      }
1468

1469
      if (NULL == taosArrayPush(pTagArray, &value)) {
7,880,952✔
1470
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1471
                  __LINE__, tstrerror(terrno), version);
1472
        taosArrayDestroy(pTagArray);
×
1473
        metaFetchEntryFree(&pChild);
×
1474
        metaFetchEntryFree(&pSuper);
×
1475
        TAOS_RETURN(terrno);
×
1476
      }
1477
    }
1478

1479
    STag *pNewTag = NULL;
7,157,520✔
1480
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,157,520✔
1481
    if (code) {
7,157,520✔
1482
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1483
                __LINE__, tstrerror(code), version);
1484
      taosArrayDestroy(pTagArray);
×
1485
      metaFetchEntryFree(&pChild);
×
1486
      metaFetchEntryFree(&pSuper);
×
1487
      TAOS_RETURN(code);
×
1488
    }
1489
    taosArrayDestroy(pTagArray);
7,157,520✔
1490
    taosMemoryFree(pChild->ctbEntry.pTags);
7,157,520✔
1491
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,157,520✔
1492
  }
1493

1494
  // do handle entry
1495
  code = metaHandleEntry2(pMeta, pChild);
7,270,439✔
1496
  if (code) {
7,270,439✔
1497
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1498
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1499
    metaFetchEntryFree(&pChild);
×
1500
    metaFetchEntryFree(&pSuper);
×
1501
    TAOS_RETURN(code);
×
1502
  } else {
1503
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
7,270,439✔
1504
             pChild->uid, version);
1505
  }
1506

1507
  // free resource and return
1508
  metaFetchEntryFree(&pChild);
7,270,439✔
1509
  metaFetchEntryFree(&pSuper);
7,270,439✔
1510
  TAOS_RETURN(code);
7,270,439✔
1511
}
1512

1513
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,120✔
1514
  int32_t code = 0;
3,120✔
1515

1516
  // check tag name
1517
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,120✔
1518
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1519
              __FILE__, __LINE__, version);
1520
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1521
  }
1522

1523
  // check name
1524
  void   *value = NULL;
3,120✔
1525
  int32_t valueSize = 0;
3,120✔
1526
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,120✔
1527
  if (code) {
3,120✔
1528
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1529
              __FILE__, __LINE__, pReq->tbName, version);
1530
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1531
    TAOS_RETURN(code);
×
1532
  }
1533
  tdbFreeClear(value);
3,120✔
1534

1535
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,120✔
1536
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1537
              __FILE__, __LINE__, version);
1538
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1539
  }
1540

1541
  TAOS_RETURN(code);
3,120✔
1542
}
1543

1544
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,120✔
1545
  int32_t code = TSDB_CODE_SUCCESS;
3,120✔
1546

1547
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,120✔
1548
  if (code) {
3,120✔
1549
    TAOS_RETURN(code);
×
1550
  }
1551

1552
  // fetch child entry
1553
  SMetaEntry *pChild = NULL;
3,120✔
1554
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,120✔
1555
  if (code) {
3,120✔
1556
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1557
              __FILE__, __LINE__, pReq->tbName, version);
1558
    TAOS_RETURN(code);
×
1559
  }
1560

1561
  if (pChild->type != TSDB_CHILD_TABLE) {
3,120✔
1562
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1563
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1564
    metaFetchEntryFree(&pChild);
×
1565
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1566
  }
1567

1568
  // fetch super entry
1569
  SMetaEntry *pSuper = NULL;
3,120✔
1570
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,120✔
1571
  if (code) {
3,120✔
1572
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1573
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1574
    metaFetchEntryFree(&pChild);
×
1575
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1576
  }
1577

1578
  // search the tags to update
1579
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,120✔
1580

1581
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,120✔
1582
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1583
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1584
    metaFetchEntryFree(&pChild);
×
1585
    metaFetchEntryFree(&pSuper);
×
1586
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1587
  }
1588

1589
  // do check if tag name exists
1590
  SHashObj *pTagTable =
1591
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
3,120✔
1592
  if (pTagTable == NULL) {
3,120✔
1593
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1594
              __LINE__, tstrerror(terrno), version);
1595
    metaFetchEntryFree(&pChild);
×
1596
    metaFetchEntryFree(&pSuper);
×
1597
    TAOS_RETURN(terrno);
×
1598
  }
1599

1600
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
21,840✔
1601
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
18,720✔
1602
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
18,720✔
1603
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1604
                __LINE__, tstrerror(terrno), version);
1605
      taosHashCleanup(pTagTable);
×
1606
      metaFetchEntryFree(&pChild);
×
1607
      metaFetchEntryFree(&pSuper);
×
1608
      TAOS_RETURN(terrno);
×
1609
    }
1610
  }
1611

1612
  int32_t numOfChangedTags = 0;
3,120✔
1613
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
24,960✔
1614
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
21,840✔
1615
        ? numOfChangedTags++
18,720✔
1616
        : 0;
21,840✔
1617
  }
1618
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,120✔
1619
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1620
              __FILE__, __LINE__, version);
1621
    taosHashCleanup(pTagTable);
×
1622
    metaFetchEntryFree(&pChild);
×
1623
    metaFetchEntryFree(&pSuper);
×
1624
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1625
  }
1626

1627
  // do change tag value
1628
  pChild->version = version;
3,120✔
1629
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,120✔
1630
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,120✔
1631
  if (NULL == pTagArray) {
3,120✔
1632
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1633
              __LINE__, tstrerror(terrno), version);
1634
    taosHashCleanup(pTagTable);
×
1635
    metaFetchEntryFree(&pChild);
×
1636
    metaFetchEntryFree(&pSuper);
×
1637
    TAOS_RETURN(terrno);
×
1638
  }
1639

1640
  bool allSame = true;
3,120✔
1641

1642
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
24,960✔
1643
    SSchema *pCol = &pTagSchema->pSchema[i];
21,840✔
1644
    STagVal  value = {
21,840✔
1645
         .cid = pCol->colId,
21,840✔
1646
    };
1647

1648
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
21,840✔
1649
    if (pTagVal == NULL) {
21,840✔
1650
      if (!tTagGet(pOldTag, &value)) {
3,120✔
1651
        continue;
×
1652
      }
1653
    } else {
1654
      value.type = pCol->type;
18,720✔
1655
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
18,720✔
1656
        allSame = false;
9,984✔
1657
      }
1658
      if (pTagVal->isNull) {
18,720✔
1659
        continue;
3,744✔
1660
      }
1661

1662
      if (IS_VAR_DATA_TYPE(pCol->type)) {
14,976✔
1663
        value.pData = pTagVal->pTagVal;
2,496✔
1664
        value.nData = pTagVal->nTagVal;
2,496✔
1665
      } else {
1666
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
12,480✔
1667
      }
1668
    }
1669

1670
    if (taosArrayPush(pTagArray, &value) == NULL) {
18,096✔
1671
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1672
                __LINE__, tstrerror(terrno), version);
1673
      taosHashCleanup(pTagTable);
×
1674
      taosArrayDestroy(pTagArray);
×
1675
      metaFetchEntryFree(&pChild);
×
1676
      metaFetchEntryFree(&pSuper);
×
1677
      TAOS_RETURN(terrno);
×
1678
    }
1679
  }
1680

1681
  if (allSame) {
3,120✔
1682
    metaWarn("vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
624✔
1683
             __LINE__, version);
1684
    taosHashCleanup(pTagTable);
624✔
1685
    taosArrayDestroy(pTagArray);
624✔
1686
    metaFetchEntryFree(&pChild);
624✔
1687
    metaFetchEntryFree(&pSuper);
624✔
1688
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
624✔
1689
  } 
1690
  STag *pNewTag = NULL;
2,496✔
1691
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
2,496✔
1692
  if (code) {
2,496✔
1693
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1694
              __LINE__, tstrerror(code), version);
1695
    taosHashCleanup(pTagTable);
×
1696
    taosArrayDestroy(pTagArray);
×
1697
    metaFetchEntryFree(&pChild);
×
1698
    metaFetchEntryFree(&pSuper);
×
1699
    TAOS_RETURN(code);
×
1700
  }
1701
  taosArrayDestroy(pTagArray);
2,496✔
1702
  taosMemoryFree(pChild->ctbEntry.pTags);
2,496✔
1703
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
2,496✔
1704

1705
  // do handle entry
1706
  code = metaHandleEntry2(pMeta, pChild);
2,496✔
1707
  if (code) {
2,496✔
1708
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1709
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1710
    taosHashCleanup(pTagTable);
×
1711
    metaFetchEntryFree(&pChild);
×
1712
    metaFetchEntryFree(&pSuper);
×
1713
    TAOS_RETURN(code);
×
1714
  } else {
1715
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
2,496✔
1716
             pChild->uid, version);
1717
  }
1718

1719
  taosHashCleanup(pTagTable);
2,496✔
1720
  metaFetchEntryFree(&pChild);
2,496✔
1721
  metaFetchEntryFree(&pSuper);
2,496✔
1722
  TAOS_RETURN(code);
2,496✔
1723
}
1724

1725
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,562✔
1726
  int32_t code = TSDB_CODE_SUCCESS;
23,562✔
1727

1728
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
23,562✔
1729
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1730
              __FILE__, __LINE__, version);
1731
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1732
  }
1733

1734
  return code;
23,562✔
1735
}
1736

1737
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,562✔
1738
  int32_t code = 0;
23,562✔
1739

1740
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
23,562✔
1741
  if (code) {
23,562✔
1742
    TAOS_RETURN(code);
×
1743
  }
1744

1745
  // fetch entry
1746
  SMetaEntry *pEntry = NULL;
23,562✔
1747
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,562✔
1748
  if (code) {
23,562✔
1749
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1750
              __FILE__, __LINE__, pReq->tbName, version);
1751
    TAOS_RETURN(code);
×
1752
  }
1753

1754
  // do change the entry
1755
  pEntry->version = version;
23,562✔
1756
  if (pEntry->type == TSDB_CHILD_TABLE) {
23,562✔
1757
    if (pReq->updateTTL) {
11,125✔
1758
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,303✔
1759
    }
1760
    if (pReq->newCommentLen >= 0) {
11,125✔
1761
      char *pNewComment = NULL;
6,822✔
1762
      if (pReq->newCommentLen) {
6,822✔
1763
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,347✔
1764
        if (NULL == pNewComment) {
4,347✔
1765
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1766
                    __LINE__, tstrerror(terrno), version);
1767
          metaFetchEntryFree(&pEntry);
×
1768
          TAOS_RETURN(terrno);
×
1769
        }
1770
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,347✔
1771
      } else {
1772
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,475✔
1773
      }
1774
      pEntry->ctbEntry.comment = pNewComment;
6,822✔
1775
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
6,822✔
1776
    }
1777
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
12,437✔
1778
    if (pReq->updateTTL) {
12,437✔
1779
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,433✔
1780
    }
1781
    if (pReq->newCommentLen >= 0) {
12,437✔
1782
      char *pNewComment = NULL;
8,004✔
1783
      if (pReq->newCommentLen > 0) {
8,004✔
1784
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
5,529✔
1785
        if (NULL == pNewComment) {
5,529✔
1786
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1787
                    __LINE__, tstrerror(terrno), version);
1788
          metaFetchEntryFree(&pEntry);
×
1789
          TAOS_RETURN(terrno);
×
1790
        }
1791
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
5,529✔
1792
      } else {
1793
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,475✔
1794
      }
1795
      pEntry->ntbEntry.comment = pNewComment;
8,004✔
1796
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,004✔
1797
    }
1798
  } else {
1799
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1800
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1801
    metaFetchEntryFree(&pEntry);
×
1802
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1803
  }
1804

1805
  // do handle entry
1806
  code = metaHandleEntry2(pMeta, pEntry);
23,562✔
1807
  if (code) {
23,562✔
1808
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1809
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1810
    metaFetchEntryFree(&pEntry);
×
1811
    TAOS_RETURN(code);
×
1812
  } else {
1813
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
23,562✔
1814
             pEntry->uid, version);
1815
  }
1816

1817
  metaFetchEntryFree(&pEntry);
23,562✔
1818
  TAOS_RETURN(code);
23,562✔
1819
}
1820

1821
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5,479✔
1822
  int32_t code = TSDB_CODE_SUCCESS;
5,479✔
1823

1824
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5,479✔
1825
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1826
              __FILE__, __LINE__, version);
1827
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1828
  }
1829

1830
  SMetaEntry *pEntry = NULL;
5,479✔
1831
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5,479✔
1832
  if (code) {
5,479✔
1833
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1834
              __FILE__, __LINE__, pReq->tbName, version);
1835
    TAOS_RETURN(code);
×
1836
  }
1837

1838
  if (pEntry->version >= version) {
5,479✔
1839
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1840
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1841
    metaFetchEntryFree(&pEntry);
×
1842
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1843
  }
1844

1845
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
5,479✔
1846
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1847
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1848
    metaFetchEntryFree(&pEntry);
×
1849
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1850
  }
1851

1852
  // do change the entry
1853
  int8_t           updated = 0;
5,479✔
1854
  SColCmprWrapper *wp = &pEntry->colCmpr;
5,479✔
1855
  for (int32_t i = 0; i < wp->nCols; i++) {
43,832✔
1856
    SColCmpr *p = &wp->pColCmpr[i];
38,353✔
1857
    if (p->id == pReq->colId) {
38,353✔
1858
      uint32_t dst = 0;
5,479✔
1859
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5,479✔
1860
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1861
      if (updated > 0) {
5,479✔
1862
        p->alg = dst;
5,479✔
1863
      }
1864
    }
1865
  }
1866

1867
  if (updated == 0) {
5,479✔
1868
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1869
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1870
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1871
    metaFetchEntryFree(&pEntry);
×
1872
    TAOS_RETURN(code);
×
1873
  } else if (updated < 0) {
5,479✔
1874
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1875
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1876
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1877
    metaFetchEntryFree(&pEntry);
×
1878
    TAOS_RETURN(code);
×
1879
  }
1880

1881
  pEntry->version = version;
5,479✔
1882

1883
  // do handle entry
1884
  code = metaHandleEntry2(pMeta, pEntry);
5,479✔
1885
  if (code) {
5,479✔
1886
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1887
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1888
    metaFetchEntryFree(&pEntry);
×
1889
    TAOS_RETURN(code);
×
1890
  } else {
1891
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5,479✔
1892
             pEntry->uid, version);
1893
  }
1894

1895
  metaFetchEntryFree(&pEntry);
5,479✔
1896
  TAOS_RETURN(code);
5,479✔
1897
}
1898

1899
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
69,288✔
1900
  int32_t code = TSDB_CODE_SUCCESS;
69,288✔
1901

1902
  // check request
1903
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
69,288✔
1904
  if (code) {
69,288✔
1905
    TAOS_RETURN(code);
×
1906
  }
1907

1908
  if (NULL == pReq->refDbName) {
69,288✔
1909
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1910
              __func__, __FILE__, __LINE__, version);
1911
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1912
  }
1913

1914
  if (NULL == pReq->refTbName) {
69,288✔
1915
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1916
              __func__, __FILE__, __LINE__, version);
1917
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1918
  }
1919

1920
  if (NULL == pReq->refColName) {
69,288✔
1921
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1922
              __func__, __FILE__, __LINE__, version);
1923
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1924
  }
1925

1926
  // fetch old entry
1927
  SMetaEntry *pEntry = NULL;
69,288✔
1928
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
69,288✔
1929
  if (code) {
69,288✔
1930
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1931
              __FILE__, __LINE__, pReq->tbName, version);
1932
    TAOS_RETURN(code);
×
1933
  }
1934

1935
  if (pEntry->version >= version) {
69,288✔
1936
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1937
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1938
    metaFetchEntryFree(&pEntry);
×
1939
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1940
  }
1941

1942
  // fetch super entry
1943
  SMetaEntry *pSuper = NULL;
69,288✔
1944
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
69,288✔
1945
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
23,931✔
1946
    if (code) {
23,931✔
1947
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1948
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1949
      metaFetchEntryFree(&pEntry);
×
1950
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1951
    }
1952
  }
1953

1954
  // search the column to update
1955
  SSchemaWrapper *pSchema =
69,288✔
1956
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
69,288✔
1957
  SColRef *pColRef = NULL;
69,288✔
1958
  int32_t  iColumn = 0;
69,288✔
1959
  for (int32_t i = 0; i < pSchema->nCols; i++) {
346,535✔
1960
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
346,535✔
1961
      pColRef = &pEntry->colRef.pColRef[i];
69,288✔
1962
      iColumn = i;
69,288✔
1963
      break;
69,288✔
1964
    }
1965
  }
1966

1967
  if (NULL == pColRef) {
69,288✔
1968
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1969
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1970
    metaFetchEntryFree(&pEntry);
×
1971
    metaFetchEntryFree(&pSuper);
×
1972
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1973
  }
1974

1975
  // do update column name
1976
  pEntry->version = version;
69,288✔
1977
  pColRef->hasRef = true;
69,288✔
1978
  pColRef->id = pSchema->pSchema[iColumn].colId;
69,288✔
1979
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
69,288✔
1980
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
69,288✔
1981
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
69,288✔
1982
  pSchema->version++;
69,288✔
1983
  pEntry->colRef.version++;
69,288✔
1984

1985
  // do handle entry
1986
  code = metaHandleEntry2(pMeta, pEntry);
69,288✔
1987
  if (code) {
69,288✔
1988
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1989
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1990
    metaFetchEntryFree(&pEntry);
×
1991
    metaFetchEntryFree(&pSuper);
×
1992
    TAOS_RETURN(code);
×
1993
  } else {
1994
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
69,288✔
1995
             pEntry->uid, version);
1996
  }
1997

1998
  // build response
1999
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
69,288✔
2000
  if (code) {
69,288✔
2001
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2002
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2003
  } else {
2004
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
523,482✔
2005
      SColRef *p = &pEntry->colRef.pColRef[i];
454,194✔
2006
      pRsp->pColRefs[i].hasRef = p->hasRef;
454,194✔
2007
      pRsp->pColRefs[i].id = p->id;
454,194✔
2008
      if (p->hasRef) {
454,194✔
2009
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
313,686✔
2010
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
313,686✔
2011
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
313,686✔
2012
      }
2013
    }
2014
  }
2015

2016
  metaFetchEntryFree(&pEntry);
69,288✔
2017
  metaFetchEntryFree(&pSuper);
69,288✔
2018
  TAOS_RETURN(code);
69,288✔
2019
}
2020

2021
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
50,436✔
2022
  int32_t code = TSDB_CODE_SUCCESS;
50,436✔
2023

2024
  // check request
2025
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
50,436✔
2026
  if (code) {
50,436✔
2027
    TAOS_RETURN(code);
×
2028
  }
2029

2030
  // fetch old entry
2031
  SMetaEntry *pEntry = NULL;
50,436✔
2032
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
50,436✔
2033
  if (code) {
50,436✔
2034
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2035
              __FILE__, __LINE__, pReq->tbName, version);
2036
    TAOS_RETURN(code);
×
2037
  }
2038

2039
  if (pEntry->version >= version) {
50,436✔
2040
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2041
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2042
    metaFetchEntryFree(&pEntry);
×
2043
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2044
  }
2045

2046
  // fetch super entry
2047
  SMetaEntry *pSuper = NULL;
50,436✔
2048
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
50,436✔
2049
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
24,390✔
2050
    if (code) {
24,390✔
2051
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2052
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2053
      metaFetchEntryFree(&pEntry);
×
2054
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2055
    }
2056
  }
2057

2058
  // search the column to update
2059
  SSchemaWrapper *pSchema =
50,436✔
2060
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
50,436✔
2061
  SColRef *pColRef = NULL;
50,436✔
2062
  int32_t  iColumn = 0;
50,436✔
2063
  for (int32_t i = 0; i < pSchema->nCols; i++) {
207,485✔
2064
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
207,485✔
2065
      pColRef = &pEntry->colRef.pColRef[i];
50,436✔
2066
      iColumn = i;
50,436✔
2067
      break;
50,436✔
2068
    }
2069
  }
2070

2071
  if (NULL == pColRef) {
50,436✔
2072
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2073
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2074
    metaFetchEntryFree(&pEntry);
×
2075
    metaFetchEntryFree(&pSuper);
×
2076
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2077
  }
2078

2079
  // do update column name
2080
  pEntry->version = version;
50,436✔
2081
  pColRef->hasRef = false;
50,436✔
2082
  pColRef->id = pSchema->pSchema[iColumn].colId;
50,436✔
2083
  pSchema->version++;
50,436✔
2084
  pEntry->colRef.version++;
50,436✔
2085

2086
  // do handle entry
2087
  code = metaHandleEntry2(pMeta, pEntry);
50,436✔
2088
  if (code) {
50,436✔
2089
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2090
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2091
    metaFetchEntryFree(&pEntry);
×
2092
    metaFetchEntryFree(&pSuper);
×
2093
    TAOS_RETURN(code);
×
2094
  } else {
2095
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
50,436✔
2096
             pEntry->uid, version);
2097
  }
2098

2099
  // build response
2100
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
50,436✔
2101
  if (code) {
50,436✔
2102
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2103
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2104
  } else {
2105
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
314,098✔
2106
      SColRef *p = &pEntry->colRef.pColRef[i];
263,662✔
2107
      pRsp->pColRefs[i].hasRef = p->hasRef;
263,662✔
2108
      pRsp->pColRefs[i].id = p->id;
263,662✔
2109
      if (p->hasRef) {
263,662✔
2110
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
131,664✔
2111
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
131,664✔
2112
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
131,664✔
2113
      }
2114
    }
2115
  }
2116

2117
  metaFetchEntryFree(&pEntry);
50,436✔
2118
  metaFetchEntryFree(&pSuper);
50,436✔
2119
  TAOS_RETURN(code);
50,436✔
2120
}
2121

2122
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,203✔
2123
  int32_t code = TSDB_CODE_SUCCESS;
4,203✔
2124

2125
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,203✔
2126
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2127
              __FILE__, __LINE__, version);
2128
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2129
  }
2130

2131
  SMetaEntry *pEntry = NULL;
4,203✔
2132
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
4,203✔
2133
  if (code) {
4,203✔
2134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2135
              __FILE__, __LINE__, pReq->name, version);
2136
    TAOS_RETURN(code);
×
2137
  }
2138

2139
  if (pEntry->type != TSDB_SUPER_TABLE) {
4,203✔
2140
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2141
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2142
    metaFetchEntryFree(&pEntry);
×
2143
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2144
  }
2145

2146
  if (pEntry->uid != pReq->suid) {
4,203✔
2147
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2148
              ", version:%" PRId64,
2149
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2150
    metaFetchEntryFree(&pEntry);
×
2151
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2152
  }
2153

2154
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2155
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2156
  //   PRId64,
2157
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2158
  //             pReq->schemaTag.version, version);
2159
  //   metaFetchEntryFree(&pEntry);
2160
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2161
  // }
2162

2163
  // do change the entry
2164
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
4,203✔
2165
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
4,203✔
2166
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,203✔
2167
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2168
              __func__, __FILE__, __LINE__, pReq->name, version);
2169
    metaFetchEntryFree(&pEntry);
×
2170
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2171
  }
2172

2173
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
4,203✔
2174
    metaError(
×
2175
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2176
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2177
        version);
2178
    metaFetchEntryFree(&pEntry);
×
2179
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2180
  }
2181

2182
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2183
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2184
  //   PRId64,
2185
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2186
  //             pNewTagSchema->version, version);
2187
  //   metaFetchEntryFree(&pEntry);
2188
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2189
  // }
2190

2191
  int32_t numOfChangedTags = 0;
4,203✔
2192
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
21,442✔
2193
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,239✔
2194
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,239✔
2195

2196
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
17,239✔
2197
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
17,239✔
2198
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2199
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2200
      metaFetchEntryFree(&pEntry);
×
2201
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2202
    }
2203

2204
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
17,239✔
2205
      numOfChangedTags++;
4,203✔
2206
      SSCHMEA_SET_IDX_ON(pOldColumn);
4,203✔
2207
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
13,036✔
2208
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2209
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2210
      metaFetchEntryFree(&pEntry);
×
2211
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2212
    }
2213
  }
2214

2215
  if (numOfChangedTags != 1) {
4,203✔
2216
    metaError(
×
2217
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2218
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2219
    metaFetchEntryFree(&pEntry);
×
2220
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2221
  }
2222

2223
  pEntry->version = version;
4,203✔
2224
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,203✔
2225

2226
  // do handle the entry
2227
  code = metaHandleEntry2(pMeta, pEntry);
4,203✔
2228
  if (code) {
4,203✔
2229
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2230
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2231
    metaFetchEntryFree(&pEntry);
×
2232
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2233
  } else {
2234
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,203✔
2235
             pEntry->uid, version);
2236
  }
2237

2238
  metaFetchEntryFree(&pEntry);
4,203✔
2239
  TAOS_RETURN(code);
4,203✔
2240
}
2241

2242
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,774✔
2243
  int32_t code = TSDB_CODE_SUCCESS;
2,774✔
2244

2245
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,774✔
2246
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2247
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2248
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2249
  }
2250

2251
  SMetaEntry *pEntry = NULL;
2,774✔
2252
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,774✔
2253
  if (code) {
2,774✔
2254
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2255
              __FILE__, __LINE__, pReq->stb, version);
2256
    TAOS_RETURN(code);
×
2257
  }
2258

2259
  if (TSDB_SUPER_TABLE != pEntry->type) {
2,774✔
2260
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2261
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2262
    metaFetchEntryFree(&pEntry);
×
2263
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2264
  }
2265

2266
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
2,774✔
2267
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,774✔
2268
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2269
              __func__, __FILE__, __LINE__, pReq->stb, version);
2270
    metaFetchEntryFree(&pEntry);
×
2271
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2272
  }
2273

2274
  // search and set the tag index off
2275
  int32_t numOfChangedTags = 0;
2,774✔
2276
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
7,630✔
2277
    SSchema *pCol = pTagSchema->pSchema + i;
7,630✔
2278
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
7,630✔
2279
      if (!IS_IDX_ON(pCol)) {
2,774✔
2280
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2281
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2282
        metaFetchEntryFree(&pEntry);
×
2283
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2284
      }
2285
      numOfChangedTags++;
2,774✔
2286
      SSCHMEA_SET_IDX_OFF(pCol);
2,774✔
2287
      break;
2,774✔
2288
    }
2289
  }
2290

2291
  if (numOfChangedTags != 1) {
2,774✔
2292
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2293
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2294
    metaFetchEntryFree(&pEntry);
×
2295
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2296
  }
2297

2298
  // do handle the entry
2299
  pEntry->version = version;
2,774✔
2300
  pTagSchema->version++;
2,774✔
2301
  code = metaHandleEntry2(pMeta, pEntry);
2,774✔
2302
  if (code) {
2,774✔
2303
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2304
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2305
    metaFetchEntryFree(&pEntry);
×
2306
    TAOS_RETURN(code);
×
2307
  } else {
2308
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
2,774✔
2309
             pEntry->uid, version);
2310
  }
2311

2312
  metaFetchEntryFree(&pEntry);
2,774✔
2313
  TAOS_RETURN(code);
2,774✔
2314
}
2315

2316
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,092,720✔
2317
  int32_t code = TSDB_CODE_SUCCESS;
7,092,720✔
2318

2319
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,092,720✔
2320
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
6,784✔
2321
              __FILE__, __LINE__, version);
2322
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
6,784✔
2323
  }
2324

2325
  SMetaEntry *pEntry = NULL;
7,086,558✔
2326
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,090,842✔
2327
  if (code) {
7,087,984✔
2328
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2329
              __FILE__, __LINE__, pReq->name, version);
2330
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2331
  }
2332

2333
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,087,984✔
2334
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2335
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2336
    metaFetchEntryFree(&pEntry);
×
2337
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2338
  }
2339

2340
  SMetaEntry entry = {
21,216,144✔
2341
      .version = version,
2342
      .type = TSDB_SUPER_TABLE,
2343
      .uid = pReq->suid,
7,083,609✔
2344
      .name = pReq->name,
7,080,202✔
2345
      .stbEntry.schemaRow = pReq->schemaRow,
2346
      .stbEntry.schemaTag = pReq->schemaTag,
2347
      .stbEntry.keep = pReq->keep,
7,085,125✔
2348
      .colCmpr = pReq->colCmpr,
2349
      .pExtSchemas = pReq->pExtSchemas,
7,068,985✔
2350
  };
2351
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,069,760✔
2352
  if (pReq->virtualStb) {
7,069,760✔
2353
    TABLE_SET_VIRTUAL(entry.flags);
17,996✔
2354
  }
2355
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,079,473✔
2356
    TABLE_SET_ROLLUP(entry.flags);
4,260✔
2357
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,260✔
2358
  }
2359

2360
  // do handle the entry
2361
  code = metaHandleEntry2(pMeta, &entry);
7,073,664✔
2362
  if (code) {
7,062,644✔
2363
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2364
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2365
    metaFetchEntryFree(&pEntry);
×
2366
    TAOS_RETURN(code);
×
2367
  } else {
2368
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,062,644✔
2369
             pReq->suid, version);
2370
  }
2371

2372
  metaFetchEntryFree(&pEntry);
7,078,759✔
2373
  TAOS_RETURN(code);
7,097,887✔
2374
}
2375

2376
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2377
  int32_t code = 0;
×
2378

2379
  if (taosArrayGetSize(uidArray) == 0) {
×
2380
    return TSDB_CODE_SUCCESS;
×
2381
  }
2382

2383
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2384
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2385
    SMetaInfo info;
×
2386
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2387
    if (code) {
×
2388
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2389
                __func__, __FILE__, __LINE__, uid, code);
2390
      return code;
×
2391
    }
2392

2393
    SMetaEntry entry = {
×
2394
        .version = version,
2395
        .uid = uid,
2396
    };
2397

2398
    if (info.suid == 0) {
×
2399
      entry.type = -TSDB_NORMAL_TABLE;
×
2400
    } else if (info.suid == uid) {
×
2401
      entry.type = -TSDB_SUPER_TABLE;
×
2402
    } else {
2403
      entry.type = -TSDB_CHILD_TABLE;
×
2404
    }
2405
    code = metaHandleEntry2(pMeta, &entry);
×
2406
    if (code) {
×
2407
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2408
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2409
      return code;
×
2410
    }
2411
  }
2412
  return code;
×
2413
}
2414

2415
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
21,300✔
2416
  int32_t code = TSDB_CODE_SUCCESS;
21,300✔
2417

2418
  if (NULL == pReq->name || pReq->name[0] == 0) {
21,300✔
2419
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2420
              __LINE__, version);
2421
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2422
  }
2423

2424
  SMetaEntry *pEntry = NULL;
21,300✔
2425
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
21,300✔
2426
  if (code) {
21,300✔
2427
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2428
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2429
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2430
  }
2431

2432
  if (pEntry->type != TSDB_SUPER_TABLE) {
21,300✔
2433
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2434
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2435
    metaFetchEntryFree(&pEntry);
×
2436
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2437
  }
2438

2439
  if (pEntry->uid != pReq->tbUid) {
21,300✔
2440
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2441
              ", version:%" PRId64,
2442
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2443
    metaFetchEntryFree(&pEntry);
×
2444
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2445
  }
2446

2447
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
21,300✔
2448
    // overwrite the old rsma definition if exists
2449
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2450
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2451
  } else {
2452
    TABLE_SET_ROLLUP(pEntry->flags);
21,300✔
2453
  }
2454

2455
  SMetaEntry entry = *pEntry;
21,300✔
2456
  entry.version = version;
21,300✔
2457
  entry.stbEntry.rsmaParam.name = pReq->name;
21,300✔
2458
  entry.stbEntry.rsmaParam.uid = pReq->uid;
21,300✔
2459
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
21,300✔
2460
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
21,300✔
2461
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
21,300✔
2462
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
21,300✔
2463
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
21,300✔
2464
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
21,300✔
2465

2466
  // do handle the entry
2467
  code = metaHandleEntry2(pMeta, &entry);
21,300✔
2468
  if (code) {
21,300✔
2469
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2470
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2471
    metaFetchEntryFree(&pEntry);
×
2472
    TAOS_RETURN(code);
×
2473
  } else {
2474
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
21,300✔
2475
    pMeta->pVnode->config.isRsma = 1;
21,300✔
2476
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
21,300✔
2477
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2478
  }
2479

2480
  metaFetchEntryFree(&pEntry);
21,300✔
2481
  TAOS_RETURN(code);
21,300✔
2482
}
2483

2484
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,260✔
2485
  int32_t code = TSDB_CODE_SUCCESS;
4,260✔
2486

2487
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,260✔
2488
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2489
              __LINE__, version);
2490
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2491
  }
2492

2493
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
4,260✔
2494
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2495
              __LINE__, version);
2496
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2497
  }
2498

2499
  SMetaEntry *pEntry = NULL;
4,260✔
2500
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,260✔
2501
  if (code) {
4,260✔
2502
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2503
             __func__, __LINE__, pReq->name, pReq->tbName, version);
2504
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2505
  }
2506

2507
  if (pEntry->type != pReq->tbType) {
4,260✔
2508
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2509
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2510
    metaFetchEntryFree(&pEntry);
×
2511
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2512
  }
2513

2514
  if (pEntry->uid != pReq->tbUid) {
4,260✔
2515
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2516
              ", version:%" PRId64,
2517
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2518
    metaFetchEntryFree(&pEntry);
×
2519
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2520
  }
2521

2522
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
4,260✔
2523
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,260✔
2524
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,260✔
2525
      metaError(
×
2526
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2527
          "uid:%" PRIi64 ", version:%" PRId64,
2528
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2529
          pReq->uid, version);
2530
      metaFetchEntryFree(&pEntry);
×
2531
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2532
    }
2533
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
4,260✔
2534
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,260✔
2535
  } else {
2536
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2537
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
2538
    metaFetchEntryFree(&pEntry);
×
2539
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2540
  }
2541

2542
  SMetaEntry entry = *pEntry;
4,260✔
2543
  entry.version = version;
4,260✔
2544
  TABLE_RESET_ROLLUP(entry.flags);
4,260✔
2545
  entry.stbEntry.rsmaParam.uid = 0;
4,260✔
2546
  entry.stbEntry.rsmaParam.name = NULL;
4,260✔
2547
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,260✔
2548
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,260✔
2549
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,260✔
2550

2551
  // do handle the entry
2552
  code = metaHandleEntry2(pMeta, &entry);
4,260✔
2553
  if (code) {
4,260✔
2554
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2555
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
2556
    metaFetchEntryFree(&pEntry);
×
2557
    TAOS_RETURN(code);
×
2558
  } else {
2559
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
4,260✔
2560
      pMeta->pVnode->config.isRsma = 0;
×
2561
    }
2562
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
4,260✔
2563
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2564
  }
2565

2566
  metaFetchEntryFree(&pEntry);
4,260✔
2567
  TAOS_RETURN(code);
4,260✔
2568
}
2569

2570
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,360✔
2571
  int32_t code = TSDB_CODE_SUCCESS;
11,360✔
2572

2573
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,360✔
2574
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2575
              __LINE__, version);
2576
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2577
  }
2578

2579
  SMetaEntry *pEntry = NULL;
11,360✔
2580
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,360✔
2581
  if (code) {
11,360✔
2582
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2583
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2584
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2585
  }
2586

2587
  if (pEntry->uid != pReq->tbUid) {
11,360✔
2588
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2589
              ", version:%" PRId64,
2590
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2591
    metaFetchEntryFree(&pEntry);
×
2592
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2593
  }
2594

2595
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
11,360✔
2596
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
11,360✔
2597
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
11,360✔
2598
  } else {
2599
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2600
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2601
    metaFetchEntryFree(&pEntry);
×
2602
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2603
  }
2604

2605
  SMetaEntry entry = *pEntry;
11,360✔
2606
  entry.version = version;
11,360✔
2607
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
11,360✔
2608
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
11,360✔
2609
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
11,360✔
2610
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
11,360✔
2611
  }
2612
  // do handle the entry
2613
  code = metaHandleEntry2(pMeta, &entry);
11,360✔
2614
  if (code) {
11,360✔
2615
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2616
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2617
    metaFetchEntryFree(&pEntry);
×
2618
    TAOS_RETURN(code);
×
2619
  } else {
2620
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
11,360✔
2621
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2622
  }
2623

2624
  metaFetchEntryFree(&pEntry);
11,360✔
2625
  TAOS_RETURN(code);
11,360✔
2626
}
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