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

taosdata / TDengine / #3654

14 Mar 2025 08:10AM UTC coverage: 36.951% (+14.4%) from 22.565%
#3654

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.

83566 of 302527 branches covered (27.62%)

Branch coverage included in aggregate %.

54 of 99 new or added lines in 12 files covered. (54.55%)

5128 existing lines in 33 files now uncovered.

140127 of 302857 relevant lines covered (46.27%)

678538.13 hits per line

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

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

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

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

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

74
  return TSDB_CODE_SUCCESS;
699✔
75
}
76

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

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

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

113
  return code;
×
114
}
115

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

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

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

144
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
4✔
145
  if (code) {
4!
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) {
4!
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;
4✔
157
}
158

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

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

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

194
  code = metaHandleEntry2(pMeta, &entry);
699✔
195
  if (TSDB_CODE_SUCCESS == code) {
700!
196
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
700!
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);
700✔
203
}
204

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

209
  // check request
210
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
4✔
211
  if (code) {
4!
UNCOV
212
    TAOS_RETURN(code);
×
213
  }
214

215
  // handle entry
216
  SMetaEntry entry = {
4✔
217
      .version = verison,
218
      .type = -TSDB_SUPER_TABLE,
219
      .uid = pReq->suid,
4✔
220
  };
221
  code = metaHandleEntry2(pMeta, &entry);
4✔
222
  if (code) {
4!
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,
4!
227
             pReq->suid, verison);
228
  }
229
  TAOS_RETURN(code);
4✔
230
}
231

232
// Alter Super Table
233

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

241
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
21,072!
242
      pReq->ctb.suid == 0) {
21,072!
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) {
21,072!
250
    pReq->uid = *(int64_t *)value;
×
251
    tdbFreeClear(value);
×
252

UNCOV
253
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
×
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
UNCOV
261
    if (info.suid == info.uid || info.suid == 0) {
×
262
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
×
263
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
UNCOV
264
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
265
    }
266

267
    // check suid
UNCOV
268
    if (info.suid != pReq->ctb.suid) {
×
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

UNCOV
276
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
277
  }
278

279
  // check super table existence
280
  if (tdbTbGet(pMeta->pNameIdx, pReq->ctb.stbName, strlen(pReq->ctb.stbName) + 1, &value, &valueSize) == 0) {
21,072!
281
    int64_t suid = *(int64_t *)value;
21,072✔
282
    tdbFreeClear(value);
21,072✔
283
    if (suid != pReq->ctb.suid) {
21,072!
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) {
21,072!
297
    metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
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) {
21,072!
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)) {
21,072!
309
    code = grantCheck(TSDB_GRANT_TIMESERIES);
21,072✔
310
    if (TSDB_CODE_SUCCESS != code) {
21,072!
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;
21,072✔
316
}
317

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

321
  if (NULL == ppRsp) {
21,072!
322
    return code;
×
323
  }
324

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

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

335
  return code;
21,072✔
336
}
337

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

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

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

364
  // build response
365
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
21,072✔
366
  if (code) {
21,072!
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);
21,072✔
373
  if (TSDB_CODE_SUCCESS == code) {
21,072!
374
    metaInfo("vgId:%d, child table:%s uid %" PRId64 " suid:%" PRId64 " is created, version:%" PRId64,
21,072!
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;
21,072✔
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) {
588✔
390
  int32_t code = 0;
588✔
391
  void   *value = NULL;
588✔
392
  int32_t valueSize = 0;
588✔
393

394
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
588!
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) {
588!
402
    // for auto create table, we return the uid of the existing table
UNCOV
403
    pReq->uid = *(tb_uid_t *)value;
×
404
    tdbFree(value);
×
405
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
406
  }
407

408
  // grant check
409
  code = grantCheck(TSDB_GRANT_TIMESERIES);
588✔
410
  if (code) {
588!
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;
588✔
415
}
416

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

420
  if (NULL == ppRsp) {
588!
421
    return code;
×
422
  }
423

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

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

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

441
  return code;
588✔
442
}
443

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

447
  // check request
448
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
588✔
449
  if (code) {
588!
450
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
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
    }
UNCOV
454
    TAOS_RETURN(code);
×
455
  }
456

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

472
  // build response
473
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
588✔
474
  if (code) {
588!
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);
588✔
481
  if (TSDB_CODE_SUCCESS == code) {
588!
482
    metaInfo("vgId:%d, normal table:%s uid %" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
588!
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);
588✔
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) {
21,660✔
496
  int32_t code = TSDB_CODE_SUCCESS;
21,660✔
497
  if (TSDB_CHILD_TABLE == pReq->type) {
21,660✔
498
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
21,072✔
499
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
588!
500
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
588✔
501
  } else {
UNCOV
502
    code = TSDB_CODE_INVALID_MSG;
×
503
  }
504
  TAOS_RETURN(code);
21,660✔
505
}
506

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

510
  // check request
UNCOV
511
  code = metaCheckDropTableReq(pMeta, version, pReq);
×
512
  if (code) {
×
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

UNCOV
520
  if (pReq->suid == pReq->uid) {
×
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

UNCOV
527
  SMetaEntry entry = {
×
528
      .version = version,
UNCOV
529
      .uid = pReq->uid,
×
530
  };
531

UNCOV
532
  if (pReq->suid == 0) {
×
533
    entry.type = -TSDB_NORMAL_TABLE;
×
534
  } else {
UNCOV
535
    entry.type = -TSDB_CHILD_TABLE;
×
536
  }
UNCOV
537
  code = metaHandleEntry2(pMeta, &entry);
×
538
  if (code) {
×
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 {
UNCOV
542
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
×
543
             pReq->uid, version);
544
  }
UNCOV
545
  TAOS_RETURN(code);
×
546
}
547

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

UNCOV
551
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
×
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
UNCOV
558
  void   *value = NULL;
×
559
  int32_t valueSize = 0;
×
560
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
561
  if (code) {
×
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
  }
UNCOV
567
  int64_t uid = *(int64_t *)value;
×
568
  tdbFreeClear(value);
×
569

570
  // check table type
571
  SMetaInfo info;
UNCOV
572
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
×
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
  }
UNCOV
579
  if (info.suid != 0) {
×
580
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
581
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
UNCOV
582
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
583
    TAOS_RETURN(code);
×
584
  }
585

586
  // check grant
UNCOV
587
  code = grantCheck(TSDB_GRANT_TIMESERIES);
×
588
  if (code) {
×
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
  }
UNCOV
593
  TAOS_RETURN(code);
×
594
}
595

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

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

605
  // fetch old entry
UNCOV
606
  SMetaEntry *pEntry = NULL;
×
607
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
608
  if (code) {
×
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
  }
UNCOV
613
  if (pEntry->version >= version) {
×
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
UNCOV
621
  int32_t         rowSize = 0;
×
622
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
623
  SSchema        *pColumn;
UNCOV
624
  pEntry->version = version;
×
625
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
626
    pColumn = &pSchema->pSchema[i];
×
627
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
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
    }
UNCOV
633
    rowSize += pColumn->bytes;
×
634
  }
635

UNCOV
636
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
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

UNCOV
643
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
×
644
  if (NULL == pNewSchema) {
×
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
  }
UNCOV
650
  pSchema->pSchema = pNewSchema;
×
651
  pSchema->version++;
×
652
  pSchema->nCols++;
×
653
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
×
654
  pColumn->bytes = pReq->bytes;
×
655
  pColumn->type = pReq->type;
×
656
  pColumn->flags = pReq->flags;
×
657
  pColumn->colId = pEntry->ntbEntry.ncid++;
×
658
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
×
659
  uint32_t compress;
UNCOV
660
  if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
×
661
    compress = createDefaultColCmprByType(pColumn->type);
×
662
  } else {
UNCOV
663
    compress = pReq->compress;
×
664
  }
UNCOV
665
  code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
×
666
  if (code) {
×
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
UNCOV
674
  code = metaHandleEntry2(pMeta, pEntry);
×
675
  if (code) {
×
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 {
UNCOV
681
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
682
             pEntry->uid, version);
683
  }
684

UNCOV
685
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
×
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 {
UNCOV
689
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
×
690
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
691
      pRsp->pSchemaExt[i].colId = p->id;
×
692
      pRsp->pSchemaExt[i].compress = p->alg;
×
693
    }
694
  }
695

UNCOV
696
  metaFetchEntryFree(&pEntry);
×
697
  TAOS_RETURN(code);
×
698
}
699

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

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

709
  // fetch old entry
UNCOV
710
  SMetaEntry *pEntry = NULL;
×
711
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
712
  if (code) {
×
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

UNCOV
718
  if (pEntry->version >= version) {
×
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
UNCOV
726
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
727
  SSchema        *pColumn = NULL;
×
728
  SSchema         tColumn;
UNCOV
729
  int32_t         iColumn = 0;
×
730
  for (; iColumn < pSchema->nCols; iColumn++) {
×
731
    pColumn = &pSchema->pSchema[iColumn];
×
732
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
733
      break;
×
734
    }
735
  }
736

UNCOV
737
  if (iColumn == pSchema->nCols) {
×
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

UNCOV
744
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
×
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

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

759
  // do drop column
UNCOV
760
  pEntry->version = version;
×
761
  if (pSchema->nCols - iColumn - 1 > 0) {
×
762
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
×
763
  }
UNCOV
764
  pSchema->nCols--;
×
765
  pSchema->version++;
×
766
  code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
×
767
  if (code) {
×
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
  }
UNCOV
773
  if (pEntry->colCmpr.nCols != pSchema->nCols) {
×
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
UNCOV
781
  code = metaHandleEntry2(pMeta, pEntry);
×
782
  if (code) {
×
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 {
UNCOV
787
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
788
             pEntry->uid, version);
789
  }
790

791
  // build response
UNCOV
792
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
×
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 {
UNCOV
796
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
×
797
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
798
      pRsp->pSchemaExt[i].colId = p->id;
×
799
      pRsp->pSchemaExt[i].compress = p->alg;
×
800
    }
801
  }
802

UNCOV
803
  metaFetchEntryFree(&pEntry);
×
804
  TAOS_RETURN(code);
×
805
}
806

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

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

UNCOV
816
  if (NULL == pReq->colNewName) {
×
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
UNCOV
823
  SMetaEntry *pEntry = NULL;
×
824
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
825
  if (code) {
×
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

UNCOV
831
  if (pEntry->version >= version) {
×
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
UNCOV
839
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
840
  SSchema        *pColumn = NULL;
×
841
  int32_t         iColumn = 0;
×
842
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
843
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
844
      pColumn = &pSchema->pSchema[i];
×
845
      iColumn = i;
×
846
      break;
×
847
    }
848
  }
849

UNCOV
850
  if (NULL == pColumn) {
×
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

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

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

869
  // do handle entry
UNCOV
870
  code = metaHandleEntry2(pMeta, pEntry);
×
871
  if (code) {
×
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 {
UNCOV
877
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
878
             pEntry->uid, version);
879
  }
880

881
  // build response
UNCOV
882
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
×
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 {
UNCOV
886
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
×
887
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
888
      pRsp->pSchemaExt[i].colId = p->id;
×
889
      pRsp->pSchemaExt[i].compress = p->alg;
×
890
    }
891
  }
892

UNCOV
893
  metaFetchEntryFree(&pEntry);
×
894
  TAOS_RETURN(code);
×
895
}
896

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

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

906
  // fetch old entry
UNCOV
907
  SMetaEntry *pEntry = NULL;
×
908
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
909
  if (code) {
×
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

UNCOV
915
  if (pEntry->version >= version) {
×
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
UNCOV
923
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
924
  SSchema        *pColumn = NULL;
×
925
  int32_t         iColumn = 0;
×
926
  int32_t         rowSize = 0;
×
927
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
928
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
929
      pColumn = &pSchema->pSchema[i];
×
930
      iColumn = i;
×
931
    }
UNCOV
932
    rowSize += pSchema->pSchema[i].bytes;
×
933
  }
934

UNCOV
935
  if (NULL == pColumn) {
×
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

UNCOV
942
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
×
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

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

UNCOV
957
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
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
UNCOV
966
  pEntry->version = version;
×
967
  pSchema->version++;
×
968
  pColumn->bytes = pReq->colModBytes;
×
969

970
  // do handle entry
UNCOV
971
  code = metaHandleEntry2(pMeta, pEntry);
×
972
  if (code) {
×
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 {
UNCOV
978
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
979
             pEntry->uid, version);
980
  }
981

982
  // build response
UNCOV
983
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
×
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 {
UNCOV
987
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
×
988
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
989
      pRsp->pSchemaExt[i].colId = p->id;
×
990
      pRsp->pSchemaExt[i].compress = p->alg;
×
991
    }
992
  }
993

UNCOV
994
  metaFetchEntryFree(&pEntry);
×
995
  TAOS_RETURN(code);
×
996
}
997

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

1001
  // check tag name
UNCOV
1002
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
×
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
UNCOV
1009
  void   *value = NULL;
×
1010
  int32_t valueSize = 0;
×
1011
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
1012
  if (code) {
×
1013
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1014
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1015
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1016
    TAOS_RETURN(code);
×
1017
  }
UNCOV
1018
  tdbFreeClear(value);
×
1019

UNCOV
1020
  TAOS_RETURN(code);
×
1021
}
1022

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

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

1032
  // fetch child entry
UNCOV
1033
  SMetaEntry *pChild = NULL;
×
1034
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
×
1035
  if (code) {
×
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

UNCOV
1041
  if (pChild->type != TSDB_CHILD_TABLE) {
×
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
UNCOV
1049
  SMetaEntry *pSuper = NULL;
×
1050
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
1051
  if (code) {
×
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
UNCOV
1059
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
×
1060
  SSchema        *pColumn = NULL;
×
1061
  int32_t         iColumn = 0;
×
1062
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
1063
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
×
1064
      pColumn = &pTagSchema->pSchema[i];
×
1065
      iColumn = i;
×
1066
      break;
×
1067
    }
1068
  }
1069

UNCOV
1070
  if (NULL == pColumn) {
×
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
UNCOV
1079
  pChild->version = version;
×
1080
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
1081
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
×
1082
    if (NULL == pNewTag) {
×
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
    }
UNCOV
1089
    pChild->ctbEntry.pTags = pNewTag;
×
1090
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
×
1091
  } else {
UNCOV
1092
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
×
1093

UNCOV
1094
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
1095
    if (NULL == pTagArray) {
×
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

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

UNCOV
1109
      if (iColumn == i) {
×
1110
        if (pReq->isNull) {
×
1111
          continue;
×
1112
        }
UNCOV
1113
        if (IS_VAR_DATA_TYPE(value.type)) {
×
1114
          value.pData = pReq->pTagVal;
×
1115
          value.nData = pReq->nTagVal;
×
1116
        } else {
UNCOV
1117
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
×
1118
        }
UNCOV
1119
      } else if (!tTagGet(pOldTag, &value)) {
×
1120
        continue;
×
1121
      }
1122

UNCOV
1123
      if (NULL == taosArrayPush(pTagArray, &value)) {
×
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

UNCOV
1133
    STag *pNewTag = NULL;
×
1134
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
×
1135
    if (code) {
×
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
    }
UNCOV
1143
    taosArrayDestroy(pTagArray);
×
1144
    taosMemoryFree(pChild->ctbEntry.pTags);
×
1145
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
×
1146
  }
1147

1148
  // do handle entry
UNCOV
1149
  code = metaHandleEntry2(pMeta, pChild);
×
1150
  if (code) {
×
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 {
UNCOV
1157
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1158
             pChild->uid, version);
1159
  }
1160

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

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

1170
  // check tag name
UNCOV
1171
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
×
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
UNCOV
1178
  void   *value = NULL;
×
1179
  int32_t valueSize = 0;
×
1180
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
1181
  if (code) {
×
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
  }
UNCOV
1187
  tdbFreeClear(value);
×
1188

UNCOV
1189
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
×
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

UNCOV
1195
  TAOS_RETURN(code);
×
1196
}
1197

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

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

1206
  // fetch child entry
UNCOV
1207
  SMetaEntry *pChild = NULL;
×
1208
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
×
1209
  if (code) {
×
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

UNCOV
1215
  if (pChild->type != TSDB_CHILD_TABLE) {
×
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
UNCOV
1223
  SMetaEntry *pSuper = NULL;
×
1224
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
1225
  if (code) {
×
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
UNCOV
1233
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
×
1234

UNCOV
1235
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
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 =
UNCOV
1245
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
×
1246
  if (pTagTable == NULL) {
×
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

UNCOV
1254
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
×
1255
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
×
1256
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
×
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

UNCOV
1266
  int32_t numOfChangedTags = 0;
×
1267
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
1268
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
×
1269
        ? numOfChangedTags++
×
1270
        : 0;
×
1271
  }
UNCOV
1272
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
×
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
UNCOV
1282
  pChild->version = version;
×
1283
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
×
1284
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
1285
  if (NULL == pTagArray) {
×
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

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

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

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

UNCOV
1319
    if (taosArrayPush(pTagArray, &value) == NULL) {
×
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

UNCOV
1330
  STag *pNewTag = NULL;
×
1331
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
×
1332
  if (code) {
×
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
  }
UNCOV
1341
  taosArrayDestroy(pTagArray);
×
1342
  taosMemoryFree(pChild->ctbEntry.pTags);
×
1343
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
×
1344

1345
  // do handle entry
UNCOV
1346
  code = metaHandleEntry2(pMeta, pChild);
×
1347
  if (code) {
×
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 {
UNCOV
1355
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1356
             pChild->uid, version);
1357
  }
1358

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

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

UNCOV
1368
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
×
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

UNCOV
1374
  return code;
×
1375
}
1376

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

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

1385
  // fetch entry
UNCOV
1386
  SMetaEntry *pEntry = NULL;
×
1387
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1388
  if (code) {
×
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
UNCOV
1395
  pEntry->version = version;
×
1396
  if (pEntry->type == TSDB_CHILD_TABLE) {
×
1397
    if (pReq->updateTTL) {
×
1398
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
×
1399
    }
UNCOV
1400
    if (pReq->newCommentLen >= 0) {
×
1401
      char *pNewComment = NULL;
×
1402
      if (pReq->newCommentLen) {
×
1403
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
×
1404
        if (NULL == pNewComment) {
×
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
        }
UNCOV
1410
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
×
1411
      } else {
UNCOV
1412
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
×
1413
      }
UNCOV
1414
      pEntry->ctbEntry.comment = pNewComment;
×
1415
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
×
1416
    }
UNCOV
1417
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
×
1418
    if (pReq->updateTTL) {
×
1419
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
×
1420
    }
UNCOV
1421
    if (pReq->newCommentLen >= 0) {
×
1422
      char *pNewComment = NULL;
×
1423
      if (pReq->newCommentLen > 0) {
×
1424
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
×
1425
        if (NULL == pNewComment) {
×
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
        }
UNCOV
1431
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
×
1432
      } else {
UNCOV
1433
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
×
1434
      }
UNCOV
1435
      pEntry->ntbEntry.comment = pNewComment;
×
1436
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
×
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
UNCOV
1446
  code = metaHandleEntry2(pMeta, pEntry);
×
1447
  if (code) {
×
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 {
UNCOV
1453
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1454
             pEntry->uid, version);
1455
  }
1456

UNCOV
1457
  metaFetchEntryFree(&pEntry);
×
1458
  TAOS_RETURN(code);
×
1459
}
1460

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

UNCOV
1464
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
×
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

UNCOV
1470
  SMetaEntry *pEntry = NULL;
×
1471
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1472
  if (code) {
×
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

UNCOV
1478
  if (pEntry->version >= version) {
×
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

UNCOV
1485
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
×
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
UNCOV
1493
  int8_t           updated = 0;
×
1494
  SColCmprWrapper *wp = &pEntry->colCmpr;
×
1495
  for (int32_t i = 0; i < wp->nCols; i++) {
×
1496
    SColCmpr *p = &wp->pColCmpr[i];
×
1497
    if (p->id == pReq->colId) {
×
1498
      uint32_t dst = 0;
×
1499
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
×
1500
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
UNCOV
1501
      if (updated > 0) {
×
1502
        p->alg = dst;
×
1503
      }
1504
    }
1505
  }
1506

UNCOV
1507
  if (updated == 0) {
×
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) {
×
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

UNCOV
1521
  pEntry->version = version;
×
1522

1523
  // do handle entry
UNCOV
1524
  code = metaHandleEntry2(pMeta, pEntry);
×
1525
  if (code) {
×
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 {
UNCOV
1531
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1532
             pEntry->uid, version);
1533
  }
1534

UNCOV
1535
  metaFetchEntryFree(&pEntry);
×
1536
  TAOS_RETURN(code);
×
1537
}
1538

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

UNCOV
1542
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
×
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

UNCOV
1548
  SMetaEntry *pEntry = NULL;
×
1549
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
×
1550
  if (code) {
×
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

UNCOV
1556
  if (pEntry->type != TSDB_SUPER_TABLE) {
×
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

UNCOV
1563
  if (pEntry->uid != pReq->suid) {
×
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
UNCOV
1581
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
×
1582
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
×
1583
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
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

UNCOV
1590
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
×
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

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

UNCOV
1613
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
×
1614
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
×
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

UNCOV
1621
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
×
1622
      numOfChangedTags++;
×
1623
      SSCHMEA_SET_IDX_ON(pOldColumn);
×
1624
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
×
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

UNCOV
1632
  if (numOfChangedTags != 1) {
×
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

UNCOV
1640
  pEntry->version = version;
×
1641
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
×
1642

1643
  // do handle the entry
UNCOV
1644
  code = metaHandleEntry2(pMeta, pEntry);
×
1645
  if (code) {
×
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 {
UNCOV
1651
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
×
1652
             pEntry->uid, version);
1653
  }
1654

UNCOV
1655
  metaFetchEntryFree(&pEntry);
×
1656
  TAOS_RETURN(code);
×
1657
}
1658

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

UNCOV
1662
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
×
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

UNCOV
1668
  SMetaEntry *pEntry = NULL;
×
1669
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
×
1670
  if (code) {
×
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

UNCOV
1676
  if (TSDB_SUPER_TABLE != pEntry->type) {
×
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

UNCOV
1683
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
×
1684
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
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
UNCOV
1692
  int32_t numOfChangedTags = 0;
×
1693
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
1694
    SSchema *pCol = pTagSchema->pSchema + i;
×
1695
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
×
1696
      if (!IS_IDX_ON(pCol)) {
×
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
      }
UNCOV
1702
      numOfChangedTags++;
×
1703
      SSCHMEA_SET_IDX_OFF(pCol);
×
1704
      break;
×
1705
    }
1706
  }
1707

UNCOV
1708
  if (numOfChangedTags != 1) {
×
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
UNCOV
1716
  pEntry->version = version;
×
1717
  pTagSchema->version++;
×
1718
  code = metaHandleEntry2(pMeta, pEntry);
×
1719
  if (code) {
×
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 {
UNCOV
1725
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
×
1726
             pEntry->uid, version);
1727
  }
1728

UNCOV
1729
  metaFetchEntryFree(&pEntry);
×
1730
  TAOS_RETURN(code);
×
1731
}
1732

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

1736
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
16!
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;
16✔
1743
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
16✔
1744
  if (code) {
16!
UNCOV
1745
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1746
              __FILE__, __LINE__, pReq->name, version);
UNCOV
1747
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
1748
  }
1749

1750
  if (pEntry->type != TSDB_SUPER_TABLE) {
16!
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 = {
16✔
1758
      .version = version,
1759
      .type = TSDB_SUPER_TABLE,
1760
      .uid = pReq->suid,
16✔
1761
      .name = pReq->name,
16✔
1762
      .stbEntry.schemaRow = pReq->schemaRow,
1763
      .stbEntry.schemaTag = pReq->schemaTag,
1764
      .stbEntry.keep = pReq->keep,
16✔
1765
      .colCmpr = pReq->colCmpr,
1766
  };
1767
  TABLE_SET_COL_COMPRESSED(entry.flags);
16✔
1768

1769
  // do handle the entry
1770
  code = metaHandleEntry2(pMeta, &entry);
16✔
1771
  if (code) {
16!
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,
16!
1778
             pReq->suid, version);
1779
  }
1780

1781
  metaFetchEntryFree(&pEntry);
16✔
1782
  TAOS_RETURN(code);
16✔
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