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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

web-flow
Merge pull request #29213 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

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

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

43
    if (pReq->suid != uid) {
4!
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

UNCOV
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

UNCOV
57
    if (info.uid == info.suid) {
×
UNCOV
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) {
16,473✔
69
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
1!
70
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
71
    return TSDB_CODE_INVALID_MSG;
×
72
  }
73

74
  return TSDB_CODE_SUCCESS;
16,477✔
75
}
76

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

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

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

113
  return code;
1,897✔
114
}
115

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

122
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
2,084!
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,088✔
129
  if (code) {
2,088!
UNCOV
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);
UNCOV
132
    return TSDB_CODE_TDB_STB_NOT_EXIST;
×
133
  } else {
134
    int64_t uid = *(int64_t *)value;
2,088✔
135
    tdbFreeClear(value);
2,088✔
136

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

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

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

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

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

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

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

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

231
// Alter Super Table
232

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

240
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
141,538!
241
      pReq->ctb.suid == 0) {
141,543!
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) {
141,543✔
249
    pReq->uid = *(int64_t *)value;
26,576✔
250
    tdbFreeClear(value);
26,576✔
251

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

266
    // check suid
267
    if (info.suid != pReq->ctb.suid) {
26,569!
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;
26,569✔
276
  }
277

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

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

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

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

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

334
  return code;
114,969✔
335
}
336

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

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

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

363
  // build response
364
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
114,922✔
365
  if (code) {
114,948!
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);
114,948✔
372
  if (TSDB_CODE_SUCCESS == code) {
114,938!
373
    metaInfo("vgId:%d, child table:%s uid %" PRId64 " suid:%" PRId64 " is created, version:%" PRId64,
114,942!
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;
114,983✔
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) {
13,165✔
393
  int32_t code = 0;
13,165✔
394
  void   *value = NULL;
13,165✔
395
  int32_t valueSize = 0;
13,165✔
396

397
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
13,165!
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) {
13,165✔
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);
13,159✔
413
  if (code) {
13,159!
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;
13,159✔
418
}
419

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

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

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

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

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

444
  return code;
13,158✔
445
}
446

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

450
  // check request
451
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
13,165✔
452
  if (code) {
13,165✔
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 = {
13,159✔
461
      .version = version,
462
      .type = TSDB_NORMAL_TABLE,
463
      .uid = pReq->uid,
13,159✔
464
      .name = pReq->name,
13,159✔
465
      .ntbEntry.btime = pReq->btime,
13,159✔
466
      .ntbEntry.ttlDays = pReq->ttl,
13,159✔
467
      .ntbEntry.commentLen = pReq->commentLen,
13,159✔
468
      .ntbEntry.comment = pReq->comment,
13,159✔
469
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
470
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
13,159✔
471
      .colCmpr = pReq->colCmpr,
472
  };
473
  TABLE_SET_COL_COMPRESSED(entry.flags);
13,159✔
474

475
  // build response
476
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
13,159✔
477
  if (code) {
13,157!
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);
13,157✔
484
  if (TSDB_CODE_SUCCESS == code) {
13,159!
485
    metaInfo("vgId:%d, normal table:%s uid %" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
13,159!
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);
13,159✔
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) {
154,707✔
502
  int32_t code = TSDB_CODE_SUCCESS;
154,707✔
503
  if (TSDB_CHILD_TABLE == pReq->type) {
154,707✔
504
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
141,546✔
505
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
13,161!
506
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
13,165✔
507
  } else {
508
    code = TSDB_CODE_INVALID_MSG;
×
509
  }
510
  TAOS_RETURN(code);
154,721✔
511
}
512

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

516
  // check request
517
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,897✔
518
  if (code) {
1,897!
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) {
1,897!
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 = {
1,897✔
534
      .version = version,
535
      .uid = pReq->uid,
1,897✔
536
  };
537

538
  if (pReq->suid == 0) {
1,897✔
539
    entry.type = -TSDB_NORMAL_TABLE;
1,678✔
540
  } else {
541
    entry.type = -TSDB_CHILD_TABLE;
219✔
542
  }
543
  code = metaHandleEntry2(pMeta, &entry);
1,897✔
544
  if (code) {
1,897!
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,
1,897!
549
             pReq->uid, version);
550
  }
551
  TAOS_RETURN(code);
1,897✔
552
}
553

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

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

576
  // check table type
577
  SMetaInfo info;
578
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
197!
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) {
197✔
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);
195✔
594
  if (code) {
195!
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);
195✔
600
}
601

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

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

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

642
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
65!
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));
65!
650
  if (NULL == pNewSchema) {
65!
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;
65✔
657
  pSchema->version++;
65✔
658
  pSchema->nCols++;
65✔
659
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
65✔
660
  pColumn->bytes = pReq->bytes;
65✔
661
  pColumn->type = pReq->type;
65✔
662
  pColumn->flags = pReq->flags;
65✔
663
  pColumn->colId = pEntry->ntbEntry.ncid++;
65✔
664
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
65✔
665
  uint32_t compress;
666
  if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
65✔
667
    compress = createDefaultColCmprByType(pColumn->type);
64✔
668
  } else {
669
    compress = pReq->compress;
1✔
670
  }
671
  code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
65✔
672
  if (code) {
65!
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);
65✔
681
  if (code) {
65!
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,
65!
688
             pEntry->uid, version);
689
  }
690

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

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

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

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

715
  // fetch old entry
716
  SMetaEntry *pEntry = NULL;
63✔
717
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
63✔
718
  if (code) {
63!
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) {
63!
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;
63✔
733
  SSchema        *pColumn = NULL;
63✔
734
  SSchema         tColumn;
735
  int32_t         iColumn = 0;
63✔
736
  for (; iColumn < pSchema->nCols; iColumn++) {
209!
737
    pColumn = &pSchema->pSchema[iColumn];
209✔
738
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
209✔
739
      break;
63✔
740
    }
741
  }
742

743
  if (iColumn == pSchema->nCols) {
63!
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) {
63!
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) {
63✔
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;
52✔
764

765
  // do drop column
766
  pEntry->version = version;
52✔
767
  if (pSchema->nCols - iColumn - 1 > 0) {
52✔
768
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
35✔
769
  }
770
  pSchema->nCols--;
52✔
771
  pSchema->version++;
52✔
772
  code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
52✔
773
  if (code) {
52!
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) {
52!
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);
52✔
788
  if (code) {
52!
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,
52!
794
             pEntry->uid, version);
795
  }
796

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

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

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

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

822
  if (NULL == pReq->colNewName) {
29!
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;
29✔
830
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
29✔
831
  if (code) {
29!
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) {
29!
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;
29✔
846
  SSchema        *pColumn = NULL;
29✔
847
  int32_t         iColumn = 0;
29✔
848
  for (int32_t i = 0; i < pSchema->nCols; i++) {
116!
849
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
116✔
850
      pColumn = &pSchema->pSchema[i];
29✔
851
      iColumn = i;
29✔
852
      break;
29✔
853
    }
854
  }
855

856
  if (NULL == pColumn) {
29!
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) {
29✔
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;
18✔
872
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
18✔
873
  pSchema->version++;
18✔
874

875
  // do handle entry
876
  code = metaHandleEntry2(pMeta, pEntry);
18✔
877
  if (code) {
18!
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,
18!
884
             pEntry->uid, version);
885
  }
886

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

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

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

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

912
  // fetch old entry
913
  SMetaEntry *pEntry = NULL;
38✔
914
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
38✔
915
  if (code) {
38!
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) {
38!
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;
38✔
930
  SSchema        *pColumn = NULL;
38✔
931
  int32_t         iColumn = 0;
38✔
932
  int32_t         rowSize = 0;
38✔
933
  for (int32_t i = 0; i < pSchema->nCols; i++) {
184✔
934
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
146✔
935
      pColumn = &pSchema->pSchema[i];
38✔
936
      iColumn = i;
38✔
937
    }
938
    rowSize += pSchema->pSchema[i].bytes;
146✔
939
  }
940

941
  if (NULL == pColumn) {
38!
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) {
38!
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) {
38✔
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) {
25!
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;
25✔
973
  pSchema->version++;
25✔
974
  pColumn->bytes = pReq->colModBytes;
25✔
975

976
  // do handle entry
977
  code = metaHandleEntry2(pMeta, pEntry);
25✔
978
  if (code) {
25!
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,
25!
985
             pEntry->uid, version);
986
  }
987

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

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

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

1007
  // check tag name
1008
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
638!
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;
638✔
1016
  int32_t valueSize = 0;
638✔
1017
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
638✔
1018
  if (code) {
638✔
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);
637✔
1025

1026
  TAOS_RETURN(code);
637✔
1027
}
1028

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

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

1038
  // fetch child entry
1039
  SMetaEntry *pChild = NULL;
637✔
1040
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
637✔
1041
  if (code) {
637!
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) {
637!
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;
637✔
1056
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
637✔
1057
  if (code) {
637!
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;
637✔
1066
  SSchema        *pColumn = NULL;
637✔
1067
  int32_t         iColumn = 0;
637✔
1068
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
1,527!
1069
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
1,527✔
1070
      pColumn = &pTagSchema->pSchema[i];
637✔
1071
      iColumn = i;
637✔
1072
      break;
637✔
1073
    }
1074
  }
1075

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

1100
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
622✔
1101
    if (NULL == pTagArray) {
622!
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,653✔
1110
      STagVal value = {
2,031✔
1111
          .type = pTagSchema->pSchema[i].type,
2,031✔
1112
          .cid = pTagSchema->pSchema[i].colId,
2,031✔
1113
      };
1114

1115
      if (iColumn == i) {
2,031✔
1116
        if (pReq->isNull) {
622✔
1117
          continue;
398✔
1118
        }
1119
        if (IS_VAR_DATA_TYPE(value.type)) {
575!
1120
          value.pData = pReq->pTagVal;
203✔
1121
          value.nData = pReq->nTagVal;
203✔
1122
        } else {
1123
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
372✔
1124
        }
1125
      } else if (!tTagGet(pOldTag, &value)) {
1,409✔
1126
        continue;
351✔
1127
      }
1128

1129
      if (NULL == taosArrayPush(pTagArray, &value)) {
1,633!
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;
622✔
1140
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
622✔
1141
    if (code) {
622!
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);
622✔
1150
    taosMemoryFree(pChild->ctbEntry.pTags);
622!
1151
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
622✔
1152
  }
1153

1154
  // do handle entry
1155
  code = metaHandleEntry2(pMeta, pChild);
637✔
1156
  if (code) {
637!
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,
637!
1164
             pChild->uid, version);
1165
  }
1166

1167
  // free resource and return
1168
  metaFetchEntryFree(&pChild);
637✔
1169
  metaFetchEntryFree(&pSuper);
637✔
1170
  TAOS_RETURN(code);
637✔
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) {
53✔
1372
  int32_t code = TSDB_CODE_SUCCESS;
53✔
1373

1374
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
53!
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;
53✔
1381
}
1382

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

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

1391
  // fetch entry
1392
  SMetaEntry *pEntry = NULL;
53✔
1393
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
53✔
1394
  if (code) {
53!
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;
53✔
1402
  if (pEntry->type == TSDB_CHILD_TABLE) {
53✔
1403
    if (pReq->updateTTL) {
26✔
1404
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
11✔
1405
    }
1406
    if (pReq->newCommentLen >= 0) {
26✔
1407
      char *pNewComment = NULL;
15✔
1408
      if (pReq->newCommentLen) {
15✔
1409
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
12!
1410
        if (NULL == pNewComment) {
12!
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);
12✔
1417
      } else {
1418
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
3!
1419
      }
1420
      pEntry->ctbEntry.comment = pNewComment;
15✔
1421
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
15✔
1422
    }
1423
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
27!
1424
    if (pReq->updateTTL) {
27✔
1425
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
8✔
1426
    }
1427
    if (pReq->newCommentLen >= 0) {
27✔
1428
      char *pNewComment = NULL;
19✔
1429
      if (pReq->newCommentLen > 0) {
19✔
1430
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
16!
1431
        if (NULL == pNewComment) {
16!
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);
16✔
1438
      } else {
1439
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
3!
1440
      }
1441
      pEntry->ntbEntry.comment = pNewComment;
19✔
1442
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
19✔
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);
53✔
1453
  if (code) {
53!
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,
53!
1460
             pEntry->uid, version);
1461
  }
1462

1463
  metaFetchEntryFree(&pEntry);
53✔
1464
  TAOS_RETURN(code);
53✔
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) {
21✔
1546
  int32_t code = TSDB_CODE_SUCCESS;
21✔
1547

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

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

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

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

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

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

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

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

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

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

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

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

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