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

taosdata / TDengine / #4899

27 Dec 2025 07:32AM UTC coverage: 65.534% (+0.5%) from 65.061%
#4899

push

travis-ci

web-flow
test: remove semaphore test (#34071)

189567 of 289265 relevant lines covered (65.53%)

114701701.06 hits per line

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

67.42
/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,445,922✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,445,922✔
31
  void     *value = NULL;
4,460,909✔
32
  int32_t   valueSize = 0;
4,461,825✔
33
  SMetaInfo info;
4,460,497✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,461,483✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
1,828✔
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,459,690✔
43
  if (r == 0) {  // name exists, check uid and type
4,450,366✔
44
    int64_t uid = *(tb_uid_t *)value;
3,162✔
45
    tdbFree(value);
3,162✔
46

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

54
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
326✔
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) {
326✔
62
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
326✔
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,447,204✔
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,454,507✔
79
}
80

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

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

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

117
  return code;
1,320,489✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
856,226✔
127
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1,104✔
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);
855,674✔
133
  if (code) {
852,966✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
709✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
709✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
852,257✔
139
    tdbFreeClear(value);
853,361✔
140

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

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
850,996✔
149
  if (code) {
851,548✔
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) {
851,548✔
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;
851,548✔
161
}
162

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

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

179
  // handle entry
180
  SMetaEntry entry = {
8,896,511✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,452,015✔
184
      .name = pReq->name,
4,450,690✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,440,093✔
188
  };
189
  if (pReq->rollup) {
4,441,096✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
4,455,282✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,450,036✔
195
    entry.colCmpr = pReq->colCmpr;
4,450,036✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
4,427,102✔
199

200
  if (pReq->virtualStb) {
4,446,095✔
201
    TABLE_SET_VIRTUAL(entry.flags);
38,206✔
202
  }
203

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

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
856,226✔
221
  if (code) {
852,862✔
222
    TAOS_RETURN(code);
1,418✔
223
  }
224

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

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
50,576,743✔
252
      pReq->ctb.suid == 0) {
50,569,759✔
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
3,935✔
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) {
50,575,239✔
260
    pReq->uid = *(int64_t *)value;
305,146✔
261
    tdbFreeClear(value);
305,146✔
262

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

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

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
50,265,179✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
50,265,540✔
292
  if (code) {
50,254,917✔
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) {
50,254,917✔
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) {
50,252,668✔
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;
50,251,774✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
50,264,278✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
50,264,249✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
229,874,938✔
319
      STagVal tagVal = {
179,860,605✔
320
          .cid = pTagSchema->pSchema[i].colId,
179,848,228✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
179,857,268✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
139,468,978✔
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);
50,242,297✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
50,244,405✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
50,257,523✔
339
    if (TSDB_CODE_SUCCESS != code) {
50,234,273✔
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;
50,238,637✔
345
}
346

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

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

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
50,099,309✔
355
  if (NULL == ppRsp) {
50,088,743✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
50,088,743✔
360
  (*ppRsp)->tuid = pEntry->uid;
50,104,970✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
50,109,163✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
50,120,078✔
363

364
  return code;
50,119,209✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
50,431,279✔
372
  if (code) {
50,401,775✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
305,106✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,707✔
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
305,064✔
378
  }
379

380
  SMetaEntry entry = {
50,096,669✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
50,095,126✔
384
      .name = pReq->name,
50,097,206✔
385
      .ctbEntry.btime = pReq->btime,
50,085,052✔
386
      .ctbEntry.ttlDays = pReq->ttl,
50,077,668✔
387
      .ctbEntry.commentLen = pReq->commentLen,
50,105,387✔
388
      .ctbEntry.comment = pReq->comment,
50,070,841✔
389
      .ctbEntry.suid = pReq->ctb.suid,
50,081,255✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
50,084,160✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
50,108,037✔
395
  if (code) {
50,119,531✔
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);
50,119,531✔
402
  if (TSDB_CODE_SUCCESS == code) {
50,120,981✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
50,124,142✔
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;
50,131,459✔
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) {
3,724,277✔
419
  int32_t code = 0;
3,724,277✔
420
  void   *value = NULL;
3,724,277✔
421
  int32_t valueSize = 0;
3,724,277✔
422

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

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

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

449
  if (NULL == ppRsp) {
3,609,926✔
450
    return code;
×
451
  }
452

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

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
41,355,489✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
37,745,563✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
37,745,563✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
37,745,563✔
468
    if (pEntry->pExtSchemas) {
37,745,563✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
415,136✔
470
    }
471
  }
472

473
  return code;
3,609,926✔
474
}
475

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

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

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

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

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

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

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

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

543
  return code;
94,431✔
544
}
545

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

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

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

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

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

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

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

610
  return code;
145,488✔
611
}
612

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

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

643
  // handle entry
644
  code = metaHandleEntry2(pMeta, &entry);
145,488✔
645
  if (TSDB_CODE_SUCCESS == code) {
145,488✔
646
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
145,488✔
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);
145,488✔
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) {
54,299,735✔
663
  int32_t code = TSDB_CODE_SUCCESS;
54,299,735✔
664
  if (TSDB_CHILD_TABLE == pReq->type) {
54,299,735✔
665
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
50,430,391✔
666
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
3,869,765✔
667
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
3,629,846✔
668
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
239,919✔
669
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
94,431✔
670
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
145,488✔
671
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
145,488✔
672
  } else {
673
    code = TSDB_CODE_INVALID_MSG;
×
674
  }
675
  TAOS_RETURN(code);
54,304,745✔
676
}
677

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

681
  // check request
682
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,320,489✔
683
  if (code) {
1,320,489✔
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,320,489✔
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,320,489✔
699
      .version = version,
700
      .uid = pReq->uid,
1,320,489✔
701
  };
702

703
  if (pReq->isVirtual) {
1,320,489✔
704
    if (pReq->suid == 0) {
52,106✔
705
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,269✔
706
    } else {
707
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
25,837✔
708
    }
709
  } else {
710
    if (pReq->suid == 0) {
1,268,383✔
711
      entry.type = -TSDB_NORMAL_TABLE;
729,633✔
712
    } else {
713
      entry.type = -TSDB_CHILD_TABLE;
538,750✔
714
    }
715
  }
716
  code = metaHandleEntry2(pMeta, &entry);
1,320,489✔
717
  if (code) {
1,320,489✔
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,320,489✔
722
             pReq->uid, version);
723
  }
724
  TAOS_RETURN(code);
1,320,489✔
725
}
726

727
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,054,911✔
728
  int32_t code = 0;
4,054,911✔
729

730
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,054,911✔
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;
4,054,911✔
738
  int32_t valueSize = 0;
4,054,911✔
739
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,054,911✔
740
  if (code) {
4,054,911✔
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;
4,054,911✔
747
  tdbFreeClear(value);
4,054,911✔
748

749
  // check table type
750
  SMetaInfo info;
4,054,911✔
751
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,054,911✔
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 &&
4,054,911✔
759
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
26,346✔
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);
4,054,911✔
768
  if (code) {
4,054,911✔
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);
4,054,911✔
774
}
775

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

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

785
  // fetch old entry
786
  SMetaEntry *pEntry = NULL;
3,360,048✔
787
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,360,048✔
788
  if (code) {
3,360,048✔
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,360,048✔
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,360,048✔
802
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,360,048✔
803
  SSchema        *pColumn;
804
  SExtSchema      extSchema = {0};
3,360,048✔
805
  pEntry->version = version;
3,360,048✔
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,
301,455✔
810
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
811
      metaFetchEntryFree(&pEntry);
301,455✔
812
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
301,455✔
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;
3,058,593✔
818
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,058,593✔
819
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,090✔
820
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
821
    metaFetchEntryFree(&pEntry);
6,090✔
822
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,090✔
823
  }
824

825
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,052,503✔
826
  if (pSchema->nCols + 1 > maxCols) {
3,052,503✔
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));
3,052,503✔
834
  if (NULL == pNewSchema) {
3,052,503✔
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;
3,052,503✔
841
  pSchema->version++;
3,052,503✔
842
  pSchema->nCols++;
3,052,503✔
843
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,052,503✔
844
  pColumn->bytes = pReq->bytes;
3,052,503✔
845
  pColumn->type = pReq->type;
3,052,503✔
846
  pColumn->flags = pReq->flags;
3,052,503✔
847
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,052,503✔
848
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
×
849
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
850
              version);
851
    metaFetchEntryFree(&pEntry);
×
852
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
×
853
  }
854
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,052,503✔
855
  extSchema.typeMod = pReq->typeMod;
3,052,503✔
856
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,052,503✔
857
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,052,503✔
858
    SColRef tmpRef;
48,741✔
859
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
48,741✔
860
      tmpRef.hasRef = false;
28,644✔
861
      tmpRef.id = pColumn->colId;
28,644✔
862
    } else {
863
      tmpRef.hasRef = true;
20,097✔
864
      tmpRef.id = pColumn->colId;
20,097✔
865
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
20,097✔
866
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
20,097✔
867
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
20,097✔
868
    }
869
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
48,741✔
870
    if (code) {
48,741✔
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) {
3,003,762✔
879
      compress = createDefaultColCmprByType(pColumn->type);
2,997,109✔
880
    } else {
881
      compress = pReq->compress;
6,653✔
882
    }
883
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
3,003,762✔
884
    if (code) {
3,003,762✔
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);
3,052,503✔
892
  if (code) {
3,052,503✔
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);
3,052,503✔
901
  if (code) {
3,052,503✔
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,
3,052,503✔
908
             pEntry->uid, version);
909
  }
910

911
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,052,503✔
912
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
48,741✔
913
    if (code) {
48,741✔
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++) {
371,982✔
918
        SColRef *p = &pEntry->colRef.pColRef[i];
323,241✔
919
        pRsp->pColRefs[i].hasRef = p->hasRef;
323,241✔
920
        pRsp->pColRefs[i].id = p->id;
323,241✔
921
        if (p->hasRef) {
323,241✔
922
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
158,751✔
923
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
158,751✔
924
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
158,751✔
925
        }
926
      }
927
    }
928
  } else {
929
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
3,003,762✔
930
    if (code) {
3,003,762✔
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);
3,052,503✔
943
  TAOS_RETURN(code);
3,052,503✔
944
}
945

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

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

955
  // fetch old entry
956
  SMetaEntry *pEntry = NULL;
72,744✔
957
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
72,744✔
958
  if (code) {
72,744✔
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) {
72,744✔
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;
72,744✔
973
  SSchema        *pColumn = NULL;
72,744✔
974
  SSchema         tColumn;
72,744✔
975
  int32_t         iColumn = 0;
72,744✔
976
  for (; iColumn < pSchema->nCols; iColumn++) {
9,574,448✔
977
    pColumn = &pSchema->pSchema[iColumn];
9,574,448✔
978
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
9,574,448✔
979
      break;
72,744✔
980
    }
981
  }
982

983
  if (iColumn == pSchema->nCols) {
72,744✔
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) {
72,744✔
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;
72,744✔
998

999
  // do drop column
1000
  pEntry->version = version;
72,744✔
1001
  if (pSchema->nCols - iColumn - 1 > 0) {
72,744✔
1002
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
52,703✔
1003
  }
1004
  pSchema->nCols--;
72,744✔
1005
  pSchema->version++;
72,744✔
1006
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
72,744✔
1007
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
27,108✔
1008
    if (code) {
27,108✔
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) {
27,108✔
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);
45,636✔
1022
    if (code) {
45,636✔
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) {
45,636✔
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);
72,744✔
1038
  if (code) {
72,744✔
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);
72,744✔
1047
  if (code) {
72,744✔
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,
72,744✔
1053
             pEntry->uid, version);
1054
  }
1055

1056
  // build response
1057
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
72,744✔
1058
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
27,108✔
1059
    if (code) {
27,108✔
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++) {
184,287✔
1064
        SColRef *p = &pEntry->colRef.pColRef[i];
157,179✔
1065
        pRsp->pColRefs[i].hasRef = p->hasRef;
157,179✔
1066
        pRsp->pColRefs[i].id = p->id;
157,179✔
1067
        if (p->hasRef) {
157,179✔
1068
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
88,248✔
1069
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
88,248✔
1070
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
88,248✔
1071
        }
1072
      }
1073
    }
1074
  } else {
1075
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
45,636✔
1076
    if (code) {
45,636✔
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++) {
13,139,553✔
1081
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
13,093,917✔
1082
        pRsp->pSchemaExt[i].colId = p->id;
13,093,917✔
1083
        pRsp->pSchemaExt[i].compress = p->alg;
13,093,917✔
1084
      }
1085
    }
1086
  }
1087

1088
  metaFetchEntryFree(&pEntry);
72,744✔
1089
  TAOS_RETURN(code);
72,744✔
1090
}
1091

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

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

1101
  if (NULL == pReq->colNewName) {
42,437✔
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;
42,437✔
1109
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
42,437✔
1110
  if (code) {
42,437✔
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) {
42,437✔
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;
42,437✔
1125
  SSchema        *pColumn = NULL;
42,437✔
1126
  int32_t         iColumn = 0;
42,437✔
1127
  for (int32_t i = 0; i < pSchema->nCols; i++) {
194,234✔
1128
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
194,234✔
1129
      pColumn = &pSchema->pSchema[i];
42,437✔
1130
      iColumn = i;
42,437✔
1131
      break;
42,437✔
1132
    }
1133
  }
1134

1135
  if (NULL == pColumn) {
42,437✔
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;
42,437✔
1145
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
42,437✔
1146
  pSchema->version++;
42,437✔
1147

1148
  // do handle entry
1149
  code = metaHandleEntry2(pMeta, pEntry);
42,437✔
1150
  if (code) {
42,437✔
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,
42,437✔
1157
             pEntry->uid, version);
1158
  }
1159

1160
  // build response
1161
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
42,437✔
1162
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,175✔
1163
    if (code) {
26,175✔
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++) {
185,076✔
1168
        SColRef *p = &pEntry->colRef.pColRef[i];
158,901✔
1169
        pRsp->pColRefs[i].hasRef = p->hasRef;
158,901✔
1170
        pRsp->pColRefs[i].id = p->id;
158,901✔
1171
        if (p->hasRef) {
158,901✔
1172
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
98,013✔
1173
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
98,013✔
1174
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
98,013✔
1175
        }
1176
      }
1177
    }
1178
  } else {
1179
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,262✔
1180
    if (code) {
16,262✔
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++) {
172,752✔
1185
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
156,490✔
1186
        pRsp->pSchemaExt[i].colId = p->id;
156,490✔
1187
        pRsp->pSchemaExt[i].compress = p->alg;
156,490✔
1188
      }
1189
    }
1190
  }
1191

1192
  metaFetchEntryFree(&pEntry);
42,437✔
1193
  TAOS_RETURN(code);
42,437✔
1194
}
1195

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

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

1205
  // fetch old entry
1206
  SMetaEntry *pEntry = NULL;
456,649✔
1207
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
456,649✔
1208
  if (code) {
456,649✔
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) {
456,649✔
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;
456,649✔
1223
  SSchema        *pColumn = NULL;
456,649✔
1224
  int32_t         iColumn = 0;
456,649✔
1225
  int32_t         rowSize = 0;
456,649✔
1226
  for (int32_t i = 0; i < pSchema->nCols; i++) {
17,937,608✔
1227
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
17,480,959✔
1228
      pColumn = &pSchema->pSchema[i];
456,649✔
1229
      iColumn = i;
456,649✔
1230
    }
1231
    rowSize += pSchema->pSchema[i].bytes;
17,480,959✔
1232
  }
1233

1234
  if (NULL == pColumn) {
456,649✔
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) {
456,649✔
1242
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
177,828✔
1243
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1244
              version);
1245
    metaFetchEntryFree(&pEntry);
177,828✔
1246
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
177,828✔
1247
  }
1248

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

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

1263
  // do handle entry
1264
  code = metaHandleEntry2(pMeta, pEntry);
236,191✔
1265
  if (code) {
236,191✔
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,
236,191✔
1272
             pEntry->uid, version);
1273
  }
1274

1275
  // build response
1276
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
236,191✔
1277
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
24,969✔
1278
    if (code) {
24,969✔
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++) {
179,046✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
154,077✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
154,077✔
1285
        pRsp->pColRefs[i].id = p->id;
154,077✔
1286
        if (p->hasRef) {
154,077✔
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
74,298✔
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
74,298✔
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
74,298✔
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
211,222✔
1295
    if (code) {
211,222✔
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,499,326✔
1300
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,288,104✔
1301
        pRsp->pSchemaExt[i].colId = p->id;
8,288,104✔
1302
        pRsp->pSchemaExt[i].compress = p->alg;
8,288,104✔
1303
      }
1304
    }
1305
  }
1306

1307
  metaFetchEntryFree(&pEntry);
236,191✔
1308
  TAOS_RETURN(code);
236,191✔
1309
}
1310

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

1314
  // check tag name
1315
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,420,130✔
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,420,130✔
1323
  int32_t valueSize = 0;
7,420,130✔
1324
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,420,130✔
1325
  if (code) {
7,420,130✔
1326
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
245✔
1327
              __FILE__, __LINE__, pReq->tbName, version);
1328
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
245✔
1329
    TAOS_RETURN(code);
245✔
1330
  }
1331
  tdbFreeClear(value);
7,419,885✔
1332

1333
  TAOS_RETURN(code);
7,419,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,323,702✔
1339
  if (isNull) {
7,323,702✔
1340
    if (!tTagGet(pOldTag, &value)) {
44,915✔
1341
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,425✔
1342
      return true;
19,425✔
1343
    }
1344
    return false;
25,490✔
1345
  }
1346
  if (!tTagGet(pOldTag, &value)){
7,278,787✔
1347
    return false;
172,794✔
1348
  }
1349
  if (IS_VAR_DATA_TYPE(value.type)) {
7,105,993✔
1350
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
56,178✔
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,049,815✔
1356
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
86,776✔
1357
      return true;
86,776✔
1358
    }
1359
  }
1360
  return false;
6,983,398✔
1361
}
1362

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

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

1372
  // fetch child entry
1373
  SMetaEntry *pChild = NULL;
7,419,885✔
1374
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,419,885✔
1375
  if (code) {
7,419,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,419,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,419,885✔
1390
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,419,885✔
1391
  if (code) {
7,419,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,419,885✔
1400
  SSchema        *pColumn = NULL;
7,419,885✔
1401
  int32_t         iColumn = 0;
7,419,885✔
1402
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,060,287✔
1403
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,060,287✔
1404
      pColumn = &pTagSchema->pSchema[i];
7,419,885✔
1405
      iColumn = i;
7,419,885✔
1406
      break;
7,419,885✔
1407
    }
1408
  }
1409

1410
  if (NULL == pColumn) {
7,419,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,419,885✔
1420
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,419,885✔
1421
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
114,993✔
1422
    if (NULL == pNewTag) {
114,993✔
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;
114,993✔
1430
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
114,993✔
1431
  } else {
1432
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,304,892✔
1433

1434
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,304,892✔
1435
    if (NULL == pTagArray) {
7,304,892✔
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,512,176✔
1444
      STagVal value = {
8,340,526✔
1445
          .type = pTagSchema->pSchema[i].type,
8,340,526✔
1446
          .cid = pTagSchema->pSchema[i].colId,
8,340,526✔
1447
      };
1448

1449
      if (iColumn == i) {
8,340,526✔
1450
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,304,892✔
1451
          taosArrayDestroy(pTagArray);
133,242✔
1452
          metaFetchEntryFree(&pChild);
133,242✔
1453
          metaFetchEntryFree(&pSuper);
133,242✔
1454
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
133,242✔
1455
        }
1456
        if (pReq->isNull) {
7,171,650✔
1457
          continue;
23,609✔
1458
        }
1459
        if (IS_VAR_DATA_TYPE(value.type)) {
7,148,041✔
1460
          value.pData = pReq->pTagVal;
97,222✔
1461
          value.nData = pReq->nTagVal;
97,222✔
1462
        } else {
1463
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,050,819✔
1464
        }
1465
      } else if (!tTagGet(pOldTag, &value)) {
1,035,634✔
1466
        continue;
282,420✔
1467
      }
1468

1469
      if (NULL == taosArrayPush(pTagArray, &value)) {
7,901,255✔
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,171,650✔
1480
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,171,650✔
1481
    if (code) {
7,171,650✔
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,171,650✔
1490
    taosMemoryFree(pChild->ctbEntry.pTags);
7,171,650✔
1491
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,171,650✔
1492
  }
1493

1494
  // do handle entry
1495
  code = metaHandleEntry2(pMeta, pChild);
7,286,643✔
1496
  if (code) {
7,286,643✔
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,286,643✔
1504
             pChild->uid, version);
1505
  }
1506

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

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

1516
  // check tag name
1517
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,135✔
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,135✔
1525
  int32_t valueSize = 0;
3,135✔
1526
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,135✔
1527
  if (code) {
3,135✔
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,135✔
1534

1535
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,135✔
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,135✔
1542
}
1543

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

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

1552
  // fetch child entry
1553
  SMetaEntry *pChild = NULL;
3,135✔
1554
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,135✔
1555
  if (code) {
3,135✔
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,135✔
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,135✔
1570
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,135✔
1571
  if (code) {
3,135✔
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,135✔
1580

1581
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,135✔
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,135✔
1592
  if (pTagTable == NULL) {
3,135✔
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++) {
21,945✔
1601
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
18,810✔
1602
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
18,810✔
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,135✔
1613
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
25,080✔
1614
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
21,945✔
1615
        ? numOfChangedTags++
18,810✔
1616
        : 0;
21,945✔
1617
  }
1618
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,135✔
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,135✔
1629
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,135✔
1630
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,135✔
1631
  if (NULL == pTagArray) {
3,135✔
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,135✔
1641

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

1648
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
21,945✔
1649
    if (pTagVal == NULL) {
21,945✔
1650
      if (!tTagGet(pOldTag, &value)) {
3,135✔
1651
        continue;
×
1652
      }
1653
    } else {
1654
      value.type = pCol->type;
18,810✔
1655
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
18,810✔
1656
        allSame = false;
10,032✔
1657
      }
1658
      if (pTagVal->isNull) {
18,810✔
1659
        continue;
3,762✔
1660
      }
1661

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

1670
    if (taosArrayPush(pTagArray, &value) == NULL) {
18,183✔
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,135✔
1682
    metaWarn("vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
627✔
1683
             __LINE__, version);
1684
    taosHashCleanup(pTagTable);
627✔
1685
    taosArrayDestroy(pTagArray);
627✔
1686
    metaFetchEntryFree(&pChild);
627✔
1687
    metaFetchEntryFree(&pSuper);
627✔
1688
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
627✔
1689
  } 
1690
  STag *pNewTag = NULL;
2,508✔
1691
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
2,508✔
1692
  if (code) {
2,508✔
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,508✔
1702
  taosMemoryFree(pChild->ctbEntry.pTags);
2,508✔
1703
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
2,508✔
1704

1705
  // do handle entry
1706
  code = metaHandleEntry2(pMeta, pChild);
2,508✔
1707
  if (code) {
2,508✔
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,508✔
1716
             pChild->uid, version);
1717
  }
1718

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

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

1728
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
23,602✔
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;
23,602✔
1735
}
1736

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

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

1745
  // fetch entry
1746
  SMetaEntry *pEntry = NULL;
23,602✔
1747
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,602✔
1748
  if (code) {
23,602✔
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;
23,602✔
1756
  if (pEntry->type == TSDB_CHILD_TABLE) {
23,602✔
1757
    if (pReq->updateTTL) {
11,140✔
1758
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,309✔
1759
    }
1760
    if (pReq->newCommentLen >= 0) {
11,140✔
1761
      char *pNewComment = NULL;
6,831✔
1762
      if (pReq->newCommentLen) {
6,831✔
1763
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,356✔
1764
        if (NULL == pNewComment) {
4,356✔
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,356✔
1771
      } else {
1772
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,475✔
1773
      }
1774
      pEntry->ctbEntry.comment = pNewComment;
6,831✔
1775
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
6,831✔
1776
    }
1777
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
12,462✔
1778
    if (pReq->updateTTL) {
12,462✔
1779
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,435✔
1780
    }
1781
    if (pReq->newCommentLen >= 0) {
12,462✔
1782
      char *pNewComment = NULL;
8,027✔
1783
      if (pReq->newCommentLen > 0) {
8,027✔
1784
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
5,552✔
1785
        if (NULL == pNewComment) {
5,552✔
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,552✔
1792
      } else {
1793
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,475✔
1794
      }
1795
      pEntry->ntbEntry.comment = pNewComment;
8,027✔
1796
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,027✔
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);
23,602✔
1807
  if (code) {
23,602✔
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,
23,602✔
1814
             pEntry->uid, version);
1815
  }
1816

1817
  metaFetchEntryFree(&pEntry);
23,602✔
1818
  TAOS_RETURN(code);
23,602✔
1819
}
1820

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

1824
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5,816✔
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,816✔
1831
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5,816✔
1832
  if (code) {
5,816✔
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,816✔
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,816✔
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,816✔
1854
  SColCmprWrapper *wp = &pEntry->colCmpr;
5,816✔
1855
  for (int32_t i = 0; i < wp->nCols; i++) {
46,528✔
1856
    SColCmpr *p = &wp->pColCmpr[i];
40,712✔
1857
    if (p->id == pReq->colId) {
40,712✔
1858
      uint32_t dst = 0;
5,816✔
1859
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5,816✔
1860
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1861
      if (updated > 0) {
5,816✔
1862
        p->alg = dst;
5,816✔
1863
      }
1864
    }
1865
  }
1866

1867
  if (updated == 0) {
5,816✔
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,816✔
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,816✔
1882

1883
  // do handle entry
1884
  code = metaHandleEntry2(pMeta, pEntry);
5,816✔
1885
  if (code) {
5,816✔
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,816✔
1892
             pEntry->uid, version);
1893
  }
1894

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

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

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

1908
  if (NULL == pReq->refDbName) {
70,725✔
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) {
70,725✔
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) {
70,725✔
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;
70,725✔
1928
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
70,725✔
1929
  if (code) {
70,725✔
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) {
70,725✔
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;
70,725✔
1944
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
70,725✔
1945
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
25,491✔
1946
    if (code) {
25,491✔
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 =
70,725✔
1956
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
70,725✔
1957
  SColRef *pColRef = NULL;
70,725✔
1958
  int32_t  iColumn = 0;
70,725✔
1959
  for (int32_t i = 0; i < pSchema->nCols; i++) {
350,534✔
1960
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
350,534✔
1961
      pColRef = &pEntry->colRef.pColRef[i];
70,725✔
1962
      iColumn = i;
70,725✔
1963
      break;
70,725✔
1964
    }
1965
  }
1966

1967
  if (NULL == pColRef) {
70,725✔
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;
70,725✔
1977
  pColRef->hasRef = true;
70,725✔
1978
  pColRef->id = pSchema->pSchema[iColumn].colId;
70,725✔
1979
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
70,725✔
1980
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
70,725✔
1981
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
70,725✔
1982
  pSchema->version++;
70,725✔
1983
  pEntry->colRef.version++;
70,725✔
1984

1985
  // do handle entry
1986
  code = metaHandleEntry2(pMeta, pEntry);
70,725✔
1987
  if (code) {
70,725✔
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,
70,725✔
1995
             pEntry->uid, version);
1996
  }
1997

1998
  // build response
1999
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
70,725✔
2000
  if (code) {
70,725✔
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++) {
528,577✔
2005
      SColRef *p = &pEntry->colRef.pColRef[i];
457,852✔
2006
      pRsp->pColRefs[i].hasRef = p->hasRef;
457,852✔
2007
      pRsp->pColRefs[i].id = p->id;
457,852✔
2008
      if (p->hasRef) {
457,852✔
2009
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
316,082✔
2010
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
316,082✔
2011
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
316,082✔
2012
      }
2013
    }
2014
  }
2015

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

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

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

2030
  // fetch old entry
2031
  SMetaEntry *pEntry = NULL;
52,308✔
2032
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
52,308✔
2033
  if (code) {
52,308✔
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) {
52,308✔
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;
52,308✔
2048
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
52,308✔
2049
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
26,346✔
2050
    if (code) {
26,346✔
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 =
52,308✔
2060
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
52,308✔
2061
  SColRef *pColRef = NULL;
52,308✔
2062
  int32_t  iColumn = 0;
52,308✔
2063
  for (int32_t i = 0; i < pSchema->nCols; i++) {
212,919✔
2064
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
212,919✔
2065
      pColRef = &pEntry->colRef.pColRef[i];
52,308✔
2066
      iColumn = i;
52,308✔
2067
      break;
52,308✔
2068
    }
2069
  }
2070

2071
  if (NULL == pColRef) {
52,308✔
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;
52,308✔
2081
  pColRef->hasRef = false;
52,308✔
2082
  pColRef->id = pSchema->pSchema[iColumn].colId;
52,308✔
2083
  pSchema->version++;
52,308✔
2084
  pEntry->colRef.version++;
52,308✔
2085

2086
  // do handle entry
2087
  code = metaHandleEntry2(pMeta, pEntry);
52,308✔
2088
  if (code) {
52,308✔
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,
52,308✔
2096
             pEntry->uid, version);
2097
  }
2098

2099
  // build response
2100
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
52,308✔
2101
  if (code) {
52,308✔
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++) {
321,222✔
2106
      SColRef *p = &pEntry->colRef.pColRef[i];
268,914✔
2107
      pRsp->pColRefs[i].hasRef = p->hasRef;
268,914✔
2108
      pRsp->pColRefs[i].id = p->id;
268,914✔
2109
      if (p->hasRef) {
268,914✔
2110
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
133,272✔
2111
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
133,272✔
2112
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
133,272✔
2113
      }
2114
    }
2115
  }
2116

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

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

2125
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,221✔
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,221✔
2132
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
4,221✔
2133
  if (code) {
4,221✔
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,221✔
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,221✔
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,221✔
2165
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
4,221✔
2166
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,221✔
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,221✔
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,221✔
2192
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
21,538✔
2193
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,317✔
2194
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,317✔
2195

2196
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
17,317✔
2197
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
17,317✔
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,317✔
2205
      numOfChangedTags++;
4,221✔
2206
      SSCHMEA_SET_IDX_ON(pOldColumn);
4,221✔
2207
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
13,096✔
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,221✔
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,221✔
2224
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,221✔
2225

2226
  // do handle the entry
2227
  code = metaHandleEntry2(pMeta, pEntry);
4,221✔
2228
  if (code) {
4,221✔
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,221✔
2235
             pEntry->uid, version);
2236
  }
2237

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

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

2245
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,788✔
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,788✔
2252
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,788✔
2253
  if (code) {
2,788✔
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,788✔
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,788✔
2267
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,788✔
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,788✔
2276
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
7,670✔
2277
    SSchema *pCol = pTagSchema->pSchema + i;
7,670✔
2278
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
7,670✔
2279
      if (!IS_IDX_ON(pCol)) {
2,788✔
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,788✔
2286
      SSCHMEA_SET_IDX_OFF(pCol);
2,788✔
2287
      break;
2,788✔
2288
    }
2289
  }
2290

2291
  if (numOfChangedTags != 1) {
2,788✔
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,788✔
2300
  pTagSchema->version++;
2,788✔
2301
  code = metaHandleEntry2(pMeta, pEntry);
2,788✔
2302
  if (code) {
2,788✔
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,788✔
2309
             pEntry->uid, version);
2310
  }
2311

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

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

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

2325
  SMetaEntry *pEntry = NULL;
7,121,997✔
2326
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,131,605✔
2327
  if (code) {
7,129,165✔
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,129,165✔
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,340,660✔
2341
      .version = version,
2342
      .type = TSDB_SUPER_TABLE,
2343
      .uid = pReq->suid,
7,127,204✔
2344
      .name = pReq->name,
7,124,004✔
2345
      .stbEntry.schemaRow = pReq->schemaRow,
2346
      .stbEntry.schemaTag = pReq->schemaTag,
2347
      .stbEntry.keep = pReq->keep,
7,126,104✔
2348
      .colCmpr = pReq->colCmpr,
2349
      .pExtSchemas = pReq->pExtSchemas,
7,108,281✔
2350
  };
2351
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,113,729✔
2352
  if (pReq->virtualStb) {
7,113,729✔
2353
    TABLE_SET_VIRTUAL(entry.flags);
15,654✔
2354
  }
2355
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,125,155✔
2356
    TABLE_SET_ROLLUP(entry.flags);
4,254✔
2357
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,254✔
2358
  }
2359

2360
  // do handle the entry
2361
  code = metaHandleEntry2(pMeta, &entry);
7,126,707✔
2362
  if (code) {
7,103,402✔
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,103,402✔
2369
             pReq->suid, version);
2370
  }
2371

2372
  metaFetchEntryFree(&pEntry);
7,128,503✔
2373
  TAOS_RETURN(code);
7,139,917✔
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,270✔
2416
  int32_t code = TSDB_CODE_SUCCESS;
21,270✔
2417

2418
  if (NULL == pReq->name || pReq->name[0] == 0) {
21,270✔
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,270✔
2425
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
21,270✔
2426
  if (code) {
21,270✔
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,270✔
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,270✔
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,270✔
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,270✔
2453
  }
2454

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

2466
  // do handle the entry
2467
  code = metaHandleEntry2(pMeta, &entry);
21,270✔
2468
  if (code) {
21,270✔
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,270✔
2475
    pMeta->pVnode->config.isRsma = 1;
21,270✔
2476
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
21,270✔
2477
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2478
  }
2479

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

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

2487
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,254✔
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,254✔
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,254✔
2500
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,254✔
2501
  if (code) {
4,254✔
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,254✔
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,254✔
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,254✔
2523
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,254✔
2524
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,254✔
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,254✔
2534
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,254✔
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,254✔
2543
  entry.version = version;
4,254✔
2544
  TABLE_RESET_ROLLUP(entry.flags);
4,254✔
2545
  entry.stbEntry.rsmaParam.uid = 0;
4,254✔
2546
  entry.stbEntry.rsmaParam.name = NULL;
4,254✔
2547
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,254✔
2548
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,254✔
2549
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,254✔
2550

2551
  // do handle the entry
2552
  code = metaHandleEntry2(pMeta, &entry);
4,254✔
2553
  if (code) {
4,254✔
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,254✔
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,254✔
2563
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2564
  }
2565

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

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

2573
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,344✔
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,344✔
2580
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,344✔
2581
  if (code) {
11,344✔
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,344✔
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,344✔
2596
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
11,344✔
2597
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,053✔
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,344✔
2606
  entry.version = version;
11,344✔
2607
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
11,344✔
2608
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
10,635✔
2609
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
11,344✔
2610
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
10,635✔
2611
  }
2612
  // do handle the entry
2613
  code = metaHandleEntry2(pMeta, &entry);
12,053✔
2614
  if (code) {
11,344✔
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,344✔
2621
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2622
  }
2623

2624
  metaFetchEntryFree(&pEntry);
11,344✔
2625
  TAOS_RETURN(code);
11,344✔
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