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

taosdata / TDengine / #3620

21 Feb 2025 09:00AM UTC coverage: 63.573% (+0.2%) from 63.423%
#3620

push

travis-ci

web-flow
ci: taosBenchmark add coverage cases branch 3.0 (#29788)

* fix: add unit test for taos-tools

* fix: only .cpp include

* fix: remove no use function

* fix: restore toolsSys.c

* fix: add toolsSys case

* fix: rebuild error fixed

* fix: fix build error

* fix: support get vgroups with core and memory limit

* fix: build error for strcasecmp

* fix: add insertBasic.py case

* fix: add command line set vgroups=3

* fix: change with ns database

* toolscJson read with int replace float and add insertPrecison.py

* fix: add insertBindVGroup.json case

* fix: remove public fun removeQuotation

* fix: vgroups change method

* fix: memory leak for runInsertLimitThread slot

* insertPrecision.py word write wrong

* fix: check isFloat number

* fix: vgroups change logic error

* fix: insertBasic.py real and expect error

* fix: adjust default vgroups

* fix: adjust default vgroups modify comment

148962 of 300203 branches covered (49.62%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 1 file covered. (93.75%)

2018 existing lines in 133 files now uncovered.

233201 of 300933 relevant lines covered (77.49%)

18174406.98 hits per line

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

54.88
/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) {
28,589✔
26
  int32_t   vgId = TD_VID(pMeta->pVnode);
28,589✔
27
  void     *value = NULL;
28,589✔
28
  int32_t   valueSize = 0;
28,589✔
29
  SMetaInfo info;
30

31
  // check name
32
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
28,589!
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);
28,665✔
39
  if (r == 0) {  // name exists, check uid and type
28,585✔
40
    int64_t uid = *(tb_uid_t *)value;
34✔
41
    tdbFree(value);
34✔
42

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

50
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
30!
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) {
30!
58
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
30✔
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) {
28,551✔
69
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
1!
70
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
71
    return TSDB_CODE_INVALID_MSG;
×
72
  }
73

74
  return TSDB_CODE_SUCCESS;
28,547✔
75
}
76

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

83
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
2,567!
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,567✔
90
  if (TSDB_CODE_SUCCESS != code) {
2,567!
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,567✔
101
  tdbFreeClear(value);
2,567✔
102

103
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
2,567✔
104
  if (TSDB_CODE_SUCCESS != code) {
2,567!
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,567✔
112

113
  return code;
2,567✔
114
}
115

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

122
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
2,347!
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,348✔
129
  if (code) {
2,346✔
130
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1!
131
              __FILE__, __LINE__, pReq->name, version);
132
    return TSDB_CODE_TDB_STB_NOT_EXIST;
1✔
133
  } else {
134
    int64_t uid = *(int64_t *)value;
2,345✔
135
    tdbFreeClear(value);
2,345✔
136

137
    if (uid != pReq->suid) {
2,348✔
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,346✔
145
  if (code) {
2,343✔
146
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
1!
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,342!
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,342✔
157
}
158

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

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

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

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

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

208
  // check request
209
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
2,346✔
210
  if (code) {
2,345✔
211
    TAOS_RETURN(code);
3✔
212
  }
213

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

231
// Alter Super Table
232

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

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

247
  // check table existence
248
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
165,226✔
249
    pReq->uid = *(int64_t *)value;
27,177✔
250
    tdbFreeClear(value);
27,177✔
251

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

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

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

275
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
27,165✔
276
  }
277

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

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

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

317
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
138,022✔
318
  int32_t code = TSDB_CODE_SUCCESS;
138,022✔
319

320
  if (NULL == ppRsp) {
138,022!
321
    return code;
×
322
  }
323

324
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
138,022!
325
  if (NULL == ppRsp) {
138,061!
326
    return terrno;
×
327
  }
328

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

334
  return code;
138,061✔
335
}
336

337
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
165,215✔
338
  int32_t code = TSDB_CODE_SUCCESS;
165,215✔
339

340
  // check request
341
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
165,215✔
342
  if (code) {
165,208✔
343
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
27,173✔
344
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
9!
345
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
346
    }
347
    return code;
27,174✔
348
  }
349

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

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

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

383
// Drop Child Table
384

385
// Alter Child Table
386

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

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

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

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

416
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
14,460✔
417
  int32_t code = TSDB_CODE_SUCCESS;
14,460✔
418

419
  if (NULL == ppRsp) {
14,460!
UNCOV
420
    return code;
×
421
  }
422

423
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
14,460!
424
  if (NULL == *ppRsp) {
14,459!
UNCOV
425
    return terrno;
×
426
  }
427

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

434
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
163,047✔
435
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
148,588✔
436
    (*ppRsp)->pSchemaExt[i].colId = p->id;
148,588✔
437
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
148,588✔
438
  }
439

440
  return code;
14,459✔
441
}
442

443
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
14,466✔
444
  int32_t code = TSDB_CODE_SUCCESS;
14,466✔
445

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

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

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

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

490
// Drop Normal Table
491

492
// Alter Normal Table
493

494
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
179,687✔
495
  int32_t code = TSDB_CODE_SUCCESS;
179,687✔
496
  if (TSDB_CHILD_TABLE == pReq->type) {
179,687✔
497
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
165,228✔
498
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
14,459!
499
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
14,466✔
500
  } else {
UNCOV
501
    code = TSDB_CODE_INVALID_MSG;
×
502
  }
503
  TAOS_RETURN(code);
179,715✔
504
}
505

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1019
  TAOS_RETURN(code);
655✔
1020
}
1021

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1194
  TAOS_RETURN(code);
5✔
1195
}
1196

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1373
  return code;
80✔
1374
}
1375

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

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

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

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

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

1456
  metaFetchEntryFree(&pEntry);
80✔
1457
  TAOS_RETURN(code);
80✔
1458
}
1459

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

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

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

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

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

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

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

1520
  pEntry->version = version;
5✔
1521

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

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

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

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

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

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

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

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

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

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

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

1607
  int32_t numOfChangedTags = 0;
965✔
1608
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
15,148✔
1609
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
14,183✔
1610
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
14,183✔
1611

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

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

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

1639
  pEntry->version = version;
965✔
1640
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
965✔
1641

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

1654
  metaFetchEntryFree(&pEntry);
965✔
1655
  TAOS_RETURN(code);
965✔
1656
}
1657

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

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

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

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

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

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

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

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

1728
  metaFetchEntryFree(&pEntry);
2,140✔
1729
  TAOS_RETURN(code);
2,140✔
1730
}
1731

1732
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,448✔
1733
  int32_t code = TSDB_CODE_SUCCESS;
7,448✔
1734

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

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

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

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

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

1779
  metaFetchEntryFree(&pEntry);
7,472✔
1780
  TAOS_RETURN(code);
7,472✔
1781
}
1782

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

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

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

1800
    SMetaEntry entry = {
×
1801
        .version = version,
1802
        .uid = uid,
1803
    };
1804

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