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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

67.08
/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) {
6,780,992✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
6,780,992✔
31
  void     *value = NULL;
6,803,550✔
32
  int32_t   valueSize = 0;
6,805,121✔
33
  SMetaInfo info;
6,801,829✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
6,804,822✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
5,385✔
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);
6,800,284✔
43
  if (r == 0) {  // name exists, check uid and type
6,788,262✔
44
    int64_t uid = *(tb_uid_t *)value;
2,319✔
45
    tdbFree(value);
2,319✔
46

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

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

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

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

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

117
  return code;
1,000,622✔
118
}
119

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

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

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

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

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

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

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

179
  // handle entry
180
  SMetaEntry entry = {
13,582,635✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
6,796,480✔
184
      .name = pReq->name,
6,793,984✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
6,783,763✔
188
  };
189
  if (pReq->rollup) {
6,781,747✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
6,799,962✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
6,784,219✔
195
    entry.colCmpr = pReq->colCmpr;
6,784,219✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
6,779,587✔
199

200
  if (pReq->virtualStb) {
6,781,199✔
201
    TABLE_SET_VIRTUAL(entry.flags);
55,948✔
202
  }
203

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

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
1,018,075✔
221
  if (code) {
1,016,718✔
222
    TAOS_RETURN(code);
1,546✔
223
  }
224

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

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
53,622,771✔
252
      pReq->ctb.suid == 0) {
53,613,655✔
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
15,147✔
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) {
53,623,331✔
260
    pReq->uid = *(int64_t *)value;
710,708✔
261
    tdbFreeClear(value);
710,708✔
262

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

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

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
52,915,319✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
52,913,450✔
292
  if (code) {
52,917,739✔
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) {
52,917,739✔
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) {
52,913,525✔
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;
52,912,359✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
52,916,697✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
52,914,431✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
239,091,699✔
319
      STagVal tagVal = {
186,502,775✔
320
          .cid = pTagSchema->pSchema[i].colId,
186,499,953✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
186,496,435✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
138,016,131✔
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);
52,910,305✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
52,907,340✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
52,913,802✔
339
    if (TSDB_CODE_SUCCESS != code) {
52,904,040✔
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;
52,910,240✔
345
}
346

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

350
  if (NULL == ppRsp) {
52,712,261✔
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
52,712,261✔
355
  if (NULL == ppRsp) {
52,695,034✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
52,695,034✔
360
  (*ppRsp)->tuid = pEntry->uid;
52,706,105✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
52,706,043✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
52,710,410✔
363

364
  return code;
52,715,242✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
53,424,756✔
372
  if (code) {
53,421,205✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
710,708✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,081✔
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
710,708✔
378
  }
379

380
  SMetaEntry entry = {
52,710,497✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
52,711,170✔
384
      .name = pReq->name,
52,704,032✔
385
      .ctbEntry.btime = pReq->btime,
52,702,322✔
386
      .ctbEntry.ttlDays = pReq->ttl,
52,697,914✔
387
      .ctbEntry.commentLen = pReq->commentLen,
52,711,346✔
388
      .ctbEntry.comment = pReq->comment,
52,701,206✔
389
      .ctbEntry.suid = pReq->ctb.suid,
52,697,474✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
52,699,180✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
52,713,546✔
395
  if (code) {
52,714,439✔
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);
52,714,439✔
402
  if (TSDB_CODE_SUCCESS == code) {
52,713,369✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
52,713,490✔
404
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
405
  } else {
406
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
407
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
408
              pReq->ctb.suid, version);
409
  }
410
  return code;
52,717,673✔
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) {
18,446,164✔
419
  int32_t code = 0;
18,446,164✔
420
  void   *value = NULL;
18,446,164✔
421
  int32_t valueSize = 0;
18,446,164✔
422

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

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

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

449
  if (NULL == ppRsp) {
18,292,463✔
450
    return code;
×
451
  }
452

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

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
100,282,798✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
81,990,335✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
81,990,335✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
81,990,335✔
468
    if (pEntry->pExtSchemas) {
81,990,335✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
791,403✔
470
    }
471
  }
472

473
  return code;
18,292,463✔
474
}
475

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

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

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

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

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

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

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

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

543
  return code;
111,435✔
544
}
545

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

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

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

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

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

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

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

610
  return code;
201,783✔
611
}
612

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

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

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

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

658
// Drop Normal Table
659

660
// Alter Normal Table
661

662
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
72,073,719✔
663
  int32_t code = TSDB_CODE_SUCCESS;
72,073,719✔
664
  if (TSDB_CHILD_TABLE == pReq->type) {
72,073,719✔
665
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
53,428,258✔
666
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
18,648,433✔
667
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
18,334,243✔
668
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
314,190✔
669
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
111,921✔
670
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
202,269✔
671
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
202,269✔
672
  } else {
673
    code = TSDB_CODE_INVALID_MSG;
×
674
  }
675
  TAOS_RETURN(code);
72,077,117✔
676
}
677

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

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

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

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

703
  if (pReq->isVirtual) {
1,000,622✔
704
    if (pReq->suid == 0) {
57,802✔
705
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
29,051✔
706
    } else {
707
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
28,751✔
708
    }
709
  } else {
710
    if (pReq->suid == 0) {
942,820✔
711
      entry.type = -TSDB_NORMAL_TABLE;
528,793✔
712
    } else {
713
      entry.type = -TSDB_CHILD_TABLE;
414,027✔
714
    }
715
  }
716
  code = metaHandleEntry2(pMeta, &entry);
1,000,622✔
717
  if (code) {
1,000,622✔
718
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
719
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
720
  } else {
721
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,000,622✔
722
             pReq->uid, version);
723
  }
724
  TAOS_RETURN(code);
1,000,622✔
725
}
726

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

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

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

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

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

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

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

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

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

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

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

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

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

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

942
  metaFetchEntryFree(&pEntry);
3,366,126✔
943
  TAOS_RETURN(code);
3,366,126✔
944
}
945

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

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

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

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

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

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

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

997
  tColumn = *pColumn;
91,024✔
998

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

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

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

1056
  // build response
1057
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
91,024✔
1058
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
31,504✔
1059
    if (code) {
31,504✔
1060
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1061
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1062
    } else {
1063
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
32,051,221✔
1064
        SColRef *p = &pEntry->colRef.pColRef[i];
32,019,717✔
1065
        pRsp->pColRefs[i].hasRef = p->hasRef;
32,019,717✔
1066
        pRsp->pColRefs[i].id = p->id;
32,019,717✔
1067
        if (p->hasRef) {
32,019,717✔
1068
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
16,022,686✔
1069
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
16,022,686✔
1070
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
16,022,686✔
1071
        }
1072
      }
1073
    }
1074
  } else {
1075
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
59,520✔
1076
    if (code) {
59,520✔
1077
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1078
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1079
    } else {
1080
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
60,898,555✔
1081
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
60,839,035✔
1082
        pRsp->pSchemaExt[i].colId = p->id;
60,839,035✔
1083
        pRsp->pSchemaExt[i].compress = p->alg;
60,839,035✔
1084
      }
1085
    }
1086
  }
1087

1088
  metaFetchEntryFree(&pEntry);
91,024✔
1089
  TAOS_RETURN(code);
91,024✔
1090
}
1091

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

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

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

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

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

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

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

1142

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

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

1160
  // build response
1161
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
45,382✔
1162
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
30,187✔
1163
    if (code) {
30,187✔
1164
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1165
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1166
    } else {
1167
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
211,188✔
1168
        SColRef *p = &pEntry->colRef.pColRef[i];
181,001✔
1169
        pRsp->pColRefs[i].hasRef = p->hasRef;
181,001✔
1170
        pRsp->pColRefs[i].id = p->id;
181,001✔
1171
        if (p->hasRef) {
181,001✔
1172
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
112,225✔
1173
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
112,225✔
1174
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
112,225✔
1175
        }
1176
      }
1177
    }
1178
  } else {
1179
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
15,195✔
1180
    if (code) {
15,195✔
1181
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1182
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1183
    } else {
1184
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
172,686✔
1185
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
157,491✔
1186
        pRsp->pSchemaExt[i].colId = p->id;
157,491✔
1187
        pRsp->pSchemaExt[i].compress = p->alg;
157,491✔
1188
      }
1189
    }
1190
  }
1191

1192
  metaFetchEntryFree(&pEntry);
45,382✔
1193
  TAOS_RETURN(code);
45,382✔
1194
}
1195

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

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

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

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

1221
  // search the column to update
1222
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
500,714✔
1223
  SSchema        *pColumn = NULL;
500,714✔
1224
  int32_t         iColumn = 0;
500,714✔
1225
  int32_t         rowSize = 0;
500,714✔
1226
  for (int32_t i = 0; i < pSchema->nCols; i++) {
35,748,203✔
1227
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
35,247,489✔
1228
      pColumn = &pSchema->pSchema[i];
500,714✔
1229
      iColumn = i;
500,714✔
1230
    }
1231
    rowSize += pSchema->pSchema[i].bytes;
35,247,489✔
1232
  }
1233

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

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

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

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

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

1275
  // build response
1276
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
257,354✔
1277
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,243✔
1278
    if (code) {
28,243✔
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++) {
16,118,940✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
16,090,697✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
16,090,697✔
1285
        pRsp->pColRefs[i].id = p->id;
16,090,697✔
1286
        if (p->hasRef) {
16,090,697✔
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
82,594✔
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
82,594✔
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
82,594✔
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
229,111✔
1295
    if (code) {
229,111✔
1296
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1297
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1298
    } else {
1299
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,408,143✔
1300
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,179,032✔
1301
        pRsp->pSchemaExt[i].colId = p->id;
9,179,032✔
1302
        pRsp->pSchemaExt[i].compress = p->alg;
9,179,032✔
1303
      }
1304
    }
1305
  }
1306

1307
  metaFetchEntryFree(&pEntry);
257,354✔
1308
  TAOS_RETURN(code);
257,354✔
1309
}
1310

1311
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,109,912✔
1312
  int32_t code = 0;
8,109,912✔
1313

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

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

1333
  TAOS_RETURN(code);
8,109,891✔
1334
}
1335

1336
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,109,912✔
1337
  int32_t code = TSDB_CODE_SUCCESS;
8,109,912✔
1338

1339
  // check request
1340
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
8,109,912✔
1341
  if (code) {
8,109,912✔
1342
    TAOS_RETURN(code);
21✔
1343
  }
1344

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

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

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

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

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

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

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

1416
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,114,716✔
1417
      STagVal value = {
9,134,337✔
1418
          .type = pTagSchema->pSchema[i].type,
9,134,337✔
1419
          .cid = pTagSchema->pSchema[i].colId,
9,134,337✔
1420
      };
1421

1422
      if (iColumn == i) {
9,134,337✔
1423
        if (pReq->isNull) {
7,980,379✔
1424
          continue;
45,944✔
1425
        }
1426
        if (IS_VAR_DATA_TYPE(value.type)) {
7,934,435✔
1427
          value.pData = pReq->pTagVal;
145,876✔
1428
          value.nData = pReq->nTagVal;
145,876✔
1429
        } else {
1430
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,788,559✔
1431
        }
1432
      } else if (!tTagGet(pOldTag, &value)) {
1,153,958✔
1433
        continue;
305,666✔
1434
      }
1435

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

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

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

1474
  // free resource and return
1475
  metaFetchEntryFree(&pChild);
8,109,891✔
1476
  metaFetchEntryFree(&pSuper);
8,109,891✔
1477
  TAOS_RETURN(code);
8,109,891✔
1478
}
1479

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

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

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

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

1508
  TAOS_RETURN(code);
3,480✔
1509
}
1510

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

1514
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,480✔
1515
  if (code) {
3,480✔
1516
    TAOS_RETURN(code);
×
1517
  }
1518

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

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

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

1545
  // search the tags to update
1546
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,480✔
1547

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

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

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

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

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

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

1613
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
24,360✔
1614
    if (pTagVal == NULL) {
24,360✔
1615
      if (!tTagGet(pOldTag, &value)) {
3,480✔
1616
        continue;
×
1617
      }
1618
    } else {
1619
      value.type = pCol->type;
20,880✔
1620
      if (pTagVal->isNull) {
20,880✔
1621
        continue;
4,176✔
1622
      }
1623

1624
      if (IS_VAR_DATA_TYPE(pCol->type)) {
16,704✔
1625
        value.pData = pTagVal->pTagVal;
2,784✔
1626
        value.nData = pTagVal->nTagVal;
2,784✔
1627
      } else {
1628
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
13,920✔
1629
      }
1630
    }
1631

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

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

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

1672
  taosHashCleanup(pTagTable);
3,480✔
1673
  metaFetchEntryFree(&pChild);
3,480✔
1674
  metaFetchEntryFree(&pSuper);
3,480✔
1675
  TAOS_RETURN(code);
3,480✔
1676
}
1677

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

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

1687
  return code;
17,676✔
1688
}
1689

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

1693
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
17,676✔
1694
  if (code) {
17,676✔
1695
    TAOS_RETURN(code);
×
1696
  }
1697

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

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

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

1770
  metaFetchEntryFree(&pEntry);
17,676✔
1771
  TAOS_RETURN(code);
17,676✔
1772
}
1773

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

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

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

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

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

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

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

1834
  pEntry->version = version;
6,638✔
1835

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

1848
  metaFetchEntryFree(&pEntry);
6,638✔
1849
  TAOS_RETURN(code);
6,638✔
1850
}
1851

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2070
  metaFetchEntryFree(&pEntry);
59,250✔
2071
  metaFetchEntryFree(&pSuper);
59,250✔
2072
  TAOS_RETURN(code);
59,250✔
2073
}
2074

2075
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,958✔
2076
  int32_t code = TSDB_CODE_SUCCESS;
5,958✔
2077

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

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

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

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

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

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

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

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

2144
  int32_t numOfChangedTags = 0;
5,958✔
2145
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
30,370✔
2146
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
24,412✔
2147
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
24,412✔
2148

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

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

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

2176
  pEntry->version = version;
5,958✔
2177
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
5,958✔
2178

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

2191
  metaFetchEntryFree(&pEntry);
5,958✔
2192
  TAOS_RETURN(code);
5,958✔
2193
}
2194

2195
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
3,970✔
2196
  int32_t code = TSDB_CODE_SUCCESS;
3,970✔
2197

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

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

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

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

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

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

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

2265
  metaFetchEntryFree(&pEntry);
3,970✔
2266
  TAOS_RETURN(code);
3,970✔
2267
}
2268

2269
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
8,454,889✔
2270
  int32_t code = TSDB_CODE_SUCCESS;
8,454,889✔
2271

2272
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
8,454,889✔
2273
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
20,959✔
2274
              __FILE__, __LINE__, version);
2275
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
20,959✔
2276
  }
2277

2278
  SMetaEntry *pEntry = NULL;
8,436,653✔
2279
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
8,455,566✔
2280
  if (code) {
8,446,396✔
2281
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2282
              __FILE__, __LINE__, pReq->name, version);
2283
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2284
  }
2285

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

2293
  SMetaEntry entry = {
25,270,817✔
2294
      .version = version,
2295
      .type = TSDB_SUPER_TABLE,
2296
      .uid = pReq->suid,
8,444,394✔
2297
      .name = pReq->name,
8,445,942✔
2298
      .stbEntry.schemaRow = pReq->schemaRow,
2299
      .stbEntry.schemaTag = pReq->schemaTag,
2300
      .stbEntry.keep = pReq->keep,
8,449,740✔
2301
      .colCmpr = pReq->colCmpr,
2302
      .pExtSchemas = pReq->pExtSchemas,
8,418,980✔
2303
  };
2304
  TABLE_SET_COL_COMPRESSED(entry.flags);
8,418,838✔
2305
  if (pReq->virtualStb) {
8,418,838✔
2306
    TABLE_SET_VIRTUAL(entry.flags);
21,358✔
2307
  }
2308
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
8,447,968✔
2309
    TABLE_SET_ROLLUP(entry.flags);
4,644✔
2310
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,644✔
2311
  }
2312

2313
  // do handle the entry
2314
  code = metaHandleEntry2(pMeta, &entry);
8,437,065✔
2315
  if (code) {
8,414,000✔
2316
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2317
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2318
    metaFetchEntryFree(&pEntry);
×
2319
    TAOS_RETURN(code);
×
2320
  } else {
2321
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
8,414,000✔
2322
             pReq->suid, version);
2323
  }
2324

2325
  metaFetchEntryFree(&pEntry);
8,434,826✔
2326
  TAOS_RETURN(code);
8,461,651✔
2327
}
2328

2329
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2330
  int32_t code = 0;
×
2331

2332
  if (taosArrayGetSize(uidArray) == 0) {
×
2333
    return TSDB_CODE_SUCCESS;
×
2334
  }
2335

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

2346
    SMetaEntry entry = {
×
2347
        .version = version,
2348
        .uid = uid,
2349
    };
2350

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

2368
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
23,220✔
2369
  int32_t code = TSDB_CODE_SUCCESS;
23,220✔
2370

2371
  if (NULL == pReq->name || pReq->name[0] == 0) {
23,220✔
2372
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2373
              __LINE__, version);
2374
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2375
  }
2376

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

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

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

2400
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
23,220✔
2401
    // overwrite the old rsma definition if exists
2402
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2403
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2404
  } else {
2405
    TABLE_SET_ROLLUP(pEntry->flags);
23,220✔
2406
  }
2407

2408
  SMetaEntry entry = *pEntry;
23,220✔
2409
  entry.version = version;
23,220✔
2410
  entry.stbEntry.rsmaParam.name = pReq->name;
23,220✔
2411
  entry.stbEntry.rsmaParam.uid = pReq->uid;
23,220✔
2412
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
23,220✔
2413
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
23,220✔
2414
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
23,220✔
2415
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
23,220✔
2416
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
23,220✔
2417
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
23,220✔
2418

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

2433
  metaFetchEntryFree(&pEntry);
23,220✔
2434
  TAOS_RETURN(code);
23,220✔
2435
}
2436

2437
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,644✔
2438
  int32_t code = TSDB_CODE_SUCCESS;
4,644✔
2439

2440
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,644✔
2441
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2442
              __LINE__, version);
2443
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2444
  }
2445

2446
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
4,644✔
2447
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2448
              __LINE__, version);
2449
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2450
  }
2451

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

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

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

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

2495
  SMetaEntry entry = *pEntry;
4,644✔
2496
  entry.version = version;
4,644✔
2497
  TABLE_RESET_ROLLUP(entry.flags);
4,644✔
2498
  entry.stbEntry.rsmaParam.uid = 0;
4,644✔
2499
  entry.stbEntry.rsmaParam.name = NULL;
4,644✔
2500
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,644✔
2501
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,644✔
2502
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,644✔
2503

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

2519
  metaFetchEntryFree(&pEntry);
4,644✔
2520
  TAOS_RETURN(code);
4,644✔
2521
}
2522

2523
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,384✔
2524
  int32_t code = TSDB_CODE_SUCCESS;
12,384✔
2525

2526
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,384✔
2527
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2528
              __LINE__, version);
2529
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2530
  }
2531

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

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

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

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

2577
  metaFetchEntryFree(&pEntry);
12,384✔
2578
  TAOS_RETURN(code);
12,384✔
2579
}
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