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

taosdata / TDengine / #3657

14 Mar 2025 08:10AM UTC coverage: 62.877% (+0.04%) from 62.841%
#3657

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

147792 of 302527 branches covered (48.85%)

Branch coverage included in aggregate %.

88 of 99 new or added lines in 12 files covered. (88.89%)

3160 existing lines in 17 files now uncovered.

232856 of 302857 relevant lines covered (76.89%)

6602224.33 hits per line

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

54.77
/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 metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
21
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
22
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
23
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
24

25
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
25,273✔
26
  int32_t   vgId = TD_VID(pMeta->pVnode);
25,273✔
27
  void     *value = NULL;
25,273✔
28
  int32_t   valueSize = 0;
25,273✔
29
  SMetaInfo info;
30

31
  // check name
32
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
25,273!
33
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
×
34
              pReq->name, version);
35
    return TSDB_CODE_INVALID_MSG;
×
36
  }
37

38
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
25,319✔
39
  if (r == 0) {  // name exists, check uid and type
25,316✔
40
    int64_t uid = *(tb_uid_t *)value;
81✔
41
    tdbFree(value);
81✔
42

43
    if (pReq->suid != uid) {
81✔
44
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
3!
45
                " version:%" PRId64,
46
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
47
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
3✔
48
    }
49

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

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

67
  // check suid
68
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
25,235✔
69
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
6!
70
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
71
    return TSDB_CODE_INVALID_MSG;
×
72
  }
73

74
  return TSDB_CODE_SUCCESS;
25,211✔
75
}
76

77
static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
2,624✔
78
  int32_t   code = TSDB_CODE_SUCCESS;
2,624✔
79
  void     *value = NULL;
2,624✔
80
  int32_t   valueSize = 0;
2,624✔
81
  SMetaInfo info;
82

83
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
2,624!
84
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
85
              __FILE__, __LINE__, pReq->name, version);
86
    return TSDB_CODE_INVALID_MSG;
×
87
  }
88

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

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

113
  return code;
2,624✔
114
}
115

116
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
2,384✔
117
  int32_t   code = TSDB_CODE_SUCCESS;
2,384✔
118
  void     *value = NULL;
2,384✔
119
  int32_t   valueSize = 0;
2,384✔
120
  SMetaInfo info;
121

122
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
2,384!
123
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
124
              __FILE__, __LINE__, pReq->name, version);
125
    return TSDB_CODE_INVALID_MSG;
×
126
  }
127

128
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
2,384✔
129
  if (code) {
2,385✔
130
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
3!
131
              __FILE__, __LINE__, pReq->name, version);
132
    return TSDB_CODE_TDB_STB_NOT_EXIST;
3✔
133
  } else {
134
    int64_t uid = *(int64_t *)value;
2,382✔
135
    tdbFreeClear(value);
2,382✔
136

137
    if (uid != pReq->suid) {
2,382✔
138
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " not match, version:%" PRId64,
2!
139
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
140
      return TSDB_CODE_TDB_STB_NOT_EXIST;
2✔
141
    }
142
  }
143

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

159
// Create Super Table
160
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
25,115✔
161
  int32_t code = TSDB_CODE_SUCCESS;
25,115✔
162

163
  // check request
164
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
25,115✔
165
  if (code != TSDB_CODE_SUCCESS) {
25,293✔
166
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
81✔
167
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
78!
168
               pReq->name, pReq->suid, version);
169
      TAOS_RETURN(TSDB_CODE_SUCCESS);
78✔
170
    } else {
171
      TAOS_RETURN(code);
3✔
172
    }
173
  }
174

175
  // handle entry
176
  SMetaEntry entry = {
25,212✔
177
      .version = version,
178
      .type = TSDB_SUPER_TABLE,
179
      .uid = pReq->suid,
25,212✔
180
      .name = pReq->name,
25,212✔
181
      .stbEntry.schemaRow = pReq->schemaRow,
182
      .stbEntry.schemaTag = pReq->schemaTag,
183
      .stbEntry.keep = pReq->keep,
25,212✔
184
  };
185
  if (pReq->rollup) {
25,212✔
186
    TABLE_SET_ROLLUP(entry.flags);
7✔
187
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
7✔
188
  }
189
  if (pReq->colCmpred) {
25,212✔
190
    TABLE_SET_COL_COMPRESSED(entry.flags);
25,171✔
191
    entry.colCmpr = pReq->colCmpr;
25,171✔
192
  }
193

194
  code = metaHandleEntry2(pMeta, &entry);
25,212✔
195
  if (TSDB_CODE_SUCCESS == code) {
25,292!
196
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
25,292!
197
             pReq->suid, version);
198
  } else {
UNCOV
199
    metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
200
              pReq->suid, tstrerror(code));
201
  }
202
  TAOS_RETURN(code);
25,292✔
203
}
204

205
// Drop Super Table
206
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
2,384✔
207
  int32_t code = TSDB_CODE_SUCCESS;
2,384✔
208

209
  // check request
210
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
2,384✔
211
  if (code) {
2,383✔
212
    TAOS_RETURN(code);
5✔
213
  }
214

215
  // handle entry
216
  SMetaEntry entry = {
2,378✔
217
      .version = verison,
218
      .type = -TSDB_SUPER_TABLE,
219
      .uid = pReq->suid,
2,378✔
220
  };
221
  code = metaHandleEntry2(pMeta, &entry);
2,378✔
222
  if (code) {
2,380!
UNCOV
223
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
224
              tstrerror(code));
225
  } else {
226
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
2,380!
227
             pReq->suid, verison);
228
  }
229
  TAOS_RETURN(code);
2,380✔
230
}
231

232
// Alter Super Table
233

234
// Create Child Table
235
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
113,917✔
236
  int32_t   code = TSDB_CODE_SUCCESS;
113,917✔
237
  void     *value = NULL;
113,917✔
238
  int32_t   valueSize = 0;
113,917✔
239
  SMetaInfo info;
240

241
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
113,917!
242
      pReq->ctb.suid == 0) {
113,920!
UNCOV
243
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
244
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
UNCOV
245
    return TSDB_CODE_INVALID_MSG;
×
246
  }
247

248
  // check table existence
249
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
113,924✔
250
    pReq->uid = *(int64_t *)value;
4,552✔
251
    tdbFreeClear(value);
4,552✔
252

253
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
4,552!
UNCOV
254
      metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
255
                ", which is an internal error, version:%" PRId64,
256
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
UNCOV
257
      return TSDB_CODE_INTERNAL_ERROR;
×
258
    }
259

260
    // check table type
261
    if (info.suid == info.uid || info.suid == 0) {
4,552!
262
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
9!
263
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
264
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
9✔
265
    }
266

267
    // check suid
268
    if (info.suid != pReq->ctb.suid) {
4,543!
UNCOV
269
      metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " exists in another stable with uid %" PRId64
×
270
                " instead of stable with uid %" PRId64 " version:%" PRId64,
271
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, info.suid, pReq->ctb.suid,
272
                version);
UNCOV
273
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
274
    }
275

276
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
4,543✔
277
  }
278

279
  // check super table existence
280
  if (tdbTbGet(pMeta->pNameIdx, pReq->ctb.stbName, strlen(pReq->ctb.stbName) + 1, &value, &valueSize) == 0) {
109,371!
281
    int64_t suid = *(int64_t *)value;
109,366✔
282
    tdbFreeClear(value);
109,366✔
283
    if (suid != pReq->ctb.suid) {
109,366!
UNCOV
284
      metaError("vgId:%d, %s failed at %s:%d since super table %s has uid %" PRId64 " instead of %" PRId64
×
285
                ", version:%" PRId64,
286
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, suid, pReq->ctb.suid, version);
UNCOV
287
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
288
    }
289
  } else {
UNCOV
290
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not eixst, version:%" PRId64,
×
291
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
UNCOV
292
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
293
  }
294

295
  // check super table is a super table
296
  if (metaGetInfo(pMeta, pReq->ctb.suid, &info, NULL) != TSDB_CODE_SUCCESS) {
109,366✔
297
    metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
4!
298
              ", which is an internal error, version:%" PRId64,
299
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.suid, version);
UNCOV
300
    return TSDB_CODE_INTERNAL_ERROR;
×
301
  } else if (info.suid != info.uid) {
109,352!
UNCOV
302
    metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
×
303
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.suid, version);
UNCOV
304
    return TSDB_CODE_INVALID_MSG;
×
305
  }
306

307
  // check grant
308
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
109,352!
309
    code = grantCheck(TSDB_GRANT_TIMESERIES);
109,362✔
310
    if (TSDB_CODE_SUCCESS != code) {
109,340!
UNCOV
311
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
312
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
313
    }
314
  }
315
  return code;
109,339✔
316
}
317

318
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
109,341✔
319
  int32_t code = TSDB_CODE_SUCCESS;
109,341✔
320

321
  if (NULL == ppRsp) {
109,341!
UNCOV
322
    return code;
×
323
  }
324

325
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
109,341!
326
  if (NULL == ppRsp) {
109,365!
UNCOV
327
    return terrno;
×
328
  }
329

330
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
109,365✔
331
  (*ppRsp)->tuid = pEntry->uid;
109,365✔
332
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
109,365✔
333
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
109,365✔
334

335
  return code;
109,365✔
336
}
337

338
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
113,918✔
339
  int32_t code = TSDB_CODE_SUCCESS;
113,918✔
340

341
  // check request
342
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
113,918✔
343
  if (code) {
113,890✔
344
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
4,552✔
345
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
9!
346
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
347
    }
348
    return code;
4,552✔
349
  }
350

351
  SMetaEntry entry = {
109,338✔
352
      .version = version,
353
      .type = TSDB_CHILD_TABLE,
354
      .uid = pReq->uid,
109,338✔
355
      .name = pReq->name,
109,338✔
356
      .ctbEntry.btime = pReq->btime,
109,338✔
357
      .ctbEntry.ttlDays = pReq->ttl,
109,338✔
358
      .ctbEntry.commentLen = pReq->commentLen,
109,338✔
359
      .ctbEntry.comment = pReq->comment,
109,338✔
360
      .ctbEntry.suid = pReq->ctb.suid,
109,338✔
361
      .ctbEntry.pTags = pReq->ctb.pTag,
109,338✔
362
  };
363

364
  // build response
365
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
109,338✔
366
  if (code) {
109,361!
UNCOV
367
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
368
              tstrerror(code));
369
  }
370

371
  // handle entry
372
  code = metaHandleEntry2(pMeta, &entry);
109,361✔
373
  if (TSDB_CODE_SUCCESS == code) {
109,356!
374
    metaInfo("vgId:%d, child table:%s uid %" PRId64 " suid:%" PRId64 " is created, version:%" PRId64,
109,356!
375
             TD_VID(pMeta->pVnode), pReq->name, pReq->uid, pReq->ctb.suid, version);
376
  } else {
UNCOV
377
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
378
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
379
              pReq->ctb.suid, version);
380
  }
381
  return code;
109,383✔
382
}
383

384
// Drop Child Table
385

386
// Alter Child Table
387

388
// Create Normal Table
389
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
13,684✔
390
  int32_t code = 0;
13,684✔
391
  void   *value = NULL;
13,684✔
392
  int32_t valueSize = 0;
13,684✔
393

394
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
13,684!
UNCOV
395
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
396
              __FILE__, __LINE__, pReq->name, version);
UNCOV
397
    return TSDB_CODE_INVALID_MSG;
×
398
  }
399

400
  // check name
401
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
13,684✔
402
    // for auto create table, we return the uid of the existing table
403
    pReq->uid = *(tb_uid_t *)value;
6✔
404
    tdbFree(value);
6✔
405
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
6✔
406
  }
407

408
  // grant check
409
  code = grantCheck(TSDB_GRANT_TIMESERIES);
13,676✔
410
  if (code) {
13,676!
UNCOV
411
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
412
              __FILE__, __LINE__, tstrerror(code), version, pReq->name);
413
  }
414
  return code;
13,676✔
415
}
416

417
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
13,677✔
418
  int32_t code = TSDB_CODE_SUCCESS;
13,677✔
419

420
  if (NULL == ppRsp) {
13,677!
UNCOV
421
    return code;
×
422
  }
423

424
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
13,677!
425
  if (NULL == *ppRsp) {
13,678!
UNCOV
426
    return terrno;
×
427
  }
428

429
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp);
13,678✔
430
  if (code) {
13,678!
UNCOV
431
    taosMemoryFreeClear(*ppRsp);
×
432
    return code;
×
433
  }
434

435
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
141,992✔
436
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
128,314✔
437
    (*ppRsp)->pSchemaExt[i].colId = p->id;
128,314✔
438
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
128,314✔
439
  }
440

441
  return code;
13,678✔
442
}
443

444
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
13,684✔
445
  int32_t code = TSDB_CODE_SUCCESS;
13,684✔
446

447
  // check request
448
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
13,684✔
449
  if (code) {
13,682✔
450
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
6!
UNCOV
451
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
452
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
453
    }
454
    TAOS_RETURN(code);
6✔
455
  }
456

457
  SMetaEntry entry = {
13,676✔
458
      .version = version,
459
      .type = TSDB_NORMAL_TABLE,
460
      .uid = pReq->uid,
13,676✔
461
      .name = pReq->name,
13,676✔
462
      .ntbEntry.btime = pReq->btime,
13,676✔
463
      .ntbEntry.ttlDays = pReq->ttl,
13,676✔
464
      .ntbEntry.commentLen = pReq->commentLen,
13,676✔
465
      .ntbEntry.comment = pReq->comment,
13,676✔
466
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
467
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
13,676✔
468
      .colCmpr = pReq->colCmpr,
469
  };
470
  TABLE_SET_COL_COMPRESSED(entry.flags);
13,676✔
471

472
  // build response
473
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
13,676✔
474
  if (code) {
13,678!
UNCOV
475
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
476
              tstrerror(code));
477
  }
478

479
  // handle entry
480
  code = metaHandleEntry2(pMeta, &entry);
13,678✔
481
  if (TSDB_CODE_SUCCESS == code) {
13,677!
482
    metaInfo("vgId:%d, normal table:%s uid %" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
13,677!
483
             pReq->uid, version);
484
  } else {
UNCOV
485
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
486
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
487
  }
488
  TAOS_RETURN(code);
13,678✔
489
}
490

491
// Drop Normal Table
492

493
// Alter Normal Table
494

495
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
127,601✔
496
  int32_t code = TSDB_CODE_SUCCESS;
127,601✔
497
  if (TSDB_CHILD_TABLE == pReq->type) {
127,601✔
498
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
113,920✔
499
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
13,681!
500
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
13,684✔
501
  } else {
UNCOV
502
    code = TSDB_CODE_INVALID_MSG;
×
503
  }
504
  TAOS_RETURN(code);
127,617✔
505
}
506

507
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
2,624✔
508
  int32_t code = TSDB_CODE_SUCCESS;
2,624✔
509

510
  // check request
511
  code = metaCheckDropTableReq(pMeta, version, pReq);
2,624✔
512
  if (code) {
2,624!
UNCOV
513
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
514
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
515
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
516
    }
UNCOV
517
    TAOS_RETURN(code);
×
518
  }
519

520
  if (pReq->suid == pReq->uid) {
2,624!
UNCOV
521
    code = TSDB_CODE_INVALID_PARA;
×
522
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
523
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
UNCOV
524
    TAOS_RETURN(code);
×
525
  }
526

527
  SMetaEntry entry = {
2,624✔
528
      .version = version,
529
      .uid = pReq->uid,
2,624✔
530
  };
531

532
  if (pReq->suid == 0) {
2,624✔
533
    entry.type = -TSDB_NORMAL_TABLE;
1,786✔
534
  } else {
535
    entry.type = -TSDB_CHILD_TABLE;
838✔
536
  }
537
  code = metaHandleEntry2(pMeta, &entry);
2,624✔
538
  if (code) {
2,624!
UNCOV
539
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
540
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
541
  } else {
542
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
2,624!
543
             pReq->uid, version);
544
  }
545
  TAOS_RETURN(code);
2,624✔
546
}
547

548
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
228✔
549
  int32_t code = 0;
228✔
550

551
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
228!
UNCOV
552
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
553
              __func__, __FILE__, __LINE__, pReq->colName, version);
UNCOV
554
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
555
  }
556

557
  // check name
558
  void   *value = NULL;
228✔
559
  int32_t valueSize = 0;
228✔
560
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
228✔
561
  if (code) {
228!
UNCOV
562
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
563
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
564
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
565
    TAOS_RETURN(code);
×
566
  }
567
  int64_t uid = *(int64_t *)value;
228✔
568
  tdbFreeClear(value);
228✔
569

570
  // check table type
571
  SMetaInfo info;
572
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
228!
UNCOV
573
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
574
              " not found, this is an internal error in meta, version:%" PRId64,
575
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
UNCOV
576
    code = TSDB_CODE_INTERNAL_ERROR;
×
577
    TAOS_RETURN(code);
×
578
  }
579
  if (info.suid != 0) {
228✔
580
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
2!
581
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
582
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
2✔
583
    TAOS_RETURN(code);
2✔
584
  }
585

586
  // check grant
587
  code = grantCheck(TSDB_GRANT_TIMESERIES);
226✔
588
  if (code) {
226!
UNCOV
589
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
590
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
UNCOV
591
    TAOS_RETURN(code);
×
592
  }
593
  TAOS_RETURN(code);
226✔
594
}
595

596
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
82✔
597
  int32_t code = TSDB_CODE_SUCCESS;
82✔
598

599
  // check request
600
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
82✔
601
  if (code) {
82!
UNCOV
602
    TAOS_RETURN(code);
×
603
  }
604

605
  // fetch old entry
606
  SMetaEntry *pEntry = NULL;
82✔
607
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
82✔
608
  if (code) {
82!
UNCOV
609
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
610
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
611
    TAOS_RETURN(code);
×
612
  }
613
  if (pEntry->version >= version) {
82!
UNCOV
614
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
615
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
616
    metaFetchEntryFree(&pEntry);
×
617
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
618
  }
619

620
  // do add column
621
  int32_t         rowSize = 0;
82✔
622
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
82✔
623
  SSchema        *pColumn;
624
  pEntry->version = version;
82✔
625
  for (int32_t i = 0; i < pSchema->nCols; i++) {
419✔
626
    pColumn = &pSchema->pSchema[i];
337✔
627
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
337!
UNCOV
628
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
×
629
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
UNCOV
630
      metaFetchEntryFree(&pEntry);
×
631
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
×
632
    }
633
    rowSize += pColumn->bytes;
337✔
634
  }
635

636
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
82!
UNCOV
637
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
638
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, TSDB_MAX_BYTES_PER_ROW, version);
UNCOV
639
    metaFetchEntryFree(&pEntry);
×
640
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
641
  }
642

643
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
82!
644
  if (NULL == pNewSchema) {
82!
UNCOV
645
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
646
              __LINE__, tstrerror(terrno), version);
UNCOV
647
    metaFetchEntryFree(&pEntry);
×
648
    TAOS_RETURN(terrno);
×
649
  }
650
  pSchema->pSchema = pNewSchema;
82✔
651
  pSchema->version++;
82✔
652
  pSchema->nCols++;
82✔
653
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
82✔
654
  pColumn->bytes = pReq->bytes;
82✔
655
  pColumn->type = pReq->type;
82✔
656
  pColumn->flags = pReq->flags;
82✔
657
  pColumn->colId = pEntry->ntbEntry.ncid++;
82✔
658
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
82✔
659
  uint32_t compress;
660
  if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
82✔
661
    compress = createDefaultColCmprByType(pColumn->type);
81✔
662
  } else {
663
    compress = pReq->compress;
1✔
664
  }
665
  code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
82✔
666
  if (code) {
82!
UNCOV
667
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
668
              __LINE__, tstrerror(code), version);
UNCOV
669
    metaFetchEntryFree(&pEntry);
×
670
    TAOS_RETURN(code);
×
671
  }
672

673
  // do handle entry
674
  code = metaHandleEntry2(pMeta, pEntry);
82✔
675
  if (code) {
82!
UNCOV
676
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
677
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
678
    metaFetchEntryFree(&pEntry);
×
679
    TAOS_RETURN(code);
×
680
  } else {
681
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
82!
682
             pEntry->uid, version);
683
  }
684

685
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
82!
UNCOV
686
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
687
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
688
  } else {
689
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
501✔
690
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
419✔
691
      pRsp->pSchemaExt[i].colId = p->id;
419✔
692
      pRsp->pSchemaExt[i].compress = p->alg;
419✔
693
    }
694
  }
695

696
  metaFetchEntryFree(&pEntry);
82✔
697
  TAOS_RETURN(code);
82✔
698
}
699

700
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
72✔
701
  int32_t code = TSDB_CODE_SUCCESS;
72✔
702

703
  // check request
704
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
72✔
705
  if (code) {
72✔
706
    TAOS_RETURN(code);
2✔
707
  }
708

709
  // fetch old entry
710
  SMetaEntry *pEntry = NULL;
70✔
711
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
70✔
712
  if (code) {
70!
UNCOV
713
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
714
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
715
    TAOS_RETURN(code);
×
716
  }
717

718
  if (pEntry->version >= version) {
70!
UNCOV
719
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
720
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
721
    metaFetchEntryFree(&pEntry);
×
722
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
723
  }
724

725
  // search the column to drop
726
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
70✔
727
  SSchema        *pColumn = NULL;
70✔
728
  SSchema         tColumn;
729
  int32_t         iColumn = 0;
70✔
730
  for (; iColumn < pSchema->nCols; iColumn++) {
223!
731
    pColumn = &pSchema->pSchema[iColumn];
223✔
732
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
223✔
733
      break;
70✔
734
    }
735
  }
736

737
  if (iColumn == pSchema->nCols) {
70!
UNCOV
738
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
739
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
UNCOV
740
    metaFetchEntryFree(&pEntry);
×
741
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
742
  }
743

744
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
70!
UNCOV
745
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
746
              __func__, __FILE__, __LINE__, pReq->colName, version);
UNCOV
747
    metaFetchEntryFree(&pEntry);
×
748
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
749
  }
750

751
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
70✔
752
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
753
              __func__, __FILE__, __LINE__, pReq->colName, version);
754
    metaFetchEntryFree(&pEntry);
11✔
755
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
11✔
756
  }
757
  tColumn = *pColumn;
59✔
758

759
  // do drop column
760
  pEntry->version = version;
59✔
761
  if (pSchema->nCols - iColumn - 1 > 0) {
59✔
762
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
42✔
763
  }
764
  pSchema->nCols--;
59✔
765
  pSchema->version++;
59✔
766
  code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
59✔
767
  if (code) {
59!
UNCOV
768
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
769
              __LINE__, tstrerror(code), version);
UNCOV
770
    metaFetchEntryFree(&pEntry);
×
771
    TAOS_RETURN(code);
×
772
  }
773
  if (pEntry->colCmpr.nCols != pSchema->nCols) {
59!
UNCOV
774
    metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
775
              __func__, __FILE__, __LINE__, version);
UNCOV
776
    metaFetchEntryFree(&pEntry);
×
777
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
778
  }
779

780
  // do handle entry
781
  code = metaHandleEntry2(pMeta, pEntry);
59✔
782
  if (code) {
59!
UNCOV
783
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
784
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
785
    metaFetchEntryFree(&pEntry);
×
786
  } else {
787
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
59!
788
             pEntry->uid, version);
789
  }
790

791
  // build response
792
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
59!
UNCOV
793
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
794
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
795
  } else {
796
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
313✔
797
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
254✔
798
      pRsp->pSchemaExt[i].colId = p->id;
254✔
799
      pRsp->pSchemaExt[i].compress = p->alg;
254✔
800
    }
801
  }
802

803
  metaFetchEntryFree(&pEntry);
59✔
804
  TAOS_RETURN(code);
59✔
805
}
806

807
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
32✔
808
  int32_t code = TSDB_CODE_SUCCESS;
32✔
809

810
  // check request
811
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
32✔
812
  if (code) {
32!
UNCOV
813
    TAOS_RETURN(code);
×
814
  }
815

816
  if (NULL == pReq->colNewName) {
32!
UNCOV
817
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
818
              __func__, __FILE__, __LINE__, version);
UNCOV
819
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
820
  }
821

822
  // fetch old entry
823
  SMetaEntry *pEntry = NULL;
32✔
824
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
32✔
825
  if (code) {
32!
UNCOV
826
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
827
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
828
    TAOS_RETURN(code);
×
829
  }
830

831
  if (pEntry->version >= version) {
32!
UNCOV
832
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
833
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
834
    metaFetchEntryFree(&pEntry);
×
835
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
836
  }
837

838
  // search the column to update
839
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
32✔
840
  SSchema        *pColumn = NULL;
32✔
841
  int32_t         iColumn = 0;
32✔
842
  for (int32_t i = 0; i < pSchema->nCols; i++) {
126!
843
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
126✔
844
      pColumn = &pSchema->pSchema[i];
32✔
845
      iColumn = i;
32✔
846
      break;
32✔
847
    }
848
  }
849

850
  if (NULL == pColumn) {
32!
UNCOV
851
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
852
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
UNCOV
853
    metaFetchEntryFree(&pEntry);
×
854
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
855
  }
856

857
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
32✔
858
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
859
              __func__, __FILE__, __LINE__, pColumn->name, version);
860
    metaFetchEntryFree(&pEntry);
11✔
861
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
11✔
862
  }
863

864
  // do update column name
865
  pEntry->version = version;
21✔
866
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
21✔
867
  pSchema->version++;
21✔
868

869
  // do handle entry
870
  code = metaHandleEntry2(pMeta, pEntry);
21✔
871
  if (code) {
21!
UNCOV
872
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
873
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
874
    metaFetchEntryFree(&pEntry);
×
875
    TAOS_RETURN(code);
×
876
  } else {
877
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
21!
878
             pEntry->uid, version);
879
  }
880

881
  // build response
882
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
21!
UNCOV
883
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
884
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
885
  } else {
886
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
121✔
887
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
100✔
888
      pRsp->pSchemaExt[i].colId = p->id;
100✔
889
      pRsp->pSchemaExt[i].compress = p->alg;
100✔
890
    }
891
  }
892

893
  metaFetchEntryFree(&pEntry);
21✔
894
  TAOS_RETURN(code);
21✔
895
}
896

897
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
42✔
898
  int32_t code = TSDB_CODE_SUCCESS;
42✔
899

900
  // check request
901
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
42✔
902
  if (code) {
42!
UNCOV
903
    TAOS_RETURN(code);
×
904
  }
905

906
  // fetch old entry
907
  SMetaEntry *pEntry = NULL;
42✔
908
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
42✔
909
  if (code) {
42!
UNCOV
910
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
911
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
912
    TAOS_RETURN(code);
×
913
  }
914

915
  if (pEntry->version >= version) {
42!
UNCOV
916
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
917
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
918
    metaFetchEntryFree(&pEntry);
×
919
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
920
  }
921

922
  // search the column to update
923
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
42✔
924
  SSchema        *pColumn = NULL;
42✔
925
  int32_t         iColumn = 0;
42✔
926
  int32_t         rowSize = 0;
42✔
927
  for (int32_t i = 0; i < pSchema->nCols; i++) {
200✔
928
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
158✔
929
      pColumn = &pSchema->pSchema[i];
42✔
930
      iColumn = i;
42✔
931
    }
932
    rowSize += pSchema->pSchema[i].bytes;
158✔
933
  }
934

935
  if (NULL == pColumn) {
42!
UNCOV
936
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
937
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
UNCOV
938
    metaFetchEntryFree(&pEntry);
×
939
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
940
  }
941

942
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
42!
UNCOV
943
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
×
944
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
945
              version);
UNCOV
946
    metaFetchEntryFree(&pEntry);
×
947
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
948
  }
949

950
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
42✔
951
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
13!
952
              __func__, __FILE__, __LINE__, pReq->colName, version);
953
    metaFetchEntryFree(&pEntry);
13✔
954
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
13✔
955
  }
956

957
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
29!
UNCOV
958
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
959
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW,
960
              version);
UNCOV
961
    metaFetchEntryFree(&pEntry);
×
962
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
963
  }
964

965
  // do change the column bytes
966
  pEntry->version = version;
29✔
967
  pSchema->version++;
29✔
968
  pColumn->bytes = pReq->colModBytes;
29✔
969

970
  // do handle entry
971
  code = metaHandleEntry2(pMeta, pEntry);
29✔
972
  if (code) {
29!
UNCOV
973
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
974
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
975
    metaFetchEntryFree(&pEntry);
×
976
    TAOS_RETURN(code);
×
977
  } else {
978
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
29!
979
             pEntry->uid, version);
980
  }
981

982
  // build response
983
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
29!
UNCOV
984
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
985
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
986
  } else {
987
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
137✔
988
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
108✔
989
      pRsp->pSchemaExt[i].colId = p->id;
108✔
990
      pRsp->pSchemaExt[i].compress = p->alg;
108✔
991
    }
992
  }
993

994
  metaFetchEntryFree(&pEntry);
29✔
995
  TAOS_RETURN(code);
29✔
996
}
997

998
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
654✔
999
  int32_t code = 0;
654✔
1000

1001
  // check tag name
1002
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
654!
UNCOV
1003
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1004
              __func__, __FILE__, __LINE__, pReq->tagName, version);
UNCOV
1005
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1006
  }
1007

1008
  // check name
1009
  void   *value = NULL;
654✔
1010
  int32_t valueSize = 0;
654✔
1011
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
654✔
1012
  if (code) {
654✔
1013
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1!
1014
              __FILE__, __LINE__, pReq->tbName, version);
1015
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1✔
1016
    TAOS_RETURN(code);
1✔
1017
  }
1018
  tdbFreeClear(value);
653✔
1019

1020
  TAOS_RETURN(code);
653✔
1021
}
1022

1023
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
654✔
1024
  int32_t code = TSDB_CODE_SUCCESS;
654✔
1025

1026
  // check request
1027
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
654✔
1028
  if (code) {
654✔
1029
    TAOS_RETURN(code);
1✔
1030
  }
1031

1032
  // fetch child entry
1033
  SMetaEntry *pChild = NULL;
653✔
1034
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
653✔
1035
  if (code) {
653!
UNCOV
1036
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1037
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1038
    TAOS_RETURN(code);
×
1039
  }
1040

1041
  if (pChild->type != TSDB_CHILD_TABLE) {
653!
UNCOV
1042
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1043
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1044
    metaFetchEntryFree(&pChild);
×
1045
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1046
  }
1047

1048
  // fetch super entry
1049
  SMetaEntry *pSuper = NULL;
653✔
1050
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
653✔
1051
  if (code) {
653!
UNCOV
1052
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1053
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
UNCOV
1054
    metaFetchEntryFree(&pChild);
×
1055
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1056
  }
1057

1058
  // search the tag to update
1059
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
653✔
1060
  SSchema        *pColumn = NULL;
653✔
1061
  int32_t         iColumn = 0;
653✔
1062
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
1,548!
1063
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
1,548✔
1064
      pColumn = &pTagSchema->pSchema[i];
653✔
1065
      iColumn = i;
653✔
1066
      break;
653✔
1067
    }
1068
  }
1069

1070
  if (NULL == pColumn) {
653!
UNCOV
1071
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1072
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
UNCOV
1073
    metaFetchEntryFree(&pChild);
×
1074
    metaFetchEntryFree(&pSuper);
×
1075
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1076
  }
1077

1078
  // do change tag value
1079
  pChild->version = version;
653✔
1080
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
653✔
1081
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
17!
1082
    if (NULL == pNewTag) {
17!
UNCOV
1083
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1084
                __LINE__, tstrerror(terrno), version);
UNCOV
1085
      metaFetchEntryFree(&pChild);
×
1086
      metaFetchEntryFree(&pSuper);
×
1087
      TAOS_RETURN(terrno);
×
1088
    }
1089
    pChild->ctbEntry.pTags = pNewTag;
17✔
1090
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
17✔
1091
  } else {
1092
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
636✔
1093

1094
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
636✔
1095
    if (NULL == pTagArray) {
636!
UNCOV
1096
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1097
                __LINE__, tstrerror(terrno), version);
UNCOV
1098
      metaFetchEntryFree(&pChild);
×
1099
      metaFetchEntryFree(&pSuper);
×
1100
      TAOS_RETURN(terrno);
×
1101
    }
1102

1103
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
2,705✔
1104
      STagVal value = {
2,069✔
1105
          .type = pTagSchema->pSchema[i].type,
2,069✔
1106
          .cid = pTagSchema->pSchema[i].colId,
2,069✔
1107
      };
1108

1109
      if (iColumn == i) {
2,069✔
1110
        if (pReq->isNull) {
636✔
1111
          continue;
413✔
1112
        }
1113
        if (IS_VAR_DATA_TYPE(value.type)) {
589!
1114
          value.pData = pReq->pTagVal;
205✔
1115
          value.nData = pReq->nTagVal;
205✔
1116
        } else {
1117
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
384✔
1118
        }
1119
      } else if (!tTagGet(pOldTag, &value)) {
1,433✔
1120
        continue;
366✔
1121
      }
1122

1123
      if (NULL == taosArrayPush(pTagArray, &value)) {
1,656!
UNCOV
1124
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1125
                  __LINE__, tstrerror(terrno), version);
UNCOV
1126
        taosArrayDestroy(pTagArray);
×
1127
        metaFetchEntryFree(&pChild);
×
1128
        metaFetchEntryFree(&pSuper);
×
1129
        TAOS_RETURN(terrno);
×
1130
      }
1131
    }
1132

1133
    STag *pNewTag = NULL;
636✔
1134
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
636✔
1135
    if (code) {
636!
UNCOV
1136
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1137
                __LINE__, tstrerror(code), version);
UNCOV
1138
      taosArrayDestroy(pTagArray);
×
1139
      metaFetchEntryFree(&pChild);
×
1140
      metaFetchEntryFree(&pSuper);
×
1141
      TAOS_RETURN(code);
×
1142
    }
1143
    taosArrayDestroy(pTagArray);
636✔
1144
    taosMemoryFree(pChild->ctbEntry.pTags);
636!
1145
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
636✔
1146
  }
1147

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

1161
  // free resource and return
1162
  metaFetchEntryFree(&pChild);
653✔
1163
  metaFetchEntryFree(&pSuper);
653✔
1164
  TAOS_RETURN(code);
653✔
1165
}
1166

1167
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1168
  int32_t code = 0;
5✔
1169

1170
  // check tag name
1171
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
UNCOV
1172
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1173
              __FILE__, __LINE__, version);
UNCOV
1174
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1175
  }
1176

1177
  // check name
1178
  void   *value = NULL;
5✔
1179
  int32_t valueSize = 0;
5✔
1180
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
5✔
1181
  if (code) {
5!
UNCOV
1182
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1183
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1184
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1185
    TAOS_RETURN(code);
×
1186
  }
1187
  tdbFreeClear(value);
5✔
1188

1189
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
UNCOV
1190
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1191
              __FILE__, __LINE__, version);
UNCOV
1192
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1193
  }
1194

1195
  TAOS_RETURN(code);
5✔
1196
}
1197

1198
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1199
  int32_t code = TSDB_CODE_SUCCESS;
5✔
1200

1201
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
5✔
1202
  if (code) {
5!
UNCOV
1203
    TAOS_RETURN(code);
×
1204
  }
1205

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

1215
  if (pChild->type != TSDB_CHILD_TABLE) {
5!
UNCOV
1216
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1217
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1218
    metaFetchEntryFree(&pChild);
×
1219
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1220
  }
1221

1222
  // fetch super entry
1223
  SMetaEntry *pSuper = NULL;
5✔
1224
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
5✔
1225
  if (code) {
5!
UNCOV
1226
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1227
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
UNCOV
1228
    metaFetchEntryFree(&pChild);
×
1229
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1230
  }
1231

1232
  // search the tags to update
1233
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
5✔
1234

1235
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5!
UNCOV
1236
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1237
              __func__, __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1238
    metaFetchEntryFree(&pChild);
×
1239
    metaFetchEntryFree(&pSuper);
×
1240
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1241
  }
1242

1243
  // do check if tag name exists
1244
  SHashObj *pTagTable =
1245
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5✔
1246
  if (pTagTable == NULL) {
5!
UNCOV
1247
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1248
              __LINE__, tstrerror(terrno), version);
UNCOV
1249
    metaFetchEntryFree(&pChild);
×
1250
    metaFetchEntryFree(&pSuper);
×
1251
    TAOS_RETURN(terrno);
×
1252
  }
1253

1254
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
35✔
1255
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
30✔
1256
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
30!
UNCOV
1257
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1258
                __LINE__, tstrerror(terrno), version);
UNCOV
1259
      taosHashCleanup(pTagTable);
×
1260
      metaFetchEntryFree(&pChild);
×
1261
      metaFetchEntryFree(&pSuper);
×
1262
      TAOS_RETURN(terrno);
×
1263
    }
1264
  }
1265

1266
  int32_t numOfChangedTags = 0;
5✔
1267
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1268
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
35✔
1269
        ? numOfChangedTags++
30✔
1270
        : 0;
35✔
1271
  }
1272
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
5!
UNCOV
1273
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1274
              __FILE__, __LINE__, version);
UNCOV
1275
    taosHashCleanup(pTagTable);
×
1276
    metaFetchEntryFree(&pChild);
×
1277
    metaFetchEntryFree(&pSuper);
×
1278
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1279
  }
1280

1281
  // do change tag value
1282
  pChild->version = version;
5✔
1283
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
5✔
1284
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
5✔
1285
  if (NULL == pTagArray) {
5!
UNCOV
1286
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1287
              __LINE__, tstrerror(terrno), version);
UNCOV
1288
    taosHashCleanup(pTagTable);
×
1289
    metaFetchEntryFree(&pChild);
×
1290
    metaFetchEntryFree(&pSuper);
×
1291
    TAOS_RETURN(terrno);
×
1292
  }
1293

1294
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1295
    SSchema *pCol = &pTagSchema->pSchema[i];
35✔
1296
    STagVal  value = {
35✔
1297
         .cid = pCol->colId,
35✔
1298
    };
1299

1300
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
35✔
1301
    if (pTagVal == NULL) {
35✔
1302
      if (!tTagGet(pOldTag, &value)) {
5!
1303
        continue;
6✔
1304
      }
1305
    } else {
1306
      value.type = pCol->type;
30✔
1307
      if (pTagVal->isNull) {
30✔
1308
        continue;
6✔
1309
      }
1310

1311
      if (IS_VAR_DATA_TYPE(pCol->type)) {
24!
1312
        value.pData = pTagVal->pTagVal;
4✔
1313
        value.nData = pTagVal->nTagVal;
4✔
1314
      } else {
1315
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
20✔
1316
      }
1317
    }
1318

1319
    if (taosArrayPush(pTagArray, &value) == NULL) {
29!
UNCOV
1320
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1321
                __LINE__, tstrerror(terrno), version);
UNCOV
1322
      taosHashCleanup(pTagTable);
×
1323
      taosArrayDestroy(pTagArray);
×
1324
      metaFetchEntryFree(&pChild);
×
1325
      metaFetchEntryFree(&pSuper);
×
1326
      TAOS_RETURN(terrno);
×
1327
    }
1328
  }
1329

1330
  STag *pNewTag = NULL;
5✔
1331
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
5✔
1332
  if (code) {
5!
UNCOV
1333
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1334
              __LINE__, tstrerror(code), version);
UNCOV
1335
    taosHashCleanup(pTagTable);
×
1336
    taosArrayDestroy(pTagArray);
×
1337
    metaFetchEntryFree(&pChild);
×
1338
    metaFetchEntryFree(&pSuper);
×
1339
    TAOS_RETURN(code);
×
1340
  }
1341
  taosArrayDestroy(pTagArray);
5✔
1342
  taosMemoryFree(pChild->ctbEntry.pTags);
5!
1343
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
5✔
1344

1345
  // do handle entry
1346
  code = metaHandleEntry2(pMeta, pChild);
5✔
1347
  if (code) {
5!
UNCOV
1348
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1349
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
UNCOV
1350
    taosHashCleanup(pTagTable);
×
1351
    metaFetchEntryFree(&pChild);
×
1352
    metaFetchEntryFree(&pSuper);
×
1353
    TAOS_RETURN(code);
×
1354
  } else {
1355
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5!
1356
             pChild->uid, version);
1357
  }
1358

1359
  taosHashCleanup(pTagTable);
5✔
1360
  metaFetchEntryFree(&pChild);
5✔
1361
  metaFetchEntryFree(&pSuper);
5✔
1362
  TAOS_RETURN(code);
5✔
1363
}
1364

1365
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
78✔
1366
  int32_t code = TSDB_CODE_SUCCESS;
78✔
1367

1368
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
78!
UNCOV
1369
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1370
              __FILE__, __LINE__, version);
UNCOV
1371
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1372
  }
1373

1374
  return code;
78✔
1375
}
1376

1377
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
78✔
1378
  int32_t code = 0;
78✔
1379

1380
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
78✔
1381
  if (code) {
78!
UNCOV
1382
    TAOS_RETURN(code);
×
1383
  }
1384

1385
  // fetch entry
1386
  SMetaEntry *pEntry = NULL;
78✔
1387
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
78✔
1388
  if (code) {
78!
UNCOV
1389
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1390
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1391
    TAOS_RETURN(code);
×
1392
  }
1393

1394
  // do change the entry
1395
  pEntry->version = version;
78✔
1396
  if (pEntry->type == TSDB_CHILD_TABLE) {
78✔
1397
    if (pReq->updateTTL) {
40✔
1398
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
15✔
1399
    }
1400
    if (pReq->newCommentLen >= 0) {
40✔
1401
      char *pNewComment = NULL;
25✔
1402
      if (pReq->newCommentLen) {
25✔
1403
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
20!
1404
        if (NULL == pNewComment) {
20!
UNCOV
1405
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1406
                    __LINE__, tstrerror(terrno), version);
UNCOV
1407
          metaFetchEntryFree(&pEntry);
×
1408
          TAOS_RETURN(terrno);
×
1409
        }
1410
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
20✔
1411
      } else {
1412
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
5!
1413
      }
1414
      pEntry->ctbEntry.comment = pNewComment;
25✔
1415
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
25✔
1416
    }
1417
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
38!
1418
    if (pReq->updateTTL) {
38✔
1419
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
12✔
1420
    }
1421
    if (pReq->newCommentLen >= 0) {
38✔
1422
      char *pNewComment = NULL;
26✔
1423
      if (pReq->newCommentLen > 0) {
26✔
1424
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
21!
1425
        if (NULL == pNewComment) {
21!
UNCOV
1426
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1427
                    __LINE__, tstrerror(terrno), version);
UNCOV
1428
          metaFetchEntryFree(&pEntry);
×
1429
          TAOS_RETURN(terrno);
×
1430
        }
1431
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
21✔
1432
      } else {
1433
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
5!
1434
      }
1435
      pEntry->ntbEntry.comment = pNewComment;
26✔
1436
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
26✔
1437
    }
1438
  } else {
UNCOV
1439
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1440
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
UNCOV
1441
    metaFetchEntryFree(&pEntry);
×
1442
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1443
  }
1444

1445
  // do handle entry
1446
  code = metaHandleEntry2(pMeta, pEntry);
78✔
1447
  if (code) {
78!
UNCOV
1448
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1449
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
1450
    metaFetchEntryFree(&pEntry);
×
1451
    TAOS_RETURN(code);
×
1452
  } else {
1453
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
78!
1454
             pEntry->uid, version);
1455
  }
1456

1457
  metaFetchEntryFree(&pEntry);
78✔
1458
  TAOS_RETURN(code);
78✔
1459
}
1460

1461
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1462
  int32_t code = TSDB_CODE_SUCCESS;
5✔
1463

1464
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5!
UNCOV
1465
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1466
              __FILE__, __LINE__, version);
UNCOV
1467
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1468
  }
1469

1470
  SMetaEntry *pEntry = NULL;
5✔
1471
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5✔
1472
  if (code) {
5!
UNCOV
1473
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1474
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1475
    TAOS_RETURN(code);
×
1476
  }
1477

1478
  if (pEntry->version >= version) {
5!
UNCOV
1479
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1480
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
1481
    metaFetchEntryFree(&pEntry);
×
1482
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1483
  }
1484

1485
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
5!
UNCOV
1486
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1487
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
UNCOV
1488
    metaFetchEntryFree(&pEntry);
×
1489
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1490
  }
1491

1492
  // do change the entry
1493
  int8_t           updated = 0;
5✔
1494
  SColCmprWrapper *wp = &pEntry->colCmpr;
5✔
1495
  for (int32_t i = 0; i < wp->nCols; i++) {
40✔
1496
    SColCmpr *p = &wp->pColCmpr[i];
35✔
1497
    if (p->id == pReq->colId) {
35✔
1498
      uint32_t dst = 0;
5✔
1499
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5✔
1500
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1501
      if (updated > 0) {
5!
1502
        p->alg = dst;
5✔
1503
      }
1504
    }
1505
  }
1506

1507
  if (updated == 0) {
5!
UNCOV
1508
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1509
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1510
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
UNCOV
1511
    metaFetchEntryFree(&pEntry);
×
1512
    TAOS_RETURN(code);
×
1513
  } else if (updated < 0) {
5!
UNCOV
1514
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1515
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1516
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
UNCOV
1517
    metaFetchEntryFree(&pEntry);
×
1518
    TAOS_RETURN(code);
×
1519
  }
1520

1521
  pEntry->version = version;
5✔
1522

1523
  // do handle entry
1524
  code = metaHandleEntry2(pMeta, pEntry);
5✔
1525
  if (code) {
5!
UNCOV
1526
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1527
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
1528
    metaFetchEntryFree(&pEntry);
×
1529
    TAOS_RETURN(code);
×
1530
  } else {
1531
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5!
1532
             pEntry->uid, version);
1533
  }
1534

1535
  metaFetchEntryFree(&pEntry);
5✔
1536
  TAOS_RETURN(code);
5✔
1537
}
1538

1539
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
27✔
1540
  int32_t code = TSDB_CODE_SUCCESS;
27✔
1541

1542
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
27!
UNCOV
1543
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1544
              __FILE__, __LINE__, version);
UNCOV
1545
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1546
  }
1547

1548
  SMetaEntry *pEntry = NULL;
27✔
1549
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
27✔
1550
  if (code) {
27!
UNCOV
1551
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1552
              __FILE__, __LINE__, pReq->name, version);
UNCOV
1553
    TAOS_RETURN(code);
×
1554
  }
1555

1556
  if (pEntry->type != TSDB_SUPER_TABLE) {
27!
UNCOV
1557
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1558
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
UNCOV
1559
    metaFetchEntryFree(&pEntry);
×
1560
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1561
  }
1562

1563
  if (pEntry->uid != pReq->suid) {
27!
UNCOV
1564
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
1565
              ", version:%" PRId64,
1566
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
UNCOV
1567
    metaFetchEntryFree(&pEntry);
×
1568
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1569
  }
1570

1571
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
1572
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
1573
  //   PRId64,
1574
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
1575
  //             pReq->schemaTag.version, version);
1576
  //   metaFetchEntryFree(&pEntry);
1577
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
1578
  // }
1579

1580
  // do change the entry
1581
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
27✔
1582
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
27✔
1583
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
27!
UNCOV
1584
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1585
              __func__, __FILE__, __LINE__, pReq->name, version);
UNCOV
1586
    metaFetchEntryFree(&pEntry);
×
1587
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1588
  }
1589

1590
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
27!
UNCOV
1591
    metaError(
×
1592
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
1593
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
1594
        version);
UNCOV
1595
    metaFetchEntryFree(&pEntry);
×
1596
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1597
  }
1598

1599
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
1600
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
1601
  //   PRId64,
1602
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
1603
  //             pNewTagSchema->version, version);
1604
  //   metaFetchEntryFree(&pEntry);
1605
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
1606
  // }
1607

1608
  int32_t numOfChangedTags = 0;
27✔
1609
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
140✔
1610
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
113✔
1611
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
113✔
1612

1613
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
113!
1614
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
113!
UNCOV
1615
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
1616
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
UNCOV
1617
      metaFetchEntryFree(&pEntry);
×
1618
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1619
    }
1620

1621
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
113✔
1622
      numOfChangedTags++;
27✔
1623
      SSCHMEA_SET_IDX_ON(pOldColumn);
27✔
1624
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
86!
UNCOV
1625
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
1626
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
UNCOV
1627
      metaFetchEntryFree(&pEntry);
×
1628
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1629
    }
1630
  }
1631

1632
  if (numOfChangedTags != 1) {
27!
UNCOV
1633
    metaError(
×
1634
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
1635
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
UNCOV
1636
    metaFetchEntryFree(&pEntry);
×
1637
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1638
  }
1639

1640
  pEntry->version = version;
27✔
1641
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
27✔
1642

1643
  // do handle the entry
1644
  code = metaHandleEntry2(pMeta, pEntry);
27✔
1645
  if (code) {
27!
UNCOV
1646
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1647
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
UNCOV
1648
    metaFetchEntryFree(&pEntry);
×
1649
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1650
  } else {
1651
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
27!
1652
             pEntry->uid, version);
1653
  }
1654

1655
  metaFetchEntryFree(&pEntry);
27✔
1656
  TAOS_RETURN(code);
27✔
1657
}
1658

1659
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
10✔
1660
  int32_t code = TSDB_CODE_SUCCESS;
10✔
1661

1662
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
10!
UNCOV
1663
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
1664
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
UNCOV
1665
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1666
  }
1667

1668
  SMetaEntry *pEntry = NULL;
10✔
1669
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
10✔
1670
  if (code) {
10!
UNCOV
1671
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1672
              __FILE__, __LINE__, pReq->stb, version);
UNCOV
1673
    TAOS_RETURN(code);
×
1674
  }
1675

1676
  if (TSDB_SUPER_TABLE != pEntry->type) {
10!
UNCOV
1677
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1678
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
UNCOV
1679
    metaFetchEntryFree(&pEntry);
×
1680
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1681
  }
1682

1683
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
10✔
1684
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
10!
UNCOV
1685
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1686
              __func__, __FILE__, __LINE__, pReq->stb, version);
UNCOV
1687
    metaFetchEntryFree(&pEntry);
×
1688
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1689
  }
1690

1691
  // search and set the tag index off
1692
  int32_t numOfChangedTags = 0;
10✔
1693
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
24!
1694
    SSchema *pCol = pTagSchema->pSchema + i;
24✔
1695
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
24✔
1696
      if (!IS_IDX_ON(pCol)) {
10!
UNCOV
1697
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
1698
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
UNCOV
1699
        metaFetchEntryFree(&pEntry);
×
1700
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1701
      }
1702
      numOfChangedTags++;
10✔
1703
      SSCHMEA_SET_IDX_OFF(pCol);
10✔
1704
      break;
10✔
1705
    }
1706
  }
1707

1708
  if (numOfChangedTags != 1) {
10!
UNCOV
1709
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
1710
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
UNCOV
1711
    metaFetchEntryFree(&pEntry);
×
1712
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1713
  }
1714

1715
  // do handle the entry
1716
  pEntry->version = version;
10✔
1717
  pTagSchema->version++;
10✔
1718
  code = metaHandleEntry2(pMeta, pEntry);
10✔
1719
  if (code) {
10!
UNCOV
1720
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1721
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
UNCOV
1722
    metaFetchEntryFree(&pEntry);
×
1723
    TAOS_RETURN(code);
×
1724
  } else {
1725
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
10!
1726
             pEntry->uid, version);
1727
  }
1728

1729
  metaFetchEntryFree(&pEntry);
10✔
1730
  TAOS_RETURN(code);
10✔
1731
}
1732

1733
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
6,555✔
1734
  int32_t code = TSDB_CODE_SUCCESS;
6,555✔
1735

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

1742
  SMetaEntry *pEntry = NULL;
6,570✔
1743
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
6,570✔
1744
  if (code) {
6,566✔
1745
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
10!
1746
              __FILE__, __LINE__, pReq->name, version);
1747
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
10✔
1748
  }
1749

1750
  if (pEntry->type != TSDB_SUPER_TABLE) {
6,556!
UNCOV
1751
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1752
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
UNCOV
1753
    metaFetchEntryFree(&pEntry);
×
1754
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1755
  }
1756

1757
  SMetaEntry entry = {
6,556✔
1758
      .version = version,
1759
      .type = TSDB_SUPER_TABLE,
1760
      .uid = pReq->suid,
6,556✔
1761
      .name = pReq->name,
6,556✔
1762
      .stbEntry.schemaRow = pReq->schemaRow,
1763
      .stbEntry.schemaTag = pReq->schemaTag,
1764
      .stbEntry.keep = pReq->keep,
6,556✔
1765
      .colCmpr = pReq->colCmpr,
1766
  };
1767
  TABLE_SET_COL_COMPRESSED(entry.flags);
6,556✔
1768

1769
  // do handle the entry
1770
  code = metaHandleEntry2(pMeta, &entry);
6,556✔
1771
  if (code) {
6,563!
UNCOV
1772
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1773
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
1774
    metaFetchEntryFree(&pEntry);
×
UNCOV
1775
    TAOS_RETURN(code);
×
1776
  } else {
1777
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
6,563!
1778
             pReq->suid, version);
1779
  }
1780

1781
  metaFetchEntryFree(&pEntry);
6,572✔
1782
  TAOS_RETURN(code);
6,576✔
1783
}
1784

UNCOV
1785
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
UNCOV
1786
  int32_t code = 0;
×
1787

1788
  if (taosArrayGetSize(uidArray) == 0) {
×
UNCOV
1789
    return TSDB_CODE_SUCCESS;
×
1790
  }
1791

UNCOV
1792
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
UNCOV
1793
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
1794
    SMetaInfo info;
1795
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
UNCOV
1796
    if (code) {
×
1797
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
1798
                __func__, __FILE__, __LINE__, uid, code);
1799
      return code;
×
1800
    }
1801

UNCOV
1802
    SMetaEntry entry = {
×
1803
        .version = version,
1804
        .uid = uid,
1805
    };
1806

UNCOV
1807
    if (info.suid == 0) {
×
UNCOV
1808
      entry.type = -TSDB_NORMAL_TABLE;
×
1809
    } else if (info.suid == uid) {
×
1810
      entry.type = -TSDB_SUPER_TABLE;
×
1811
    } else {
1812
      entry.type = -TSDB_CHILD_TABLE;
×
1813
    }
1814
    code = metaHandleEntry2(pMeta, &entry);
×
UNCOV
1815
    if (code) {
×
1816
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
1817
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
1818
      return code;
×
1819
    }
1820
  }
UNCOV
1821
  return code;
×
1822
}
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