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

taosdata / TDengine / #4991

17 Mar 2026 07:57AM UTC coverage: 69.756% (+0.4%) from 69.348%
#4991

push

travis-ci

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

14 of 16 new or added lines in 5 files covered. (87.5%)

3928 existing lines in 138 files now uncovered.

192146 of 275455 relevant lines covered (69.76%)

137208686.18 hits per line

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

68.01
/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, int64_t ownerId,
20
                                 STableMetaRsp *pMetaRsp);
21
extern int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
22
                                    int64_t ownerId, STableMetaRsp *pMetaRsp, int8_t tableType);
23
extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
24
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
25
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
26
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
27
extern int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema);
28
extern int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum);
29

30
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,470,628✔
31
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,470,628✔
32
  void     *value = NULL;
5,503,521✔
33
  int32_t   valueSize = 0;
5,508,114✔
34
  SMetaInfo info;
5,503,531✔
35

36
  // check name
37
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
5,504,521✔
38
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
30,265✔
39
              pReq->name, version);
40
    return TSDB_CODE_INVALID_MSG;
×
41
  }
42

43
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
5,460,351✔
44
  if (r == 0) {  // name exists, check uid and type
5,480,326✔
45
    int64_t uid = *(tb_uid_t *)value;
4,131✔
46
    tdbFree(value);
4,131✔
47

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

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

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

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

79
  return TSDB_CODE_SUCCESS;
5,494,844✔
80
}
81

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

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

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

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

118
  return code;
1,492,257✔
119
}
120

121
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
923,646✔
122
  int32_t   code = TSDB_CODE_SUCCESS;
923,646✔
123
  void     *value = NULL;
923,646✔
124
  int32_t   valueSize = 0;
924,086✔
125
  SMetaInfo info;
922,170✔
126

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

133
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
922,930✔
134
  if (code) {
916,172✔
135
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
746✔
136
              __FILE__, __LINE__, pReq->name, version);
137
    return TSDB_CODE_TDB_STB_NOT_EXIST;
746✔
138
  } else {
139
    int64_t uid = *(int64_t *)value;
915,426✔
140
    tdbFreeClear(value);
918,399✔
141

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

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

164
// Create Super Table
165
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,478,932✔
166
  int32_t code = TSDB_CODE_SUCCESS;
5,478,932✔
167

168
  // check request
169
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,478,932✔
170
  if (code != TSDB_CODE_SUCCESS) {
5,496,918✔
171
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
4,131✔
172
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
1,893✔
173
               pReq->name, pReq->suid, version);
174
      TAOS_RETURN(TSDB_CODE_SUCCESS);
1,893✔
175
    } else {
176
      TAOS_RETURN(code);
2,238✔
177
    }
178
  }
179

180
  // handle entry
181
  SMetaEntry entry = {
10,946,872✔
182
      .version = version,
183
      .type = TSDB_SUPER_TABLE,
184
      .uid = pReq->suid,
5,491,675✔
185
      .name = pReq->name,
5,478,314✔
186
      .stbEntry.schemaRow = pReq->schemaRow,
187
      .stbEntry.schemaTag = pReq->schemaTag,
188
      .stbEntry.keep = pReq->keep,
5,484,505✔
189
      .stbEntry.ownerId = pReq->ownerId,
5,462,003✔
190
  };
191
  if (pReq->rollup) {
5,454,463✔
192
    TABLE_SET_ROLLUP(entry.flags);
×
193
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
194
  }
195
  if (pReq->colCmpred) {
5,452,694✔
196
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,440,436✔
197
    entry.colCmpr = pReq->colCmpr;
5,440,436✔
198
  }
199

200
  entry.pExtSchemas = pReq->pExtSchemas;
5,454,603✔
201

202
  if (pReq->virtualStb) {
5,453,201✔
203
    TABLE_SET_VIRTUAL(entry.flags);
77,652✔
204
  }
205

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

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

221
  // check request
222
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
923,646✔
223
  if (code) {
911,468✔
224
    TAOS_RETURN(code);
1,492✔
225
  }
226

227
  // handle entry
228
  SMetaEntry entry = {
909,976✔
229
      .version = verison,
230
      .type = -TSDB_SUPER_TABLE,
231
      .uid = pReq->suid,
910,023✔
232
  };
233
  code = metaHandleEntry2(pMeta, &entry);
915,386✔
234
  if (code) {
921,350✔
235
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
236
              tstrerror(code));
237
  } else {
238
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
921,350✔
239
             pReq->suid, verison);
240
  }
241
  TAOS_RETURN(code);
921,939✔
242
}
243

244
// Alter Super Table
245

246
// Create Child Table
247
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
61,341,271✔
248
  int32_t   code = TSDB_CODE_SUCCESS;
61,341,271✔
249
  void     *value = NULL;
61,341,271✔
250
  int32_t   valueSize = 0;
61,342,162✔
251
  SMetaInfo info;
61,338,120✔
252

253
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
61,337,820✔
254
      pReq->ctb.suid == 0) {
61,333,735✔
255
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
27,107✔
256
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
257
    return TSDB_CODE_INVALID_MSG;
×
258
  }
259

260
  // check table existence
261
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
61,320,628✔
262
    pReq->uid = *(int64_t *)value;
323,978✔
263
    tdbFreeClear(value);
324,014✔
264

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

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

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

288
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
322,250✔
289
  }
290

291
  // check super table existence
292
  SMetaEntry *pStbEntry = NULL;
61,015,925✔
293
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
61,013,858✔
294
  if (code) {
60,999,008✔
295
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
296
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
297
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
298
  }
299

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

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

316
  // Check tag value
317
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
60,996,213✔
318
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
61,008,170✔
319
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
61,005,764✔
320
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
271,392,352✔
321
      STagVal tagVal = {
210,674,982✔
322
          .cid = pTagSchema->pSchema[i].colId,
210,658,118✔
323
      };
324

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

336
  metaFetchEntryFree(&pStbEntry);
60,989,258✔
337

338
  // check grant
339
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
60,977,153✔
340
    code = grantCheck(TSDB_GRANT_TIMESERIES);
61,011,368✔
341
    if (TSDB_CODE_SUCCESS != code) {
60,954,493✔
342
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
343
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
344
    }
345
  }
346
  return code;
60,953,134✔
347
}
348

349
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
60,700,967✔
350
  int32_t code = TSDB_CODE_SUCCESS;
60,700,967✔
351

352
  if (NULL == ppRsp) {
60,700,967✔
353
    return code;
×
354
  }
355

356
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
60,700,967✔
357
  if (NULL == ppRsp) {
60,710,148✔
358
    return terrno;
×
359
  }
360

361
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
60,710,148✔
362
  (*ppRsp)->tuid = pEntry->uid;
60,725,247✔
363
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
60,740,657✔
364
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
60,750,948✔
365

366
  return code;
60,754,191✔
367
}
368

369
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
61,106,801✔
370
  int32_t code = TSDB_CODE_SUCCESS;
61,106,801✔
371

372
  // check request
373
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
61,106,801✔
374
  if (code) {
61,054,623✔
375
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
324,050✔
376
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,800✔
377
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
378
    }
379
    return code;
324,050✔
380
  }
381

382
  SMetaEntry entry = {
60,730,573✔
383
      .version = version,
384
      .type = TSDB_CHILD_TABLE,
385
      .uid = pReq->uid,
60,726,085✔
386
      .name = pReq->name,
60,735,431✔
387
      .ctbEntry.btime = pReq->btime,
60,735,230✔
388
      .ctbEntry.ttlDays = pReq->ttl,
60,734,447✔
389
      .ctbEntry.commentLen = pReq->commentLen,
60,724,366✔
390
      .ctbEntry.comment = pReq->comment,
60,721,695✔
391
      .ctbEntry.suid = pReq->ctb.suid,
60,732,015✔
392
      .ctbEntry.pTags = pReq->ctb.pTag,
60,718,553✔
393
  };
394

395
  // build response
396
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
60,717,629✔
397
  if (code) {
60,752,751✔
398
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
399
              tstrerror(code));
400
  }
401

402
  // handle entry
403
  code = metaHandleEntry2(pMeta, &entry);
60,752,751✔
404
  if (TSDB_CODE_SUCCESS == code) {
60,775,222✔
405
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
60,777,559✔
406
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
407
  } else {
408
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
409
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
410
              pReq->ctb.suid, version);
411
  }
412
  return code;
60,793,986✔
413
}
414

415
// Drop Child Table
416

417
// Alter Child Table
418

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

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

431
  // check name
432
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
7,488,366✔
433
    // for auto create table, we return the uid of the existing table
434
    pReq->uid = *(tb_uid_t *)value;
34,453✔
435
    tdbFree(value);
34,453✔
436
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
34,453✔
437
  }
438

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

448
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
7,302,230✔
449
  int32_t code = TSDB_CODE_SUCCESS;
7,302,230✔
450

451
  if (NULL == ppRsp) {
7,302,230✔
452
    return code;
×
453
  }
454

455
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
7,302,230✔
456
  if (NULL == *ppRsp) {
7,302,230✔
457
    return terrno;
×
458
  }
459

460
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, pEntry->ntbEntry.ownerId, *ppRsp);
7,302,230✔
461
  if (code) {
7,302,230✔
462
    taosMemoryFreeClear(*ppRsp);
×
463
    return code;
×
464
  }
465

466
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
83,536,143✔
467
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
76,233,913✔
468
    (*ppRsp)->pSchemaExt[i].colId = p->id;
76,233,913✔
469
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
76,233,913✔
470
    if (pEntry->pExtSchemas) {
76,233,913✔
471
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
455,148✔
472
    }
473
  }
474

475
  return code;
7,302,230✔
476
}
477

478
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
7,336,683✔
479
  int32_t code = TSDB_CODE_SUCCESS;
7,336,683✔
480

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

491
  SMetaEntry entry = {
14,604,035✔
492
      .version = version,
493
      .type = TSDB_NORMAL_TABLE,
494
      .uid = pReq->uid,
7,302,230✔
495
      .name = pReq->name,
7,302,230✔
496
      .ntbEntry.btime = pReq->btime,
7,302,230✔
497
      .ntbEntry.ttlDays = pReq->ttl,
7,302,230✔
498
      .ntbEntry.commentLen = pReq->commentLen,
7,302,230✔
499
      .ntbEntry.comment = pReq->comment,
7,302,230✔
500
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
501
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
7,302,230✔
502
      .ntbEntry.ownerId = pReq->ntb.userId,
7,302,230✔
503
      .colCmpr = pReq->colCmpr,
504
      .pExtSchemas = pReq->pExtSchemas,
7,302,230✔
505
  };
506
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,302,230✔
507

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

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

527
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
151,683✔
528
  int32_t code = TSDB_CODE_SUCCESS;
151,683✔
529

530
  if (NULL == ppRsp) {
151,683✔
531
    return code;
×
532
  }
533

534
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
151,683✔
535
  if (NULL == *ppRsp) {
151,683✔
536
    return terrno;
×
537
  }
538

539
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, pEntry->ntbEntry.ownerId, *ppRsp,
151,683✔
540
                              TSDB_VIRTUAL_NORMAL_TABLE);
541
  if (code) {
151,683✔
542
    taosMemoryFreeClear(*ppRsp);
×
543
    return code;
×
544
  }
545

546
  return code;
151,683✔
547
}
548

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

560
  SMetaEntry entry = {.version = version,
303,312✔
561
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
562
                      .uid = pReq->uid,
151,683✔
563
                      .name = pReq->name,
151,683✔
564
                      .ntbEntry.btime = pReq->btime,
151,683✔
565
                      .ntbEntry.ttlDays = pReq->ttl,
151,683✔
566
                      .ntbEntry.commentLen = pReq->commentLen,
151,683✔
567
                      .ntbEntry.comment = pReq->comment,
151,683✔
568
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
569
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
151,683✔
570
                      .ntbEntry.ownerId = pReq->ntb.userId,
151,683✔
571
                      .colRef = pReq->colRef};
572

573
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
151,683✔
574
  if (code) {
151,683✔
575
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
576
              tstrerror(code));
577
    TAOS_RETURN(code);
×
578
  }
579

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

595
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
237,960✔
596
  int32_t code = TSDB_CODE_SUCCESS;
237,960✔
597

598
  if (NULL == ppRsp) {
237,960✔
599
    return code;
×
600
  }
601

602
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
237,960✔
603
  if (NULL == *ppRsp) {
237,960✔
604
    return terrno;
×
605
  }
606

607
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, 0, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
237,960✔
608
  if (code) {
237,960✔
609
    taosMemoryFreeClear(*ppRsp);
×
610
    return code;
×
611
  }
612
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
237,960✔
613

614
  return code;
237,960✔
615
}
616

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

628
  SMetaEntry entry = {.version = version,
475,866✔
629
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
630
                      .uid = pReq->uid,
237,960✔
631
                      .name = pReq->name,
237,960✔
632
                      .ctbEntry.btime = pReq->btime,
237,960✔
633
                      .ctbEntry.ttlDays = pReq->ttl,
237,960✔
634
                      .ctbEntry.commentLen = pReq->commentLen,
237,960✔
635
                      .ctbEntry.comment = pReq->comment,
237,960✔
636
                      .ctbEntry.suid = pReq->ctb.suid,
237,960✔
637
                      .ctbEntry.pTags = pReq->ctb.pTag,
237,960✔
638
                      .colRef = pReq->colRef};
639

640
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
237,960✔
641
  if (code) {
237,960✔
642
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
643
              tstrerror(code));
644
    TAOS_RETURN(code);
×
645
  }
646

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

662
// Drop Normal Table
663

664
// Alter Normal Table
665

666
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
68,824,587✔
667
  int32_t code = TSDB_CODE_SUCCESS;
68,824,587✔
668
  if (TSDB_CHILD_TABLE == pReq->type) {
68,824,587✔
669
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
61,107,533✔
670
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
7,726,326✔
671
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
7,336,683✔
672
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
389,643✔
673
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
151,683✔
674
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
237,960✔
675
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
237,960✔
676
  } else {
677
    code = TSDB_CODE_INVALID_MSG;
×
678
  }
679
  TAOS_RETURN(code);
68,843,681✔
680
}
681

682
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,492,257✔
683
  int32_t code = TSDB_CODE_SUCCESS;
1,492,257✔
684

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

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

702
  SMetaEntry entry = {
1,492,257✔
703
      .version = version,
704
      .uid = pReq->uid,
1,492,257✔
705
  };
706

707
  if (pReq->isVirtual) {
1,492,257✔
708
    if (pReq->suid == 0) {
55,467✔
709
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
27,976✔
710
    } else {
711
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
27,491✔
712
    }
713
  } else {
714
    if (pReq->suid == 0) {
1,436,790✔
715
      entry.type = -TSDB_NORMAL_TABLE;
802,847✔
716
    } else {
717
      entry.type = -TSDB_CHILD_TABLE;
633,943✔
718
    }
719
  }
720
  code = metaHandleEntry2(pMeta, &entry);
1,492,257✔
721
  if (code) {
1,492,257✔
722
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
723
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
724
  } else {
725
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,492,257✔
726
             pReq->uid, version);
727
  }
728
  TAOS_RETURN(code);
1,492,257✔
729
}
730

731
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,383,938✔
732
  int32_t code = 0;
4,383,938✔
733

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

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

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

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

780
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,608,792✔
781
  int32_t code = TSDB_CODE_SUCCESS;
3,608,792✔
782

783
  // check request
784
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,608,792✔
785
  if (code) {
3,608,792✔
786
    TAOS_RETURN(code);
×
787
  }
788

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

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

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

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

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

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

915
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,274,621✔
916
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
54,266✔
917
                                pEntry->type);
54,266✔
918
    if (code) {
54,266✔
919
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
920
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
921
    } else {
922
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,912,847✔
923
        SColRef *p = &pEntry->colRef.pColRef[i];
18,858,581✔
924
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,858,581✔
925
        pRsp->pColRefs[i].id = p->id;
18,858,581✔
926
        if (p->hasRef) {
18,858,581✔
927
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
174,290✔
928
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
174,290✔
929
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
174,290✔
930
        }
931
      }
932
    }
933
  } else {
934
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,220,355✔
935
    if (code) {
3,220,355✔
936
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
937
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
938
    } else {
939
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
940
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
941
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
942
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
943
      }
944
    }
945
  }
946

947
  metaFetchEntryFree(&pEntry);
3,274,621✔
948
  TAOS_RETURN(code);
3,274,621✔
949
}
950

951
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
91,186✔
952
  int32_t code = TSDB_CODE_SUCCESS;
91,186✔
953

954
  // check request
955
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
91,186✔
956
  if (code) {
91,186✔
957
    TAOS_RETURN(code);
×
958
  }
959

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

969
  if (pEntry->version >= version) {
91,186✔
970
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
971
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
972
    metaFetchEntryFree(&pEntry);
×
973
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
974
  }
975

976
  // search the column to drop
977
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
91,186✔
978
  SSchema        *pColumn = NULL;
91,186✔
979
  SSchema         tColumn;
91,078✔
980
  int32_t         iColumn = 0;
91,186✔
981
  for (; iColumn < pSchema->nCols; iColumn++) {
69,697,506✔
982
    pColumn = &pSchema->pSchema[iColumn];
69,697,506✔
983
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
69,697,506✔
984
      break;
91,186✔
985
    }
986
  }
987

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

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

1002
  tColumn = *pColumn;
91,186✔
1003

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

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

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

1062
  // build response
1063
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
91,186✔
1064
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
30,040✔
1065
                                pEntry->type);
30,040✔
1066
    if (code) {
30,040✔
1067
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1068
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1069
    } else {
1070
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
37,217,075✔
1071
        SColRef *p = &pEntry->colRef.pColRef[i];
37,187,035✔
1072
        pRsp->pColRefs[i].hasRef = p->hasRef;
37,187,035✔
1073
        pRsp->pColRefs[i].id = p->id;
37,187,035✔
1074
        if (p->hasRef) {
37,187,035✔
1075
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
18,606,345✔
1076
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
18,606,345✔
1077
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
18,606,345✔
1078
        }
1079
      }
1080
    }
1081
  } else {
1082
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
61,146✔
1083
    if (code) {
61,146✔
1084
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1085
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1086
    } else {
1087
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
62,140,669✔
1088
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
62,079,523✔
1089
        pRsp->pSchemaExt[i].colId = p->id;
62,079,523✔
1090
        pRsp->pSchemaExt[i].compress = p->alg;
62,079,523✔
1091
      }
1092
    }
1093
  }
1094

1095
  metaFetchEntryFree(&pEntry);
91,186✔
1096
  TAOS_RETURN(code);
91,186✔
1097
}
1098

1099
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
45,883✔
1100
  int32_t code = TSDB_CODE_SUCCESS;
45,883✔
1101

1102
  // check request
1103
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
45,883✔
1104
  if (code) {
45,883✔
1105
    TAOS_RETURN(code);
×
1106
  }
1107

1108
  if (NULL == pReq->colNewName) {
45,883✔
1109
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1110
              __func__, __FILE__, __LINE__, version);
1111
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1112
  }
1113

1114
  // fetch old entry
1115
  SMetaEntry *pEntry = NULL;
45,883✔
1116
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
45,883✔
1117
  if (code) {
45,883✔
1118
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1119
              __FILE__, __LINE__, pReq->tbName, version);
1120
    TAOS_RETURN(code);
×
1121
  }
1122

1123
  if (pEntry->version >= version) {
45,883✔
1124
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1125
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1126
    metaFetchEntryFree(&pEntry);
×
1127
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1128
  }
1129

1130
  // search the column to update
1131
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
45,883✔
1132
  SSchema        *pColumn = NULL;
45,883✔
1133
  int32_t         iColumn = 0;
45,883✔
1134
  for (int32_t i = 0; i < pSchema->nCols; i++) {
209,543✔
1135
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
209,543✔
1136
      pColumn = &pSchema->pSchema[i];
45,883✔
1137
      iColumn = i;
45,883✔
1138
      break;
45,883✔
1139
    }
1140
  }
1141

1142
  if (NULL == pColumn) {
45,883✔
1143
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1144
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1145
    metaFetchEntryFree(&pEntry);
×
1146
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1147
  }
1148

1149

1150
  // do update column name
1151
  pEntry->version = version;
45,883✔
1152
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
45,883✔
1153
  pSchema->version++;
45,883✔
1154

1155
  // do handle entry
1156
  code = metaHandleEntry2(pMeta, pEntry);
45,883✔
1157
  if (code) {
45,883✔
1158
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1159
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1160
    metaFetchEntryFree(&pEntry);
×
1161
    TAOS_RETURN(code);
×
1162
  } else {
1163
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
45,883✔
1164
             pEntry->uid, version);
1165
  }
1166

1167
  // build response
1168
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
45,883✔
1169
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
27,896✔
1170
                                pEntry->type);
27,896✔
1171
    if (code) {
27,896✔
1172
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1173
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1174
    } else {
1175
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
197,080✔
1176
        SColRef *p = &pEntry->colRef.pColRef[i];
169,184✔
1177
        pRsp->pColRefs[i].hasRef = p->hasRef;
169,184✔
1178
        pRsp->pColRefs[i].id = p->id;
169,184✔
1179
        if (p->hasRef) {
169,184✔
1180
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
104,408✔
1181
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
104,408✔
1182
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
104,408✔
1183
        }
1184
      }
1185
    }
1186
  } else {
1187
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
17,987✔
1188
    if (code) {
17,987✔
1189
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1190
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1191
    } else {
1192
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
187,957✔
1193
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
169,970✔
1194
        pRsp->pSchemaExt[i].colId = p->id;
169,970✔
1195
        pRsp->pSchemaExt[i].compress = p->alg;
169,970✔
1196
      }
1197
    }
1198
  }
1199

1200
  metaFetchEntryFree(&pEntry);
45,883✔
1201
  TAOS_RETURN(code);
45,883✔
1202
}
1203

1204
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
501,742✔
1205
  int32_t code = TSDB_CODE_SUCCESS;
501,742✔
1206

1207
  // check request
1208
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
501,742✔
1209
  if (code) {
501,742✔
1210
    TAOS_RETURN(code);
×
1211
  }
1212

1213
  // fetch old entry
1214
  SMetaEntry *pEntry = NULL;
501,742✔
1215
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
501,742✔
1216
  if (code) {
501,742✔
1217
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1218
              __FILE__, __LINE__, pReq->tbName, version);
1219
    TAOS_RETURN(code);
×
1220
  }
1221

1222
  if (pEntry->version >= version) {
501,742✔
1223
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1224
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1225
    metaFetchEntryFree(&pEntry);
×
1226
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1227
  }
1228

1229
  // search the column to update
1230
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
501,742✔
1231
  SSchema        *pColumn = NULL;
501,742✔
1232
  int32_t         iColumn = 0;
501,742✔
1233
  int32_t         rowSize = 0;
501,742✔
1234
  for (int32_t i = 0; i < pSchema->nCols; i++) {
38,218,588✔
1235
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
37,716,846✔
1236
      pColumn = &pSchema->pSchema[i];
501,742✔
1237
      iColumn = i;
501,742✔
1238
    }
1239
    rowSize += pSchema->pSchema[i].bytes;
37,716,846✔
1240
  }
1241

1242
  if (NULL == pColumn) {
501,742✔
1243
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1244
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1245
    metaFetchEntryFree(&pEntry);
×
1246
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1247
  }
1248

1249
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
501,742✔
1250
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
200,032✔
1251
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1252
              version);
1253
    metaFetchEntryFree(&pEntry);
200,032✔
1254
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
200,032✔
1255
  }
1256

1257
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
301,710✔
1258
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
301,710✔
1259
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
46,060✔
1260
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1261
              version);
1262
    metaFetchEntryFree(&pEntry);
46,060✔
1263
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
46,060✔
1264
  }
1265

1266
  // do change the column bytes
1267
  pEntry->version = version;
255,650✔
1268
  pSchema->version++;
255,650✔
1269
  pColumn->bytes = pReq->colModBytes;
255,650✔
1270

1271
  // do handle entry
1272
  code = metaHandleEntry2(pMeta, pEntry);
255,650✔
1273
  if (code) {
255,650✔
1274
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1275
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1276
    metaFetchEntryFree(&pEntry);
×
1277
    TAOS_RETURN(code);
×
1278
  } else {
1279
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
255,650✔
1280
             pEntry->uid, version);
1281
  }
1282

1283
  // build response
1284
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
255,650✔
1285
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
27,093✔
1286
                                pEntry->type);
27,093✔
1287
    if (code) {
27,093✔
1288
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1289
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1290
    } else {
1291
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,697,945✔
1292
        SColRef *p = &pEntry->colRef.pColRef[i];
18,670,852✔
1293
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,670,852✔
1294
        pRsp->pColRefs[i].id = p->id;
18,670,852✔
1295
        if (p->hasRef) {
18,670,852✔
1296
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
78,952✔
1297
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
78,952✔
1298
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
78,952✔
1299
        }
1300
      }
1301
    }
1302
  } else {
1303
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
228,557✔
1304
    if (code) {
228,557✔
1305
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1306
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1307
    } else {
1308
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,184,779✔
1309
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,956,222✔
1310
        pRsp->pSchemaExt[i].colId = p->id;
8,956,222✔
1311
        pRsp->pSchemaExt[i].compress = p->alg;
8,956,222✔
1312
      }
1313
    }
1314
  }
1315

1316
  metaFetchEntryFree(&pEntry);
255,650✔
1317
  TAOS_RETURN(code);
255,650✔
1318
}
1319

1320
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,830,091✔
1321
  int32_t code = 0;
7,830,091✔
1322

1323
  // check tag name
1324
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,830,091✔
1325
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1326
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1327
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1328
  }
1329

1330
  // check name
1331
  void   *value = NULL;
7,830,091✔
1332
  int32_t valueSize = 0;
7,830,091✔
1333
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,830,091✔
1334
  if (code) {
7,830,091✔
1335
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
279✔
1336
              __FILE__, __LINE__, pReq->tbName, version);
1337
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
279✔
1338
    TAOS_RETURN(code);
279✔
1339
  }
1340
  tdbFreeClear(value);
7,829,812✔
1341

1342
  TAOS_RETURN(code);
7,829,812✔
1343
}
1344

1345
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1346

1347
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,727,628✔
1348
  if (isNull) {
7,727,628✔
1349
    if (!tTagGet(pOldTag, &value)) {
47,595✔
1350
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
20,534✔
1351
      return true;
20,534✔
1352
    }
1353
    return false;
27,061✔
1354
  }
1355
  if (!tTagGet(pOldTag, &value)){
7,680,033✔
1356
    return false;
184,832✔
1357
  }
1358
  if (IS_VAR_DATA_TYPE(value.type)) {
7,495,201✔
1359
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
59,427✔
1360
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
37,708✔
1361
      return true;
37,708✔
1362
    }
1363
  } else {
1364
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,435,774✔
1365
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
93,044✔
1366
      return true;
93,044✔
1367
    }
1368
  }
1369
  return false;
7,364,449✔
1370
}
1371

1372
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,830,091✔
1373
  int32_t code = TSDB_CODE_SUCCESS;
7,830,091✔
1374

1375
  // check request
1376
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,830,091✔
1377
  if (code) {
7,830,091✔
1378
    TAOS_RETURN(code);
279✔
1379
  }
1380

1381
  // fetch child entry
1382
  SMetaEntry *pChild = NULL;
7,829,812✔
1383
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,829,812✔
1384
  if (code) {
7,829,812✔
1385
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1386
              __FILE__, __LINE__, pReq->tbName, version);
1387
    TAOS_RETURN(code);
×
1388
  }
1389

1390
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
7,829,812✔
1391
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1392
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1393
    metaFetchEntryFree(&pChild);
×
1394
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1395
  }
1396

1397
  // fetch super entry
1398
  SMetaEntry *pSuper = NULL;
7,829,812✔
1399
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,829,812✔
1400
  if (code) {
7,829,812✔
1401
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1402
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1403
    metaFetchEntryFree(&pChild);
×
1404
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1405
  }
1406

1407
  // search the tag to update
1408
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,829,812✔
1409
  SSchema        *pColumn = NULL;
7,829,812✔
1410
  int32_t         iColumn = 0;
7,829,812✔
1411
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,513,569✔
1412
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,513,569✔
1413
      pColumn = &pTagSchema->pSchema[i];
7,829,812✔
1414
      iColumn = i;
7,829,812✔
1415
      break;
7,829,812✔
1416
    }
1417
  }
1418

1419
  if (NULL == pColumn) {
7,829,812✔
1420
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1421
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1422
    metaFetchEntryFree(&pChild);
×
1423
    metaFetchEntryFree(&pSuper);
×
1424
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1425
  }
1426

1427
  // do change tag value
1428
  pChild->version = version;
7,829,812✔
1429
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,829,812✔
1430
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
123,000✔
1431
    if (NULL == pNewTag) {
123,000✔
1432
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1433
                __LINE__, tstrerror(terrno), version);
1434
      metaFetchEntryFree(&pChild);
×
1435
      metaFetchEntryFree(&pSuper);
×
1436
      TAOS_RETURN(terrno);
×
1437
    }
1438
    pChild->ctbEntry.pTags = pNewTag;
123,000✔
1439
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
123,000✔
1440
  } else {
1441
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,706,812✔
1442

1443
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,706,812✔
1444
    if (NULL == pTagArray) {
7,706,812✔
1445
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1446
                __LINE__, tstrerror(terrno), version);
1447
      metaFetchEntryFree(&pChild);
×
1448
      metaFetchEntryFree(&pSuper);
×
1449
      TAOS_RETURN(terrno);
×
1450
    }
1451

1452
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
16,377,571✔
1453
      STagVal value = {
8,812,665✔
1454
          .type = pTagSchema->pSchema[i].type,
8,812,665✔
1455
          .cid = pTagSchema->pSchema[i].colId,
8,812,665✔
1456
      };
1457

1458
      if (iColumn == i) {
8,812,665✔
1459
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,706,812✔
1460
          taosArrayDestroy(pTagArray);
141,906✔
1461
          metaFetchEntryFree(&pChild);
141,906✔
1462
          metaFetchEntryFree(&pSuper);
141,906✔
1463
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
141,906✔
1464
        }
1465
        if (pReq->isNull) {
7,564,906✔
1466
          continue;
25,105✔
1467
        }
1468
        if (IS_VAR_DATA_TYPE(value.type)) {
7,539,855✔
1469
          value.pData = pReq->pTagVal;
103,950✔
1470
          value.nData = pReq->nTagVal;
103,950✔
1471
        } else {
1472
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,435,905✔
1473
        }
1474
      } else if (!tTagGet(pOldTag, &value)) {
1,105,853✔
1475
        continue;
301,257✔
1476
      }
1477

1478
      if (NULL == taosArrayPush(pTagArray, &value)) {
8,344,451✔
1479
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1480
                  __LINE__, tstrerror(terrno), version);
1481
        taosArrayDestroy(pTagArray);
×
1482
        metaFetchEntryFree(&pChild);
×
1483
        metaFetchEntryFree(&pSuper);
×
1484
        TAOS_RETURN(terrno);
×
1485
      }
1486
    }
1487

1488
    STag *pNewTag = NULL;
7,564,906✔
1489
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,564,906✔
1490
    if (code) {
7,564,906✔
1491
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1492
                __LINE__, tstrerror(code), version);
1493
      taosArrayDestroy(pTagArray);
×
1494
      metaFetchEntryFree(&pChild);
×
1495
      metaFetchEntryFree(&pSuper);
×
1496
      TAOS_RETURN(code);
×
1497
    }
1498
    taosArrayDestroy(pTagArray);
7,564,906✔
1499
    taosMemoryFree(pChild->ctbEntry.pTags);
7,564,906✔
1500
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,564,906✔
1501
  }
1502

1503
  // do handle entry
1504
  code = metaHandleEntry2(pMeta, pChild);
7,687,906✔
1505
  if (code) {
7,687,906✔
1506
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1507
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1508
    metaFetchEntryFree(&pChild);
×
1509
    metaFetchEntryFree(&pSuper);
×
1510
    TAOS_RETURN(code);
×
1511
  } else {
1512
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
7,687,906✔
1513
             pChild->uid, version);
1514
  }
1515

1516
  // free resource and return
1517
  metaFetchEntryFree(&pChild);
7,687,906✔
1518
  metaFetchEntryFree(&pSuper);
7,687,906✔
1519
  TAOS_RETURN(code);
7,687,906✔
1520
}
1521

1522
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,708✔
1523
  int32_t code = 0;
3,708✔
1524

1525
  // check tag name
1526
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,708✔
1527
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1528
              __FILE__, __LINE__, version);
1529
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1530
  }
1531

1532
  // check name
1533
  void   *value = NULL;
3,708✔
1534
  int32_t valueSize = 0;
3,708✔
1535
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,708✔
1536
  if (code) {
3,708✔
1537
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1538
              __FILE__, __LINE__, pReq->tbName, version);
1539
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1540
    TAOS_RETURN(code);
×
1541
  }
1542
  tdbFreeClear(value);
3,708✔
1543

1544
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,708✔
1545
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1546
              __FILE__, __LINE__, version);
1547
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1548
  }
1549

1550
  TAOS_RETURN(code);
3,708✔
1551
}
1552

1553
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,708✔
1554
  int32_t code = TSDB_CODE_SUCCESS;
3,708✔
1555

1556
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,708✔
1557
  if (code) {
3,708✔
1558
    TAOS_RETURN(code);
×
1559
  }
1560

1561
  // fetch child entry
1562
  SMetaEntry *pChild = NULL;
3,708✔
1563
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,708✔
1564
  if (code) {
3,708✔
1565
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1566
              __FILE__, __LINE__, pReq->tbName, version);
1567
    TAOS_RETURN(code);
×
1568
  }
1569

1570
  if (pChild->type != TSDB_CHILD_TABLE) {
3,708✔
1571
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1572
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1573
    metaFetchEntryFree(&pChild);
×
1574
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1575
  }
1576

1577
  // fetch super entry
1578
  SMetaEntry *pSuper = NULL;
3,708✔
1579
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,708✔
1580
  if (code) {
3,708✔
1581
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1582
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1583
    metaFetchEntryFree(&pChild);
×
1584
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1585
  }
1586

1587
  // search the tags to update
1588
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,708✔
1589

1590
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,708✔
1591
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1592
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1593
    metaFetchEntryFree(&pChild);
×
1594
    metaFetchEntryFree(&pSuper);
×
1595
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1596
  }
1597

1598
  // do check if tag name exists
1599
  SHashObj *pTagTable =
1600
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
3,708✔
1601
  if (pTagTable == NULL) {
3,708✔
1602
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1603
              __LINE__, tstrerror(terrno), version);
1604
    metaFetchEntryFree(&pChild);
×
1605
    metaFetchEntryFree(&pSuper);
×
1606
    TAOS_RETURN(terrno);
×
1607
  }
1608

1609
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
24,524✔
1610
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
20,816✔
1611
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
20,816✔
1612
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1613
                __LINE__, tstrerror(terrno), version);
1614
      taosHashCleanup(pTagTable);
×
1615
      metaFetchEntryFree(&pChild);
×
1616
      metaFetchEntryFree(&pSuper);
×
1617
      TAOS_RETURN(terrno);
×
1618
    }
1619
  }
1620

1621
  int32_t numOfChangedTags = 0;
3,708✔
1622
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,874✔
1623
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
24,166✔
1624
        ? numOfChangedTags++
20,816✔
1625
        : 0;
24,166✔
1626
  }
1627
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,708✔
1628
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1629
              __FILE__, __LINE__, version);
1630
    taosHashCleanup(pTagTable);
×
1631
    metaFetchEntryFree(&pChild);
×
1632
    metaFetchEntryFree(&pSuper);
×
1633
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1634
  }
1635

1636
  // do change tag value
1637
  pChild->version = version;
3,708✔
1638
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,708✔
1639
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,708✔
1640
  if (NULL == pTagArray) {
3,708✔
1641
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1642
              __LINE__, tstrerror(terrno), version);
1643
    taosHashCleanup(pTagTable);
×
1644
    metaFetchEntryFree(&pChild);
×
1645
    metaFetchEntryFree(&pSuper);
×
1646
    TAOS_RETURN(terrno);
×
1647
  }
1648

1649
  bool allSame = true;
3,708✔
1650

1651
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,874✔
1652
    SSchema *pCol = &pTagSchema->pSchema[i];
24,166✔
1653
    STagVal  value = {
24,166✔
1654
         .cid = pCol->colId,
24,166✔
1655
    };
1656

1657
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
24,166✔
1658
    if (pTagVal == NULL) {
24,166✔
1659
      if (!tTagGet(pOldTag, &value)) {
3,350✔
1660
        continue;
×
1661
      }
1662
    } else {
1663
      value.type = pCol->type;
20,816✔
1664
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
20,816✔
1665
        allSame = false;
11,436✔
1666
      }
1667
      if (pTagVal->isNull) {
20,816✔
1668
        continue;
4,020✔
1669
      }
1670

1671
      if (IS_VAR_DATA_TYPE(pCol->type)) {
16,796✔
1672
        value.pData = pTagVal->pTagVal;
2,680✔
1673
        value.nData = pTagVal->nTagVal;
2,680✔
1674
      } else {
1675
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
14,116✔
1676
      }
1677
    }
1678

1679
    if (taosArrayPush(pTagArray, &value) == NULL) {
20,146✔
1680
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1681
                __LINE__, tstrerror(terrno), version);
1682
      taosHashCleanup(pTagTable);
×
1683
      taosArrayDestroy(pTagArray);
×
1684
      metaFetchEntryFree(&pChild);
×
1685
      metaFetchEntryFree(&pSuper);
×
1686
      TAOS_RETURN(terrno);
×
1687
    }
1688
  }
1689

1690
  if (allSame) {
3,708✔
1691
    metaWarn("vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
670✔
1692
             __LINE__, version);
1693
    taosHashCleanup(pTagTable);
670✔
1694
    taosArrayDestroy(pTagArray);
670✔
1695
    metaFetchEntryFree(&pChild);
670✔
1696
    metaFetchEntryFree(&pSuper);
670✔
1697
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
670✔
1698
  } 
1699
  STag *pNewTag = NULL;
3,038✔
1700
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
3,038✔
1701
  if (code) {
3,038✔
1702
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1703
              __LINE__, tstrerror(code), version);
1704
    taosHashCleanup(pTagTable);
×
1705
    taosArrayDestroy(pTagArray);
×
1706
    metaFetchEntryFree(&pChild);
×
1707
    metaFetchEntryFree(&pSuper);
×
1708
    TAOS_RETURN(code);
×
1709
  }
1710
  taosArrayDestroy(pTagArray);
3,038✔
1711
  taosMemoryFree(pChild->ctbEntry.pTags);
3,038✔
1712
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
3,038✔
1713

1714
  // do handle entry
1715
  code = metaHandleEntry2(pMeta, pChild);
3,038✔
1716
  if (code) {
3,038✔
1717
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1718
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1719
    taosHashCleanup(pTagTable);
×
1720
    metaFetchEntryFree(&pChild);
×
1721
    metaFetchEntryFree(&pSuper);
×
1722
    TAOS_RETURN(code);
×
1723
  } else {
1724
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,038✔
1725
             pChild->uid, version);
1726
  }
1727

1728
  taosHashCleanup(pTagTable);
3,038✔
1729
  metaFetchEntryFree(&pChild);
3,038✔
1730
  metaFetchEntryFree(&pSuper);
3,038✔
1731
  TAOS_RETURN(code);
3,038✔
1732
}
1733

1734
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
25,518✔
1735
  int32_t code = TSDB_CODE_SUCCESS;
25,518✔
1736

1737
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
25,518✔
1738
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1739
              __FILE__, __LINE__, version);
1740
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1741
  }
1742

1743
  return code;
25,518✔
1744
}
1745

1746
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
25,518✔
1747
  int32_t code = 0;
25,518✔
1748

1749
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
25,518✔
1750
  if (code) {
25,518✔
1751
    TAOS_RETURN(code);
×
1752
  }
1753

1754
  // fetch entry
1755
  SMetaEntry *pEntry = NULL;
25,518✔
1756
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
25,518✔
1757
  if (code) {
25,518✔
1758
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1759
              __FILE__, __LINE__, pReq->tbName, version);
1760
    TAOS_RETURN(code);
×
1761
  }
1762

1763
  // do change the entry
1764
  pEntry->version = version;
25,518✔
1765
  if (pEntry->type == TSDB_CHILD_TABLE) {
25,518✔
1766
    if (pReq->updateTTL) {
11,975✔
1767
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,635✔
1768
    }
1769
    if (pReq->newCommentLen >= 0) {
11,975✔
1770
      char *pNewComment = NULL;
7,340✔
1771
      if (pReq->newCommentLen) {
7,340✔
1772
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,675✔
1773
        if (NULL == pNewComment) {
4,675✔
1774
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1775
                    __LINE__, tstrerror(terrno), version);
1776
          metaFetchEntryFree(&pEntry);
×
1777
          TAOS_RETURN(terrno);
×
1778
        }
1779
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,675✔
1780
      } else {
1781
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,665✔
1782
      }
1783
      pEntry->ctbEntry.comment = pNewComment;
7,340✔
1784
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
7,340✔
1785
    }
1786
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
13,543✔
1787
    if (pReq->updateTTL) {
13,543✔
1788
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,753✔
1789
    }
1790
    if (pReq->newCommentLen >= 0) {
13,543✔
1791
      char *pNewComment = NULL;
8,790✔
1792
      if (pReq->newCommentLen > 0) {
8,790✔
1793
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
6,125✔
1794
        if (NULL == pNewComment) {
6,125✔
1795
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1796
                    __LINE__, tstrerror(terrno), version);
1797
          metaFetchEntryFree(&pEntry);
×
1798
          TAOS_RETURN(terrno);
×
1799
        }
1800
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
6,125✔
1801
      } else {
1802
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,665✔
1803
      }
1804
      pEntry->ntbEntry.comment = pNewComment;
8,790✔
1805
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,790✔
1806
    }
1807
  } else {
1808
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1809
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1810
    metaFetchEntryFree(&pEntry);
×
1811
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1812
  }
1813

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

1826
  metaFetchEntryFree(&pEntry);
25,518✔
1827
  TAOS_RETURN(code);
25,518✔
1828
}
1829

1830
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,164✔
1831
  int32_t code = TSDB_CODE_SUCCESS;
6,164✔
1832

1833
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,164✔
1834
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1835
              __FILE__, __LINE__, version);
1836
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1837
  }
1838

1839
  SMetaEntry *pEntry = NULL;
6,164✔
1840
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,164✔
1841
  if (code) {
6,164✔
1842
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1843
              __FILE__, __LINE__, pReq->tbName, version);
1844
    TAOS_RETURN(code);
×
1845
  }
1846

1847
  if (pEntry->version >= version) {
6,164✔
1848
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1849
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1850
    metaFetchEntryFree(&pEntry);
×
1851
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1852
  }
1853

1854
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,164✔
1855
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1856
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1857
    metaFetchEntryFree(&pEntry);
×
1858
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1859
  }
1860

1861
  // do change the entry
1862
  int8_t           updated = 0;
6,164✔
1863
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,164✔
1864
  for (int32_t i = 0; i < wp->nCols; i++) {
49,312✔
1865
    SColCmpr *p = &wp->pColCmpr[i];
43,148✔
1866
    if (p->id == pReq->colId) {
43,148✔
1867
      uint32_t dst = 0;
6,164✔
1868
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,164✔
1869
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1870
      if (updated > 0) {
6,164✔
1871
        p->alg = dst;
6,164✔
1872
      }
1873
    }
1874
  }
1875

1876
  if (updated == 0) {
6,164✔
1877
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1878
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1879
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1880
    metaFetchEntryFree(&pEntry);
×
1881
    TAOS_RETURN(code);
×
1882
  } else if (updated < 0) {
6,164✔
1883
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1884
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1885
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1886
    metaFetchEntryFree(&pEntry);
×
1887
    TAOS_RETURN(code);
×
1888
  }
1889

1890
  pEntry->version = version;
6,164✔
1891

1892
  // do handle entry
1893
  code = metaHandleEntry2(pMeta, pEntry);
6,164✔
1894
  if (code) {
6,164✔
1895
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1896
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1897
    metaFetchEntryFree(&pEntry);
×
1898
    TAOS_RETURN(code);
×
1899
  } else {
1900
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
6,164✔
1901
             pEntry->uid, version);
1902
  }
1903

1904
  metaFetchEntryFree(&pEntry);
6,164✔
1905
  TAOS_RETURN(code);
6,164✔
1906
}
1907

1908
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
77,957✔
1909
  int32_t code = TSDB_CODE_SUCCESS;
77,957✔
1910

1911
  // check request
1912
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
77,957✔
1913
  if (code) {
77,957✔
1914
    TAOS_RETURN(code);
×
1915
  }
1916

1917
  if (NULL == pReq->refDbName) {
77,957✔
1918
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1919
              __func__, __FILE__, __LINE__, version);
1920
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1921
  }
1922

1923
  if (NULL == pReq->refTbName) {
77,957✔
1924
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1925
              __func__, __FILE__, __LINE__, version);
1926
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1927
  }
1928

1929
  if (NULL == pReq->refColName) {
77,957✔
1930
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1931
              __func__, __FILE__, __LINE__, version);
1932
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1933
  }
1934

1935
  // fetch old entry
1936
  SMetaEntry *pEntry = NULL;
77,957✔
1937
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
77,957✔
1938
  if (code) {
77,957✔
1939
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1940
              __FILE__, __LINE__, pReq->tbName, version);
1941
    TAOS_RETURN(code);
×
1942
  }
1943

1944
  if (pEntry->version >= version) {
77,957✔
1945
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1946
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1947
    metaFetchEntryFree(&pEntry);
×
1948
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1949
  }
1950

1951
  // fetch super entry
1952
  SMetaEntry *pSuper = NULL;
77,957✔
1953
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
77,957✔
1954
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
29,019✔
1955
    if (code) {
29,019✔
1956
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1957
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1958
      metaFetchEntryFree(&pEntry);
×
1959
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1960
    }
1961
  }
1962

1963
  // search the column to update
1964
  SSchemaWrapper *pSchema =
77,957✔
1965
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
77,957✔
1966
  SColRef *pColRef = NULL;
77,957✔
1967
  int32_t  iColumn = 0;
77,957✔
1968
  for (int32_t i = 0; i < pSchema->nCols; i++) {
384,210✔
1969
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
384,210✔
1970
      pColRef = &pEntry->colRef.pColRef[i];
77,957✔
1971
      iColumn = i;
77,957✔
1972
      break;
77,957✔
1973
    }
1974
  }
1975

1976
  if (NULL == pColRef) {
77,957✔
1977
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1978
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1979
    metaFetchEntryFree(&pEntry);
×
1980
    metaFetchEntryFree(&pSuper);
×
1981
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1982
  }
1983

1984
  // do update column name
1985
  pEntry->version = version;
77,957✔
1986
  pColRef->hasRef = true;
77,957✔
1987
  pColRef->id = pSchema->pSchema[iColumn].colId;
77,957✔
1988
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
77,957✔
1989
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
77,957✔
1990
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
77,957✔
1991
  pSchema->version++;
77,957✔
1992
  pEntry->colRef.version++;
77,957✔
1993

1994
  // do handle entry
1995
  code = metaHandleEntry2(pMeta, pEntry);
77,957✔
1996
  if (code) {
77,957✔
1997
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1998
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1999
    metaFetchEntryFree(&pEntry);
×
2000
    metaFetchEntryFree(&pSuper);
×
2001
    TAOS_RETURN(code);
×
2002
  } else {
2003
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
77,957✔
2004
             pEntry->uid, version);
2005
  }
2006

2007
  // build response
2008
  code = metaUpdateVtbMetaRsp(
155,914✔
2009
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
77,957✔
2010
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
77,957✔
2011
      pEntry->type);
77,957✔
2012
  if (code) {
77,957✔
2013
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2014
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2015
  } else {
2016
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
577,001✔
2017
      SColRef *p = &pEntry->colRef.pColRef[i];
499,044✔
2018
      pRsp->pColRefs[i].hasRef = p->hasRef;
499,044✔
2019
      pRsp->pColRefs[i].id = p->id;
499,044✔
2020
      if (p->hasRef) {
499,044✔
2021
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
343,623✔
2022
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
343,623✔
2023
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
343,623✔
2024
      }
2025
    }
2026
  }
2027

2028
  metaFetchEntryFree(&pEntry);
77,957✔
2029
  metaFetchEntryFree(&pSuper);
77,957✔
2030
  TAOS_RETURN(code);
77,957✔
2031
}
2032

2033
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
58,378✔
2034
  int32_t code = TSDB_CODE_SUCCESS;
58,378✔
2035

2036
  // check request
2037
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
58,378✔
2038
  if (code) {
58,378✔
2039
    TAOS_RETURN(code);
×
2040
  }
2041

2042
  // fetch old entry
2043
  SMetaEntry *pEntry = NULL;
58,378✔
2044
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
58,378✔
2045
  if (code) {
58,378✔
2046
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2047
              __FILE__, __LINE__, pReq->tbName, version);
2048
    TAOS_RETURN(code);
×
2049
  }
2050

2051
  if (pEntry->version >= version) {
58,378✔
2052
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2053
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2054
    metaFetchEntryFree(&pEntry);
×
2055
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2056
  }
2057

2058
  // fetch super entry
2059
  SMetaEntry *pSuper = NULL;
58,378✔
2060
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
58,378✔
2061
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
29,077✔
2062
    if (code) {
29,077✔
2063
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2064
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2065
      metaFetchEntryFree(&pEntry);
×
2066
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2067
    }
2068
  }
2069

2070
  // search the column to update
2071
  SSchemaWrapper *pSchema =
58,378✔
2072
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
58,378✔
2073
  SColRef *pColRef = NULL;
58,378✔
2074
  int32_t  iColumn = 0;
58,378✔
2075
  for (int32_t i = 0; i < pSchema->nCols; i++) {
233,376✔
2076
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
233,376✔
2077
      pColRef = &pEntry->colRef.pColRef[i];
58,378✔
2078
      iColumn = i;
58,378✔
2079
      break;
58,378✔
2080
    }
2081
  }
2082

2083
  if (NULL == pColRef) {
58,378✔
2084
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2085
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2086
    metaFetchEntryFree(&pEntry);
×
2087
    metaFetchEntryFree(&pSuper);
×
2088
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2089
  }
2090

2091
  // do update column name
2092
  pEntry->version = version;
58,378✔
2093
  pColRef->hasRef = false;
58,378✔
2094
  pColRef->id = pSchema->pSchema[iColumn].colId;
58,378✔
2095
  pSchema->version++;
58,378✔
2096
  pEntry->colRef.version++;
58,378✔
2097

2098
  // do handle entry
2099
  code = metaHandleEntry2(pMeta, pEntry);
58,378✔
2100
  if (code) {
58,378✔
2101
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2102
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2103
    metaFetchEntryFree(&pEntry);
×
2104
    metaFetchEntryFree(&pSuper);
×
2105
    TAOS_RETURN(code);
×
2106
  } else {
2107
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
58,378✔
2108
             pEntry->uid, version);
2109
  }
2110

2111
  // build response
2112
  code = metaUpdateVtbMetaRsp(
116,756✔
2113
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
58,378✔
2114
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
58,378✔
2115
      pEntry->type);
58,378✔
2116
  if (code) {
58,378✔
2117
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2118
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2119
  } else {
2120
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
363,153✔
2121
      SColRef *p = &pEntry->colRef.pColRef[i];
304,775✔
2122
      pRsp->pColRefs[i].hasRef = p->hasRef;
304,775✔
2123
      pRsp->pColRefs[i].id = p->id;
304,775✔
2124
      if (p->hasRef) {
304,775✔
2125
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
152,865✔
2126
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
152,865✔
2127
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
152,865✔
2128
      }
2129
    }
2130
  }
2131

2132
  metaFetchEntryFree(&pEntry);
58,378✔
2133
  metaFetchEntryFree(&pSuper);
58,378✔
2134
  TAOS_RETURN(code);
58,378✔
2135
}
2136

2137
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
18,010✔
2138
  int32_t code = TSDB_CODE_SUCCESS;
18,010✔
2139

2140
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
18,010✔
2141
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2142
              __FILE__, __LINE__, version);
2143
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2144
  }
2145

2146
  SMetaEntry *pEntry = NULL;
18,010✔
2147
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
18,010✔
2148
  if (code) {
18,010✔
2149
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2150
              __FILE__, __LINE__, pReq->name, version);
2151
    TAOS_RETURN(code);
×
2152
  }
2153

2154
  if (pEntry->type != TSDB_SUPER_TABLE) {
18,010✔
2155
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2156
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2157
    metaFetchEntryFree(&pEntry);
×
2158
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2159
  }
2160

2161
  if (pEntry->uid != pReq->suid) {
18,010✔
2162
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2163
              ", version:%" PRId64,
2164
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2165
    metaFetchEntryFree(&pEntry);
×
2166
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2167
  }
2168

2169
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2170
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2171
  //   PRId64,
2172
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2173
  //             pReq->schemaTag.version, version);
2174
  //   metaFetchEntryFree(&pEntry);
2175
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2176
  // }
2177

2178
  // do change the entry
2179
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
18,010✔
2180
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
18,010✔
2181
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
18,010✔
2182
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2183
              __func__, __FILE__, __LINE__, pReq->name, version);
2184
    metaFetchEntryFree(&pEntry);
×
2185
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2186
  }
2187

2188
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
18,010✔
2189
    metaError(
×
2190
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2191
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2192
        version);
2193
    metaFetchEntryFree(&pEntry);
×
2194
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2195
  }
2196

2197
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2198
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2199
  //   PRId64,
2200
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2201
  //             pNewTagSchema->version, version);
2202
  //   metaFetchEntryFree(&pEntry);
2203
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2204
  // }
2205

2206
  int32_t numOfChangedTags = 0;
18,010✔
2207
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
193,742✔
2208
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
175,732✔
2209
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
175,732✔
2210

2211
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
175,732✔
2212
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
175,732✔
2213
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2214
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2215
      metaFetchEntryFree(&pEntry);
×
2216
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2217
    }
2218

2219
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
175,732✔
2220
      numOfChangedTags++;
18,010✔
2221
      SSCHMEA_SET_IDX_ON(pOldColumn);
18,010✔
2222
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
157,722✔
2223
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2224
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2225
      metaFetchEntryFree(&pEntry);
×
2226
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2227
    }
2228
  }
2229

2230
  if (numOfChangedTags != 1) {
18,010✔
2231
    metaError(
×
2232
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2233
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2234
    metaFetchEntryFree(&pEntry);
×
2235
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2236
  }
2237

2238
  pEntry->version = version;
18,010✔
2239
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
18,010✔
2240

2241
  // do handle the entry
2242
  code = metaHandleEntry2(pMeta, pEntry);
18,010✔
2243
  if (code) {
18,010✔
2244
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2245
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2246
    metaFetchEntryFree(&pEntry);
×
2247
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2248
  } else {
2249
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
18,010✔
2250
             pEntry->uid, version);
2251
  }
2252

2253
  metaFetchEntryFree(&pEntry);
18,010✔
2254
  TAOS_RETURN(code);
18,010✔
2255
}
2256

2257
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
14,644✔
2258
  int32_t code = TSDB_CODE_SUCCESS;
14,644✔
2259

2260
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
14,644✔
2261
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2262
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2263
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2264
  }
2265

2266
  SMetaEntry *pEntry = NULL;
14,644✔
2267
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
14,644✔
2268
  if (code) {
14,644✔
2269
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2270
              __FILE__, __LINE__, pReq->stb, version);
2271
    TAOS_RETURN(code);
×
2272
  }
2273

2274
  if (TSDB_SUPER_TABLE != pEntry->type) {
14,644✔
2275
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2276
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2277
    metaFetchEntryFree(&pEntry);
×
2278
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2279
  }
2280

2281
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
14,644✔
2282
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
14,644✔
2283
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2284
              __func__, __FILE__, __LINE__, pReq->stb, version);
2285
    metaFetchEntryFree(&pEntry);
×
2286
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2287
  }
2288

2289
  // search and set the tag index off
2290
  int32_t numOfChangedTags = 0;
14,644✔
2291
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
90,674✔
2292
    SSchema *pCol = pTagSchema->pSchema + i;
90,674✔
2293
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
90,674✔
2294
      if (!IS_IDX_ON(pCol)) {
14,644✔
2295
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2296
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2297
        metaFetchEntryFree(&pEntry);
×
2298
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2299
      }
2300
      numOfChangedTags++;
14,644✔
2301
      SSCHMEA_SET_IDX_OFF(pCol);
14,644✔
2302
      break;
14,644✔
2303
    }
2304
  }
2305

2306
  if (numOfChangedTags != 1) {
14,644✔
2307
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2308
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2309
    metaFetchEntryFree(&pEntry);
×
2310
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2311
  }
2312

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

2327
  metaFetchEntryFree(&pEntry);
14,644✔
2328
  TAOS_RETURN(code);
14,644✔
2329
}
2330

2331
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,742,815✔
2332
  int32_t code = TSDB_CODE_SUCCESS;
7,742,815✔
2333

2334
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,742,815✔
2335
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
27,541✔
2336
              __FILE__, __LINE__, version);
2337
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
27,541✔
2338
  }
2339

2340
  SMetaEntry *pEntry = NULL;
7,715,271✔
2341
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,722,415✔
2342
  if (code) {
7,725,893✔
2343
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
18✔
2344
              __FILE__, __LINE__, pReq->name, version);
2345
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
18✔
2346
  }
2347

2348
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,725,875✔
2349
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2350
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2351
    metaFetchEntryFree(&pEntry);
×
2352
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2353
  }
2354

2355
  SMetaEntry entry = {
23,106,342✔
2356
      .version = version,
2357
      .type = TSDB_SUPER_TABLE,
2358
      .uid = pReq->suid,
7,717,249✔
2359
      .name = pReq->name,
7,730,970✔
2360
      .stbEntry.schemaRow = pReq->schemaRow,
2361
      .stbEntry.schemaTag = pReq->schemaTag,
2362
      .stbEntry.keep = pReq->keep,
7,693,254✔
2363
      .stbEntry.ownerId = pReq->ownerId,
7,692,287✔
2364
      .colCmpr = pReq->colCmpr,
2365
      .pExtSchemas = pReq->pExtSchemas,
7,693,859✔
2366
  };
2367
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,691,717✔
2368
  if (pReq->virtualStb) {
7,691,717✔
2369
    TABLE_SET_VIRTUAL(entry.flags);
20,816✔
2370
  }
2371
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,689,869✔
2372
    TABLE_SET_ROLLUP(entry.flags);
3,750✔
2373
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
3,750✔
2374
  }
2375

2376
  // do handle the entry
2377
  code = metaHandleEntry2(pMeta, &entry);
7,705,552✔
2378
  if (code) {
7,695,686✔
2379
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2380
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2381
    metaFetchEntryFree(&pEntry);
×
2382
    TAOS_RETURN(code);
×
2383
  } else {
2384
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,695,686✔
2385
             pReq->suid, version);
2386
  }
2387

2388
  metaFetchEntryFree(&pEntry);
7,727,956✔
2389
  TAOS_RETURN(code);
7,753,290✔
2390
}
2391

2392
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2393
  int32_t code = 0;
×
2394

2395
  if (taosArrayGetSize(uidArray) == 0) {
×
2396
    return TSDB_CODE_SUCCESS;
×
2397
  }
2398

2399
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2400
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2401
    SMetaInfo info;
×
2402
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2403
    if (code) {
×
2404
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2405
                __func__, __FILE__, __LINE__, uid, code);
2406
      return code;
×
2407
    }
2408

2409
    SMetaEntry entry = {
×
2410
        .version = version,
2411
        .uid = uid,
2412
    };
2413

2414
    if (info.suid == 0) {
×
2415
      entry.type = -TSDB_NORMAL_TABLE;
×
2416
    } else if (info.suid == uid) {
×
2417
      entry.type = -TSDB_SUPER_TABLE;
×
2418
    } else {
2419
      entry.type = -TSDB_CHILD_TABLE;
×
2420
    }
2421
    code = metaHandleEntry2(pMeta, &entry);
×
2422
    if (code) {
×
2423
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2424
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2425
      return code;
×
2426
    }
2427
  }
2428
  return code;
×
2429
}
2430

2431
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
39,810✔
2432
  int32_t code = TSDB_CODE_SUCCESS;
39,810✔
2433

2434
  if (NULL == pReq->name || pReq->name[0] == 0) {
39,810✔
2435
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2436
              __LINE__, version);
2437
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2438
  }
2439

2440
  SMetaEntry *pEntry = NULL;
39,810✔
2441
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
39,810✔
2442
  if (code) {
39,810✔
2443
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2444
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2445
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2446
  }
2447

2448
  if (pEntry->type != TSDB_SUPER_TABLE) {
39,810✔
2449
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2450
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2451
    metaFetchEntryFree(&pEntry);
×
2452
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2453
  }
2454

2455
  if (pEntry->uid != pReq->tbUid) {
39,060✔
2456
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2457
              ", version:%" PRId64,
2458
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2459
    metaFetchEntryFree(&pEntry);
×
2460
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2461
  }
2462

2463
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
39,060✔
2464
    // overwrite the old rsma definition if exists
2465
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2466
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2467
  } else {
2468
    TABLE_SET_ROLLUP(pEntry->flags);
39,810✔
2469
  }
2470

2471
  SMetaEntry entry = *pEntry;
38,310✔
2472
  entry.version = version;
39,060✔
2473
  entry.stbEntry.rsmaParam.name = pReq->name;
39,060✔
2474
  entry.stbEntry.rsmaParam.uid = pReq->uid;
38,310✔
2475
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
39,810✔
2476
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
39,810✔
2477
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
39,810✔
2478
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
39,060✔
2479
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
39,060✔
2480
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
39,060✔
2481

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

2496
  metaFetchEntryFree(&pEntry);
39,810✔
2497
  TAOS_RETURN(code);
39,810✔
2498
}
2499

2500
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
20,040✔
2501
  int32_t code = TSDB_CODE_SUCCESS;
20,040✔
2502

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

2509
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
20,040✔
2510
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2511
              __LINE__, version);
2512
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2513
  }
2514

2515
  SMetaEntry *pEntry = NULL;
20,040✔
2516
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
20,040✔
2517
  if (code) {
20,040✔
2518
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2519
             __func__, __LINE__, pReq->name, pReq->tbName, version);
2520
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2521
  }
2522

2523
  if (pEntry->type != pReq->tbType) {
20,040✔
2524
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2525
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2526
    metaFetchEntryFree(&pEntry);
×
2527
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2528
  }
2529

2530
  if (pEntry->uid != pReq->tbUid) {
20,040✔
2531
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2532
              ", version:%" PRId64,
2533
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2534
    metaFetchEntryFree(&pEntry);
×
2535
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2536
  }
2537

2538
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
20,040✔
2539
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
20,040✔
2540
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
20,040✔
UNCOV
2541
      metaError(
×
2542
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2543
          "uid:%" PRIi64 ", version:%" PRId64,
2544
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2545
          pReq->uid, version);
UNCOV
2546
      metaFetchEntryFree(&pEntry);
×
2547
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2548
    }
2549
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
20,040✔
2550
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
20,040✔
2551
  } else {
2552
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2553
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
2554
    metaFetchEntryFree(&pEntry);
×
2555
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2556
  }
2557

2558
  SMetaEntry entry = *pEntry;
20,040✔
2559
  entry.version = version;
20,040✔
2560
  TABLE_RESET_ROLLUP(entry.flags);
20,040✔
2561
  entry.stbEntry.rsmaParam.uid = 0;
20,040✔
2562
  entry.stbEntry.rsmaParam.name = NULL;
20,040✔
2563
  entry.stbEntry.rsmaParam.nFuncs = 0;
20,040✔
2564
  entry.stbEntry.rsmaParam.funcColIds = NULL;
20,040✔
2565
  entry.stbEntry.rsmaParam.funcIds = NULL;
20,040✔
2566

2567
  // do handle the entry
2568
  code = metaHandleEntry2(pMeta, &entry);
20,040✔
2569
  if (code) {
20,040✔
2570
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2571
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
2572
    metaFetchEntryFree(&pEntry);
×
2573
    TAOS_RETURN(code);
×
2574
  } else {
2575
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
20,040✔
2576
      pMeta->pVnode->config.isRsma = 0;
×
2577
    }
2578
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
20,040✔
2579
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2580
  }
2581

2582
  metaFetchEntryFree(&pEntry);
20,040✔
2583
  TAOS_RETURN(code);
20,040✔
2584
}
2585

2586
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,270✔
2587
  int32_t code = TSDB_CODE_SUCCESS;
12,270✔
2588

2589
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,270✔
2590
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2591
              __LINE__, version);
2592
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2593
  }
2594

2595
  SMetaEntry *pEntry = NULL;
12,270✔
2596
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
12,270✔
2597
  if (code) {
12,270✔
2598
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2599
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2600
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2601
  }
2602

2603
  if (pEntry->uid != pReq->tbUid) {
12,270✔
2604
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2605
              ", version:%" PRId64,
2606
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2607
    metaFetchEntryFree(&pEntry);
×
2608
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2609
  }
2610

2611
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
12,270✔
2612
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
12,270✔
2613
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,270✔
2614
  } else {
2615
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2616
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2617
    metaFetchEntryFree(&pEntry);
×
2618
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2619
  }
2620

2621
  SMetaEntry entry = *pEntry;
12,270✔
2622
  entry.version = version;
12,270✔
2623
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
12,270✔
2624
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
12,270✔
2625
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
12,270✔
2626
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
12,270✔
2627
  }
2628
  // do handle the entry
2629
  code = metaHandleEntry2(pMeta, &entry);
12,270✔
2630
  if (code) {
12,270✔
2631
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2632
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2633
    metaFetchEntryFree(&pEntry);
×
2634
    TAOS_RETURN(code);
×
2635
  } else {
2636
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
12,270✔
2637
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2638
  }
2639

2640
  metaFetchEntryFree(&pEntry);
12,270✔
2641
  TAOS_RETURN(code);
12,270✔
2642
}
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