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

taosdata / TDengine / #4837

07 Nov 2025 09:40AM UTC coverage: 58.963% (+0.2%) from 58.728%
#4837

push

travis-ci

DuanKuanJun
coverity: cases_other.task add -R -Q2 -Q3 -Q4

150245 of 324452 branches covered (46.31%)

Branch coverage included in aggregate %.

200054 of 269646 relevant lines covered (74.19%)

317833830.25 hits per line

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

55.5
/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) {
79,158,123✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
79,158,123✔
31
  void     *value = NULL;
79,186,338✔
32
  int32_t   valueSize = 0;
79,200,537✔
33
  SMetaInfo info;
79,194,591✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
79,169,909✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
141,007!
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);
79,016,083!
43
  if (r == 0) {  // name exists, check uid and type
79,019,332✔
44
    int64_t uid = *(tb_uid_t *)value;
13,826✔
45
    tdbFree(value);
13,826✔
46

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

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

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

71
  // check suid
72
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
79,005,506!
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;
79,094,636✔
79
}
80

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

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

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

117
  return code;
13,440,762✔
118
}
119

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

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

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

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

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
7,433,553✔
149
  if (code) {
7,452,363!
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) {
7,452,363!
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;
7,452,363✔
161
}
162

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

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

179
  // handle entry
180
  SMetaEntry entry = {
158,081,980✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
79,085,632✔
184
      .name = pReq->name,
79,040,359✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
79,094,451✔
188
  };
189
  if (pReq->rollup) {
78,944,967!
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
78,935,063✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
79,064,733✔
195
    entry.colCmpr = pReq->colCmpr;
79,064,733✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
78,962,173✔
199

200
  if (pReq->virtualStb) {
78,990,539✔
201
    TABLE_SET_VIRTUAL(entry.flags);
427,418✔
202
  }
203

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

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
7,476,586✔
221
  if (code) {
7,459,874✔
222
    TAOS_RETURN(code);
10,802✔
223
  }
224

225
  // handle entry
226
  SMetaEntry entry = {
7,449,072✔
227
      .version = verison,
228
      .type = -TSDB_SUPER_TABLE,
229
      .uid = pReq->suid,
7,430,635✔
230
  };
231
  code = metaHandleEntry2(pMeta, &entry);
7,452,962✔
232
  if (code) {
7,464,079!
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,
7,464,079!
237
             pReq->suid, verison);
238
  }
239
  TAOS_RETURN(code);
7,465,784✔
240
}
241

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
653,884,749!
252
      pReq->ctb.suid == 0) {
653,952,191✔
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
23,937!
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) {
653,831,982✔
260
    pReq->uid = *(int64_t *)value;
8,791,141✔
261
    tdbFreeClear(value);
8,789,847✔
262

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

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

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
645,101,602✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
645,088,444✔
292
  if (code) {
645,052,736!
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) {
645,052,736!
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) {
645,027,927!
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;
644,951,046✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
645,080,621✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
645,019,346✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
2,147,483,647✔
319
      STagVal tagVal = {
2,147,483,647✔
320
          .cid = pTagSchema->pSchema[i].colId,
2,147,483,647✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
2,147,483,647✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
1,758,989,436!
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);
645,134,390✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
644,915,550!
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
645,067,395✔
339
    if (TSDB_CODE_SUCCESS != code) {
644,926,321!
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;
644,984,348✔
345
}
346

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

350
  if (NULL == ppRsp) {
643,387,602!
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
643,387,602!
355
  if (NULL == ppRsp) {
643,274,512!
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
643,274,512✔
360
  (*ppRsp)->tuid = pEntry->uid;
643,374,807✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
643,474,996✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
643,439,681!
363

364
  return code;
643,484,767✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
652,378,364✔
372
  if (code) {
652,295,518✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
8,789,847✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
12,463!
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
8,788,553✔
378
  }
379

380
  SMetaEntry entry = {
643,505,671✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
643,453,787✔
384
      .name = pReq->name,
643,521,125✔
385
      .ctbEntry.btime = pReq->btime,
643,560,423✔
386
      .ctbEntry.ttlDays = pReq->ttl,
643,475,491✔
387
      .ctbEntry.commentLen = pReq->commentLen,
643,279,395✔
388
      .ctbEntry.comment = pReq->comment,
643,422,726✔
389
      .ctbEntry.suid = pReq->ctb.suid,
643,507,159✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
643,302,593✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
643,253,056✔
395
  if (code) {
643,493,735!
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);
643,493,735✔
402
  if (TSDB_CODE_SUCCESS == code) {
643,614,914✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
643,618,447!
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,
190!
407
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
408
              pReq->ctb.suid, version);
409
  }
410
  return code;
643,682,554✔
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) {
37,723,346✔
419
  int32_t code = 0;
37,723,346✔
420
  void   *value = NULL;
37,723,346✔
421
  int32_t valueSize = 0;
37,723,346✔
422

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

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

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

449
  if (NULL == ppRsp) {
36,401,756!
450
    return code;
×
451
  }
452

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

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
464,515,163✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
428,113,407✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
428,113,407✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
428,113,407✔
468
    if (pEntry->pExtSchemas) {
428,113,407✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
7,135,300✔
470
    }
471
  }
472

473
  return code;
36,401,756✔
474
}
475

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

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

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

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

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

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

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

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

543
  return code;
770,042✔
544
}
545

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

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

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

590
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
1,500,749✔
591
  int32_t code = TSDB_CODE_SUCCESS;
1,500,749✔
592

593
  if (NULL == ppRsp) {
1,500,749!
594
    return code;
×
595
  }
596

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

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

609
  return code;
1,500,749✔
610
}
611

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

623
  SMetaEntry entry = {.version = version,
3,001,498✔
624
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
625
                      .uid = pReq->uid,
1,500,749✔
626
                      .name = pReq->name,
1,500,749✔
627
                      .ctbEntry.btime = pReq->btime,
1,500,749✔
628
                      .ctbEntry.ttlDays = pReq->ttl,
1,500,749✔
629
                      .ctbEntry.commentLen = pReq->commentLen,
1,500,749✔
630
                      .ctbEntry.comment = pReq->comment,
1,500,749✔
631
                      .ctbEntry.suid = pReq->ctb.suid,
1,500,749✔
632
                      .ctbEntry.pTags = pReq->ctb.pTag,
1,500,749✔
633
                      .colRef = pReq->colRef};
634

635
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
1,500,749✔
636
  if (code) {
1,500,749!
637
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
638
              tstrerror(code));
639
  }
640

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

656
// Drop Normal Table
657

658
// Alter Normal Table
659

660
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
691,618,193✔
661
  int32_t code = TSDB_CODE_SUCCESS;
691,618,193✔
662
  if (TSDB_CHILD_TABLE == pReq->type) {
691,618,193✔
663
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
652,428,926✔
664
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
39,224,095✔
665
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
36,953,304✔
666
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
2,270,791✔
667
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
770,042✔
668
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
1,500,749!
669
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
1,500,749✔
670
  } else {
671
    code = TSDB_CODE_INVALID_MSG;
×
672
  }
673
  TAOS_RETURN(code);
691,691,998✔
674
}
675

676
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
13,440,762✔
677
  int32_t code = TSDB_CODE_SUCCESS;
13,440,762✔
678

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

689
  if (pReq->suid == pReq->uid) {
13,440,762!
690
    code = TSDB_CODE_INVALID_PARA;
×
691
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
692
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
693
    TAOS_RETURN(code);
×
694
  }
695

696
  SMetaEntry entry = {
13,440,762✔
697
      .version = version,
698
      .uid = pReq->uid,
13,440,762✔
699
  };
700

701
  if (pReq->isVirtual) {
13,440,762✔
702
    if (pReq->suid == 0) {
362,869✔
703
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
181,742✔
704
    } else {
705
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
181,127✔
706
    }
707
  } else {
708
    if (pReq->suid == 0) {
13,077,893✔
709
      entry.type = -TSDB_NORMAL_TABLE;
7,874,673✔
710
    } else {
711
      entry.type = -TSDB_CHILD_TABLE;
5,203,220✔
712
    }
713
  }
714
  code = metaHandleEntry2(pMeta, &entry);
13,440,762✔
715
  if (code) {
13,440,762!
716
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
717
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
718
  } else {
719
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
13,440,762!
720
             pReq->uid, version);
721
  }
722
  TAOS_RETURN(code);
13,440,762✔
723
}
724

725
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
27,256,962✔
726
  int32_t code = 0;
27,256,962✔
727

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

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

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

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

774
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
22,210,563✔
775
  int32_t code = TSDB_CODE_SUCCESS;
22,210,563✔
776

777
  // check request
778
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
22,210,563✔
779
  if (code) {
22,210,563!
780
    TAOS_RETURN(code);
×
781
  }
782

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

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

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

822
  if (pSchema->nCols + 1 > TSDB_MAX_COLUMNS) {
20,124,091!
823
    metaError("vgId:%d, %s failed at %s:%d since column count %d + 1 > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
824
              __func__, __FILE__, __LINE__, pSchema->nCols, TSDB_MAX_COLUMNS, version);
825
    metaFetchEntryFree(&pEntry);
×
826
    TAOS_RETURN(TSDB_CODE_PAR_TOO_MANY_COLUMNS);
×
827
  }
828

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

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

900
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
20,124,091✔
901
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
334,318!
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
    } else {
905
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
2,557,227✔
906
        SColRef *p = &pEntry->colRef.pColRef[i];
2,222,909✔
907
        pRsp->pColRefs[i].hasRef = p->hasRef;
2,222,909!
908
        pRsp->pColRefs[i].id = p->id;
2,222,909✔
909
        if (p->hasRef) {
2,222,909!
910
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
1,092,729!
911
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
1,092,729!
912
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
1,092,729!
913
        }
914
      }
915
    }
916
  } else {
917
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
19,789,773!
918
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
919
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
920
    } else {
921
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
922
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
923
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
924
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
925
      }
926
    }
927
  }
928

929
  metaFetchEntryFree(&pEntry);
20,124,091✔
930
  TAOS_RETURN(code);
20,124,091✔
931
}
932

933
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
670,772✔
934
  int32_t code = TSDB_CODE_SUCCESS;
670,772✔
935

936
  // check request
937
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
670,772✔
938
  if (code) {
670,772!
939
    TAOS_RETURN(code);
×
940
  }
941

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

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

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

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

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

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

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

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

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

1049
  // build response
1050
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
637,453✔
1051
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
189,143!
1052
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1053
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1054
    } else {
1055
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
1,279,874✔
1056
        SColRef *p = &pEntry->colRef.pColRef[i];
1,090,731✔
1057
        pRsp->pColRefs[i].hasRef = p->hasRef;
1,090,731!
1058
        pRsp->pColRefs[i].id = p->id;
1,090,731✔
1059
        if (p->hasRef) {
1,090,731!
1060
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
609,785!
1061
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
609,785!
1062
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
609,785!
1063
        }
1064
      }
1065
    }
1066
  } else {
1067
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
448,310!
1068
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1069
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1070
    } else {
1071
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
412,430,553✔
1072
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
411,982,243✔
1073
        pRsp->pSchemaExt[i].colId = p->id;
411,982,243✔
1074
        pRsp->pSchemaExt[i].compress = p->alg;
411,982,243✔
1075
      }
1076
    }
1077
  }
1078

1079
  metaFetchEntryFree(&pEntry);
637,453✔
1080
  TAOS_RETURN(code);
637,453✔
1081
}
1082

1083
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
386,111✔
1084
  int32_t code = TSDB_CODE_SUCCESS;
386,111✔
1085

1086
  // check request
1087
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
386,111✔
1088
  if (code) {
386,111!
1089
    TAOS_RETURN(code);
×
1090
  }
1091

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

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

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

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

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

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

1140
  // do update column name
1141
  pEntry->version = version;
352,792✔
1142
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
352,792!
1143
  pSchema->version++;
352,792✔
1144

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

1157
  // build response
1158
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
352,792✔
1159
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
191,363!
1160
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1161
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1162
    } else {
1163
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
1,325,042✔
1164
        SColRef *p = &pEntry->colRef.pColRef[i];
1,133,679✔
1165
        pRsp->pColRefs[i].hasRef = p->hasRef;
1,133,679!
1166
        pRsp->pColRefs[i].id = p->id;
1,133,679✔
1167
        if (p->hasRef) {
1,133,679!
1168
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
706,505!
1169
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
706,505!
1170
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
706,505!
1171
        }
1172
      }
1173
    }
1174
  } else {
1175
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
161,429!
1176
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1177
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1178
    } else {
1179
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
1,441,611✔
1180
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
1,280,182✔
1181
        pRsp->pSchemaExt[i].colId = p->id;
1,280,182✔
1182
        pRsp->pSchemaExt[i].compress = p->alg;
1,280,182✔
1183
      }
1184
    }
1185
  }
1186

1187
  metaFetchEntryFree(&pEntry);
352,792✔
1188
  TAOS_RETURN(code);
352,792✔
1189
}
1190

1191
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,103,841✔
1192
  int32_t code = TSDB_CODE_SUCCESS;
3,103,841✔
1193

1194
  // check request
1195
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,103,841✔
1196
  if (code) {
3,103,841!
1197
    TAOS_RETURN(code);
×
1198
  }
1199

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

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

1216
  // search the column to update
1217
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,103,841✔
1218
  SSchema        *pColumn = NULL;
3,103,841✔
1219
  int32_t         iColumn = 0;
3,103,841✔
1220
  int32_t         rowSize = 0;
3,103,841✔
1221
  for (int32_t i = 0; i < pSchema->nCols; i++) {
119,933,392✔
1222
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
116,829,551!
1223
      pColumn = &pSchema->pSchema[i];
3,103,841✔
1224
      iColumn = i;
3,103,841✔
1225
    }
1226
    rowSize += pSchema->pSchema[i].bytes;
116,829,551✔
1227
  }
1228

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

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

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

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

1259
  // do change the column bytes
1260
  pEntry->version = version;
1,621,043✔
1261
  pSchema->version++;
1,621,043✔
1262
  pColumn->bytes = pReq->colModBytes;
1,621,043✔
1263

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

1276
  // build response
1277
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
1,621,043✔
1278
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
169,619!
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++) {
1,216,322✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
1,046,703✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
1,046,703!
1285
        pRsp->pColRefs[i].id = p->id;
1,046,703✔
1286
        if (p->hasRef) {
1,046,703!
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
504,750!
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
504,750!
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
504,750!
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
1,451,424!
1295
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1296
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1297
    } else {
1298
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
57,855,956✔
1299
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
56,404,532✔
1300
        pRsp->pSchemaExt[i].colId = p->id;
56,404,532✔
1301
        pRsp->pSchemaExt[i].compress = p->alg;
56,404,532✔
1302
      }
1303
    }
1304
  }
1305

1306
  metaFetchEntryFree(&pEntry);
1,621,043✔
1307
  TAOS_RETURN(code);
1,621,043✔
1308
}
1309

1310
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
46,497,960✔
1311
  int32_t code = 0;
46,497,960✔
1312

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

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

1332
  TAOS_RETURN(code);
46,495,089✔
1333
}
1334

1335
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
46,497,960✔
1336
  int32_t code = TSDB_CODE_SUCCESS;
46,497,960✔
1337

1338
  // check request
1339
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
46,497,960✔
1340
  if (code) {
46,497,960✔
1341
    TAOS_RETURN(code);
2,871✔
1342
  }
1343

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

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

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

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

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

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

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

1415
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
98,489,841✔
1416
      STagVal value = {
52,812,446✔
1417
          .type = pTagSchema->pSchema[i].type,
52,812,446✔
1418
          .cid = pTagSchema->pSchema[i].colId,
52,812,446✔
1419
      };
1420

1421
      if (iColumn == i) {
52,812,446✔
1422
        if (pReq->isNull) {
45,677,395✔
1423
          continue;
259,330✔
1424
        }
1425
        if (IS_VAR_DATA_TYPE(value.type)) {
45,418,065!
1426
          value.pData = pReq->pTagVal;
951,347✔
1427
          value.nData = pReq->nTagVal;
951,347✔
1428
        } else {
1429
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
44,466,718!
1430
        }
1431
      } else if (!tTagGet(pOldTag, &value)) {
7,135,051✔
1432
        continue;
1,952,608✔
1433
      }
1434

1435
      if (NULL == taosArrayPush(pTagArray, &value)) {
50,600,508!
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
        taosArrayDestroy(pTagArray);
×
1439
        metaFetchEntryFree(&pChild);
×
1440
        metaFetchEntryFree(&pSuper);
×
1441
        TAOS_RETURN(terrno);
×
1442
      }
1443
    }
1444

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

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

1473
  // free resource and return
1474
  metaFetchEntryFree(&pChild);
46,495,089✔
1475
  metaFetchEntryFree(&pSuper);
46,495,089✔
1476
  TAOS_RETURN(code);
46,495,089✔
1477
}
1478

1479
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
20,845✔
1480
  int32_t code = 0;
20,845✔
1481

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

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

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

1507
  TAOS_RETURN(code);
20,845✔
1508
}
1509

1510
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
20,845✔
1511
  int32_t code = TSDB_CODE_SUCCESS;
20,845✔
1512

1513
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
20,845✔
1514
  if (code) {
20,845!
1515
    TAOS_RETURN(code);
×
1516
  }
1517

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

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

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

1544
  // search the tags to update
1545
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
20,845✔
1546

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

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

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

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

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

1606
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
166,760✔
1607
    SSchema *pCol = &pTagSchema->pSchema[i];
145,915✔
1608
    STagVal  value = {
145,915✔
1609
         .cid = pCol->colId,
145,915✔
1610
    };
1611

1612
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
145,915!
1613
    if (pTagVal == NULL) {
145,915✔
1614
      if (!tTagGet(pOldTag, &value)) {
20,845!
1615
        continue;
×
1616
      }
1617
    } else {
1618
      value.type = pCol->type;
125,070✔
1619
      if (pTagVal->isNull) {
125,070✔
1620
        continue;
25,014✔
1621
      }
1622

1623
      if (IS_VAR_DATA_TYPE(pCol->type)) {
100,056!
1624
        value.pData = pTagVal->pTagVal;
16,676✔
1625
        value.nData = pTagVal->nTagVal;
16,676✔
1626
      } else {
1627
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
83,380!
1628
      }
1629
    }
1630

1631
    if (taosArrayPush(pTagArray, &value) == NULL) {
120,901!
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
      taosArrayDestroy(pTagArray);
×
1636
      metaFetchEntryFree(&pChild);
×
1637
      metaFetchEntryFree(&pSuper);
×
1638
      TAOS_RETURN(terrno);
×
1639
    }
1640
  }
1641

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

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

1671
  taosHashCleanup(pTagTable);
20,845✔
1672
  metaFetchEntryFree(&pChild);
20,845✔
1673
  metaFetchEntryFree(&pSuper);
20,845✔
1674
  TAOS_RETURN(code);
20,845✔
1675
}
1676

1677
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
186,146✔
1678
  int32_t code = TSDB_CODE_SUCCESS;
186,146✔
1679

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

1686
  return code;
186,146✔
1687
}
1688

1689
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
186,146✔
1690
  int32_t code = 0;
186,146✔
1691

1692
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
186,146✔
1693
  if (code) {
186,146!
1694
    TAOS_RETURN(code);
×
1695
  }
1696

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

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

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

1769
  metaFetchEntryFree(&pEntry);
186,146✔
1770
  TAOS_RETURN(code);
186,146✔
1771
}
1772

1773
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
44,366✔
1774
  int32_t code = TSDB_CODE_SUCCESS;
44,366✔
1775

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

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

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

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

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

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

1833
  pEntry->version = version;
44,366✔
1834

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

1847
  metaFetchEntryFree(&pEntry);
44,366✔
1848
  TAOS_RETURN(code);
44,366✔
1849
}
1850

1851
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
516,509✔
1852
  int32_t code = TSDB_CODE_SUCCESS;
516,509✔
1853

1854
  // check request
1855
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
516,509✔
1856
  if (code) {
516,509!
1857
    TAOS_RETURN(code);
×
1858
  }
1859

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

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

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

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

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

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

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

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

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

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

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

1967
  metaFetchEntryFree(&pEntry);
516,509✔
1968
  metaFetchEntryFree(&pSuper);
516,509✔
1969
  TAOS_RETURN(code);
516,509✔
1970
}
1971

1972
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
369,166✔
1973
  int32_t code = TSDB_CODE_SUCCESS;
369,166✔
1974

1975
  // check request
1976
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
369,166✔
1977
  if (code) {
369,166!
1978
    TAOS_RETURN(code);
×
1979
  }
1980

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

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

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

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

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

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

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

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

2067
  metaFetchEntryFree(&pEntry);
369,166✔
2068
  metaFetchEntryFree(&pSuper);
369,166✔
2069
  TAOS_RETURN(code);
369,166✔
2070
}
2071

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

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

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

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

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

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

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

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

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

2141
  int32_t numOfChangedTags = 0;
81,894✔
2142
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
392,832✔
2143
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
310,938✔
2144
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
310,938✔
2145

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

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

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

2173
  pEntry->version = version;
81,894✔
2174
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
81,894✔
2175

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

2188
  metaFetchEntryFree(&pEntry);
81,894✔
2189
  TAOS_RETURN(code);
81,894✔
2190
}
2191

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

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

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

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

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

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

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

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

2262
  metaFetchEntryFree(&pEntry);
47,210✔
2263
  TAOS_RETURN(code);
47,210✔
2264
}
2265

2266
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
62,100,844✔
2267
  int32_t code = TSDB_CODE_SUCCESS;
62,100,844✔
2268

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

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

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

2290
  SMetaEntry entry = {
185,996,111✔
2291
      .version = version,
2292
      .type = TSDB_SUPER_TABLE,
2293
      .uid = pReq->suid,
62,054,654✔
2294
      .name = pReq->name,
62,005,340✔
2295
      .stbEntry.schemaRow = pReq->schemaRow,
2296
      .stbEntry.schemaTag = pReq->schemaTag,
2297
      .stbEntry.keep = pReq->keep,
61,951,712✔
2298
      .colCmpr = pReq->colCmpr,
2299
      .pExtSchemas = pReq->pExtSchemas,
62,041,744✔
2300
  };
2301
  TABLE_SET_COL_COMPRESSED(entry.flags);
61,917,320✔
2302
  if (pReq->virtualStb) {
61,917,320✔
2303
    TABLE_SET_VIRTUAL(entry.flags);
107,332✔
2304
  }
2305
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
61,902,226✔
2306
    TABLE_SET_ROLLUP(entry.flags);
30,756✔
2307
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
30,756✔
2308
  }
2309

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

2322
  metaFetchEntryFree(&pEntry);
62,046,219✔
2323
  TAOS_RETURN(code);
62,167,864✔
2324
}
2325

2326
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2327
  int32_t code = 0;
×
2328

2329
  if (taosArrayGetSize(uidArray) == 0) {
×
2330
    return TSDB_CODE_SUCCESS;
×
2331
  }
2332

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

2343
    SMetaEntry entry = {
×
2344
        .version = version,
2345
        .uid = uid,
2346
    };
2347

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

2365
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
153,780✔
2366
  int32_t code = TSDB_CODE_SUCCESS;
153,780✔
2367

2368
  if (NULL == pReq->name || pReq->name[0] == 0) {
153,780!
2369
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2370
              __LINE__, version);
2371
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2372
  }
2373

2374
  SMetaEntry *pEntry = NULL;
153,780✔
2375
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
153,780✔
2376
  if (code) {
153,780!
2377
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2378
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2379
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2380
  }
2381

2382
  if (pEntry->type != TSDB_SUPER_TABLE) {
153,780!
2383
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2384
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2385
    metaFetchEntryFree(&pEntry);
×
2386
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2387
  }
2388

2389
  if (pEntry->uid != pReq->tbUid) {
153,780!
2390
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2391
              ", version:%" PRId64,
2392
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2393
    metaFetchEntryFree(&pEntry);
×
2394
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2395
  }
2396

2397
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
153,780!
2398
    // overwrite the old rsma definition if exists
2399
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2400
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2401
  } else {
2402
    TABLE_SET_ROLLUP(pEntry->flags);
153,780✔
2403
  }
2404

2405
  SMetaEntry entry = *pEntry;
153,780✔
2406
  entry.version = version;
153,780✔
2407
  entry.stbEntry.rsmaParam.name = pReq->name;
153,780✔
2408
  entry.stbEntry.rsmaParam.uid = pReq->uid;
153,780✔
2409
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
153,780✔
2410
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
153,780✔
2411
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
153,780✔
2412
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
153,780✔
2413
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
153,780✔
2414
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
153,780✔
2415

2416
  // do handle the entry
2417
  code = metaHandleEntry2(pMeta, &entry);
153,780✔
2418
  if (code) {
153,780!
2419
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2420
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2421
    metaFetchEntryFree(&pEntry);
×
2422
    TAOS_RETURN(code);
×
2423
  } else {
2424
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
153,780✔
2425
    pMeta->pVnode->config.isRsma = 1;
153,780✔
2426
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
153,780!
2427
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2428
  }
2429

2430
  metaFetchEntryFree(&pEntry);
153,780✔
2431
  TAOS_RETURN(code);
153,780✔
2432
}
2433

2434
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
30,756✔
2435
  int32_t code = TSDB_CODE_SUCCESS;
30,756✔
2436

2437
  if (NULL == pReq->name || pReq->name[0] == 0) {
30,756!
2438
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2439
              __LINE__, version);
2440
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2441
  }
2442

2443
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
30,756!
2444
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2445
              __LINE__, version);
2446
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2447
  }
2448

2449
  SMetaEntry *pEntry = NULL;
30,756✔
2450
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
30,756✔
2451
  if (code) {
30,756!
2452
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2453
             __func__, __LINE__, pReq->name, pReq->tbName, version);
2454
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2455
  }
2456

2457
  if (pEntry->type != pReq->tbType) {
30,756!
2458
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2459
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2460
    metaFetchEntryFree(&pEntry);
×
2461
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2462
  }
2463

2464
  if (pEntry->uid != pReq->tbUid) {
30,756!
2465
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2466
              ", version:%" PRId64,
2467
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2468
    metaFetchEntryFree(&pEntry);
×
2469
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2470
  }
2471

2472
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
30,756!
2473
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
30,756!
2474
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
30,756!
2475
      metaError(
×
2476
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2477
          "uid:%" PRIi64 ", version:%" PRId64,
2478
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2479
          pReq->uid, version);
2480
      metaFetchEntryFree(&pEntry);
×
2481
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2482
    }
2483
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
30,756!
2484
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
30,756!
2485
  } else {
2486
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2487
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
2488
    metaFetchEntryFree(&pEntry);
×
2489
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2490
  }
2491

2492
  SMetaEntry entry = *pEntry;
30,756✔
2493
  entry.version = version;
30,756✔
2494
  TABLE_RESET_ROLLUP(entry.flags);
30,756✔
2495
  entry.stbEntry.rsmaParam.uid = 0;
30,756✔
2496
  entry.stbEntry.rsmaParam.name = NULL;
30,756✔
2497
  entry.stbEntry.rsmaParam.nFuncs = 0;
30,756✔
2498
  entry.stbEntry.rsmaParam.funcColIds = NULL;
30,756✔
2499
  entry.stbEntry.rsmaParam.funcIds = NULL;
30,756✔
2500

2501
  // do handle the entry
2502
  code = metaHandleEntry2(pMeta, &entry);
30,756✔
2503
  if (code) {
30,756!
2504
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2505
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
2506
    metaFetchEntryFree(&pEntry);
×
2507
    TAOS_RETURN(code);
×
2508
  } else {
2509
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
30,756!
2510
      pMeta->pVnode->config.isRsma = 0;
×
2511
    }
2512
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
30,756!
2513
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2514
  }
2515

2516
  metaFetchEntryFree(&pEntry);
30,756✔
2517
  TAOS_RETURN(code);
30,756✔
2518
}
2519

2520
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
82,016✔
2521
  int32_t code = TSDB_CODE_SUCCESS;
82,016✔
2522

2523
  if (NULL == pReq->name || pReq->name[0] == 0) {
82,016!
2524
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2525
              __LINE__, version);
2526
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2527
  }
2528

2529
  SMetaEntry *pEntry = NULL;
82,016✔
2530
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
82,016✔
2531
  if (code) {
82,016!
2532
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2533
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2534
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2535
  }
2536

2537
  if (pEntry->uid != pReq->tbUid) {
82,016!
2538
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2539
              ", version:%" PRId64,
2540
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2541
    metaFetchEntryFree(&pEntry);
×
2542
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2543
  }
2544

2545
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
79,453!
2546
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
82,016!
2547
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
82,016!
2548
  } else {
2549
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2550
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2551
    metaFetchEntryFree(&pEntry);
×
2552
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2553
  }
2554

2555
  SMetaEntry entry = *pEntry;
79,453✔
2556
  entry.version = version;
79,453✔
2557
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
79,453!
2558
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
76,890✔
2559
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
76,890✔
2560
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
82,016✔
2561
  }
2562
  // do handle the entry
2563
  code = metaHandleEntry2(pMeta, &entry);
82,016✔
2564
  if (code) {
82,016!
2565
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2566
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2567
    metaFetchEntryFree(&pEntry);
×
2568
    TAOS_RETURN(code);
×
2569
  } else {
2570
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
82,016!
2571
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2572
  }
2573

2574
  metaFetchEntryFree(&pEntry);
82,016✔
2575
  TAOS_RETURN(code);
82,016✔
2576
}
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