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

taosdata / TDengine / #4902

27 Dec 2025 02:36PM UTC coverage: 65.642% (-0.09%) from 65.734%
#4902

push

travis-ci

web-flow
fix: ci errors (#34079)

192706 of 293572 relevant lines covered (65.64%)

117199453.55 hits per line

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

67.83
/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) {
4,679,372✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,679,372✔
31
  void     *value = NULL;
4,684,943✔
32
  int32_t   valueSize = 0;
4,685,688✔
33
  SMetaInfo info;
4,685,166✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,685,839✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
21,111✔
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);
4,657,854✔
43
  if (r == 0) {  // name exists, check uid and type
4,666,588✔
44
    int64_t uid = *(tb_uid_t *)value;
2,193✔
45
    tdbFree(value);
2,193✔
46

47
    if (pReq->suid != uid) {
2,193✔
48
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
2,193✔
49
                " version:%" PRId64,
50
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
51
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
2,193✔
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) {
4,664,395✔
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;
4,671,931✔
79
}
80

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

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

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

117
  return code;
1,363,415✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
874,304✔
127
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
355✔
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);
873,949✔
133
  if (code) {
870,582✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
731✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
731✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
869,851✔
139
    tdbFreeClear(value);
871,827✔
140

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

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
866,176✔
149
  if (code) {
869,013✔
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) {
869,013✔
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;
869,013✔
161
}
162

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
4,664,217✔
169
  if (code != TSDB_CODE_SUCCESS) {
4,672,103✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
2,193✔
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);
2,193✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
9,331,698✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,670,904✔
184
      .name = pReq->name,
4,665,391✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,666,872✔
188
  };
189
  if (pReq->rollup) {
4,653,744✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
4,659,251✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,650,655✔
195
    entry.colCmpr = pReq->colCmpr;
4,650,655✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
4,638,753✔
199

200
  if (pReq->virtualStb) {
4,653,472✔
201
    TABLE_SET_VIRTUAL(entry.flags);
44,226✔
202
  }
203

204
  code = metaHandleEntry2(pMeta, &entry);
4,648,103✔
205
  if (TSDB_CODE_SUCCESS == code) {
4,685,417✔
206
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,684,929✔
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,
488✔
210
              pReq->suid, tstrerror(code));
211
  }
212
  TAOS_RETURN(code);
4,685,417✔
213
}
214

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
873,949✔
221
  if (code) {
869,863✔
222
    TAOS_RETURN(code);
1,462✔
223
  }
224

225
  // handle entry
226
  SMetaEntry entry = {
868,401✔
227
      .version = verison,
228
      .type = -TSDB_SUPER_TABLE,
229
      .uid = pReq->suid,
870,252✔
230
  };
231
  code = metaHandleEntry2(pMeta, &entry);
867,031✔
232
  if (code) {
872,842✔
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,
872,842✔
237
             pReq->suid, verison);
238
  }
239
  TAOS_RETURN(code);
872,842✔
240
}
241

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
51,494,943✔
252
      pReq->ctb.suid == 0) {
51,491,431✔
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) {
51,478,956✔
260
    pReq->uid = *(int64_t *)value;
332,160✔
261
    tdbFreeClear(value);
332,160✔
262

263
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
332,160✔
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) {
332,160✔
272
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
1,751✔
273
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
274
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
1,751✔
275
    }
276

277
    // check suid
278
    if (info.suid != pReq->ctb.suid) {
330,409✔
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;
330,409✔
287
  }
288

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
51,165,687✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
51,163,666✔
292
  if (code) {
51,153,442✔
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) {
51,153,442✔
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) {
51,149,377✔
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_PAR_TABLE_NOT_EXIST;
×
312
  }
313

314
  // Check tag value
315
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
51,153,058✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
51,163,060✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
51,161,109✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
233,906,561✔
319
      STagVal tagVal = {
183,017,601✔
320
          .cid = pTagSchema->pSchema[i].colId,
183,005,417✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
183,013,399✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
141,731,525✔
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);
51,151,583✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
51,137,420✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
51,157,629✔
339
    if (TSDB_CODE_SUCCESS != code) {
51,130,689✔
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;
51,127,757✔
345
}
346

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

350
  if (NULL == ppRsp) {
50,981,857✔
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
50,981,857✔
355
  if (NULL == ppRsp) {
50,973,329✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
50,973,329✔
360
  (*ppRsp)->tuid = pEntry->uid;
50,978,363✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
50,988,875✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
50,991,682✔
363

364
  return code;
51,001,343✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
51,342,532✔
372
  if (code) {
51,316,682✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
332,160✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,751✔
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
332,160✔
378
  }
379

380
  SMetaEntry entry = {
50,984,522✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
50,984,267✔
384
      .name = pReq->name,
50,983,557✔
385
      .ctbEntry.btime = pReq->btime,
50,966,101✔
386
      .ctbEntry.ttlDays = pReq->ttl,
50,967,776✔
387
      .ctbEntry.commentLen = pReq->commentLen,
50,978,450✔
388
      .ctbEntry.comment = pReq->comment,
50,975,337✔
389
      .ctbEntry.suid = pReq->ctb.suid,
50,968,332✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
50,967,898✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
50,964,115✔
395
  if (code) {
51,000,948✔
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);
51,000,948✔
402
  if (TSDB_CODE_SUCCESS == code) {
51,006,776✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
51,008,342✔
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;
51,017,010✔
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) {
19,408,590✔
419
  int32_t code = 0;
19,408,590✔
420
  void   *value = NULL;
19,408,590✔
421
  int32_t valueSize = 0;
19,408,590✔
422

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

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

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

449
  if (NULL == ppRsp) {
19,275,174✔
450
    return code;
×
451
  }
452

453
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
19,275,174✔
454
  if (NULL == *ppRsp) {
19,275,174✔
455
    return terrno;
×
456
  }
457

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
104,785,159✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
85,509,985✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
85,509,985✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
85,509,985✔
468
    if (pEntry->pExtSchemas) {
85,509,985✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
438,386✔
470
    }
471
  }
472

473
  return code;
19,275,174✔
474
}
475

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

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

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

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

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

527
  if (NULL == ppRsp) {
99,839✔
528
    return code;
×
529
  }
530

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

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

543
  return code;
99,365✔
544
}
545

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

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

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

576
  // handle entry
577
  code = metaHandleEntry2(pMeta, &entry);
99,365✔
578
  if (TSDB_CODE_SUCCESS == code) {
99,365✔
579
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
99,365✔
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);
99,365✔
586
#if 0
587
  metaTimeSeriesNotifyCheck(pMeta);
588
#endif
589
}
590

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

594
  if (NULL == ppRsp) {
154,194✔
595
    return code;
×
596
  }
597

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

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

610
  return code;
153,720✔
611
}
612

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

636
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
154,194✔
637
  if (code) {
154,194✔
638
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
474✔
639
              tstrerror(code));
640
    TAOS_RETURN(code);
474✔
641
  }
642

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

658
// Drop Normal Table
659

660
// Alter Normal Table
661

662
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
70,905,873✔
663
  int32_t code = TSDB_CODE_SUCCESS;
70,905,873✔
664
  if (TSDB_CHILD_TABLE == pReq->type) {
70,905,873✔
665
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
51,345,570✔
666
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
19,562,784✔
667
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
19,308,751✔
668
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
254,033✔
669
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
99,839✔
670
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
154,194✔
671
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
154,194✔
672
  } else {
673
    code = TSDB_CODE_INVALID_MSG;
×
674
  }
675
  TAOS_RETURN(code);
70,912,251✔
676
}
677

678
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,363,415✔
679
  int32_t code = TSDB_CODE_SUCCESS;
1,363,415✔
680

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

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

698
  SMetaEntry entry = {
1,363,415✔
699
      .version = version,
700
      .uid = pReq->uid,
1,363,415✔
701
  };
702

703
  if (pReq->isVirtual) {
1,363,415✔
704
    if (pReq->suid == 0) {
52,906✔
705
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,667✔
706
    } else {
707
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,239✔
708
    }
709
  } else {
710
    if (pReq->suid == 0) {
1,310,509✔
711
      entry.type = -TSDB_NORMAL_TABLE;
755,321✔
712
    } else {
713
      entry.type = -TSDB_CHILD_TABLE;
555,188✔
714
    }
715
  }
716
  code = metaHandleEntry2(pMeta, &entry);
1,363,415✔
717
  if (code) {
1,363,415✔
718
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
719
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
720
  } else {
721
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,363,415✔
722
             pReq->uid, version);
723
  }
724
  TAOS_RETURN(code);
1,363,415✔
725
}
726

727
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,938,348✔
728
  int32_t code = 0;
3,938,348✔
729

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

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

749
  // check table type
750
  SMetaInfo info;
3,938,348✔
751
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
3,938,348✔
752
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
753
              " not found, this is an internal error in meta, version:%" PRId64,
754
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
755
    code = TSDB_CODE_INTERNAL_ERROR;
×
756
    TAOS_RETURN(code);
×
757
  }
758
  if (info.suid != 0 && pReq->action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF &&
3,938,348✔
759
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
26,780✔
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);
3,938,348✔
768
  if (code) {
3,938,348✔
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);
3,938,348✔
774
}
775

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

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

785
  // fetch old entry
786
  SMetaEntry *pEntry = NULL;
3,223,308✔
787
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,223,308✔
788
  if (code) {
3,223,308✔
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) {
3,223,308✔
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;
3,223,308✔
802
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,223,308✔
803
  SSchema        *pColumn;
804
  SExtSchema      extSchema = {0};
3,223,308✔
805
  pEntry->version = version;
3,223,308✔
806
  for (int32_t i = 0; i < pSchema->nCols; i++) {
2,147,483,647✔
807
    pColumn = &pSchema->pSchema[i];
2,147,483,647✔
808
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
2,147,483,647✔
809
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
305,786✔
810
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
811
      metaFetchEntryFree(&pEntry);
305,786✔
812
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
305,786✔
813
    }
814
    rowSize += pColumn->bytes;
2,147,483,647✔
815
  }
816

817
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
2,917,522✔
818
  if (rowSize + pReq->bytes > maxBytesPerRow) {
2,917,522✔
819
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,190✔
820
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
821
    metaFetchEntryFree(&pEntry);
6,190✔
822
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,190✔
823
  }
824

825
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
2,911,332✔
826
  if (pSchema->nCols + 1 > maxCols) {
2,911,332✔
827
    metaError("vgId:%d, %s failed at %s:%d since column count %d + 1 > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
828
              __func__, __FILE__, __LINE__, pSchema->nCols, maxCols, version);
829
    metaFetchEntryFree(&pEntry);
×
830
    TAOS_RETURN(TSDB_CODE_PAR_TOO_MANY_COLUMNS);
×
831
  }
832

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

899
  // do handle entry
900
  code = metaHandleEntry2(pMeta, pEntry);
2,910,858✔
901
  if (code) {
2,910,858✔
902
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
903
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
904
    metaFetchEntryFree(&pEntry);
×
905
    TAOS_RETURN(code);
×
906
  } else {
907
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
2,910,858✔
908
             pEntry->uid, version);
909
  }
910

911
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
2,910,858✔
912
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
49,973✔
913
    if (code) {
49,973✔
914
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
915
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
916
    } else {
917
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
15,904,899✔
918
        SColRef *p = &pEntry->colRef.pColRef[i];
15,854,926✔
919
        pRsp->pColRefs[i].hasRef = p->hasRef;
15,854,926✔
920
        pRsp->pColRefs[i].id = p->id;
15,854,926✔
921
        if (p->hasRef) {
15,854,926✔
922
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
161,368✔
923
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
161,368✔
924
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
161,368✔
925
        }
926
      }
927
    }
928
  } else {
929
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
2,860,885✔
930
    if (code) {
2,860,885✔
931
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
932
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
933
    } else {
934
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
935
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
936
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
937
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
938
      }
939
    }
940
  }
941

942
  metaFetchEntryFree(&pEntry);
2,910,858✔
943
  TAOS_RETURN(code);
2,910,858✔
944
}
945

946
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
73,431✔
947
  int32_t code = TSDB_CODE_SUCCESS;
73,431✔
948

949
  // check request
950
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
73,431✔
951
  if (code) {
73,431✔
952
    TAOS_RETURN(code);
×
953
  }
954

955
  // fetch old entry
956
  SMetaEntry *pEntry = NULL;
73,431✔
957
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
73,431✔
958
  if (code) {
73,431✔
959
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
960
              __FILE__, __LINE__, pReq->tbName, version);
961
    TAOS_RETURN(code);
×
962
  }
963

964
  if (pEntry->version >= version) {
73,431✔
965
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
966
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
967
    metaFetchEntryFree(&pEntry);
×
968
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
969
  }
970

971
  // search the column to drop
972
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
73,431✔
973
  SSchema        *pColumn = NULL;
73,431✔
974
  SSchema         tColumn;
73,431✔
975
  int32_t         iColumn = 0;
73,431✔
976
  for (; iColumn < pSchema->nCols; iColumn++) {
35,783,829✔
977
    pColumn = &pSchema->pSchema[iColumn];
35,783,829✔
978
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
35,783,829✔
979
      break;
73,431✔
980
    }
981
  }
982

983
  if (iColumn == pSchema->nCols) {
73,431✔
984
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
985
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
986
    metaFetchEntryFree(&pEntry);
×
987
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
988
  }
989

990
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
73,431✔
991
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
992
              __func__, __FILE__, __LINE__, pReq->colName, version);
993
    metaFetchEntryFree(&pEntry);
×
994
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
995
  }
996

997
  tColumn = *pColumn;
73,431✔
998

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

1036
  // update column extschema
1037
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
73,431✔
1038
  if (code) {
73,431✔
1039
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1040
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1041
    metaFetchEntryFree(&pEntry);
×
1042
    TAOS_RETURN(code);
×
1043
  }
1044

1045
  // do handle entry
1046
  code = metaHandleEntry2(pMeta, pEntry);
73,431✔
1047
  if (code) {
73,431✔
1048
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1049
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1050
    metaFetchEntryFree(&pEntry);
×
1051
  } else {
1052
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
73,431✔
1053
             pEntry->uid, version);
1054
  }
1055

1056
  // build response
1057
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
73,431✔
1058
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,483✔
1059
    if (code) {
28,483✔
1060
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1061
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1062
    } else {
1063
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
31,245,219✔
1064
        SColRef *p = &pEntry->colRef.pColRef[i];
31,216,736✔
1065
        pRsp->pColRefs[i].hasRef = p->hasRef;
31,216,736✔
1066
        pRsp->pColRefs[i].id = p->id;
31,216,736✔
1067
        if (p->hasRef) {
31,216,736✔
1068
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
15,620,297✔
1069
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
15,620,297✔
1070
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
15,620,297✔
1071
        }
1072
      }
1073
    }
1074
  } else {
1075
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
44,948✔
1076
    if (code) {
44,948✔
1077
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1078
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1079
    } else {
1080
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
7,622,285✔
1081
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
7,577,337✔
1082
        pRsp->pSchemaExt[i].colId = p->id;
7,577,337✔
1083
        pRsp->pSchemaExt[i].compress = p->alg;
7,577,337✔
1084
      }
1085
    }
1086
  }
1087

1088
  metaFetchEntryFree(&pEntry);
73,431✔
1089
  TAOS_RETURN(code);
73,431✔
1090
}
1091

1092
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
43,194✔
1093
  int32_t code = TSDB_CODE_SUCCESS;
43,194✔
1094

1095
  // check request
1096
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
43,194✔
1097
  if (code) {
43,194✔
1098
    TAOS_RETURN(code);
×
1099
  }
1100

1101
  if (NULL == pReq->colNewName) {
43,194✔
1102
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1103
              __func__, __FILE__, __LINE__, version);
1104
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1105
  }
1106

1107
  // fetch old entry
1108
  SMetaEntry *pEntry = NULL;
43,194✔
1109
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
43,194✔
1110
  if (code) {
43,194✔
1111
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1112
              __FILE__, __LINE__, pReq->tbName, version);
1113
    TAOS_RETURN(code);
×
1114
  }
1115

1116
  if (pEntry->version >= version) {
43,194✔
1117
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1118
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1119
    metaFetchEntryFree(&pEntry);
×
1120
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1121
  }
1122

1123
  // search the column to update
1124
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
43,194✔
1125
  SSchema        *pColumn = NULL;
43,194✔
1126
  int32_t         iColumn = 0;
43,194✔
1127
  for (int32_t i = 0; i < pSchema->nCols; i++) {
197,501✔
1128
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
197,501✔
1129
      pColumn = &pSchema->pSchema[i];
43,194✔
1130
      iColumn = i;
43,194✔
1131
      break;
43,194✔
1132
    }
1133
  }
1134

1135
  if (NULL == pColumn) {
43,194✔
1136
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1137
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1138
    metaFetchEntryFree(&pEntry);
×
1139
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1140
  }
1141

1142

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

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

1160
  // build response
1161
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
43,194✔
1162
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,592✔
1163
    if (code) {
26,592✔
1164
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1165
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1166
    } else {
1167
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
187,962✔
1168
        SColRef *p = &pEntry->colRef.pColRef[i];
161,370✔
1169
        pRsp->pColRefs[i].hasRef = p->hasRef;
161,370✔
1170
        pRsp->pColRefs[i].id = p->id;
161,370✔
1171
        if (p->hasRef) {
161,370✔
1172
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
99,552✔
1173
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
99,552✔
1174
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
99,552✔
1175
        }
1176
      }
1177
    }
1178
  } else {
1179
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,602✔
1180
    if (code) {
16,602✔
1181
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1182
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1183
    } else {
1184
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
175,793✔
1185
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
159,191✔
1186
        pRsp->pSchemaExt[i].colId = p->id;
159,191✔
1187
        pRsp->pSchemaExt[i].compress = p->alg;
159,191✔
1188
      }
1189
    }
1190
  }
1191

1192
  metaFetchEntryFree(&pEntry);
43,194✔
1193
  TAOS_RETURN(code);
43,194✔
1194
}
1195

1196
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
473,249✔
1197
  int32_t code = TSDB_CODE_SUCCESS;
473,249✔
1198

1199
  // check request
1200
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
473,249✔
1201
  if (code) {
473,249✔
1202
    TAOS_RETURN(code);
×
1203
  }
1204

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

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

1221
  // search the column to update
1222
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
473,249✔
1223
  SSchema        *pColumn = NULL;
473,249✔
1224
  int32_t         iColumn = 0;
473,249✔
1225
  int32_t         rowSize = 0;
473,249✔
1226
  for (int32_t i = 0; i < pSchema->nCols; i++) {
34,122,651✔
1227
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
33,649,402✔
1228
      pColumn = &pSchema->pSchema[i];
473,249✔
1229
      iColumn = i;
473,249✔
1230
    }
1231
    rowSize += pSchema->pSchema[i].bytes;
33,649,402✔
1232
  }
1233

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

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

1249
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
283,835✔
1250
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
283,835✔
1251
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
43,330✔
1252
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1253
              version);
1254
    metaFetchEntryFree(&pEntry);
43,330✔
1255
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
43,330✔
1256
  }
1257

1258
  // do change the column bytes
1259
  pEntry->version = version;
240,505✔
1260
  pSchema->version++;
240,505✔
1261
  pColumn->bytes = pReq->colModBytes;
240,505✔
1262

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

1275
  // build response
1276
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
240,505✔
1277
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,812✔
1278
    if (code) {
25,812✔
1279
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1280
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1281
    } else {
1282
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
15,708,510✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
15,682,698✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
15,682,698✔
1285
        pRsp->pColRefs[i].id = p->id;
15,682,698✔
1286
        if (p->hasRef) {
15,682,698✔
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
75,396✔
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
75,396✔
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
75,396✔
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
214,693✔
1295
    if (code) {
214,693✔
1296
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1297
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1298
    } else {
1299
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,638,893✔
1300
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,424,200✔
1301
        pRsp->pSchemaExt[i].colId = p->id;
8,424,200✔
1302
        pRsp->pSchemaExt[i].compress = p->alg;
8,424,200✔
1303
      }
1304
    }
1305
  }
1306

1307
  metaFetchEntryFree(&pEntry);
240,505✔
1308
  TAOS_RETURN(code);
240,505✔
1309
}
1310

1311
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,468,134✔
1312
  int32_t code = 0;
7,468,134✔
1313

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

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

1333
  TAOS_RETURN(code);
7,467,885✔
1334
}
1335

1336
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1337

1338
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,370,750✔
1339
  if (isNull) {
7,370,750✔
1340
    if (!tTagGet(pOldTag, &value)) {
45,336✔
1341
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,482✔
1342
      return true;
19,482✔
1343
    }
1344
    return false;
25,854✔
1345
  }
1346
  if (!tTagGet(pOldTag, &value)){
7,325,414✔
1347
    return false;
177,305✔
1348
  }
1349
  if (IS_VAR_DATA_TYPE(value.type)) {
7,148,109✔
1350
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
56,592✔
1351
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
35,819✔
1352
      return true;
35,819✔
1353
    }
1354
  } else {
1355
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,091,517✔
1356
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
87,627✔
1357
      return true;
87,627✔
1358
    }
1359
  }
1360
  return false;
7,024,663✔
1361
}
1362

1363
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,468,134✔
1364
  int32_t code = TSDB_CODE_SUCCESS;
7,468,134✔
1365

1366
  // check request
1367
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,468,134✔
1368
  if (code) {
7,468,134✔
1369
    TAOS_RETURN(code);
249✔
1370
  }
1371

1372
  // fetch child entry
1373
  SMetaEntry *pChild = NULL;
7,467,885✔
1374
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,467,885✔
1375
  if (code) {
7,467,885✔
1376
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1377
              __FILE__, __LINE__, pReq->tbName, version);
1378
    TAOS_RETURN(code);
×
1379
  }
1380

1381
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
7,467,885✔
1382
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1383
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1384
    metaFetchEntryFree(&pChild);
×
1385
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1386
  }
1387

1388
  // fetch super entry
1389
  SMetaEntry *pSuper = NULL;
7,467,885✔
1390
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,467,885✔
1391
  if (code) {
7,467,885✔
1392
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1393
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1394
    metaFetchEntryFree(&pChild);
×
1395
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1396
  }
1397

1398
  // search the tag to update
1399
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,467,885✔
1400
  SSchema        *pColumn = NULL;
7,467,885✔
1401
  int32_t         iColumn = 0;
7,467,885✔
1402
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,122,671✔
1403
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,122,671✔
1404
      pColumn = &pTagSchema->pSchema[i];
7,467,885✔
1405
      iColumn = i;
7,467,885✔
1406
      break;
7,467,885✔
1407
    }
1408
  }
1409

1410
  if (NULL == pColumn) {
7,467,885✔
1411
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1412
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1413
    metaFetchEntryFree(&pChild);
×
1414
    metaFetchEntryFree(&pSuper);
×
1415
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1416
  }
1417

1418
  // do change tag value
1419
  pChild->version = version;
7,467,885✔
1420
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,467,885✔
1421
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
116,515✔
1422
    if (NULL == pNewTag) {
116,515✔
1423
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1424
                __LINE__, tstrerror(terrno), version);
1425
      metaFetchEntryFree(&pChild);
×
1426
      metaFetchEntryFree(&pSuper);
×
1427
      TAOS_RETURN(terrno);
×
1428
    }
1429
    pChild->ctbEntry.pTags = pNewTag;
116,515✔
1430
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
116,515✔
1431
  } else {
1432
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,351,370✔
1433

1434
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,351,370✔
1435
    if (NULL == pTagArray) {
7,351,370✔
1436
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1437
                __LINE__, tstrerror(terrno), version);
1438
      metaFetchEntryFree(&pChild);
×
1439
      metaFetchEntryFree(&pSuper);
×
1440
      TAOS_RETURN(terrno);
×
1441
    }
1442

1443
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,624,369✔
1444
      STagVal value = {
8,406,883✔
1445
          .type = pTagSchema->pSchema[i].type,
8,406,883✔
1446
          .cid = pTagSchema->pSchema[i].colId,
8,406,883✔
1447
      };
1448

1449
      if (iColumn == i) {
8,406,883✔
1450
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,351,370✔
1451
          taosArrayDestroy(pTagArray);
133,884✔
1452
          metaFetchEntryFree(&pChild);
133,884✔
1453
          metaFetchEntryFree(&pSuper);
133,884✔
1454
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
133,884✔
1455
        }
1456
        if (pReq->isNull) {
7,217,486✔
1457
          continue;
23,916✔
1458
        }
1459
        if (IS_VAR_DATA_TYPE(value.type)) {
7,193,570✔
1460
          value.pData = pReq->pTagVal;
99,800✔
1461
          value.nData = pReq->nTagVal;
99,800✔
1462
        } else {
1463
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,093,770✔
1464
        }
1465
      } else if (!tTagGet(pOldTag, &value)) {
1,055,513✔
1466
        continue;
286,845✔
1467
      }
1468

1469
      if (NULL == taosArrayPush(pTagArray, &value)) {
7,962,238✔
1470
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1471
                  __LINE__, tstrerror(terrno), version);
1472
        taosArrayDestroy(pTagArray);
×
1473
        metaFetchEntryFree(&pChild);
×
1474
        metaFetchEntryFree(&pSuper);
×
1475
        TAOS_RETURN(terrno);
×
1476
      }
1477
    }
1478

1479
    STag *pNewTag = NULL;
7,217,486✔
1480
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,217,486✔
1481
    if (code) {
7,217,486✔
1482
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1483
                __LINE__, tstrerror(code), version);
1484
      taosArrayDestroy(pTagArray);
×
1485
      metaFetchEntryFree(&pChild);
×
1486
      metaFetchEntryFree(&pSuper);
×
1487
      TAOS_RETURN(code);
×
1488
    }
1489
    taosArrayDestroy(pTagArray);
7,217,486✔
1490
    taosMemoryFree(pChild->ctbEntry.pTags);
7,217,486✔
1491
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,217,486✔
1492
  }
1493

1494
  // do handle entry
1495
  code = metaHandleEntry2(pMeta, pChild);
7,334,001✔
1496
  if (code) {
7,334,001✔
1497
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1498
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1499
    metaFetchEntryFree(&pChild);
×
1500
    metaFetchEntryFree(&pSuper);
×
1501
    TAOS_RETURN(code);
×
1502
  } else {
1503
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
7,334,001✔
1504
             pChild->uid, version);
1505
  }
1506

1507
  // free resource and return
1508
  metaFetchEntryFree(&pChild);
7,334,001✔
1509
  metaFetchEntryFree(&pSuper);
7,334,001✔
1510
  TAOS_RETURN(code);
7,334,001✔
1511
}
1512

1513
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,230✔
1514
  int32_t code = 0;
3,230✔
1515

1516
  // check tag name
1517
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,230✔
1518
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1519
              __FILE__, __LINE__, version);
1520
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1521
  }
1522

1523
  // check name
1524
  void   *value = NULL;
3,230✔
1525
  int32_t valueSize = 0;
3,230✔
1526
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,230✔
1527
  if (code) {
3,230✔
1528
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1529
              __FILE__, __LINE__, pReq->tbName, version);
1530
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1531
    TAOS_RETURN(code);
×
1532
  }
1533
  tdbFreeClear(value);
3,230✔
1534

1535
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,230✔
1536
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1537
              __FILE__, __LINE__, version);
1538
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1539
  }
1540

1541
  TAOS_RETURN(code);
3,230✔
1542
}
1543

1544
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,230✔
1545
  int32_t code = TSDB_CODE_SUCCESS;
3,230✔
1546

1547
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,230✔
1548
  if (code) {
3,230✔
1549
    TAOS_RETURN(code);
×
1550
  }
1551

1552
  // fetch child entry
1553
  SMetaEntry *pChild = NULL;
3,230✔
1554
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,230✔
1555
  if (code) {
3,230✔
1556
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1557
              __FILE__, __LINE__, pReq->tbName, version);
1558
    TAOS_RETURN(code);
×
1559
  }
1560

1561
  if (pChild->type != TSDB_CHILD_TABLE) {
3,230✔
1562
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1563
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1564
    metaFetchEntryFree(&pChild);
×
1565
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1566
  }
1567

1568
  // fetch super entry
1569
  SMetaEntry *pSuper = NULL;
3,230✔
1570
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,230✔
1571
  if (code) {
3,230✔
1572
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1573
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1574
    metaFetchEntryFree(&pChild);
×
1575
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1576
  }
1577

1578
  // search the tags to update
1579
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,230✔
1580

1581
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,230✔
1582
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1583
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1584
    metaFetchEntryFree(&pChild);
×
1585
    metaFetchEntryFree(&pSuper);
×
1586
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1587
  }
1588

1589
  // do check if tag name exists
1590
  SHashObj *pTagTable =
1591
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
3,230✔
1592
  if (pTagTable == NULL) {
3,230✔
1593
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1594
              __LINE__, tstrerror(terrno), version);
1595
    metaFetchEntryFree(&pChild);
×
1596
    metaFetchEntryFree(&pSuper);
×
1597
    TAOS_RETURN(terrno);
×
1598
  }
1599

1600
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
22,610✔
1601
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
19,380✔
1602
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
19,380✔
1603
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1604
                __LINE__, tstrerror(terrno), version);
1605
      taosHashCleanup(pTagTable);
×
1606
      metaFetchEntryFree(&pChild);
×
1607
      metaFetchEntryFree(&pSuper);
×
1608
      TAOS_RETURN(terrno);
×
1609
    }
1610
  }
1611

1612
  int32_t numOfChangedTags = 0;
3,230✔
1613
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
25,840✔
1614
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
22,610✔
1615
        ? numOfChangedTags++
19,380✔
1616
        : 0;
22,610✔
1617
  }
1618
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,230✔
1619
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1620
              __FILE__, __LINE__, version);
1621
    taosHashCleanup(pTagTable);
×
1622
    metaFetchEntryFree(&pChild);
×
1623
    metaFetchEntryFree(&pSuper);
×
1624
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1625
  }
1626

1627
  // do change tag value
1628
  pChild->version = version;
3,230✔
1629
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,230✔
1630
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,230✔
1631
  if (NULL == pTagArray) {
3,230✔
1632
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1633
              __LINE__, tstrerror(terrno), version);
1634
    taosHashCleanup(pTagTable);
×
1635
    metaFetchEntryFree(&pChild);
×
1636
    metaFetchEntryFree(&pSuper);
×
1637
    TAOS_RETURN(terrno);
×
1638
  }
1639

1640
  bool allSame = true;
3,230✔
1641

1642
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
25,840✔
1643
    SSchema *pCol = &pTagSchema->pSchema[i];
22,610✔
1644
    STagVal  value = {
22,610✔
1645
         .cid = pCol->colId,
22,610✔
1646
    };
1647

1648
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
22,610✔
1649
    if (pTagVal == NULL) {
22,610✔
1650
      if (!tTagGet(pOldTag, &value)) {
3,230✔
1651
        continue;
×
1652
      }
1653
    } else {
1654
      value.type = pCol->type;
19,380✔
1655
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
19,380✔
1656
        allSame = false;
10,336✔
1657
      }
1658
      if (pTagVal->isNull) {
19,380✔
1659
        continue;
3,876✔
1660
      }
1661

1662
      if (IS_VAR_DATA_TYPE(pCol->type)) {
15,504✔
1663
        value.pData = pTagVal->pTagVal;
2,584✔
1664
        value.nData = pTagVal->nTagVal;
2,584✔
1665
      } else {
1666
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
12,920✔
1667
      }
1668
    }
1669

1670
    if (taosArrayPush(pTagArray, &value) == NULL) {
18,734✔
1671
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1672
                __LINE__, tstrerror(terrno), version);
1673
      taosHashCleanup(pTagTable);
×
1674
      taosArrayDestroy(pTagArray);
×
1675
      metaFetchEntryFree(&pChild);
×
1676
      metaFetchEntryFree(&pSuper);
×
1677
      TAOS_RETURN(terrno);
×
1678
    }
1679
  }
1680

1681
  if (allSame) {
3,230✔
1682
    metaWarn("vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
646✔
1683
             __LINE__, version);
1684
    taosHashCleanup(pTagTable);
646✔
1685
    taosArrayDestroy(pTagArray);
646✔
1686
    metaFetchEntryFree(&pChild);
646✔
1687
    metaFetchEntryFree(&pSuper);
646✔
1688
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
646✔
1689
  } 
1690
  STag *pNewTag = NULL;
2,584✔
1691
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
2,584✔
1692
  if (code) {
2,584✔
1693
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1694
              __LINE__, tstrerror(code), version);
1695
    taosHashCleanup(pTagTable);
×
1696
    taosArrayDestroy(pTagArray);
×
1697
    metaFetchEntryFree(&pChild);
×
1698
    metaFetchEntryFree(&pSuper);
×
1699
    TAOS_RETURN(code);
×
1700
  }
1701
  taosArrayDestroy(pTagArray);
2,584✔
1702
  taosMemoryFree(pChild->ctbEntry.pTags);
2,584✔
1703
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
2,584✔
1704

1705
  // do handle entry
1706
  code = metaHandleEntry2(pMeta, pChild);
2,584✔
1707
  if (code) {
2,584✔
1708
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1709
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1710
    taosHashCleanup(pTagTable);
×
1711
    metaFetchEntryFree(&pChild);
×
1712
    metaFetchEntryFree(&pSuper);
×
1713
    TAOS_RETURN(code);
×
1714
  } else {
1715
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
2,584✔
1716
             pChild->uid, version);
1717
  }
1718

1719
  taosHashCleanup(pTagTable);
2,584✔
1720
  metaFetchEntryFree(&pChild);
2,584✔
1721
  metaFetchEntryFree(&pSuper);
2,584✔
1722
  TAOS_RETURN(code);
2,584✔
1723
}
1724

1725
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,116✔
1726
  int32_t code = TSDB_CODE_SUCCESS;
24,116✔
1727

1728
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
24,116✔
1729
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1730
              __FILE__, __LINE__, version);
1731
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1732
  }
1733

1734
  return code;
24,116✔
1735
}
1736

1737
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,116✔
1738
  int32_t code = 0;
24,116✔
1739

1740
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
24,116✔
1741
  if (code) {
24,116✔
1742
    TAOS_RETURN(code);
×
1743
  }
1744

1745
  // fetch entry
1746
  SMetaEntry *pEntry = NULL;
24,116✔
1747
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
24,116✔
1748
  if (code) {
24,116✔
1749
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1750
              __FILE__, __LINE__, pReq->tbName, version);
1751
    TAOS_RETURN(code);
×
1752
  }
1753

1754
  // do change the entry
1755
  pEntry->version = version;
24,116✔
1756
  if (pEntry->type == TSDB_CHILD_TABLE) {
24,116✔
1757
    if (pReq->updateTTL) {
11,379✔
1758
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,387✔
1759
    }
1760
    if (pReq->newCommentLen >= 0) {
11,379✔
1761
      char *pNewComment = NULL;
6,992✔
1762
      if (pReq->newCommentLen) {
6,992✔
1763
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,465✔
1764
        if (NULL == pNewComment) {
4,465✔
1765
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1766
                    __LINE__, tstrerror(terrno), version);
1767
          metaFetchEntryFree(&pEntry);
×
1768
          TAOS_RETURN(terrno);
×
1769
        }
1770
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,465✔
1771
      } else {
1772
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,527✔
1773
      }
1774
      pEntry->ctbEntry.comment = pNewComment;
6,992✔
1775
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
6,992✔
1776
    }
1777
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
12,737✔
1778
    if (pReq->updateTTL) {
12,737✔
1779
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,547✔
1780
    }
1781
    if (pReq->newCommentLen >= 0) {
12,737✔
1782
      char *pNewComment = NULL;
8,190✔
1783
      if (pReq->newCommentLen > 0) {
8,190✔
1784
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
5,663✔
1785
        if (NULL == pNewComment) {
5,663✔
1786
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1787
                    __LINE__, tstrerror(terrno), version);
1788
          metaFetchEntryFree(&pEntry);
×
1789
          TAOS_RETURN(terrno);
×
1790
        }
1791
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
5,663✔
1792
      } else {
1793
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,527✔
1794
      }
1795
      pEntry->ntbEntry.comment = pNewComment;
8,190✔
1796
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,190✔
1797
    }
1798
  } else {
1799
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1800
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1801
    metaFetchEntryFree(&pEntry);
×
1802
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1803
  }
1804

1805
  // do handle entry
1806
  code = metaHandleEntry2(pMeta, pEntry);
24,116✔
1807
  if (code) {
24,116✔
1808
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1809
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1810
    metaFetchEntryFree(&pEntry);
×
1811
    TAOS_RETURN(code);
×
1812
  } else {
1813
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
24,116✔
1814
             pEntry->uid, version);
1815
  }
1816

1817
  metaFetchEntryFree(&pEntry);
24,116✔
1818
  TAOS_RETURN(code);
24,116✔
1819
}
1820

1821
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5,893✔
1822
  int32_t code = TSDB_CODE_SUCCESS;
5,893✔
1823

1824
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5,893✔
1825
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1826
              __FILE__, __LINE__, version);
1827
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1828
  }
1829

1830
  SMetaEntry *pEntry = NULL;
5,893✔
1831
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5,893✔
1832
  if (code) {
5,893✔
1833
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1834
              __FILE__, __LINE__, pReq->tbName, version);
1835
    TAOS_RETURN(code);
×
1836
  }
1837

1838
  if (pEntry->version >= version) {
5,893✔
1839
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1840
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1841
    metaFetchEntryFree(&pEntry);
×
1842
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1843
  }
1844

1845
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
5,893✔
1846
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1847
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1848
    metaFetchEntryFree(&pEntry);
×
1849
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1850
  }
1851

1852
  // do change the entry
1853
  int8_t           updated = 0;
5,893✔
1854
  SColCmprWrapper *wp = &pEntry->colCmpr;
5,893✔
1855
  for (int32_t i = 0; i < wp->nCols; i++) {
47,144✔
1856
    SColCmpr *p = &wp->pColCmpr[i];
41,251✔
1857
    if (p->id == pReq->colId) {
41,251✔
1858
      uint32_t dst = 0;
5,893✔
1859
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5,893✔
1860
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1861
      if (updated > 0) {
5,893✔
1862
        p->alg = dst;
5,893✔
1863
      }
1864
    }
1865
  }
1866

1867
  if (updated == 0) {
5,893✔
1868
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1869
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1870
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1871
    metaFetchEntryFree(&pEntry);
×
1872
    TAOS_RETURN(code);
×
1873
  } else if (updated < 0) {
5,893✔
1874
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1875
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1876
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1877
    metaFetchEntryFree(&pEntry);
×
1878
    TAOS_RETURN(code);
×
1879
  }
1880

1881
  pEntry->version = version;
5,893✔
1882

1883
  // do handle entry
1884
  code = metaHandleEntry2(pMeta, pEntry);
5,893✔
1885
  if (code) {
5,893✔
1886
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1887
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1888
    metaFetchEntryFree(&pEntry);
×
1889
    TAOS_RETURN(code);
×
1890
  } else {
1891
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5,893✔
1892
             pEntry->uid, version);
1893
  }
1894

1895
  metaFetchEntryFree(&pEntry);
5,893✔
1896
  TAOS_RETURN(code);
5,893✔
1897
}
1898

1899
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
72,018✔
1900
  int32_t code = TSDB_CODE_SUCCESS;
72,018✔
1901

1902
  // check request
1903
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
72,018✔
1904
  if (code) {
72,018✔
1905
    TAOS_RETURN(code);
×
1906
  }
1907

1908
  if (NULL == pReq->refDbName) {
72,018✔
1909
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1910
              __func__, __FILE__, __LINE__, version);
1911
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1912
  }
1913

1914
  if (NULL == pReq->refTbName) {
72,018✔
1915
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1916
              __func__, __FILE__, __LINE__, version);
1917
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1918
  }
1919

1920
  if (NULL == pReq->refColName) {
72,018✔
1921
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1922
              __func__, __FILE__, __LINE__, version);
1923
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1924
  }
1925

1926
  // fetch old entry
1927
  SMetaEntry *pEntry = NULL;
72,018✔
1928
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
72,018✔
1929
  if (code) {
72,018✔
1930
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1931
              __FILE__, __LINE__, pReq->tbName, version);
1932
    TAOS_RETURN(code);
×
1933
  }
1934

1935
  if (pEntry->version >= version) {
72,018✔
1936
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1937
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1938
    metaFetchEntryFree(&pEntry);
×
1939
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1940
  }
1941

1942
  // fetch super entry
1943
  SMetaEntry *pSuper = NULL;
72,018✔
1944
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
72,018✔
1945
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
26,012✔
1946
    if (code) {
26,012✔
1947
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1948
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1949
      metaFetchEntryFree(&pEntry);
×
1950
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1951
    }
1952
  }
1953

1954
  // search the column to update
1955
  SSchemaWrapper *pSchema =
72,018✔
1956
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
72,018✔
1957
  SColRef *pColRef = NULL;
72,018✔
1958
  int32_t  iColumn = 0;
72,018✔
1959
  for (int32_t i = 0; i < pSchema->nCols; i++) {
357,428✔
1960
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
357,428✔
1961
      pColRef = &pEntry->colRef.pColRef[i];
72,018✔
1962
      iColumn = i;
72,018✔
1963
      break;
72,018✔
1964
    }
1965
  }
1966

1967
  if (NULL == pColRef) {
72,018✔
1968
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1969
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1970
    metaFetchEntryFree(&pEntry);
×
1971
    metaFetchEntryFree(&pSuper);
×
1972
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1973
  }
1974

1975
  // do update column name
1976
  pEntry->version = version;
72,018✔
1977
  pColRef->hasRef = true;
72,018✔
1978
  pColRef->id = pSchema->pSchema[iColumn].colId;
72,018✔
1979
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
72,018✔
1980
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
72,018✔
1981
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
72,018✔
1982
  pSchema->version++;
72,018✔
1983
  pEntry->colRef.version++;
72,018✔
1984

1985
  // do handle entry
1986
  code = metaHandleEntry2(pMeta, pEntry);
72,018✔
1987
  if (code) {
72,018✔
1988
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1989
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1990
    metaFetchEntryFree(&pEntry);
×
1991
    metaFetchEntryFree(&pSuper);
×
1992
    TAOS_RETURN(code);
×
1993
  } else {
1994
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
72,018✔
1995
             pEntry->uid, version);
1996
  }
1997

1998
  // build response
1999
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
72,018✔
2000
  if (code) {
72,018✔
2001
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2002
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2003
  } else {
2004
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
538,420✔
2005
      SColRef *p = &pEntry->colRef.pColRef[i];
466,402✔
2006
      pRsp->pColRefs[i].hasRef = p->hasRef;
466,402✔
2007
      pRsp->pColRefs[i].id = p->id;
466,402✔
2008
      if (p->hasRef) {
466,402✔
2009
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
321,936✔
2010
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
321,936✔
2011
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
321,936✔
2012
      }
2013
    }
2014
  }
2015

2016
  metaFetchEntryFree(&pEntry);
72,018✔
2017
  metaFetchEntryFree(&pSuper);
72,018✔
2018
  TAOS_RETURN(code);
72,018✔
2019
}
2020

2021
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
53,148✔
2022
  int32_t code = TSDB_CODE_SUCCESS;
53,148✔
2023

2024
  // check request
2025
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
53,148✔
2026
  if (code) {
53,148✔
2027
    TAOS_RETURN(code);
×
2028
  }
2029

2030
  // fetch old entry
2031
  SMetaEntry *pEntry = NULL;
53,148✔
2032
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
53,148✔
2033
  if (code) {
53,148✔
2034
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2035
              __FILE__, __LINE__, pReq->tbName, version);
2036
    TAOS_RETURN(code);
×
2037
  }
2038

2039
  if (pEntry->version >= version) {
53,148✔
2040
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2041
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2042
    metaFetchEntryFree(&pEntry);
×
2043
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2044
  }
2045

2046
  // fetch super entry
2047
  SMetaEntry *pSuper = NULL;
53,148✔
2048
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
53,148✔
2049
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
26,780✔
2050
    if (code) {
26,780✔
2051
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2052
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2053
      metaFetchEntryFree(&pEntry);
×
2054
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2055
    }
2056
  }
2057

2058
  // search the column to update
2059
  SSchemaWrapper *pSchema =
53,148✔
2060
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
53,148✔
2061
  SColRef *pColRef = NULL;
53,148✔
2062
  int32_t  iColumn = 0;
53,148✔
2063
  for (int32_t i = 0; i < pSchema->nCols; i++) {
216,300✔
2064
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
216,300✔
2065
      pColRef = &pEntry->colRef.pColRef[i];
53,148✔
2066
      iColumn = i;
53,148✔
2067
      break;
53,148✔
2068
    }
2069
  }
2070

2071
  if (NULL == pColRef) {
53,148✔
2072
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2073
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2074
    metaFetchEntryFree(&pEntry);
×
2075
    metaFetchEntryFree(&pSuper);
×
2076
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2077
  }
2078

2079
  // do update column name
2080
  pEntry->version = version;
53,148✔
2081
  pColRef->hasRef = false;
53,148✔
2082
  pColRef->id = pSchema->pSchema[iColumn].colId;
53,148✔
2083
  pSchema->version++;
53,148✔
2084
  pEntry->colRef.version++;
53,148✔
2085

2086
  // do handle entry
2087
  code = metaHandleEntry2(pMeta, pEntry);
53,148✔
2088
  if (code) {
53,148✔
2089
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2090
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2091
    metaFetchEntryFree(&pEntry);
×
2092
    metaFetchEntryFree(&pSuper);
×
2093
    TAOS_RETURN(code);
×
2094
  } else {
2095
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
53,148✔
2096
             pEntry->uid, version);
2097
  }
2098

2099
  // build response
2100
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
53,148✔
2101
  if (code) {
53,148✔
2102
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2103
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2104
  } else {
2105
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
326,304✔
2106
      SColRef *p = &pEntry->colRef.pColRef[i];
273,156✔
2107
      pRsp->pColRefs[i].hasRef = p->hasRef;
273,156✔
2108
      pRsp->pColRefs[i].id = p->id;
273,156✔
2109
      if (p->hasRef) {
273,156✔
2110
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
135,342✔
2111
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
135,342✔
2112
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
135,342✔
2113
      }
2114
    }
2115
  }
2116

2117
  metaFetchEntryFree(&pEntry);
53,148✔
2118
  metaFetchEntryFree(&pSuper);
53,148✔
2119
  TAOS_RETURN(code);
53,148✔
2120
}
2121

2122
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,320✔
2123
  int32_t code = TSDB_CODE_SUCCESS;
4,320✔
2124

2125
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,320✔
2126
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2127
              __FILE__, __LINE__, version);
2128
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2129
  }
2130

2131
  SMetaEntry *pEntry = NULL;
4,320✔
2132
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
4,320✔
2133
  if (code) {
4,320✔
2134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2135
              __FILE__, __LINE__, pReq->name, version);
2136
    TAOS_RETURN(code);
×
2137
  }
2138

2139
  if (pEntry->type != TSDB_SUPER_TABLE) {
4,320✔
2140
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2141
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2142
    metaFetchEntryFree(&pEntry);
×
2143
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2144
  }
2145

2146
  if (pEntry->uid != pReq->suid) {
4,320✔
2147
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2148
              ", version:%" PRId64,
2149
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2150
    metaFetchEntryFree(&pEntry);
×
2151
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2152
  }
2153

2154
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2155
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2156
  //   PRId64,
2157
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2158
  //             pReq->schemaTag.version, version);
2159
  //   metaFetchEntryFree(&pEntry);
2160
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2161
  // }
2162

2163
  // do change the entry
2164
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
4,320✔
2165
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
4,320✔
2166
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,320✔
2167
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2168
              __func__, __FILE__, __LINE__, pReq->name, version);
2169
    metaFetchEntryFree(&pEntry);
×
2170
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2171
  }
2172

2173
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
4,320✔
2174
    metaError(
×
2175
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2176
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2177
        version);
2178
    metaFetchEntryFree(&pEntry);
×
2179
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2180
  }
2181

2182
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2183
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2184
  //   PRId64,
2185
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2186
  //             pNewTagSchema->version, version);
2187
  //   metaFetchEntryFree(&pEntry);
2188
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2189
  // }
2190

2191
  int32_t numOfChangedTags = 0;
4,320✔
2192
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
22,096✔
2193
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,776✔
2194
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,776✔
2195

2196
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
17,776✔
2197
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
17,776✔
2198
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2199
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2200
      metaFetchEntryFree(&pEntry);
×
2201
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2202
    }
2203

2204
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
17,776✔
2205
      numOfChangedTags++;
4,320✔
2206
      SSCHMEA_SET_IDX_ON(pOldColumn);
4,320✔
2207
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
13,456✔
2208
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2209
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2210
      metaFetchEntryFree(&pEntry);
×
2211
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2212
    }
2213
  }
2214

2215
  if (numOfChangedTags != 1) {
4,320✔
2216
    metaError(
×
2217
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2218
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2219
    metaFetchEntryFree(&pEntry);
×
2220
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2221
  }
2222

2223
  pEntry->version = version;
4,320✔
2224
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,320✔
2225

2226
  // do handle the entry
2227
  code = metaHandleEntry2(pMeta, pEntry);
4,320✔
2228
  if (code) {
4,320✔
2229
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2230
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2231
    metaFetchEntryFree(&pEntry);
×
2232
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2233
  } else {
2234
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,320✔
2235
             pEntry->uid, version);
2236
  }
2237

2238
  metaFetchEntryFree(&pEntry);
4,320✔
2239
  TAOS_RETURN(code);
4,320✔
2240
}
2241

2242
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,870✔
2243
  int32_t code = TSDB_CODE_SUCCESS;
2,870✔
2244

2245
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,870✔
2246
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2247
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2248
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2249
  }
2250

2251
  SMetaEntry *pEntry = NULL;
2,870✔
2252
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,870✔
2253
  if (code) {
2,870✔
2254
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2255
              __FILE__, __LINE__, pReq->stb, version);
2256
    TAOS_RETURN(code);
×
2257
  }
2258

2259
  if (TSDB_SUPER_TABLE != pEntry->type) {
2,870✔
2260
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2261
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2262
    metaFetchEntryFree(&pEntry);
×
2263
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2264
  }
2265

2266
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
2,870✔
2267
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,870✔
2268
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2269
              __func__, __FILE__, __LINE__, pReq->stb, version);
2270
    metaFetchEntryFree(&pEntry);
×
2271
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2272
  }
2273

2274
  // search and set the tag index off
2275
  int32_t numOfChangedTags = 0;
2,870✔
2276
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
7,912✔
2277
    SSchema *pCol = pTagSchema->pSchema + i;
7,912✔
2278
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
7,912✔
2279
      if (!IS_IDX_ON(pCol)) {
2,870✔
2280
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2281
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2282
        metaFetchEntryFree(&pEntry);
×
2283
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2284
      }
2285
      numOfChangedTags++;
2,870✔
2286
      SSCHMEA_SET_IDX_OFF(pCol);
2,870✔
2287
      break;
2,870✔
2288
    }
2289
  }
2290

2291
  if (numOfChangedTags != 1) {
2,870✔
2292
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2293
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2294
    metaFetchEntryFree(&pEntry);
×
2295
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2296
  }
2297

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

2312
  metaFetchEntryFree(&pEntry);
2,870✔
2313
  TAOS_RETURN(code);
2,870✔
2314
}
2315

2316
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,249,456✔
2317
  int32_t code = TSDB_CODE_SUCCESS;
7,249,456✔
2318

2319
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,249,456✔
2320
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
16,565✔
2321
              __FILE__, __LINE__, version);
2322
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
16,565✔
2323
  }
2324

2325
  SMetaEntry *pEntry = NULL;
7,233,879✔
2326
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,235,223✔
2327
  if (code) {
7,246,827✔
2328
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2329
              __FILE__, __LINE__, pReq->name, version);
2330
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2331
  }
2332

2333
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,246,827✔
2334
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2335
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2336
    metaFetchEntryFree(&pEntry);
×
2337
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2338
  }
2339

2340
  SMetaEntry entry = {
21,680,417✔
2341
      .version = version,
2342
      .type = TSDB_SUPER_TABLE,
2343
      .uid = pReq->suid,
7,228,040✔
2344
      .name = pReq->name,
7,225,011✔
2345
      .stbEntry.schemaRow = pReq->schemaRow,
2346
      .stbEntry.schemaTag = pReq->schemaTag,
2347
      .stbEntry.keep = pReq->keep,
7,224,110✔
2348
      .colCmpr = pReq->colCmpr,
2349
      .pExtSchemas = pReq->pExtSchemas,
7,226,929✔
2350
  };
2351
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,219,525✔
2352
  if (pReq->virtualStb) {
7,219,525✔
2353
    TABLE_SET_VIRTUAL(entry.flags);
19,710✔
2354
  }
2355
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,215,424✔
2356
    TABLE_SET_ROLLUP(entry.flags);
4,386✔
2357
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,386✔
2358
  }
2359

2360
  // do handle the entry
2361
  code = metaHandleEntry2(pMeta, &entry);
7,223,335✔
2362
  if (code) {
7,223,716✔
2363
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2364
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2365
    metaFetchEntryFree(&pEntry);
×
2366
    TAOS_RETURN(code);
×
2367
  } else {
2368
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,223,716✔
2369
             pReq->suid, version);
2370
  }
2371

2372
  metaFetchEntryFree(&pEntry);
7,257,731✔
2373
  TAOS_RETURN(code);
7,253,944✔
2374
}
2375

2376
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2377
  int32_t code = 0;
×
2378

2379
  if (taosArrayGetSize(uidArray) == 0) {
×
2380
    return TSDB_CODE_SUCCESS;
×
2381
  }
2382

2383
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2384
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2385
    SMetaInfo info;
×
2386
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2387
    if (code) {
×
2388
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2389
                __func__, __FILE__, __LINE__, uid, code);
2390
      return code;
×
2391
    }
2392

2393
    SMetaEntry entry = {
×
2394
        .version = version,
2395
        .uid = uid,
2396
    };
2397

2398
    if (info.suid == 0) {
×
2399
      entry.type = -TSDB_NORMAL_TABLE;
×
2400
    } else if (info.suid == uid) {
×
2401
      entry.type = -TSDB_SUPER_TABLE;
×
2402
    } else {
2403
      entry.type = -TSDB_CHILD_TABLE;
×
2404
    }
2405
    code = metaHandleEntry2(pMeta, &entry);
×
2406
    if (code) {
×
2407
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2408
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2409
      return code;
×
2410
    }
2411
  }
2412
  return code;
×
2413
}
2414

2415
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
21,930✔
2416
  int32_t code = TSDB_CODE_SUCCESS;
21,930✔
2417

2418
  if (NULL == pReq->name || pReq->name[0] == 0) {
21,930✔
2419
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2420
              __LINE__, version);
2421
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2422
  }
2423

2424
  SMetaEntry *pEntry = NULL;
21,930✔
2425
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
21,930✔
2426
  if (code) {
21,930✔
2427
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2428
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2429
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2430
  }
2431

2432
  if (pEntry->type != TSDB_SUPER_TABLE) {
21,930✔
2433
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2434
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2435
    metaFetchEntryFree(&pEntry);
×
2436
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2437
  }
2438

2439
  if (pEntry->uid != pReq->tbUid) {
21,930✔
2440
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2441
              ", version:%" PRId64,
2442
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2443
    metaFetchEntryFree(&pEntry);
×
2444
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2445
  }
2446

2447
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
21,930✔
2448
    // overwrite the old rsma definition if exists
2449
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2450
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2451
  } else {
2452
    TABLE_SET_ROLLUP(pEntry->flags);
21,930✔
2453
  }
2454

2455
  SMetaEntry entry = *pEntry;
21,930✔
2456
  entry.version = version;
21,930✔
2457
  entry.stbEntry.rsmaParam.name = pReq->name;
21,930✔
2458
  entry.stbEntry.rsmaParam.uid = pReq->uid;
21,930✔
2459
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
21,930✔
2460
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
21,930✔
2461
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
21,930✔
2462
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
21,930✔
2463
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
21,930✔
2464
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
21,930✔
2465

2466
  // do handle the entry
2467
  code = metaHandleEntry2(pMeta, &entry);
21,930✔
2468
  if (code) {
21,930✔
2469
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2470
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2471
    metaFetchEntryFree(&pEntry);
×
2472
    TAOS_RETURN(code);
×
2473
  } else {
2474
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
21,930✔
2475
    pMeta->pVnode->config.isRsma = 1;
21,930✔
2476
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
21,930✔
2477
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2478
  }
2479

2480
  metaFetchEntryFree(&pEntry);
21,930✔
2481
  TAOS_RETURN(code);
21,930✔
2482
}
2483

2484
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,386✔
2485
  int32_t code = TSDB_CODE_SUCCESS;
4,386✔
2486

2487
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,386✔
2488
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2489
              __LINE__, version);
2490
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2491
  }
2492

2493
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
4,386✔
2494
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2495
              __LINE__, version);
2496
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2497
  }
2498

2499
  SMetaEntry *pEntry = NULL;
4,386✔
2500
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,386✔
2501
  if (code) {
4,386✔
2502
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2503
             __func__, __LINE__, pReq->name, pReq->tbName, version);
2504
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2505
  }
2506

2507
  if (pEntry->type != pReq->tbType) {
4,386✔
2508
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2509
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2510
    metaFetchEntryFree(&pEntry);
×
2511
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2512
  }
2513

2514
  if (pEntry->uid != pReq->tbUid) {
4,386✔
2515
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2516
              ", version:%" PRId64,
2517
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2518
    metaFetchEntryFree(&pEntry);
×
2519
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2520
  }
2521

2522
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
4,386✔
2523
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,386✔
2524
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,386✔
2525
      metaError(
×
2526
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2527
          "uid:%" PRIi64 ", version:%" PRId64,
2528
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2529
          pReq->uid, version);
2530
      metaFetchEntryFree(&pEntry);
×
2531
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2532
    }
2533
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
4,386✔
2534
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,386✔
2535
  } else {
2536
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2537
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
2538
    metaFetchEntryFree(&pEntry);
×
2539
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2540
  }
2541

2542
  SMetaEntry entry = *pEntry;
4,386✔
2543
  entry.version = version;
4,386✔
2544
  TABLE_RESET_ROLLUP(entry.flags);
4,386✔
2545
  entry.stbEntry.rsmaParam.uid = 0;
4,386✔
2546
  entry.stbEntry.rsmaParam.name = NULL;
4,386✔
2547
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,386✔
2548
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,386✔
2549
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,386✔
2550

2551
  // do handle the entry
2552
  code = metaHandleEntry2(pMeta, &entry);
4,386✔
2553
  if (code) {
4,386✔
2554
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2555
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
2556
    metaFetchEntryFree(&pEntry);
×
2557
    TAOS_RETURN(code);
×
2558
  } else {
2559
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
4,386✔
2560
      pMeta->pVnode->config.isRsma = 0;
×
2561
    }
2562
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
4,386✔
2563
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2564
  }
2565

2566
  metaFetchEntryFree(&pEntry);
4,386✔
2567
  TAOS_RETURN(code);
4,386✔
2568
}
2569

2570
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,696✔
2571
  int32_t code = TSDB_CODE_SUCCESS;
11,696✔
2572

2573
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,696✔
2574
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2575
              __LINE__, version);
2576
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2577
  }
2578

2579
  SMetaEntry *pEntry = NULL;
11,696✔
2580
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,696✔
2581
  if (code) {
11,696✔
2582
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2583
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2584
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2585
  }
2586

2587
  if (pEntry->uid != pReq->tbUid) {
11,696✔
2588
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2589
              ", version:%" PRId64,
2590
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2591
    metaFetchEntryFree(&pEntry);
×
2592
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2593
  }
2594

2595
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
11,696✔
2596
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
11,696✔
2597
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
11,696✔
2598
  } else {
2599
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2600
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2601
    metaFetchEntryFree(&pEntry);
×
2602
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2603
  }
2604

2605
  SMetaEntry entry = *pEntry;
11,696✔
2606
  entry.version = version;
11,696✔
2607
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
11,696✔
2608
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
11,696✔
2609
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
11,696✔
2610
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
11,696✔
2611
  }
2612
  // do handle the entry
2613
  code = metaHandleEntry2(pMeta, &entry);
11,696✔
2614
  if (code) {
11,696✔
2615
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2616
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2617
    metaFetchEntryFree(&pEntry);
×
2618
    TAOS_RETURN(code);
×
2619
  } else {
2620
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
11,696✔
2621
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2622
  }
2623

2624
  metaFetchEntryFree(&pEntry);
11,696✔
2625
  TAOS_RETURN(code);
11,696✔
2626
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc