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

taosdata / TDengine / #4945

30 Jan 2026 06:19AM UTC coverage: 66.87% (+0.02%) from 66.849%
#4945

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1126 of 2018 new or added lines in 72 files covered. (55.8%)

13708 existing lines in 159 files now uncovered.

205277 of 306978 relevant lines covered (66.87%)

126353544.65 hits per line

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

68.27
/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,739,964✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,739,964✔
31
  void     *value = NULL;
4,762,371✔
32
  int32_t   valueSize = 0;
4,763,281✔
33
  SMetaInfo info;
4,761,536✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,763,507✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
978✔
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,760,096✔
43
  if (r == 0) {  // name exists, check uid and type
4,750,495✔
44
    int64_t uid = *(tb_uid_t *)value;
4,378✔
45
    tdbFree(value);
4,378✔
46

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

54
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
1,506✔
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) {
1,506✔
62
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
1,506✔
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,746,117✔
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,751,531✔
79
}
80

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

87
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,353,310✔
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,353,310✔
94
  if (TSDB_CODE_SUCCESS != code) {
1,353,310✔
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,353,310✔
105
  tdbFreeClear(value);
1,353,310✔
106

107
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
1,353,310✔
108
  if (TSDB_CODE_SUCCESS != code) {
1,353,310✔
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,353,310✔
116

117
  return code;
1,353,310✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
876,794✔
127
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
566✔
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);
876,228✔
133
  if (code) {
875,012✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
718✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
718✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
874,294✔
139
    tdbFreeClear(value);
875,203✔
140

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

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
871,657✔
149
  if (code) {
875,100✔
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) {
875,100✔
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;
875,100✔
161
}
162

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
4,744,691✔
169
  if (code != TSDB_CODE_SUCCESS) {
4,753,980✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
4,378✔
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
1,506✔
172
               pReq->name, pReq->suid, version);
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
1,506✔
174
    } else {
175
      TAOS_RETURN(code);
2,872✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
9,486,100✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,752,340✔
184
      .name = pReq->name,
4,743,933✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,750,130✔
188
      .stbEntry.ownerId = pReq->ownerId,
4,734,375✔
189
  };
190
  if (pReq->rollup) {
4,743,884✔
191
    TABLE_SET_ROLLUP(entry.flags);
×
192
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
193
  }
194
  if (pReq->colCmpred) {
4,724,658✔
195
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,743,946✔
196
    entry.colCmpr = pReq->colCmpr;
4,743,946✔
197
  }
198

199
  entry.pExtSchemas = pReq->pExtSchemas;
4,731,166✔
200

201
  if (pReq->virtualStb) {
4,743,418✔
202
    TABLE_SET_VIRTUAL(entry.flags);
65,250✔
203
  }
204

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

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

220
  // check request
221
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
876,794✔
222
  if (code) {
875,048✔
223
    TAOS_RETURN(code);
1,436✔
224
  }
225

226
  // handle entry
227
  SMetaEntry entry = {
873,612✔
228
      .version = verison,
229
      .type = -TSDB_SUPER_TABLE,
230
      .uid = pReq->suid,
871,349✔
231
  };
232
  code = metaHandleEntry2(pMeta, &entry);
874,535✔
233
  if (code) {
875,000✔
234
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
235
              tstrerror(code));
236
  } else {
237
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
875,000✔
238
             pReq->suid, verison);
239
  }
240
  TAOS_RETURN(code);
875,358✔
241
}
242

243
// Alter Super Table
244

245
// Create Child Table
246
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
55,468,348✔
247
  int32_t   code = TSDB_CODE_SUCCESS;
55,468,348✔
248
  void     *value = NULL;
55,468,348✔
249
  int32_t   valueSize = 0;
55,468,648✔
250
  SMetaInfo info;
55,463,580✔
251

252
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
55,462,938✔
253
      pReq->ctb.suid == 0) {
55,470,074✔
254
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
12,978✔
255
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
256
    return TSDB_CODE_INVALID_MSG;
×
257
  }
258

259
  // check table existence
260
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
55,453,130✔
261
    pReq->uid = *(int64_t *)value;
320,367✔
262
    tdbFreeClear(value);
320,407✔
263

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

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

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

287
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
318,637✔
288
  }
289

290
  // check super table existence
291
  SMetaEntry *pStbEntry = NULL;
55,145,163✔
292
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
55,144,963✔
293
  if (code) {
55,140,302✔
294
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
295
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
296
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
297
  }
298

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

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

315
  // Check tag value
316
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
55,127,434✔
317
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
55,144,114✔
318
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
55,132,490✔
319
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
244,652,171✔
320
      STagVal tagVal = {
189,787,449✔
321
          .cid = pTagSchema->pSchema[i].colId,
189,773,990✔
322
      };
323

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

335
  metaFetchEntryFree(&pStbEntry);
55,138,814✔
336

337
  // check grant
338
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
55,128,905✔
339
    code = grantCheck(TSDB_GRANT_TIMESERIES);
55,138,056✔
340
    if (TSDB_CODE_SUCCESS != code) {
55,126,792✔
341
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
342
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
343
    }
344
  }
345
  return code;
55,127,986✔
346
}
347

348
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
54,927,629✔
349
  int32_t code = TSDB_CODE_SUCCESS;
54,927,629✔
350

351
  if (NULL == ppRsp) {
54,927,629✔
352
    return code;
×
353
  }
354

355
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
54,927,629✔
356
  if (NULL == ppRsp) {
54,907,749✔
357
    return terrno;
×
358
  }
359

360
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
54,907,749✔
361
  (*ppRsp)->tuid = pEntry->uid;
54,916,406✔
362
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
54,929,015✔
363
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
54,927,004✔
364

365
  return code;
54,934,462✔
366
}
367

368
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
55,265,043✔
369
  int32_t code = TSDB_CODE_SUCCESS;
55,265,043✔
370

371
  // check request
372
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
55,265,043✔
373
  if (code) {
55,244,875✔
374
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
320,407✔
375
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,730✔
376
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
377
    }
378
    return code;
320,367✔
379
  }
380

381
  SMetaEntry entry = {
54,924,468✔
382
      .version = version,
383
      .type = TSDB_CHILD_TABLE,
384
      .uid = pReq->uid,
54,927,095✔
385
      .name = pReq->name,
54,918,245✔
386
      .ctbEntry.btime = pReq->btime,
54,933,440✔
387
      .ctbEntry.ttlDays = pReq->ttl,
54,919,512✔
388
      .ctbEntry.commentLen = pReq->commentLen,
54,907,433✔
389
      .ctbEntry.comment = pReq->comment,
54,909,240✔
390
      .ctbEntry.suid = pReq->ctb.suid,
54,933,627✔
391
      .ctbEntry.pTags = pReq->ctb.pTag,
54,912,970✔
392
  };
393

394
  // build response
395
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
54,909,615✔
396
  if (code) {
54,934,020✔
397
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
398
              tstrerror(code));
399
  }
400

401
  // handle entry
402
  code = metaHandleEntry2(pMeta, &entry);
54,934,020✔
403
  if (TSDB_CODE_SUCCESS == code) {
54,944,290✔
404
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
54,944,605✔
405
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
406
  } else {
407
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
9✔
408
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
409
              pReq->ctb.suid, version);
410
  }
411
  return code;
54,949,140✔
412
}
413

414
// Drop Child Table
415

416
// Alter Child Table
417

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

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

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

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

447
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
5,612,228✔
448
  int32_t code = TSDB_CODE_SUCCESS;
5,612,228✔
449

450
  if (NULL == ppRsp) {
5,612,228✔
451
    return code;
×
452
  }
453

454
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
5,612,228✔
455
  if (NULL == *ppRsp) {
5,612,228✔
456
    return terrno;
×
457
  }
458

459
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp);
5,612,228✔
460
  if (code) {
5,612,228✔
461
    taosMemoryFreeClear(*ppRsp);
×
462
    return code;
×
463
  }
464

465
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
72,295,993✔
466
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
66,683,765✔
467
    (*ppRsp)->pSchemaExt[i].colId = p->id;
66,683,765✔
468
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
66,683,765✔
469
    if (pEntry->pExtSchemas) {
66,683,765✔
470
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
429,298✔
471
    }
472
  }
473

474
  return code;
5,612,228✔
475
}
476

477
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
5,639,806✔
478
  int32_t code = TSDB_CODE_SUCCESS;
5,639,806✔
479

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

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

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

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

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

529
  if (NULL == ppRsp) {
130,527✔
UNCOV
530
    return code;
×
531
  }
532

533
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
130,527✔
534
  if (NULL == *ppRsp) {
130,527✔
UNCOV
535
    return terrno;
×
536
  }
537

538
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, *ppRsp,
130,527✔
539
                              TSDB_VIRTUAL_NORMAL_TABLE);
540
  if (code) {
130,527✔
541
    taosMemoryFreeClear(*ppRsp);
520✔
542
    return code;
520✔
543
  }
544

545
  return code;
130,007✔
546
}
547

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

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

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

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

594
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
202,719✔
595
  int32_t code = TSDB_CODE_SUCCESS;
202,719✔
596

597
  if (NULL == ppRsp) {
202,719✔
UNCOV
598
    return code;
×
599
  }
600

601
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
202,719✔
602
  if (NULL == *ppRsp) {
202,719✔
UNCOV
603
    return terrno;
×
604
  }
605

606
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
202,719✔
607
  if (code) {
202,719✔
608
    taosMemoryFreeClear(*ppRsp);
520✔
609
    return code;
520✔
610
  }
611
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
202,199✔
612

613
  return code;
202,199✔
614
}
615

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

627
  SMetaEntry entry = {.version = version,
405,438✔
628
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
629
                      .uid = pReq->uid,
202,719✔
630
                      .name = pReq->name,
202,719✔
631
                      .ctbEntry.btime = pReq->btime,
202,719✔
632
                      .ctbEntry.ttlDays = pReq->ttl,
202,719✔
633
                      .ctbEntry.commentLen = pReq->commentLen,
202,719✔
634
                      .ctbEntry.comment = pReq->comment,
202,719✔
635
                      .ctbEntry.suid = pReq->ctb.suid,
202,719✔
636
                      .ctbEntry.pTags = pReq->ctb.pTag,
202,719✔
637
                      .colRef = pReq->colRef};
638

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

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

661
// Drop Normal Table
662

663
// Alter Normal Table
664

665
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
61,236,867✔
666
  int32_t code = TSDB_CODE_SUCCESS;
61,236,867✔
667
  if (TSDB_CHILD_TABLE == pReq->type) {
61,236,867✔
668
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
55,265,112✔
669
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
5,973,052✔
670
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
5,639,806✔
671
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
333,246✔
672
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
130,527✔
673
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
202,719✔
674
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
202,719✔
675
  } else {
UNCOV
676
    code = TSDB_CODE_INVALID_MSG;
×
677
  }
678
  TAOS_RETURN(code);
61,241,943✔
679
}
680

681
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,353,310✔
682
  int32_t code = TSDB_CODE_SUCCESS;
1,353,310✔
683

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

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

701
  SMetaEntry entry = {
1,353,310✔
702
      .version = version,
703
      .uid = pReq->uid,
1,353,310✔
704
  };
705

706
  if (pReq->isVirtual) {
1,353,310✔
707
    if (pReq->suid == 0) {
52,819✔
708
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,627✔
709
    } else {
710
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,192✔
711
    }
712
  } else {
713
    if (pReq->suid == 0) {
1,300,491✔
714
      entry.type = -TSDB_NORMAL_TABLE;
750,381✔
715
    } else {
716
      entry.type = -TSDB_CHILD_TABLE;
550,110✔
717
    }
718
  }
719
  code = metaHandleEntry2(pMeta, &entry);
1,353,310✔
720
  if (code) {
1,353,310✔
UNCOV
721
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
722
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
723
  } else {
724
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,353,310✔
725
             pReq->uid, version);
726
  }
727
  TAOS_RETURN(code);
1,353,310✔
728
}
729

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

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

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

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

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

779
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,430,223✔
780
  int32_t code = TSDB_CODE_SUCCESS;
3,430,223✔
781

782
  // check request
783
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,430,223✔
784
  if (code) {
3,430,223✔
UNCOV
785
    TAOS_RETURN(code);
×
786
  }
787

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

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

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

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

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

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

914
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,118,735✔
915
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
49,947✔
916
    if (code) {
49,947✔
UNCOV
917
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
918
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
919
    } else {
920
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
17,411,254✔
921
        SColRef *p = &pEntry->colRef.pColRef[i];
17,361,307✔
922
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,361,307✔
923
        pRsp->pColRefs[i].id = p->id;
17,361,307✔
924
        if (p->hasRef) {
17,361,307✔
925
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
161,229✔
926
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
161,229✔
927
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
161,229✔
928
        }
929
      }
930
    }
931
  } else {
932
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
3,068,788✔
933
    if (code) {
3,068,788✔
UNCOV
934
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
935
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
936
    } else {
937
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
938
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
939
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
940
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
941
      }
942
    }
943
  }
944

945
  metaFetchEntryFree(&pEntry);
3,118,735✔
946
  TAOS_RETURN(code);
3,118,735✔
947
}
948

949
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
96,457✔
950
  int32_t code = TSDB_CODE_SUCCESS;
96,457✔
951

952
  // check request
953
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
96,457✔
954
  if (code) {
96,457✔
UNCOV
955
    TAOS_RETURN(code);
×
956
  }
957

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

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

974
  // search the column to drop
975
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
96,457✔
976
  SSchema        *pColumn = NULL;
96,457✔
977
  SSchema         tColumn;
96,457✔
978
  int32_t         iColumn = 0;
96,457✔
979
  for (; iColumn < pSchema->nCols; iColumn++) {
85,022,350✔
980
    pColumn = &pSchema->pSchema[iColumn];
85,022,350✔
981
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
85,022,350✔
982
      break;
96,457✔
983
    }
984
  }
985

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

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

1000
  tColumn = *pColumn;
96,457✔
1001

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

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

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

1060
  // build response
1061
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
96,457✔
1062
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,538✔
1063
    if (code) {
28,538✔
UNCOV
1064
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1065
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1066
    } else {
1067
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
34,259,059✔
1068
        SColRef *p = &pEntry->colRef.pColRef[i];
34,230,521✔
1069
        pRsp->pColRefs[i].hasRef = p->hasRef;
34,230,521✔
1070
        pRsp->pColRefs[i].id = p->id;
34,230,521✔
1071
        if (p->hasRef) {
34,230,521✔
1072
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
17,127,405✔
1073
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
17,127,405✔
1074
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
17,127,405✔
1075
        }
1076
      }
1077
    }
1078
  } else {
1079
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
67,919✔
1080
    if (code) {
67,919✔
UNCOV
1081
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1082
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1083
    } else {
1084
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
83,060,327✔
1085
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
82,992,408✔
1086
        pRsp->pSchemaExt[i].colId = p->id;
82,992,408✔
1087
        pRsp->pSchemaExt[i].compress = p->alg;
82,992,408✔
1088
      }
1089
    }
1090
  }
1091

1092
  metaFetchEntryFree(&pEntry);
96,457✔
1093
  TAOS_RETURN(code);
96,457✔
1094
}
1095

1096
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
43,344✔
1097
  int32_t code = TSDB_CODE_SUCCESS;
43,344✔
1098

1099
  // check request
1100
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
43,344✔
1101
  if (code) {
43,344✔
UNCOV
1102
    TAOS_RETURN(code);
×
1103
  }
1104

1105
  if (NULL == pReq->colNewName) {
43,344✔
UNCOV
1106
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1107
              __func__, __FILE__, __LINE__, version);
UNCOV
1108
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1109
  }
1110

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

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

1127
  // search the column to update
1128
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
43,344✔
1129
  SSchema        *pColumn = NULL;
43,344✔
1130
  int32_t         iColumn = 0;
43,344✔
1131
  for (int32_t i = 0; i < pSchema->nCols; i++) {
198,173✔
1132
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
198,173✔
1133
      pColumn = &pSchema->pSchema[i];
43,344✔
1134
      iColumn = i;
43,344✔
1135
      break;
43,344✔
1136
    }
1137
  }
1138

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

1146

1147
  // do update column name
1148
  pEntry->version = version;
43,344✔
1149
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
43,344✔
1150
  pSchema->version++;
43,344✔
1151

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

1164
  // build response
1165
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
43,344✔
1166
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,566✔
1167
    if (code) {
26,566✔
UNCOV
1168
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1169
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1170
    } else {
1171
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
187,794✔
1172
        SColRef *p = &pEntry->colRef.pColRef[i];
161,228✔
1173
        pRsp->pColRefs[i].hasRef = p->hasRef;
161,228✔
1174
        pRsp->pColRefs[i].id = p->id;
161,228✔
1175
        if (p->hasRef) {
161,228✔
1176
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
99,490✔
1177
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
99,490✔
1178
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
99,490✔
1179
        }
1180
      }
1181
    }
1182
  } else {
1183
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,778✔
1184
    if (code) {
16,778✔
UNCOV
1185
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1186
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1187
    } else {
1188
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
177,009✔
1189
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
160,231✔
1190
        pRsp->pSchemaExt[i].colId = p->id;
160,231✔
1191
        pRsp->pSchemaExt[i].compress = p->alg;
160,231✔
1192
      }
1193
    }
1194
  }
1195

1196
  metaFetchEntryFree(&pEntry);
43,344✔
1197
  TAOS_RETURN(code);
43,344✔
1198
}
1199

1200
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
462,770✔
1201
  int32_t code = TSDB_CODE_SUCCESS;
462,770✔
1202

1203
  // check request
1204
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
462,770✔
1205
  if (code) {
462,770✔
UNCOV
1206
    TAOS_RETURN(code);
×
1207
  }
1208

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

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

1225
  // search the column to update
1226
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
462,770✔
1227
  SSchema        *pColumn = NULL;
462,770✔
1228
  int32_t         iColumn = 0;
462,770✔
1229
  int32_t         rowSize = 0;
462,770✔
1230
  for (int32_t i = 0; i < pSchema->nCols; i++) {
35,182,121✔
1231
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
34,719,351✔
1232
      pColumn = &pSchema->pSchema[i];
462,770✔
1233
      iColumn = i;
462,770✔
1234
    }
1235
    rowSize += pSchema->pSchema[i].bytes;
34,719,351✔
1236
  }
1237

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

1245
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
462,770✔
1246
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
179,547✔
1247
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1248
              version);
1249
    metaFetchEntryFree(&pEntry);
179,547✔
1250
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
179,547✔
1251
  }
1252

1253
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
283,223✔
1254
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
283,223✔
1255
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
43,190✔
1256
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1257
              version);
1258
    metaFetchEntryFree(&pEntry);
43,190✔
1259
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
43,190✔
1260
  }
1261

1262
  // do change the column bytes
1263
  pEntry->version = version;
240,033✔
1264
  pSchema->version++;
240,033✔
1265
  pColumn->bytes = pReq->colModBytes;
240,033✔
1266

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

1279
  // build response
1280
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
240,033✔
1281
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,820✔
1282
    if (code) {
25,820✔
UNCOV
1283
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1284
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1285
    } else {
1286
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
17,215,104✔
1287
        SColRef *p = &pEntry->colRef.pColRef[i];
17,189,284✔
1288
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,189,284✔
1289
        pRsp->pColRefs[i].id = p->id;
17,189,284✔
1290
        if (p->hasRef) {
17,189,284✔
1291
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
75,328✔
1292
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
75,328✔
1293
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
75,328✔
1294
        }
1295
      }
1296
    }
1297
  } else {
1298
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
214,213✔
1299
    if (code) {
214,213✔
UNCOV
1300
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1301
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1302
    } else {
1303
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,612,063✔
1304
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,397,850✔
1305
        pRsp->pSchemaExt[i].colId = p->id;
8,397,850✔
1306
        pRsp->pSchemaExt[i].compress = p->alg;
8,397,850✔
1307
      }
1308
    }
1309
  }
1310

1311
  metaFetchEntryFree(&pEntry);
240,033✔
1312
  TAOS_RETURN(code);
240,033✔
1313
}
1314

1315
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,479,365✔
1316
  int32_t code = 0;
7,479,365✔
1317

1318
  // check tag name
1319
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,479,365✔
UNCOV
1320
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1321
              __func__, __FILE__, __LINE__, pReq->tagName, version);
UNCOV
1322
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1323
  }
1324

1325
  // check name
1326
  void   *value = NULL;
7,479,365✔
1327
  int32_t valueSize = 0;
7,479,365✔
1328
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,479,365✔
1329
  if (code) {
7,479,365✔
1330
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
258✔
1331
              __FILE__, __LINE__, pReq->tbName, version);
1332
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
258✔
1333
    TAOS_RETURN(code);
258✔
1334
  }
1335
  tdbFreeClear(value);
7,479,107✔
1336

1337
  TAOS_RETURN(code);
7,479,107✔
1338
}
1339

1340
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1341

1342
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,381,822✔
1343
  if (isNull) {
7,381,822✔
1344
    if (!tTagGet(pOldTag, &value)) {
45,624✔
1345
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,698✔
1346
      return true;
19,698✔
1347
    }
1348
    return false;
25,926✔
1349
  }
1350
  if (!tTagGet(pOldTag, &value)){
7,336,198✔
1351
    return false;
175,861✔
1352
  }
1353
  if (IS_VAR_DATA_TYPE(value.type)) {
7,160,337✔
1354
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
57,080✔
1355
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
36,309✔
1356
      return true;
36,309✔
1357
    }
1358
  } else {
1359
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,103,257✔
1360
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
88,624✔
1361
      return true;
88,624✔
1362
    }
1363
  }
1364
  return false;
7,035,404✔
1365
}
1366

1367
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,479,365✔
1368
  int32_t code = TSDB_CODE_SUCCESS;
7,479,365✔
1369

1370
  // check request
1371
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,479,365✔
1372
  if (code) {
7,479,365✔
1373
    TAOS_RETURN(code);
258✔
1374
  }
1375

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

1385
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
7,479,107✔
UNCOV
1386
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1387
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1388
    metaFetchEntryFree(&pChild);
×
1389
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1390
  }
1391

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

1402
  // search the tag to update
1403
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,479,107✔
1404
  SSchema        *pColumn = NULL;
7,479,107✔
1405
  int32_t         iColumn = 0;
7,479,107✔
1406
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,131,095✔
1407
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,131,095✔
1408
      pColumn = &pTagSchema->pSchema[i];
7,479,107✔
1409
      iColumn = i;
7,479,107✔
1410
      break;
7,479,107✔
1411
    }
1412
  }
1413

1414
  if (NULL == pColumn) {
7,479,107✔
UNCOV
1415
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1416
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
UNCOV
1417
    metaFetchEntryFree(&pChild);
×
1418
    metaFetchEntryFree(&pSuper);
×
UNCOV
1419
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1420
  }
1421

1422
  // do change tag value
1423
  pChild->version = version;
7,479,107✔
1424
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,479,107✔
1425
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
116,425✔
1426
    if (NULL == pNewTag) {
116,425✔
UNCOV
1427
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1428
                __LINE__, tstrerror(terrno), version);
UNCOV
1429
      metaFetchEntryFree(&pChild);
×
1430
      metaFetchEntryFree(&pSuper);
×
UNCOV
1431
      TAOS_RETURN(terrno);
×
1432
    }
1433
    pChild->ctbEntry.pTags = pNewTag;
116,425✔
1434
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
116,425✔
1435
  } else {
1436
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,362,682✔
1437

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

1447
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,643,042✔
1448
      STagVal value = {
8,416,059✔
1449
          .type = pTagSchema->pSchema[i].type,
8,416,059✔
1450
          .cid = pTagSchema->pSchema[i].colId,
8,416,059✔
1451
      };
1452

1453
      if (iColumn == i) {
8,416,059✔
1454
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,362,682✔
1455
          taosArrayDestroy(pTagArray);
135,699✔
1456
          metaFetchEntryFree(&pChild);
135,699✔
1457
          metaFetchEntryFree(&pSuper);
135,699✔
1458
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
135,699✔
1459
        }
1460
        if (pReq->isNull) {
7,226,983✔
1461
          continue;
24,012✔
1462
        }
1463
        if (IS_VAR_DATA_TYPE(value.type)) {
7,202,971✔
1464
          value.pData = pReq->pTagVal;
99,017✔
1465
          value.nData = pReq->nTagVal;
99,017✔
1466
        } else {
1467
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,103,954✔
1468
        }
1469
      } else if (!tTagGet(pOldTag, &value)) {
1,053,377✔
1470
        continue;
287,518✔
1471
      }
1472

1473
      if (NULL == taosArrayPush(pTagArray, &value)) {
7,968,830✔
UNCOV
1474
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1475
                  __LINE__, tstrerror(terrno), version);
UNCOV
1476
        taosArrayDestroy(pTagArray);
×
1477
        metaFetchEntryFree(&pChild);
×
UNCOV
1478
        metaFetchEntryFree(&pSuper);
×
1479
        TAOS_RETURN(terrno);
×
1480
      }
1481
    }
1482

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

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

1511
  // free resource and return
1512
  metaFetchEntryFree(&pChild);
7,343,408✔
1513
  metaFetchEntryFree(&pSuper);
7,343,408✔
1514
  TAOS_RETURN(code);
7,343,408✔
1515
}
1516

1517
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,190✔
1518
  int32_t code = 0;
3,190✔
1519

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

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

1539
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,190✔
UNCOV
1540
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1541
              __FILE__, __LINE__, version);
UNCOV
1542
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1543
  }
1544

1545
  TAOS_RETURN(code);
3,190✔
1546
}
1547

1548
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,190✔
1549
  int32_t code = TSDB_CODE_SUCCESS;
3,190✔
1550

1551
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,190✔
1552
  if (code) {
3,190✔
UNCOV
1553
    TAOS_RETURN(code);
×
1554
  }
1555

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

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

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

1582
  // search the tags to update
1583
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,190✔
1584

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

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

1604
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
22,330✔
1605
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
19,140✔
1606
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
19,140✔
UNCOV
1607
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1608
                __LINE__, tstrerror(terrno), version);
UNCOV
1609
      taosHashCleanup(pTagTable);
×
1610
      metaFetchEntryFree(&pChild);
×
UNCOV
1611
      metaFetchEntryFree(&pSuper);
×
1612
      TAOS_RETURN(terrno);
×
1613
    }
1614
  }
1615

1616
  int32_t numOfChangedTags = 0;
3,190✔
1617
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
25,520✔
1618
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
22,330✔
1619
        ? numOfChangedTags++
19,140✔
1620
        : 0;
22,330✔
1621
  }
1622
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,190✔
UNCOV
1623
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1624
              __FILE__, __LINE__, version);
UNCOV
1625
    taosHashCleanup(pTagTable);
×
1626
    metaFetchEntryFree(&pChild);
×
UNCOV
1627
    metaFetchEntryFree(&pSuper);
×
1628
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1629
  }
1630

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

1644
  bool allSame = true;
3,190✔
1645

1646
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
25,520✔
1647
    SSchema *pCol = &pTagSchema->pSchema[i];
22,330✔
1648
    STagVal  value = {
22,330✔
1649
         .cid = pCol->colId,
22,330✔
1650
    };
1651

1652
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
22,330✔
1653
    if (pTagVal == NULL) {
22,330✔
1654
      if (!tTagGet(pOldTag, &value)) {
3,190✔
UNCOV
1655
        continue;
×
1656
      }
1657
    } else {
1658
      value.type = pCol->type;
19,140✔
1659
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
19,140✔
1660
        allSame = false;
10,208✔
1661
      }
1662
      if (pTagVal->isNull) {
19,140✔
1663
        continue;
3,828✔
1664
      }
1665

1666
      if (IS_VAR_DATA_TYPE(pCol->type)) {
15,312✔
1667
        value.pData = pTagVal->pTagVal;
2,552✔
1668
        value.nData = pTagVal->nTagVal;
2,552✔
1669
      } else {
1670
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
12,760✔
1671
      }
1672
    }
1673

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

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

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

1723
  taosHashCleanup(pTagTable);
2,552✔
1724
  metaFetchEntryFree(&pChild);
2,552✔
1725
  metaFetchEntryFree(&pSuper);
2,552✔
1726
  TAOS_RETURN(code);
2,552✔
1727
}
1728

1729
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,116✔
1730
  int32_t code = TSDB_CODE_SUCCESS;
24,116✔
1731

1732
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
24,116✔
UNCOV
1733
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1734
              __FILE__, __LINE__, version);
UNCOV
1735
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1736
  }
1737

1738
  return code;
24,116✔
1739
}
1740

1741
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,116✔
1742
  int32_t code = 0;
24,116✔
1743

1744
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
24,116✔
1745
  if (code) {
24,116✔
UNCOV
1746
    TAOS_RETURN(code);
×
1747
  }
1748

1749
  // fetch entry
1750
  SMetaEntry *pEntry = NULL;
24,116✔
1751
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
24,116✔
1752
  if (code) {
24,116✔
UNCOV
1753
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1754
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1755
    TAOS_RETURN(code);
×
1756
  }
1757

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

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

1821
  metaFetchEntryFree(&pEntry);
24,116✔
1822
  TAOS_RETURN(code);
24,116✔
1823
}
1824

1825
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5,802✔
1826
  int32_t code = TSDB_CODE_SUCCESS;
5,802✔
1827

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

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

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

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

1856
  // do change the entry
1857
  int8_t           updated = 0;
5,802✔
1858
  SColCmprWrapper *wp = &pEntry->colCmpr;
5,802✔
1859
  for (int32_t i = 0; i < wp->nCols; i++) {
46,416✔
1860
    SColCmpr *p = &wp->pColCmpr[i];
40,614✔
1861
    if (p->id == pReq->colId) {
40,614✔
1862
      uint32_t dst = 0;
5,802✔
1863
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5,802✔
1864
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1865
      if (updated > 0) {
5,802✔
1866
        p->alg = dst;
5,802✔
1867
      }
1868
    }
1869
  }
1870

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

1885
  pEntry->version = version;
5,802✔
1886

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

1899
  metaFetchEntryFree(&pEntry);
5,802✔
1900
  TAOS_RETURN(code);
5,802✔
1901
}
1902

1903
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
71,878✔
1904
  int32_t code = TSDB_CODE_SUCCESS;
71,878✔
1905

1906
  // check request
1907
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
71,878✔
1908
  if (code) {
71,878✔
UNCOV
1909
    TAOS_RETURN(code);
×
1910
  }
1911

1912
  if (NULL == pReq->refDbName) {
71,878✔
UNCOV
1913
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1914
              __func__, __FILE__, __LINE__, version);
UNCOV
1915
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1916
  }
1917

1918
  if (NULL == pReq->refTbName) {
71,878✔
UNCOV
1919
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1920
              __func__, __FILE__, __LINE__, version);
UNCOV
1921
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1922
  }
1923

1924
  if (NULL == pReq->refColName) {
71,878✔
UNCOV
1925
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1926
              __func__, __FILE__, __LINE__, version);
UNCOV
1927
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1928
  }
1929

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

1939
  if (pEntry->version >= version) {
71,878✔
UNCOV
1940
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1941
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
1942
    metaFetchEntryFree(&pEntry);
×
1943
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1944
  }
1945

1946
  // fetch super entry
1947
  SMetaEntry *pSuper = NULL;
71,878✔
1948
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
71,878✔
1949
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
25,946✔
1950
    if (code) {
25,946✔
UNCOV
1951
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1952
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
UNCOV
1953
      metaFetchEntryFree(&pEntry);
×
1954
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1955
    }
1956
  }
1957

1958
  // search the column to update
1959
  SSchemaWrapper *pSchema =
71,878✔
1960
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
71,878✔
1961
  SColRef *pColRef = NULL;
71,878✔
1962
  int32_t  iColumn = 0;
71,878✔
1963
  for (int32_t i = 0; i < pSchema->nCols; i++) {
356,863✔
1964
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
356,863✔
1965
      pColRef = &pEntry->colRef.pColRef[i];
71,878✔
1966
      iColumn = i;
71,878✔
1967
      break;
71,878✔
1968
    }
1969
  }
1970

1971
  if (NULL == pColRef) {
71,878✔
UNCOV
1972
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1973
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
UNCOV
1974
    metaFetchEntryFree(&pEntry);
×
1975
    metaFetchEntryFree(&pSuper);
×
UNCOV
1976
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1977
  }
1978

1979
  // do update column name
1980
  pEntry->version = version;
71,878✔
1981
  pColRef->hasRef = true;
71,878✔
1982
  pColRef->id = pSchema->pSchema[iColumn].colId;
71,878✔
1983
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
71,878✔
1984
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
71,878✔
1985
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
71,878✔
1986
  pSchema->version++;
71,878✔
1987
  pEntry->colRef.version++;
71,878✔
1988

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

2002
  // build response
2003
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
71,878✔
2004
  if (code) {
71,878✔
UNCOV
2005
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2006
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2007
  } else {
2008
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
537,570✔
2009
      SColRef *p = &pEntry->colRef.pColRef[i];
465,692✔
2010
      pRsp->pColRefs[i].hasRef = p->hasRef;
465,692✔
2011
      pRsp->pColRefs[i].id = p->id;
465,692✔
2012
      if (p->hasRef) {
465,692✔
2013
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
321,493✔
2014
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
321,493✔
2015
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
321,493✔
2016
      }
2017
    }
2018
  }
2019

2020
  metaFetchEntryFree(&pEntry);
71,878✔
2021
  metaFetchEntryFree(&pSuper);
71,878✔
2022
  TAOS_RETURN(code);
71,878✔
2023
}
2024

2025
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
53,058✔
2026
  int32_t code = TSDB_CODE_SUCCESS;
53,058✔
2027

2028
  // check request
2029
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
53,058✔
2030
  if (code) {
53,058✔
UNCOV
2031
    TAOS_RETURN(code);
×
2032
  }
2033

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

2043
  if (pEntry->version >= version) {
53,058✔
UNCOV
2044
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2045
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
2046
    metaFetchEntryFree(&pEntry);
×
2047
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2048
  }
2049

2050
  // fetch super entry
2051
  SMetaEntry *pSuper = NULL;
53,058✔
2052
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
53,058✔
2053
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
26,732✔
2054
    if (code) {
26,732✔
UNCOV
2055
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2056
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
UNCOV
2057
      metaFetchEntryFree(&pEntry);
×
2058
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2059
    }
2060
  }
2061

2062
  // search the column to update
2063
  SSchemaWrapper *pSchema =
53,058✔
2064
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
53,058✔
2065
  SColRef *pColRef = NULL;
53,058✔
2066
  int32_t  iColumn = 0;
53,058✔
2067
  for (int32_t i = 0; i < pSchema->nCols; i++) {
216,083✔
2068
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
216,083✔
2069
      pColRef = &pEntry->colRef.pColRef[i];
53,058✔
2070
      iColumn = i;
53,058✔
2071
      break;
53,058✔
2072
    }
2073
  }
2074

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

2083
  // do update column name
2084
  pEntry->version = version;
53,058✔
2085
  pColRef->hasRef = false;
53,058✔
2086
  pColRef->id = pSchema->pSchema[iColumn].colId;
53,058✔
2087
  pSchema->version++;
53,058✔
2088
  pEntry->colRef.version++;
53,058✔
2089

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

2103
  // build response
2104
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
53,058✔
2105
  if (code) {
53,058✔
UNCOV
2106
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2107
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2108
  } else {
2109
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
325,954✔
2110
      SColRef *p = &pEntry->colRef.pColRef[i];
272,896✔
2111
      pRsp->pColRefs[i].hasRef = p->hasRef;
272,896✔
2112
      pRsp->pColRefs[i].id = p->id;
272,896✔
2113
      if (p->hasRef) {
272,896✔
2114
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
135,315✔
2115
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
135,315✔
2116
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
135,315✔
2117
      }
2118
    }
2119
  }
2120

2121
  metaFetchEntryFree(&pEntry);
53,058✔
2122
  metaFetchEntryFree(&pSuper);
53,058✔
2123
  TAOS_RETURN(code);
53,058✔
2124
}
2125

2126
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,364✔
2127
  int32_t code = TSDB_CODE_SUCCESS;
4,364✔
2128

2129
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,364✔
UNCOV
2130
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2131
              __FILE__, __LINE__, version);
UNCOV
2132
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2133
  }
2134

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

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

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

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

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

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

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

2195
  int32_t numOfChangedTags = 0;
4,364✔
2196
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
22,277✔
2197
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,913✔
2198
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,913✔
2199

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

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

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

2227
  pEntry->version = version;
4,364✔
2228
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,364✔
2229

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

2242
  metaFetchEntryFree(&pEntry);
4,364✔
2243
  TAOS_RETURN(code);
4,364✔
2244
}
2245

2246
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,888✔
2247
  int32_t code = TSDB_CODE_SUCCESS;
2,888✔
2248

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

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

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

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

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

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

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

2316
  metaFetchEntryFree(&pEntry);
2,888✔
2317
  TAOS_RETURN(code);
2,888✔
2318
}
2319

2320
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,352,577✔
2321
  int32_t code = TSDB_CODE_SUCCESS;
7,352,577✔
2322

2323
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,352,577✔
2324
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
12,506✔
2325
              __FILE__, __LINE__, version);
2326
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
12,506✔
2327
  }
2328

2329
  SMetaEntry *pEntry = NULL;
7,343,518✔
2330
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,354,006✔
2331
  if (code) {
7,344,363✔
UNCOV
2332
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2333
              __FILE__, __LINE__, pReq->name, version);
UNCOV
2334
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2335
  }
2336

2337
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,344,363✔
UNCOV
2338
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2339
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
UNCOV
2340
    metaFetchEntryFree(&pEntry);
×
2341
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2342
  }
2343

2344
  SMetaEntry entry = {
22,003,501✔
2345
      .version = version,
2346
      .type = TSDB_SUPER_TABLE,
2347
      .uid = pReq->suid,
7,346,294✔
2348
      .name = pReq->name,
7,339,600✔
2349
      .stbEntry.schemaRow = pReq->schemaRow,
2350
      .stbEntry.schemaTag = pReq->schemaTag,
2351
      .stbEntry.keep = pReq->keep,
7,321,436✔
2352
      .stbEntry.ownerId = pReq->ownerId,
7,332,548✔
2353
      .colCmpr = pReq->colCmpr,
2354
      .pExtSchemas = pReq->pExtSchemas,
7,333,152✔
2355
  };
2356
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,326,754✔
2357
  if (pReq->virtualStb) {
7,326,754✔
2358
    TABLE_SET_VIRTUAL(entry.flags);
20,112✔
2359
  }
2360
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,329,023✔
2361
    TABLE_SET_ROLLUP(entry.flags);
4,314✔
2362
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,314✔
2363
  }
2364

2365
  // do handle the entry
2366
  code = metaHandleEntry2(pMeta, &entry);
7,356,193✔
2367
  if (code) {
7,338,365✔
UNCOV
2368
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2369
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
UNCOV
2370
    metaFetchEntryFree(&pEntry);
×
2371
    TAOS_RETURN(code);
×
2372
  } else {
2373
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,338,365✔
2374
             pReq->suid, version);
2375
  }
2376

2377
  metaFetchEntryFree(&pEntry);
7,359,101✔
2378
  TAOS_RETURN(code);
7,363,715✔
2379
}
2380

UNCOV
2381
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
UNCOV
2382
  int32_t code = 0;
×
2383

2384
  if (taosArrayGetSize(uidArray) == 0) {
×
2385
    return TSDB_CODE_SUCCESS;
×
2386
  }
2387

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

2398
    SMetaEntry entry = {
×
2399
        .version = version,
2400
        .uid = uid,
2401
    };
2402

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

2420
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
21,570✔
2421
  int32_t code = TSDB_CODE_SUCCESS;
21,570✔
2422

2423
  if (NULL == pReq->name || pReq->name[0] == 0) {
21,570✔
UNCOV
2424
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2425
              __LINE__, version);
UNCOV
2426
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2427
  }
2428

2429
  SMetaEntry *pEntry = NULL;
21,570✔
2430
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
21,570✔
2431
  if (code) {
21,570✔
UNCOV
2432
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2433
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2434
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2435
  }
2436

2437
  if (pEntry->type != TSDB_SUPER_TABLE) {
21,570✔
UNCOV
2438
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2439
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
UNCOV
2440
    metaFetchEntryFree(&pEntry);
×
2441
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2442
  }
2443

2444
  if (pEntry->uid != pReq->tbUid) {
20,851✔
UNCOV
2445
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2446
              ", version:%" PRId64,
2447
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2448
    metaFetchEntryFree(&pEntry);
×
UNCOV
2449
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2450
  }
2451

2452
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
21,570✔
2453
    // overwrite the old rsma definition if exists
UNCOV
2454
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
UNCOV
2455
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2456
  } else {
2457
    TABLE_SET_ROLLUP(pEntry->flags);
20,851✔
2458
  }
2459

2460
  SMetaEntry entry = *pEntry;
21,570✔
2461
  entry.version = version;
21,570✔
2462
  entry.stbEntry.rsmaParam.name = pReq->name;
21,570✔
2463
  entry.stbEntry.rsmaParam.uid = pReq->uid;
21,570✔
2464
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
20,851✔
2465
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
21,570✔
2466
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
21,570✔
2467
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
21,570✔
2468
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
20,851✔
2469
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
21,570✔
2470

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

2485
  metaFetchEntryFree(&pEntry);
21,570✔
2486
  TAOS_RETURN(code);
21,570✔
2487
}
2488

2489
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,314✔
2490
  int32_t code = TSDB_CODE_SUCCESS;
4,314✔
2491

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

2498
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
4,314✔
UNCOV
2499
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2500
              __LINE__, version);
UNCOV
2501
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2502
  }
2503

2504
  SMetaEntry *pEntry = NULL;
4,314✔
2505
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,314✔
2506
  if (code) {
4,314✔
UNCOV
2507
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2508
             __func__, __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2509
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2510
  }
2511

2512
  if (pEntry->type != pReq->tbType) {
4,314✔
UNCOV
2513
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2514
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
UNCOV
2515
    metaFetchEntryFree(&pEntry);
×
2516
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2517
  }
2518

2519
  if (pEntry->uid != pReq->tbUid) {
4,314✔
UNCOV
2520
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2521
              ", version:%" PRId64,
2522
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2523
    metaFetchEntryFree(&pEntry);
×
UNCOV
2524
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2525
  }
2526

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

2547
  SMetaEntry entry = *pEntry;
4,314✔
2548
  entry.version = version;
4,314✔
2549
  TABLE_RESET_ROLLUP(entry.flags);
4,314✔
2550
  entry.stbEntry.rsmaParam.uid = 0;
4,314✔
2551
  entry.stbEntry.rsmaParam.name = NULL;
4,314✔
2552
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,314✔
2553
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,314✔
2554
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,314✔
2555

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

2571
  metaFetchEntryFree(&pEntry);
4,314✔
2572
  TAOS_RETURN(code);
4,314✔
2573
}
2574

2575
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,504✔
2576
  int32_t code = TSDB_CODE_SUCCESS;
11,504✔
2577

2578
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,504✔
UNCOV
2579
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2580
              __LINE__, version);
UNCOV
2581
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2582
  }
2583

2584
  SMetaEntry *pEntry = NULL;
11,504✔
2585
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,504✔
2586
  if (code) {
11,504✔
UNCOV
2587
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2588
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2589
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2590
  }
2591

2592
  if (pEntry->uid != pReq->tbUid) {
11,504✔
UNCOV
2593
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2594
              ", version:%" PRId64,
2595
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2596
    metaFetchEntryFree(&pEntry);
×
UNCOV
2597
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2598
  }
2599

2600
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
10,066✔
2601
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
10,785✔
2602
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,223✔
2603
  } else {
UNCOV
2604
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2605
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2606
    metaFetchEntryFree(&pEntry);
×
2607
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2608
  }
2609

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

2629
  metaFetchEntryFree(&pEntry);
11,504✔
2630
  TAOS_RETURN(code);
11,504✔
2631
}
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