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

taosdata / TDengine / #4895

23 Dec 2025 01:08PM UTC coverage: 65.513% (-0.2%) from 65.72%
#4895

push

travis-ci

web-flow
fix: mem leak (#34023)

6 of 9 new or added lines in 1 file covered. (66.67%)

7770 existing lines in 123 files now uncovered.

184705 of 281937 relevant lines covered (65.51%)

112009834.14 hits per line

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

67.76
/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) {
7,061,389✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
7,061,389✔
31
  void     *value = NULL;
7,060,959✔
32
  int32_t   valueSize = 0;
7,062,014✔
33
  SMetaInfo info;
7,061,173✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,061,906✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
96✔
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);
7,060,783✔
43
  if (r == 0) {  // name exists, check uid and type
7,052,916✔
44
    int64_t uid = *(tb_uid_t *)value;
3,152✔
45
    tdbFree(value);
3,152✔
46

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

UNCOV
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

UNCOV
61
    if (info.uid == info.suid) {
×
UNCOV
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) {
7,049,764✔
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;
7,054,632✔
79
}
80

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

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

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

117
  return code;
991,241✔
118
}
119

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

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

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

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

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
7,051,930✔
169
  if (code != TSDB_CODE_SUCCESS) {
7,058,194✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
3,152✔
UNCOV
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
×
172
               pReq->name, pReq->suid, version);
UNCOV
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
×
174
    } else {
175
      TAOS_RETURN(code);
3,152✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
14,104,531✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
7,053,129✔
184
      .name = pReq->name,
7,051,092✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
7,048,498✔
188
  };
189
  if (pReq->rollup) {
7,045,044✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
7,053,212✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
7,051,271✔
195
    entry.colCmpr = pReq->colCmpr;
7,051,271✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
7,039,531✔
199

200
  if (pReq->virtualStb) {
7,046,034✔
201
    TABLE_SET_VIRTUAL(entry.flags);
57,479✔
202
  }
203

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

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
1,016,445✔
221
  if (code) {
1,013,026✔
222
    TAOS_RETURN(code);
1,576✔
223
  }
224

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

242
// Alter Super Table
243

244
// Create Child Table
245
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
53,537,301✔
246
  int32_t   code = TSDB_CODE_SUCCESS;
53,537,301✔
247
  void     *value = NULL;
53,537,301✔
248
  int32_t   valueSize = 0;
53,537,532✔
249
  SMetaInfo info;
53,535,119✔
250

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
53,535,898✔
252
      pReq->ctb.suid == 0) {
53,537,905✔
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
1,132✔
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,537,206✔
260
    pReq->uid = *(int64_t *)value;
701,371✔
261
    tdbFreeClear(value);
701,371✔
262

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

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

289
  // check super table existence
290
  SMetaEntry *pStbEntry = NULL;
52,834,746✔
291
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
52,834,009✔
292
  if (code) {
52,836,783✔
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,836,783✔
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,835,897✔
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,835,587✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
52,838,750✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
52,837,252✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
238,053,661✔
319
      STagVal tagVal = {
185,541,627✔
320
          .cid = pTagSchema->pSchema[i].colId,
185,539,967✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
185,543,432✔
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
137,859,627✔
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,833,058✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
52,828,961✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
52,835,897✔
339
    if (TSDB_CODE_SUCCESS != code) {
52,834,554✔
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,834,799✔
345
}
346

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

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

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
52,624,833✔
355
  if (NULL == ppRsp) {
52,620,192✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
52,620,192✔
360
  (*ppRsp)->tuid = pEntry->uid;
52,627,117✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
52,627,381✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
52,629,601✔
363

364
  return code;
52,628,500✔
365
}
366

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

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

380
  SMetaEntry entry = {
52,628,135✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
52,625,550✔
384
      .name = pReq->name,
52,626,939✔
385
      .ctbEntry.btime = pReq->btime,
52,620,852✔
386
      .ctbEntry.ttlDays = pReq->ttl,
52,621,481✔
387
      .ctbEntry.commentLen = pReq->commentLen,
52,628,880✔
388
      .ctbEntry.comment = pReq->comment,
52,622,405✔
389
      .ctbEntry.suid = pReq->ctb.suid,
52,619,325✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
52,620,395✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
52,628,169✔
395
  if (code) {
52,627,658✔
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,627,658✔
402
  if (TSDB_CODE_SUCCESS == code) {
52,629,892✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
52,630,461✔
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,633,720✔
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) {
15,696,014✔
419
  int32_t code = 0;
15,696,014✔
420
  void   *value = NULL;
15,696,014✔
421
  int32_t valueSize = 0;
15,696,014✔
422

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

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

446
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
15,537,552✔
447
  int32_t code = TSDB_CODE_SUCCESS;
15,537,552✔
448

449
  if (NULL == ppRsp) {
15,537,552✔
450
    return code;
×
451
  }
452

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

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
89,836,616✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
74,299,064✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
74,299,064✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
74,299,064✔
468
    if (pEntry->pExtSchemas) {
74,299,064✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
792,829✔
470
    }
471
  }
472

473
  return code;
15,537,552✔
474
}
475

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

479
  // check request
480
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
15,581,613✔
481
  if (code) {
15,581,613✔
482
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
44,061✔
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);
44,061✔
487
  }
488

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

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

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

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

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

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

543
  return code;
114,000✔
544
}
545

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

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

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

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

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

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

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

610
  return code;
207,424✔
611
}
612

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

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

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

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

658
// Drop Normal Table
659

660
// Alter Normal Table
661

662
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
69,232,071✔
663
  int32_t code = TSDB_CODE_SUCCESS;
69,232,071✔
664
  if (TSDB_CHILD_TABLE == pReq->type) {
69,232,071✔
665
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
53,331,808✔
666
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
15,903,839✔
667
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
15,581,613✔
668
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
322,226✔
669
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
114,401✔
670
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
207,825✔
671
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
207,825✔
672
  } else {
673
    code = TSDB_CODE_INVALID_MSG;
×
674
  }
675
  TAOS_RETURN(code);
69,238,591✔
676
}
677

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

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

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

698
  SMetaEntry entry = {
991,241✔
699
      .version = version,
700
      .uid = pReq->uid,
991,217✔
701
  };
702

703
  if (pReq->isVirtual) {
991,241✔
704
    if (pReq->suid == 0) {
58,003✔
705
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
29,214✔
706
    } else {
707
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
28,789✔
708
    }
709
  } else {
710
    if (pReq->suid == 0) {
933,238✔
711
      entry.type = -TSDB_NORMAL_TABLE;
523,571✔
712
    } else {
713
      entry.type = -TSDB_CHILD_TABLE;
409,667✔
714
    }
715
  }
716
  code = metaHandleEntry2(pMeta, &entry);
991,241✔
717
  if (code) {
991,241✔
718
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
719
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
720
  } else {
721
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
991,241✔
722
             pReq->uid, version);
723
  }
724
  TAOS_RETURN(code);
991,241✔
725
}
726

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

997
  tColumn = *pColumn;
97,659✔
998

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

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

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

1056
  // build response
1057
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
97,659✔
1058
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
31,516✔
1059
    if (code) {
31,516✔
1060
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1061
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1062
    } else {
1063
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
26,483,174✔
1064
        SColRef *p = &pEntry->colRef.pColRef[i];
26,451,658✔
1065
        pRsp->pColRefs[i].hasRef = p->hasRef;
26,451,658✔
1066
        pRsp->pColRefs[i].id = p->id;
26,451,658✔
1067
        if (p->hasRef) {
26,451,658✔
1068
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
13,238,355✔
1069
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
13,238,355✔
1070
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
13,238,355✔
1071
        }
1072
      }
1073
    }
1074
  } else {
1075
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
66,143✔
1076
    if (code) {
66,143✔
1077
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1078
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1079
    } else {
1080
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
85,644,328✔
1081
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
85,578,185✔
1082
        pRsp->pSchemaExt[i].colId = p->id;
85,578,185✔
1083
        pRsp->pSchemaExt[i].compress = p->alg;
85,578,185✔
1084
      }
1085
    }
1086
  }
1087

1088
  metaFetchEntryFree(&pEntry);
97,659✔
1089
  TAOS_RETURN(code);
97,659✔
1090
}
1091

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

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

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

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

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

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

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

1142

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1275
  // build response
1276
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
261,825✔
1277
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,324✔
1278
    if (code) {
28,324✔
1279
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1280
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1281
    } else {
1282
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
13,335,815✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
13,307,491✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
13,307,491✔
1285
        pRsp->pColRefs[i].id = p->id;
13,307,491✔
1286
        if (p->hasRef) {
13,307,491✔
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
83,118✔
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
83,118✔
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
83,118✔
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
233,501✔
1295
    if (code) {
233,501✔
1296
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1297
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1298
    } else {
1299
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,588,943✔
1300
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,355,442✔
1301
        pRsp->pSchemaExt[i].colId = p->id;
9,355,442✔
1302
        pRsp->pSchemaExt[i].compress = p->alg;
9,355,442✔
1303
      }
1304
    }
1305
  }
1306

1307
  metaFetchEntryFree(&pEntry);
261,825✔
1308
  TAOS_RETURN(code);
261,825✔
1309
}
1310

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

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

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

1333
  TAOS_RETURN(code);
8,277,091✔
1334
}
1335

1336
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1337

1338
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
8,167,721✔
1339
  if (isNull) {
8,167,721✔
1340
    if (!tTagGet(pOldTag, &value)) {
50,694✔
1341
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
21,591✔
1342
      return true;
21,591✔
1343
    }
1344
    return false;
29,103✔
1345
  }
1346
  if (!tTagGet(pOldTag, &value)){
8,117,027✔
1347
    return false;
194,354✔
1348
  }
1349
  if (IS_VAR_DATA_TYPE(value.type)) {
7,922,673✔
1350
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
61,044✔
1351
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
39,788✔
1352
      return true;
39,788✔
1353
    }
1354
  } else {
1355
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,861,629✔
1356
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
96,842✔
1357
      return true;
96,842✔
1358
    }
1359
  }
1360
  return false;
7,786,043✔
1361
}
1362

1363
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,277,115✔
1364
  int32_t code = TSDB_CODE_SUCCESS;
8,277,115✔
1365

1366
  // check request
1367
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
8,277,115✔
1368
  if (code) {
8,277,115✔
1369
    TAOS_RETURN(code);
24✔
1370
  }
1371

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

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

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

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

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

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

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

1443
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,296,131✔
1444
      STagVal value = {
9,297,847✔
1445
          .type = pTagSchema->pSchema[i].type,
9,297,847✔
1446
          .cid = pTagSchema->pSchema[i].colId,
9,297,847✔
1447
      };
1448

1449
      if (iColumn == i) {
9,297,847✔
1450
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
8,146,691✔
1451
          taosArrayDestroy(pTagArray);
148,407✔
1452
          metaFetchEntryFree(&pChild);
148,407✔
1453
          metaFetchEntryFree(&pSuper);
148,407✔
1454
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
148,407✔
1455
        }
1456
        if (pReq->isNull) {
7,998,284✔
1457
          continue;
27,000✔
1458
        }
1459
        if (IS_VAR_DATA_TYPE(value.type)) {
7,971,284✔
1460
          value.pData = pReq->pTagVal;
107,603✔
1461
          value.nData = pReq->nTagVal;
107,603✔
1462
        } else {
1463
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,863,681✔
1464
        }
1465
      } else if (!tTagGet(pOldTag, &value)) {
1,151,156✔
1466
        continue;
310,195✔
1467
      }
1468

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

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

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

1507
  // free resource and return
1508
  metaFetchEntryFree(&pChild);
8,128,684✔
1509
  metaFetchEntryFree(&pSuper);
8,128,684✔
1510
  TAOS_RETURN(code);
8,128,684✔
1511
}
1512

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1640
  bool allSame = true;
3,505✔
1641

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

1648
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
24,535✔
1649
    if (pTagVal == NULL) {
24,535✔
1650
      if (!tTagGet(pOldTag, &value)) {
3,505✔
1651
        continue;
×
1652
      }
1653
    } else {
1654
      value.type = pCol->type;
21,030✔
1655
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
21,030✔
1656
        allSame = false;
11,216✔
1657
      }
1658
      if (pTagVal->isNull) {
21,030✔
1659
        continue;
4,206✔
1660
      }
1661

1662
      if (IS_VAR_DATA_TYPE(pCol->type)) {
16,824✔
1663
        value.pData = pTagVal->pTagVal;
2,804✔
1664
        value.nData = pTagVal->nTagVal;
2,804✔
1665
      } else {
1666
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
14,020✔
1667
      }
1668
    }
1669

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

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

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

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

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

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

1734
  return code;
17,906✔
1735
}
1736

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

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

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

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

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

1817
  metaFetchEntryFree(&pEntry);
17,906✔
1818
  TAOS_RETURN(code);
17,906✔
1819
}
1820

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

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

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

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

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

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

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

1881
  pEntry->version = version;
6,753✔
1882

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

1895
  metaFetchEntryFree(&pEntry);
6,753✔
1896
  TAOS_RETURN(code);
6,753✔
1897
}
1898

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2191
  int32_t numOfChangedTags = 0;
5,896✔
2192
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
30,108✔
2193
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
24,212✔
2194
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
24,212✔
2195

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

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

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

2223
  pEntry->version = version;
5,896✔
2224
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
5,896✔
2225

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

2238
  metaFetchEntryFree(&pEntry);
5,896✔
2239
  TAOS_RETURN(code);
5,896✔
2240
}
2241

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

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

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

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

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

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

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

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

2312
  metaFetchEntryFree(&pEntry);
3,940✔
2313
  TAOS_RETURN(code);
3,940✔
2314
}
2315

2316
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
8,632,924✔
2317
  int32_t code = TSDB_CODE_SUCCESS;
8,632,924✔
2318

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

2325
  SMetaEntry *pEntry = NULL;
8,629,838✔
2326
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
8,632,534✔
2327
  if (code) {
8,631,604✔
UNCOV
2328
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2329
              __FILE__, __LINE__, pReq->name, version);
2330
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2331
  }
2332

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

2340
  SMetaEntry entry = {
25,866,710✔
2341
      .version = version,
2342
      .type = TSDB_SUPER_TABLE,
2343
      .uid = pReq->suid,
8,627,409✔
2344
      .name = pReq->name,
8,628,771✔
2345
      .stbEntry.schemaRow = pReq->schemaRow,
2346
      .stbEntry.schemaTag = pReq->schemaTag,
2347
      .stbEntry.keep = pReq->keep,
8,627,933✔
2348
      .colCmpr = pReq->colCmpr,
2349
      .pExtSchemas = pReq->pExtSchemas,
8,615,857✔
2350
  };
2351
  TABLE_SET_COL_COMPRESSED(entry.flags);
8,616,523✔
2352
  if (pReq->virtualStb) {
8,616,523✔
2353
    TABLE_SET_VIRTUAL(entry.flags);
20,846✔
2354
  }
2355
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
8,627,392✔
2356
    TABLE_SET_ROLLUP(entry.flags);
4,734✔
2357
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,734✔
2358
  }
2359

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

2372
  metaFetchEntryFree(&pEntry);
8,631,419✔
2373
  TAOS_RETURN(code);
8,638,234✔
2374
}
2375

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

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

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

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

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

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

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

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

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

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

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

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

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

2480
  metaFetchEntryFree(&pEntry);
23,670✔
2481
  TAOS_RETURN(code);
23,670✔
2482
}
2483

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc