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

taosdata / TDengine / #4066

12 May 2025 05:35AM UTC coverage: 62.547% (+0.04%) from 62.508%
#4066

push

travis-ci

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

merge: from main to 3.0 branch

155777 of 317858 branches covered (49.01%)

Branch coverage included in aggregate %.

382 of 573 new or added lines in 31 files covered. (66.67%)

648 existing lines in 129 files now uncovered.

241270 of 316936 relevant lines covered (76.13%)

6462449.7 hits per line

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

50.19
/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 metaUpdateVtbMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
21
                                    STableMetaRsp *pMetaRsp, int8_t tableType);
22
extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
23
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
24
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
25
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
26
extern int32_t addTableExtSchema(SMetaEntry* pEntry, const SSchema* pColumn, int32_t newColNum, SExtSchema* pExtSchema);
27
extern int32_t dropTableExtSchema(SMetaEntry* pEntry, int32_t dropColId, int32_t newColNum);
28

29
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
25,557✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
25,557✔
31
  void     *value = NULL;
25,557✔
32
  int32_t   valueSize = 0;
25,557✔
33
  SMetaInfo info;
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
25,557!
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
×
38
              pReq->name, version);
39
    return TSDB_CODE_INVALID_MSG;
×
40
  }
41

42
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
25,575✔
43
  if (r == 0) {  // name exists, check uid and type
25,545✔
44
    int64_t uid = *(tb_uid_t *)value;
60✔
45
    tdbFree(value);
60✔
46

47
    if (pReq->suid != uid) {
60✔
48
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
3!
49
                " version:%" PRId64,
50
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
51
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
3✔
52
    }
53

54
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
57!
55
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
56
                " not found, this is an internal error in meta, version:%" PRId64,
57
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
58
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
59
    }
60

61
    if (info.uid == info.suid) {
57!
62
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
57✔
63
    } else {
64
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
65
                " already exists but not a super table, version:%" PRId64,
66
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
67
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
68
    }
69
  }
70

71
  // check suid
72
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
25,485✔
73
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
3!
74
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
75
    return TSDB_CODE_INVALID_MSG;
×
76
  }
77

78
  return TSDB_CODE_SUCCESS;
25,483✔
79
}
80

81
static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
15,529✔
82
  int32_t   code = TSDB_CODE_SUCCESS;
15,529✔
83
  void     *value = NULL;
15,529✔
84
  int32_t   valueSize = 0;
15,529✔
85
  SMetaInfo info;
86

87
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
15,529!
88
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
89
              __FILE__, __LINE__, pReq->name, version);
90
    return TSDB_CODE_INVALID_MSG;
×
91
  }
92

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

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

117
  return code;
15,529✔
118
}
119

120
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
1,875✔
121
  int32_t   code = TSDB_CODE_SUCCESS;
1,875✔
122
  void     *value = NULL;
1,875✔
123
  int32_t   valueSize = 0;
1,875✔
124
  SMetaInfo info;
125

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,875!
UNCOV
127
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
128
              __FILE__, __LINE__, pReq->name, version);
129
    return TSDB_CODE_INVALID_MSG;
×
130
  }
131

132
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
1,875✔
133
  if (code) {
1,878✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
37!
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
37✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
1,841✔
139
    tdbFreeClear(value);
1,841✔
140

141
    if (uid != pReq->suid) {
1,841✔
142
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " not match, version:%" PRId64,
1!
143
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
144
      return TSDB_CODE_TDB_STB_NOT_EXIST;
1✔
145
    }
146
  }
147

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
1,840✔
149
  if (code) {
1,839!
150
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
151
              " not found, this is an internal error, version:%" PRId64,
152
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
153
    return TSDB_CODE_INTERNAL_ERROR;
×
154
  }
155
  if (info.suid != info.uid) {
1,839!
156
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a super table, version:%" PRId64,
×
157
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
158
    return TSDB_CODE_INVALID_MSG;
×
159
  }
160
  return code;
1,839✔
161
}
162

163
// Create Super Table
164
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
25,387✔
165
  int32_t code = TSDB_CODE_SUCCESS;
25,387✔
166

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
25,387✔
169
  if (code != TSDB_CODE_SUCCESS) {
25,545✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
60✔
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
57!
172
               pReq->name, pReq->suid, version);
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
57✔
174
    } else {
175
      TAOS_RETURN(code);
3✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
25,485✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
25,485✔
184
      .name = pReq->name,
25,485✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
25,485✔
188
  };
189
  if (pReq->rollup) {
25,485✔
190
    TABLE_SET_ROLLUP(entry.flags);
10✔
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
10✔
192
  }
193
  if (pReq->colCmpred) {
25,485✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
25,481✔
195
    entry.colCmpr = pReq->colCmpr;
25,481✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
25,485✔
199

200
  if (pReq->virtualStb) {
25,485✔
201
    TABLE_SET_VIRTUAL(entry.flags);
9✔
202
  }
203

204
  code = metaHandleEntry2(pMeta, &entry);
25,485✔
205
  if (TSDB_CODE_SUCCESS == code) {
25,561!
206
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
25,561!
207
             pReq->suid, version);
208
  } else {
209
    metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
210
              pReq->suid, tstrerror(code));
211
  }
212
  TAOS_RETURN(code);
25,561✔
213
}
214

215
// Drop Super Table
216
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
1,875✔
217
  int32_t code = TSDB_CODE_SUCCESS;
1,875✔
218

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
1,875✔
221
  if (code) {
1,876✔
222
    TAOS_RETURN(code);
38✔
223
  }
224

225
  // handle entry
226
  SMetaEntry entry = {
1,838✔
227
      .version = verison,
228
      .type = -TSDB_SUPER_TABLE,
229
      .uid = pReq->suid,
1,838✔
230
  };
231
  code = metaHandleEntry2(pMeta, &entry);
1,838✔
232
  if (code) {
1,840!
233
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
234
              tstrerror(code));
235
  } else {
236
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,840!
237
             pReq->suid, verison);
238
  }
239
  TAOS_RETURN(code);
1,841✔
240
}
241

242
// Alter Super Table
243

244
// Create Child Table
245
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
138,672✔
246
  int32_t   code = TSDB_CODE_SUCCESS;
138,672✔
247
  void     *value = NULL;
138,672✔
248
  int32_t   valueSize = 0;
138,672✔
249
  SMetaInfo info;
250

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
138,672!
252
      pReq->ctb.suid == 0) {
138,677!
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
254
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
255
    return TSDB_CODE_INVALID_MSG;
×
256
  }
257

258
  // check table existence
259
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
138,679✔
260
    pReq->uid = *(int64_t *)value;
3,785✔
261
    tdbFreeClear(value);
3,785✔
262

263
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
3,785!
264
      metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
265
                ", which is an internal error, version:%" PRId64,
266
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
267
      return TSDB_CODE_INTERNAL_ERROR;
×
268
    }
269

270
    // check table type
271
    if (info.suid == info.uid || info.suid == 0) {
3,785!
272
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
9!
273
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
274
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
9✔
275
    }
276

277
    // check suid
278
    if (info.suid != pReq->ctb.suid) {
3,776!
279
      metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " exists in another stable with uid %" PRId64
×
280
                " instead of stable with uid %" PRId64 " version:%" PRId64,
281
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, info.suid, pReq->ctb.suid,
282
                version);
283
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
284
    }
285

286
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
3,776✔
287
  }
288

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
134,890✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
134,890✔
292
  if (code) {
134,832✔
293
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
2!
294
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
295
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
296
  }
297

298
  if (pStbEntry->type != TSDB_SUPER_TABLE) {
134,830!
299
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64,
×
300
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
301
    metaFetchEntryFree(&pStbEntry);
×
302
    return TSDB_CODE_INVALID_MSG;
×
303
  }
304

305
  if (pStbEntry->uid != pReq->ctb.suid) {
134,830!
306
    metaError("vgId:%d, %s failed at %s:%d since super table %s uid %" PRId64 " does not match request uid %" PRId64
×
307
              ", version:%" PRId64,
308
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, pStbEntry->uid, pReq->ctb.suid,
309
              version);
310
    metaFetchEntryFree(&pStbEntry);
×
311
    return TSDB_CODE_INVALID_MSG;
×
312
  }
313

314
  // Check tag value
315
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
134,830✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
134,830✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
134,830✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
579,301✔
319
      STagVal tagVal = {
444,865✔
320
          .cid = pTagSchema->pSchema[i].colId,
444,865✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
444,865✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
441,327!
325
          metaError("vgId:%d, %s failed at %s:%d since child table %s tag type does not match the expected type, version:%" PRId64,
×
326
                    TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, version);
327
          metaFetchEntryFree(&pStbEntry);
×
328
          return TSDB_CODE_INVALID_MSG;
×
329
        }
330
      }
331
    }
332
  }
333

334
  metaFetchEntryFree(&pStbEntry);
134,857✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
134,882!
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
134,883✔
339
    if (TSDB_CODE_SUCCESS != code) {
134,820!
340
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
341
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
342
    }
343
  }
344
  return code;
134,825✔
345
}
346

347
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
134,806✔
348
  int32_t code = TSDB_CODE_SUCCESS;
134,806✔
349

350
  if (NULL == ppRsp) {
134,806!
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
134,806!
355
  if (NULL == ppRsp) {
134,858!
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
134,858✔
360
  (*ppRsp)->tuid = pEntry->uid;
134,858✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
134,858✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
134,858✔
363

364
  return code;
134,858✔
365
}
366

367
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
138,667✔
368
  int32_t code = TSDB_CODE_SUCCESS;
138,667✔
369

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
138,667✔
372
  if (code) {
138,596✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
3,785✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
9!
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
3,785✔
378
  }
379

380
  SMetaEntry entry = {
134,811✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
134,811✔
384
      .name = pReq->name,
134,811✔
385
      .ctbEntry.btime = pReq->btime,
134,811✔
386
      .ctbEntry.ttlDays = pReq->ttl,
134,811✔
387
      .ctbEntry.commentLen = pReq->commentLen,
134,811✔
388
      .ctbEntry.comment = pReq->comment,
134,811✔
389
      .ctbEntry.suid = pReq->ctb.suid,
134,811✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
134,811✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
134,811✔
395
  if (code) {
134,859!
396
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
397
              tstrerror(code));
398
  }
399

400
  // handle entry
401
  code = metaHandleEntry2(pMeta, &entry);
134,859✔
402
  if (TSDB_CODE_SUCCESS == code) {
134,870!
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
134,873!
404
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
405
  } else {
406
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
407
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
408
              pReq->ctb.suid, version);
409
  }
410
  return code;
134,899✔
411
}
412

413
// Drop Child Table
414

415
// Alter Child Table
416

417
// Create Normal Table
418
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
14,486✔
419
  int32_t code = 0;
14,486✔
420
  void   *value = NULL;
14,486✔
421
  int32_t valueSize = 0;
14,486✔
422

423
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
14,486!
424
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
425
              __FILE__, __LINE__, pReq->name, version);
426
    return TSDB_CODE_INVALID_MSG;
×
427
  }
428

429
  // check name
430
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
14,486✔
431
    // for auto create table, we return the uid of the existing table
432
    pReq->uid = *(tb_uid_t *)value;
7✔
433
    tdbFree(value);
7✔
434
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
7✔
435
  }
436

437
  // grant check
438
  code = grantCheck(TSDB_GRANT_TIMESERIES);
14,479✔
439
  if (code) {
14,479!
440
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
441
              __FILE__, __LINE__, tstrerror(code), version, pReq->name);
442
  }
443
  return code;
14,479✔
444
}
445

446
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
14,475✔
447
  int32_t code = TSDB_CODE_SUCCESS;
14,475✔
448

449
  if (NULL == ppRsp) {
14,475!
450
    return code;
×
451
  }
452

453
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
14,475!
454
  if (NULL == *ppRsp) {
14,475!
455
    return terrno;
×
456
  }
457

458
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp);
14,475✔
459
  if (code) {
14,475!
460
    taosMemoryFreeClear(*ppRsp);
×
461
    return code;
×
462
  }
463

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
253,792✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
239,317✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
239,317✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
239,317✔
468
    if (pEntry->pExtSchemas) {
239,317✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
220✔
470
    }
471
  }
472

473
  return code;
14,475✔
474
}
475

476
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
14,482✔
477
  int32_t code = TSDB_CODE_SUCCESS;
14,482✔
478

479
  // check request
480
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
14,482✔
481
  if (code) {
14,482✔
482
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
7!
483
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
484
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
485
    }
486
    TAOS_RETURN(code);
7✔
487
  }
488

489
  SMetaEntry entry = {
14,475✔
490
    .version = version,
491
    .type = TSDB_NORMAL_TABLE,
492
    .uid = pReq->uid,
14,475✔
493
    .name = pReq->name,
14,475✔
494
    .ntbEntry.btime = pReq->btime,
14,475✔
495
    .ntbEntry.ttlDays = pReq->ttl,
14,475✔
496
    .ntbEntry.commentLen = pReq->commentLen,
14,475✔
497
    .ntbEntry.comment = pReq->comment,
14,475✔
498
    .ntbEntry.schemaRow = pReq->ntb.schemaRow,
499
    .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
14,475✔
500
    .colCmpr = pReq->colCmpr,
501
    .pExtSchemas = pReq->pExtSchemas,
14,475✔
502
  };
503
  TABLE_SET_COL_COMPRESSED(entry.flags);
14,475✔
504

505
  // build response
506
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
14,475✔
507
  if (code) {
14,475!
508
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
509
              tstrerror(code));
510
  }
511

512
  // handle entry
513
  code = metaHandleEntry2(pMeta, &entry);
14,475✔
514
  if (TSDB_CODE_SUCCESS == code) {
14,475!
515
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
14,475!
516
             pReq->name, pReq->uid);
517
  } else {
518
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
519
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
520
  }
521
  TAOS_RETURN(code);
14,475✔
522
}
523

524
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
4✔
525
  int32_t code = TSDB_CODE_SUCCESS;
4✔
526

527
  if (NULL == ppRsp) {
4!
528
    return code;
×
529
  }
530

531
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
4!
532
  if (NULL == *ppRsp) {
4!
533
    return terrno;
×
534
  }
535

536
  code = metaUpdateVtbMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_NORMAL_TABLE);
4✔
537
  if (code) {
4!
538
    taosMemoryFreeClear(*ppRsp);
×
539
    return code;
×
540
  }
541

542
  return code;
4✔
543
}
544

545
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
4✔
546
  // check request
547
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
4✔
548
  if (code) {
4!
549
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
550
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
551
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
552
    }
553
    TAOS_RETURN(code);
×
554
  }
555

556
  SMetaEntry entry = {
4✔
557
      .version = version,
558
      .type = TSDB_VIRTUAL_NORMAL_TABLE,
559
      .uid = pReq->uid,
4✔
560
      .name = pReq->name,
4✔
561
      .ntbEntry.btime = pReq->btime,
4✔
562
      .ntbEntry.ttlDays = pReq->ttl,
4✔
563
      .ntbEntry.commentLen = pReq->commentLen,
4✔
564
      .ntbEntry.comment = pReq->comment,
4✔
565
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
566
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
4✔
567
      .colRef = pReq->colRef
568
  };
569

570
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
4✔
571
  if (code) {
4!
572
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
573
              tstrerror(code));
574
  }
575

576
  // handle entry
577
  code = metaHandleEntry2(pMeta, &entry);
4✔
578
  if (TSDB_CODE_SUCCESS == code) {
4!
579
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
4!
580
             version, pReq->name, pReq->uid);
581
  } else {
582
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
583
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
584
  }
585
  TAOS_RETURN(code);
4✔
586
#if 0
587
  metaTimeSeriesNotifyCheck(pMeta);
588
#endif
589
}
590

591
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
4✔
592
  int32_t code = TSDB_CODE_SUCCESS;
4✔
593

594
  if (NULL == ppRsp) {
4!
595
    return code;
×
596
  }
597

598
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
4!
599
  if (NULL == *ppRsp) {
4!
600
    return terrno;
×
601
  }
602

603
  code = metaUpdateVtbMetaRsp(pEntry->uid, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
4✔
604
  if (code) {
4!
605
    taosMemoryFreeClear(*ppRsp);
×
606
    return code;
×
607
  }
608
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
4✔
609

610
  return code;
4✔
611
}
612

613
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
4✔
614
  // check request
615
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
4✔
616
  if (code) {
4!
617
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
618
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
619
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
620
    }
621
    TAOS_RETURN(code);
×
622
  }
623

624
  SMetaEntry entry = {
4✔
625
      .version = version,
626
      .type = TSDB_VIRTUAL_CHILD_TABLE,
627
      .uid = pReq->uid,
4✔
628
      .name = pReq->name,
4✔
629
      .ctbEntry.btime = pReq->btime,
4✔
630
      .ctbEntry.ttlDays = pReq->ttl,
4✔
631
      .ctbEntry.commentLen = pReq->commentLen,
4✔
632
      .ctbEntry.comment = pReq->comment,
4✔
633
      .ctbEntry.suid = pReq->ctb.suid,
4✔
634
      .ctbEntry.pTags = pReq->ctb.pTag,
4✔
635
      .colRef = pReq->colRef
636
  };
637

638
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
4✔
639
  if (code) {
4!
640
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
641
              tstrerror(code));
642
  }
643

644
  // handle entry
645
  code = metaHandleEntry2(pMeta, &entry);
4✔
646
  if (TSDB_CODE_SUCCESS == code) {
4!
647
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
4!
648
             version, pReq->name, pReq->uid);
649
  } else {
650
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
651
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
652
  }
653
  TAOS_RETURN(code);
4✔
654
#if 0
655
  metaTimeSeriesNotifyCheck(pMeta);
656
#endif
657
}
658

659
// Drop Normal Table
660

661
// Alter Normal Table
662

663
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
153,155✔
664
  int32_t code = TSDB_CODE_SUCCESS;
153,155✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
153,155✔
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
138,667✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
14,488✔
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
14,481✔
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
7✔
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
4✔
671
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
3!
672
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
4✔
673
  } else {
674
    code = TSDB_CODE_INVALID_MSG;
×
675
  }
676
  TAOS_RETURN(code);
153,173✔
677
}
678

679
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
15,529✔
680
  int32_t code = TSDB_CODE_SUCCESS;
15,529✔
681

682
  // check request
683
  code = metaCheckDropTableReq(pMeta, version, pReq);
15,529✔
684
  if (code) {
15,529!
685
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
686
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
687
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
688
    }
689
    TAOS_RETURN(code);
×
690
  }
691

692
  if (pReq->suid == pReq->uid) {
15,529!
693
    code = TSDB_CODE_INVALID_PARA;
×
694
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
695
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
696
    TAOS_RETURN(code);
×
697
  }
698

699
  SMetaEntry entry = {
15,529✔
700
      .version = version,
701
      .uid = pReq->uid,
15,529✔
702
  };
703

704
  if (pReq->isVirtual) {
15,529✔
705
    if (pReq->suid == 0) {
6✔
706
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
3✔
707
    } else {
708
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
3✔
709
    }
710
  } else {
711
    if (pReq->suid == 0) {
15,523✔
712
      entry.type = -TSDB_NORMAL_TABLE;
2,224✔
713
    } else {
714
      entry.type = -TSDB_CHILD_TABLE;
13,299✔
715
    }
716
  }
717
  code = metaHandleEntry2(pMeta, &entry);
15,529✔
718
  if (code) {
15,529!
719
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
720
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
721
  } else {
722
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
15,529!
723
             pReq->uid, version);
724
  }
725
  TAOS_RETURN(code);
15,529✔
726
}
727

728
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
2,195✔
729
  int32_t code = 0;
2,195✔
730

731
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
2,195!
732
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
733
              __func__, __FILE__, __LINE__, pReq->colName, version);
734
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
735
  }
736

737
  // check name
738
  void   *value = NULL;
2,196✔
739
  int32_t valueSize = 0;
2,196✔
740
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
2,196✔
741
  if (code) {
2,195!
742
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
743
              __FILE__, __LINE__, pReq->tbName, version);
744
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
745
    TAOS_RETURN(code);
×
746
  }
747
  int64_t uid = *(int64_t *)value;
2,195✔
748
  tdbFreeClear(value);
2,195✔
749

750
  // check table type
751
  SMetaInfo info;
752
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
2,195!
753
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
754
              " not found, this is an internal error in meta, version:%" PRId64,
755
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
756
    code = TSDB_CODE_INTERNAL_ERROR;
×
757
    TAOS_RETURN(code);
×
758
  }
759
  if (info.suid != 0 && pReq->action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF && pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
2,195!
760
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
761
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
762
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
763
    TAOS_RETURN(code);
×
764
  }
765

766
  // check grant
767
  code = grantCheck(TSDB_GRANT_TIMESERIES);
2,195✔
768
  if (code) {
2,195!
769
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
770
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
771
    TAOS_RETURN(code);
×
772
  }
773
  TAOS_RETURN(code);
2,195✔
774
}
775

776
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
1,229✔
777
  int32_t code = TSDB_CODE_SUCCESS;
1,229✔
778

779
  // check request
780
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
1,229✔
781
  if (code) {
1,229!
782
    TAOS_RETURN(code);
×
783
  }
784

785
  // fetch old entry
786
  SMetaEntry *pEntry = NULL;
1,229✔
787
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
1,229✔
788
  if (code) {
1,229!
789
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
790
              __FILE__, __LINE__, pReq->tbName, version);
791
    TAOS_RETURN(code);
×
792
  }
793
  if (pEntry->version >= version) {
1,229!
794
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
795
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
796
    metaFetchEntryFree(&pEntry);
×
797
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
798
  }
799

800
  // do add column
801
  int32_t         rowSize = 0;
1,229✔
802
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
1,229✔
803
  SSchema        *pColumn;
804
  SExtSchema      extSchema = {0};
1,229✔
805
  pEntry->version = version;
1,229✔
806
  for (int32_t i = 0; i < pSchema->nCols; i++) {
50,264✔
807
    pColumn = &pSchema->pSchema[i];
49,534✔
808
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
49,534✔
809
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
499!
810
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
811
      metaFetchEntryFree(&pEntry);
499✔
812
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
499✔
813
    }
814
    rowSize += pColumn->bytes;
49,035✔
815
  }
816

817
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
730✔
818
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
10!
819
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, TSDB_MAX_BYTES_PER_ROW, version);
820
    metaFetchEntryFree(&pEntry);
10✔
821
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
10✔
822
  }
823

824
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
720!
825
  if (NULL == pNewSchema) {
720!
826
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
827
              __LINE__, tstrerror(terrno), version);
828
    metaFetchEntryFree(&pEntry);
×
829
    TAOS_RETURN(terrno);
×
830
  }
831
  pSchema->pSchema = pNewSchema;
720✔
832
  pSchema->version++;
720✔
833
  pSchema->nCols++;
720✔
834
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
720✔
835
  pColumn->bytes = pReq->bytes;
720✔
836
  pColumn->type = pReq->type;
720✔
837
  pColumn->flags = pReq->flags;
720✔
838
  pColumn->colId = pEntry->ntbEntry.ncid++;
720✔
839
  extSchema.typeMod = pReq->typeMod;
720✔
840
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
720✔
841
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
720✔
842
    SColRef tmpRef;
843
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
3!
844
      tmpRef.hasRef = false;
3✔
845
      tmpRef.id = pColumn->colId;
3✔
846
    } else {
847
      tmpRef.hasRef = true;
×
848
      tmpRef.id = pColumn->colId;
×
849
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
×
850
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
×
851
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
×
852
    }
853
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
3✔
854
    if (code) {
3!
855
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
856
                __LINE__, tstrerror(code), version);
857
      metaFetchEntryFree(&pEntry);
×
858
      TAOS_RETURN(code);
×
859
    }
860
  } else {
861
    uint32_t compress;
862
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
717✔
863
      compress = createDefaultColCmprByType(pColumn->type);
676✔
864
    } else {
865
      compress = pReq->compress;
41✔
866
    }
867
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
717✔
868
    if (code) {
717!
869
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
870
                __LINE__, tstrerror(code), version);
871
      metaFetchEntryFree(&pEntry);
×
872
      TAOS_RETURN(code);
×
873
    }
874
  }
875
  code = addTableExtSchema(pEntry, pColumn, pSchema->nCols, &extSchema);
720✔
876
  if (code) {
720!
877
    metaError("vgId:%d, %s failed to add ext schema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
878
              __func__, __FILE__, __LINE__, tstrerror(code), version);
879
    metaFetchEntryFree(&pEntry);
×
880
    TAOS_RETURN(code);
×
881
  }
882

883
  // do handle entry
884
  code = metaHandleEntry2(pMeta, pEntry);
720✔
885
  if (code) {
720!
886
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
887
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
888
    metaFetchEntryFree(&pEntry);
×
889
    TAOS_RETURN(code);
×
890
  } else {
891
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
720!
892
             pEntry->uid, version);
893
  }
894

895
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
720✔
896
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
3!
897
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
898
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
899
    } else {
900
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
15✔
901
        SColRef *p = &pEntry->colRef.pColRef[i];
12✔
902
        pRsp->pColRefs[i].hasRef = p->hasRef;
12✔
903
        pRsp->pColRefs[i].id = p->id;
12✔
904
        if (p->hasRef) {
12✔
905
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
6✔
906
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
6✔
907
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
6✔
908
        }
909
      }
910
    }
911
  } else {
912
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
717!
913
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
914
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
915
    } else {
916
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
36,692✔
917
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
35,975✔
918
        pRsp->pSchemaExt[i].colId = p->id;
35,975✔
919
        pRsp->pSchemaExt[i].compress = p->alg;
35,975✔
920
      }
921
    }
922
  }
923

924
  metaFetchEntryFree(&pEntry);
720✔
925
  TAOS_RETURN(code);
720✔
926
}
927

928
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
176✔
929
  int32_t code = TSDB_CODE_SUCCESS;
176✔
930

931
  // check request
932
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
176✔
933
  if (code) {
176!
934
    TAOS_RETURN(code);
×
935
  }
936

937
  // fetch old entry
938
  SMetaEntry *pEntry = NULL;
176✔
939
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
176✔
940
  if (code) {
176!
941
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
942
              __FILE__, __LINE__, pReq->tbName, version);
943
    TAOS_RETURN(code);
×
944
  }
945

946
  if (pEntry->version >= version) {
176!
947
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
948
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
949
    metaFetchEntryFree(&pEntry);
×
950
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
951
  }
952

953
  // search the column to drop
954
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
176✔
955
  SSchema        *pColumn = NULL;
176✔
956
  SSchema         tColumn;
957
  int32_t         iColumn = 0;
176✔
958
  for (; iColumn < pSchema->nCols; iColumn++) {
21,546!
959
    pColumn = &pSchema->pSchema[iColumn];
21,546✔
960
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
21,546✔
961
      break;
176✔
962
    }
963
  }
964

965
  if (iColumn == pSchema->nCols) {
176!
966
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
967
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
968
    metaFetchEntryFree(&pEntry);
×
969
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
970
  }
971

972
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
176!
973
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
974
              __func__, __FILE__, __LINE__, pReq->colName, version);
975
    metaFetchEntryFree(&pEntry);
×
976
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
977
  }
978

979
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
176✔
980
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
981
              __func__, __FILE__, __LINE__, pReq->colName, version);
982
    metaFetchEntryFree(&pEntry);
11✔
983
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
11✔
984
  }
985
  tColumn = *pColumn;
165✔
986

987
  // do drop column
988
  pEntry->version = version;
165✔
989
  if (pSchema->nCols - iColumn - 1 > 0) {
165✔
990
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
98✔
991
  }
992
  pSchema->nCols--;
165✔
993
  pSchema->version++;
165✔
994
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
165✔
995
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
3✔
996
    if (code) {
3!
997
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
998
                __LINE__, tstrerror(code), version);
999
      metaFetchEntryFree(&pEntry);
×
1000
      TAOS_RETURN(code);
×
1001
    }
1002
    if (pEntry->colRef.nCols != pSchema->nCols) {
3!
1003
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1004
                __func__, __FILE__, __LINE__, version);
1005
      metaFetchEntryFree(&pEntry);
×
1006
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1007
    }
1008
  } else {
1009
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
162✔
1010
    if (code) {
162!
1011
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1012
                __LINE__, tstrerror(code), version);
1013
      metaFetchEntryFree(&pEntry);
×
1014
      TAOS_RETURN(code);
×
1015
    }
1016
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
162!
1017
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1018
                __func__, __FILE__, __LINE__, version);
1019
      metaFetchEntryFree(&pEntry);
×
1020
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1021
    }
1022
  }
1023

1024
  // update column extschema
1025
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
165✔
1026
  if (code) {
165!
1027
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1028
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1029
    metaFetchEntryFree(&pEntry);
×
1030
    TAOS_RETURN(code);
×
1031
  }
1032

1033
  // do handle entry
1034
  code = metaHandleEntry2(pMeta, pEntry);
165✔
1035
  if (code) {
165!
1036
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1037
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1038
    metaFetchEntryFree(&pEntry);
×
1039
  } else {
1040
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
165!
1041
             pEntry->uid, version);
1042
  }
1043

1044
  // build response
1045
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
165✔
1046
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
3!
1047
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1048
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1049
    } else {
1050
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
12✔
1051
        SColRef *p = &pEntry->colRef.pColRef[i];
9✔
1052
        pRsp->pColRefs[i].hasRef = p->hasRef;
9✔
1053
        pRsp->pColRefs[i].id = p->id;
9✔
1054
        if (p->hasRef) {
9✔
1055
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
6✔
1056
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
6✔
1057
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
6✔
1058
        }
1059
      }
1060
    }
1061
  } else {
1062
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
162!
1063
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1064
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1065
    } else {
1066
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
21,812✔
1067
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
21,650✔
1068
        pRsp->pSchemaExt[i].colId = p->id;
21,650✔
1069
        pRsp->pSchemaExt[i].compress = p->alg;
21,650✔
1070
      }
1071
    }
1072
  }
1073

1074
  metaFetchEntryFree(&pEntry);
165✔
1075
  TAOS_RETURN(code);
165✔
1076
}
1077

1078
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
46✔
1079
  int32_t code = TSDB_CODE_SUCCESS;
46✔
1080

1081
  // check request
1082
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
46✔
1083
  if (code) {
46!
1084
    TAOS_RETURN(code);
×
1085
  }
1086

1087
  if (NULL == pReq->colNewName) {
46!
1088
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1089
              __func__, __FILE__, __LINE__, version);
1090
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1091
  }
1092

1093
  // fetch old entry
1094
  SMetaEntry *pEntry = NULL;
46✔
1095
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
46✔
1096
  if (code) {
46!
1097
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1098
              __FILE__, __LINE__, pReq->tbName, version);
1099
    TAOS_RETURN(code);
×
1100
  }
1101

1102
  if (pEntry->version >= version) {
46!
1103
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1104
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1105
    metaFetchEntryFree(&pEntry);
×
1106
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1107
  }
1108

1109
  // search the column to update
1110
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
46✔
1111
  SSchema        *pColumn = NULL;
46✔
1112
  int32_t         iColumn = 0;
46✔
1113
  for (int32_t i = 0; i < pSchema->nCols; i++) {
231!
1114
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
231✔
1115
      pColumn = &pSchema->pSchema[i];
46✔
1116
      iColumn = i;
46✔
1117
      break;
46✔
1118
    }
1119
  }
1120

1121
  if (NULL == pColumn) {
46!
1122
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1123
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1124
    metaFetchEntryFree(&pEntry);
×
1125
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1126
  }
1127

1128
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
46✔
1129
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
1130
              __func__, __FILE__, __LINE__, pColumn->name, version);
1131
    metaFetchEntryFree(&pEntry);
11✔
1132
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
11✔
1133
  }
1134

1135
  // do update column name
1136
  pEntry->version = version;
35✔
1137
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
35✔
1138
  pSchema->version++;
35✔
1139

1140
  // do handle entry
1141
  code = metaHandleEntry2(pMeta, pEntry);
35✔
1142
  if (code) {
35!
1143
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1144
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1145
    metaFetchEntryFree(&pEntry);
×
1146
    TAOS_RETURN(code);
×
1147
  } else {
1148
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
35!
1149
             pEntry->uid, version);
1150
  }
1151

1152
  // build response
1153
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
35!
1154
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1155
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1156
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1157
    } else {
1158
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1159
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1160
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1161
        pRsp->pColRefs[i].id = p->id;
×
1162
        if (p->hasRef) {
×
1163
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1164
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1165
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1166
        }
1167
      }
1168
    }
1169
  } else {
1170
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
35!
1171
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1172
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1173
    } else {
1174
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
331✔
1175
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
296✔
1176
        pRsp->pSchemaExt[i].colId = p->id;
296✔
1177
        pRsp->pSchemaExt[i].compress = p->alg;
296✔
1178
      }
1179
    }
1180
  }
1181

1182
  metaFetchEntryFree(&pEntry);
35✔
1183
  TAOS_RETURN(code);
35✔
1184
}
1185

1186
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
744✔
1187
  int32_t code = TSDB_CODE_SUCCESS;
744✔
1188

1189
  // check request
1190
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
744✔
1191
  if (code) {
744!
1192
    TAOS_RETURN(code);
×
1193
  }
1194

1195
  // fetch old entry
1196
  SMetaEntry *pEntry = NULL;
744✔
1197
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
744✔
1198
  if (code) {
744!
1199
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1200
              __FILE__, __LINE__, pReq->tbName, version);
1201
    TAOS_RETURN(code);
×
1202
  }
1203

1204
  if (pEntry->version >= version) {
744!
1205
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1206
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1207
    metaFetchEntryFree(&pEntry);
×
1208
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1209
  }
1210

1211
  // search the column to update
1212
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
744✔
1213
  SSchema        *pColumn = NULL;
744✔
1214
  int32_t         iColumn = 0;
744✔
1215
  int32_t         rowSize = 0;
744✔
1216
  for (int32_t i = 0; i < pSchema->nCols; i++) {
29,664✔
1217
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
28,920✔
1218
      pColumn = &pSchema->pSchema[i];
745✔
1219
      iColumn = i;
745✔
1220
    }
1221
    rowSize += pSchema->pSchema[i].bytes;
28,920✔
1222
  }
1223

1224
  if (NULL == pColumn) {
744!
1225
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1226
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1227
    metaFetchEntryFree(&pEntry);
×
1228
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1229
  }
1230

1231
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
744!
1232
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
301!
1233
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1234
              version);
1235
    metaFetchEntryFree(&pEntry);
301✔
1236
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
301✔
1237
  }
1238

1239
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
443✔
1240
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
12!
1241
              __func__, __FILE__, __LINE__, pReq->colName, version);
1242
    metaFetchEntryFree(&pEntry);
12✔
1243
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
13✔
1244
  }
1245

1246
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
431✔
1247
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
70!
1248
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW,
1249
              version);
1250
    metaFetchEntryFree(&pEntry);
70✔
1251
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
70✔
1252
  }
1253

1254
  // do change the column bytes
1255
  pEntry->version = version;
361✔
1256
  pSchema->version++;
361✔
1257
  pColumn->bytes = pReq->colModBytes;
361✔
1258

1259
  // do handle entry
1260
  code = metaHandleEntry2(pMeta, pEntry);
361✔
1261
  if (code) {
361!
1262
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1263
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1264
    metaFetchEntryFree(&pEntry);
×
1265
    TAOS_RETURN(code);
×
1266
  } else {
1267
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
361!
1268
             pEntry->uid, version);
1269
  }
1270

1271
  // build response
1272
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
361!
1273
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1274
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1275
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1276
    } else {
1277
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1278
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1279
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1280
        pRsp->pColRefs[i].id = p->id;
×
1281
        if (p->hasRef) {
×
1282
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1283
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1284
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1285
        }
1286
      }
1287
    }
1288
  } else {
1289
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
361!
1290
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1291
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1292
    } else {
1293
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
14,027✔
1294
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
13,666✔
1295
        pRsp->pSchemaExt[i].colId = p->id;
13,666✔
1296
        pRsp->pSchemaExt[i].compress = p->alg;
13,666✔
1297
      }
1298
    }
1299
  }
1300

1301
  metaFetchEntryFree(&pEntry);
361✔
1302
  TAOS_RETURN(code);
361✔
1303
}
1304

1305
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,167✔
1306
  int32_t code = 0;
3,167✔
1307

1308
  // check tag name
1309
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
3,167!
1310
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1311
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1312
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1313
  }
1314

1315
  // check name
1316
  void   *value = NULL;
3,167✔
1317
  int32_t valueSize = 0;
3,167✔
1318
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,167✔
1319
  if (code) {
3,167✔
1320
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1!
1321
              __FILE__, __LINE__, pReq->tbName, version);
1322
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1✔
1323
    TAOS_RETURN(code);
1✔
1324
  }
1325
  tdbFreeClear(value);
3,166✔
1326

1327
  TAOS_RETURN(code);
3,166✔
1328
}
1329

1330
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,167✔
1331
  int32_t code = TSDB_CODE_SUCCESS;
3,167✔
1332

1333
  // check request
1334
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
3,167✔
1335
  if (code) {
3,167✔
1336
    TAOS_RETURN(code);
1✔
1337
  }
1338

1339
  // fetch child entry
1340
  SMetaEntry *pChild = NULL;
3,166✔
1341
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,166✔
1342
  if (code) {
3,166!
1343
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1344
              __FILE__, __LINE__, pReq->tbName, version);
1345
    TAOS_RETURN(code);
×
1346
  }
1347

1348
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
3,166!
1349
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1350
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1351
    metaFetchEntryFree(&pChild);
×
1352
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1353
  }
1354

1355
  // fetch super entry
1356
  SMetaEntry *pSuper = NULL;
3,166✔
1357
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,166✔
1358
  if (code) {
3,166!
1359
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1360
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1361
    metaFetchEntryFree(&pChild);
×
1362
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1363
  }
1364

1365
  // search the tag to update
1366
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,166✔
1367
  SSchema        *pColumn = NULL;
3,166✔
1368
  int32_t         iColumn = 0;
3,166✔
1369
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
5,411!
1370
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
5,411✔
1371
      pColumn = &pTagSchema->pSchema[i];
3,166✔
1372
      iColumn = i;
3,166✔
1373
      break;
3,166✔
1374
    }
1375
  }
1376

1377
  if (NULL == pColumn) {
3,166!
1378
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1379
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1380
    metaFetchEntryFree(&pChild);
×
1381
    metaFetchEntryFree(&pSuper);
×
1382
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1383
  }
1384

1385
  // do change tag value
1386
  pChild->version = version;
3,166✔
1387
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,166✔
1388
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
205!
1389
    if (NULL == pNewTag) {
205!
1390
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1391
                __LINE__, tstrerror(terrno), version);
1392
      metaFetchEntryFree(&pChild);
×
1393
      metaFetchEntryFree(&pSuper);
×
1394
      TAOS_RETURN(terrno);
×
1395
    }
1396
    pChild->ctbEntry.pTags = pNewTag;
205✔
1397
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
205✔
1398
  } else {
1399
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
2,961✔
1400

1401
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
2,961✔
1402
    if (NULL == pTagArray) {
2,961!
1403
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1404
                __LINE__, tstrerror(terrno), version);
1405
      metaFetchEntryFree(&pChild);
×
1406
      metaFetchEntryFree(&pSuper);
×
1407
      TAOS_RETURN(terrno);
×
1408
    }
1409

1410
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
10,037✔
1411
      STagVal value = {
7,076✔
1412
          .type = pTagSchema->pSchema[i].type,
7,076✔
1413
          .cid = pTagSchema->pSchema[i].colId,
7,076✔
1414
      };
1415

1416
      if (iColumn == i) {
7,076✔
1417
        if (pReq->isNull) {
2,961✔
1418
          continue;
973✔
1419
        }
1420
        if (IS_VAR_DATA_TYPE(value.type)) {
2,705!
1421
          value.pData = pReq->pTagVal;
1,186✔
1422
          value.nData = pReq->nTagVal;
1,186✔
1423
        } else {
1424
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
1,519✔
1425
        }
1426
      } else if (!tTagGet(pOldTag, &value)) {
4,115✔
1427
        continue;
717✔
1428
      }
1429

1430
      if (NULL == taosArrayPush(pTagArray, &value)) {
6,103!
1431
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1432
                  __LINE__, tstrerror(terrno), version);
1433
        taosArrayDestroy(pTagArray);
×
1434
        metaFetchEntryFree(&pChild);
×
1435
        metaFetchEntryFree(&pSuper);
×
1436
        TAOS_RETURN(terrno);
×
1437
      }
1438
    }
1439

1440
    STag *pNewTag = NULL;
2,961✔
1441
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
2,961✔
1442
    if (code) {
2,961!
1443
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1444
                __LINE__, tstrerror(code), version);
1445
      taosArrayDestroy(pTagArray);
×
1446
      metaFetchEntryFree(&pChild);
×
1447
      metaFetchEntryFree(&pSuper);
×
1448
      TAOS_RETURN(code);
×
1449
    }
1450
    taosArrayDestroy(pTagArray);
2,961✔
1451
    taosMemoryFree(pChild->ctbEntry.pTags);
2,961!
1452
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
2,961✔
1453
  }
1454

1455
  // do handle entry
1456
  code = metaHandleEntry2(pMeta, pChild);
3,166✔
1457
  if (code) {
3,166!
1458
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1459
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1460
    metaFetchEntryFree(&pChild);
×
1461
    metaFetchEntryFree(&pSuper);
×
1462
    TAOS_RETURN(code);
×
1463
  } else {
1464
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,166!
1465
             pChild->uid, version);
1466
  }
1467

1468
  // free resource and return
1469
  metaFetchEntryFree(&pChild);
3,166✔
1470
  metaFetchEntryFree(&pSuper);
3,166✔
1471
  TAOS_RETURN(code);
3,166✔
1472
}
1473

1474
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1475
  int32_t code = 0;
5✔
1476

1477
  // check tag name
1478
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
1479
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1480
              __FILE__, __LINE__, version);
1481
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1482
  }
1483

1484
  // check name
1485
  void   *value = NULL;
5✔
1486
  int32_t valueSize = 0;
5✔
1487
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
5✔
1488
  if (code) {
5!
1489
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1490
              __FILE__, __LINE__, pReq->tbName, version);
1491
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1492
    TAOS_RETURN(code);
×
1493
  }
1494
  tdbFreeClear(value);
5✔
1495

1496
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
1497
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1498
              __FILE__, __LINE__, version);
1499
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1500
  }
1501

1502
  TAOS_RETURN(code);
5✔
1503
}
1504

1505
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1506
  int32_t code = TSDB_CODE_SUCCESS;
5✔
1507

1508
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
5✔
1509
  if (code) {
5!
1510
    TAOS_RETURN(code);
×
1511
  }
1512

1513
  // fetch child entry
1514
  SMetaEntry *pChild = NULL;
5✔
1515
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
5✔
1516
  if (code) {
5!
1517
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1518
              __FILE__, __LINE__, pReq->tbName, version);
1519
    TAOS_RETURN(code);
×
1520
  }
1521

1522
  if (pChild->type != TSDB_CHILD_TABLE) {
5!
1523
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1524
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1525
    metaFetchEntryFree(&pChild);
×
1526
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1527
  }
1528

1529
  // fetch super entry
1530
  SMetaEntry *pSuper = NULL;
5✔
1531
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
5✔
1532
  if (code) {
5!
1533
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1534
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1535
    metaFetchEntryFree(&pChild);
×
1536
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1537
  }
1538

1539
  // search the tags to update
1540
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
5✔
1541

1542
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5!
1543
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1544
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1545
    metaFetchEntryFree(&pChild);
×
1546
    metaFetchEntryFree(&pSuper);
×
1547
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1548
  }
1549

1550
  // do check if tag name exists
1551
  SHashObj *pTagTable =
1552
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5✔
1553
  if (pTagTable == NULL) {
5!
1554
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1555
              __LINE__, tstrerror(terrno), version);
1556
    metaFetchEntryFree(&pChild);
×
1557
    metaFetchEntryFree(&pSuper);
×
1558
    TAOS_RETURN(terrno);
×
1559
  }
1560

1561
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
35✔
1562
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
30✔
1563
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
30!
1564
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1565
                __LINE__, tstrerror(terrno), version);
1566
      taosHashCleanup(pTagTable);
×
1567
      metaFetchEntryFree(&pChild);
×
1568
      metaFetchEntryFree(&pSuper);
×
1569
      TAOS_RETURN(terrno);
×
1570
    }
1571
  }
1572

1573
  int32_t numOfChangedTags = 0;
5✔
1574
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1575
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
35✔
1576
        ? numOfChangedTags++
30✔
1577
        : 0;
35✔
1578
  }
1579
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
5!
1580
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1581
              __FILE__, __LINE__, version);
1582
    taosHashCleanup(pTagTable);
×
1583
    metaFetchEntryFree(&pChild);
×
1584
    metaFetchEntryFree(&pSuper);
×
1585
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1586
  }
1587

1588
  // do change tag value
1589
  pChild->version = version;
5✔
1590
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
5✔
1591
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
5✔
1592
  if (NULL == pTagArray) {
5!
1593
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1594
              __LINE__, tstrerror(terrno), version);
1595
    taosHashCleanup(pTagTable);
×
1596
    metaFetchEntryFree(&pChild);
×
1597
    metaFetchEntryFree(&pSuper);
×
1598
    TAOS_RETURN(terrno);
×
1599
  }
1600

1601
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1602
    SSchema *pCol = &pTagSchema->pSchema[i];
35✔
1603
    STagVal  value = {
35✔
1604
         .cid = pCol->colId,
35✔
1605
    };
1606

1607
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
35✔
1608
    if (pTagVal == NULL) {
35✔
1609
      if (!tTagGet(pOldTag, &value)) {
5!
1610
        continue;
6✔
1611
      }
1612
    } else {
1613
      value.type = pCol->type;
30✔
1614
      if (pTagVal->isNull) {
30✔
1615
        continue;
6✔
1616
      }
1617

1618
      if (IS_VAR_DATA_TYPE(pCol->type)) {
24!
1619
        value.pData = pTagVal->pTagVal;
4✔
1620
        value.nData = pTagVal->nTagVal;
4✔
1621
      } else {
1622
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
20✔
1623
      }
1624
    }
1625

1626
    if (taosArrayPush(pTagArray, &value) == NULL) {
29!
1627
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1628
                __LINE__, tstrerror(terrno), version);
1629
      taosHashCleanup(pTagTable);
×
1630
      taosArrayDestroy(pTagArray);
×
1631
      metaFetchEntryFree(&pChild);
×
1632
      metaFetchEntryFree(&pSuper);
×
1633
      TAOS_RETURN(terrno);
×
1634
    }
1635
  }
1636

1637
  STag *pNewTag = NULL;
5✔
1638
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
5✔
1639
  if (code) {
5!
1640
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1641
              __LINE__, tstrerror(code), version);
1642
    taosHashCleanup(pTagTable);
×
1643
    taosArrayDestroy(pTagArray);
×
1644
    metaFetchEntryFree(&pChild);
×
1645
    metaFetchEntryFree(&pSuper);
×
1646
    TAOS_RETURN(code);
×
1647
  }
1648
  taosArrayDestroy(pTagArray);
5✔
1649
  taosMemoryFree(pChild->ctbEntry.pTags);
5!
1650
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
5✔
1651

1652
  // do handle entry
1653
  code = metaHandleEntry2(pMeta, pChild);
5✔
1654
  if (code) {
5!
1655
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1656
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1657
    taosHashCleanup(pTagTable);
×
1658
    metaFetchEntryFree(&pChild);
×
1659
    metaFetchEntryFree(&pSuper);
×
1660
    TAOS_RETURN(code);
×
1661
  } else {
1662
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5!
1663
             pChild->uid, version);
1664
  }
1665

1666
  taosHashCleanup(pTagTable);
5✔
1667
  metaFetchEntryFree(&pChild);
5✔
1668
  metaFetchEntryFree(&pSuper);
5✔
1669
  TAOS_RETURN(code);
5✔
1670
}
1671

1672
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
105✔
1673
  int32_t code = TSDB_CODE_SUCCESS;
105✔
1674

1675
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
105!
1676
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1677
              __FILE__, __LINE__, version);
1678
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1679
  }
1680

1681
  return code;
105✔
1682
}
1683

1684
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
105✔
1685
  int32_t code = 0;
105✔
1686

1687
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
105✔
1688
  if (code) {
105!
1689
    TAOS_RETURN(code);
×
1690
  }
1691

1692
  // fetch entry
1693
  SMetaEntry *pEntry = NULL;
105✔
1694
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
105✔
1695
  if (code) {
105!
1696
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1697
              __FILE__, __LINE__, pReq->tbName, version);
1698
    TAOS_RETURN(code);
×
1699
  }
1700

1701
  // do change the entry
1702
  pEntry->version = version;
105✔
1703
  if (pEntry->type == TSDB_CHILD_TABLE) {
105✔
1704
    if (pReq->updateTTL) {
51✔
1705
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
20✔
1706
    }
1707
    if (pReq->newCommentLen >= 0) {
51✔
1708
      char *pNewComment = NULL;
31✔
1709
      if (pReq->newCommentLen) {
31✔
1710
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
23!
1711
        if (NULL == pNewComment) {
23!
1712
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1713
                    __LINE__, tstrerror(terrno), version);
1714
          metaFetchEntryFree(&pEntry);
×
1715
          TAOS_RETURN(terrno);
×
1716
        }
1717
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
23✔
1718
      } else {
1719
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
8!
1720
      }
1721
      pEntry->ctbEntry.comment = pNewComment;
31✔
1722
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
31✔
1723
    }
1724
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
54!
1725
    if (pReq->updateTTL) {
54✔
1726
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
22✔
1727
    }
1728
    if (pReq->newCommentLen >= 0) {
54✔
1729
      char *pNewComment = NULL;
32✔
1730
      if (pReq->newCommentLen > 0) {
32✔
1731
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
24!
1732
        if (NULL == pNewComment) {
24!
1733
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1734
                    __LINE__, tstrerror(terrno), version);
1735
          metaFetchEntryFree(&pEntry);
×
1736
          TAOS_RETURN(terrno);
×
1737
        }
1738
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
24✔
1739
      } else {
1740
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
8!
1741
      }
1742
      pEntry->ntbEntry.comment = pNewComment;
32✔
1743
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
32✔
1744
    }
1745
  } else {
1746
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1747
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1748
    metaFetchEntryFree(&pEntry);
×
1749
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1750
  }
1751

1752
  // do handle entry
1753
  code = metaHandleEntry2(pMeta, pEntry);
105✔
1754
  if (code) {
105!
1755
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1756
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1757
    metaFetchEntryFree(&pEntry);
×
1758
    TAOS_RETURN(code);
×
1759
  } else {
1760
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
105!
1761
             pEntry->uid, version);
1762
  }
1763

1764
  metaFetchEntryFree(&pEntry);
105✔
1765
  TAOS_RETURN(code);
105✔
1766
}
1767

1768
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
15✔
1769
  int32_t code = TSDB_CODE_SUCCESS;
15✔
1770

1771
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
15!
1772
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1773
              __FILE__, __LINE__, version);
1774
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1775
  }
1776

1777
  SMetaEntry *pEntry = NULL;
15✔
1778
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
15✔
1779
  if (code) {
15!
1780
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1781
              __FILE__, __LINE__, pReq->tbName, version);
1782
    TAOS_RETURN(code);
×
1783
  }
1784

1785
  if (pEntry->version >= version) {
15!
1786
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1787
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1788
    metaFetchEntryFree(&pEntry);
×
1789
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1790
  }
1791

1792
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
15!
1793
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1794
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1795
    metaFetchEntryFree(&pEntry);
×
1796
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1797
  }
1798

1799
  // do change the entry
1800
  int8_t           updated = 0;
15✔
1801
  SColCmprWrapper *wp = &pEntry->colCmpr;
15✔
1802
  for (int32_t i = 0; i < wp->nCols; i++) {
120✔
1803
    SColCmpr *p = &wp->pColCmpr[i];
105✔
1804
    if (p->id == pReq->colId) {
105✔
1805
      uint32_t dst = 0;
15✔
1806
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
15✔
1807
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1808
      if (updated > 0) {
15!
1809
        p->alg = dst;
15✔
1810
      }
1811
    }
1812
  }
1813

1814
  if (updated == 0) {
15!
1815
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1816
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1817
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1818
    metaFetchEntryFree(&pEntry);
×
1819
    TAOS_RETURN(code);
×
1820
  } else if (updated < 0) {
15!
1821
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1822
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1823
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1824
    metaFetchEntryFree(&pEntry);
×
1825
    TAOS_RETURN(code);
×
1826
  }
1827

1828
  pEntry->version = version;
15✔
1829

1830
  // do handle entry
1831
  code = metaHandleEntry2(pMeta, pEntry);
15✔
1832
  if (code) {
15!
1833
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1834
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1835
    metaFetchEntryFree(&pEntry);
×
1836
    TAOS_RETURN(code);
×
1837
  } else {
1838
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
15!
1839
             pEntry->uid, version);
1840
  }
1841

1842
  metaFetchEntryFree(&pEntry);
15✔
1843
  TAOS_RETURN(code);
15✔
1844
}
1845

1846
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1847
  int32_t code = TSDB_CODE_SUCCESS;
×
1848

1849
  // check request
1850
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1851
  if (code) {
×
1852
    TAOS_RETURN(code);
×
1853
  }
1854

1855
  if (NULL == pReq->refDbName) {
×
1856
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1857
              __func__, __FILE__, __LINE__, version);
1858
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1859
  }
1860

1861
  if (NULL == pReq->refTbName) {
×
1862
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1863
              __func__, __FILE__, __LINE__, version);
1864
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1865
  }
1866

1867
  if (NULL == pReq->refColName) {
×
1868
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1869
              __func__, __FILE__, __LINE__, version);
1870
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1871
  }
1872

1873

1874
  // fetch old entry
1875
  SMetaEntry *pEntry = NULL;
×
1876
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1877
  if (code) {
×
1878
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1879
              __FILE__, __LINE__, pReq->tbName, version);
1880
    TAOS_RETURN(code);
×
1881
  }
1882

1883
  if (pEntry->version >= version) {
×
1884
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1885
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1886
    metaFetchEntryFree(&pEntry);
×
1887
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1888
  }
1889

1890
  // fetch super entry
1891
  SMetaEntry *pSuper = NULL;
×
1892
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
×
1893
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
×
1894
    if (code) {
×
1895
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1896
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1897
      metaFetchEntryFree(&pEntry);
×
1898
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1899
    }
1900
  }
1901

1902
  // search the column to update
1903
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
×
1904
  SColRef        *pColRef = NULL;
×
1905
  int32_t         iColumn = 0;
×
1906
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1907
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1908
      pColRef = &pEntry->colRef.pColRef[i];
×
1909
      iColumn = i;
×
1910
      break;
×
1911
    }
1912
  }
1913

1914
  if (NULL == pColRef) {
×
1915
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1916
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1917
    metaFetchEntryFree(&pEntry);
×
1918
    metaFetchEntryFree(&pSuper);
×
1919
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1920
  }
1921

1922
  // do update column name
1923
  pEntry->version = version;
×
1924
  pColRef->hasRef = true;
×
1925
  pColRef->id = pSchema->pSchema[iColumn].colId;
×
1926
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
×
1927
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
×
1928
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
×
1929
  pSchema->version++;
×
NEW
1930
  pEntry->colRef.version++;
×
1931

1932
  // do handle entry
1933
  code = metaHandleEntry2(pMeta, pEntry);
×
1934
  if (code) {
×
1935
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1936
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1937
    metaFetchEntryFree(&pEntry);
×
1938
    metaFetchEntryFree(&pSuper);
×
1939
    TAOS_RETURN(code);
×
1940
  } else {
1941
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1942
             pEntry->uid, version);
1943
  }
1944

1945
  // build response
1946
  if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1947
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1948
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1949
  } else {
1950
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1951
      SColRef *p = &pEntry->colRef.pColRef[i];
×
1952
      pRsp->pColRefs[i].hasRef = p->hasRef;
×
1953
      pRsp->pColRefs[i].id = p->id;
×
1954
      if (p->hasRef) {
×
1955
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1956
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1957
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1958
      }
1959
    }
1960
  }
1961

1962
  metaFetchEntryFree(&pEntry);
×
1963
  metaFetchEntryFree(&pSuper);
×
1964
  TAOS_RETURN(code);
×
1965
}
1966

1967
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1968
  int32_t code = TSDB_CODE_SUCCESS;
×
1969

1970
  // check request
1971
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1972
  if (code) {
×
1973
    TAOS_RETURN(code);
×
1974
  }
1975

1976
  // fetch old entry
1977
  SMetaEntry *pEntry = NULL;
×
1978
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1979
  if (code) {
×
1980
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1981
              __FILE__, __LINE__, pReq->tbName, version);
1982
    TAOS_RETURN(code);
×
1983
  }
1984

1985
  if (pEntry->version >= version) {
×
1986
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1987
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1988
    metaFetchEntryFree(&pEntry);
×
1989
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1990
  }
1991

1992
  // fetch super entry
1993
  SMetaEntry *pSuper = NULL;
×
1994
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
×
1995
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
×
1996
    if (code) {
×
1997
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1998
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1999
      metaFetchEntryFree(&pEntry);
×
2000
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2001
    }
2002
  }
2003

2004
  // search the column to update
2005
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
×
2006
  SColRef        *pColRef = NULL;
×
2007
  int32_t         iColumn = 0;
×
2008
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
2009
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
2010
      pColRef = &pEntry->colRef.pColRef[i];
×
2011
      iColumn = i;
×
2012
      break;
×
2013
    }
2014
  }
2015

2016
  if (NULL == pColRef) {
×
2017
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2018
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2019
    metaFetchEntryFree(&pEntry);
×
2020
    metaFetchEntryFree(&pSuper);
×
2021
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2022
  }
2023

2024
  // do update column name
2025
  pEntry->version = version;
×
2026
  pColRef->hasRef = false;
×
2027
  pColRef->id = pSchema->pSchema[iColumn].colId;
×
2028
  pSchema->version++;
×
NEW
2029
  pEntry->colRef.version++;
×
2030

2031
  // do handle entry
2032
  code = metaHandleEntry2(pMeta, pEntry);
×
2033
  if (code) {
×
2034
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2035
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2036
    metaFetchEntryFree(&pEntry);
×
2037
    metaFetchEntryFree(&pSuper);
×
2038
    TAOS_RETURN(code);
×
2039
  } else {
2040
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
2041
             pEntry->uid, version);
2042
  }
2043

2044
  // build response
2045
  if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
2046
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2047
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2048
  } else {
2049
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
2050
      SColRef *p = &pEntry->colRef.pColRef[i];
×
2051
      pRsp->pColRefs[i].hasRef = p->hasRef;
×
2052
      pRsp->pColRefs[i].id = p->id;
×
2053
      if (p->hasRef) {
×
2054
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
2055
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
2056
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
2057
      }
2058
    }
2059
  }
2060

2061
  metaFetchEntryFree(&pEntry);
×
2062
  metaFetchEntryFree(&pSuper);
×
2063
  TAOS_RETURN(code);
×
2064
}
2065

2066
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
17✔
2067
  int32_t code = TSDB_CODE_SUCCESS;
17✔
2068

2069
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
17!
2070
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2071
              __FILE__, __LINE__, version);
2072
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2073
  }
2074

2075
  SMetaEntry *pEntry = NULL;
17✔
2076
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
17✔
2077
  if (code) {
17!
2078
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2079
              __FILE__, __LINE__, pReq->name, version);
2080
    TAOS_RETURN(code);
×
2081
  }
2082

2083
  if (pEntry->type != TSDB_SUPER_TABLE) {
17!
2084
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2085
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2086
    metaFetchEntryFree(&pEntry);
×
2087
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2088
  }
2089

2090
  if (pEntry->uid != pReq->suid) {
17!
2091
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2092
              ", version:%" PRId64,
2093
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2094
    metaFetchEntryFree(&pEntry);
×
2095
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2096
  }
2097

2098
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2099
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2100
  //   PRId64,
2101
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2102
  //             pReq->schemaTag.version, version);
2103
  //   metaFetchEntryFree(&pEntry);
2104
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2105
  // }
2106

2107
  // do change the entry
2108
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
17✔
2109
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
17✔
2110
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
17!
2111
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2112
              __func__, __FILE__, __LINE__, pReq->name, version);
2113
    metaFetchEntryFree(&pEntry);
×
2114
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2115
  }
2116

2117
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
17!
2118
    metaError(
×
2119
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2120
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2121
        version);
2122
    metaFetchEntryFree(&pEntry);
×
2123
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2124
  }
2125

2126
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2127
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2128
  //   PRId64,
2129
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2130
  //             pNewTagSchema->version, version);
2131
  //   metaFetchEntryFree(&pEntry);
2132
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2133
  // }
2134

2135
  int32_t numOfChangedTags = 0;
17✔
2136
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
76✔
2137
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
59✔
2138
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
59✔
2139

2140
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
59!
2141
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
59!
2142
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2143
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2144
      metaFetchEntryFree(&pEntry);
×
2145
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2146
    }
2147

2148
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
59✔
2149
      numOfChangedTags++;
17✔
2150
      SSCHMEA_SET_IDX_ON(pOldColumn);
17✔
2151
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
42!
2152
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2153
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2154
      metaFetchEntryFree(&pEntry);
×
2155
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2156
    }
2157
  }
2158

2159
  if (numOfChangedTags != 1) {
17!
2160
    metaError(
×
2161
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2162
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2163
    metaFetchEntryFree(&pEntry);
×
2164
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2165
  }
2166

2167
  pEntry->version = version;
17✔
2168
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
17✔
2169

2170
  // do handle the entry
2171
  code = metaHandleEntry2(pMeta, pEntry);
17✔
2172
  if (code) {
17!
2173
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2174
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2175
    metaFetchEntryFree(&pEntry);
×
2176
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2177
  } else {
2178
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
17!
2179
             pEntry->uid, version);
2180
  }
2181

2182
  metaFetchEntryFree(&pEntry);
17✔
2183
  TAOS_RETURN(code);
17✔
2184
}
2185

2186
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
8✔
2187
  int32_t code = TSDB_CODE_SUCCESS;
8✔
2188

2189
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
8!
2190
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2191
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2192
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2193
  }
2194

2195
  SMetaEntry *pEntry = NULL;
8✔
2196
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
8✔
2197
  if (code) {
8!
2198
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2199
              __FILE__, __LINE__, pReq->stb, version);
2200
    TAOS_RETURN(code);
×
2201
  }
2202

2203
  if (TSDB_SUPER_TABLE != pEntry->type) {
8!
2204
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2205
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2206
    metaFetchEntryFree(&pEntry);
×
2207
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2208
  }
2209

2210
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
8✔
2211
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8!
2212
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2213
              __func__, __FILE__, __LINE__, pReq->stb, version);
2214
    metaFetchEntryFree(&pEntry);
×
2215
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2216
  }
2217

2218
  // search and set the tag index off
2219
  int32_t numOfChangedTags = 0;
8✔
2220
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
20!
2221
    SSchema *pCol = pTagSchema->pSchema + i;
20✔
2222
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
20✔
2223
      if (!IS_IDX_ON(pCol)) {
8!
2224
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2225
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2226
        metaFetchEntryFree(&pEntry);
×
2227
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2228
      }
2229
      numOfChangedTags++;
8✔
2230
      SSCHMEA_SET_IDX_OFF(pCol);
8✔
2231
      break;
8✔
2232
    }
2233
  }
2234

2235
  if (numOfChangedTags != 1) {
8!
2236
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2237
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2238
    metaFetchEntryFree(&pEntry);
×
2239
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2240
  }
2241

2242
  // do handle the entry
2243
  pEntry->version = version;
8✔
2244
  pTagSchema->version++;
8✔
2245
  code = metaHandleEntry2(pMeta, pEntry);
8✔
2246
  if (code) {
8!
2247
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2248
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2249
    metaFetchEntryFree(&pEntry);
×
2250
    TAOS_RETURN(code);
×
2251
  } else {
2252
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
8!
2253
             pEntry->uid, version);
2254
  }
2255

2256
  metaFetchEntryFree(&pEntry);
8✔
2257
  TAOS_RETURN(code);
8✔
2258
}
2259

2260
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
10,030✔
2261
  int32_t code = TSDB_CODE_SUCCESS;
10,030✔
2262

2263
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
10,030!
2264
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2265
              __FILE__, __LINE__, version);
2266
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2267
  }
2268

2269
  SMetaEntry *pEntry = NULL;
10,073✔
2270
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
10,073✔
2271
  if (code) {
9,994✔
2272
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
7!
2273
              __FILE__, __LINE__, pReq->name, version);
2274
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
7✔
2275
  }
2276

2277
  if (pEntry->type != TSDB_SUPER_TABLE) {
9,987!
2278
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2279
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2280
    metaFetchEntryFree(&pEntry);
×
2281
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2282
  }
2283

2284
  SMetaEntry entry = {
9,987✔
2285
      .version = version,
2286
      .type = TSDB_SUPER_TABLE,
2287
      .uid = pReq->suid,
9,987✔
2288
      .name = pReq->name,
9,987✔
2289
      .stbEntry.schemaRow = pReq->schemaRow,
2290
      .stbEntry.schemaTag = pReq->schemaTag,
2291
      .stbEntry.keep = pReq->keep,
9,987✔
2292
      .colCmpr = pReq->colCmpr,
2293
      .pExtSchemas = pReq->pExtSchemas,
9,987✔
2294
  };
2295
  TABLE_SET_COL_COMPRESSED(entry.flags);
9,987✔
2296
  if (pReq->virtualStb) {
9,987✔
2297
    TABLE_SET_VIRTUAL(entry.flags);
12✔
2298
  }
2299

2300
  // do handle the entry
2301
  code = metaHandleEntry2(pMeta, &entry);
9,987✔
2302
  if (code) {
10,016!
2303
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2304
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2305
    metaFetchEntryFree(&pEntry);
×
2306
    TAOS_RETURN(code);
×
2307
  } else {
2308
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
10,016✔
2309
             pReq->suid, version);
2310
  }
2311

2312
  metaFetchEntryFree(&pEntry);
10,088✔
2313
  TAOS_RETURN(code);
10,081✔
2314
}
2315

2316
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2317
  int32_t code = 0;
×
2318

2319
  if (taosArrayGetSize(uidArray) == 0) {
×
2320
    return TSDB_CODE_SUCCESS;
×
2321
  }
2322

2323
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2324
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2325
    SMetaInfo info;
2326
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2327
    if (code) {
×
2328
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2329
                __func__, __FILE__, __LINE__, uid, code);
2330
      return code;
×
2331
    }
2332

2333
    SMetaEntry entry = {
×
2334
        .version = version,
2335
        .uid = uid,
2336
    };
2337

2338
    if (info.suid == 0) {
×
2339
      entry.type = -TSDB_NORMAL_TABLE;
×
2340
    } else if (info.suid == uid) {
×
2341
      entry.type = -TSDB_SUPER_TABLE;
×
2342
    } else {
2343
      entry.type = -TSDB_CHILD_TABLE;
×
2344
    }
2345
    code = metaHandleEntry2(pMeta, &entry);
×
2346
    if (code) {
×
2347
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2348
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2349
      return code;
×
2350
    }
2351
  }
2352
  return code;
×
2353
}
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