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

taosdata / TDengine / #4954

09 Feb 2026 01:16AM UTC coverage: 66.876% (-0.001%) from 66.877%
#4954

push

travis-ci

web-flow
docs: add support for recording STMT to CSV files (#34276)

* docs: add support for recording STMT to CSV files

* docs: update version for STMT recording feature in CSV files

205775 of 307696 relevant lines covered (66.88%)

127898023.42 hits per line

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

67.55
/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) {
4,963,186✔
31
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,963,186✔
32
  void     *value = NULL;
4,985,921✔
33
  int32_t   valueSize = 0;
4,984,966✔
34
  SMetaInfo info;
4,985,621✔
35

36
  // check name
37
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,986,331✔
38
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
1,012✔
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);
4,981,660✔
44
  if (r == 0) {  // name exists, check uid and type
4,975,891✔
45
    int64_t uid = *(tb_uid_t *)value;
2,888✔
46
    tdbFree(value);
2,888✔
47

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

55
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
×
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) {
×
63
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
×
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) {
4,973,003✔
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;
4,977,160✔
80
}
81

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

88
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,358,408✔
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,358,408✔
95
  if (TSDB_CODE_SUCCESS != code) {
1,358,408✔
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,358,408✔
106
  tdbFreeClear(value);
1,358,408✔
107

108
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
1,358,408✔
109
  if (TSDB_CODE_SUCCESS != code) {
1,358,408✔
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,358,408✔
117

118
  return code;
1,358,408✔
119
}
120

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

127
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
868,374✔
128
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
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);
868,374✔
134
  if (code) {
867,046✔
135
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
722✔
136
              __FILE__, __LINE__, pReq->name, version);
137
    return TSDB_CODE_TDB_STB_NOT_EXIST;
722✔
138
  } else {
139
    int64_t uid = *(int64_t *)value;
866,324✔
140
    tdbFreeClear(value);
866,948✔
141

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

149
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
865,098✔
150
  if (code) {
866,192✔
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) {
866,192✔
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;
866,192✔
162
}
163

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

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

180
  // handle entry
181
  SMetaEntry entry = {
9,948,398✔
182
      .version = version,
183
      .type = TSDB_SUPER_TABLE,
184
      .uid = pReq->suid,
4,975,199✔
185
      .name = pReq->name,
4,972,579✔
186
      .stbEntry.schemaRow = pReq->schemaRow,
187
      .stbEntry.schemaTag = pReq->schemaTag,
188
      .stbEntry.keep = pReq->keep,
4,976,220✔
189
      .stbEntry.ownerId = pReq->ownerId,
4,964,699✔
190
  };
191
  if (pReq->rollup) {
4,961,915✔
192
    TABLE_SET_ROLLUP(entry.flags);
×
193
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
194
  }
195
  if (pReq->colCmpred) {
4,962,488✔
196
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,969,208✔
197
    entry.colCmpr = pReq->colCmpr;
4,969,208✔
198
  }
199

200
  entry.pExtSchemas = pReq->pExtSchemas;
4,961,067✔
201

202
  if (pReq->virtualStb) {
4,969,318✔
203
    TABLE_SET_VIRTUAL(entry.flags);
66,584✔
204
  }
205

206
  code = metaHandleEntry2(pMeta, &entry);
4,965,149✔
207
  if (TSDB_CODE_SUCCESS == code) {
4,984,275✔
208
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,984,275✔
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);
4,984,729✔
215
}
216

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

221
  // check request
222
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
868,374✔
223
  if (code) {
867,636✔
224
    TAOS_RETURN(code);
1,444✔
225
  }
226

227
  // handle entry
228
  SMetaEntry entry = {
866,192✔
229
      .version = verison,
230
      .type = -TSDB_SUPER_TABLE,
231
      .uid = pReq->suid,
865,112✔
232
  };
233
  code = metaHandleEntry2(pMeta, &entry);
866,226✔
234
  if (code) {
866,930✔
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,
866,930✔
239
             pReq->suid, verison);
240
  }
241
  TAOS_RETURN(code);
866,930✔
242
}
243

244
// Alter Super Table
245

246
// Create Child Table
247
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
55,313,279✔
248
  int32_t   code = TSDB_CODE_SUCCESS;
55,313,279✔
249
  void     *value = NULL;
55,313,279✔
250
  int32_t   valueSize = 0;
55,312,682✔
251
  SMetaInfo info;
55,311,707✔
252

253
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
55,311,616✔
254
      pReq->ctb.suid == 0) {
55,310,664✔
255
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
3,378✔
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) {
55,309,130✔
262
    pReq->uid = *(int64_t *)value;
317,058✔
263
    tdbFreeClear(value);
317,058✔
264

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

279
    // check suid
280
    if (info.suid != pReq->ctb.suid) {
315,314✔
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;
315,314✔
289
  }
290

291
  // check super table existence
292
  SMetaEntry *pStbEntry = NULL;
54,994,981✔
293
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
54,994,215✔
294
  if (code) {
54,989,692✔
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) {
54,989,692✔
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) {
54,990,152✔
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;
54,989,545✔
318
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
54,992,669✔
319
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
54,991,440✔
320
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
242,618,654✔
321
      STagVal tagVal = {
187,880,201✔
322
          .cid = pTagSchema->pSchema[i].colId,
187,873,936✔
323
      };
324

325
      if (tTagGet(pTag, &tagVal)) {
187,869,169✔
326
        if (pTagSchema->pSchema[i].type != tagVal.type) {
146,682,288✔
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);
54,990,447✔
337

338
  // check grant
339
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
54,984,218✔
340
    code = grantCheck(TSDB_GRANT_TIMESERIES);
54,995,187✔
341
    if (TSDB_CODE_SUCCESS != code) {
54,988,087✔
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;
54,988,907✔
347
}
348

349
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
54,782,815✔
350
  int32_t code = TSDB_CODE_SUCCESS;
54,782,815✔
351

352
  if (NULL == ppRsp) {
54,782,815✔
353
    return code;
×
354
  }
355

356
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
54,782,815✔
357
  if (NULL == ppRsp) {
54,772,245✔
358
    return terrno;
×
359
  }
360

361
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
54,772,245✔
362
  (*ppRsp)->tuid = pEntry->uid;
54,777,752✔
363
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
54,783,456✔
364
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
54,782,650✔
365

366
  return code;
54,784,017✔
367
}
368

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

372
  // check request
373
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
55,108,619✔
374
  if (code) {
55,097,625✔
375
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
316,547✔
376
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,744✔
377
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
378
    }
379
    return code;
317,058✔
380
  }
381

382
  SMetaEntry entry = {
54,781,078✔
383
      .version = version,
384
      .type = TSDB_CHILD_TABLE,
385
      .uid = pReq->uid,
54,781,590✔
386
      .name = pReq->name,
54,781,091✔
387
      .ctbEntry.btime = pReq->btime,
54,787,033✔
388
      .ctbEntry.ttlDays = pReq->ttl,
54,780,587✔
389
      .ctbEntry.commentLen = pReq->commentLen,
54,775,033✔
390
      .ctbEntry.comment = pReq->comment,
54,776,321✔
391
      .ctbEntry.suid = pReq->ctb.suid,
54,787,085✔
392
      .ctbEntry.pTags = pReq->ctb.pTag,
54,779,682✔
393
  };
394

395
  // build response
396
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
54,775,465✔
397
  if (code) {
54,786,621✔
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);
54,786,621✔
404
  if (TSDB_CODE_SUCCESS == code) {
54,790,242✔
405
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
54,790,799✔
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,
15✔
409
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
410
              pReq->ctb.suid, version);
411
  }
412
  return code;
54,794,032✔
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) {
6,921,656✔
421
  int32_t code = 0;
6,921,656✔
422
  void   *value = NULL;
6,921,656✔
423
  int32_t valueSize = 0;
6,921,656✔
424

425
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
6,921,656✔
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) {
6,921,656✔
433
    // for auto create table, we return the uid of the existing table
434
    pReq->uid = *(tb_uid_t *)value;
32,666✔
435
    tdbFree(value);
32,666✔
436
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
32,666✔
437
  }
438

439
  // grant check
440
  code = grantCheck(TSDB_GRANT_TIMESERIES);
6,888,990✔
441
  if (code) {
6,888,990✔
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;
6,888,990✔
446
}
447

448
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
6,756,724✔
449
  int32_t code = TSDB_CODE_SUCCESS;
6,756,724✔
450

451
  if (NULL == ppRsp) {
6,756,724✔
452
    return code;
×
453
  }
454

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

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

466
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
77,019,753✔
467
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
70,263,029✔
468
    (*ppRsp)->pSchemaExt[i].colId = p->id;
70,263,029✔
469
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
70,263,029✔
470
    if (pEntry->pExtSchemas) {
70,263,029✔
471
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
427,727✔
472
    }
473
  }
474

475
  return code;
6,756,724✔
476
}
477

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

481
  // check request
482
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
6,789,390✔
483
  if (code) {
6,789,390✔
484
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
32,666✔
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);
32,666✔
489
  }
490

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

508
  // build response
509
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
6,756,724✔
510
  if (code) {
6,756,724✔
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);
6,756,724✔
517
  if (TSDB_CODE_SUCCESS == code) {
6,756,724✔
518
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
6,756,724✔
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);
6,756,724✔
525
}
526

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

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

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

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

546
  return code;
132,266✔
547
}
548

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

573
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
132,266✔
574
  if (code) {
132,266✔
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);
132,266✔
582
  if (TSDB_CODE_SUCCESS == code) {
132,266✔
583
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
132,266✔
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);
132,266✔
590
#if 0
591
  metaTimeSeriesNotifyCheck(pMeta);
592
#endif
593
}
594

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

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

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

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

614
  return code;
204,660✔
615
}
616

617
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
204,660✔
618
  // check request
619
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
204,660✔
620
  if (code) {
204,660✔
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,
409,320✔
629
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
630
                      .uid = pReq->uid,
204,660✔
631
                      .name = pReq->name,
204,660✔
632
                      .ctbEntry.btime = pReq->btime,
204,660✔
633
                      .ctbEntry.ttlDays = pReq->ttl,
204,660✔
634
                      .ctbEntry.commentLen = pReq->commentLen,
204,660✔
635
                      .ctbEntry.comment = pReq->comment,
204,660✔
636
                      .ctbEntry.suid = pReq->ctb.suid,
204,660✔
637
                      .ctbEntry.pTags = pReq->ctb.pTag,
204,660✔
638
                      .colRef = pReq->colRef};
639

640
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
204,660✔
641
  if (code) {
204,660✔
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);
204,660✔
649
  if (TSDB_CODE_SUCCESS == code) {
204,660✔
650
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
204,660✔
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);
204,660✔
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) {
62,233,803✔
667
  int32_t code = TSDB_CODE_SUCCESS;
62,233,803✔
668
  if (TSDB_CHILD_TABLE == pReq->type) {
62,233,803✔
669
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
55,108,211✔
670
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
7,126,316✔
671
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
6,789,390✔
672
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
336,926✔
673
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
132,266✔
674
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
204,660✔
675
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
204,660✔
676
  } else {
677
    code = TSDB_CODE_INVALID_MSG;
×
678
  }
679
  TAOS_RETURN(code);
62,236,420✔
680
}
681

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

685
  // check request
686
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,358,408✔
687
  if (code) {
1,358,408✔
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,358,408✔
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,358,408✔
703
      .version = version,
704
      .uid = pReq->uid,
1,358,408✔
705
  };
706

707
  if (pReq->isVirtual) {
1,358,408✔
708
    if (pReq->suid == 0) {
52,666✔
709
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,586✔
710
    } else {
711
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,080✔
712
    }
713
  } else {
714
    if (pReq->suid == 0) {
1,305,742✔
715
      entry.type = -TSDB_NORMAL_TABLE;
755,948✔
716
    } else {
717
      entry.type = -TSDB_CHILD_TABLE;
549,794✔
718
    }
719
  }
720
  code = metaHandleEntry2(pMeta, &entry);
1,358,408✔
721
  if (code) {
1,358,408✔
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,358,408✔
726
             pReq->uid, version);
727
  }
728
  TAOS_RETURN(code);
1,358,408✔
729
}
730

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

734
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,155,494✔
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,155,494✔
742
  int32_t valueSize = 0;
4,155,494✔
743
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,155,494✔
744
  if (code) {
4,155,494✔
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,155,494✔
751
  tdbFreeClear(value);
4,155,494✔
752

753
  // check table type
754
  SMetaInfo info;
4,155,494✔
755
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,155,494✔
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,155,494✔
763
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
26,616✔
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,155,494✔
772
  if (code) {
4,155,494✔
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,155,494✔
778
}
779

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

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

789
  // fetch old entry
790
  SMetaEntry *pEntry = NULL;
3,432,370✔
791
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,432,370✔
792
  if (code) {
3,432,370✔
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,432,370✔
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,432,370✔
806
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,432,370✔
807
  SSchema        *pColumn;
808
  SExtSchema      extSchema = {0};
3,432,370✔
809
  pEntry->version = version;
3,432,370✔
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,
307,008✔
814
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
815
      metaFetchEntryFree(&pEntry);
307,008✔
816
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
307,008✔
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,125,362✔
822
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,125,362✔
823
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,240✔
824
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
825
    metaFetchEntryFree(&pEntry);
6,240✔
826
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,240✔
827
  }
828

829
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,119,122✔
830
  if (pSchema->nCols + 1 > maxCols) {
3,119,122✔
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,119,122✔
838
  if (NULL == pNewSchema) {
3,119,122✔
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,119,122✔
845
  pSchema->version++;
3,119,122✔
846
  pSchema->nCols++;
3,119,122✔
847
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,119,122✔
848
  pColumn->bytes = pReq->bytes;
3,119,122✔
849
  pColumn->type = pReq->type;
3,119,122✔
850
  pColumn->flags = pReq->flags;
3,119,122✔
851
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,119,122✔
852
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
526✔
853
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
854
              version);
855
    metaFetchEntryFree(&pEntry);
526✔
856
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
526✔
857
  }
858
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,118,596✔
859
  extSchema.typeMod = pReq->typeMod;
3,118,596✔
860
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,118,596✔
861
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,118,596✔
862
    SColRef tmpRef;
49,881✔
863
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
49,881✔
864
      tmpRef.hasRef = false;
29,553✔
865
      tmpRef.id = pColumn->colId;
29,553✔
866
    } else {
867
      tmpRef.hasRef = true;
20,328✔
868
      tmpRef.id = pColumn->colId;
20,328✔
869
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
20,328✔
870
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
20,328✔
871
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
20,328✔
872
    }
873
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
49,881✔
874
    if (code) {
49,881✔
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,068,715✔
883
      compress = createDefaultColCmprByType(pColumn->type);
3,062,004✔
884
    } else {
885
      compress = pReq->compress;
6,711✔
886
    }
887
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
3,068,715✔
888
    if (code) {
3,068,715✔
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,118,596✔
896
  if (code) {
3,118,596✔
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,118,596✔
905
  if (code) {
3,118,596✔
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,118,596✔
912
             pEntry->uid, version);
913
  }
914

915
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,118,596✔
916
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
49,881✔
917
                                pEntry->type);
49,881✔
918
    if (code) {
49,881✔
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++) {
17,607,269✔
923
        SColRef *p = &pEntry->colRef.pColRef[i];
17,557,388✔
924
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,557,388✔
925
        pRsp->pColRefs[i].id = p->id;
17,557,388✔
926
        if (p->hasRef) {
17,557,388✔
927
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
160,950✔
928
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
160,950✔
929
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
160,950✔
930
        }
931
      }
932
    }
933
  } else {
934
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,068,715✔
935
    if (code) {
3,068,715✔
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,118,596✔
948
  TAOS_RETURN(code);
3,118,596✔
949
}
950

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

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

960
  // fetch old entry
961
  SMetaEntry *pEntry = NULL;
82,533✔
962
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
82,533✔
963
  if (code) {
82,533✔
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) {
82,533✔
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;
82,533✔
978
  SSchema        *pColumn = NULL;
82,533✔
979
  SSchema         tColumn;
82,533✔
980
  int32_t         iColumn = 0;
82,533✔
981
  for (; iColumn < pSchema->nCols; iColumn++) {
52,819,677✔
982
    pColumn = &pSchema->pSchema[iColumn];
52,819,677✔
983
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
52,819,677✔
984
      break;
82,533✔
985
    }
986
  }
987

988
  if (iColumn == pSchema->nCols) {
82,533✔
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) {
82,533✔
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;
82,533✔
1003

1004
  // do drop column
1005
  pEntry->version = version;
82,533✔
1006
  if (pSchema->nCols - iColumn - 1 > 0) {
82,533✔
1007
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
61,199✔
1008
  }
1009
  pSchema->nCols--;
82,533✔
1010
  pSchema->version++;
82,533✔
1011
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
82,533✔
1012
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
28,513✔
1013
    if (code) {
28,513✔
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) {
28,513✔
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);
54,020✔
1027
    if (code) {
54,020✔
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) {
54,020✔
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);
82,533✔
1043
  if (code) {
82,533✔
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);
82,533✔
1052
  if (code) {
82,533✔
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,
82,533✔
1059
             pEntry->uid, version);
1060
  }
1061

1062
  // build response
1063
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
82,533✔
1064
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
28,513✔
1065
                                pEntry->type);
28,513✔
1066
    if (code) {
28,513✔
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++) {
34,651,943✔
1071
        SColRef *p = &pEntry->colRef.pColRef[i];
34,623,430✔
1072
        pRsp->pColRefs[i].hasRef = p->hasRef;
34,623,430✔
1073
        pRsp->pColRefs[i].id = p->id;
34,623,430✔
1074
        if (p->hasRef) {
34,623,430✔
1075
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
17,323,843✔
1076
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
17,323,843✔
1077
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
17,323,843✔
1078
        }
1079
      }
1080
    }
1081
  } else {
1082
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
54,020✔
1083
    if (code) {
54,020✔
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++) {
43,202,709✔
1088
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
43,148,689✔
1089
        pRsp->pSchemaExt[i].colId = p->id;
43,148,689✔
1090
        pRsp->pSchemaExt[i].compress = p->alg;
43,148,689✔
1091
      }
1092
    }
1093
  }
1094

1095
  metaFetchEntryFree(&pEntry);
82,533✔
1096
  TAOS_RETURN(code);
82,533✔
1097
}
1098

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

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

1108
  if (NULL == pReq->colNewName) {
43,652✔
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;
43,652✔
1116
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
43,652✔
1117
  if (code) {
43,652✔
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) {
43,652✔
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;
43,652✔
1132
  SSchema        *pColumn = NULL;
43,652✔
1133
  int32_t         iColumn = 0;
43,652✔
1134
  for (int32_t i = 0; i < pSchema->nCols; i++) {
199,369✔
1135
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
199,369✔
1136
      pColumn = &pSchema->pSchema[i];
43,652✔
1137
      iColumn = i;
43,652✔
1138
      break;
43,652✔
1139
    }
1140
  }
1141

1142
  if (NULL == pColumn) {
43,652✔
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;
43,652✔
1152
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
43,652✔
1153
  pSchema->version++;
43,652✔
1154

1155
  // do handle entry
1156
  code = metaHandleEntry2(pMeta, pEntry);
43,652✔
1157
  if (code) {
43,652✔
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,
43,652✔
1164
             pEntry->uid, version);
1165
  }
1166

1167
  // build response
1168
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
43,652✔
1169
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
26,534✔
1170
                                pEntry->type);
26,534✔
1171
    if (code) {
26,534✔
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++) {
187,494✔
1176
        SColRef *p = &pEntry->colRef.pColRef[i];
160,960✔
1177
        pRsp->pColRefs[i].hasRef = p->hasRef;
160,960✔
1178
        pRsp->pColRefs[i].id = p->id;
160,960✔
1179
        if (p->hasRef) {
160,960✔
1180
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
99,314✔
1181
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
99,314✔
1182
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
99,314✔
1183
        }
1184
      }
1185
    }
1186
  } else {
1187
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
17,118✔
1188
    if (code) {
17,118✔
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++) {
178,717✔
1193
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
161,599✔
1194
        pRsp->pSchemaExt[i].colId = p->id;
161,599✔
1195
        pRsp->pSchemaExt[i].compress = p->alg;
161,599✔
1196
      }
1197
    }
1198
  }
1199

1200
  metaFetchEntryFree(&pEntry);
43,652✔
1201
  TAOS_RETURN(code);
43,652✔
1202
}
1203

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

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

1213
  // fetch old entry
1214
  SMetaEntry *pEntry = NULL;
472,256✔
1215
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
472,256✔
1216
  if (code) {
472,256✔
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) {
472,256✔
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;
472,256✔
1231
  SSchema        *pColumn = NULL;
472,256✔
1232
  int32_t         iColumn = 0;
472,256✔
1233
  int32_t         rowSize = 0;
472,256✔
1234
  for (int32_t i = 0; i < pSchema->nCols; i++) {
35,766,488✔
1235
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
35,294,232✔
1236
      pColumn = &pSchema->pSchema[i];
472,256✔
1237
      iColumn = i;
472,256✔
1238
    }
1239
    rowSize += pSchema->pSchema[i].bytes;
35,294,232✔
1240
  }
1241

1242
  if (NULL == pColumn) {
472,256✔
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) {
472,256✔
1250
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
185,952✔
1251
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1252
              version);
1253
    metaFetchEntryFree(&pEntry);
185,952✔
1254
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
185,952✔
1255
  }
1256

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

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

1271
  // do handle entry
1272
  code = metaHandleEntry2(pMeta, pEntry);
242,624✔
1273
  if (code) {
242,624✔
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,
242,624✔
1280
             pEntry->uid, version);
1281
  }
1282

1283
  // build response
1284
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
242,624✔
1285
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
25,782✔
1286
                                pEntry->type);
25,782✔
1287
    if (code) {
25,782✔
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++) {
17,411,286✔
1292
        SColRef *p = &pEntry->colRef.pColRef[i];
17,385,504✔
1293
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,385,504✔
1294
        pRsp->pColRefs[i].id = p->id;
17,385,504✔
1295
        if (p->hasRef) {
17,385,504✔
1296
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
75,152✔
1297
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
75,152✔
1298
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
75,152✔
1299
        }
1300
      }
1301
    }
1302
  } else {
1303
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
216,842✔
1304
    if (code) {
216,842✔
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++) {
8,710,658✔
1309
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,493,816✔
1310
        pRsp->pSchemaExt[i].colId = p->id;
8,493,816✔
1311
        pRsp->pSchemaExt[i].compress = p->alg;
8,493,816✔
1312
      }
1313
    }
1314
  }
1315

1316
  metaFetchEntryFree(&pEntry);
242,624✔
1317
  TAOS_RETURN(code);
242,624✔
1318
}
1319

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

1323
  // check tag name
1324
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,573,428✔
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,573,428✔
1332
  int32_t valueSize = 0;
7,573,428✔
1333
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,573,428✔
1334
  if (code) {
7,573,428✔
1335
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
270✔
1336
              __FILE__, __LINE__, pReq->tbName, version);
1337
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
270✔
1338
    TAOS_RETURN(code);
270✔
1339
  }
1340
  tdbFreeClear(value);
7,573,158✔
1341

1342
  TAOS_RETURN(code);
7,573,158✔
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,475,710✔
1348
  if (isNull) {
7,475,710✔
1349
    if (!tTagGet(pOldTag, &value)) {
45,939✔
1350
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,863✔
1351
      return true;
19,863✔
1352
    }
1353
    return false;
26,076✔
1354
  }
1355
  if (!tTagGet(pOldTag, &value)){
7,429,771✔
1356
    return false;
176,049✔
1357
  }
1358
  if (IS_VAR_DATA_TYPE(value.type)) {
7,253,722✔
1359
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
57,493✔
1360
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
36,652✔
1361
      return true;
36,652✔
1362
    }
1363
  } else {
1364
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,196,229✔
1365
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
89,374✔
1366
      return true;
89,374✔
1367
    }
1368
  }
1369
  return false;
7,127,696✔
1370
}
1371

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

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

1381
  // fetch child entry
1382
  SMetaEntry *pChild = NULL;
7,573,158✔
1383
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,573,158✔
1384
  if (code) {
7,573,158✔
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,573,158✔
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,573,158✔
1399
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,573,158✔
1400
  if (code) {
7,573,158✔
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,573,158✔
1409
  SSchema        *pColumn = NULL;
7,573,158✔
1410
  int32_t         iColumn = 0;
7,573,158✔
1411
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,226,694✔
1412
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,226,694✔
1413
      pColumn = &pTagSchema->pSchema[i];
7,573,158✔
1414
      iColumn = i;
7,573,158✔
1415
      break;
7,573,158✔
1416
    }
1417
  }
1418

1419
  if (NULL == pColumn) {
7,573,158✔
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,573,158✔
1429
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,573,158✔
1430
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
117,234✔
1431
    if (NULL == pNewTag) {
117,234✔
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;
117,234✔
1439
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
117,234✔
1440
  } else {
1441
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,455,924✔
1442

1443
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,455,924✔
1444
    if (NULL == pTagArray) {
7,455,924✔
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++) {
15,832,582✔
1453
      STagVal value = {
8,513,629✔
1454
          .type = pTagSchema->pSchema[i].type,
8,513,629✔
1455
          .cid = pTagSchema->pSchema[i].colId,
8,513,629✔
1456
      };
1457

1458
      if (iColumn == i) {
8,513,629✔
1459
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,455,924✔
1460
          taosArrayDestroy(pTagArray);
136,971✔
1461
          metaFetchEntryFree(&pChild);
136,971✔
1462
          metaFetchEntryFree(&pSuper);
136,971✔
1463
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
136,971✔
1464
        }
1465
        if (pReq->isNull) {
7,318,953✔
1466
          continue;
24,165✔
1467
        }
1468
        if (IS_VAR_DATA_TYPE(value.type)) {
7,294,788✔
1469
          value.pData = pReq->pTagVal;
99,125✔
1470
          value.nData = pReq->nTagVal;
99,125✔
1471
        } else {
1472
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,195,663✔
1473
        }
1474
      } else if (!tTagGet(pOldTag, &value)) {
1,057,705✔
1475
        continue;
289,095✔
1476
      }
1477

1478
      if (NULL == taosArrayPush(pTagArray, &value)) {
8,063,398✔
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,318,953✔
1489
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,318,953✔
1490
    if (code) {
7,318,953✔
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,318,953✔
1499
    taosMemoryFree(pChild->ctbEntry.pTags);
7,318,953✔
1500
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,318,953✔
1501
  }
1502

1503
  // do handle entry
1504
  code = metaHandleEntry2(pMeta, pChild);
7,436,187✔
1505
  if (code) {
7,436,187✔
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,436,187✔
1513
             pChild->uid, version);
1514
  }
1515

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

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

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

1544
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,523✔
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,523✔
1551
}
1552

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

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

1561
  // fetch child entry
1562
  SMetaEntry *pChild = NULL;
3,523✔
1563
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,523✔
1564
  if (code) {
3,523✔
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,523✔
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,523✔
1579
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,523✔
1580
  if (code) {
3,523✔
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,523✔
1589

1590
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,523✔
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,523✔
1601
  if (pTagTable == NULL) {
3,523✔
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++) {
23,309✔
1610
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
19,786✔
1611
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
19,786✔
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,523✔
1622
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
26,494✔
1623
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
22,971✔
1624
        ? numOfChangedTags++
19,786✔
1625
        : 0;
22,971✔
1626
  }
1627
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,523✔
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,523✔
1638
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,523✔
1639
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,523✔
1640
  if (NULL == pTagArray) {
3,523✔
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,523✔
1650

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

1657
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
22,971✔
1658
    if (pTagVal == NULL) {
22,971✔
1659
      if (!tTagGet(pOldTag, &value)) {
3,185✔
1660
        continue;
×
1661
      }
1662
    } else {
1663
      value.type = pCol->type;
19,786✔
1664
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
19,786✔
1665
        allSame = false;
10,868✔
1666
      }
1667
      if (pTagVal->isNull) {
19,786✔
1668
        continue;
3,822✔
1669
      }
1670

1671
      if (IS_VAR_DATA_TYPE(pCol->type)) {
15,964✔
1672
        value.pData = pTagVal->pTagVal;
2,548✔
1673
        value.nData = pTagVal->nTagVal;
2,548✔
1674
      } else {
1675
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
13,416✔
1676
      }
1677
    }
1678

1679
    if (taosArrayPush(pTagArray, &value) == NULL) {
19,149✔
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,523✔
1691
    metaWarn("vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
637✔
1692
             __LINE__, version);
1693
    taosHashCleanup(pTagTable);
637✔
1694
    taosArrayDestroy(pTagArray);
637✔
1695
    metaFetchEntryFree(&pChild);
637✔
1696
    metaFetchEntryFree(&pSuper);
637✔
1697
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
637✔
1698
  } 
1699
  STag *pNewTag = NULL;
2,886✔
1700
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
2,886✔
1701
  if (code) {
2,886✔
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);
2,886✔
1711
  taosMemoryFree(pChild->ctbEntry.pTags);
2,886✔
1712
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
2,886✔
1713

1714
  // do handle entry
1715
  code = metaHandleEntry2(pMeta, pChild);
2,886✔
1716
  if (code) {
2,886✔
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,
2,886✔
1725
             pChild->uid, version);
1726
  }
1727

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

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

1737
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
24,320✔
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;
24,320✔
1744
}
1745

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

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

1754
  // fetch entry
1755
  SMetaEntry *pEntry = NULL;
24,320✔
1756
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
24,320✔
1757
  if (code) {
24,320✔
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;
24,320✔
1765
  if (pEntry->type == TSDB_CHILD_TABLE) {
24,320✔
1766
    if (pReq->updateTTL) {
11,388✔
1767
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,406✔
1768
    }
1769
    if (pReq->newCommentLen >= 0) {
11,388✔
1770
      char *pNewComment = NULL;
6,982✔
1771
      if (pReq->newCommentLen) {
6,982✔
1772
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,448✔
1773
        if (NULL == pNewComment) {
4,448✔
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,448✔
1780
      } else {
1781
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,534✔
1782
      }
1783
      pEntry->ctbEntry.comment = pNewComment;
6,982✔
1784
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
6,982✔
1785
    }
1786
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
12,932✔
1787
    if (pReq->updateTTL) {
12,932✔
1788
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,526✔
1789
    }
1790
    if (pReq->newCommentLen >= 0) {
12,932✔
1791
      char *pNewComment = NULL;
8,406✔
1792
      if (pReq->newCommentLen > 0) {
8,406✔
1793
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
5,872✔
1794
        if (NULL == pNewComment) {
5,872✔
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);
5,872✔
1801
      } else {
1802
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,534✔
1803
      }
1804
      pEntry->ntbEntry.comment = pNewComment;
8,406✔
1805
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,406✔
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);
24,320✔
1816
  if (code) {
24,320✔
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,
24,320✔
1823
             pEntry->uid, version);
1824
  }
1825

1826
  metaFetchEntryFree(&pEntry);
24,320✔
1827
  TAOS_RETURN(code);
24,320✔
1828
}
1829

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

1833
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5,938✔
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;
5,938✔
1840
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5,938✔
1841
  if (code) {
5,938✔
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) {
5,938✔
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) {
5,938✔
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;
5,938✔
1863
  SColCmprWrapper *wp = &pEntry->colCmpr;
5,938✔
1864
  for (int32_t i = 0; i < wp->nCols; i++) {
47,504✔
1865
    SColCmpr *p = &wp->pColCmpr[i];
41,566✔
1866
    if (p->id == pReq->colId) {
41,566✔
1867
      uint32_t dst = 0;
5,938✔
1868
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5,938✔
1869
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1870
      if (updated > 0) {
5,938✔
1871
        p->alg = dst;
5,938✔
1872
      }
1873
    }
1874
  }
1875

1876
  if (updated == 0) {
5,938✔
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) {
5,938✔
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;
5,938✔
1891

1892
  // do handle entry
1893
  code = metaHandleEntry2(pMeta, pEntry);
5,938✔
1894
  if (code) {
5,938✔
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,
5,938✔
1901
             pEntry->uid, version);
1902
  }
1903

1904
  metaFetchEntryFree(&pEntry);
5,938✔
1905
  TAOS_RETURN(code);
5,938✔
1906
}
1907

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

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

1917
  if (NULL == pReq->refDbName) {
71,783✔
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) {
71,783✔
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) {
71,783✔
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;
71,783✔
1937
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
71,783✔
1938
  if (code) {
71,783✔
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) {
71,783✔
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;
71,783✔
1953
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
71,783✔
1954
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
25,882✔
1955
    if (code) {
25,882✔
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 =
71,783✔
1965
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
71,783✔
1966
  SColRef *pColRef = NULL;
71,783✔
1967
  int32_t  iColumn = 0;
71,783✔
1968
  for (int32_t i = 0; i < pSchema->nCols; i++) {
356,567✔
1969
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
356,567✔
1970
      pColRef = &pEntry->colRef.pColRef[i];
71,783✔
1971
      iColumn = i;
71,783✔
1972
      break;
71,783✔
1973
    }
1974
  }
1975

1976
  if (NULL == pColRef) {
71,783✔
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;
71,783✔
1986
  pColRef->hasRef = true;
71,783✔
1987
  pColRef->id = pSchema->pSchema[iColumn].colId;
71,783✔
1988
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
71,783✔
1989
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
71,783✔
1990
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
71,783✔
1991
  pSchema->version++;
71,783✔
1992
  pEntry->colRef.version++;
71,783✔
1993

1994
  // do handle entry
1995
  code = metaHandleEntry2(pMeta, pEntry);
71,783✔
1996
  if (code) {
71,783✔
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,
71,783✔
2004
             pEntry->uid, version);
2005
  }
2006

2007
  // build response
2008
  code = metaUpdateVtbMetaRsp(
143,566✔
2009
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
71,783✔
2010
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
71,783✔
2011
      pEntry->type);
71,783✔
2012
  if (code) {
71,783✔
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++) {
536,989✔
2017
      SColRef *p = &pEntry->colRef.pColRef[i];
465,206✔
2018
      pRsp->pColRefs[i].hasRef = p->hasRef;
465,206✔
2019
      pRsp->pColRefs[i].id = p->id;
465,206✔
2020
      if (p->hasRef) {
465,206✔
2021
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
321,092✔
2022
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
321,092✔
2023
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
321,092✔
2024
      }
2025
    }
2026
  }
2027

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

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

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

2042
  // fetch old entry
2043
  SMetaEntry *pEntry = NULL;
52,900✔
2044
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
52,900✔
2045
  if (code) {
52,900✔
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) {
52,900✔
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;
52,900✔
2060
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
52,900✔
2061
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
26,616✔
2062
    if (code) {
26,616✔
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 =
52,900✔
2072
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
52,900✔
2073
  SColRef *pColRef = NULL;
52,900✔
2074
  int32_t  iColumn = 0;
52,900✔
2075
  for (int32_t i = 0; i < pSchema->nCols; i++) {
215,374✔
2076
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
215,374✔
2077
      pColRef = &pEntry->colRef.pColRef[i];
52,900✔
2078
      iColumn = i;
52,900✔
2079
      break;
52,900✔
2080
    }
2081
  }
2082

2083
  if (NULL == pColRef) {
52,900✔
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;
52,900✔
2093
  pColRef->hasRef = false;
52,900✔
2094
  pColRef->id = pSchema->pSchema[iColumn].colId;
52,900✔
2095
  pSchema->version++;
52,900✔
2096
  pEntry->colRef.version++;
52,900✔
2097

2098
  // do handle entry
2099
  code = metaHandleEntry2(pMeta, pEntry);
52,900✔
2100
  if (code) {
52,900✔
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,
52,900✔
2108
             pEntry->uid, version);
2109
  }
2110

2111
  // build response
2112
  code = metaUpdateVtbMetaRsp(
105,800✔
2113
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
52,900✔
2114
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
52,900✔
2115
      pEntry->type);
52,900✔
2116
  if (code) {
52,900✔
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++) {
324,948✔
2121
      SColRef *p = &pEntry->colRef.pColRef[i];
272,048✔
2122
      pRsp->pColRefs[i].hasRef = p->hasRef;
272,048✔
2123
      pRsp->pColRefs[i].id = p->id;
272,048✔
2124
      if (p->hasRef) {
272,048✔
2125
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
134,830✔
2126
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
134,830✔
2127
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
134,830✔
2128
      }
2129
    }
2130
  }
2131

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

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

2140
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,360✔
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;
4,360✔
2147
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
4,360✔
2148
  if (code) {
4,360✔
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) {
4,360✔
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) {
4,360✔
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;
4,360✔
2180
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
4,360✔
2181
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,360✔
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) {
4,360✔
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;
4,360✔
2207
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
22,245✔
2208
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,885✔
2209
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,885✔
2210

2211
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
17,885✔
2212
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
17,885✔
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)) {
17,885✔
2220
      numOfChangedTags++;
4,360✔
2221
      SSCHMEA_SET_IDX_ON(pOldColumn);
4,360✔
2222
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
13,525✔
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) {
4,360✔
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;
4,360✔
2239
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,360✔
2240

2241
  // do handle the entry
2242
  code = metaHandleEntry2(pMeta, pEntry);
4,360✔
2243
  if (code) {
4,360✔
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,
4,360✔
2250
             pEntry->uid, version);
2251
  }
2252

2253
  metaFetchEntryFree(&pEntry);
4,360✔
2254
  TAOS_RETURN(code);
4,360✔
2255
}
2256

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

2260
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,880✔
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;
2,880✔
2267
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,880✔
2268
  if (code) {
2,880✔
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) {
2,880✔
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;
2,880✔
2282
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,880✔
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;
2,880✔
2291
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
7,932✔
2292
    SSchema *pCol = pTagSchema->pSchema + i;
7,932✔
2293
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
7,932✔
2294
      if (!IS_IDX_ON(pCol)) {
2,880✔
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++;
2,880✔
2301
      SSCHMEA_SET_IDX_OFF(pCol);
2,880✔
2302
      break;
2,880✔
2303
    }
2304
  }
2305

2306
  if (numOfChangedTags != 1) {
2,880✔
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;
2,880✔
2315
  pTagSchema->version++;
2,880✔
2316
  code = metaHandleEntry2(pMeta, pEntry);
2,880✔
2317
  if (code) {
2,880✔
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,
2,880✔
2324
             pEntry->uid, version);
2325
  }
2326

2327
  metaFetchEntryFree(&pEntry);
2,880✔
2328
  TAOS_RETURN(code);
2,880✔
2329
}
2330

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

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

2340
  SMetaEntry *pEntry = NULL;
7,331,903✔
2341
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,340,976✔
2342
  if (code) {
7,338,286✔
2343
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2344
              __FILE__, __LINE__, pReq->name, version);
2345
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2346
  }
2347

2348
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,338,286✔
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 = {
21,975,076✔
2356
      .version = version,
2357
      .type = TSDB_SUPER_TABLE,
2358
      .uid = pReq->suid,
7,333,222✔
2359
      .name = pReq->name,
7,330,985✔
2360
      .stbEntry.schemaRow = pReq->schemaRow,
2361
      .stbEntry.schemaTag = pReq->schemaTag,
2362
      .stbEntry.keep = pReq->keep,
7,317,309✔
2363
      .stbEntry.ownerId = pReq->ownerId,
7,316,399✔
2364
      .colCmpr = pReq->colCmpr,
2365
      .pExtSchemas = pReq->pExtSchemas,
7,321,784✔
2366
  };
2367
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,309,567✔
2368
  if (pReq->virtualStb) {
7,309,567✔
2369
    TABLE_SET_VIRTUAL(entry.flags);
20,086✔
2370
  }
2371
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,308,898✔
2372
    TABLE_SET_ROLLUP(entry.flags);
4,350✔
2373
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,350✔
2374
  }
2375

2376
  // do handle the entry
2377
  code = metaHandleEntry2(pMeta, &entry);
7,343,930✔
2378
  if (code) {
7,327,867✔
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,327,867✔
2385
             pReq->suid, version);
2386
  }
2387

2388
  metaFetchEntryFree(&pEntry);
7,347,429✔
2389
  TAOS_RETURN(code);
7,349,350✔
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) {
21,750✔
2432
  int32_t code = TSDB_CODE_SUCCESS;
21,750✔
2433

2434
  if (NULL == pReq->name || pReq->name[0] == 0) {
21,750✔
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;
21,750✔
2441
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
21,750✔
2442
  if (code) {
21,750✔
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) {
21,750✔
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) {
21,750✔
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)) {
21,750✔
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);
21,750✔
2469
  }
2470

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

2482
  // do handle the entry
2483
  code = metaHandleEntry2(pMeta, &entry);
21,750✔
2484
  if (code) {
21,750✔
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++;
21,750✔
2491
    pMeta->pVnode->config.isRsma = 1;
21,750✔
2492
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
21,750✔
2493
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2494
  }
2495

2496
  metaFetchEntryFree(&pEntry);
21,750✔
2497
  TAOS_RETURN(code);
21,750✔
2498
}
2499

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

2503
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,350✔
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) {
4,350✔
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;
4,350✔
2516
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,350✔
2517
  if (code) {
4,350✔
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) {
4,350✔
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) {
4,350✔
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)) {
4,350✔
2539
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,350✔
2540
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,350✔
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);
2546
      metaFetchEntryFree(&pEntry);
×
2547
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2548
    }
2549
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
4,350✔
2550
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,350✔
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;
4,350✔
2559
  entry.version = version;
3,625✔
2560
  TABLE_RESET_ROLLUP(entry.flags);
3,625✔
2561
  entry.stbEntry.rsmaParam.uid = 0;
3,625✔
2562
  entry.stbEntry.rsmaParam.name = NULL;
3,625✔
2563
  entry.stbEntry.rsmaParam.nFuncs = 0;
3,625✔
2564
  entry.stbEntry.rsmaParam.funcColIds = NULL;
3,625✔
2565
  entry.stbEntry.rsmaParam.funcIds = NULL;
3,625✔
2566

2567
  // do handle the entry
2568
  code = metaHandleEntry2(pMeta, &entry);
3,625✔
2569
  if (code) {
4,350✔
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) {
4,350✔
2576
      pMeta->pVnode->config.isRsma = 0;
×
2577
    }
2578
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
4,350✔
2579
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2580
  }
2581

2582
  metaFetchEntryFree(&pEntry);
4,350✔
2583
  TAOS_RETURN(code);
4,350✔
2584
}
2585

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

2589
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,600✔
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;
11,600✔
2596
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,600✔
2597
  if (code) {
11,600✔
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) {
11,600✔
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)) {
11,600✔
2612
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
11,600✔
2613
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
11,600✔
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;
11,600✔
2622
  entry.version = version;
11,600✔
2623
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
11,600✔
2624
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
11,600✔
2625
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
11,600✔
2626
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
11,600✔
2627
  }
2628
  // do handle the entry
2629
  code = metaHandleEntry2(pMeta, &entry);
11,600✔
2630
  if (code) {
11,600✔
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,
11,600✔
2637
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2638
  }
2639

2640
  metaFetchEntryFree(&pEntry);
11,600✔
2641
  TAOS_RETURN(code);
11,600✔
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