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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

67.87
/source/dnode/vnode/src/meta/metaTable2.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17

18
extern int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry);
19
extern int32_t metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp);
20
extern int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
21
                                    STableMetaRsp *pMetaRsp, int8_t tableType);
22
extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
23
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
24
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
25
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
26
extern int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema);
27
extern int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum);
28

29
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,709,801✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,709,801✔
31
  void     *value = NULL;
4,723,745✔
32
  int32_t   valueSize = 0;
4,723,300✔
33
  SMetaInfo info;
4,723,455✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,722,225✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
10,235✔
38
              pReq->name, version);
39
    return TSDB_CODE_INVALID_MSG;
×
40
  }
41

42
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
4,713,256✔
43
  if (r == 0) {  // name exists, check uid and type
4,713,785✔
44
    int64_t uid = *(tb_uid_t *)value;
2,924✔
45
    tdbFree(value);
2,924✔
46

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

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

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

71
  // check suid
72
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
4,710,861✔
73
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
×
74
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
75
    return TSDB_CODE_INVALID_MSG;
×
76
  }
77

78
  return TSDB_CODE_SUCCESS;
4,717,413✔
79
}
80

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

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

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

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

117
  return code;
1,364,920✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
865,861✔
127
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1,151✔
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);
865,861✔
133
  if (code) {
861,931✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
731✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
731✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
861,200✔
139
    tdbFreeClear(value);
862,671✔
140

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

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
859,657✔
149
  if (code) {
860,769✔
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) {
860,769✔
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;
860,769✔
161
}
162

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

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

179
  // handle entry
180
  SMetaEntry entry = {
9,424,584✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,717,839✔
184
      .name = pReq->name,
4,711,753✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,702,566✔
188
      .stbEntry.ownerId = pReq->ownerId,
4,694,978✔
189
  };
190
  if (pReq->rollup) {
4,706,784✔
191
    TABLE_SET_ROLLUP(entry.flags);
×
192
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
193
  }
194
  if (pReq->colCmpred) {
4,696,262✔
195
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,699,633✔
196
    entry.colCmpr = pReq->colCmpr;
4,699,633✔
197
  }
198

199
  entry.pExtSchemas = pReq->pExtSchemas;
4,682,110✔
200

201
  if (pReq->virtualStb) {
4,685,760✔
202
    TABLE_SET_VIRTUAL(entry.flags);
42,127✔
203
  }
204

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

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

220
  // check request
221
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
865,325✔
222
  if (code) {
863,898✔
223
    TAOS_RETURN(code);
1,462✔
224
  }
225

226
  // handle entry
227
  SMetaEntry entry = {
862,436✔
228
      .version = verison,
229
      .type = -TSDB_SUPER_TABLE,
230
      .uid = pReq->suid,
862,496✔
231
  };
232
  code = metaHandleEntry2(pMeta, &entry);
860,769✔
233
  if (code) {
865,014✔
234
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
235
              tstrerror(code));
236
  } else {
237
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
865,014✔
238
             pReq->suid, verison);
239
  }
240
  TAOS_RETURN(code);
865,014✔
241
}
242

243
// Alter Super Table
244

245
// Create Child Table
246
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
52,083,305✔
247
  int32_t   code = TSDB_CODE_SUCCESS;
52,083,305✔
248
  void     *value = NULL;
52,083,305✔
249
  int32_t   valueSize = 0;
52,082,098✔
250
  SMetaInfo info;
52,071,988✔
251

252
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
52,077,364✔
253
      pReq->ctb.suid == 0) {
52,071,147✔
254
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
3,745✔
255
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
256
    return TSDB_CODE_INVALID_MSG;
×
257
  }
258

259
  // check table existence
260
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
52,071,341✔
261
    pReq->uid = *(int64_t *)value;
324,772✔
262
    tdbFreeClear(value);
324,772✔
263

264
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
324,772✔
265
      metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
266
                ", which is an internal error, version:%" PRId64,
267
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
268
      return TSDB_CODE_INTERNAL_ERROR;
×
269
    }
270

271
    // check table type
272
    if (info.suid == info.uid || info.suid == 0) {
324,772✔
273
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
1,749✔
274
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
275
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
1,749✔
276
    }
277

278
    // check suid
279
    if (info.suid != pReq->ctb.suid) {
323,023✔
280
      metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " exists in another stable with uid %" PRId64
×
281
                " instead of stable with uid %" PRId64 " version:%" PRId64,
282
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, info.suid, pReq->ctb.suid,
283
                version);
284
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
285
    }
286

287
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
323,023✔
288
  }
289

290
  // check super table existence
291
  SMetaEntry *pStbEntry = NULL;
51,756,195✔
292
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
51,754,077✔
293
  if (code) {
51,749,308✔
294
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
295
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
296
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
297
  }
298

299
  if (pStbEntry->type != TSDB_SUPER_TABLE) {
51,749,308✔
300
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64,
×
301
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
302
    metaFetchEntryFree(&pStbEntry);
×
303
    return TSDB_CODE_INVALID_MSG;
×
304
  }
305

306
  if (pStbEntry->uid != pReq->ctb.suid) {
51,739,215✔
307
    metaError("vgId:%d, %s failed at %s:%d since super table %s uid %" PRId64 " does not match request uid %" PRId64
×
308
              ", version:%" PRId64,
309
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, pStbEntry->uid, pReq->ctb.suid,
310
              version);
311
    metaFetchEntryFree(&pStbEntry);
×
312
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
313
  }
314

315
  // Check tag value
316
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
51,739,902✔
317
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
51,746,351✔
318
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
51,746,464✔
319
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
236,168,435✔
320
      STagVal tagVal = {
184,686,006✔
321
          .cid = pTagSchema->pSchema[i].colId,
184,682,154✔
322
      };
323

324
      if (tTagGet(pTag, &tagVal)) {
184,696,397✔
325
        if (pTagSchema->pSchema[i].type != tagVal.type) {
143,560,611✔
326
          metaError("vgId:%d, %s failed at %s:%d since child table %s tag type does not match the expected type, version:%" PRId64,
×
327
                    TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, version);
328
          metaFetchEntryFree(&pStbEntry);
×
329
          return TSDB_CODE_INVALID_MSG;
×
330
        }
331
      }
332
    }
333
  }
334

335
  metaFetchEntryFree(&pStbEntry);
51,756,474✔
336

337
  // check grant
338
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
51,732,649✔
339
    code = grantCheck(TSDB_GRANT_TIMESERIES);
51,747,682✔
340
    if (TSDB_CODE_SUCCESS != code) {
51,729,037✔
341
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
342
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
343
    }
344
  }
345
  return code;
51,728,461✔
346
}
347

348
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
51,567,052✔
349
  int32_t code = TSDB_CODE_SUCCESS;
51,567,052✔
350

351
  if (NULL == ppRsp) {
51,567,052✔
352
    return code;
×
353
  }
354

355
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
51,567,052✔
356
  if (NULL == ppRsp) {
51,570,930✔
357
    return terrno;
×
358
  }
359

360
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
51,570,930✔
361
  (*ppRsp)->tuid = pEntry->uid;
51,575,060✔
362
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
51,585,648✔
363
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
51,590,533✔
364

365
  return code;
51,603,341✔
366
}
367

368
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
51,932,011✔
369
  int32_t code = TSDB_CODE_SUCCESS;
51,932,011✔
370

371
  // check request
372
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
51,932,011✔
373
  if (code) {
51,906,742✔
374
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
324,772✔
375
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,749✔
376
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
377
    }
378
    return code;
324,772✔
379
  }
380

381
  SMetaEntry entry = {
51,581,970✔
382
      .version = version,
383
      .type = TSDB_CHILD_TABLE,
384
      .uid = pReq->uid,
51,584,231✔
385
      .name = pReq->name,
51,576,353✔
386
      .ctbEntry.btime = pReq->btime,
51,572,134✔
387
      .ctbEntry.ttlDays = pReq->ttl,
51,572,890✔
388
      .ctbEntry.commentLen = pReq->commentLen,
51,574,493✔
389
      .ctbEntry.comment = pReq->comment,
51,573,116✔
390
      .ctbEntry.suid = pReq->ctb.suid,
51,573,023✔
391
      .ctbEntry.pTags = pReq->ctb.pTag,
51,574,625✔
392
  };
393

394
  // build response
395
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
51,579,998✔
396
  if (code) {
51,594,822✔
397
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
398
              tstrerror(code));
399
  }
400

401
  // handle entry
402
  code = metaHandleEntry2(pMeta, &entry);
51,594,822✔
403
  if (TSDB_CODE_SUCCESS == code) {
51,602,069✔
404
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
51,603,107✔
405
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
406
  } else {
UNCOV
407
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
408
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
409
              pReq->ctb.suid, version);
410
  }
411
  return code;
51,611,669✔
412
}
413

414
// Drop Child Table
415

416
// Alter Child Table
417

418
// Create Normal Table
419
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
19,224,618✔
420
  int32_t code = 0;
19,224,618✔
421
  void   *value = NULL;
19,224,618✔
422
  int32_t valueSize = 0;
19,224,618✔
423

424
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
19,224,618✔
425
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
426
              __FILE__, __LINE__, pReq->name, version);
427
    return TSDB_CODE_INVALID_MSG;
×
428
  }
429

430
  // check name
431
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
19,224,618✔
432
    // for auto create table, we return the uid of the existing table
433
    pReq->uid = *(tb_uid_t *)value;
27,486✔
434
    tdbFree(value);
27,486✔
435
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
27,486✔
436
  }
437

438
  // grant check
439
  code = grantCheck(TSDB_GRANT_TIMESERIES);
19,197,132✔
440
  if (code) {
19,197,132✔
441
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
442
              __FILE__, __LINE__, tstrerror(code), version, pReq->name);
443
  }
444
  return code;
19,197,132✔
445
}
446

447
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
19,098,856✔
448
  int32_t code = TSDB_CODE_SUCCESS;
19,098,856✔
449

450
  if (NULL == ppRsp) {
19,098,856✔
451
    return code;
×
452
  }
453

454
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
19,098,856✔
455
  if (NULL == *ppRsp) {
19,098,856✔
456
    return terrno;
×
457
  }
458

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

465
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
106,696,553✔
466
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
87,597,697✔
467
    (*ppRsp)->pSchemaExt[i].colId = p->id;
87,597,697✔
468
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
87,597,697✔
469
    if (pEntry->pExtSchemas) {
87,597,697✔
470
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
437,394✔
471
    }
472
  }
473

474
  return code;
19,098,856✔
475
}
476

477
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
19,126,342✔
478
  int32_t code = TSDB_CODE_SUCCESS;
19,126,342✔
479

480
  // check request
481
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
19,126,342✔
482
  if (code) {
19,126,342✔
483
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
27,486✔
484
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
485
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
486
    }
487
    TAOS_RETURN(code);
27,486✔
488
  }
489

490
  SMetaEntry entry = {
38,197,371✔
491
      .version = version,
492
      .type = TSDB_NORMAL_TABLE,
493
      .uid = pReq->uid,
19,098,856✔
494
      .name = pReq->name,
19,098,856✔
495
      .ntbEntry.btime = pReq->btime,
19,098,856✔
496
      .ntbEntry.ttlDays = pReq->ttl,
19,098,856✔
497
      .ntbEntry.commentLen = pReq->commentLen,
19,098,856✔
498
      .ntbEntry.comment = pReq->comment,
19,098,856✔
499
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
500
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
19,098,856✔
501
      .colCmpr = pReq->colCmpr,
502
      .pExtSchemas = pReq->pExtSchemas,
19,098,856✔
503
  };
504
  TABLE_SET_COL_COMPRESSED(entry.flags);
19,098,856✔
505

506
  // build response
507
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
19,098,856✔
508
  if (code) {
19,098,856✔
509
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
510
              tstrerror(code));
511
  }
512

513
  // handle entry
514
  code = metaHandleEntry2(pMeta, &entry);
19,098,856✔
515
  if (TSDB_CODE_SUCCESS == code) {
19,098,856✔
516
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
19,098,856✔
517
             pReq->name, pReq->uid);
518
  } else {
519
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
520
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
521
  }
522
  TAOS_RETURN(code);
19,098,856✔
523
}
524

525
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
98,276✔
526
  int32_t code = TSDB_CODE_SUCCESS;
98,276✔
527

528
  if (NULL == ppRsp) {
98,276✔
529
    return code;
×
530
  }
531

532
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
98,276✔
533
  if (NULL == *ppRsp) {
98,276✔
534
    return terrno;
×
535
  }
536

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

544
  return code;
97,808✔
545
}
546

547
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
98,276✔
548
  // check request
549
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
98,276✔
550
  if (code) {
98,276✔
551
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
552
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
553
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
554
    }
555
    TAOS_RETURN(code);
×
556
  }
557

558
  SMetaEntry entry = {.version = version,
196,552✔
559
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
560
                      .uid = pReq->uid,
98,276✔
561
                      .name = pReq->name,
98,276✔
562
                      .ntbEntry.btime = pReq->btime,
98,276✔
563
                      .ntbEntry.ttlDays = pReq->ttl,
98,276✔
564
                      .ntbEntry.commentLen = pReq->commentLen,
98,276✔
565
                      .ntbEntry.comment = pReq->comment,
98,276✔
566
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
567
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
98,276✔
568
                      .colRef = pReq->colRef};
569

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

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

592
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
152,362✔
593
  int32_t code = TSDB_CODE_SUCCESS;
152,362✔
594

595
  if (NULL == ppRsp) {
152,362✔
596
    return code;
×
597
  }
598

599
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
152,362✔
600
  if (NULL == *ppRsp) {
152,362✔
601
    return terrno;
×
602
  }
603

604
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
152,362✔
605
  if (code) {
152,362✔
606
    taosMemoryFreeClear(*ppRsp);
468✔
607
    return code;
468✔
608
  }
609
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
151,894✔
610

611
  return code;
151,894✔
612
}
613

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

625
  SMetaEntry entry = {.version = version,
304,724✔
626
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
627
                      .uid = pReq->uid,
152,362✔
628
                      .name = pReq->name,
152,362✔
629
                      .ctbEntry.btime = pReq->btime,
152,362✔
630
                      .ctbEntry.ttlDays = pReq->ttl,
152,362✔
631
                      .ctbEntry.commentLen = pReq->commentLen,
152,362✔
632
                      .ctbEntry.comment = pReq->comment,
152,362✔
633
                      .ctbEntry.suid = pReq->ctb.suid,
152,362✔
634
                      .ctbEntry.pTags = pReq->ctb.pTag,
152,362✔
635
                      .colRef = pReq->colRef};
636

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

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

659
// Drop Normal Table
660

661
// Alter Normal Table
662

663
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
71,307,061✔
664
  int32_t code = TSDB_CODE_SUCCESS;
71,307,061✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
71,307,061✔
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
51,933,984✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
19,376,980✔
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
19,126,342✔
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
250,638✔
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
98,276✔
671
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
152,362✔
672
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
152,362✔
673
  } else {
674
    code = TSDB_CODE_INVALID_MSG;
×
675
  }
676
  TAOS_RETURN(code);
71,313,120✔
677
}
678

679
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,364,920✔
680
  int32_t code = TSDB_CODE_SUCCESS;
1,364,920✔
681

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

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

699
  SMetaEntry entry = {
1,364,920✔
700
      .version = version,
701
      .uid = pReq->uid,
1,364,920✔
702
  };
703

704
  if (pReq->isVirtual) {
1,364,920✔
705
    if (pReq->suid == 0) {
52,734✔
706
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,580✔
707
    } else {
708
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,154✔
709
    }
710
  } else {
711
    if (pReq->suid == 0) {
1,312,186✔
712
      entry.type = -TSDB_NORMAL_TABLE;
758,449✔
713
    } else {
714
      entry.type = -TSDB_CHILD_TABLE;
553,737✔
715
    }
716
  }
717
  code = metaHandleEntry2(pMeta, &entry);
1,364,920✔
718
  if (code) {
1,364,920✔
719
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
720
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
721
  } else {
722
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,364,920✔
723
             pReq->uid, version);
724
  }
725
  TAOS_RETURN(code);
1,364,920✔
726
}
727

728
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,116,589✔
729
  int32_t code = 0;
4,116,589✔
730

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

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

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

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

777
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,403,182✔
778
  int32_t code = TSDB_CODE_SUCCESS;
3,403,182✔
779

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

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

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

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

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

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

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

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

943
  metaFetchEntryFree(&pEntry);
3,095,946✔
944
  TAOS_RETURN(code);
3,095,946✔
945
}
946

947
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
78,049✔
948
  int32_t code = TSDB_CODE_SUCCESS;
78,049✔
949

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

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

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

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

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

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

998
  tColumn = *pColumn;
78,049✔
999

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

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

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

1057
  // build response
1058
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
78,049✔
1059
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,378✔
1060
    if (code) {
28,378✔
1061
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1062
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1063
    } else {
1064
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
30,851,403✔
1065
        SColRef *p = &pEntry->colRef.pColRef[i];
30,823,025✔
1066
        pRsp->pColRefs[i].hasRef = p->hasRef;
30,823,025✔
1067
        pRsp->pColRefs[i].id = p->id;
30,823,025✔
1068
        if (p->hasRef) {
30,823,025✔
1069
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
15,423,392✔
1070
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
15,423,392✔
1071
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
15,423,392✔
1072
        }
1073
      }
1074
    }
1075
  } else {
1076
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
49,671✔
1077
    if (code) {
49,671✔
1078
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1079
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1080
    } else {
1081
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,020,710✔
1082
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
7,971,039✔
1083
        pRsp->pSchemaExt[i].colId = p->id;
7,971,039✔
1084
        pRsp->pSchemaExt[i].compress = p->alg;
7,971,039✔
1085
      }
1086
    }
1087
  }
1088

1089
  metaFetchEntryFree(&pEntry);
78,049✔
1090
  TAOS_RETURN(code);
78,049✔
1091
}
1092

1093
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
43,157✔
1094
  int32_t code = TSDB_CODE_SUCCESS;
43,157✔
1095

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

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

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

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

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

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

1143

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

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

1161
  // build response
1162
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
43,157✔
1163
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,511✔
1164
    if (code) {
26,511✔
1165
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1166
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1167
    } else {
1168
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
187,396✔
1169
        SColRef *p = &pEntry->colRef.pColRef[i];
160,885✔
1170
        pRsp->pColRefs[i].hasRef = p->hasRef;
160,885✔
1171
        pRsp->pColRefs[i].id = p->id;
160,885✔
1172
        if (p->hasRef) {
160,885✔
1173
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
99,261✔
1174
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
99,261✔
1175
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
99,261✔
1176
        }
1177
      }
1178
    }
1179
  } else {
1180
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,646✔
1181
    if (code) {
16,646✔
1182
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1183
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1184
    } else {
1185
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
175,447✔
1186
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
158,801✔
1187
        pRsp->pSchemaExt[i].colId = p->id;
158,801✔
1188
        pRsp->pSchemaExt[i].compress = p->alg;
158,801✔
1189
      }
1190
    }
1191
  }
1192

1193
  metaFetchEntryFree(&pEntry);
43,157✔
1194
  TAOS_RETURN(code);
43,157✔
1195
}
1196

1197
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
467,436✔
1198
  int32_t code = TSDB_CODE_SUCCESS;
467,436✔
1199

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

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

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

1222
  // search the column to update
1223
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
467,436✔
1224
  SSchema        *pColumn = NULL;
467,436✔
1225
  int32_t         iColumn = 0;
467,436✔
1226
  int32_t         rowSize = 0;
467,436✔
1227
  for (int32_t i = 0; i < pSchema->nCols; i++) {
33,674,317✔
1228
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
33,208,113✔
1229
      pColumn = &pSchema->pSchema[i];
467,436✔
1230
      iColumn = i;
467,436✔
1231
    }
1232
    rowSize += pSchema->pSchema[i].bytes;
33,206,881✔
1233
  }
1234

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

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

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

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

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

1276
  // build response
1277
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
239,516✔
1278
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,725✔
1279
    if (code) {
25,725✔
1280
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1281
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1282
    } else {
1283
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
15,511,402✔
1284
        SColRef *p = &pEntry->colRef.pColRef[i];
15,485,677✔
1285
        pRsp->pColRefs[i].hasRef = p->hasRef;
15,485,677✔
1286
        pRsp->pColRefs[i].id = p->id;
15,485,677✔
1287
        if (p->hasRef) {
15,485,677✔
1288
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
75,170✔
1289
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
75,170✔
1290
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
75,170✔
1291
        }
1292
      }
1293
    }
1294
  } else {
1295
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
213,791✔
1296
    if (code) {
213,791✔
1297
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1298
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1299
    } else {
1300
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,597,667✔
1301
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,383,876✔
1302
        pRsp->pSchemaExt[i].colId = p->id;
8,383,876✔
1303
        pRsp->pSchemaExt[i].compress = p->alg;
8,383,876✔
1304
      }
1305
    }
1306
  }
1307

1308
  metaFetchEntryFree(&pEntry);
239,516✔
1309
  TAOS_RETURN(code);
239,516✔
1310
}
1311

1312
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,468,318✔
1313
  int32_t code = 0;
7,468,318✔
1314

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

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

1334
  TAOS_RETURN(code);
7,468,062✔
1335
}
1336

1337
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1338

1339
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,370,771✔
1340
  if (isNull) {
7,370,771✔
1341
    if (!tTagGet(pOldTag, &value)) {
45,315✔
1342
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,485✔
1343
      return true;
19,485✔
1344
    }
1345
    return false;
25,830✔
1346
  }
1347
  if (!tTagGet(pOldTag, &value)){
7,325,456✔
1348
    return false;
177,340✔
1349
  }
1350
  if (IS_VAR_DATA_TYPE(value.type)) {
7,148,116✔
1351
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
56,605✔
1352
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
35,819✔
1353
      return true;
35,819✔
1354
    }
1355
  } else {
1356
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,091,511✔
1357
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
87,018✔
1358
      return true;
87,018✔
1359
    }
1360
  }
1361
  return false;
7,025,279✔
1362
}
1363

1364
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,468,318✔
1365
  int32_t code = TSDB_CODE_SUCCESS;
7,468,318✔
1366

1367
  // check request
1368
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,468,318✔
1369
  if (code) {
7,468,318✔
1370
    TAOS_RETURN(code);
256✔
1371
  }
1372

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

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

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

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

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

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

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

1444
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,625,659✔
1445
      STagVal value = {
8,407,562✔
1446
          .type = pTagSchema->pSchema[i].type,
8,407,562✔
1447
          .cid = pTagSchema->pSchema[i].colId,
8,407,562✔
1448
      };
1449

1450
      if (iColumn == i) {
8,407,562✔
1451
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,351,361✔
1452
          taosArrayDestroy(pTagArray);
133,264✔
1453
          metaFetchEntryFree(&pChild);
133,264✔
1454
          metaFetchEntryFree(&pSuper);
133,264✔
1455
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
133,264✔
1456
        }
1457
        if (pReq->isNull) {
7,218,097✔
1458
          continue;
23,889✔
1459
        }
1460
        if (IS_VAR_DATA_TYPE(value.type)) {
7,194,208✔
1461
          value.pData = pReq->pTagVal;
99,810✔
1462
          value.nData = pReq->nTagVal;
99,810✔
1463
        } else {
1464
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,094,398✔
1465
        }
1466
      } else if (!tTagGet(pOldTag, &value)) {
1,056,201✔
1467
        continue;
286,640✔
1468
      }
1469

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

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

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

1508
  // free resource and return
1509
  metaFetchEntryFree(&pChild);
7,334,798✔
1510
  metaFetchEntryFree(&pSuper);
7,334,798✔
1511
  TAOS_RETURN(code);
7,334,798✔
1512
}
1513

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

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

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

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

1542
  TAOS_RETURN(code);
3,235✔
1543
}
1544

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

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

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

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

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

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

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

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

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

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

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

1641
  bool allSame = true;
3,235✔
1642

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

1649
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
22,645✔
1650
    if (pTagVal == NULL) {
22,645✔
1651
      if (!tTagGet(pOldTag, &value)) {
3,235✔
1652
        continue;
×
1653
      }
1654
    } else {
1655
      value.type = pCol->type;
19,410✔
1656
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
19,410✔
1657
        allSame = false;
10,352✔
1658
      }
1659
      if (pTagVal->isNull) {
19,410✔
1660
        continue;
3,882✔
1661
      }
1662

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

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

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

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

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

1726
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,158✔
1727
  int32_t code = TSDB_CODE_SUCCESS;
24,158✔
1728

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

1735
  return code;
24,158✔
1736
}
1737

1738
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,158✔
1739
  int32_t code = 0;
24,158✔
1740

1741
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
24,158✔
1742
  if (code) {
24,158✔
1743
    TAOS_RETURN(code);
×
1744
  }
1745

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

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

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

1818
  metaFetchEntryFree(&pEntry);
24,158✔
1819
  TAOS_RETURN(code);
24,158✔
1820
}
1821

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

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

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

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

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

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

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

1882
  pEntry->version = version;
5,912✔
1883

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

1896
  metaFetchEntryFree(&pEntry);
5,912✔
1897
  TAOS_RETURN(code);
5,912✔
1898
}
1899

1900
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
71,781✔
1901
  int32_t code = TSDB_CODE_SUCCESS;
71,781✔
1902

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

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

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

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

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

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

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

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

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

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

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

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

2017
  metaFetchEntryFree(&pEntry);
71,781✔
2018
  metaFetchEntryFree(&pSuper);
71,781✔
2019
  TAOS_RETURN(code);
71,781✔
2020
}
2021

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2192
  int32_t numOfChangedTags = 0;
4,332✔
2193
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
22,150✔
2194
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,818✔
2195
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,818✔
2196

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

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

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

2224
  pEntry->version = version;
4,332✔
2225
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,332✔
2226

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

2239
  metaFetchEntryFree(&pEntry);
4,332✔
2240
  TAOS_RETURN(code);
4,332✔
2241
}
2242

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

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

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

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

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

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

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

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

2313
  metaFetchEntryFree(&pEntry);
2,878✔
2314
  TAOS_RETURN(code);
2,878✔
2315
}
2316

2317
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,282,182✔
2318
  int32_t code = TSDB_CODE_SUCCESS;
7,282,182✔
2319

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

2326
  SMetaEntry *pEntry = NULL;
7,268,203✔
2327
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,268,096✔
2328
  if (code) {
7,268,371✔
2329
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2330
              __FILE__, __LINE__, pReq->name, version);
2331
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2332
  }
2333

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

2341
  SMetaEntry entry = {
21,759,856✔
2342
      .version = version,
2343
      .type = TSDB_SUPER_TABLE,
2344
      .uid = pReq->suid,
7,261,369✔
2345
      .name = pReq->name,
7,266,472✔
2346
      .stbEntry.schemaRow = pReq->schemaRow,
2347
      .stbEntry.schemaTag = pReq->schemaTag,
2348
      .stbEntry.keep = pReq->keep,
7,257,597✔
2349
      .stbEntry.ownerId = pReq->ownerId,
7,254,339✔
2350
      .colCmpr = pReq->colCmpr,
2351
      .pExtSchemas = pReq->pExtSchemas,
7,249,262✔
2352
  };
2353
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,246,320✔
2354
  if (pReq->virtualStb) {
7,246,320✔
2355
    TABLE_SET_VIRTUAL(entry.flags);
19,618✔
2356
  }
2357
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,246,113✔
2358
    TABLE_SET_ROLLUP(entry.flags);
4,386✔
2359
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,386✔
2360
  }
2361

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

2374
  metaFetchEntryFree(&pEntry);
7,279,595✔
2375
  TAOS_RETURN(code);
7,293,043✔
2376
}
2377

2378
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2379
  int32_t code = 0;
×
2380

2381
  if (taosArrayGetSize(uidArray) == 0) {
×
2382
    return TSDB_CODE_SUCCESS;
×
2383
  }
2384

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

2395
    SMetaEntry entry = {
×
2396
        .version = version,
2397
        .uid = uid,
2398
    };
2399

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

2417
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
21,930✔
2418
  int32_t code = TSDB_CODE_SUCCESS;
21,930✔
2419

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

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

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

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

2449
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
21,930✔
2450
    // overwrite the old rsma definition if exists
2451
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2452
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2453
  } else {
2454
    TABLE_SET_ROLLUP(pEntry->flags);
21,930✔
2455
  }
2456

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

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

2482
  metaFetchEntryFree(&pEntry);
21,930✔
2483
  TAOS_RETURN(code);
21,930✔
2484
}
2485

2486
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,386✔
2487
  int32_t code = TSDB_CODE_SUCCESS;
4,386✔
2488

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

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

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

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

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

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

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

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

2568
  metaFetchEntryFree(&pEntry);
4,386✔
2569
  TAOS_RETURN(code);
4,386✔
2570
}
2571

2572
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,696✔
2573
  int32_t code = TSDB_CODE_SUCCESS;
11,696✔
2574

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

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

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

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

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

2626
  metaFetchEntryFree(&pEntry);
11,696✔
2627
  TAOS_RETURN(code);
11,696✔
2628
}
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