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

taosdata / TDengine / #4955

09 Feb 2026 01:16AM UTC coverage: 66.884% (+0.008%) from 66.876%
#4955

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

205800 of 307696 relevant lines covered (66.88%)

127581826.31 hits per line

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

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

16
#include "meta.h"
17

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

30
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,093,206✔
31
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,093,206✔
32
  void     *value = NULL;
5,138,495✔
33
  int32_t   valueSize = 0;
5,140,600✔
34
  SMetaInfo info;
5,140,671✔
35

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

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

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

55
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
3,327✔
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) {
3,327✔
63
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
3,327✔
64
    } else {
65
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
66
                " already exists but not a super table, version:%" PRId64,
67
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
68
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
69
    }
70
  }
71

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

79
  return TSDB_CODE_SUCCESS;
5,117,945✔
80
}
81

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

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

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

118
  return code;
1,369,070✔
119
}
120

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

127
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
873,375✔
128
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
71✔
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);
873,304✔
134
  if (code) {
868,682✔
135
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
2,746✔
136
              __FILE__, __LINE__, pReq->name, version);
137
    return TSDB_CODE_TDB_STB_NOT_EXIST;
2,746✔
138
  } else {
139
    int64_t uid = *(int64_t *)value;
865,936✔
140
    tdbFreeClear(value);
865,444✔
141

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

149
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
855,810✔
150
  if (code) {
863,614✔
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) {
863,614✔
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;
863,614✔
162
}
163

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

168
  // check request
169
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,107,779✔
170
  if (code != TSDB_CODE_SUCCESS) {
5,124,185✔
171
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
6,191✔
172
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
3,327✔
173
               pReq->name, pReq->suid, version);
174
      TAOS_RETURN(TSDB_CODE_SUCCESS);
3,327✔
175
    } else {
176
      TAOS_RETURN(code);
2,864✔
177
    }
178
  }
179

180
  // handle entry
181
  SMetaEntry entry = {
10,213,787✔
182
      .version = version,
183
      .type = TSDB_SUPER_TABLE,
184
      .uid = pReq->suid,
5,123,004✔
185
      .name = pReq->name,
5,110,539✔
186
      .stbEntry.schemaRow = pReq->schemaRow,
187
      .stbEntry.schemaTag = pReq->schemaTag,
188
      .stbEntry.keep = pReq->keep,
5,116,903✔
189
      .stbEntry.ownerId = pReq->ownerId,
5,083,237✔
190
  };
191
  if (pReq->rollup) {
5,074,917✔
192
    TABLE_SET_ROLLUP(entry.flags);
×
193
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
194
  }
195
  if (pReq->colCmpred) {
5,088,324✔
196
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,096,748✔
197
    entry.colCmpr = pReq->colCmpr;
5,096,748✔
198
  }
199

200
  entry.pExtSchemas = pReq->pExtSchemas;
5,074,563✔
201

202
  if (pReq->virtualStb) {
5,088,882✔
203
    TABLE_SET_VIRTUAL(entry.flags);
65,640✔
204
  }
205

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

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

221
  // check request
222
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
872,812✔
223
  if (code) {
864,269✔
224
    TAOS_RETURN(code);
3,462✔
225
  }
226

227
  // handle entry
228
  SMetaEntry entry = {
860,807✔
229
      .version = verison,
230
      .type = -TSDB_SUPER_TABLE,
231
      .uid = pReq->suid,
860,269✔
232
  };
233
  code = metaHandleEntry2(pMeta, &entry);
860,622✔
234
  if (code) {
871,035✔
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,
871,035✔
239
             pReq->suid, verison);
240
  }
241
  TAOS_RETURN(code);
871,035✔
242
}
243

244
// Alter Super Table
245

246
// Create Child Table
247
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
56,844,192✔
248
  int32_t   code = TSDB_CODE_SUCCESS;
56,844,192✔
249
  void     *value = NULL;
56,844,192✔
250
  int32_t   valueSize = 0;
56,846,330✔
251
  SMetaInfo info;
56,842,111✔
252

253
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
56,847,175✔
254
      pReq->ctb.suid == 0) {
56,840,411✔
255
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
1,472✔
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) {
56,838,916✔
262
    pReq->uid = *(int64_t *)value;
313,693✔
263
    tdbFreeClear(value);
313,693✔
264

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

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

291
  // check super table existence
292
  SMetaEntry *pStbEntry = NULL;
56,527,750✔
293
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
56,523,531✔
294
  if (code) {
56,521,613✔
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) {
56,521,613✔
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) {
56,511,120✔
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;
56,516,349✔
318
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
56,524,983✔
319
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
56,525,938✔
320
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
249,329,535✔
321
      STagVal tagVal = {
193,077,243✔
322
          .cid = pTagSchema->pSchema[i].colId,
193,058,552✔
323
      };
324

325
      if (tTagGet(pTag, &tagVal)) {
193,048,398✔
326
        if (pTagSchema->pSchema[i].type != tagVal.type) {
152,096,628✔
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);
56,502,326✔
337

338
  // check grant
339
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
56,495,587✔
340
    code = grantCheck(TSDB_GRANT_TIMESERIES);
56,529,602✔
341
    if (TSDB_CODE_SUCCESS != code) {
56,505,111✔
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;
56,508,625✔
347
}
348

349
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
56,256,906✔
350
  int32_t code = TSDB_CODE_SUCCESS;
56,256,906✔
351

352
  if (NULL == ppRsp) {
56,256,906✔
353
    return code;
×
354
  }
355

356
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
56,256,906✔
357
  if (NULL == ppRsp) {
56,271,065✔
358
    return terrno;
×
359
  }
360

361
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
56,271,065✔
362
  (*ppRsp)->tuid = pEntry->uid;
56,282,526✔
363
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
56,289,444✔
364
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
56,301,461✔
365

366
  return code;
56,296,701✔
367
}
368

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

372
  // check request
373
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
56,641,127✔
374
  if (code) {
56,607,292✔
375
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
313,693✔
376
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,730✔
377
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
378
    }
379
    return code;
313,658✔
380
  }
381

382
  SMetaEntry entry = {
56,293,599✔
383
      .version = version,
384
      .type = TSDB_CHILD_TABLE,
385
      .uid = pReq->uid,
56,283,900✔
386
      .name = pReq->name,
56,293,410✔
387
      .ctbEntry.btime = pReq->btime,
56,287,186✔
388
      .ctbEntry.ttlDays = pReq->ttl,
56,281,115✔
389
      .ctbEntry.commentLen = pReq->commentLen,
56,277,432✔
390
      .ctbEntry.comment = pReq->comment,
56,282,676✔
391
      .ctbEntry.suid = pReq->ctb.suid,
56,275,396✔
392
      .ctbEntry.pTags = pReq->ctb.pTag,
56,282,325✔
393
  };
394

395
  // build response
396
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
56,271,237✔
397
  if (code) {
56,304,053✔
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);
56,304,053✔
404
  if (TSDB_CODE_SUCCESS == code) {
56,320,323✔
405
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
56,322,574✔
406
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
407
  } else {
408
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
409
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
410
              pReq->ctb.suid, version);
411
  }
412
  return code;
56,342,000✔
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,996,130✔
421
  int32_t code = 0;
6,996,130✔
422
  void   *value = NULL;
6,996,130✔
423
  int32_t valueSize = 0;
6,996,130✔
424

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

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

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

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

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

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

466
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
77,840,477✔
467
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
71,019,594✔
468
    (*ppRsp)->pSchemaExt[i].colId = p->id;
71,019,594✔
469
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
71,019,594✔
470
    if (pEntry->pExtSchemas) {
71,019,594✔
471
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
434,229✔
472
    }
473
  }
474

475
  return code;
6,820,883✔
476
}
477

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

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

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

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

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

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

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

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

546
  return code;
133,094✔
547
}
548

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

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

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

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

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

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

614
  return code;
207,246✔
615
}
616

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

640
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
207,246✔
641
  if (code) {
207,246✔
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);
207,246✔
649
  if (TSDB_CODE_SUCCESS == code) {
207,246✔
650
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
207,246✔
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);
207,246✔
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) {
63,839,212✔
667
  int32_t code = TSDB_CODE_SUCCESS;
63,839,212✔
668
  if (TSDB_CHILD_TABLE == pReq->type) {
63,839,212✔
669
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
56,647,023✔
670
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
7,203,376✔
671
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
6,863,036✔
672
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
340,340✔
673
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
133,094✔
674
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
207,246✔
675
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
207,246✔
676
  } else {
677
    code = TSDB_CODE_INVALID_MSG;
×
678
  }
679
  TAOS_RETURN(code);
63,857,928✔
680
}
681

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

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

707
  if (pReq->isVirtual) {
1,369,070✔
708
    if (pReq->suid == 0) {
52,866✔
709
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,667✔
710
    } else {
711
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,199✔
712
    }
713
  } else {
714
    if (pReq->suid == 0) {
1,316,204✔
715
      entry.type = -TSDB_NORMAL_TABLE;
760,873✔
716
    } else {
717
      entry.type = -TSDB_CHILD_TABLE;
555,331✔
718
    }
719
  }
720
  code = metaHandleEntry2(pMeta, &entry);
1,369,070✔
721
  if (code) {
1,369,070✔
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,369,070✔
726
             pReq->uid, version);
727
  }
728
  TAOS_RETURN(code);
1,369,070✔
729
}
730

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

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

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

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

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

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

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

915
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,123,857✔
916
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
50,062✔
917
                                pEntry->type);
50,062✔
918
    if (code) {
50,062✔
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,707,149✔
923
        SColRef *p = &pEntry->colRef.pColRef[i];
17,657,087✔
924
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,657,087✔
925
        pRsp->pColRefs[i].id = p->id;
17,657,087✔
926
        if (p->hasRef) {
17,657,087✔
927
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
161,681✔
928
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
161,681✔
929
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
161,681✔
930
        }
931
      }
932
    }
933
  } else {
934
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,073,795✔
935
    if (code) {
3,073,795✔
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,123,857✔
948
  TAOS_RETURN(code);
3,123,857✔
949
}
950

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

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

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

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

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

1062
  // build response
1063
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
77,890✔
1064
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
28,610✔
1065
                                pEntry->type);
28,610✔
1066
    if (code) {
28,610✔
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,849,268✔
1071
        SColRef *p = &pEntry->colRef.pColRef[i];
34,820,658✔
1072
        pRsp->pColRefs[i].hasRef = p->hasRef;
34,820,658✔
1073
        pRsp->pColRefs[i].id = p->id;
34,820,658✔
1074
        if (p->hasRef) {
34,820,658✔
1075
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
17,422,520✔
1076
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
17,422,520✔
1077
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
17,422,520✔
1078
        }
1079
      }
1080
    }
1081
  } else {
1082
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
49,280✔
1083
    if (code) {
49,280✔
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++) {
21,742,801✔
1088
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
21,693,521✔
1089
        pRsp->pSchemaExt[i].colId = p->id;
21,693,521✔
1090
        pRsp->pSchemaExt[i].compress = p->alg;
21,693,521✔
1091
      }
1092
    }
1093
  }
1094

1095
  metaFetchEntryFree(&pEntry);
77,890✔
1096
  TAOS_RETURN(code);
77,890✔
1097
}
1098

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1283
  // build response
1284
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
243,586✔
1285
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
25,869✔
1286
                                pEntry->type);
25,869✔
1287
    if (code) {
25,869✔
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,510,189✔
1292
        SColRef *p = &pEntry->colRef.pColRef[i];
17,484,320✔
1293
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,484,320✔
1294
        pRsp->pColRefs[i].id = p->id;
17,484,320✔
1295
        if (p->hasRef) {
17,484,320✔
1296
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
75,432✔
1297
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
75,432✔
1298
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
75,432✔
1299
        }
1300
      }
1301
    }
1302
  } else {
1303
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
217,717✔
1304
    if (code) {
217,717✔
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,751,729✔
1309
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,534,012✔
1310
        pRsp->pSchemaExt[i].colId = p->id;
8,534,012✔
1311
        pRsp->pSchemaExt[i].compress = p->alg;
8,534,012✔
1312
      }
1313
    }
1314
  }
1315

1316
  metaFetchEntryFree(&pEntry);
243,586✔
1317
  TAOS_RETURN(code);
243,586✔
1318
}
1319

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

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

1342
  TAOS_RETURN(code);
7,493,903✔
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,396,738✔
1348
  if (isNull) {
7,396,738✔
1349
    if (!tTagGet(pOldTag, &value)) {
45,702✔
1350
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,776✔
1351
      return true;
19,776✔
1352
    }
1353
    return false;
25,926✔
1354
  }
1355
  if (!tTagGet(pOldTag, &value)){
7,351,036✔
1356
    return false;
177,731✔
1357
  }
1358
  if (IS_VAR_DATA_TYPE(value.type)) {
7,173,305✔
1359
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
57,379✔
1360
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
36,561✔
1361
      return true;
36,561✔
1362
    }
1363
  } else {
1364
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,115,926✔
1365
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
88,729✔
1366
      return true;
88,729✔
1367
    }
1368
  }
1369
  return false;
7,048,015✔
1370
}
1371

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

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

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

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

1443
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,376,680✔
1444
    if (NULL == pTagArray) {
7,376,680✔
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,679,151✔
1453
      STagVal value = {
8,438,493✔
1454
          .type = pTagSchema->pSchema[i].type,
8,438,493✔
1455
          .cid = pTagSchema->pSchema[i].colId,
8,438,493✔
1456
      };
1457

1458
      if (iColumn == i) {
8,438,493✔
1459
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,376,680✔
1460
          taosArrayDestroy(pTagArray);
136,022✔
1461
          metaFetchEntryFree(&pChild);
136,022✔
1462
          metaFetchEntryFree(&pSuper);
136,022✔
1463
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
136,022✔
1464
        }
1465
        if (pReq->isNull) {
7,240,658✔
1466
          continue;
23,988✔
1467
        }
1468
        if (IS_VAR_DATA_TYPE(value.type)) {
7,216,670✔
1469
          value.pData = pReq->pTagVal;
99,928✔
1470
          value.nData = pReq->nTagVal;
99,928✔
1471
        } else {
1472
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,116,742✔
1473
        }
1474
      } else if (!tTagGet(pOldTag, &value)) {
1,061,813✔
1475
        continue;
288,385✔
1476
      }
1477

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2007
  // build response
2008
  code = metaUpdateVtbMetaRsp(
144,272✔
2009
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
72,136✔
2010
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
72,136✔
2011
      pEntry->type);
72,136✔
2012
  if (code) {
72,136✔
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++) {
539,864✔
2017
      SColRef *p = &pEntry->colRef.pColRef[i];
467,728✔
2018
      pRsp->pColRefs[i].hasRef = p->hasRef;
467,728✔
2019
      pRsp->pColRefs[i].id = p->id;
467,728✔
2020
      if (p->hasRef) {
467,728✔
2021
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
322,848✔
2022
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
322,848✔
2023
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
322,848✔
2024
      }
2025
    }
2026
  }
2027

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

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

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

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

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

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

2111
  // build response
2112
  code = metaUpdateVtbMetaRsp(
106,280✔
2113
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
53,140✔
2114
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
53,140✔
2115
      pEntry->type);
53,140✔
2116
  if (code) {
53,140✔
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++) {
326,436✔
2121
      SColRef *p = &pEntry->colRef.pColRef[i];
273,296✔
2122
      pRsp->pColRefs[i].hasRef = p->hasRef;
273,296✔
2123
      pRsp->pColRefs[i].id = p->id;
273,296✔
2124
      if (p->hasRef) {
273,296✔
2125
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
135,482✔
2126
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
135,482✔
2127
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
135,482✔
2128
      }
2129
    }
2130
  }
2131

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

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

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

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

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

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

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

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

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

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

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

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

2340
  SMetaEntry *pEntry = NULL;
7,424,489✔
2341
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,428,912✔
2342
  if (code) {
7,446,345✔
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,446,345✔
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 = {
22,248,072✔
2356
      .version = version,
2357
      .type = TSDB_SUPER_TABLE,
2358
      .uid = pReq->suid,
7,426,937✔
2359
      .name = pReq->name,
7,432,968✔
2360
      .stbEntry.schemaRow = pReq->schemaRow,
2361
      .stbEntry.schemaTag = pReq->schemaTag,
2362
      .stbEntry.keep = pReq->keep,
7,407,424✔
2363
      .stbEntry.ownerId = pReq->ownerId,
7,400,288✔
2364
      .colCmpr = pReq->colCmpr,
2365
      .pExtSchemas = pReq->pExtSchemas,
7,407,613✔
2366
  };
2367
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,388,979✔
2368
  if (pReq->virtualStb) {
7,388,979✔
2369
    TABLE_SET_VIRTUAL(entry.flags);
19,600✔
2370
  }
2371
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,391,977✔
2372
    TABLE_SET_ROLLUP(entry.flags);
3,650✔
2373
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
3,650✔
2374
  }
2375

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

2388
  metaFetchEntryFree(&pEntry);
7,453,898✔
2389
  TAOS_RETURN(code);
7,468,244✔
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,900✔
2432
  int32_t code = TSDB_CODE_SUCCESS;
21,900✔
2433

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

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

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

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

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

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

2567
  // do handle the entry
2568
  code = metaHandleEntry2(pMeta, &entry);
4,380✔
2569
  if (code) {
4,380✔
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,380✔
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,380✔
2579
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2580
  }
2581

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

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

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

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