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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

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

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

UNCOV
43
    if (pReq->suid != uid) {
×
UNCOV
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);
UNCOV
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) {
460!
UNCOV
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;
460✔
75
}
76

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

UNCOV
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

UNCOV
89
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
×
UNCOV
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
  }
UNCOV
100
  pReq->uid = *(tb_uid_t *)value;
×
UNCOV
101
  tdbFreeClear(value);
×
102

UNCOV
103
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
×
UNCOV
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
  }
UNCOV
111
  pReq->suid = info.suid;
×
112

UNCOV
113
  return code;
×
114
}
115

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

122
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
2!
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✔
129
  if (code) {
2!
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;
2✔
135
    tdbFreeClear(value);
2✔
136

137
    if (uid != pReq->suid) {
2!
UNCOV
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);
UNCOV
140
      return TSDB_CODE_TDB_STB_NOT_EXIST;
×
141
    }
142
  }
143

144
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
2✔
145
  if (code) {
2!
UNCOV
146
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
147
              " not found, this is an internal error, version:%" PRId64,
148
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
149
    return TSDB_CODE_INTERNAL_ERROR;
×
150
  }
151
  if (info.suid != info.uid) {
2!
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✔
157
}
158

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

163
  // check request
164
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
459✔
165
  if (code != TSDB_CODE_SUCCESS) {
460!
UNCOV
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 {
UNCOV
171
      TAOS_RETURN(code);
×
172
    }
173
  }
174

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

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

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

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

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

231
// Alter Super Table
232

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

240
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
453!
241
      pReq->ctb.suid == 0) {
453!
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) {
453!
UNCOV
249
    pReq->uid = *(int64_t *)value;
×
UNCOV
250
    tdbFreeClear(value);
×
251

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

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

UNCOV
275
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
276
  }
277

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

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

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

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

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

334
  return code;
453✔
335
}
336

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

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

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

363
  // build response
364
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
453✔
365
  if (code) {
453!
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);
453✔
372
  if (TSDB_CODE_SUCCESS == code) {
453!
373
    metaInfo("vgId:%d, child table:%s uid %" PRId64 " suid:%" PRId64 " is created, version:%" PRId64,
453!
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;
453✔
381

382
#if 0
383
  metaTimeSeriesNotifyCheck(pMeta);
384
#endif
385
}
386

387
// Drop Child Table
388

389
// Alter Child Table
390

391
// Create Normal Table
392
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
113✔
393
  int32_t code = 0;
113✔
394
  void   *value = NULL;
113✔
395
  int32_t valueSize = 0;
113✔
396

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

403
  // check name
404
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
113!
405
    // for auto create table, we return the uid of the existing table
UNCOV
406
    pReq->uid = *(tb_uid_t *)value;
×
UNCOV
407
    tdbFree(value);
×
UNCOV
408
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
409
  }
410

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

420
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
113✔
421
  int32_t code = TSDB_CODE_SUCCESS;
113✔
422

423
  if (NULL == ppRsp) {
113!
424
    return code;
×
425
  }
426

427
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
113!
428
  if (NULL == *ppRsp) {
113!
429
    return terrno;
×
430
  }
431

432
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp);
113✔
433
  if (code) {
113!
434
    taosMemoryFreeClear(*ppRsp);
×
435
    return code;
×
436
  }
437

438
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
523✔
439
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
410✔
440
    (*ppRsp)->pSchemaExt[i].colId = p->id;
410✔
441
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
410✔
442
  }
443

444
  return code;
113✔
445
}
446

447
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
113✔
448
  int32_t code = TSDB_CODE_SUCCESS;
113✔
449

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

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

475
  // build response
476
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
113✔
477
  if (code) {
113!
478
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
479
              tstrerror(code));
480
  }
481

482
  // handle entry
483
  code = metaHandleEntry2(pMeta, &entry);
113✔
484
  if (TSDB_CODE_SUCCESS == code) {
113!
485
    metaInfo("vgId:%d, normal table:%s uid %" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
113!
486
             pReq->uid, version);
487
  } else {
488
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
489
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
490
  }
491
  TAOS_RETURN(code);
113✔
492
#if 0
493
  metaTimeSeriesNotifyCheck(pMeta);
494
#endif
495
}
496

497
// Drop Normal Table
498

499
// Alter Normal Table
500

501
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
566✔
502
  int32_t code = TSDB_CODE_SUCCESS;
566✔
503
  if (TSDB_CHILD_TABLE == pReq->type) {
566✔
504
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
453✔
505
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
113!
506
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
113✔
507
  } else {
508
    code = TSDB_CODE_INVALID_MSG;
×
509
  }
510
  TAOS_RETURN(code);
566✔
511
}
512

UNCOV
513
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
×
UNCOV
514
  int32_t code = TSDB_CODE_SUCCESS;
×
515

516
  // check request
UNCOV
517
  code = metaCheckDropTableReq(pMeta, version, pReq);
×
UNCOV
518
  if (code) {
×
519
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
520
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
521
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
522
    }
523
    TAOS_RETURN(code);
×
524
  }
525

UNCOV
526
  if (pReq->suid == pReq->uid) {
×
527
    code = TSDB_CODE_INVALID_PARA;
×
528
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
529
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
530
    TAOS_RETURN(code);
×
531
  }
532

UNCOV
533
  SMetaEntry entry = {
×
534
      .version = version,
UNCOV
535
      .uid = pReq->uid,
×
536
  };
537

UNCOV
538
  if (pReq->suid == 0) {
×
UNCOV
539
    entry.type = -TSDB_NORMAL_TABLE;
×
540
  } else {
UNCOV
541
    entry.type = -TSDB_CHILD_TABLE;
×
542
  }
UNCOV
543
  code = metaHandleEntry2(pMeta, &entry);
×
UNCOV
544
  if (code) {
×
545
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
546
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
547
  } else {
UNCOV
548
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
×
549
             pReq->uid, version);
550
  }
UNCOV
551
  TAOS_RETURN(code);
×
552
}
553

UNCOV
554
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
555
  int32_t code = 0;
×
556

UNCOV
557
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
×
558
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
559
              __func__, __FILE__, __LINE__, pReq->colName, version);
560
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
561
  }
562

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

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

592
  // check grant
UNCOV
593
  code = grantCheck(TSDB_GRANT_TIMESERIES);
×
UNCOV
594
  if (code) {
×
595
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
596
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
597
    TAOS_RETURN(code);
×
598
  }
UNCOV
599
  TAOS_RETURN(code);
×
600
}
601

UNCOV
602
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
UNCOV
603
  int32_t code = TSDB_CODE_SUCCESS;
×
604

605
  // check request
UNCOV
606
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
UNCOV
607
  if (code) {
×
608
    TAOS_RETURN(code);
×
609
  }
610

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

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

UNCOV
642
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
643
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
644
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, TSDB_MAX_BYTES_PER_ROW, version);
645
    metaFetchEntryFree(&pEntry);
×
646
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
647
  }
648

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

679
  // do handle entry
UNCOV
680
  code = metaHandleEntry2(pMeta, pEntry);
×
UNCOV
681
  if (code) {
×
682
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
683
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
684
    metaFetchEntryFree(&pEntry);
×
685
    TAOS_RETURN(code);
×
686
  } else {
UNCOV
687
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
688
             pEntry->uid, version);
689
  }
690

UNCOV
691
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
×
692
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
693
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
694
  } else {
UNCOV
695
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
×
UNCOV
696
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
UNCOV
697
      pRsp->pSchemaExt[i].colId = p->id;
×
UNCOV
698
      pRsp->pSchemaExt[i].compress = p->alg;
×
699
    }
700
  }
701

UNCOV
702
  metaFetchEntryFree(&pEntry);
×
UNCOV
703
  TAOS_RETURN(code);
×
704
}
705

UNCOV
706
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
UNCOV
707
  int32_t code = TSDB_CODE_SUCCESS;
×
708

709
  // check request
UNCOV
710
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
UNCOV
711
  if (code) {
×
UNCOV
712
    TAOS_RETURN(code);
×
713
  }
714

715
  // fetch old entry
UNCOV
716
  SMetaEntry *pEntry = NULL;
×
UNCOV
717
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
UNCOV
718
  if (code) {
×
719
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
720
              __FILE__, __LINE__, pReq->tbName, version);
721
    TAOS_RETURN(code);
×
722
  }
723

UNCOV
724
  if (pEntry->version >= version) {
×
725
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
726
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
727
    metaFetchEntryFree(&pEntry);
×
728
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
729
  }
730

731
  // search the column to drop
UNCOV
732
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
UNCOV
733
  SSchema        *pColumn = NULL;
×
734
  SSchema         tColumn;
UNCOV
735
  int32_t         iColumn = 0;
×
UNCOV
736
  for (; iColumn < pSchema->nCols; iColumn++) {
×
UNCOV
737
    pColumn = &pSchema->pSchema[iColumn];
×
UNCOV
738
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
UNCOV
739
      break;
×
740
    }
741
  }
742

UNCOV
743
  if (iColumn == pSchema->nCols) {
×
744
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
745
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
746
    metaFetchEntryFree(&pEntry);
×
747
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
748
  }
749

UNCOV
750
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
×
751
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
752
              __func__, __FILE__, __LINE__, pReq->colName, version);
753
    metaFetchEntryFree(&pEntry);
×
754
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
755
  }
756

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

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

786
  // do handle entry
UNCOV
787
  code = metaHandleEntry2(pMeta, pEntry);
×
UNCOV
788
  if (code) {
×
789
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
790
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
791
    metaFetchEntryFree(&pEntry);
×
792
  } else {
UNCOV
793
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
794
             pEntry->uid, version);
795
  }
796

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

UNCOV
809
  metaFetchEntryFree(&pEntry);
×
UNCOV
810
  TAOS_RETURN(code);
×
811
}
812

UNCOV
813
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
UNCOV
814
  int32_t code = TSDB_CODE_SUCCESS;
×
815

816
  // check request
UNCOV
817
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
UNCOV
818
  if (code) {
×
819
    TAOS_RETURN(code);
×
820
  }
821

UNCOV
822
  if (NULL == pReq->colNewName) {
×
823
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
824
              __func__, __FILE__, __LINE__, version);
825
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
826
  }
827

828
  // fetch old entry
UNCOV
829
  SMetaEntry *pEntry = NULL;
×
UNCOV
830
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
UNCOV
831
  if (code) {
×
832
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
833
              __FILE__, __LINE__, pReq->tbName, version);
834
    TAOS_RETURN(code);
×
835
  }
836

UNCOV
837
  if (pEntry->version >= version) {
×
838
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
839
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
840
    metaFetchEntryFree(&pEntry);
×
841
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
842
  }
843

844
  // search the column to update
UNCOV
845
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
UNCOV
846
  SSchema        *pColumn = NULL;
×
UNCOV
847
  int32_t         iColumn = 0;
×
UNCOV
848
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
UNCOV
849
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
UNCOV
850
      pColumn = &pSchema->pSchema[i];
×
UNCOV
851
      iColumn = i;
×
UNCOV
852
      break;
×
853
    }
854
  }
855

UNCOV
856
  if (NULL == pColumn) {
×
857
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
858
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
859
    metaFetchEntryFree(&pEntry);
×
860
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
861
  }
862

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

870
  // do update column name
UNCOV
871
  pEntry->version = version;
×
UNCOV
872
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
×
UNCOV
873
  pSchema->version++;
×
874

875
  // do handle entry
UNCOV
876
  code = metaHandleEntry2(pMeta, pEntry);
×
UNCOV
877
  if (code) {
×
878
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
879
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
880
    metaFetchEntryFree(&pEntry);
×
881
    TAOS_RETURN(code);
×
882
  } else {
UNCOV
883
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
884
             pEntry->uid, version);
885
  }
886

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

UNCOV
899
  metaFetchEntryFree(&pEntry);
×
UNCOV
900
  TAOS_RETURN(code);
×
901
}
902

UNCOV
903
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
UNCOV
904
  int32_t code = TSDB_CODE_SUCCESS;
×
905

906
  // check request
UNCOV
907
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
UNCOV
908
  if (code) {
×
909
    TAOS_RETURN(code);
×
910
  }
911

912
  // fetch old entry
UNCOV
913
  SMetaEntry *pEntry = NULL;
×
UNCOV
914
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
UNCOV
915
  if (code) {
×
916
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
917
              __FILE__, __LINE__, pReq->tbName, version);
918
    TAOS_RETURN(code);
×
919
  }
920

UNCOV
921
  if (pEntry->version >= version) {
×
922
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
923
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
924
    metaFetchEntryFree(&pEntry);
×
925
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
926
  }
927

928
  // search the column to update
UNCOV
929
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
UNCOV
930
  SSchema        *pColumn = NULL;
×
UNCOV
931
  int32_t         iColumn = 0;
×
UNCOV
932
  int32_t         rowSize = 0;
×
UNCOV
933
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
UNCOV
934
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
UNCOV
935
      pColumn = &pSchema->pSchema[i];
×
UNCOV
936
      iColumn = i;
×
937
    }
UNCOV
938
    rowSize += pSchema->pSchema[i].bytes;
×
939
  }
940

UNCOV
941
  if (NULL == pColumn) {
×
942
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
943
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
944
    metaFetchEntryFree(&pEntry);
×
945
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
946
  }
947

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

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

UNCOV
963
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
964
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
965
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW,
966
              version);
967
    metaFetchEntryFree(&pEntry);
×
968
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
969
  }
970

971
  // do change the column bytes
UNCOV
972
  pEntry->version = version;
×
UNCOV
973
  pSchema->version++;
×
UNCOV
974
  pColumn->bytes = pReq->colModBytes;
×
975

976
  // do handle entry
UNCOV
977
  code = metaHandleEntry2(pMeta, pEntry);
×
UNCOV
978
  if (code) {
×
979
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
980
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
981
    metaFetchEntryFree(&pEntry);
×
982
    TAOS_RETURN(code);
×
983
  } else {
UNCOV
984
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
985
             pEntry->uid, version);
986
  }
987

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

UNCOV
1000
  metaFetchEntryFree(&pEntry);
×
UNCOV
1001
  TAOS_RETURN(code);
×
1002
}
1003

UNCOV
1004
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
1005
  int32_t code = 0;
×
1006

1007
  // check tag name
UNCOV
1008
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
×
1009
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1010
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1011
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1012
  }
1013

1014
  // check name
UNCOV
1015
  void   *value = NULL;
×
UNCOV
1016
  int32_t valueSize = 0;
×
UNCOV
1017
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
UNCOV
1018
  if (code) {
×
UNCOV
1019
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1020
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1021
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
UNCOV
1022
    TAOS_RETURN(code);
×
1023
  }
UNCOV
1024
  tdbFreeClear(value);
×
1025

UNCOV
1026
  TAOS_RETURN(code);
×
1027
}
1028

UNCOV
1029
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
1030
  int32_t code = TSDB_CODE_SUCCESS;
×
1031

1032
  // check request
UNCOV
1033
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
×
UNCOV
1034
  if (code) {
×
UNCOV
1035
    TAOS_RETURN(code);
×
1036
  }
1037

1038
  // fetch child entry
UNCOV
1039
  SMetaEntry *pChild = NULL;
×
UNCOV
1040
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
×
UNCOV
1041
  if (code) {
×
1042
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1043
              __FILE__, __LINE__, pReq->tbName, version);
1044
    TAOS_RETURN(code);
×
1045
  }
1046

UNCOV
1047
  if (pChild->type != TSDB_CHILD_TABLE) {
×
1048
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1049
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1050
    metaFetchEntryFree(&pChild);
×
1051
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1052
  }
1053

1054
  // fetch super entry
UNCOV
1055
  SMetaEntry *pSuper = NULL;
×
UNCOV
1056
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
UNCOV
1057
  if (code) {
×
1058
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1059
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1060
    metaFetchEntryFree(&pChild);
×
1061
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1062
  }
1063

1064
  // search the tag to update
UNCOV
1065
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
×
UNCOV
1066
  SSchema        *pColumn = NULL;
×
UNCOV
1067
  int32_t         iColumn = 0;
×
UNCOV
1068
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
UNCOV
1069
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
×
UNCOV
1070
      pColumn = &pTagSchema->pSchema[i];
×
UNCOV
1071
      iColumn = i;
×
UNCOV
1072
      break;
×
1073
    }
1074
  }
1075

UNCOV
1076
  if (NULL == pColumn) {
×
1077
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1078
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1079
    metaFetchEntryFree(&pChild);
×
1080
    metaFetchEntryFree(&pSuper);
×
1081
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1082
  }
1083

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

UNCOV
1100
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
UNCOV
1101
    if (NULL == pTagArray) {
×
1102
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1103
                __LINE__, tstrerror(terrno), version);
1104
      metaFetchEntryFree(&pChild);
×
1105
      metaFetchEntryFree(&pSuper);
×
1106
      TAOS_RETURN(terrno);
×
1107
    }
1108

UNCOV
1109
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
UNCOV
1110
      STagVal value = {
×
UNCOV
1111
          .type = pTagSchema->pSchema[i].type,
×
UNCOV
1112
          .cid = pTagSchema->pSchema[i].colId,
×
1113
      };
1114

UNCOV
1115
      if (iColumn == i) {
×
UNCOV
1116
        if (pReq->isNull) {
×
UNCOV
1117
          continue;
×
1118
        }
UNCOV
1119
        if (IS_VAR_DATA_TYPE(value.type)) {
×
UNCOV
1120
          value.pData = pReq->pTagVal;
×
UNCOV
1121
          value.nData = pReq->nTagVal;
×
1122
        } else {
UNCOV
1123
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
×
1124
        }
UNCOV
1125
      } else if (!tTagGet(pOldTag, &value)) {
×
UNCOV
1126
        continue;
×
1127
      }
1128

UNCOV
1129
      if (NULL == taosArrayPush(pTagArray, &value)) {
×
1130
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1131
                  __LINE__, tstrerror(terrno), version);
1132
        taosArrayDestroy(pTagArray);
×
1133
        metaFetchEntryFree(&pChild);
×
1134
        metaFetchEntryFree(&pSuper);
×
1135
        TAOS_RETURN(terrno);
×
1136
      }
1137
    }
1138

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

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

1167
  // free resource and return
UNCOV
1168
  metaFetchEntryFree(&pChild);
×
UNCOV
1169
  metaFetchEntryFree(&pSuper);
×
UNCOV
1170
  TAOS_RETURN(code);
×
1171
}
1172

UNCOV
1173
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
1174
  int32_t code = 0;
×
1175

1176
  // check tag name
UNCOV
1177
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
×
1178
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1179
              __FILE__, __LINE__, version);
1180
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1181
  }
1182

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

UNCOV
1195
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
×
1196
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1197
              __FILE__, __LINE__, version);
1198
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1199
  }
1200

UNCOV
1201
  TAOS_RETURN(code);
×
1202
}
1203

UNCOV
1204
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
1205
  int32_t code = TSDB_CODE_SUCCESS;
×
1206

UNCOV
1207
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
×
UNCOV
1208
  if (code) {
×
1209
    TAOS_RETURN(code);
×
1210
  }
1211

1212
  // fetch child entry
UNCOV
1213
  SMetaEntry *pChild = NULL;
×
UNCOV
1214
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
×
UNCOV
1215
  if (code) {
×
1216
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1217
              __FILE__, __LINE__, pReq->tbName, version);
1218
    TAOS_RETURN(code);
×
1219
  }
1220

UNCOV
1221
  if (pChild->type != TSDB_CHILD_TABLE) {
×
1222
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1223
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1224
    metaFetchEntryFree(&pChild);
×
1225
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1226
  }
1227

1228
  // fetch super entry
UNCOV
1229
  SMetaEntry *pSuper = NULL;
×
UNCOV
1230
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
UNCOV
1231
  if (code) {
×
1232
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1233
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1234
    metaFetchEntryFree(&pChild);
×
1235
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1236
  }
1237

1238
  // search the tags to update
UNCOV
1239
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
×
1240

UNCOV
1241
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
1242
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1243
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1244
    metaFetchEntryFree(&pChild);
×
1245
    metaFetchEntryFree(&pSuper);
×
1246
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1247
  }
1248

1249
  // do check if tag name exists
1250
  SHashObj *pTagTable =
UNCOV
1251
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
×
UNCOV
1252
  if (pTagTable == NULL) {
×
1253
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1254
              __LINE__, tstrerror(terrno), version);
1255
    metaFetchEntryFree(&pChild);
×
1256
    metaFetchEntryFree(&pSuper);
×
1257
    TAOS_RETURN(terrno);
×
1258
  }
1259

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

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

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

UNCOV
1300
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
UNCOV
1301
    SSchema *pCol = &pTagSchema->pSchema[i];
×
UNCOV
1302
    STagVal  value = {
×
UNCOV
1303
         .cid = pCol->colId,
×
1304
    };
1305

UNCOV
1306
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
×
UNCOV
1307
    if (pTagVal == NULL) {
×
UNCOV
1308
      if (!tTagGet(pOldTag, &value)) {
×
UNCOV
1309
        continue;
×
1310
      }
1311
    } else {
UNCOV
1312
      value.type = pCol->type;
×
UNCOV
1313
      if (pTagVal->isNull) {
×
UNCOV
1314
        continue;
×
1315
      }
1316

UNCOV
1317
      if (IS_VAR_DATA_TYPE(pCol->type)) {
×
UNCOV
1318
        value.pData = pTagVal->pTagVal;
×
UNCOV
1319
        value.nData = pTagVal->nTagVal;
×
1320
      } else {
UNCOV
1321
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
×
1322
      }
1323
    }
1324

UNCOV
1325
    if (taosArrayPush(pTagArray, &value) == NULL) {
×
1326
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1327
                __LINE__, tstrerror(terrno), version);
1328
      taosHashCleanup(pTagTable);
×
1329
      taosArrayDestroy(pTagArray);
×
1330
      metaFetchEntryFree(&pChild);
×
1331
      metaFetchEntryFree(&pSuper);
×
1332
      TAOS_RETURN(terrno);
×
1333
    }
1334
  }
1335

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

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

UNCOV
1365
  taosHashCleanup(pTagTable);
×
UNCOV
1366
  metaFetchEntryFree(&pChild);
×
UNCOV
1367
  metaFetchEntryFree(&pSuper);
×
UNCOV
1368
  TAOS_RETURN(code);
×
1369
}
1370

UNCOV
1371
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
1372
  int32_t code = TSDB_CODE_SUCCESS;
×
1373

UNCOV
1374
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
×
1375
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1376
              __FILE__, __LINE__, version);
1377
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1378
  }
1379

UNCOV
1380
  return code;
×
1381
}
1382

UNCOV
1383
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
1384
  int32_t code = 0;
×
1385

UNCOV
1386
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
×
UNCOV
1387
  if (code) {
×
1388
    TAOS_RETURN(code);
×
1389
  }
1390

1391
  // fetch entry
UNCOV
1392
  SMetaEntry *pEntry = NULL;
×
UNCOV
1393
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
UNCOV
1394
  if (code) {
×
1395
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1396
              __FILE__, __LINE__, pReq->tbName, version);
1397
    TAOS_RETURN(code);
×
1398
  }
1399

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

1451
  // do handle entry
UNCOV
1452
  code = metaHandleEntry2(pMeta, pEntry);
×
UNCOV
1453
  if (code) {
×
1454
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1455
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1456
    metaFetchEntryFree(&pEntry);
×
1457
    TAOS_RETURN(code);
×
1458
  } else {
UNCOV
1459
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1460
             pEntry->uid, version);
1461
  }
1462

UNCOV
1463
  metaFetchEntryFree(&pEntry);
×
UNCOV
1464
  TAOS_RETURN(code);
×
1465
}
1466

UNCOV
1467
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
UNCOV
1468
  int32_t code = TSDB_CODE_SUCCESS;
×
1469

UNCOV
1470
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
×
1471
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1472
              __FILE__, __LINE__, version);
1473
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1474
  }
1475

UNCOV
1476
  SMetaEntry *pEntry = NULL;
×
UNCOV
1477
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
UNCOV
1478
  if (code) {
×
1479
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1480
              __FILE__, __LINE__, pReq->tbName, version);
1481
    TAOS_RETURN(code);
×
1482
  }
1483

UNCOV
1484
  if (pEntry->version >= version) {
×
1485
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1486
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1487
    metaFetchEntryFree(&pEntry);
×
1488
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1489
  }
1490

UNCOV
1491
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
×
1492
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1493
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1494
    metaFetchEntryFree(&pEntry);
×
1495
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1496
  }
1497

1498
  // do change the entry
UNCOV
1499
  int8_t           updated = 0;
×
UNCOV
1500
  SColCmprWrapper *wp = &pEntry->colCmpr;
×
UNCOV
1501
  for (int32_t i = 0; i < wp->nCols; i++) {
×
UNCOV
1502
    SColCmpr *p = &wp->pColCmpr[i];
×
UNCOV
1503
    if (p->id == pReq->colId) {
×
UNCOV
1504
      uint32_t dst = 0;
×
UNCOV
1505
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
×
1506
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
UNCOV
1507
      if (updated > 0) {
×
UNCOV
1508
        p->alg = dst;
×
1509
      }
1510
    }
1511
  }
1512

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

UNCOV
1527
  pEntry->version = version;
×
1528

1529
  // do handle entry
UNCOV
1530
  code = metaHandleEntry2(pMeta, pEntry);
×
UNCOV
1531
  if (code) {
×
1532
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1533
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1534
    metaFetchEntryFree(&pEntry);
×
1535
    TAOS_RETURN(code);
×
1536
  } else {
UNCOV
1537
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1538
             pEntry->uid, version);
1539
  }
1540

UNCOV
1541
  metaFetchEntryFree(&pEntry);
×
UNCOV
1542
  TAOS_RETURN(code);
×
1543
}
1544

1545
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
8✔
1546
  int32_t code = TSDB_CODE_SUCCESS;
8✔
1547

1548
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
8!
1549
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1550
              __FILE__, __LINE__, version);
1551
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1552
  }
1553

1554
  SMetaEntry *pEntry = NULL;
8✔
1555
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
8✔
1556
  if (code) {
8!
1557
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1558
              __FILE__, __LINE__, pReq->name, version);
1559
    TAOS_RETURN(code);
×
1560
  }
1561

1562
  if (pEntry->type != TSDB_SUPER_TABLE) {
8!
1563
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1564
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
1565
    metaFetchEntryFree(&pEntry);
×
1566
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1567
  }
1568

1569
  if (pEntry->uid != pReq->suid) {
8!
1570
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
1571
              ", version:%" PRId64,
1572
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
1573
    metaFetchEntryFree(&pEntry);
×
1574
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1575
  }
1576

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

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

1596
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
8!
1597
    metaError(
×
1598
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
1599
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
1600
        version);
1601
    metaFetchEntryFree(&pEntry);
×
1602
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1603
  }
1604

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

1614
  int32_t numOfChangedTags = 0;
8✔
1615
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
128✔
1616
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
120✔
1617
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
120✔
1618

1619
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
120!
1620
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
120!
1621
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
1622
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
1623
      metaFetchEntryFree(&pEntry);
×
1624
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1625
    }
1626

1627
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
120!
1628
      numOfChangedTags++;
8✔
1629
      SSCHMEA_SET_IDX_ON(pOldColumn);
8✔
1630
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
112!
1631
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
1632
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
1633
      metaFetchEntryFree(&pEntry);
×
1634
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1635
    }
1636
  }
1637

1638
  if (numOfChangedTags != 1) {
8!
1639
    metaError(
×
1640
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
1641
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
1642
    metaFetchEntryFree(&pEntry);
×
1643
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1644
  }
1645

1646
  pEntry->version = version;
8✔
1647
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
8✔
1648

1649
  // do handle the entry
1650
  code = metaHandleEntry2(pMeta, pEntry);
8✔
1651
  if (code) {
8!
1652
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1653
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
1654
    metaFetchEntryFree(&pEntry);
×
1655
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1656
  } else {
1657
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
8!
1658
             pEntry->uid, version);
1659
  }
1660

1661
  metaFetchEntryFree(&pEntry);
8✔
1662
  TAOS_RETURN(code);
8✔
1663
}
1664

1665
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
24✔
1666
  int32_t code = TSDB_CODE_SUCCESS;
24✔
1667

1668
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
24!
1669
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
1670
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
1671
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1672
  }
1673

1674
  SMetaEntry *pEntry = NULL;
24✔
1675
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
24✔
1676
  if (code) {
24!
1677
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1678
              __FILE__, __LINE__, pReq->stb, version);
1679
    TAOS_RETURN(code);
×
1680
  }
1681

1682
  if (TSDB_SUPER_TABLE != pEntry->type) {
24!
1683
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1684
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
1685
    metaFetchEntryFree(&pEntry);
×
1686
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1687
  }
1688

1689
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
24✔
1690
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
24!
1691
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1692
              __func__, __FILE__, __LINE__, pReq->stb, version);
1693
    metaFetchEntryFree(&pEntry);
×
1694
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1695
  }
1696

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

1714
  if (numOfChangedTags != 1) {
24!
1715
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
1716
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
1717
    metaFetchEntryFree(&pEntry);
×
1718
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1719
  }
1720

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

1735
  metaFetchEntryFree(&pEntry);
24✔
1736
  TAOS_RETURN(code);
24✔
1737
}
1738

1739
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
16✔
1740
  int32_t code = TSDB_CODE_SUCCESS;
16✔
1741

1742
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
16!
1743
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1744
              __FILE__, __LINE__, version);
1745
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1746
  }
1747

1748
  SMetaEntry *pEntry = NULL;
16✔
1749
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
16✔
1750
  if (code) {
16!
UNCOV
1751
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1752
              __FILE__, __LINE__, pReq->name, version);
UNCOV
1753
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
1754
  }
1755

1756
  if (pEntry->type != TSDB_SUPER_TABLE) {
16!
1757
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1758
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
1759
    metaFetchEntryFree(&pEntry);
×
1760
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1761
  }
1762

1763
  SMetaEntry entry = {
16✔
1764
      .version = version,
1765
      .type = TSDB_SUPER_TABLE,
1766
      .uid = pReq->suid,
16✔
1767
      .name = pReq->name,
16✔
1768
      .stbEntry.schemaRow = pReq->schemaRow,
1769
      .stbEntry.schemaTag = pReq->schemaTag,
1770
      .colCmpr = pReq->colCmpr,
1771
  };
1772
  TABLE_SET_COL_COMPRESSED(entry.flags);
16✔
1773

1774
  // do handle the entry
1775
  code = metaHandleEntry2(pMeta, &entry);
16✔
1776
  if (code) {
16!
1777
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1778
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
1779
    metaFetchEntryFree(&pEntry);
×
1780
    TAOS_RETURN(code);
×
1781
  } else {
1782
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
16!
1783
             pReq->suid, version);
1784
  }
1785

1786
  metaFetchEntryFree(&pEntry);
16✔
1787
  TAOS_RETURN(code);
16✔
1788
}
1789

1790
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
1791
  int32_t code = 0;
×
1792

1793
  if (taosArrayGetSize(uidArray) == 0) {
×
1794
    return TSDB_CODE_SUCCESS;
×
1795
  }
1796

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

1807
    SMetaEntry entry = {
×
1808
        .version = version,
1809
        .uid = uid,
1810
    };
1811

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