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

taosdata / TDengine / #4885

15 Dec 2025 03:26AM UTC coverage: 65.258% (+4.6%) from 60.617%
#4885

push

travis-ci

web-flow
feat(tmq): [TS-6379]remove limition for table operation in tmq  (#33834)

872 of 1074 new or added lines in 16 files covered. (81.19%)

659 existing lines in 92 files now uncovered.

177890 of 272597 relevant lines covered (65.26%)

103732965.73 hits per line

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

66.3
/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,980,821✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
6,980,821✔
31
  void     *value = NULL;
6,978,962✔
32
  int32_t   valueSize = 0;
6,982,240✔
33
  SMetaInfo info;
6,981,172✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
6,981,077✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
95✔
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,978,343✔
43
  if (r == 0) {  // name exists, check uid and type
6,969,756✔
44
    int64_t uid = *(tb_uid_t *)value;
2,316✔
45
    tdbFree(value);
2,316✔
46

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

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

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

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

117
  return code;
964,064✔
118
}
119

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

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

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

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
1,009,244✔
149
  if (code) {
1,009,925✔
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,009,925✔
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,009,925✔
161
}
162

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
6,964,400✔
169
  if (code != TSDB_CODE_SUCCESS) {
6,974,576✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
2,316✔
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,316✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
13,933,432✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
6,969,909✔
184
      .name = pReq->name,
6,966,444✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
6,959,068✔
188
  };
189
  if (pReq->rollup) {
6,961,080✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
6,970,706✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
6,960,042✔
195
    entry.colCmpr = pReq->colCmpr;
6,960,042✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
6,950,725✔
199

200
  if (pReq->virtualStb) {
6,960,801✔
201
    TABLE_SET_VIRTUAL(entry.flags);
53,143✔
202
  }
203

204
  code = metaHandleEntry2(pMeta, &entry);
6,958,093✔
205
  if (TSDB_CODE_SUCCESS == code) {
6,982,940✔
206
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
6,982,266✔
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,
674✔
210
              pReq->suid, tstrerror(code));
211
  }
212
  TAOS_RETURN(code);
6,982,940✔
213
}
214

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
1,014,627✔
221
  if (code) {
1,012,834✔
222
    TAOS_RETURN(code);
1,544✔
223
  }
224

225
  // handle entry
226
  SMetaEntry entry = {
1,011,290✔
227
      .version = verison,
228
      .type = -TSDB_SUPER_TABLE,
229
      .uid = pReq->suid,
1,011,041✔
230
  };
231
  code = metaHandleEntry2(pMeta, &entry);
1,008,144✔
232
  if (code) {
1,013,083✔
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,013,083✔
237
             pReq->suid, verison);
238
  }
239
  TAOS_RETURN(code);
1,013,083✔
240
}
241

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
53,159,094✔
252
      pReq->ctb.suid == 0) {
53,158,698✔
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
339✔
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,160,561✔
260
    pReq->uid = *(int64_t *)value;
707,232✔
261
    tdbFreeClear(value);
707,232✔
262

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

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

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
52,453,831✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
52,455,210✔
292
  if (code) {
52,453,683✔
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,453,683✔
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,451,881✔
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,448,834✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
52,455,141✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
52,450,210✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
237,512,829✔
319
      STagVal tagVal = {
185,390,744✔
320
          .cid = pTagSchema->pSchema[i].colId,
185,384,026✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
185,386,979✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
136,833,091✔
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,448,186✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
52,450,883✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
52,451,457✔
339
    if (TSDB_CODE_SUCCESS != code) {
52,450,820✔
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,451,352✔
345
}
346

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

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

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
52,247,175✔
355
  if (NULL == ppRsp) {
52,239,674✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
52,239,674✔
360
  (*ppRsp)->tuid = pEntry->uid;
52,245,596✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
52,245,066✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
52,245,522✔
363

364
  return code;
52,250,504✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
52,956,757✔
372
  if (code) {
52,955,378✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
707,232✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,074✔
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
707,232✔
378
  }
379

380
  SMetaEntry entry = {
52,248,146✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
52,248,753✔
384
      .name = pReq->name,
52,246,406✔
385
      .ctbEntry.btime = pReq->btime,
52,243,470✔
386
      .ctbEntry.ttlDays = pReq->ttl,
52,239,882✔
387
      .ctbEntry.commentLen = pReq->commentLen,
52,249,358✔
388
      .ctbEntry.comment = pReq->comment,
52,243,123✔
389
      .ctbEntry.suid = pReq->ctb.suid,
52,241,503✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
52,242,777✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
52,250,064✔
395
  if (code) {
52,248,445✔
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,248,445✔
402
  if (TSDB_CODE_SUCCESS == code) {
52,252,519✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
52,252,771✔
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,254,983✔
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) {
2,471,331✔
419
  int32_t code = 0;
2,471,331✔
420
  void   *value = NULL;
2,471,331✔
421
  int32_t valueSize = 0;
2,471,331✔
422

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

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

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

449
  if (NULL == ppRsp) {
2,309,876✔
450
    return code;
×
451
  }
452

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

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
36,361,616✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
34,051,740✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
34,051,740✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
34,051,740✔
468
    if (pEntry->pExtSchemas) {
34,051,740✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
795,019✔
470
    }
471
  }
472

473
  return code;
2,309,876✔
474
}
475

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

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

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

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

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

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

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

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

543
  return code;
110,740✔
544
}
545

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

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

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

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

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

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

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

609
  return code;
202,435✔
610
}
611

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

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

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

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

656
// Drop Normal Table
657

658
// Alter Normal Table
659

660
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
55,632,675✔
661
  int32_t code = TSDB_CODE_SUCCESS;
55,632,675✔
662
  if (TSDB_CHILD_TABLE == pReq->type) {
55,632,675✔
663
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
52,960,294✔
664
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
2,673,766✔
665
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
2,360,591✔
666
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
313,175✔
667
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
110,740✔
668
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
202,435✔
669
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
202,435✔
670
  } else {
671
    code = TSDB_CODE_INVALID_MSG;
×
672
  }
673
  TAOS_RETURN(code);
55,635,451✔
674
}
675

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

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

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

696
  SMetaEntry entry = {
964,064✔
697
      .version = version,
698
      .uid = pReq->uid,
964,064✔
699
  };
700

701
  if (pReq->isVirtual) {
964,064✔
702
    if (pReq->suid == 0) {
57,622✔
703
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
28,962✔
704
    } else {
705
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
28,660✔
706
    }
707
  } else {
708
    if (pReq->suid == 0) {
906,442✔
709
      entry.type = -TSDB_NORMAL_TABLE;
495,960✔
710
    } else {
711
      entry.type = -TSDB_CHILD_TABLE;
410,482✔
712
    }
713
  }
714
  code = metaHandleEntry2(pMeta, &entry);
964,064✔
715
  if (code) {
963,689✔
716
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
717
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
718
  } else {
719
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
963,689✔
720
             pReq->uid, version);
721
  }
722
  TAOS_RETURN(code);
964,064✔
723
}
724

725
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,472,502✔
726
  int32_t code = 0;
4,472,502✔
727

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

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

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

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

774
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,686,752✔
775
  int32_t code = TSDB_CODE_SUCCESS;
3,686,752✔
776

777
  // check request
778
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,686,752✔
779
  if (code) {
3,686,752✔
780
    TAOS_RETURN(code);
×
781
  }
782

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

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

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

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

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

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

900
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,358,514✔
901
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
54,258✔
902
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
903
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
904
    } else {
905
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
414,342✔
906
        SColRef *p = &pEntry->colRef.pColRef[i];
360,084✔
907
        pRsp->pColRefs[i].hasRef = p->hasRef;
360,084✔
908
        pRsp->pColRefs[i].id = p->id;
360,084✔
909
        if (p->hasRef) {
360,084✔
910
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
176,928✔
911
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
176,928✔
912
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
176,928✔
913
        }
914
      }
915
    }
916
  } else {
917
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
3,304,256✔
918
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
919
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
920
    } else {
921
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
922
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
923
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
924
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
925
      }
926
    }
927
  }
928

929
  metaFetchEntryFree(&pEntry);
3,358,514✔
930
  TAOS_RETURN(code);
3,358,514✔
931
}
932

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

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

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

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

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

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

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

984
  tColumn = *pColumn;
90,199✔
985

986
  // do drop column
987
  pEntry->version = version;
90,199✔
988
  if (pSchema->nCols - iColumn - 1 > 0) {
90,199✔
989
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
66,144✔
990
  }
991
  pSchema->nCols--;
90,199✔
992
  pSchema->version++;
90,199✔
993
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
90,199✔
994
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
30,432✔
995
    if (code) {
30,432✔
996
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
997
                __LINE__, tstrerror(code), version);
998
      metaFetchEntryFree(&pEntry);
×
999
      TAOS_RETURN(code);
×
1000
    }
1001
    if (pEntry->colRef.nCols != pSchema->nCols) {
30,432✔
1002
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1003
                __func__, __FILE__, __LINE__, version);
1004
      metaFetchEntryFree(&pEntry);
×
1005
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1006
    }
1007
  } else {
1008
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
59,767✔
1009
    if (code) {
59,767✔
1010
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1011
                __LINE__, tstrerror(code), version);
1012
      metaFetchEntryFree(&pEntry);
×
1013
      TAOS_RETURN(code);
×
1014
    }
1015
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
59,767✔
1016
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1017
                __func__, __FILE__, __LINE__, version);
1018
      metaFetchEntryFree(&pEntry);
×
1019
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1020
    }
1021
  }
1022

1023
  // update column extschema
1024
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
90,199✔
1025
  if (code) {
90,199✔
1026
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1027
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1028
    metaFetchEntryFree(&pEntry);
×
1029
    TAOS_RETURN(code);
×
1030
  }
1031

1032
  // do handle entry
1033
  code = metaHandleEntry2(pMeta, pEntry);
90,199✔
1034
  if (code) {
90,199✔
1035
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1036
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1037
    metaFetchEntryFree(&pEntry);
×
1038
  } else {
1039
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
90,199✔
1040
             pEntry->uid, version);
1041
  }
1042

1043
  // build response
1044
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
90,199✔
1045
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
30,432✔
1046
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1047
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1048
    } else {
1049
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
206,316✔
1050
        SColRef *p = &pEntry->colRef.pColRef[i];
175,884✔
1051
        pRsp->pColRefs[i].hasRef = p->hasRef;
175,884✔
1052
        pRsp->pColRefs[i].id = p->id;
175,884✔
1053
        if (p->hasRef) {
175,884✔
1054
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
98,553✔
1055
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
98,553✔
1056
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
98,553✔
1057
        }
1058
      }
1059
    }
1060
  } else {
1061
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
59,767✔
1062
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1063
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1064
    } else {
1065
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
43,451,993✔
1066
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
43,392,226✔
1067
        pRsp->pSchemaExt[i].colId = p->id;
43,392,226✔
1068
        pRsp->pSchemaExt[i].compress = p->alg;
43,392,226✔
1069
      }
1070
    }
1071
  }
1072

1073
  metaFetchEntryFree(&pEntry);
90,199✔
1074
  TAOS_RETURN(code);
90,199✔
1075
}
1076

1077
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
45,193✔
1078
  int32_t code = TSDB_CODE_SUCCESS;
45,193✔
1079

1080
  // check request
1081
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
45,193✔
1082
  if (code) {
45,193✔
1083
    TAOS_RETURN(code);
×
1084
  }
1085

1086
  if (NULL == pReq->colNewName) {
45,193✔
1087
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1088
              __func__, __FILE__, __LINE__, version);
1089
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1090
  }
1091

1092
  // fetch old entry
1093
  SMetaEntry *pEntry = NULL;
45,193✔
1094
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
45,193✔
1095
  if (code) {
45,193✔
1096
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1097
              __FILE__, __LINE__, pReq->tbName, version);
1098
    TAOS_RETURN(code);
×
1099
  }
1100

1101
  if (pEntry->version >= version) {
45,193✔
1102
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1103
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1104
    metaFetchEntryFree(&pEntry);
×
1105
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1106
  }
1107

1108
  // search the column to update
1109
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
45,193✔
1110
  SSchema        *pColumn = NULL;
45,193✔
1111
  int32_t         iColumn = 0;
45,193✔
1112
  for (int32_t i = 0; i < pSchema->nCols; i++) {
205,518✔
1113
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
205,518✔
1114
      pColumn = &pSchema->pSchema[i];
45,193✔
1115
      iColumn = i;
45,193✔
1116
      break;
45,193✔
1117
    }
1118
  }
1119

1120
  if (NULL == pColumn) {
45,193✔
1121
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1122
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1123
    metaFetchEntryFree(&pEntry);
×
1124
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1125
  }
1126

1127

1128
  // do update column name
1129
  pEntry->version = version;
45,193✔
1130
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
45,193✔
1131
  pSchema->version++;
45,193✔
1132

1133
  // do handle entry
1134
  code = metaHandleEntry2(pMeta, pEntry);
45,193✔
1135
  if (code) {
45,193✔
1136
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1137
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1138
    metaFetchEntryFree(&pEntry);
×
1139
    TAOS_RETURN(code);
×
1140
  } else {
1141
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
45,193✔
1142
             pEntry->uid, version);
1143
  }
1144

1145
  // build response
1146
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
45,193✔
1147
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
30,087✔
1148
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1149
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1150
    } else {
1151
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
210,510✔
1152
        SColRef *p = &pEntry->colRef.pColRef[i];
180,423✔
1153
        pRsp->pColRefs[i].hasRef = p->hasRef;
180,423✔
1154
        pRsp->pColRefs[i].id = p->id;
180,423✔
1155
        if (p->hasRef) {
180,423✔
1156
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
111,861✔
1157
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
111,861✔
1158
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
111,861✔
1159
        }
1160
      }
1161
    }
1162
  } else {
1163
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
15,106✔
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->colCmpr.nCols; i++) {
171,683✔
1168
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
156,577✔
1169
        pRsp->pSchemaExt[i].colId = p->id;
156,577✔
1170
        pRsp->pSchemaExt[i].compress = p->alg;
156,577✔
1171
      }
1172
    }
1173
  }
1174

1175
  metaFetchEntryFree(&pEntry);
45,193✔
1176
  TAOS_RETURN(code);
45,193✔
1177
}
1178

1179
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
509,495✔
1180
  int32_t code = TSDB_CODE_SUCCESS;
509,495✔
1181

1182
  // check request
1183
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
509,495✔
1184
  if (code) {
509,495✔
1185
    TAOS_RETURN(code);
×
1186
  }
1187

1188
  // fetch old entry
1189
  SMetaEntry *pEntry = NULL;
509,495✔
1190
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
509,495✔
1191
  if (code) {
509,495✔
1192
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1193
              __FILE__, __LINE__, pReq->tbName, version);
1194
    TAOS_RETURN(code);
×
1195
  }
1196

1197
  if (pEntry->version >= version) {
509,495✔
1198
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1199
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1200
    metaFetchEntryFree(&pEntry);
×
1201
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1202
  }
1203

1204
  // search the column to update
1205
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
509,495✔
1206
  SSchema        *pColumn = NULL;
509,495✔
1207
  int32_t         iColumn = 0;
509,495✔
1208
  int32_t         rowSize = 0;
509,495✔
1209
  for (int32_t i = 0; i < pSchema->nCols; i++) {
20,222,338✔
1210
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
19,712,843✔
1211
      pColumn = &pSchema->pSchema[i];
509,495✔
1212
      iColumn = i;
509,495✔
1213
    }
1214
    rowSize += pSchema->pSchema[i].bytes;
19,712,843✔
1215
  }
1216

1217
  if (NULL == pColumn) {
509,495✔
1218
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1219
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1220
    metaFetchEntryFree(&pEntry);
×
1221
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1222
  }
1223

1224
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
509,495✔
1225
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
206,244✔
1226
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1227
              version);
1228
    metaFetchEntryFree(&pEntry);
206,244✔
1229
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
206,244✔
1230
  }
1231

1232
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
303,251✔
1233
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
47,180✔
1234
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW,
1235
              version);
1236
    metaFetchEntryFree(&pEntry);
47,180✔
1237
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
47,180✔
1238
  }
1239

1240
  // do change the column bytes
1241
  pEntry->version = version;
256,071✔
1242
  pSchema->version++;
256,071✔
1243
  pColumn->bytes = pReq->colModBytes;
256,071✔
1244

1245
  // do handle entry
1246
  code = metaHandleEntry2(pMeta, pEntry);
256,071✔
1247
  if (code) {
256,071✔
1248
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1249
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1250
    metaFetchEntryFree(&pEntry);
×
1251
    TAOS_RETURN(code);
×
1252
  } else {
1253
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
256,071✔
1254
             pEntry->uid, version);
1255
  }
1256

1257
  // build response
1258
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
256,071✔
1259
    if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
27,675✔
1260
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1261
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1262
    } else {
1263
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
198,450✔
1264
        SColRef *p = &pEntry->colRef.pColRef[i];
170,775✔
1265
        pRsp->pColRefs[i].hasRef = p->hasRef;
170,775✔
1266
        pRsp->pColRefs[i].id = p->id;
170,775✔
1267
        if (p->hasRef) {
170,775✔
1268
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
82,350✔
1269
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
82,350✔
1270
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
82,350✔
1271
        }
1272
      }
1273
    }
1274
  } else {
1275
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
228,396✔
1276
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1277
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1278
    } else {
1279
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,380,080✔
1280
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,151,684✔
1281
        pRsp->pSchemaExt[i].colId = p->id;
9,151,684✔
1282
        pRsp->pSchemaExt[i].compress = p->alg;
9,151,684✔
1283
      }
1284
    }
1285
  }
1286

1287
  metaFetchEntryFree(&pEntry);
256,071✔
1288
  TAOS_RETURN(code);
256,071✔
1289
}
1290

1291
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,057,213✔
1292
  int32_t code = 0;
8,057,213✔
1293

1294
  // check tag name
1295
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
8,057,213✔
1296
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1297
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1298
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1299
  }
1300

1301
  // check name
1302
  void   *value = NULL;
8,057,213✔
1303
  int32_t valueSize = 0;
8,057,213✔
1304
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
8,057,213✔
1305
  if (code) {
8,057,213✔
1306
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
19✔
1307
              __FILE__, __LINE__, pReq->tbName, version);
1308
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
19✔
1309
    TAOS_RETURN(code);
19✔
1310
  }
1311
  tdbFreeClear(value);
8,057,194✔
1312

1313
  TAOS_RETURN(code);
8,057,194✔
1314
}
1315

1316
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,057,213✔
1317
  int32_t code = TSDB_CODE_SUCCESS;
8,057,213✔
1318

1319
  // check request
1320
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
8,057,213✔
1321
  if (code) {
8,057,213✔
1322
    TAOS_RETURN(code);
19✔
1323
  }
1324

1325
  // fetch child entry
1326
  SMetaEntry *pChild = NULL;
8,057,194✔
1327
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
8,057,194✔
1328
  if (code) {
8,057,194✔
1329
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1330
              __FILE__, __LINE__, pReq->tbName, version);
1331
    TAOS_RETURN(code);
×
1332
  }
1333

1334
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
8,057,194✔
1335
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1336
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1337
    metaFetchEntryFree(&pChild);
×
1338
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1339
  }
1340

1341
  // fetch super entry
1342
  SMetaEntry *pSuper = NULL;
8,057,194✔
1343
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
8,057,194✔
1344
  if (code) {
8,057,194✔
1345
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1346
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1347
    metaFetchEntryFree(&pChild);
×
1348
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1349
  }
1350

1351
  // search the tag to update
1352
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
8,057,194✔
1353
  SSchema        *pColumn = NULL;
8,057,194✔
1354
  int32_t         iColumn = 0;
8,057,194✔
1355
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,765,293✔
1356
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,765,293✔
1357
      pColumn = &pTagSchema->pSchema[i];
8,057,194✔
1358
      iColumn = i;
8,057,194✔
1359
      break;
8,057,194✔
1360
    }
1361
  }
1362

1363
  if (NULL == pColumn) {
8,057,194✔
1364
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1365
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1366
    metaFetchEntryFree(&pChild);
×
1367
    metaFetchEntryFree(&pSuper);
×
1368
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1369
  }
1370

1371
  // do change tag value
1372
  pChild->version = version;
8,057,194✔
1373
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8,057,194✔
1374
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
129,495✔
1375
    if (NULL == pNewTag) {
129,495✔
1376
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1377
                __LINE__, tstrerror(terrno), version);
1378
      metaFetchEntryFree(&pChild);
×
1379
      metaFetchEntryFree(&pSuper);
×
1380
      TAOS_RETURN(terrno);
×
1381
    }
1382
    pChild->ctbEntry.pTags = pNewTag;
129,495✔
1383
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
129,495✔
1384
  } else {
1385
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,927,699✔
1386

1387
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,927,699✔
1388
    if (NULL == pTagArray) {
7,927,699✔
1389
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1390
                __LINE__, tstrerror(terrno), version);
1391
      metaFetchEntryFree(&pChild);
×
1392
      metaFetchEntryFree(&pSuper);
×
1393
      TAOS_RETURN(terrno);
×
1394
    }
1395

1396
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,001,427✔
1397
      STagVal value = {
9,073,728✔
1398
          .type = pTagSchema->pSchema[i].type,
9,073,728✔
1399
          .cid = pTagSchema->pSchema[i].colId,
9,073,728✔
1400
      };
1401

1402
      if (iColumn == i) {
9,073,728✔
1403
        if (pReq->isNull) {
7,927,699✔
1404
          continue;
45,805✔
1405
        }
1406
        if (IS_VAR_DATA_TYPE(value.type)) {
7,881,894✔
1407
          value.pData = pReq->pTagVal;
144,995✔
1408
          value.nData = pReq->nTagVal;
144,995✔
1409
        } else {
1410
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,736,899✔
1411
        }
1412
      } else if (!tTagGet(pOldTag, &value)) {
1,146,029✔
1413
        continue;
303,377✔
1414
      }
1415

1416
      if (NULL == taosArrayPush(pTagArray, &value)) {
8,724,546✔
1417
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1418
                  __LINE__, tstrerror(terrno), version);
1419
        taosArrayDestroy(pTagArray);
×
1420
        metaFetchEntryFree(&pChild);
×
1421
        metaFetchEntryFree(&pSuper);
×
1422
        TAOS_RETURN(terrno);
×
1423
      }
1424
    }
1425

1426
    STag *pNewTag = NULL;
7,927,699✔
1427
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,927,699✔
1428
    if (code) {
7,927,699✔
1429
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1430
                __LINE__, tstrerror(code), version);
1431
      taosArrayDestroy(pTagArray);
×
1432
      metaFetchEntryFree(&pChild);
×
1433
      metaFetchEntryFree(&pSuper);
×
1434
      TAOS_RETURN(code);
×
1435
    }
1436
    taosArrayDestroy(pTagArray);
7,927,699✔
1437
    taosMemoryFree(pChild->ctbEntry.pTags);
7,927,699✔
1438
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,927,699✔
1439
  }
1440

1441
  // do handle entry
1442
  code = metaHandleEntry2(pMeta, pChild);
8,057,194✔
1443
  if (code) {
8,057,194✔
1444
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1445
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1446
    metaFetchEntryFree(&pChild);
×
1447
    metaFetchEntryFree(&pSuper);
×
1448
    TAOS_RETURN(code);
×
1449
  } else {
1450
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
8,057,194✔
1451
             pChild->uid, version);
1452
  }
1453

1454
  // free resource and return
1455
  metaFetchEntryFree(&pChild);
8,057,194✔
1456
  metaFetchEntryFree(&pSuper);
8,057,194✔
1457
  TAOS_RETURN(code);
8,057,194✔
1458
}
1459

1460
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,445✔
1461
  int32_t code = 0;
3,445✔
1462

1463
  // check tag name
1464
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,445✔
1465
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1466
              __FILE__, __LINE__, version);
1467
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1468
  }
1469

1470
  // check name
1471
  void   *value = NULL;
3,445✔
1472
  int32_t valueSize = 0;
3,445✔
1473
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,445✔
1474
  if (code) {
3,445✔
1475
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1476
              __FILE__, __LINE__, pReq->tbName, version);
1477
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1478
    TAOS_RETURN(code);
×
1479
  }
1480
  tdbFreeClear(value);
3,445✔
1481

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

1488
  TAOS_RETURN(code);
3,445✔
1489
}
1490

1491
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,445✔
1492
  int32_t code = TSDB_CODE_SUCCESS;
3,445✔
1493

1494
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,445✔
1495
  if (code) {
3,445✔
1496
    TAOS_RETURN(code);
×
1497
  }
1498

1499
  // fetch child entry
1500
  SMetaEntry *pChild = NULL;
3,445✔
1501
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,445✔
1502
  if (code) {
3,445✔
1503
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1504
              __FILE__, __LINE__, pReq->tbName, version);
1505
    TAOS_RETURN(code);
×
1506
  }
1507

1508
  if (pChild->type != TSDB_CHILD_TABLE) {
3,445✔
1509
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1510
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1511
    metaFetchEntryFree(&pChild);
×
1512
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1513
  }
1514

1515
  // fetch super entry
1516
  SMetaEntry *pSuper = NULL;
3,445✔
1517
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,445✔
1518
  if (code) {
3,445✔
1519
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1520
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1521
    metaFetchEntryFree(&pChild);
×
1522
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1523
  }
1524

1525
  // search the tags to update
1526
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,445✔
1527

1528
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,445✔
1529
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1530
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1531
    metaFetchEntryFree(&pChild);
×
1532
    metaFetchEntryFree(&pSuper);
×
1533
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1534
  }
1535

1536
  // do check if tag name exists
1537
  SHashObj *pTagTable =
1538
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
3,445✔
1539
  if (pTagTable == NULL) {
3,445✔
1540
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1541
              __LINE__, tstrerror(terrno), version);
1542
    metaFetchEntryFree(&pChild);
×
1543
    metaFetchEntryFree(&pSuper);
×
1544
    TAOS_RETURN(terrno);
×
1545
  }
1546

1547
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
24,115✔
1548
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
20,670✔
1549
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
20,670✔
1550
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1551
                __LINE__, tstrerror(terrno), version);
1552
      taosHashCleanup(pTagTable);
×
1553
      metaFetchEntryFree(&pChild);
×
1554
      metaFetchEntryFree(&pSuper);
×
1555
      TAOS_RETURN(terrno);
×
1556
    }
1557
  }
1558

1559
  int32_t numOfChangedTags = 0;
3,445✔
1560
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,560✔
1561
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
24,115✔
1562
        ? numOfChangedTags++
20,670✔
1563
        : 0;
24,115✔
1564
  }
1565
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,445✔
1566
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1567
              __FILE__, __LINE__, version);
1568
    taosHashCleanup(pTagTable);
×
1569
    metaFetchEntryFree(&pChild);
×
1570
    metaFetchEntryFree(&pSuper);
×
1571
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1572
  }
1573

1574
  // do change tag value
1575
  pChild->version = version;
3,445✔
1576
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,445✔
1577
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,445✔
1578
  if (NULL == pTagArray) {
3,445✔
1579
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1580
              __LINE__, tstrerror(terrno), version);
1581
    taosHashCleanup(pTagTable);
×
1582
    metaFetchEntryFree(&pChild);
×
1583
    metaFetchEntryFree(&pSuper);
×
1584
    TAOS_RETURN(terrno);
×
1585
  }
1586

1587
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,560✔
1588
    SSchema *pCol = &pTagSchema->pSchema[i];
24,115✔
1589
    STagVal  value = {
24,115✔
1590
         .cid = pCol->colId,
24,115✔
1591
    };
1592

1593
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
24,115✔
1594
    if (pTagVal == NULL) {
24,115✔
1595
      if (!tTagGet(pOldTag, &value)) {
3,445✔
1596
        continue;
×
1597
      }
1598
    } else {
1599
      value.type = pCol->type;
20,670✔
1600
      if (pTagVal->isNull) {
20,670✔
1601
        continue;
4,134✔
1602
      }
1603

1604
      if (IS_VAR_DATA_TYPE(pCol->type)) {
16,536✔
1605
        value.pData = pTagVal->pTagVal;
2,756✔
1606
        value.nData = pTagVal->nTagVal;
2,756✔
1607
      } else {
1608
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
13,780✔
1609
      }
1610
    }
1611

1612
    if (taosArrayPush(pTagArray, &value) == NULL) {
19,981✔
1613
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1614
                __LINE__, tstrerror(terrno), version);
1615
      taosHashCleanup(pTagTable);
×
1616
      taosArrayDestroy(pTagArray);
×
1617
      metaFetchEntryFree(&pChild);
×
1618
      metaFetchEntryFree(&pSuper);
×
1619
      TAOS_RETURN(terrno);
×
1620
    }
1621
  }
1622

1623
  STag *pNewTag = NULL;
3,445✔
1624
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
3,445✔
1625
  if (code) {
3,445✔
1626
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1627
              __LINE__, tstrerror(code), version);
1628
    taosHashCleanup(pTagTable);
×
1629
    taosArrayDestroy(pTagArray);
×
1630
    metaFetchEntryFree(&pChild);
×
1631
    metaFetchEntryFree(&pSuper);
×
1632
    TAOS_RETURN(code);
×
1633
  }
1634
  taosArrayDestroy(pTagArray);
3,445✔
1635
  taosMemoryFree(pChild->ctbEntry.pTags);
3,445✔
1636
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
3,445✔
1637

1638
  // do handle entry
1639
  code = metaHandleEntry2(pMeta, pChild);
3,445✔
1640
  if (code) {
3,445✔
1641
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1642
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1643
    taosHashCleanup(pTagTable);
×
1644
    metaFetchEntryFree(&pChild);
×
1645
    metaFetchEntryFree(&pSuper);
×
1646
    TAOS_RETURN(code);
×
1647
  } else {
1648
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,445✔
1649
             pChild->uid, version);
1650
  }
1651

1652
  taosHashCleanup(pTagTable);
3,445✔
1653
  metaFetchEntryFree(&pChild);
3,445✔
1654
  metaFetchEntryFree(&pSuper);
3,445✔
1655
  TAOS_RETURN(code);
3,445✔
1656
}
1657

1658
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
17,602✔
1659
  int32_t code = TSDB_CODE_SUCCESS;
17,602✔
1660

1661
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
17,602✔
1662
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1663
              __FILE__, __LINE__, version);
1664
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1665
  }
1666

1667
  return code;
17,602✔
1668
}
1669

1670
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
17,602✔
1671
  int32_t code = 0;
17,602✔
1672

1673
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
17,602✔
1674
  if (code) {
17,602✔
1675
    TAOS_RETURN(code);
×
1676
  }
1677

1678
  // fetch entry
1679
  SMetaEntry *pEntry = NULL;
17,602✔
1680
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
17,602✔
1681
  if (code) {
17,602✔
1682
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1683
              __FILE__, __LINE__, pReq->tbName, version);
1684
    TAOS_RETURN(code);
×
1685
  }
1686

1687
  // do change the entry
1688
  pEntry->version = version;
17,602✔
1689
  if (pEntry->type == TSDB_CHILD_TABLE) {
17,602✔
1690
    if (pReq->updateTTL) {
8,593✔
1691
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
3,695✔
1692
    }
1693
    if (pReq->newCommentLen >= 0) {
8,593✔
1694
      char *pNewComment = NULL;
4,898✔
1695
      if (pReq->newCommentLen) {
4,898✔
1696
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
2,689✔
1697
        if (NULL == pNewComment) {
2,689✔
1698
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1699
                    __LINE__, tstrerror(terrno), version);
1700
          metaFetchEntryFree(&pEntry);
×
1701
          TAOS_RETURN(terrno);
×
1702
        }
1703
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
2,689✔
1704
      } else {
1705
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,209✔
1706
      }
1707
      pEntry->ctbEntry.comment = pNewComment;
4,898✔
1708
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
4,898✔
1709
    }
1710
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
9,009✔
1711
    if (pReq->updateTTL) {
9,009✔
1712
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,241✔
1713
    }
1714
    if (pReq->newCommentLen >= 0) {
9,009✔
1715
      char *pNewComment = NULL;
4,768✔
1716
      if (pReq->newCommentLen > 0) {
4,768✔
1717
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
2,559✔
1718
        if (NULL == pNewComment) {
2,559✔
1719
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1720
                    __LINE__, tstrerror(terrno), version);
1721
          metaFetchEntryFree(&pEntry);
×
1722
          TAOS_RETURN(terrno);
×
1723
        }
1724
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
2,559✔
1725
      } else {
1726
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,209✔
1727
      }
1728
      pEntry->ntbEntry.comment = pNewComment;
4,768✔
1729
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
4,768✔
1730
    }
1731
  } else {
1732
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1733
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1734
    metaFetchEntryFree(&pEntry);
×
1735
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1736
  }
1737

1738
  // do handle entry
1739
  code = metaHandleEntry2(pMeta, pEntry);
17,602✔
1740
  if (code) {
17,602✔
1741
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1742
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1743
    metaFetchEntryFree(&pEntry);
×
1744
    TAOS_RETURN(code);
×
1745
  } else {
1746
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
17,602✔
1747
             pEntry->uid, version);
1748
  }
1749

1750
  metaFetchEntryFree(&pEntry);
17,602✔
1751
  TAOS_RETURN(code);
17,602✔
1752
}
1753

1754
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,561✔
1755
  int32_t code = TSDB_CODE_SUCCESS;
6,561✔
1756

1757
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,561✔
1758
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1759
              __FILE__, __LINE__, version);
1760
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1761
  }
1762

1763
  SMetaEntry *pEntry = NULL;
6,561✔
1764
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,561✔
1765
  if (code) {
6,561✔
1766
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1767
              __FILE__, __LINE__, pReq->tbName, version);
1768
    TAOS_RETURN(code);
×
1769
  }
1770

1771
  if (pEntry->version >= version) {
6,561✔
1772
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1773
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1774
    metaFetchEntryFree(&pEntry);
×
1775
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1776
  }
1777

1778
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,561✔
1779
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1780
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1781
    metaFetchEntryFree(&pEntry);
×
1782
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1783
  }
1784

1785
  // do change the entry
1786
  int8_t           updated = 0;
6,561✔
1787
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,561✔
1788
  for (int32_t i = 0; i < wp->nCols; i++) {
52,488✔
1789
    SColCmpr *p = &wp->pColCmpr[i];
45,927✔
1790
    if (p->id == pReq->colId) {
45,927✔
1791
      uint32_t dst = 0;
6,561✔
1792
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,561✔
1793
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1794
      if (updated > 0) {
6,561✔
1795
        p->alg = dst;
6,561✔
1796
      }
1797
    }
1798
  }
1799

1800
  if (updated == 0) {
6,561✔
1801
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1802
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1803
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1804
    metaFetchEntryFree(&pEntry);
×
1805
    TAOS_RETURN(code);
×
1806
  } else if (updated < 0) {
6,561✔
1807
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1808
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1809
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1810
    metaFetchEntryFree(&pEntry);
×
1811
    TAOS_RETURN(code);
×
1812
  }
1813

1814
  pEntry->version = version;
6,561✔
1815

1816
  // do handle entry
1817
  code = metaHandleEntry2(pMeta, pEntry);
6,561✔
1818
  if (code) {
6,561✔
1819
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1820
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1821
    metaFetchEntryFree(&pEntry);
×
1822
    TAOS_RETURN(code);
×
1823
  } else {
1824
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
6,561✔
1825
             pEntry->uid, version);
1826
  }
1827

1828
  metaFetchEntryFree(&pEntry);
6,561✔
1829
  TAOS_RETURN(code);
6,561✔
1830
}
1831

1832
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
81,787✔
1833
  int32_t code = TSDB_CODE_SUCCESS;
81,787✔
1834

1835
  // check request
1836
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
81,787✔
1837
  if (code) {
81,787✔
1838
    TAOS_RETURN(code);
×
1839
  }
1840

1841
  if (NULL == pReq->refDbName) {
81,787✔
1842
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1843
              __func__, __FILE__, __LINE__, version);
1844
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1845
  }
1846

1847
  if (NULL == pReq->refTbName) {
81,787✔
1848
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1849
              __func__, __FILE__, __LINE__, version);
1850
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1851
  }
1852

1853
  if (NULL == pReq->refColName) {
81,787✔
1854
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1855
              __func__, __FILE__, __LINE__, version);
1856
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1857
  }
1858

1859
  // fetch old entry
1860
  SMetaEntry *pEntry = NULL;
81,787✔
1861
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
81,787✔
1862
  if (code) {
81,787✔
1863
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1864
              __FILE__, __LINE__, pReq->tbName, version);
1865
    TAOS_RETURN(code);
×
1866
  }
1867

1868
  if (pEntry->version >= version) {
81,787✔
1869
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1870
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1871
    metaFetchEntryFree(&pEntry);
×
1872
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1873
  }
1874

1875
  // fetch super entry
1876
  SMetaEntry *pSuper = NULL;
81,787✔
1877
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
81,787✔
1878
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
30,141✔
1879
    if (code) {
30,141✔
1880
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1881
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1882
      metaFetchEntryFree(&pEntry);
×
1883
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1884
    }
1885
  }
1886

1887
  // search the column to update
1888
  SSchemaWrapper *pSchema =
81,787✔
1889
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
81,787✔
1890
  SColRef *pColRef = NULL;
81,787✔
1891
  int32_t  iColumn = 0;
81,787✔
1892
  for (int32_t i = 0; i < pSchema->nCols; i++) {
401,503✔
1893
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
401,503✔
1894
      pColRef = &pEntry->colRef.pColRef[i];
81,787✔
1895
      iColumn = i;
81,787✔
1896
      break;
81,787✔
1897
    }
1898
  }
1899

1900
  if (NULL == pColRef) {
81,787✔
1901
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1902
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1903
    metaFetchEntryFree(&pEntry);
×
1904
    metaFetchEntryFree(&pSuper);
×
1905
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1906
  }
1907

1908
  // do update column name
1909
  pEntry->version = version;
81,787✔
1910
  pColRef->hasRef = true;
81,787✔
1911
  pColRef->id = pSchema->pSchema[iColumn].colId;
81,787✔
1912
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
81,787✔
1913
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
81,787✔
1914
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
81,787✔
1915
  pSchema->version++;
81,787✔
1916
  pEntry->colRef.version++;
81,787✔
1917

1918
  // do handle entry
1919
  code = metaHandleEntry2(pMeta, pEntry);
81,787✔
1920
  if (code) {
81,787✔
1921
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1922
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1923
    metaFetchEntryFree(&pEntry);
×
1924
    metaFetchEntryFree(&pSuper);
×
1925
    TAOS_RETURN(code);
×
1926
  } else {
1927
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
81,787✔
1928
             pEntry->uid, version);
1929
  }
1930

1931
  // build response
1932
  if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
81,787✔
1933
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1934
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1935
  } else {
1936
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
603,517✔
1937
      SColRef *p = &pEntry->colRef.pColRef[i];
521,730✔
1938
      pRsp->pColRefs[i].hasRef = p->hasRef;
521,730✔
1939
      pRsp->pColRefs[i].id = p->id;
521,730✔
1940
      if (p->hasRef) {
521,730✔
1941
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
359,439✔
1942
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
359,439✔
1943
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
359,439✔
1944
      }
1945
    }
1946
  }
1947

1948
  metaFetchEntryFree(&pEntry);
81,787✔
1949
  metaFetchEntryFree(&pSuper);
81,787✔
1950
  TAOS_RETURN(code);
81,787✔
1951
}
1952

1953
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
59,076✔
1954
  int32_t code = TSDB_CODE_SUCCESS;
59,076✔
1955

1956
  // check request
1957
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
59,076✔
1958
  if (code) {
59,076✔
1959
    TAOS_RETURN(code);
×
1960
  }
1961

1962
  // fetch old entry
1963
  SMetaEntry *pEntry = NULL;
59,076✔
1964
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
59,076✔
1965
  if (code) {
59,076✔
1966
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1967
              __FILE__, __LINE__, pReq->tbName, version);
1968
    TAOS_RETURN(code);
×
1969
  }
1970

1971
  if (pEntry->version >= version) {
59,076✔
1972
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1973
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1974
    metaFetchEntryFree(&pEntry);
×
1975
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1976
  }
1977

1978
  // fetch super entry
1979
  SMetaEntry *pSuper = NULL;
59,076✔
1980
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
59,076✔
1981
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
29,934✔
1982
    if (code) {
29,934✔
1983
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1984
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1985
      metaFetchEntryFree(&pEntry);
×
1986
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1987
    }
1988
  }
1989

1990
  // search the column to update
1991
  SSchemaWrapper *pSchema =
59,076✔
1992
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
59,076✔
1993
  SColRef *pColRef = NULL;
59,076✔
1994
  int32_t  iColumn = 0;
59,076✔
1995
  for (int32_t i = 0; i < pSchema->nCols; i++) {
239,841✔
1996
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
239,841✔
1997
      pColRef = &pEntry->colRef.pColRef[i];
59,076✔
1998
      iColumn = i;
59,076✔
1999
      break;
59,076✔
2000
    }
2001
  }
2002

2003
  if (NULL == pColRef) {
59,076✔
2004
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2005
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2006
    metaFetchEntryFree(&pEntry);
×
2007
    metaFetchEntryFree(&pSuper);
×
2008
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2009
  }
2010

2011
  // do update column name
2012
  pEntry->version = version;
59,076✔
2013
  pColRef->hasRef = false;
59,076✔
2014
  pColRef->id = pSchema->pSchema[iColumn].colId;
59,076✔
2015
  pSchema->version++;
59,076✔
2016
  pEntry->colRef.version++;
59,076✔
2017

2018
  // do handle entry
2019
  code = metaHandleEntry2(pMeta, pEntry);
59,076✔
2020
  if (code) {
59,076✔
2021
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2022
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2023
    metaFetchEntryFree(&pEntry);
×
2024
    metaFetchEntryFree(&pSuper);
×
2025
    TAOS_RETURN(code);
×
2026
  } else {
2027
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
59,076✔
2028
             pEntry->uid, version);
2029
  }
2030

2031
  // build response
2032
  if (metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
59,076✔
2033
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2034
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2035
  } else {
2036
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
361,530✔
2037
      SColRef *p = &pEntry->colRef.pColRef[i];
302,454✔
2038
      pRsp->pColRefs[i].hasRef = p->hasRef;
302,454✔
2039
      pRsp->pColRefs[i].id = p->id;
302,454✔
2040
      if (p->hasRef) {
302,454✔
2041
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
149,364✔
2042
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
149,364✔
2043
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
149,364✔
2044
      }
2045
    }
2046
  }
2047

2048
  metaFetchEntryFree(&pEntry);
59,076✔
2049
  metaFetchEntryFree(&pSuper);
59,076✔
2050
  TAOS_RETURN(code);
59,076✔
2051
}
2052

2053
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,956✔
2054
  int32_t code = TSDB_CODE_SUCCESS;
5,956✔
2055

2056
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
5,956✔
2057
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2058
              __FILE__, __LINE__, version);
2059
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2060
  }
2061

2062
  SMetaEntry *pEntry = NULL;
5,956✔
2063
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
5,956✔
2064
  if (code) {
5,956✔
2065
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2066
              __FILE__, __LINE__, pReq->name, version);
2067
    TAOS_RETURN(code);
×
2068
  }
2069

2070
  if (pEntry->type != TSDB_SUPER_TABLE) {
5,956✔
2071
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2072
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2073
    metaFetchEntryFree(&pEntry);
×
2074
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2075
  }
2076

2077
  if (pEntry->uid != pReq->suid) {
5,956✔
2078
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2079
              ", version:%" PRId64,
2080
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2081
    metaFetchEntryFree(&pEntry);
×
2082
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2083
  }
2084

2085
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2086
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2087
  //   PRId64,
2088
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2089
  //             pReq->schemaTag.version, version);
2090
  //   metaFetchEntryFree(&pEntry);
2091
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2092
  // }
2093

2094
  // do change the entry
2095
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
5,956✔
2096
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
5,956✔
2097
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5,956✔
2098
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2099
              __func__, __FILE__, __LINE__, pReq->name, version);
2100
    metaFetchEntryFree(&pEntry);
×
2101
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2102
  }
2103

2104
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
5,956✔
2105
    metaError(
×
2106
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2107
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2108
        version);
2109
    metaFetchEntryFree(&pEntry);
×
2110
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2111
  }
2112

2113
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2114
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2115
  //   PRId64,
2116
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2117
  //             pNewTagSchema->version, version);
2118
  //   metaFetchEntryFree(&pEntry);
2119
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2120
  // }
2121

2122
  int32_t numOfChangedTags = 0;
5,956✔
2123
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
30,328✔
2124
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
24,372✔
2125
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
24,372✔
2126

2127
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
24,372✔
2128
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
24,372✔
2129
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2130
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2131
      metaFetchEntryFree(&pEntry);
×
2132
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2133
    }
2134

2135
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
24,372✔
2136
      numOfChangedTags++;
5,956✔
2137
      SSCHMEA_SET_IDX_ON(pOldColumn);
5,956✔
2138
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
18,416✔
2139
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2140
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2141
      metaFetchEntryFree(&pEntry);
×
2142
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2143
    }
2144
  }
2145

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

2154
  pEntry->version = version;
5,956✔
2155
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
5,956✔
2156

2157
  // do handle the entry
2158
  code = metaHandleEntry2(pMeta, pEntry);
5,956✔
2159
  if (code) {
5,956✔
2160
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2161
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2162
    metaFetchEntryFree(&pEntry);
×
2163
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2164
  } else {
2165
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
5,956✔
2166
             pEntry->uid, version);
2167
  }
2168

2169
  metaFetchEntryFree(&pEntry);
5,956✔
2170
  TAOS_RETURN(code);
5,956✔
2171
}
2172

2173
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
3,958✔
2174
  int32_t code = TSDB_CODE_SUCCESS;
3,958✔
2175

2176
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
3,958✔
2177
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2178
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2179
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2180
  }
2181

2182
  SMetaEntry *pEntry = NULL;
3,958✔
2183
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
3,958✔
2184
  if (code) {
3,958✔
2185
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2186
              __FILE__, __LINE__, pReq->stb, version);
2187
    TAOS_RETURN(code);
×
2188
  }
2189

2190
  if (TSDB_SUPER_TABLE != pEntry->type) {
3,958✔
2191
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2192
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2193
    metaFetchEntryFree(&pEntry);
×
2194
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2195
  }
2196

2197
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
3,958✔
2198
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,958✔
2199
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2200
              __func__, __FILE__, __LINE__, pReq->stb, version);
2201
    metaFetchEntryFree(&pEntry);
×
2202
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2203
  }
2204

2205
  // search and set the tag index off
2206
  int32_t numOfChangedTags = 0;
3,958✔
2207
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
10,362✔
2208
    SSchema *pCol = pTagSchema->pSchema + i;
10,362✔
2209
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
10,362✔
2210
      if (!IS_IDX_ON(pCol)) {
3,958✔
2211
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2212
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2213
        metaFetchEntryFree(&pEntry);
×
2214
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2215
      }
2216
      numOfChangedTags++;
3,958✔
2217
      SSCHMEA_SET_IDX_OFF(pCol);
3,958✔
2218
      break;
3,958✔
2219
    }
2220
  }
2221

2222
  if (numOfChangedTags != 1) {
3,958✔
2223
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2224
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2225
    metaFetchEntryFree(&pEntry);
×
2226
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2227
  }
2228

2229
  // do handle the entry
2230
  pEntry->version = version;
3,958✔
2231
  pTagSchema->version++;
3,958✔
2232
  code = metaHandleEntry2(pMeta, pEntry);
3,958✔
2233
  if (code) {
3,958✔
2234
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2235
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2236
    metaFetchEntryFree(&pEntry);
×
2237
    TAOS_RETURN(code);
×
2238
  } else {
2239
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
3,958✔
2240
             pEntry->uid, version);
2241
  }
2242

2243
  metaFetchEntryFree(&pEntry);
3,958✔
2244
  TAOS_RETURN(code);
3,958✔
2245
}
2246

2247
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
8,826,189✔
2248
  int32_t code = TSDB_CODE_SUCCESS;
8,826,189✔
2249

2250
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
8,826,189✔
2251
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
16,457✔
2252
              __FILE__, __LINE__, version);
2253
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
16,457✔
2254
  }
2255

2256
  SMetaEntry *pEntry = NULL;
8,814,157✔
2257
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
8,824,246✔
2258
  if (code) {
8,820,906✔
2259
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2260
              __FILE__, __LINE__, pReq->name, version);
2261
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2262
  }
2263

2264
  if (pEntry->type != TSDB_SUPER_TABLE) {
8,820,906✔
2265
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2266
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2267
    metaFetchEntryFree(&pEntry);
×
2268
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2269
  }
2270

2271
  SMetaEntry entry = {
26,451,272✔
2272
      .version = version,
2273
      .type = TSDB_SUPER_TABLE,
2274
      .uid = pReq->suid,
8,824,870✔
2275
      .name = pReq->name,
8,817,977✔
2276
      .stbEntry.schemaRow = pReq->schemaRow,
2277
      .stbEntry.schemaTag = pReq->schemaTag,
2278
      .stbEntry.keep = pReq->keep,
8,805,414✔
2279
      .colCmpr = pReq->colCmpr,
2280
      .pExtSchemas = pReq->pExtSchemas,
8,819,054✔
2281
  };
2282
  TABLE_SET_COL_COMPRESSED(entry.flags);
8,816,084✔
2283
  if (pReq->virtualStb) {
8,816,084✔
2284
    TABLE_SET_VIRTUAL(entry.flags);
16,755✔
2285
  }
2286
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
8,813,267✔
2287
    TABLE_SET_ROLLUP(entry.flags);
4,632✔
2288
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,632✔
2289
  }
2290

2291
  // do handle the entry
2292
  code = metaHandleEntry2(pMeta, &entry);
8,815,185✔
2293
  if (code) {
8,815,709✔
2294
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2295
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2296
    metaFetchEntryFree(&pEntry);
×
2297
    TAOS_RETURN(code);
×
2298
  } else {
2299
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
8,815,709✔
2300
             pReq->suid, version);
2301
  }
2302

2303
  metaFetchEntryFree(&pEntry);
8,827,147✔
2304
  TAOS_RETURN(code);
8,833,163✔
2305
}
2306

2307
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2308
  int32_t code = 0;
×
2309

2310
  if (taosArrayGetSize(uidArray) == 0) {
×
2311
    return TSDB_CODE_SUCCESS;
×
2312
  }
2313

2314
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2315
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2316
    SMetaInfo info;
×
2317
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2318
    if (code) {
×
2319
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2320
                __func__, __FILE__, __LINE__, uid, code);
2321
      return code;
×
2322
    }
2323

2324
    SMetaEntry entry = {
×
2325
        .version = version,
2326
        .uid = uid,
2327
    };
2328

2329
    if (info.suid == 0) {
×
2330
      entry.type = -TSDB_NORMAL_TABLE;
×
2331
    } else if (info.suid == uid) {
×
2332
      entry.type = -TSDB_SUPER_TABLE;
×
2333
    } else {
2334
      entry.type = -TSDB_CHILD_TABLE;
×
2335
    }
2336
    code = metaHandleEntry2(pMeta, &entry);
×
2337
    if (code) {
×
2338
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2339
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2340
      return code;
×
2341
    }
2342
  }
2343
  return code;
×
2344
}
2345

2346
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
22,388✔
2347
  int32_t code = TSDB_CODE_SUCCESS;
22,388✔
2348

2349
  if (NULL == pReq->name || pReq->name[0] == 0) {
22,388✔
2350
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2351
              __LINE__, version);
2352
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2353
  }
2354

2355
  SMetaEntry *pEntry = NULL;
23,160✔
2356
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,160✔
2357
  if (code) {
23,160✔
2358
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2359
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2360
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2361
  }
2362

2363
  if (pEntry->type != TSDB_SUPER_TABLE) {
23,160✔
2364
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2365
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2366
    metaFetchEntryFree(&pEntry);
×
2367
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2368
  }
2369

2370
  if (pEntry->uid != pReq->tbUid) {
23,160✔
2371
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2372
              ", version:%" PRId64,
2373
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2374
    metaFetchEntryFree(&pEntry);
×
2375
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2376
  }
2377

2378
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
23,160✔
2379
    // overwrite the old rsma definition if exists
2380
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2381
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2382
  } else {
2383
    TABLE_SET_ROLLUP(pEntry->flags);
22,388✔
2384
  }
2385

2386
  SMetaEntry entry = *pEntry;
23,160✔
2387
  entry.version = version;
22,388✔
2388
  entry.stbEntry.rsmaParam.name = pReq->name;
22,388✔
2389
  entry.stbEntry.rsmaParam.uid = pReq->uid;
23,160✔
2390
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
22,388✔
2391
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
23,160✔
2392
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
23,160✔
2393
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
22,388✔
2394
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
22,388✔
2395
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
22,388✔
2396

2397
  // do handle the entry
2398
  code = metaHandleEntry2(pMeta, &entry);
23,160✔
2399
  if (code) {
23,160✔
2400
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2401
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2402
    metaFetchEntryFree(&pEntry);
×
2403
    TAOS_RETURN(code);
×
2404
  } else {
2405
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
23,160✔
2406
    pMeta->pVnode->config.isRsma = 1;
23,160✔
2407
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
23,160✔
2408
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2409
  }
2410

2411
  metaFetchEntryFree(&pEntry);
23,160✔
2412
  TAOS_RETURN(code);
23,160✔
2413
}
2414

2415
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,632✔
2416
  int32_t code = TSDB_CODE_SUCCESS;
4,632✔
2417

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

2424
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
4,632✔
2425
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2426
              __LINE__, version);
2427
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2428
  }
2429

2430
  SMetaEntry *pEntry = NULL;
4,632✔
2431
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,632✔
2432
  if (code) {
4,632✔
2433
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2434
             __func__, __LINE__, pReq->name, pReq->tbName, version);
2435
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2436
  }
2437

2438
  if (pEntry->type != pReq->tbType) {
4,632✔
2439
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2440
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2441
    metaFetchEntryFree(&pEntry);
×
2442
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2443
  }
2444

2445
  if (pEntry->uid != pReq->tbUid) {
4,632✔
2446
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2447
              ", version:%" PRId64,
2448
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2449
    metaFetchEntryFree(&pEntry);
×
2450
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2451
  }
2452

2453
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
4,632✔
2454
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,632✔
2455
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,632✔
2456
      metaError(
×
2457
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2458
          "uid:%" PRIi64 ", version:%" PRId64,
2459
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2460
          pReq->uid, version);
2461
      metaFetchEntryFree(&pEntry);
×
2462
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2463
    }
2464
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
4,632✔
2465
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,632✔
2466
  } else {
2467
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2468
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
2469
    metaFetchEntryFree(&pEntry);
×
2470
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2471
  }
2472

2473
  SMetaEntry entry = *pEntry;
4,632✔
2474
  entry.version = version;
4,632✔
2475
  TABLE_RESET_ROLLUP(entry.flags);
4,632✔
2476
  entry.stbEntry.rsmaParam.uid = 0;
4,632✔
2477
  entry.stbEntry.rsmaParam.name = NULL;
4,632✔
2478
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,632✔
2479
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,632✔
2480
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,632✔
2481

2482
  // do handle the entry
2483
  code = metaHandleEntry2(pMeta, &entry);
4,632✔
2484
  if (code) {
4,632✔
2485
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2486
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
2487
    metaFetchEntryFree(&pEntry);
×
2488
    TAOS_RETURN(code);
×
2489
  } else {
2490
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
4,632✔
2491
      pMeta->pVnode->config.isRsma = 0;
×
2492
    }
2493
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
4,632✔
2494
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2495
  }
2496

2497
  metaFetchEntryFree(&pEntry);
4,632✔
2498
  TAOS_RETURN(code);
4,632✔
2499
}
2500

2501
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,352✔
2502
  int32_t code = TSDB_CODE_SUCCESS;
12,352✔
2503

2504
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,352✔
2505
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2506
              __LINE__, version);
2507
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2508
  }
2509

2510
  SMetaEntry *pEntry = NULL;
12,352✔
2511
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
12,352✔
2512
  if (code) {
12,352✔
2513
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2514
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2515
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2516
  }
2517

2518
  if (pEntry->uid != pReq->tbUid) {
12,352✔
2519
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2520
              ", version:%" PRId64,
2521
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2522
    metaFetchEntryFree(&pEntry);
×
2523
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2524
  }
2525

2526
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
12,352✔
2527
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
12,352✔
2528
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,352✔
2529
  } else {
2530
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2531
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2532
    metaFetchEntryFree(&pEntry);
×
2533
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2534
  }
2535

2536
  SMetaEntry entry = *pEntry;
12,352✔
2537
  entry.version = version;
12,352✔
2538
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
12,352✔
2539
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
12,352✔
2540
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
12,352✔
2541
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
12,352✔
2542
  }
2543
  // do handle the entry
2544
  code = metaHandleEntry2(pMeta, &entry);
12,352✔
2545
  if (code) {
12,352✔
2546
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2547
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2548
    metaFetchEntryFree(&pEntry);
×
2549
    TAOS_RETURN(code);
×
2550
  } else {
2551
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
12,352✔
2552
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2553
  }
2554

2555
  metaFetchEntryFree(&pEntry);
12,352✔
2556
  TAOS_RETURN(code);
12,352✔
2557
}
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