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

taosdata / TDengine / #3571

31 Dec 2024 08:57AM UTC coverage: 63.276% (+0.3%) from 62.94%
#3571

push

travis-ci

web-flow
Merge pull request #29434 from taosdata/enh/3.0/TD-33266-2

Fix(cover):fix some review errors in pr 29417.

140094 of 284342 branches covered (49.27%)

Branch coverage included in aggregate %.

217978 of 281543 relevant lines covered (77.42%)

20036556.91 hits per line

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

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

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

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

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

50
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
35!
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) {
35!
58
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
35✔
59
    } else {
60
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
61
                " already exists but not a super table, version:%" PRId64,
62
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
63
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
64
    }
65
  }
66

67
  // check suid
68
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
28,138!
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;
28,153✔
75
}
76

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

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

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

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

113
  return code;
2,564✔
114
}
115

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

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

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

144
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
2,345✔
145
  if (code) {
2,342✔
146
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
11!
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,331!
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,331✔
157
}
158

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

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

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

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

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

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

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

231
// Alter Super Table
232

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

240
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
171,762!
241
      pReq->ctb.suid == 0) {
171,775✔
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) {
171,774✔
249
    pReq->uid = *(int64_t *)value;
27,177✔
250
    tdbFreeClear(value);
27,177✔
251

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

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

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

275
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
27,170✔
276
  }
277

278
  // check super table existence
279
  if (tdbTbGet(pMeta->pNameIdx, pReq->ctb.stbName, strlen(pReq->ctb.stbName) + 1, &value, &valueSize) == 0) {
144,582!
280
    int64_t suid = *(int64_t *)value;
144,580✔
281
    tdbFreeClear(value);
144,580✔
282
    if (suid != pReq->ctb.suid) {
144,580!
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) {
144,580!
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) {
144,571!
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)) {
144,571!
308
    code = grantCheck(TSDB_GRANT_TIMESERIES);
144,572✔
309
    if (TSDB_CODE_SUCCESS != code) {
144,543!
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;
144,554✔
315
}
316

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

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

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

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

334
  return code;
144,573✔
335
}
336

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

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

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

363
  // build response
364
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
144,554✔
365
  if (code) {
144,587!
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);
144,587✔
372
  if (TSDB_CODE_SUCCESS == code) {
144,551!
373
    metaInfo("vgId:%d, child table:%s uid %" PRId64 " suid:%" PRId64 " is created, version:%" PRId64,
144,557!
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;
144,603✔
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) {
14,477✔
393
  int32_t code = 0;
14,477✔
394
  void   *value = NULL;
14,477✔
395
  int32_t valueSize = 0;
14,477✔
396

397
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
14,477!
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) {
14,477✔
405
    // for auto create table, we return the uid of the existing table
406
    pReq->uid = *(tb_uid_t *)value;
6✔
407
    tdbFree(value);
6✔
408
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
6✔
409
  }
410

411
  // grant check
412
  code = grantCheck(TSDB_GRANT_TIMESERIES);
14,471✔
413
  if (code) {
14,469!
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;
14,471✔
418
}
419

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

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

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

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

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

444
  return code;
14,469✔
445
}
446

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

450
  // check request
451
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
14,477✔
452
  if (code) {
14,476✔
453
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
6!
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
    }
457
    TAOS_RETURN(code);
6✔
458
  }
459

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

475
  // build response
476
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
14,470✔
477
  if (code) {
14,469!
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);
14,469✔
484
  if (TSDB_CODE_SUCCESS == code) {
14,469!
485
    metaInfo("vgId:%d, normal table:%s uid %" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
14,469!
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);
14,471✔
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) {
186,240✔
502
  int32_t code = TSDB_CODE_SUCCESS;
186,240✔
503
  if (TSDB_CHILD_TABLE == pReq->type) {
186,240✔
504
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
171,768✔
505
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
14,472!
506
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
14,477✔
507
  } else {
508
    code = TSDB_CODE_INVALID_MSG;
×
509
  }
510
  TAOS_RETURN(code);
186,253✔
511
}
512

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

516
  // check request
517
  code = metaCheckDropTableReq(pMeta, version, pReq);
2,564✔
518
  if (code) {
2,564!
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

526
  if (pReq->suid == pReq->uid) {
2,564!
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

533
  SMetaEntry entry = {
2,564✔
534
      .version = version,
535
      .uid = pReq->uid,
2,564✔
536
  };
537

538
  if (pReq->suid == 0) {
2,564✔
539
    entry.type = -TSDB_NORMAL_TABLE;
1,777✔
540
  } else {
541
    entry.type = -TSDB_CHILD_TABLE;
787✔
542
  }
543
  code = metaHandleEntry2(pMeta, &entry);
2,564✔
544
  if (code) {
2,564!
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 {
548
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
2,564!
549
             pReq->uid, version);
550
  }
551
  TAOS_RETURN(code);
2,564✔
552
}
553

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

557
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
220!
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
564
  void   *value = NULL;
220✔
565
  int32_t valueSize = 0;
220✔
566
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
220✔
567
  if (code) {
220!
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
  }
573
  int64_t uid = *(int64_t *)value;
220✔
574
  tdbFreeClear(value);
220✔
575

576
  // check table type
577
  SMetaInfo info;
578
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
220!
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
  }
585
  if (info.suid != 0) {
220✔
586
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
2!
587
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
588
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
2✔
589
    TAOS_RETURN(code);
2✔
590
  }
591

592
  // check grant
593
  code = grantCheck(TSDB_GRANT_TIMESERIES);
218✔
594
  if (code) {
218!
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
  }
599
  TAOS_RETURN(code);
218✔
600
}
601

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

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

611
  // fetch old entry
612
  SMetaEntry *pEntry = NULL;
80✔
613
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
80✔
614
  if (code) {
80!
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
  }
619
  if (pEntry->version >= version) {
80!
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
627
  int32_t         rowSize = 0;
80✔
628
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
80✔
629
  SSchema        *pColumn;
630
  pEntry->version = version;
80✔
631
  for (int32_t i = 0; i < pSchema->nCols; i++) {
411✔
632
    pColumn = &pSchema->pSchema[i];
331✔
633
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
331!
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
    }
639
    rowSize += pColumn->bytes;
331✔
640
  }
641

642
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
80!
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

649
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
80!
650
  if (NULL == pNewSchema) {
80!
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
  }
656
  pSchema->pSchema = pNewSchema;
80✔
657
  pSchema->version++;
80✔
658
  pSchema->nCols++;
80✔
659
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
80✔
660
  pColumn->bytes = pReq->bytes;
80✔
661
  pColumn->type = pReq->type;
80✔
662
  pColumn->flags = pReq->flags;
80✔
663
  pColumn->colId = pEntry->ntbEntry.ncid++;
80✔
664
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
80✔
665
  uint32_t compress;
666
  if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
80✔
667
    compress = createDefaultColCmprByType(pColumn->type);
79✔
668
  } else {
669
    compress = pReq->compress;
1✔
670
  }
671
  code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
80✔
672
  if (code) {
80!
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
680
  code = metaHandleEntry2(pMeta, pEntry);
80✔
681
  if (code) {
80!
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 {
687
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
80!
688
             pEntry->uid, version);
689
  }
690

691
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
80!
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 {
695
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
491✔
696
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
411✔
697
      pRsp->pSchemaExt[i].colId = p->id;
411✔
698
      pRsp->pSchemaExt[i].compress = p->alg;
411✔
699
    }
700
  }
701

702
  metaFetchEntryFree(&pEntry);
80✔
703
  TAOS_RETURN(code);
80✔
704
}
705

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

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

715
  // fetch old entry
716
  SMetaEntry *pEntry = NULL;
68✔
717
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
68✔
718
  if (code) {
68!
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

724
  if (pEntry->version >= version) {
68!
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
732
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
68✔
733
  SSchema        *pColumn = NULL;
68✔
734
  SSchema         tColumn;
735
  int32_t         iColumn = 0;
68✔
736
  for (; iColumn < pSchema->nCols; iColumn++) {
219!
737
    pColumn = &pSchema->pSchema[iColumn];
219✔
738
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
219✔
739
      break;
68✔
740
    }
741
  }
742

743
  if (iColumn == pSchema->nCols) {
68!
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

750
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
68!
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

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

765
  // do drop column
766
  pEntry->version = version;
57✔
767
  if (pSchema->nCols - iColumn - 1 > 0) {
57✔
768
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
40✔
769
  }
770
  pSchema->nCols--;
57✔
771
  pSchema->version++;
57✔
772
  code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
57✔
773
  if (code) {
57!
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
  }
779
  if (pEntry->colCmpr.nCols != pSchema->nCols) {
57!
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
787
  code = metaHandleEntry2(pMeta, pEntry);
57✔
788
  if (code) {
57!
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 {
793
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
57!
794
             pEntry->uid, version);
795
  }
796

797
  // build response
798
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
57!
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 {
802
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
305✔
803
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
248✔
804
      pRsp->pSchemaExt[i].colId = p->id;
248✔
805
      pRsp->pSchemaExt[i].compress = p->alg;
248✔
806
    }
807
  }
808

809
  metaFetchEntryFree(&pEntry);
57✔
810
  TAOS_RETURN(code);
57✔
811
}
812

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

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

822
  if (NULL == pReq->colNewName) {
30!
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
829
  SMetaEntry *pEntry = NULL;
30✔
830
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
30✔
831
  if (code) {
30!
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

837
  if (pEntry->version >= version) {
30!
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
845
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
30✔
846
  SSchema        *pColumn = NULL;
30✔
847
  int32_t         iColumn = 0;
30✔
848
  for (int32_t i = 0; i < pSchema->nCols; i++) {
118!
849
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
118✔
850
      pColumn = &pSchema->pSchema[i];
30✔
851
      iColumn = i;
30✔
852
      break;
30✔
853
    }
854
  }
855

856
  if (NULL == pColumn) {
30!
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

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

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

875
  // do handle entry
876
  code = metaHandleEntry2(pMeta, pEntry);
19✔
877
  if (code) {
19!
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 {
883
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
19!
884
             pEntry->uid, version);
885
  }
886

887
  // build response
888
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
19!
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 {
892
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
111✔
893
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
92✔
894
      pRsp->pSchemaExt[i].colId = p->id;
92✔
895
      pRsp->pSchemaExt[i].compress = p->alg;
92✔
896
    }
897
  }
898

899
  metaFetchEntryFree(&pEntry);
19✔
900
  TAOS_RETURN(code);
19✔
901
}
902

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

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

912
  // fetch old entry
913
  SMetaEntry *pEntry = NULL;
40✔
914
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
40✔
915
  if (code) {
40!
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

921
  if (pEntry->version >= version) {
40!
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
929
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
40✔
930
  SSchema        *pColumn = NULL;
40✔
931
  int32_t         iColumn = 0;
40✔
932
  int32_t         rowSize = 0;
40✔
933
  for (int32_t i = 0; i < pSchema->nCols; i++) {
190✔
934
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
150✔
935
      pColumn = &pSchema->pSchema[i];
40✔
936
      iColumn = i;
40✔
937
    }
938
    rowSize += pSchema->pSchema[i].bytes;
150✔
939
  }
940

941
  if (NULL == pColumn) {
40!
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

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

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

963
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
27!
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
972
  pEntry->version = version;
27✔
973
  pSchema->version++;
27✔
974
  pColumn->bytes = pReq->colModBytes;
27✔
975

976
  // do handle entry
977
  code = metaHandleEntry2(pMeta, pEntry);
27✔
978
  if (code) {
27!
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 {
984
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
27!
985
             pEntry->uid, version);
986
  }
987

988
  // build response
989
  if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
27!
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 {
993
    for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
127✔
994
      SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
100✔
995
      pRsp->pSchemaExt[i].colId = p->id;
100✔
996
      pRsp->pSchemaExt[i].compress = p->alg;
100✔
997
    }
998
  }
999

1000
  metaFetchEntryFree(&pEntry);
27✔
1001
  TAOS_RETURN(code);
27✔
1002
}
1003

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

1007
  // check tag name
1008
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
650!
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
1015
  void   *value = NULL;
650✔
1016
  int32_t valueSize = 0;
650✔
1017
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
650✔
1018
  if (code) {
650✔
1019
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1!
1020
              __FILE__, __LINE__, pReq->tbName, version);
1021
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1✔
1022
    TAOS_RETURN(code);
1✔
1023
  }
1024
  tdbFreeClear(value);
649✔
1025

1026
  TAOS_RETURN(code);
649✔
1027
}
1028

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

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

1038
  // fetch child entry
1039
  SMetaEntry *pChild = NULL;
649✔
1040
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
649✔
1041
  if (code) {
649!
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

1047
  if (pChild->type != TSDB_CHILD_TABLE) {
649!
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
1055
  SMetaEntry *pSuper = NULL;
649✔
1056
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
649✔
1057
  if (code) {
649!
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
1065
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
649✔
1066
  SSchema        *pColumn = NULL;
649✔
1067
  int32_t         iColumn = 0;
649✔
1068
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
1,542!
1069
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
1,542✔
1070
      pColumn = &pTagSchema->pSchema[i];
649✔
1071
      iColumn = i;
649✔
1072
      break;
649✔
1073
    }
1074
  }
1075

1076
  if (NULL == pColumn) {
649!
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
1085
  pChild->version = version;
649✔
1086
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
649✔
1087
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
17!
1088
    if (NULL == pNewTag) {
17!
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
    }
1095
    pChild->ctbEntry.pTags = pNewTag;
17✔
1096
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
17✔
1097
  } else {
1098
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
632✔
1099

1100
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
632✔
1101
    if (NULL == pTagArray) {
632!
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

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

1115
      if (iColumn == i) {
2,057✔
1116
        if (pReq->isNull) {
632✔
1117
          continue;
407✔
1118
        }
1119
        if (IS_VAR_DATA_TYPE(value.type)) {
585!
1120
          value.pData = pReq->pTagVal;
205✔
1121
          value.nData = pReq->nTagVal;
205✔
1122
        } else {
1123
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
380✔
1124
        }
1125
      } else if (!tTagGet(pOldTag, &value)) {
1,425✔
1126
        continue;
360✔
1127
      }
1128

1129
      if (NULL == taosArrayPush(pTagArray, &value)) {
1,650!
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

1139
    STag *pNewTag = NULL;
632✔
1140
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
632✔
1141
    if (code) {
632!
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
    }
1149
    taosArrayDestroy(pTagArray);
632✔
1150
    taosMemoryFree(pChild->ctbEntry.pTags);
632!
1151
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
632✔
1152
  }
1153

1154
  // do handle entry
1155
  code = metaHandleEntry2(pMeta, pChild);
649✔
1156
  if (code) {
649!
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 {
1163
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
649!
1164
             pChild->uid, version);
1165
  }
1166

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

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

1176
  // check tag name
1177
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
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
1184
  void   *value = NULL;
5✔
1185
  int32_t valueSize = 0;
5✔
1186
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
5✔
1187
  if (code) {
5!
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
  }
1193
  tdbFreeClear(value);
5✔
1194

1195
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
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

1201
  TAOS_RETURN(code);
5✔
1202
}
1203

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

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

1212
  // fetch child entry
1213
  SMetaEntry *pChild = NULL;
5✔
1214
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
5✔
1215
  if (code) {
5!
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

1221
  if (pChild->type != TSDB_CHILD_TABLE) {
5!
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
1229
  SMetaEntry *pSuper = NULL;
5✔
1230
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
5✔
1231
  if (code) {
5!
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
1239
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
5✔
1240

1241
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5!
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 =
1251
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5✔
1252
  if (pTagTable == NULL) {
5!
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

1260
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
35✔
1261
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
30✔
1262
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
30!
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

1272
  int32_t numOfChangedTags = 0;
5✔
1273
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1274
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
35✔
1275
        ? numOfChangedTags++
30✔
1276
        : 0;
35✔
1277
  }
1278
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
5!
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
1288
  pChild->version = version;
5✔
1289
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
5✔
1290
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
5✔
1291
  if (NULL == pTagArray) {
5!
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

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

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

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

1325
    if (taosArrayPush(pTagArray, &value) == NULL) {
29!
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

1336
  STag *pNewTag = NULL;
5✔
1337
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
5✔
1338
  if (code) {
5!
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
  }
1347
  taosArrayDestroy(pTagArray);
5✔
1348
  taosMemoryFree(pChild->ctbEntry.pTags);
5!
1349
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
5✔
1350

1351
  // do handle entry
1352
  code = metaHandleEntry2(pMeta, pChild);
5✔
1353
  if (code) {
5!
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 {
1361
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5!
1362
             pChild->uid, version);
1363
  }
1364

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

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

1374
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
77!
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

1380
  return code;
77✔
1381
}
1382

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

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

1391
  // fetch entry
1392
  SMetaEntry *pEntry = NULL;
77✔
1393
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
77✔
1394
  if (code) {
77!
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
1401
  pEntry->version = version;
77✔
1402
  if (pEntry->type == TSDB_CHILD_TABLE) {
77✔
1403
    if (pReq->updateTTL) {
40✔
1404
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
15✔
1405
    }
1406
    if (pReq->newCommentLen >= 0) {
40✔
1407
      char *pNewComment = NULL;
25✔
1408
      if (pReq->newCommentLen) {
25✔
1409
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
20!
1410
        if (NULL == pNewComment) {
20!
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
        }
1416
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
20✔
1417
      } else {
1418
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
5!
1419
      }
1420
      pEntry->ctbEntry.comment = pNewComment;
25✔
1421
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
25✔
1422
    }
1423
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
37!
1424
    if (pReq->updateTTL) {
37✔
1425
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
12✔
1426
    }
1427
    if (pReq->newCommentLen >= 0) {
37✔
1428
      char *pNewComment = NULL;
25✔
1429
      if (pReq->newCommentLen > 0) {
25✔
1430
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
20!
1431
        if (NULL == pNewComment) {
20!
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
        }
1437
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
20✔
1438
      } else {
1439
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
5!
1440
      }
1441
      pEntry->ntbEntry.comment = pNewComment;
25✔
1442
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
25✔
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
1452
  code = metaHandleEntry2(pMeta, pEntry);
77✔
1453
  if (code) {
77!
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 {
1459
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
77!
1460
             pEntry->uid, version);
1461
  }
1462

1463
  metaFetchEntryFree(&pEntry);
77✔
1464
  TAOS_RETURN(code);
77✔
1465
}
1466

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

1470
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5!
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

1476
  SMetaEntry *pEntry = NULL;
5✔
1477
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5✔
1478
  if (code) {
5!
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

1484
  if (pEntry->version >= version) {
5!
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

1491
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
5!
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
1499
  int8_t           updated = 0;
5✔
1500
  SColCmprWrapper *wp = &pEntry->colCmpr;
5✔
1501
  for (int32_t i = 0; i < wp->nCols; i++) {
40✔
1502
    SColCmpr *p = &wp->pColCmpr[i];
35✔
1503
    if (p->id == pReq->colId) {
35✔
1504
      uint32_t dst = 0;
5✔
1505
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5✔
1506
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1507
      if (updated > 0) {
5!
1508
        p->alg = dst;
5✔
1509
      }
1510
    }
1511
  }
1512

1513
  if (updated == 0) {
5!
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);
×
1519
  } else if (updated < 0) {
5!
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

1527
  pEntry->version = version;
5✔
1528

1529
  // do handle entry
1530
  code = metaHandleEntry2(pMeta, pEntry);
5✔
1531
  if (code) {
5!
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 {
1537
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5!
1538
             pEntry->uid, version);
1539
  }
1540

1541
  metaFetchEntryFree(&pEntry);
5✔
1542
  TAOS_RETURN(code);
5✔
1543
}
1544

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

1548
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
971!
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;
971✔
1555
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
971✔
1556
  if (code) {
971!
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) {
971!
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) {
971!
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;
971✔
1588
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
971✔
1589
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
971!
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) {
971!
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;
971✔
1615
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
15,244✔
1616
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
14,273✔
1617
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
14,273✔
1618

1619
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
14,273!
1620
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
14,273!
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)) {
14,273✔
1628
      numOfChangedTags++;
971✔
1629
      SSCHMEA_SET_IDX_ON(pOldColumn);
971✔
1630
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
13,302!
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) {
971!
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;
971✔
1647
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
971✔
1648

1649
  // do handle the entry
1650
  code = metaHandleEntry2(pMeta, pEntry);
971✔
1651
  if (code) {
971!
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,
971!
1658
             pEntry->uid, version);
1659
  }
1660

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

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

1668
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,146!
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;
2,146✔
1675
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,146✔
1676
  if (code) {
2,146!
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) {
2,146!
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;
2,146✔
1690
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,146!
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;
2,146✔
1699
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
6,724!
1700
    SSchema *pCol = pTagSchema->pSchema + i;
6,724✔
1701
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
6,724✔
1702
      if (!IS_IDX_ON(pCol)) {
2,146!
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++;
2,146✔
1709
      SSCHMEA_SET_IDX_OFF(pCol);
2,146✔
1710
      break;
2,146✔
1711
    }
1712
  }
1713

1714
  if (numOfChangedTags != 1) {
2,146!
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;
2,146✔
1723
  pTagSchema->version++;
2,146✔
1724
  code = metaHandleEntry2(pMeta, pEntry);
2,146✔
1725
  if (code) {
2,146!
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,
2,146!
1732
             pEntry->uid, version);
1733
  }
1734

1735
  metaFetchEntryFree(&pEntry);
2,146✔
1736
  TAOS_RETURN(code);
2,146✔
1737
}
1738

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

1742
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,909!
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;
7,924✔
1749
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,924✔
1750
  if (code) {
7,913✔
1751
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
3!
1752
              __FILE__, __LINE__, pReq->name, version);
1753
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
3✔
1754
  }
1755

1756
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,910!
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 = {
7,910✔
1764
      .version = version,
1765
      .type = TSDB_SUPER_TABLE,
1766
      .uid = pReq->suid,
7,910✔
1767
      .name = pReq->name,
7,910✔
1768
      .stbEntry.schemaRow = pReq->schemaRow,
1769
      .stbEntry.schemaTag = pReq->schemaTag,
1770
      .colCmpr = pReq->colCmpr,
1771
  };
1772
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,910✔
1773

1774
  // do handle the entry
1775
  code = metaHandleEntry2(pMeta, &entry);
7,910✔
1776
  if (code) {
7,929!
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,
7,929✔
1783
             pReq->suid, version);
1784
  }
1785

1786
  metaFetchEntryFree(&pEntry);
7,931✔
1787
  TAOS_RETURN(code);
7,930✔
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