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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

56.1
/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(SMetaEntry *pEntry, 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,785✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
25,785✔
31
  void     *value = NULL;
25,785✔
32
  int32_t   valueSize = 0;
25,785✔
33
  SMetaInfo info;
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
25,785!
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,787✔
43
  if (r == 0) {  // name exists, check uid and type
25,768✔
44
    int64_t uid = *(tb_uid_t *)value;
3✔
45
    tdbFree(value);
3✔
46

47
    if (pReq->suid != uid) {
3!
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) {
×
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) {
×
62
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
×
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,765!
73
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
×
74
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
75
    return TSDB_CODE_INVALID_MSG;
×
76
  }
77

78
  return TSDB_CODE_SUCCESS;
25,767✔
79
}
80

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

87
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
16,127!
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);
16,127✔
94
  if (TSDB_CODE_SUCCESS != code) {
16,127!
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;
16,127✔
105
  tdbFreeClear(value);
16,127✔
106

107
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
16,127✔
108
  if (TSDB_CODE_SUCCESS != code) {
16,127!
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;
16,127✔
116

117
  return code;
16,127✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
2,040!
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);
2,040✔
133
  if (code) {
2,040✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1!
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
1✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
2,039✔
139
    tdbFreeClear(value);
2,039✔
140

141
    if (uid != pReq->suid) {
2,039✔
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);
2,038✔
149
  if (code) {
2,038!
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) {
2,038!
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;
2,038✔
161
}
162

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

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

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

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

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

204
  code = metaHandleEntry2(pMeta, &entry);
25,761✔
205
  if (TSDB_CODE_SUCCESS == code) {
25,794!
206
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
25,794!
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,794✔
213
}
214

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

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

225
  // handle entry
226
  SMetaEntry entry = {
2,038✔
227
      .version = verison,
228
      .type = -TSDB_SUPER_TABLE,
229
      .uid = pReq->suid,
2,038✔
230
  };
231
  code = metaHandleEntry2(pMeta, &entry);
2,038✔
232
  if (code) {
2,036!
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,
2,036!
237
             pReq->suid, verison);
238
  }
239
  TAOS_RETURN(code);
2,038✔
240
}
241

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
144,452!
252
      pReq->ctb.suid == 0) {
144,456!
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) {
144,458✔
260
    pReq->uid = *(int64_t *)value;
3,721✔
261
    tdbFreeClear(value);
3,721✔
262

263
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
3,721!
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,721!
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,712!
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,712✔
287
  }
288

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
140,725✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
140,725✔
292
  if (code) {
140,703!
293
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
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) {
140,706!
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) {
140,706!
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;
140,706✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
140,706✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
140,706✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
744,139✔
319
      STagVal tagVal = {
604,197✔
320
          .cid = pTagSchema->pSchema[i].colId,
604,197✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
604,197✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
441,485!
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);
140,691✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
140,724!
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
140,713✔
339
    if (TSDB_CODE_SUCCESS != code) {
140,690!
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;
140,689✔
345
}
346

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

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

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

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

364
  return code;
140,448✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
144,176✔
372
  if (code) {
144,128✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
3,721✔
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,721✔
378
  }
379

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

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
140,407✔
395
  if (code) {
140,444!
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);
140,444✔
402
  if (TSDB_CODE_SUCCESS == code) {
140,429!
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
140,433✔
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;
140,465✔
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) {
12,082✔
419
  int32_t code = 0;
12,082✔
420
  void   *value = NULL;
12,082✔
421
  int32_t valueSize = 0;
12,082✔
422

423
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
12,082!
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) {
12,082✔
431
    // for auto create table, we return the uid of the existing table
432
    pReq->uid = *(tb_uid_t *)value;
123✔
433
    tdbFree(value);
123✔
434
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
123✔
435
  }
436

437
  // grant check
438
  code = grantCheck(TSDB_GRANT_TIMESERIES);
11,959✔
439
  if (code) {
11,959!
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;
11,959✔
444
}
445

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

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

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

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

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

473
  return code;
11,791✔
474
}
475

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

479
  // check request
480
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
11,914✔
481
  if (code) {
11,914✔
482
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
123!
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);
123✔
487
  }
488

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

505
  // build response
506
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
11,791✔
507
  if (code) {
11,791!
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);
11,791✔
514
  if (TSDB_CODE_SUCCESS == code) {
11,791!
515
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
11,791!
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);
11,791✔
522
}
523

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

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

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

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

542
  return code;
168✔
543
}
544

545
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
168✔
546
  // check request
547
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
168✔
548
  if (code) {
168!
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 = {
168✔
557
      .version = version,
558
      .type = TSDB_VIRTUAL_NORMAL_TABLE,
559
      .uid = pReq->uid,
168✔
560
      .name = pReq->name,
168✔
561
      .ntbEntry.btime = pReq->btime,
168✔
562
      .ntbEntry.ttlDays = pReq->ttl,
168✔
563
      .ntbEntry.commentLen = pReq->commentLen,
168✔
564
      .ntbEntry.comment = pReq->comment,
168✔
565
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
566
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
168✔
567
      .colRef = pReq->colRef
568
  };
569

570
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
168✔
571
  if (code) {
168!
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);
168✔
578
  if (TSDB_CODE_SUCCESS == code) {
168!
579
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
168!
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);
168✔
586
#if 0
587
  metaTimeSeriesNotifyCheck(pMeta);
588
#endif
589
}
590

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

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

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

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

610
  return code;
277✔
611
}
612

613
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
277✔
614
  // check request
615
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
277✔
616
  if (code) {
277!
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 = {
277✔
625
      .version = version,
626
      .type = TSDB_VIRTUAL_CHILD_TABLE,
627
      .uid = pReq->uid,
277✔
628
      .name = pReq->name,
277✔
629
      .ctbEntry.btime = pReq->btime,
277✔
630
      .ctbEntry.ttlDays = pReq->ttl,
277✔
631
      .ctbEntry.commentLen = pReq->commentLen,
277✔
632
      .ctbEntry.comment = pReq->comment,
277✔
633
      .ctbEntry.suid = pReq->ctb.suid,
277✔
634
      .ctbEntry.pTags = pReq->ctb.pTag,
277✔
635
      .colRef = pReq->colRef
636
  };
637

638
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
277✔
639
  if (code) {
277!
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);
277✔
646
  if (TSDB_CODE_SUCCESS == code) {
277!
647
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
277!
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);
277✔
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) {
156,520✔
664
  int32_t code = TSDB_CODE_SUCCESS;
156,520✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
156,520✔
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
144,176✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
12,344✔
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
11,914✔
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
430✔
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
168✔
671
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
262!
672
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
277✔
673
  } else {
674
    code = TSDB_CODE_INVALID_MSG;
×
675
  }
676
  TAOS_RETURN(code);
156,529✔
677
}
678

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

682
  // check request
683
  code = metaCheckDropTableReq(pMeta, version, pReq);
16,127✔
684
  if (code) {
16,127!
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) {
16,127!
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 = {
16,127✔
700
      .version = version,
701
      .uid = pReq->uid,
16,127✔
702
  };
703

704
  if (pReq->isVirtual) {
16,127✔
705
    if (pReq->suid == 0) {
54✔
706
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
47✔
707
    } else {
708
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
7✔
709
    }
710
  } else {
711
    if (pReq->suid == 0) {
16,073✔
712
      entry.type = -TSDB_NORMAL_TABLE;
2,517✔
713
    } else {
714
      entry.type = -TSDB_CHILD_TABLE;
13,556✔
715
    }
716
  }
717
  code = metaHandleEntry2(pMeta, &entry);
16,127✔
718
  if (code) {
16,127!
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,
16,127!
723
             pReq->uid, version);
724
  }
725
  TAOS_RETURN(code);
16,127✔
726
}
727

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

731
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
6,495!
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;
6,495✔
739
  int32_t valueSize = 0;
6,495✔
740
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
6,495✔
741
  if (code) {
6,495!
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;
6,495✔
748
  tdbFreeClear(value);
6,495✔
749

750
  // check table type
751
  SMetaInfo info;
752
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
6,495!
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) {
6,495!
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);
6,495✔
768
  if (code) {
6,495!
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);
6,495✔
774
}
775

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

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

785
  // fetch old entry
786
  SMetaEntry *pEntry = NULL;
5,312✔
787
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5,312✔
788
  if (code) {
5,312!
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) {
5,312!
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;
5,312✔
802
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
5,312✔
803
  SSchema        *pColumn;
804
  SExtSchema      extSchema = {0};
5,312✔
805
  pEntry->version = version;
5,312✔
806
  for (int32_t i = 0; i < pSchema->nCols; i++) {
8,542,385✔
807
    pColumn = &pSchema->pSchema[i];
8,537,572✔
808
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
8,537,572✔
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;
8,537,073✔
815
  }
816

817
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
4,813✔
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
  if (pSchema->nCols + 1 > TSDB_MAX_COLUMNS) {
4,803!
825
    metaError("vgId:%d, %s failed at %s:%d since column count %d + 1 > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
826
              __func__, __FILE__, __LINE__, pSchema->nCols, TSDB_MAX_COLUMNS, version);
827
    metaFetchEntryFree(&pEntry);
×
828
    TAOS_RETURN(TSDB_CODE_PAR_TOO_MANY_COLUMNS);
×
829
  }
830

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

890
  // do handle entry
891
  code = metaHandleEntry2(pMeta, pEntry);
4,803✔
892
  if (code) {
4,803!
893
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
894
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
895
    metaFetchEntryFree(&pEntry);
×
896
    TAOS_RETURN(code);
×
897
  } else {
898
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
4,803!
899
             pEntry->uid, version);
900
  }
901

902
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
4,803✔
903
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
82!
904
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
905
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
906
    } else {
907
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
626✔
908
        SColRef *p = &pEntry->colRef.pColRef[i];
544✔
909
        pRsp->pColRefs[i].hasRef = p->hasRef;
544✔
910
        pRsp->pColRefs[i].id = p->id;
544✔
911
        if (p->hasRef) {
544✔
912
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
268✔
913
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
268✔
914
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
268✔
915
        }
916
      }
917
    }
918
  } else {
919
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
4,721!
920
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
921
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
922
    } else {
923
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,532,285✔
924
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,527,564✔
925
        pRsp->pSchemaExt[i].colId = p->id;
8,527,564✔
926
        pRsp->pSchemaExt[i].compress = p->alg;
8,527,564✔
927
      }
928
    }
929
  }
930

931
  metaFetchEntryFree(&pEntry);
4,803✔
932
  TAOS_RETURN(code);
4,803✔
933
}
934

935
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
164✔
936
  int32_t code = TSDB_CODE_SUCCESS;
164✔
937

938
  // check request
939
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
164✔
940
  if (code) {
164!
941
    TAOS_RETURN(code);
×
942
  }
943

944
  // fetch old entry
945
  SMetaEntry *pEntry = NULL;
164✔
946
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
164✔
947
  if (code) {
164!
948
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
949
              __FILE__, __LINE__, pReq->tbName, version);
950
    TAOS_RETURN(code);
×
951
  }
952

953
  if (pEntry->version >= version) {
164!
954
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
955
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
956
    metaFetchEntryFree(&pEntry);
×
957
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
958
  }
959

960
  // search the column to drop
961
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
164✔
962
  SSchema        *pColumn = NULL;
164✔
963
  SSchema         tColumn;
964
  int32_t         iColumn = 0;
164✔
965
  for (; iColumn < pSchema->nCols; iColumn++) {
70,624!
966
    pColumn = &pSchema->pSchema[iColumn];
70,624✔
967
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
70,624✔
968
      break;
164✔
969
    }
970
  }
971

972
  if (iColumn == pSchema->nCols) {
164!
973
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
974
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
975
    metaFetchEntryFree(&pEntry);
×
976
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
977
  }
978

979
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
164!
980
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
981
              __func__, __FILE__, __LINE__, pReq->colName, version);
982
    metaFetchEntryFree(&pEntry);
×
983
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
984
  }
985

986
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
164✔
987
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
988
              __func__, __FILE__, __LINE__, pReq->colName, version);
989
    metaFetchEntryFree(&pEntry);
11✔
990
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
11✔
991
  }
992
  tColumn = *pColumn;
153✔
993

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

1031
  // update column extschema
1032
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
153✔
1033
  if (code) {
153!
1034
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1035
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1036
    metaFetchEntryFree(&pEntry);
×
1037
    TAOS_RETURN(code);
×
1038
  }
1039

1040
  // do handle entry
1041
  code = metaHandleEntry2(pMeta, pEntry);
153✔
1042
  if (code) {
153!
1043
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1044
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1045
    metaFetchEntryFree(&pEntry);
×
1046
  } else {
1047
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
153!
1048
             pEntry->uid, version);
1049
  }
1050

1051
  // build response
1052
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
153✔
1053
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
48!
1054
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1055
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1056
    } else {
1057
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
320✔
1058
        SColRef *p = &pEntry->colRef.pColRef[i];
272✔
1059
        pRsp->pColRefs[i].hasRef = p->hasRef;
272✔
1060
        pRsp->pColRefs[i].id = p->id;
272✔
1061
        if (p->hasRef) {
272✔
1062
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
151✔
1063
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
151✔
1064
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
151✔
1065
        }
1066
      }
1067
    }
1068
  } else {
1069
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
105!
1070
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1071
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1072
    } else {
1073
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
123,060✔
1074
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
122,955✔
1075
        pRsp->pSchemaExt[i].colId = p->id;
122,955✔
1076
        pRsp->pSchemaExt[i].compress = p->alg;
122,955✔
1077
      }
1078
    }
1079
  }
1080

1081
  metaFetchEntryFree(&pEntry);
153✔
1082
  TAOS_RETURN(code);
153✔
1083
}
1084

1085
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
104✔
1086
  int32_t code = TSDB_CODE_SUCCESS;
104✔
1087

1088
  // check request
1089
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
104✔
1090
  if (code) {
104!
1091
    TAOS_RETURN(code);
×
1092
  }
1093

1094
  if (NULL == pReq->colNewName) {
104!
1095
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1096
              __func__, __FILE__, __LINE__, version);
1097
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1098
  }
1099

1100
  // fetch old entry
1101
  SMetaEntry *pEntry = NULL;
104✔
1102
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
104✔
1103
  if (code) {
104!
1104
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1105
              __FILE__, __LINE__, pReq->tbName, version);
1106
    TAOS_RETURN(code);
×
1107
  }
1108

1109
  if (pEntry->version >= version) {
104!
1110
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1111
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1112
    metaFetchEntryFree(&pEntry);
×
1113
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1114
  }
1115

1116
  // search the column to update
1117
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
104✔
1118
  SSchema        *pColumn = NULL;
104✔
1119
  int32_t         iColumn = 0;
104✔
1120
  for (int32_t i = 0; i < pSchema->nCols; i++) {
444!
1121
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
444✔
1122
      pColumn = &pSchema->pSchema[i];
104✔
1123
      iColumn = i;
104✔
1124
      break;
104✔
1125
    }
1126
  }
1127

1128
  if (NULL == pColumn) {
104!
1129
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1130
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1131
    metaFetchEntryFree(&pEntry);
×
1132
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1133
  }
1134

1135
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
104✔
1136
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
1137
              __func__, __FILE__, __LINE__, pColumn->name, version);
1138
    metaFetchEntryFree(&pEntry);
11✔
1139
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
11✔
1140
  }
1141

1142
  // do update column name
1143
  pEntry->version = version;
93✔
1144
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
93✔
1145
  pSchema->version++;
93✔
1146

1147
  // do handle entry
1148
  code = metaHandleEntry2(pMeta, pEntry);
93✔
1149
  if (code) {
93!
1150
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1151
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1152
    metaFetchEntryFree(&pEntry);
×
1153
    TAOS_RETURN(code);
×
1154
  } else {
1155
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
93!
1156
             pEntry->uid, version);
1157
  }
1158

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

1189
  metaFetchEntryFree(&pEntry);
93✔
1190
  TAOS_RETURN(code);
93✔
1191
}
1192

1193
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
791✔
1194
  int32_t code = TSDB_CODE_SUCCESS;
791✔
1195

1196
  // check request
1197
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
791✔
1198
  if (code) {
791!
1199
    TAOS_RETURN(code);
×
1200
  }
1201

1202
  // fetch old entry
1203
  SMetaEntry *pEntry = NULL;
791✔
1204
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
791✔
1205
  if (code) {
791!
1206
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1207
              __FILE__, __LINE__, pReq->tbName, version);
1208
    TAOS_RETURN(code);
×
1209
  }
1210

1211
  if (pEntry->version >= version) {
791!
1212
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1213
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1214
    metaFetchEntryFree(&pEntry);
×
1215
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1216
  }
1217

1218
  // search the column to update
1219
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
791✔
1220
  SSchema        *pColumn = NULL;
791✔
1221
  int32_t         iColumn = 0;
791✔
1222
  int32_t         rowSize = 0;
791✔
1223
  for (int32_t i = 0; i < pSchema->nCols; i++) {
30,213✔
1224
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
29,422✔
1225
      pColumn = &pSchema->pSchema[i];
791✔
1226
      iColumn = i;
791✔
1227
    }
1228
    rowSize += pSchema->pSchema[i].bytes;
29,422✔
1229
  }
1230

1231
  if (NULL == pColumn) {
791!
1232
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1233
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1234
    metaFetchEntryFree(&pEntry);
×
1235
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1236
  }
1237

1238
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
791!
1239
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
307!
1240
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1241
              version);
1242
    metaFetchEntryFree(&pEntry);
307✔
1243
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
307✔
1244
  }
1245

1246
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
484✔
1247
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
13!
1248
              __func__, __FILE__, __LINE__, pReq->colName, version);
1249
    metaFetchEntryFree(&pEntry);
13✔
1250
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
13✔
1251
  }
1252

1253
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
471✔
1254
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
70!
1255
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW,
1256
              version);
1257
    metaFetchEntryFree(&pEntry);
70✔
1258
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
70✔
1259
  }
1260

1261
  // do change the column bytes
1262
  pEntry->version = version;
401✔
1263
  pSchema->version++;
401✔
1264
  pColumn->bytes = pReq->colModBytes;
401✔
1265

1266
  // do handle entry
1267
  code = metaHandleEntry2(pMeta, pEntry);
401✔
1268
  if (code) {
401!
1269
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1270
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1271
    metaFetchEntryFree(&pEntry);
×
1272
    TAOS_RETURN(code);
×
1273
  } else {
1274
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
401!
1275
             pEntry->uid, version);
1276
  }
1277

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

1308
  metaFetchEntryFree(&pEntry);
401✔
1309
  TAOS_RETURN(code);
401✔
1310
}
1311

1312
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
13,152✔
1313
  int32_t code = 0;
13,152✔
1314

1315
  // check tag name
1316
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
13,152!
1317
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1318
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1319
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1320
  }
1321

1322
  // check name
1323
  void   *value = NULL;
13,152✔
1324
  int32_t valueSize = 0;
13,152✔
1325
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
13,152✔
1326
  if (code) {
13,152✔
1327
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1!
1328
              __FILE__, __LINE__, pReq->tbName, version);
1329
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1✔
1330
    TAOS_RETURN(code);
1✔
1331
  }
1332
  tdbFreeClear(value);
13,151✔
1333

1334
  TAOS_RETURN(code);
13,151✔
1335
}
1336

1337
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
13,152✔
1338
  int32_t code = TSDB_CODE_SUCCESS;
13,152✔
1339

1340
  // check request
1341
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
13,152✔
1342
  if (code) {
13,152✔
1343
    TAOS_RETURN(code);
1✔
1344
  }
1345

1346
  // fetch child entry
1347
  SMetaEntry *pChild = NULL;
13,151✔
1348
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
13,151✔
1349
  if (code) {
13,151!
1350
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1351
              __FILE__, __LINE__, pReq->tbName, version);
1352
    TAOS_RETURN(code);
×
1353
  }
1354

1355
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
13,151!
1356
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1357
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1358
    metaFetchEntryFree(&pChild);
×
1359
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1360
  }
1361

1362
  // fetch super entry
1363
  SMetaEntry *pSuper = NULL;
13,151✔
1364
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
13,151✔
1365
  if (code) {
13,151!
1366
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1367
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1368
    metaFetchEntryFree(&pChild);
×
1369
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1370
  }
1371

1372
  // search the tag to update
1373
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
13,151✔
1374
  SSchema        *pColumn = NULL;
13,151✔
1375
  int32_t         iColumn = 0;
13,151✔
1376
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,321!
1377
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
15,321✔
1378
      pColumn = &pTagSchema->pSchema[i];
13,151✔
1379
      iColumn = i;
13,151✔
1380
      break;
13,151✔
1381
    }
1382
  }
1383

1384
  if (NULL == pColumn) {
13,151!
1385
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1386
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1387
    metaFetchEntryFree(&pChild);
×
1388
    metaFetchEntryFree(&pSuper);
×
1389
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1390
  }
1391

1392
  // do change tag value
1393
  pChild->version = version;
13,151✔
1394
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
13,151✔
1395
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
205!
1396
    if (NULL == pNewTag) {
205!
1397
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1398
                __LINE__, tstrerror(terrno), version);
1399
      metaFetchEntryFree(&pChild);
×
1400
      metaFetchEntryFree(&pSuper);
×
1401
      TAOS_RETURN(terrno);
×
1402
    }
1403
    pChild->ctbEntry.pTags = pNewTag;
205✔
1404
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
205✔
1405
  } else {
1406
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
12,946✔
1407

1408
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
12,946✔
1409
    if (NULL == pTagArray) {
12,946!
1410
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1411
                __LINE__, tstrerror(terrno), version);
1412
      metaFetchEntryFree(&pChild);
×
1413
      metaFetchEntryFree(&pSuper);
×
1414
      TAOS_RETURN(terrno);
×
1415
    }
1416

1417
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
29,883✔
1418
      STagVal value = {
16,937✔
1419
          .type = pTagSchema->pSchema[i].type,
16,937✔
1420
          .cid = pTagSchema->pSchema[i].colId,
16,937✔
1421
      };
1422

1423
      if (iColumn == i) {
16,937✔
1424
        if (pReq->isNull) {
12,946✔
1425
          continue;
936✔
1426
        }
1427
        if (IS_VAR_DATA_TYPE(value.type)) {
12,690!
1428
          value.pData = pReq->pTagVal;
1,165✔
1429
          value.nData = pReq->nTagVal;
1,165✔
1430
        } else {
1431
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
11,525✔
1432
        }
1433
      } else if (!tTagGet(pOldTag, &value)) {
3,991✔
1434
        continue;
680✔
1435
      }
1436

1437
      if (NULL == taosArrayPush(pTagArray, &value)) {
16,001!
1438
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1439
                  __LINE__, tstrerror(terrno), version);
1440
        taosArrayDestroy(pTagArray);
×
1441
        metaFetchEntryFree(&pChild);
×
1442
        metaFetchEntryFree(&pSuper);
×
1443
        TAOS_RETURN(terrno);
×
1444
      }
1445
    }
1446

1447
    STag *pNewTag = NULL;
12,946✔
1448
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
12,946✔
1449
    if (code) {
12,946!
1450
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1451
                __LINE__, tstrerror(code), version);
1452
      taosArrayDestroy(pTagArray);
×
1453
      metaFetchEntryFree(&pChild);
×
1454
      metaFetchEntryFree(&pSuper);
×
1455
      TAOS_RETURN(code);
×
1456
    }
1457
    taosArrayDestroy(pTagArray);
12,946✔
1458
    taosMemoryFree(pChild->ctbEntry.pTags);
12,946!
1459
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
12,946✔
1460
  }
1461

1462
  // do handle entry
1463
  code = metaHandleEntry2(pMeta, pChild);
13,151✔
1464
  if (code) {
13,151!
1465
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1466
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1467
    metaFetchEntryFree(&pChild);
×
1468
    metaFetchEntryFree(&pSuper);
×
1469
    TAOS_RETURN(code);
×
1470
  } else {
1471
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
13,151!
1472
             pChild->uid, version);
1473
  }
1474

1475
  // free resource and return
1476
  metaFetchEntryFree(&pChild);
13,151✔
1477
  metaFetchEntryFree(&pSuper);
13,151✔
1478
  TAOS_RETURN(code);
13,151✔
1479
}
1480

1481
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1482
  int32_t code = 0;
5✔
1483

1484
  // check tag name
1485
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
1486
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1487
              __FILE__, __LINE__, version);
1488
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1489
  }
1490

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

1503
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
1504
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1505
              __FILE__, __LINE__, version);
1506
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1507
  }
1508

1509
  TAOS_RETURN(code);
5✔
1510
}
1511

1512
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1513
  int32_t code = TSDB_CODE_SUCCESS;
5✔
1514

1515
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
5✔
1516
  if (code) {
5!
1517
    TAOS_RETURN(code);
×
1518
  }
1519

1520
  // fetch child entry
1521
  SMetaEntry *pChild = NULL;
5✔
1522
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
5✔
1523
  if (code) {
5!
1524
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1525
              __FILE__, __LINE__, pReq->tbName, version);
1526
    TAOS_RETURN(code);
×
1527
  }
1528

1529
  if (pChild->type != TSDB_CHILD_TABLE) {
5!
1530
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1531
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1532
    metaFetchEntryFree(&pChild);
×
1533
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1534
  }
1535

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

1546
  // search the tags to update
1547
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
5✔
1548

1549
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5!
1550
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1551
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1552
    metaFetchEntryFree(&pChild);
×
1553
    metaFetchEntryFree(&pSuper);
×
1554
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1555
  }
1556

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

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

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

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

1608
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1609
    SSchema *pCol = &pTagSchema->pSchema[i];
35✔
1610
    STagVal  value = {
35✔
1611
         .cid = pCol->colId,
35✔
1612
    };
1613

1614
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
35✔
1615
    if (pTagVal == NULL) {
35✔
1616
      if (!tTagGet(pOldTag, &value)) {
5!
1617
        continue;
6✔
1618
      }
1619
    } else {
1620
      value.type = pCol->type;
30✔
1621
      if (pTagVal->isNull) {
30✔
1622
        continue;
6✔
1623
      }
1624

1625
      if (IS_VAR_DATA_TYPE(pCol->type)) {
24!
1626
        value.pData = pTagVal->pTagVal;
4✔
1627
        value.nData = pTagVal->nTagVal;
4✔
1628
      } else {
1629
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
20✔
1630
      }
1631
    }
1632

1633
    if (taosArrayPush(pTagArray, &value) == NULL) {
29!
1634
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1635
                __LINE__, tstrerror(terrno), version);
1636
      taosHashCleanup(pTagTable);
×
1637
      taosArrayDestroy(pTagArray);
×
1638
      metaFetchEntryFree(&pChild);
×
1639
      metaFetchEntryFree(&pSuper);
×
1640
      TAOS_RETURN(terrno);
×
1641
    }
1642
  }
1643

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

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

1673
  taosHashCleanup(pTagTable);
5✔
1674
  metaFetchEntryFree(&pChild);
5✔
1675
  metaFetchEntryFree(&pSuper);
5✔
1676
  TAOS_RETURN(code);
5✔
1677
}
1678

1679
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
104✔
1680
  int32_t code = TSDB_CODE_SUCCESS;
104✔
1681

1682
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
104!
1683
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1684
              __FILE__, __LINE__, version);
1685
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1686
  }
1687

1688
  return code;
104✔
1689
}
1690

1691
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
104✔
1692
  int32_t code = 0;
104✔
1693

1694
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
104✔
1695
  if (code) {
104!
1696
    TAOS_RETURN(code);
×
1697
  }
1698

1699
  // fetch entry
1700
  SMetaEntry *pEntry = NULL;
104✔
1701
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
104✔
1702
  if (code) {
104!
1703
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1704
              __FILE__, __LINE__, pReq->tbName, version);
1705
    TAOS_RETURN(code);
×
1706
  }
1707

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

1759
  // do handle entry
1760
  code = metaHandleEntry2(pMeta, pEntry);
104✔
1761
  if (code) {
104!
1762
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1763
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1764
    metaFetchEntryFree(&pEntry);
×
1765
    TAOS_RETURN(code);
×
1766
  } else {
1767
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
104!
1768
             pEntry->uid, version);
1769
  }
1770

1771
  metaFetchEntryFree(&pEntry);
104✔
1772
  TAOS_RETURN(code);
104✔
1773
}
1774

1775
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
11✔
1776
  int32_t code = TSDB_CODE_SUCCESS;
11✔
1777

1778
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
11!
1779
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1780
              __FILE__, __LINE__, version);
1781
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1782
  }
1783

1784
  SMetaEntry *pEntry = NULL;
11✔
1785
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11✔
1786
  if (code) {
11!
1787
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1788
              __FILE__, __LINE__, pReq->tbName, version);
1789
    TAOS_RETURN(code);
×
1790
  }
1791

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

1799
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
11!
1800
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1801
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1802
    metaFetchEntryFree(&pEntry);
×
1803
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1804
  }
1805

1806
  // do change the entry
1807
  int8_t           updated = 0;
11✔
1808
  SColCmprWrapper *wp = &pEntry->colCmpr;
11✔
1809
  for (int32_t i = 0; i < wp->nCols; i++) {
88✔
1810
    SColCmpr *p = &wp->pColCmpr[i];
77✔
1811
    if (p->id == pReq->colId) {
77✔
1812
      uint32_t dst = 0;
11✔
1813
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
11✔
1814
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1815
      if (updated > 0) {
11!
1816
        p->alg = dst;
11✔
1817
      }
1818
    }
1819
  }
1820

1821
  if (updated == 0) {
11!
1822
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1823
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1824
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1825
    metaFetchEntryFree(&pEntry);
×
1826
    TAOS_RETURN(code);
×
1827
  } else if (updated < 0) {
11!
1828
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1829
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1830
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1831
    metaFetchEntryFree(&pEntry);
×
1832
    TAOS_RETURN(code);
×
1833
  }
1834

1835
  pEntry->version = version;
11✔
1836

1837
  // do handle entry
1838
  code = metaHandleEntry2(pMeta, pEntry);
11✔
1839
  if (code) {
11!
1840
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1841
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1842
    metaFetchEntryFree(&pEntry);
×
1843
    TAOS_RETURN(code);
×
1844
  } else {
1845
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
11!
1846
             pEntry->uid, version);
1847
  }
1848

1849
  metaFetchEntryFree(&pEntry);
11✔
1850
  TAOS_RETURN(code);
11✔
1851
}
1852

1853
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
80✔
1854
  int32_t code = TSDB_CODE_SUCCESS;
80✔
1855

1856
  // check request
1857
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
80✔
1858
  if (code) {
80!
1859
    TAOS_RETURN(code);
×
1860
  }
1861

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

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

1874
  if (NULL == pReq->refColName) {
80!
1875
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1876
              __func__, __FILE__, __LINE__, version);
1877
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1878
  }
1879

1880

1881
  // fetch old entry
1882
  SMetaEntry *pEntry = NULL;
80✔
1883
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
80✔
1884
  if (code) {
80!
1885
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1886
              __FILE__, __LINE__, pReq->tbName, version);
1887
    TAOS_RETURN(code);
×
1888
  }
1889

1890
  if (pEntry->version >= version) {
80!
1891
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1892
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1893
    metaFetchEntryFree(&pEntry);
×
1894
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1895
  }
1896

1897
  // fetch super entry
1898
  SMetaEntry *pSuper = NULL;
80✔
1899
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
80✔
1900
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
7✔
1901
    if (code) {
7!
1902
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1903
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1904
      metaFetchEntryFree(&pEntry);
×
1905
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1906
    }
1907
  }
1908

1909
  // search the column to update
1910
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
80✔
1911
  SColRef        *pColRef = NULL;
80✔
1912
  int32_t         iColumn = 0;
80✔
1913
  for (int32_t i = 0; i < pSchema->nCols; i++) {
472!
1914
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
472✔
1915
      pColRef = &pEntry->colRef.pColRef[i];
80✔
1916
      iColumn = i;
80✔
1917
      break;
80✔
1918
    }
1919
  }
1920

1921
  if (NULL == pColRef) {
80!
1922
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1923
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1924
    metaFetchEntryFree(&pEntry);
×
1925
    metaFetchEntryFree(&pSuper);
×
1926
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1927
  }
1928

1929
  // do update column name
1930
  pEntry->version = version;
80✔
1931
  pColRef->hasRef = true;
80✔
1932
  pColRef->id = pSchema->pSchema[iColumn].colId;
80✔
1933
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
80✔
1934
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
80✔
1935
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
80✔
1936
  pSchema->version++;
80✔
1937
  pEntry->colRef.version++;
80✔
1938

1939
  // do handle entry
1940
  code = metaHandleEntry2(pMeta, pEntry);
80✔
1941
  if (code) {
80!
1942
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1943
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1944
    metaFetchEntryFree(&pEntry);
×
1945
    metaFetchEntryFree(&pSuper);
×
1946
    TAOS_RETURN(code);
×
1947
  } else {
1948
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
80!
1949
             pEntry->uid, version);
1950
  }
1951

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

1969
  metaFetchEntryFree(&pEntry);
80✔
1970
  metaFetchEntryFree(&pSuper);
80✔
1971
  TAOS_RETURN(code);
80✔
1972
}
1973

1974
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
44✔
1975
  int32_t code = TSDB_CODE_SUCCESS;
44✔
1976

1977
  // check request
1978
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
44✔
1979
  if (code) {
44!
1980
    TAOS_RETURN(code);
×
1981
  }
1982

1983
  // fetch old entry
1984
  SMetaEntry *pEntry = NULL;
44✔
1985
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
44✔
1986
  if (code) {
44!
1987
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1988
              __FILE__, __LINE__, pReq->tbName, version);
1989
    TAOS_RETURN(code);
×
1990
  }
1991

1992
  if (pEntry->version >= version) {
44!
1993
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1994
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1995
    metaFetchEntryFree(&pEntry);
×
1996
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1997
  }
1998

1999
  // fetch super entry
2000
  SMetaEntry *pSuper = NULL;
44✔
2001
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
44✔
2002
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
2✔
2003
    if (code) {
2!
2004
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2005
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2006
      metaFetchEntryFree(&pEntry);
×
2007
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2008
    }
2009
  }
2010

2011
  // search the column to update
2012
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
44✔
2013
  SColRef        *pColRef = NULL;
44✔
2014
  int32_t         iColumn = 0;
44✔
2015
  for (int32_t i = 0; i < pSchema->nCols; i++) {
223!
2016
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
223✔
2017
      pColRef = &pEntry->colRef.pColRef[i];
44✔
2018
      iColumn = i;
44✔
2019
      break;
44✔
2020
    }
2021
  }
2022

2023
  if (NULL == pColRef) {
44!
2024
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2025
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2026
    metaFetchEntryFree(&pEntry);
×
2027
    metaFetchEntryFree(&pSuper);
×
2028
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2029
  }
2030

2031
  // do update column name
2032
  pEntry->version = version;
44✔
2033
  pColRef->hasRef = false;
44✔
2034
  pColRef->id = pSchema->pSchema[iColumn].colId;
44✔
2035
  pSchema->version++;
44✔
2036
  pEntry->colRef.version++;
44✔
2037

2038
  // do handle entry
2039
  code = metaHandleEntry2(pMeta, pEntry);
44✔
2040
  if (code) {
44!
2041
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2042
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2043
    metaFetchEntryFree(&pEntry);
×
2044
    metaFetchEntryFree(&pSuper);
×
2045
    TAOS_RETURN(code);
×
2046
  } else {
2047
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
44!
2048
             pEntry->uid, version);
2049
  }
2050

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

2068
  metaFetchEntryFree(&pEntry);
44✔
2069
  metaFetchEntryFree(&pSuper);
44✔
2070
  TAOS_RETURN(code);
44✔
2071
}
2072

2073
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
19✔
2074
  int32_t code = TSDB_CODE_SUCCESS;
19✔
2075

2076
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
19!
2077
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2078
              __FILE__, __LINE__, version);
2079
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2080
  }
2081

2082
  SMetaEntry *pEntry = NULL;
19✔
2083
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
19✔
2084
  if (code) {
19!
2085
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2086
              __FILE__, __LINE__, pReq->name, version);
2087
    TAOS_RETURN(code);
×
2088
  }
2089

2090
  if (pEntry->type != TSDB_SUPER_TABLE) {
19!
2091
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2092
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2093
    metaFetchEntryFree(&pEntry);
×
2094
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2095
  }
2096

2097
  if (pEntry->uid != pReq->suid) {
19!
2098
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2099
              ", version:%" PRId64,
2100
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2101
    metaFetchEntryFree(&pEntry);
×
2102
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2103
  }
2104

2105
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2106
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2107
  //   PRId64,
2108
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2109
  //             pReq->schemaTag.version, version);
2110
  //   metaFetchEntryFree(&pEntry);
2111
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2112
  // }
2113

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

2124
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
19!
2125
    metaError(
×
2126
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2127
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2128
        version);
2129
    metaFetchEntryFree(&pEntry);
×
2130
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2131
  }
2132

2133
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2134
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2135
  //   PRId64,
2136
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2137
  //             pNewTagSchema->version, version);
2138
  //   metaFetchEntryFree(&pEntry);
2139
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2140
  // }
2141

2142
  int32_t numOfChangedTags = 0;
19✔
2143
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
88✔
2144
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
69✔
2145
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
69✔
2146

2147
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
69!
2148
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
69!
2149
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2150
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2151
      metaFetchEntryFree(&pEntry);
×
2152
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2153
    }
2154

2155
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
69✔
2156
      numOfChangedTags++;
19✔
2157
      SSCHMEA_SET_IDX_ON(pOldColumn);
19✔
2158
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
50!
2159
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2160
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2161
      metaFetchEntryFree(&pEntry);
×
2162
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2163
    }
2164
  }
2165

2166
  if (numOfChangedTags != 1) {
19!
2167
    metaError(
×
2168
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2169
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2170
    metaFetchEntryFree(&pEntry);
×
2171
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2172
  }
2173

2174
  pEntry->version = version;
19✔
2175
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
19✔
2176

2177
  // do handle the entry
2178
  code = metaHandleEntry2(pMeta, pEntry);
19✔
2179
  if (code) {
19!
2180
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2181
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2182
    metaFetchEntryFree(&pEntry);
×
2183
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2184
  } else {
2185
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
19!
2186
             pEntry->uid, version);
2187
  }
2188

2189
  metaFetchEntryFree(&pEntry);
19✔
2190
  TAOS_RETURN(code);
19✔
2191
}
2192

2193
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
10✔
2194
  int32_t code = TSDB_CODE_SUCCESS;
10✔
2195

2196
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
10!
2197
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2198
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2199
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2200
  }
2201

2202
  SMetaEntry *pEntry = NULL;
10✔
2203
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
10✔
2204
  if (code) {
10!
2205
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2206
              __FILE__, __LINE__, pReq->stb, version);
2207
    TAOS_RETURN(code);
×
2208
  }
2209

2210
  if (TSDB_SUPER_TABLE != pEntry->type) {
10!
2211
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2212
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2213
    metaFetchEntryFree(&pEntry);
×
2214
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2215
  }
2216

2217
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
10✔
2218
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
10!
2219
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2220
              __func__, __FILE__, __LINE__, pReq->stb, version);
2221
    metaFetchEntryFree(&pEntry);
×
2222
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2223
  }
2224

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

2242
  if (numOfChangedTags != 1) {
10!
2243
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2244
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2245
    metaFetchEntryFree(&pEntry);
×
2246
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2247
  }
2248

2249
  // do handle the entry
2250
  pEntry->version = version;
10✔
2251
  pTagSchema->version++;
10✔
2252
  code = metaHandleEntry2(pMeta, pEntry);
10✔
2253
  if (code) {
10!
2254
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2255
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2256
    metaFetchEntryFree(&pEntry);
×
2257
    TAOS_RETURN(code);
×
2258
  } else {
2259
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
10!
2260
             pEntry->uid, version);
2261
  }
2262

2263
  metaFetchEntryFree(&pEntry);
10✔
2264
  TAOS_RETURN(code);
10✔
2265
}
2266

2267
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
16,219✔
2268
  int32_t code = TSDB_CODE_SUCCESS;
16,219✔
2269

2270
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
16,219!
2271
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2272
              __FILE__, __LINE__, version);
2273
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2274
  }
2275

2276
  SMetaEntry *pEntry = NULL;
16,219✔
2277
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
16,219✔
2278
  if (code) {
16,196!
2279
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2280
              __FILE__, __LINE__, pReq->name, version);
2281
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2282
  }
2283

2284
  if (pEntry->type != TSDB_SUPER_TABLE) {
16,196!
2285
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2286
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2287
    metaFetchEntryFree(&pEntry);
×
2288
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2289
  }
2290

2291
  SMetaEntry entry = {
16,196✔
2292
      .version = version,
2293
      .type = TSDB_SUPER_TABLE,
2294
      .uid = pReq->suid,
16,196✔
2295
      .name = pReq->name,
16,196✔
2296
      .stbEntry.schemaRow = pReq->schemaRow,
2297
      .stbEntry.schemaTag = pReq->schemaTag,
2298
      .stbEntry.keep = pReq->keep,
16,196✔
2299
      .colCmpr = pReq->colCmpr,
2300
      .pExtSchemas = pReq->pExtSchemas,
16,196✔
2301
  };
2302
  TABLE_SET_COL_COMPRESSED(entry.flags);
16,196✔
2303
  if (pReq->virtualStb) {
16,196✔
2304
    TABLE_SET_VIRTUAL(entry.flags);
29✔
2305
  }
2306

2307
  // do handle the entry
2308
  code = metaHandleEntry2(pMeta, &entry);
16,196✔
2309
  if (code) {
16,143!
2310
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2311
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2312
    metaFetchEntryFree(&pEntry);
×
2313
    TAOS_RETURN(code);
×
2314
  } else {
2315
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
16,143!
2316
             pReq->suid, version);
2317
  }
2318

2319
  metaFetchEntryFree(&pEntry);
16,219✔
2320
  TAOS_RETURN(code);
16,220✔
2321
}
2322

2323
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2324
  int32_t code = 0;
×
2325

2326
  if (taosArrayGetSize(uidArray) == 0) {
×
2327
    return TSDB_CODE_SUCCESS;
×
2328
  }
2329

2330
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2331
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2332
    SMetaInfo info;
2333
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2334
    if (code) {
×
2335
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2336
                __func__, __FILE__, __LINE__, uid, code);
2337
      return code;
×
2338
    }
2339

2340
    SMetaEntry entry = {
×
2341
        .version = version,
2342
        .uid = uid,
2343
    };
2344

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

© 2025 Coveralls, Inc