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

taosdata / TDengine / #4907

30 Dec 2025 10:52AM UTC coverage: 65.541% (+0.03%) from 65.514%
#4907

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

808 existing lines in 106 files now uncovered.

193920 of 295877 relevant lines covered (65.54%)

118520209.34 hits per line

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

67.13
/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,496,406✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,496,406✔
31
  void     *value = NULL;
4,495,780✔
32
  int32_t   valueSize = 0;
4,497,860✔
33
  SMetaInfo info;
4,497,763✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,497,739✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
3,145✔
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,494,337✔
43
  if (r == 0) {  // name exists, check uid and type
4,494,172✔
44
    int64_t uid = *(tb_uid_t *)value;
2,832✔
45
    tdbFree(value);
2,832✔
46

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

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

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

71
  // check suid
72
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
4,491,340✔
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,491,131✔
79
}
80

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

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

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

117
  return code;
1,308,438✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
847,885✔
127
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
548✔
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);
847,337✔
133
  if (code) {
846,900✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
708✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
708✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
846,192✔
139
    tdbFreeClear(value);
846,192✔
140

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

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

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

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

179
  // handle entry
180
  SMetaEntry entry = {
8,977,158✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,491,198✔
184
      .name = pReq->name,
4,491,347✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,490,327✔
188
      .stbEntry.ownerId = pReq->ownerId,
4,485,842✔
189
  };
190
  if (pReq->rollup) {
4,485,396✔
191
    TABLE_SET_ROLLUP(entry.flags);
×
192
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
193
  }
194
  if (pReq->colCmpred) {
4,485,866✔
195
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,482,656✔
196
    entry.colCmpr = pReq->colCmpr;
4,482,656✔
197
  }
198

199
  entry.pExtSchemas = pReq->pExtSchemas;
4,476,898✔
200

201
  if (pReq->virtualStb) {
4,487,121✔
202
    TABLE_SET_VIRTUAL(entry.flags);
42,381✔
203
  }
204

205
  code = metaHandleEntry2(pMeta, &entry);
4,481,309✔
206
  if (TSDB_CODE_SUCCESS == code) {
4,497,183✔
207
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,497,183✔
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,497,183✔
214
}
215

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

220
  // check request
221
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
847,337✔
222
  if (code) {
846,046✔
223
    TAOS_RETURN(code);
1,416✔
224
  }
225

226
  // handle entry
227
  SMetaEntry entry = {
844,630✔
228
      .version = verison,
229
      .type = -TSDB_SUPER_TABLE,
230
      .uid = pReq->suid,
845,856✔
231
  };
232
  code = metaHandleEntry2(pMeta, &entry);
844,325✔
233
  if (code) {
845,921✔
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,
845,921✔
238
             pReq->suid, verison);
239
  }
240
  TAOS_RETURN(code);
846,469✔
241
}
242

243
// Alter Super Table
244

245
// Create Child Table
246
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
49,948,466✔
247
  int32_t   code = TSDB_CODE_SUCCESS;
49,948,466✔
248
  void     *value = NULL;
49,948,466✔
249
  int32_t   valueSize = 0;
49,949,618✔
250
  SMetaInfo info;
49,946,156✔
251

252
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
49,947,620✔
253
      pReq->ctb.suid == 0) {
49,949,954✔
254
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
398✔
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) {
49,944,346✔
261
    pReq->uid = *(int64_t *)value;
307,233✔
262
    tdbFreeClear(value);
307,233✔
263

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

278
    // check suid
279
    if (info.suid != pReq->ctb.suid) {
305,486✔
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;
305,486✔
288
  }
289

290
  // check super table existence
291
  SMetaEntry *pStbEntry = NULL;
49,638,662✔
292
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
49,638,974✔
293
  if (code) {
49,638,653✔
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) {
49,638,653✔
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) {
49,638,402✔
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;
49,641,953✔
317
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
49,642,136✔
318
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
49,641,474✔
319
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
227,158,097✔
320
      STagVal tagVal = {
177,773,001✔
321
          .cid = pTagSchema->pSchema[i].colId,
177,765,725✔
322
      };
323

324
      if (tTagGet(pTag, &tagVal)) {
177,770,053✔
325
        if (pTagSchema->pSchema[i].type != tagVal.type) {
137,706,806✔
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);
49,636,225✔
336

337
  // check grant
338
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
49,631,872✔
339
    code = grantCheck(TSDB_GRANT_TIMESERIES);
49,638,261✔
340
    if (TSDB_CODE_SUCCESS != code) {
49,635,549✔
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;
49,633,388✔
346
}
347

348
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
49,474,859✔
349
  int32_t code = TSDB_CODE_SUCCESS;
49,474,859✔
350

351
  if (NULL == ppRsp) {
49,474,859✔
352
    return code;
×
353
  }
354

355
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
49,474,859✔
356
  if (NULL == ppRsp) {
49,467,566✔
357
    return terrno;
×
358
  }
359

360
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
49,467,566✔
361
  (*ppRsp)->tuid = pEntry->uid;
49,471,287✔
362
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
49,477,901✔
363
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
49,473,629✔
364

365
  return code;
49,479,343✔
366
}
367

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

371
  // check request
372
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
49,788,912✔
373
  if (code) {
49,784,442✔
374
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
307,233✔
375
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,710✔
376
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
377
    }
378
    return code;
307,196✔
379
  }
380

381
  SMetaEntry entry = {
49,477,209✔
382
      .version = version,
383
      .type = TSDB_CHILD_TABLE,
384
      .uid = pReq->uid,
49,474,757✔
385
      .name = pReq->name,
49,476,297✔
386
      .ctbEntry.btime = pReq->btime,
49,474,125✔
387
      .ctbEntry.ttlDays = pReq->ttl,
49,470,755✔
388
      .ctbEntry.commentLen = pReq->commentLen,
49,471,153✔
389
      .ctbEntry.comment = pReq->comment,
49,473,620✔
390
      .ctbEntry.suid = pReq->ctb.suid,
49,469,533✔
391
      .ctbEntry.pTags = pReq->ctb.pTag,
49,472,884✔
392
  };
393

394
  // build response
395
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
49,473,744✔
396
  if (code) {
49,475,970✔
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);
49,475,970✔
403
  if (TSDB_CODE_SUCCESS == code) {
49,484,756✔
404
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
49,485,343✔
405
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
406
  } else {
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;
49,488,226✔
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) {
3,785,826✔
420
  int32_t code = 0;
3,785,826✔
421
  void   *value = NULL;
3,785,826✔
422
  int32_t valueSize = 0;
3,785,826✔
423

424
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
3,785,826✔
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) {
3,785,826✔
432
    // for auto create table, we return the uid of the existing table
433
    pReq->uid = *(tb_uid_t *)value;
34,053✔
434
    tdbFree(value);
34,053✔
435
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
34,053✔
436
  }
437

438
  // grant check
439
  code = grantCheck(TSDB_GRANT_TIMESERIES);
3,751,773✔
440
  if (code) {
3,751,773✔
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;
3,751,773✔
445
}
446

447
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
3,652,140✔
448
  int32_t code = TSDB_CODE_SUCCESS;
3,652,140✔
449

450
  if (NULL == ppRsp) {
3,652,140✔
451
    return code;
×
452
  }
453

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

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

465
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
42,204,481✔
466
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
38,552,341✔
467
    (*ppRsp)->pSchemaExt[i].colId = p->id;
38,552,341✔
468
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
38,552,341✔
469
    if (pEntry->pExtSchemas) {
38,552,341✔
470
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
420,611✔
471
    }
472
  }
473

474
  return code;
3,652,140✔
475
}
476

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

480
  // check request
481
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
3,686,193✔
482
  if (code) {
3,686,193✔
483
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
34,053✔
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);
34,053✔
488
  }
489

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

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

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

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

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

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

544
  return code;
99,633✔
545
}
546

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

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

577
  // handle entry
578
  code = metaHandleEntry2(pMeta, &entry);
99,633✔
579
  if (TSDB_CODE_SUCCESS == code) {
99,633✔
580
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
99,633✔
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);
99,633✔
587
#if 0
588
  metaTimeSeriesNotifyCheck(pMeta);
589
#endif
590
}
591

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

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

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

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

611
  return code;
157,846✔
612
}
613

614
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
157,846✔
615
  // check request
616
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
157,846✔
617
  if (code) {
157,846✔
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,
315,692✔
626
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
627
                      .uid = pReq->uid,
157,846✔
628
                      .name = pReq->name,
157,846✔
629
                      .ctbEntry.btime = pReq->btime,
157,846✔
630
                      .ctbEntry.ttlDays = pReq->ttl,
157,846✔
631
                      .ctbEntry.commentLen = pReq->commentLen,
157,846✔
632
                      .ctbEntry.comment = pReq->comment,
157,846✔
633
                      .ctbEntry.suid = pReq->ctb.suid,
157,846✔
634
                      .ctbEntry.pTags = pReq->ctb.pTag,
157,846✔
635
                      .colRef = pReq->colRef};
636

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

644
  // handle entry
645
  code = metaHandleEntry2(pMeta, &entry);
157,846✔
646
  if (TSDB_CODE_SUCCESS == code) {
157,846✔
647
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
157,846✔
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);
157,846✔
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) {
53,733,551✔
664
  int32_t code = TSDB_CODE_SUCCESS;
53,733,551✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
53,733,551✔
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
49,792,258✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
3,943,672✔
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
3,686,193✔
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
257,479✔
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
99,633✔
671
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
157,846✔
672
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
157,846✔
673
  } else {
674
    code = TSDB_CODE_INVALID_MSG;
×
675
  }
676
  TAOS_RETURN(code);
53,738,802✔
677
}
678

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

682
  // check request
683
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,308,438✔
684
  if (code) {
1,308,438✔
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,308,438✔
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,308,438✔
700
      .version = version,
701
      .uid = pReq->uid,
1,308,438✔
702
  };
703

704
  if (pReq->isVirtual) {
1,308,438✔
705
    if (pReq->suid == 0) {
51,673✔
706
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,048✔
707
    } else {
708
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
25,625✔
709
    }
710
  } else {
711
    if (pReq->suid == 0) {
1,256,765✔
712
      entry.type = -TSDB_NORMAL_TABLE;
721,749✔
713
    } else {
714
      entry.type = -TSDB_CHILD_TABLE;
535,016✔
715
    }
716
  }
717
  code = metaHandleEntry2(pMeta, &entry);
1,308,438✔
718
  if (code) {
1,308,438✔
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,308,438✔
723
             pReq->uid, version);
724
  }
725
  TAOS_RETURN(code);
1,308,438✔
726
}
727

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

731
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,030,897✔
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,030,897✔
739
  int32_t valueSize = 0;
4,030,897✔
740
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,030,897✔
741
  if (code) {
4,030,897✔
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,030,897✔
748
  tdbFreeClear(value);
4,030,897✔
749

750
  // check table type
751
  SMetaInfo info;
4,030,897✔
752
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,030,897✔
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,030,897✔
760
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
26,170✔
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,030,897✔
769
  if (code) {
4,030,897✔
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,030,897✔
775
}
776

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

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

786
  // fetch old entry
787
  SMetaEntry *pEntry = NULL;
3,315,232✔
788
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,315,232✔
789
  if (code) {
3,315,232✔
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,315,232✔
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,315,232✔
803
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,315,232✔
804
  SSchema        *pColumn;
805
  SExtSchema      extSchema = {0};
3,315,232✔
806
  pEntry->version = version;
3,315,232✔
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,
299,019✔
811
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
812
      metaFetchEntryFree(&pEntry);
299,019✔
813
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
299,019✔
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,016,213✔
819
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,016,213✔
820
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,090✔
821
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
822
    metaFetchEntryFree(&pEntry);
6,090✔
823
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,090✔
824
  }
825

826
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,010,123✔
827
  if (pSchema->nCols + 1 > maxCols) {
3,010,123✔
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,010,123✔
835
  if (NULL == pNewSchema) {
3,010,123✔
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,010,123✔
842
  pSchema->version++;
3,010,123✔
843
  pSchema->nCols++;
3,010,123✔
844
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,010,123✔
845
  pColumn->bytes = pReq->bytes;
3,010,123✔
846
  pColumn->type = pReq->type;
3,010,123✔
847
  pColumn->flags = pReq->flags;
3,010,123✔
848
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,010,123✔
849
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
×
850
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
851
              version);
852
    metaFetchEntryFree(&pEntry);
×
853
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
×
854
  }
855
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,010,123✔
856
  extSchema.typeMod = pReq->typeMod;
3,010,123✔
857
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,010,123✔
858
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,010,123✔
859
    SColRef tmpRef;
48,371✔
860
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
48,371✔
861
      tmpRef.hasRef = false;
28,438✔
862
      tmpRef.id = pColumn->colId;
28,438✔
863
    } else {
864
      tmpRef.hasRef = true;
19,933✔
865
      tmpRef.id = pColumn->colId;
19,933✔
866
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
19,933✔
867
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
19,933✔
868
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
19,933✔
869
    }
870
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
48,371✔
871
    if (code) {
48,371✔
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) {
2,961,752✔
880
      compress = createDefaultColCmprByType(pColumn->type);
2,952,491✔
881
    } else {
882
      compress = pReq->compress;
9,261✔
883
    }
884
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
2,961,752✔
885
    if (code) {
2,961,752✔
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,010,123✔
893
  if (code) {
3,010,123✔
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,010,123✔
902
  if (code) {
3,010,123✔
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,010,123✔
909
             pEntry->uid, version);
910
  }
911

912
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,010,123✔
913
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
48,371✔
914
    if (code) {
48,371✔
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++) {
369,401✔
919
        SColRef *p = &pEntry->colRef.pColRef[i];
321,030✔
920
        pRsp->pColRefs[i].hasRef = p->hasRef;
321,030✔
921
        pRsp->pColRefs[i].id = p->id;
321,030✔
922
        if (p->hasRef) {
321,030✔
923
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
157,680✔
924
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
157,680✔
925
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
157,680✔
926
        }
927
      }
928
    }
929
  } else {
930
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
2,961,752✔
931
    if (code) {
2,961,752✔
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,010,123✔
944
  TAOS_RETURN(code);
3,010,123✔
945
}
946

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

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

956
  // fetch old entry
957
  SMetaEntry *pEntry = NULL;
85,926✔
958
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
85,926✔
959
  if (code) {
85,926✔
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) {
85,926✔
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;
85,926✔
974
  SSchema        *pColumn = NULL;
85,926✔
975
  SSchema         tColumn;
85,926✔
976
  int32_t         iColumn = 0;
85,926✔
977
  for (; iColumn < pSchema->nCols; iColumn++) {
30,346,249✔
978
    pColumn = &pSchema->pSchema[iColumn];
30,346,249✔
979
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
30,346,249✔
980
      break;
85,926✔
981
    }
982
  }
983

984
  if (iColumn == pSchema->nCols) {
85,926✔
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) {
85,926✔
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;
85,926✔
999

1000
  // do drop column
1001
  pEntry->version = version;
85,926✔
1002
  if (pSchema->nCols - iColumn - 1 > 0) {
85,926✔
1003
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
63,999✔
1004
  }
1005
  pSchema->nCols--;
85,926✔
1006
  pSchema->version++;
85,926✔
1007
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
85,926✔
1008
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
26,909✔
1009
    if (code) {
26,909✔
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) {
26,909✔
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);
59,017✔
1023
    if (code) {
59,017✔
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) {
59,017✔
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);
85,926✔
1039
  if (code) {
85,926✔
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);
85,926✔
1048
  if (code) {
85,926✔
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,
85,926✔
1054
             pEntry->uid, version);
1055
  }
1056

1057
  // build response
1058
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
85,926✔
1059
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,909✔
1060
    if (code) {
26,909✔
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++) {
183,027✔
1065
        SColRef *p = &pEntry->colRef.pColRef[i];
156,118✔
1066
        pRsp->pColRefs[i].hasRef = p->hasRef;
156,118✔
1067
        pRsp->pColRefs[i].id = p->id;
156,118✔
1068
        if (p->hasRef) {
156,118✔
1069
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
87,642✔
1070
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
87,642✔
1071
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
87,642✔
1072
        }
1073
      }
1074
    }
1075
  } else {
1076
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
59,017✔
1077
    if (code) {
59,017✔
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++) {
49,231,151✔
1082
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
49,172,134✔
1083
        pRsp->pSchemaExt[i].colId = p->id;
49,172,134✔
1084
        pRsp->pSchemaExt[i].compress = p->alg;
49,172,134✔
1085
      }
1086
    }
1087
  }
1088

1089
  metaFetchEntryFree(&pEntry);
85,926✔
1090
  TAOS_RETURN(code);
85,926✔
1091
}
1092

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

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

1102
  if (NULL == pReq->colNewName) {
42,354✔
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;
42,354✔
1110
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
42,354✔
1111
  if (code) {
42,354✔
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) {
42,354✔
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;
42,354✔
1126
  SSchema        *pColumn = NULL;
42,354✔
1127
  int32_t         iColumn = 0;
42,354✔
1128
  for (int32_t i = 0; i < pSchema->nCols; i++) {
193,679✔
1129
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
193,679✔
1130
      pColumn = &pSchema->pSchema[i];
42,354✔
1131
      iColumn = i;
42,354✔
1132
      break;
42,354✔
1133
    }
1134
  }
1135

1136
  if (NULL == pColumn) {
42,354✔
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;
42,354✔
1146
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
42,354✔
1147
  pSchema->version++;
42,354✔
1148

1149
  // do handle entry
1150
  code = metaHandleEntry2(pMeta, pEntry);
42,354✔
1151
  if (code) {
42,354✔
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,
42,354✔
1158
             pEntry->uid, version);
1159
  }
1160

1161
  // build response
1162
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
42,354✔
1163
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,995✔
1164
    if (code) {
25,995✔
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++) {
183,748✔
1169
        SColRef *p = &pEntry->colRef.pColRef[i];
157,753✔
1170
        pRsp->pColRefs[i].hasRef = p->hasRef;
157,753✔
1171
        pRsp->pColRefs[i].id = p->id;
157,753✔
1172
        if (p->hasRef) {
157,753✔
1173
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
97,329✔
1174
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
97,329✔
1175
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
97,329✔
1176
        }
1177
      }
1178
    }
1179
  } else {
1180
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,359✔
1181
    if (code) {
16,359✔
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++) {
172,632✔
1186
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
156,273✔
1187
        pRsp->pSchemaExt[i].colId = p->id;
156,273✔
1188
        pRsp->pSchemaExt[i].compress = p->alg;
156,273✔
1189
      }
1190
    }
1191
  }
1192

1193
  metaFetchEntryFree(&pEntry);
42,354✔
1194
  TAOS_RETURN(code);
42,354✔
1195
}
1196

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

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

1206
  // fetch old entry
1207
  SMetaEntry *pEntry = NULL;
465,095✔
1208
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
465,095✔
1209
  if (code) {
465,095✔
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) {
465,095✔
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;
465,095✔
1224
  SSchema        *pColumn = NULL;
465,095✔
1225
  int32_t         iColumn = 0;
465,095✔
1226
  int32_t         rowSize = 0;
465,095✔
1227
  for (int32_t i = 0; i < pSchema->nCols; i++) {
18,294,748✔
1228
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
17,829,653✔
1229
      pColumn = &pSchema->pSchema[i];
465,095✔
1230
      iColumn = i;
465,095✔
1231
    }
1232
    rowSize += pSchema->pSchema[i].bytes;
17,829,653✔
1233
  }
1234

1235
  if (NULL == pColumn) {
465,095✔
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) {
465,095✔
1243
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
186,354✔
1244
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1245
              version);
1246
    metaFetchEntryFree(&pEntry);
186,354✔
1247
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
186,354✔
1248
  }
1249

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

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

1264
  // do handle entry
1265
  code = metaHandleEntry2(pMeta, pEntry);
236,111✔
1266
  if (code) {
236,111✔
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,
236,111✔
1273
             pEntry->uid, version);
1274
  }
1275

1276
  // build response
1277
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
236,111✔
1278
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
24,765✔
1279
    if (code) {
24,765✔
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++) {
177,598✔
1284
        SColRef *p = &pEntry->colRef.pColRef[i];
152,833✔
1285
        pRsp->pColRefs[i].hasRef = p->hasRef;
152,833✔
1286
        pRsp->pColRefs[i].id = p->id;
152,833✔
1287
        if (p->hasRef) {
152,833✔
1288
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
73,706✔
1289
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
73,706✔
1290
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
73,706✔
1291
        }
1292
      }
1293
    }
1294
  } else {
1295
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
211,346✔
1296
    if (code) {
211,346✔
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,499,822✔
1301
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,288,476✔
1302
        pRsp->pSchemaExt[i].colId = p->id;
8,288,476✔
1303
        pRsp->pSchemaExt[i].compress = p->alg;
8,288,476✔
1304
      }
1305
    }
1306
  }
1307

1308
  metaFetchEntryFree(&pEntry);
236,111✔
1309
  TAOS_RETURN(code);
236,111✔
1310
}
1311

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

1315
  // check tag name
1316
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,228,479✔
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,228,479✔
1324
  int32_t valueSize = 0;
7,228,479✔
1325
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,228,479✔
1326
  if (code) {
7,228,479✔
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,228,223✔
1333

1334
  TAOS_RETURN(code);
7,228,223✔
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,132,464✔
1340
  if (isNull) {
7,132,464✔
1341
    if (!tTagGet(pOldTag, &value)) {
44,769✔
1342
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,329✔
1343
      return true;
19,329✔
1344
    }
1345
    return false;
25,440✔
1346
  }
1347
  if (!tTagGet(pOldTag, &value)){
7,087,695✔
1348
    return false;
172,850✔
1349
  }
1350
  if (IS_VAR_DATA_TYPE(value.type)) {
6,914,845✔
1351
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
55,897✔
1352
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
35,623✔
1353
      return true;
35,623✔
1354
    }
1355
  } else {
1356
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
6,858,948✔
1357
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
86,956✔
1358
      return true;
86,956✔
1359
    }
1360
  }
1361
  return false;
6,792,266✔
1362
}
1363

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

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

1373
  // fetch child entry
1374
  SMetaEntry *pChild = NULL;
7,228,223✔
1375
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,228,223✔
1376
  if (code) {
7,228,223✔
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,228,223✔
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,228,223✔
1391
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,228,223✔
1392
  if (code) {
7,228,223✔
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,228,223✔
1401
  SSchema        *pColumn = NULL;
7,228,223✔
1402
  int32_t         iColumn = 0;
7,228,223✔
1403
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
7,868,028✔
1404
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
7,868,028✔
1405
      pColumn = &pTagSchema->pSchema[i];
7,228,223✔
1406
      iColumn = i;
7,228,223✔
1407
      break;
7,228,223✔
1408
    }
1409
  }
1410

1411
  if (NULL == pColumn) {
7,228,223✔
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,228,223✔
1421
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,228,223✔
1422
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
114,569✔
1423
    if (NULL == pNewTag) {
114,569✔
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;
114,569✔
1431
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
114,569✔
1432
  } else {
1433
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,113,654✔
1434

1435
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,113,654✔
1436
    if (NULL == pTagArray) {
7,113,654✔
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,127,507✔
1445
      STagVal value = {
8,146,983✔
1446
          .type = pTagSchema->pSchema[i].type,
8,146,983✔
1447
          .cid = pTagSchema->pSchema[i].colId,
8,146,983✔
1448
      };
1449

1450
      if (iColumn == i) {
8,146,983✔
1451
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,113,654✔
1452
          taosArrayDestroy(pTagArray);
133,130✔
1453
          metaFetchEntryFree(&pChild);
133,130✔
1454
          metaFetchEntryFree(&pSuper);
133,130✔
1455
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
133,130✔
1456
        }
1457
        if (pReq->isNull) {
6,980,524✔
1458
          continue;
23,559✔
1459
        }
1460
        if (IS_VAR_DATA_TYPE(value.type)) {
6,956,965✔
1461
          value.pData = pReq->pTagVal;
97,172✔
1462
          value.nData = pReq->nTagVal;
97,172✔
1463
        } else {
1464
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
6,859,793✔
1465
        }
1466
      } else if (!tTagGet(pOldTag, &value)) {
1,033,329✔
1467
        continue;
282,121✔
1468
      }
1469

1470
      if (NULL == taosArrayPush(pTagArray, &value)) {
7,708,173✔
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;
6,980,524✔
1481
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
6,980,524✔
1482
    if (code) {
6,980,524✔
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);
6,980,524✔
1491
    taosMemoryFree(pChild->ctbEntry.pTags);
6,980,524✔
1492
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
6,980,524✔
1493
  }
1494

1495
  // do handle entry
1496
  code = metaHandleEntry2(pMeta, pChild);
7,095,093✔
1497
  if (code) {
7,095,093✔
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,095,093✔
1505
             pChild->uid, version);
1506
  }
1507

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

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

1517
  // check tag name
1518
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,135✔
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,135✔
1526
  int32_t valueSize = 0;
3,135✔
1527
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,135✔
1528
  if (code) {
3,135✔
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,135✔
1535

1536
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,135✔
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,135✔
1543
}
1544

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

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

1553
  // fetch child entry
1554
  SMetaEntry *pChild = NULL;
3,135✔
1555
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,135✔
1556
  if (code) {
3,135✔
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,135✔
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,135✔
1571
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,135✔
1572
  if (code) {
3,135✔
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,135✔
1581

1582
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,135✔
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,135✔
1593
  if (pTagTable == NULL) {
3,135✔
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++) {
21,945✔
1602
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
18,810✔
1603
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
18,810✔
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,135✔
1614
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
25,080✔
1615
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
21,945✔
1616
        ? numOfChangedTags++
18,810✔
1617
        : 0;
21,945✔
1618
  }
1619
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,135✔
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,135✔
1630
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,135✔
1631
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,135✔
1632
  if (NULL == pTagArray) {
3,135✔
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,135✔
1642

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

1649
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
21,945✔
1650
    if (pTagVal == NULL) {
21,945✔
1651
      if (!tTagGet(pOldTag, &value)) {
3,135✔
1652
        continue;
×
1653
      }
1654
    } else {
1655
      value.type = pCol->type;
18,810✔
1656
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
18,810✔
1657
        allSame = false;
10,032✔
1658
      }
1659
      if (pTagVal->isNull) {
18,810✔
1660
        continue;
3,762✔
1661
      }
1662

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

1671
    if (taosArrayPush(pTagArray, &value) == NULL) {
18,183✔
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,135✔
1683
    metaWarn("vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
627✔
1684
             __LINE__, version);
1685
    taosHashCleanup(pTagTable);
627✔
1686
    taosArrayDestroy(pTagArray);
627✔
1687
    metaFetchEntryFree(&pChild);
627✔
1688
    metaFetchEntryFree(&pSuper);
627✔
1689
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
627✔
1690
  } 
1691
  STag *pNewTag = NULL;
2,508✔
1692
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
2,508✔
1693
  if (code) {
2,508✔
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,508✔
1703
  taosMemoryFree(pChild->ctbEntry.pTags);
2,508✔
1704
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
2,508✔
1705

1706
  // do handle entry
1707
  code = metaHandleEntry2(pMeta, pChild);
2,508✔
1708
  if (code) {
2,508✔
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,508✔
1717
             pChild->uid, version);
1718
  }
1719

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

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

1729
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
23,692✔
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;
23,692✔
1736
}
1737

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

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

1746
  // fetch entry
1747
  SMetaEntry *pEntry = NULL;
23,692✔
1748
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,692✔
1749
  if (code) {
23,692✔
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;
23,692✔
1757
  if (pEntry->type == TSDB_CHILD_TABLE) {
23,692✔
1758
    if (pReq->updateTTL) {
11,128✔
1759
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,309✔
1760
    }
1761
    if (pReq->newCommentLen >= 0) {
11,128✔
1762
      char *pNewComment = NULL;
6,819✔
1763
      if (pReq->newCommentLen) {
6,819✔
1764
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,350✔
1765
        if (NULL == pNewComment) {
4,350✔
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,350✔
1772
      } else {
1773
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,469✔
1774
      }
1775
      pEntry->ctbEntry.comment = pNewComment;
6,819✔
1776
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
6,819✔
1777
    }
1778
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
12,564✔
1779
    if (pReq->updateTTL) {
12,564✔
1780
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,439✔
1781
    }
1782
    if (pReq->newCommentLen >= 0) {
12,564✔
1783
      char *pNewComment = NULL;
8,125✔
1784
      if (pReq->newCommentLen > 0) {
8,125✔
1785
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
5,656✔
1786
        if (NULL == pNewComment) {
5,656✔
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,656✔
1793
      } else {
1794
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,469✔
1795
      }
1796
      pEntry->ntbEntry.comment = pNewComment;
8,125✔
1797
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,125✔
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);
23,692✔
1808
  if (code) {
23,692✔
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,
23,692✔
1815
             pEntry->uid, version);
1816
  }
1817

1818
  metaFetchEntryFree(&pEntry);
23,692✔
1819
  TAOS_RETURN(code);
23,692✔
1820
}
1821

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

1825
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5,798✔
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,798✔
1832
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5,798✔
1833
  if (code) {
5,798✔
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,798✔
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,798✔
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,798✔
1855
  SColCmprWrapper *wp = &pEntry->colCmpr;
5,798✔
1856
  for (int32_t i = 0; i < wp->nCols; i++) {
46,384✔
1857
    SColCmpr *p = &wp->pColCmpr[i];
40,586✔
1858
    if (p->id == pReq->colId) {
40,586✔
1859
      uint32_t dst = 0;
5,798✔
1860
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5,798✔
1861
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1862
      if (updated > 0) {
5,798✔
1863
        p->alg = dst;
5,798✔
1864
      }
1865
    }
1866
  }
1867

1868
  if (updated == 0) {
5,798✔
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,798✔
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,798✔
1883

1884
  // do handle entry
1885
  code = metaHandleEntry2(pMeta, pEntry);
5,798✔
1886
  if (code) {
5,798✔
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,798✔
1893
             pEntry->uid, version);
1894
  }
1895

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

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

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

1909
  if (NULL == pReq->refDbName) {
70,350✔
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) {
70,350✔
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) {
70,350✔
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;
70,350✔
1929
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
70,350✔
1930
  if (code) {
70,350✔
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) {
70,350✔
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;
70,350✔
1945
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
70,350✔
1946
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
25,401✔
1947
    if (code) {
25,401✔
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 =
70,350✔
1957
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
70,350✔
1958
  SColRef *pColRef = NULL;
70,350✔
1959
  int32_t  iColumn = 0;
70,350✔
1960
  for (int32_t i = 0; i < pSchema->nCols; i++) {
348,999✔
1961
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
348,999✔
1962
      pColRef = &pEntry->colRef.pColRef[i];
70,350✔
1963
      iColumn = i;
70,350✔
1964
      break;
70,350✔
1965
    }
1966
  }
1967

1968
  if (NULL == pColRef) {
70,350✔
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;
70,350✔
1978
  pColRef->hasRef = true;
70,350✔
1979
  pColRef->id = pSchema->pSchema[iColumn].colId;
70,350✔
1980
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
70,350✔
1981
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
70,350✔
1982
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
70,350✔
1983
  pSchema->version++;
70,350✔
1984
  pEntry->colRef.version++;
70,350✔
1985

1986
  // do handle entry
1987
  code = metaHandleEntry2(pMeta, pEntry);
70,350✔
1988
  if (code) {
70,350✔
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,
70,350✔
1996
             pEntry->uid, version);
1997
  }
1998

1999
  // build response
2000
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
70,350✔
2001
  if (code) {
70,350✔
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++) {
525,864✔
2006
      SColRef *p = &pEntry->colRef.pColRef[i];
455,514✔
2007
      pRsp->pColRefs[i].hasRef = p->hasRef;
455,514✔
2008
      pRsp->pColRefs[i].id = p->id;
455,514✔
2009
      if (p->hasRef) {
455,514✔
2010
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
314,442✔
2011
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
314,442✔
2012
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
314,442✔
2013
      }
2014
    }
2015
  }
2016

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

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

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

2031
  // fetch old entry
2032
  SMetaEntry *pEntry = NULL;
51,940✔
2033
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
51,940✔
2034
  if (code) {
51,940✔
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) {
51,940✔
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;
51,940✔
2049
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
51,940✔
2050
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
26,170✔
2051
    if (code) {
26,170✔
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 =
51,940✔
2061
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
51,940✔
2062
  SColRef *pColRef = NULL;
51,940✔
2063
  int32_t  iColumn = 0;
51,940✔
2064
  for (int32_t i = 0; i < pSchema->nCols; i++) {
211,435✔
2065
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
211,435✔
2066
      pColRef = &pEntry->colRef.pColRef[i];
51,940✔
2067
      iColumn = i;
51,940✔
2068
      break;
51,940✔
2069
    }
2070
  }
2071

2072
  if (NULL == pColRef) {
51,940✔
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;
51,940✔
2082
  pColRef->hasRef = false;
51,940✔
2083
  pColRef->id = pSchema->pSchema[iColumn].colId;
51,940✔
2084
  pSchema->version++;
51,940✔
2085
  pEntry->colRef.version++;
51,940✔
2086

2087
  // do handle entry
2088
  code = metaHandleEntry2(pMeta, pEntry);
51,940✔
2089
  if (code) {
51,940✔
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,
51,940✔
2097
             pEntry->uid, version);
2098
  }
2099

2100
  // build response
2101
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
51,940✔
2102
  if (code) {
51,940✔
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++) {
318,958✔
2107
      SColRef *p = &pEntry->colRef.pColRef[i];
267,018✔
2108
      pRsp->pColRefs[i].hasRef = p->hasRef;
267,018✔
2109
      pRsp->pColRefs[i].id = p->id;
267,018✔
2110
      if (p->hasRef) {
267,018✔
2111
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
132,336✔
2112
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
132,336✔
2113
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
132,336✔
2114
      }
2115
    }
2116
  }
2117

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

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

2126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,153✔
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,153✔
2133
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
4,153✔
2134
  if (code) {
4,153✔
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,153✔
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,153✔
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,153✔
2166
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
4,153✔
2167
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,153✔
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,153✔
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,153✔
2193
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
21,130✔
2194
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
16,977✔
2195
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
16,977✔
2196

2197
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
16,977✔
2198
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
16,977✔
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)) {
16,977✔
2206
      numOfChangedTags++;
4,153✔
2207
      SSCHMEA_SET_IDX_ON(pOldColumn);
4,153✔
2208
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
12,824✔
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,153✔
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,153✔
2225
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,153✔
2226

2227
  // do handle the entry
2228
  code = metaHandleEntry2(pMeta, pEntry);
4,153✔
2229
  if (code) {
4,153✔
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,153✔
2236
             pEntry->uid, version);
2237
  }
2238

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

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

2246
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,720✔
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,720✔
2253
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,720✔
2254
  if (code) {
2,720✔
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,720✔
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,720✔
2268
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,720✔
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,720✔
2277
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
7,486✔
2278
    SSchema *pCol = pTagSchema->pSchema + i;
7,486✔
2279
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
7,486✔
2280
      if (!IS_IDX_ON(pCol)) {
2,720✔
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,720✔
2287
      SSCHMEA_SET_IDX_OFF(pCol);
2,720✔
2288
      break;
2,720✔
2289
    }
2290
  }
2291

2292
  if (numOfChangedTags != 1) {
2,720✔
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,720✔
2301
  pTagSchema->version++;
2,720✔
2302
  code = metaHandleEntry2(pMeta, pEntry);
2,720✔
2303
  if (code) {
2,720✔
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,720✔
2310
             pEntry->uid, version);
2311
  }
2312

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

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

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

2326
  SMetaEntry *pEntry = NULL;
7,011,448✔
2327
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,012,702✔
2328
  if (code) {
7,012,703✔
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,012,703✔
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,026,307✔
2342
      .version = version,
2343
      .type = TSDB_SUPER_TABLE,
2344
      .uid = pReq->suid,
7,011,409✔
2345
      .name = pReq->name,
7,011,409✔
2346
      .stbEntry.schemaRow = pReq->schemaRow,
2347
      .stbEntry.schemaTag = pReq->schemaTag,
2348
      .stbEntry.keep = pReq->keep,
7,007,752✔
2349
      .stbEntry.ownerId = pReq->ownerId,
7,010,191✔
2350
      .colCmpr = pReq->colCmpr,
2351
      .pExtSchemas = pReq->pExtSchemas,
7,008,973✔
2352
  };
2353
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,006,459✔
2354
  if (pReq->virtualStb) {
7,006,459✔
2355
    TABLE_SET_VIRTUAL(entry.flags);
15,568✔
2356
  }
2357
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,010,801✔
2358
    TABLE_SET_ROLLUP(entry.flags);
4,362✔
2359
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,362✔
2360
  }
2361

2362
  // do handle the entry
2363
  code = metaHandleEntry2(pMeta, &entry);
7,010,800✔
2364
  if (code) {
7,004,615✔
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,004,615✔
2371
             pReq->suid, version);
2372
  }
2373

2374
  metaFetchEntryFree(&pEntry);
7,013,971✔
2375
  TAOS_RETURN(code);
7,015,099✔
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,083✔
2418
  int32_t code = TSDB_CODE_SUCCESS;
21,083✔
2419

2420
  if (NULL == pReq->name || pReq->name[0] == 0) {
21,083✔
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,810✔
2427
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
21,810✔
2428
  if (code) {
21,810✔
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,810✔
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,810✔
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,810✔
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,810✔
2455
  }
2456

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

2468
  // do handle the entry
2469
  code = metaHandleEntry2(pMeta, &entry);
21,810✔
2470
  if (code) {
21,810✔
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,810✔
2477
    pMeta->pVnode->config.isRsma = 1;
21,810✔
2478
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
21,810✔
2479
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2480
  }
2481

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

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

2489
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,362✔
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,362✔
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,362✔
2502
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,362✔
2503
  if (code) {
4,362✔
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,362✔
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,362✔
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,362✔
2525
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,362✔
2526
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,362✔
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,362✔
2536
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,362✔
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,362✔
2545
  entry.version = version;
4,362✔
2546
  TABLE_RESET_ROLLUP(entry.flags);
4,362✔
2547
  entry.stbEntry.rsmaParam.uid = 0;
4,362✔
2548
  entry.stbEntry.rsmaParam.name = NULL;
4,362✔
2549
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,362✔
2550
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,362✔
2551
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,362✔
2552

2553
  // do handle the entry
2554
  code = metaHandleEntry2(pMeta, &entry);
4,362✔
2555
  if (code) {
4,362✔
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,362✔
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,362✔
2565
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2566
  }
2567

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

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

2575
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,632✔
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,632✔
2582
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,632✔
2583
  if (code) {
11,632✔
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) {
11,632✔
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)) {
11,632✔
2598
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
11,632✔
2599
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
11,632✔
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;
11,632✔
2608
  entry.version = version;
11,632✔
2609
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
11,632✔
2610
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
11,632✔
2611
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
11,632✔
2612
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
11,632✔
2613
  }
2614
  // do handle the entry
2615
  code = metaHandleEntry2(pMeta, &entry);
11,632✔
2616
  if (code) {
11,632✔
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,632✔
2623
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2624
  }
2625

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