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

taosdata / TDengine / #4892

20 Dec 2025 01:15PM UTC coverage: 65.571% (+0.02%) from 65.549%
#4892

push

travis-ci

web-flow
feat: support taos_connect_with func (#33952)

* feat: support taos_connect_with

* refactor: enhance connection options and add tests for taos_set_option and taos_connect_with

* fix: handle NULL keys and values in taos_connect_with options

* fix: revert TAOSWS_GIT_TAG to default value "main"

* docs: add TLS configuration options for WebSocket connections in documentation

* docs: modify zh docs and add en docs

* chore: update taos.cfg

* docs: add examples

* docs: add error handling for connection failure in example code

2 of 82 new or added lines in 3 files covered. (2.44%)

527 existing lines in 120 files now uncovered.

182859 of 278870 relevant lines covered (65.57%)

104634355.9 hits per line

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

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

16
#include "meta.h"
17

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

29
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,195,107✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
7,195,107✔
31
  void     *value = NULL;
7,214,048✔
32
  int32_t   valueSize = 0;
7,214,026✔
33
  SMetaInfo info;
7,213,597✔
34

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

42
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
7,201,813✔
43
  if (r == 0) {  // name exists, check uid and type
7,202,133✔
44
    int64_t uid = *(tb_uid_t *)value;
2,331✔
45
    tdbFree(value);
2,331✔
46

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

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

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

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

78
  return TSDB_CODE_SUCCESS;
7,203,467✔
79
}
80

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

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

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

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

117
  return code;
1,005,212✔
118
}
119

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

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

132
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
1,016,824✔
133
  if (code) {
1,013,404✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
777✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
777✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
1,012,627✔
139
    tdbFreeClear(value);
1,013,250✔
140

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

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

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

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

179
  // handle entry
180
  SMetaEntry entry = {
14,399,826✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
7,203,066✔
184
      .name = pReq->name,
7,200,396✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
7,200,688✔
188
  };
189
  if (pReq->rollup) {
7,192,589✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
7,194,222✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
7,198,138✔
195
    entry.colCmpr = pReq->colCmpr;
7,198,138✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
7,196,196✔
199

200
  if (pReq->virtualStb) {
7,196,709✔
201
    TABLE_SET_VIRTUAL(entry.flags);
54,026✔
202
  }
203

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

215
// Drop Super Table
216
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
1,016,824✔
217
  int32_t code = TSDB_CODE_SUCCESS;
1,016,824✔
218

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
1,016,824✔
221
  if (code) {
1,014,080✔
222
    TAOS_RETURN(code);
1,554✔
223
  }
224

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

242
// Alter Super Table
243

244
// Create Child Table
245
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
50,360,265✔
246
  int32_t   code = TSDB_CODE_SUCCESS;
50,360,265✔
247
  void     *value = NULL;
50,360,265✔
248
  int32_t   valueSize = 0;
50,360,862✔
249
  SMetaInfo info;
50,359,566✔
250

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
50,361,387✔
252
      pReq->ctb.suid == 0) {
50,360,817✔
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
3,601✔
254
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
255
    return TSDB_CODE_INVALID_MSG;
×
256
  }
257

258
  // check table existence
259
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
50,357,458✔
260
    pReq->uid = *(int64_t *)value;
704,009✔
261
    tdbFreeClear(value);
704,009✔
262

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

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

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

286
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
702,938✔
287
  }
288

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

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

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

314
  // Check tag value
315
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
49,655,272✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
49,657,375✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
49,657,601✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
229,438,542✔
319
      STagVal tagVal = {
180,114,292✔
320
          .cid = pTagSchema->pSchema[i].colId,
180,112,439✔
321
      };
322

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

334
  metaFetchEntryFree(&pStbEntry);
49,654,718✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
49,652,113✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
49,656,611✔
339
    if (TSDB_CODE_SUCCESS != code) {
49,648,437✔
340
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
341
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
342
    }
343
  }
344
  return code;
49,650,769✔
345
}
346

347
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
49,451,769✔
348
  int32_t code = TSDB_CODE_SUCCESS;
49,451,769✔
349

350
  if (NULL == ppRsp) {
49,451,769✔
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
49,451,769✔
355
  if (NULL == ppRsp) {
49,443,852✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
49,443,852✔
360
  (*ppRsp)->tuid = pEntry->uid;
49,446,313✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
49,447,421✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
49,449,034✔
363

364
  return code;
49,454,079✔
365
}
366

367
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
50,158,970✔
368
  int32_t code = TSDB_CODE_SUCCESS;
50,158,970✔
369

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
50,158,970✔
372
  if (code) {
50,154,239✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
704,009✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,071✔
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
704,009✔
378
  }
379

380
  SMetaEntry entry = {
49,450,230✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
49,449,409✔
384
      .name = pReq->name,
49,449,362✔
385
      .ctbEntry.btime = pReq->btime,
49,446,755✔
386
      .ctbEntry.ttlDays = pReq->ttl,
49,445,571✔
387
      .ctbEntry.commentLen = pReq->commentLen,
49,447,163✔
388
      .ctbEntry.comment = pReq->comment,
49,447,804✔
389
      .ctbEntry.suid = pReq->ctb.suid,
49,446,670✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
49,446,783✔
391
  };
392

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
49,452,917✔
395
  if (code) {
49,452,245✔
396
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
397
              tstrerror(code));
398
  }
399

400
  // handle entry
401
  code = metaHandleEntry2(pMeta, &entry);
49,452,245✔
402
  if (TSDB_CODE_SUCCESS == code) {
49,454,235✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
49,455,145✔
404
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
405
  } else {
406
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
407
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
408
              pReq->ctb.suid, version);
409
  }
410
  return code;
49,457,101✔
411
}
412

413
// Drop Child Table
414

415
// Alter Child Table
416

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

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

429
  // check name
430
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
2,560,211✔
431
    // for auto create table, we return the uid of the existing table
432
    pReq->uid = *(tb_uid_t *)value;
53,228✔
433
    tdbFree(value);
53,228✔
434
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
53,228✔
435
  }
436

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

446
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
2,396,319✔
447
  int32_t code = TSDB_CODE_SUCCESS;
2,396,319✔
448

449
  if (NULL == ppRsp) {
2,396,319✔
450
    return code;
×
451
  }
452

453
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
2,396,319✔
454
  if (NULL == *ppRsp) {
2,396,319✔
455
    return terrno;
×
456
  }
457

458
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp);
2,396,319✔
459
  if (code) {
2,396,319✔
460
    taosMemoryFreeClear(*ppRsp);
×
461
    return code;
×
462
  }
463

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
36,909,254✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
34,512,935✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
34,512,935✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
34,512,935✔
468
    if (pEntry->pExtSchemas) {
34,512,935✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
805,551✔
470
    }
471
  }
472

473
  return code;
2,396,319✔
474
}
475

476
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
2,449,547✔
477
  int32_t code = TSDB_CODE_SUCCESS;
2,449,547✔
478

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

489
  SMetaEntry entry = {
4,792,101✔
490
      .version = version,
491
      .type = TSDB_NORMAL_TABLE,
492
      .uid = pReq->uid,
2,396,319✔
493
      .name = pReq->name,
2,396,319✔
494
      .ntbEntry.btime = pReq->btime,
2,396,319✔
495
      .ntbEntry.ttlDays = pReq->ttl,
2,396,319✔
496
      .ntbEntry.commentLen = pReq->commentLen,
2,396,319✔
497
      .ntbEntry.comment = pReq->comment,
2,396,319✔
498
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
499
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
2,396,319✔
500
      .colCmpr = pReq->colCmpr,
501
      .pExtSchemas = pReq->pExtSchemas,
2,396,319✔
502
  };
503
  TABLE_SET_COL_COMPRESSED(entry.flags);
2,396,319✔
504

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

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

524
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
110,664✔
525
  int32_t code = TSDB_CODE_SUCCESS;
110,664✔
526

527
  if (NULL == ppRsp) {
110,664✔
528
    return code;
×
529
  }
530

531
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
110,664✔
532
  if (NULL == *ppRsp) {
110,664✔
533
    return terrno;
×
534
  }
535

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

543
  return code;
110,664✔
544
}
545

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

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

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

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

591
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
203,072✔
592
  int32_t code = TSDB_CODE_SUCCESS;
203,072✔
593

594
  if (NULL == ppRsp) {
203,072✔
595
    return code;
×
596
  }
597

598
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
203,072✔
599
  if (NULL == *ppRsp) {
203,072✔
600
    return terrno;
×
601
  }
602

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

610
  return code;
203,072✔
611
}
612

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

624
  SMetaEntry entry = {.version = version,
406,144✔
625
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
626
                      .uid = pReq->uid,
203,072✔
627
                      .name = pReq->name,
203,072✔
628
                      .ctbEntry.btime = pReq->btime,
203,072✔
629
                      .ctbEntry.ttlDays = pReq->ttl,
203,072✔
630
                      .ctbEntry.commentLen = pReq->commentLen,
203,072✔
631
                      .ctbEntry.comment = pReq->comment,
203,072✔
632
                      .ctbEntry.suid = pReq->ctb.suid,
203,072✔
633
                      .ctbEntry.pTags = pReq->ctb.pTag,
203,072✔
634
                      .colRef = pReq->colRef};
635

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

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

658
// Drop Normal Table
659

660
// Alter Normal Table
661

662
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
52,921,685✔
663
  int32_t code = TSDB_CODE_SUCCESS;
52,921,685✔
664
  if (TSDB_CHILD_TABLE == pReq->type) {
52,921,685✔
665
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
50,158,009✔
666
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
2,763,283✔
667
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
2,449,547✔
668
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
313,736✔
669
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
110,664✔
670
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
203,072✔
671
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
203,072✔
672
  } else {
673
    code = TSDB_CODE_INVALID_MSG;
×
674
  }
675
  TAOS_RETURN(code);
52,924,304✔
676
}
677

678
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,005,212✔
679
  int32_t code = TSDB_CODE_SUCCESS;
1,005,212✔
680

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

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

698
  SMetaEntry entry = {
1,005,212✔
699
      .version = version,
700
      .uid = pReq->uid,
1,005,212✔
701
  };
702

703
  if (pReq->isVirtual) {
1,005,212✔
704
    if (pReq->suid == 0) {
57,734✔
705
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
29,011✔
706
    } else {
707
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
28,723✔
708
    }
709
  } else {
710
    if (pReq->suid == 0) {
947,478✔
711
      entry.type = -TSDB_NORMAL_TABLE;
532,084✔
712
    } else {
713
      entry.type = -TSDB_CHILD_TABLE;
415,394✔
714
    }
715
  }
716
  code = metaHandleEntry2(pMeta, &entry);
1,005,212✔
717
  if (code) {
1,005,212✔
718
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
719
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
720
  } else {
721
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,005,212✔
722
             pReq->uid, version);
723
  }
724
  TAOS_RETURN(code);
1,005,212✔
725
}
726

727
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,487,427✔
728
  int32_t code = 0;
4,487,427✔
729

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

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

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

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

776
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,706,089✔
777
  int32_t code = TSDB_CODE_SUCCESS;
3,706,089✔
778

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

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

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

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

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

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

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

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

942
  metaFetchEntryFree(&pEntry);
3,364,204✔
943
  TAOS_RETURN(code);
3,364,204✔
944
}
945

946
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
88,162✔
947
  int32_t code = TSDB_CODE_SUCCESS;
88,162✔
948

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

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

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

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

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

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

997
  tColumn = *pColumn;
88,162✔
998

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

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

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

1056
  // build response
1057
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
88,162✔
1058
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
30,485✔
1059
    if (code) {
30,485✔
1060
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1061
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1062
    } else {
1063
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
206,659✔
1064
        SColRef *p = &pEntry->colRef.pColRef[i];
176,174✔
1065
        pRsp->pColRefs[i].hasRef = p->hasRef;
176,174✔
1066
        pRsp->pColRefs[i].id = p->id;
176,174✔
1067
        if (p->hasRef) {
176,174✔
1068
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
98,729✔
1069
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
98,729✔
1070
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
98,729✔
1071
        }
1072
      }
1073
    }
1074
  } else {
1075
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
57,677✔
1076
    if (code) {
57,677✔
1077
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1078
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1079
    } else {
1080
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
52,226,855✔
1081
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
52,169,178✔
1082
        pRsp->pSchemaExt[i].colId = p->id;
52,169,178✔
1083
        pRsp->pSchemaExt[i].compress = p->alg;
52,169,178✔
1084
      }
1085
    }
1086
  }
1087

1088
  metaFetchEntryFree(&pEntry);
88,162✔
1089
  TAOS_RETURN(code);
88,162✔
1090
}
1091

1092
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
45,534✔
1093
  int32_t code = TSDB_CODE_SUCCESS;
45,534✔
1094

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

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

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

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

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

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

1142

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

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

1160
  // build response
1161
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
45,534✔
1162
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
30,160✔
1163
    if (code) {
30,160✔
1164
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1165
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1166
    } else {
1167
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
210,998✔
1168
        SColRef *p = &pEntry->colRef.pColRef[i];
180,838✔
1169
        pRsp->pColRefs[i].hasRef = p->hasRef;
180,838✔
1170
        pRsp->pColRefs[i].id = p->id;
180,838✔
1171
        if (p->hasRef) {
180,838✔
1172
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
112,144✔
1173
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
112,144✔
1174
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
112,144✔
1175
        }
1176
      }
1177
    }
1178
  } else {
1179
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
15,374✔
1180
    if (code) {
15,374✔
1181
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1182
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1183
    } else {
1184
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
173,681✔
1185
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
158,307✔
1186
        pRsp->pSchemaExt[i].colId = p->id;
158,307✔
1187
        pRsp->pSchemaExt[i].compress = p->alg;
158,307✔
1188
      }
1189
    }
1190
  }
1191

1192
  metaFetchEntryFree(&pEntry);
45,534✔
1193
  TAOS_RETURN(code);
45,534✔
1194
}
1195

1196
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
506,466✔
1197
  int32_t code = TSDB_CODE_SUCCESS;
506,466✔
1198

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

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

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

1221
  // search the column to update
1222
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
506,466✔
1223
  SSchema        *pColumn = NULL;
506,466✔
1224
  int32_t         iColumn = 0;
506,466✔
1225
  int32_t         rowSize = 0;
506,466✔
1226
  for (int32_t i = 0; i < pSchema->nCols; i++) {
20,085,316✔
1227
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
19,578,850✔
1228
      pColumn = &pSchema->pSchema[i];
506,466✔
1229
      iColumn = i;
506,466✔
1230
    }
1231
    rowSize += pSchema->pSchema[i].bytes;
19,578,850✔
1232
  }
1233

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

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

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

1258
  // do change the column bytes
1259
  pEntry->version = version;
257,330✔
1260
  pSchema->version++;
257,330✔
1261
  pColumn->bytes = pReq->colModBytes;
257,330✔
1262

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

1275
  // build response
1276
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
257,330✔
1277
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
27,718✔
1278
    if (code) {
27,718✔
1279
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1280
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1281
    } else {
1282
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
198,788✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
171,070✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
171,070✔
1285
        pRsp->pColRefs[i].id = p->id;
171,070✔
1286
        if (p->hasRef) {
171,070✔
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
82,508✔
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
82,508✔
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
82,508✔
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
229,612✔
1295
    if (code) {
229,612✔
1296
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1297
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1298
    } else {
1299
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,422,816✔
1300
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,193,204✔
1301
        pRsp->pSchemaExt[i].colId = p->id;
9,193,204✔
1302
        pRsp->pSchemaExt[i].compress = p->alg;
9,193,204✔
1303
      }
1304
    }
1305
  }
1306

1307
  metaFetchEntryFree(&pEntry);
257,330✔
1308
  TAOS_RETURN(code);
257,330✔
1309
}
1310

1311
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,990,755✔
1312
  int32_t code = 0;
7,990,755✔
1313

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

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

1333
  TAOS_RETURN(code);
7,990,723✔
1334
}
1335

1336
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,990,755✔
1337
  int32_t code = TSDB_CODE_SUCCESS;
7,990,755✔
1338

1339
  // check request
1340
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,990,755✔
1341
  if (code) {
7,990,755✔
1342
    TAOS_RETURN(code);
32✔
1343
  }
1344

1345
  // fetch child entry
1346
  SMetaEntry *pChild = NULL;
7,990,723✔
1347
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,990,723✔
1348
  if (code) {
7,990,723✔
1349
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1350
              __FILE__, __LINE__, pReq->tbName, version);
1351
    TAOS_RETURN(code);
×
1352
  }
1353

1354
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
7,990,723✔
1355
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1356
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1357
    metaFetchEntryFree(&pChild);
×
1358
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1359
  }
1360

1361
  // fetch super entry
1362
  SMetaEntry *pSuper = NULL;
7,990,723✔
1363
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,990,723✔
1364
  if (code) {
7,990,723✔
1365
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1366
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1367
    metaFetchEntryFree(&pChild);
×
1368
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1369
  }
1370

1371
  // search the tag to update
1372
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,990,723✔
1373
  SSchema        *pColumn = NULL;
7,990,723✔
1374
  int32_t         iColumn = 0;
7,990,723✔
1375
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,704,439✔
1376
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,704,439✔
1377
      pColumn = &pTagSchema->pSchema[i];
7,990,723✔
1378
      iColumn = i;
7,990,723✔
1379
      break;
7,990,723✔
1380
    }
1381
  }
1382

1383
  if (NULL == pColumn) {
7,990,723✔
1384
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1385
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1386
    metaFetchEntryFree(&pChild);
×
1387
    metaFetchEntryFree(&pSuper);
×
1388
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1389
  }
1390

1391
  // do change tag value
1392
  pChild->version = version;
7,990,723✔
1393
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,990,723✔
1394
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
130,080✔
1395
    if (NULL == pNewTag) {
130,080✔
1396
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1397
                __LINE__, tstrerror(terrno), version);
1398
      metaFetchEntryFree(&pChild);
×
1399
      metaFetchEntryFree(&pSuper);
×
1400
      TAOS_RETURN(terrno);
×
1401
    }
1402
    pChild->ctbEntry.pTags = pNewTag;
130,080✔
1403
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
130,080✔
1404
  } else {
1405
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,860,643✔
1406

1407
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,860,643✔
1408
    if (NULL == pTagArray) {
7,860,643✔
1409
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1410
                __LINE__, tstrerror(terrno), version);
1411
      metaFetchEntryFree(&pChild);
×
1412
      metaFetchEntryFree(&pSuper);
×
1413
      TAOS_RETURN(terrno);
×
1414
    }
1415

1416
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
16,877,311✔
1417
      STagVal value = {
9,016,668✔
1418
          .type = pTagSchema->pSchema[i].type,
9,016,668✔
1419
          .cid = pTagSchema->pSchema[i].colId,
9,016,668✔
1420
      };
1421

1422
      if (iColumn == i) {
9,016,668✔
1423
        if (pReq->isNull) {
7,860,643✔
1424
          continue;
46,035✔
1425
        }
1426
        if (IS_VAR_DATA_TYPE(value.type)) {
7,814,608✔
1427
          value.pData = pReq->pTagVal;
145,875✔
1428
          value.nData = pReq->nTagVal;
145,875✔
1429
        } else {
1430
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,668,733✔
1431
        }
1432
      } else if (!tTagGet(pOldTag, &value)) {
1,156,025✔
1433
        continue;
306,367✔
1434
      }
1435

1436
      if (NULL == taosArrayPush(pTagArray, &value)) {
8,664,266✔
1437
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1438
                  __LINE__, tstrerror(terrno), version);
1439
        taosArrayDestroy(pTagArray);
×
1440
        metaFetchEntryFree(&pChild);
×
1441
        metaFetchEntryFree(&pSuper);
×
1442
        TAOS_RETURN(terrno);
×
1443
      }
1444
    }
1445

1446
    STag *pNewTag = NULL;
7,860,643✔
1447
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
7,860,643✔
1448
    if (code) {
7,860,643✔
1449
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1450
                __LINE__, tstrerror(code), version);
1451
      taosArrayDestroy(pTagArray);
×
1452
      metaFetchEntryFree(&pChild);
×
1453
      metaFetchEntryFree(&pSuper);
×
1454
      TAOS_RETURN(code);
×
1455
    }
1456
    taosArrayDestroy(pTagArray);
7,860,643✔
1457
    taosMemoryFree(pChild->ctbEntry.pTags);
7,860,643✔
1458
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
7,860,643✔
1459
  }
1460

1461
  // do handle entry
1462
  code = metaHandleEntry2(pMeta, pChild);
7,990,723✔
1463
  if (code) {
7,990,723✔
1464
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1465
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1466
    metaFetchEntryFree(&pChild);
×
1467
    metaFetchEntryFree(&pSuper);
×
1468
    TAOS_RETURN(code);
×
1469
  } else {
1470
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
7,990,723✔
1471
             pChild->uid, version);
1472
  }
1473

1474
  // free resource and return
1475
  metaFetchEntryFree(&pChild);
7,990,723✔
1476
  metaFetchEntryFree(&pSuper);
7,990,723✔
1477
  TAOS_RETURN(code);
7,990,723✔
1478
}
1479

1480
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,470✔
1481
  int32_t code = 0;
3,470✔
1482

1483
  // check tag name
1484
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,470✔
1485
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1486
              __FILE__, __LINE__, version);
1487
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1488
  }
1489

1490
  // check name
1491
  void   *value = NULL;
3,470✔
1492
  int32_t valueSize = 0;
3,470✔
1493
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
3,470✔
1494
  if (code) {
3,470✔
1495
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1496
              __FILE__, __LINE__, pReq->tbName, version);
1497
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1498
    TAOS_RETURN(code);
×
1499
  }
1500
  tdbFreeClear(value);
3,470✔
1501

1502
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,470✔
1503
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1504
              __FILE__, __LINE__, version);
1505
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1506
  }
1507

1508
  TAOS_RETURN(code);
3,470✔
1509
}
1510

1511
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,470✔
1512
  int32_t code = TSDB_CODE_SUCCESS;
3,470✔
1513

1514
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,470✔
1515
  if (code) {
3,470✔
1516
    TAOS_RETURN(code);
×
1517
  }
1518

1519
  // fetch child entry
1520
  SMetaEntry *pChild = NULL;
3,470✔
1521
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
3,470✔
1522
  if (code) {
3,470✔
1523
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1524
              __FILE__, __LINE__, pReq->tbName, version);
1525
    TAOS_RETURN(code);
×
1526
  }
1527

1528
  if (pChild->type != TSDB_CHILD_TABLE) {
3,470✔
1529
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1530
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1531
    metaFetchEntryFree(&pChild);
×
1532
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1533
  }
1534

1535
  // fetch super entry
1536
  SMetaEntry *pSuper = NULL;
3,470✔
1537
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
3,470✔
1538
  if (code) {
3,470✔
1539
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1540
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1541
    metaFetchEntryFree(&pChild);
×
1542
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1543
  }
1544

1545
  // search the tags to update
1546
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,470✔
1547

1548
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,470✔
1549
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1550
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1551
    metaFetchEntryFree(&pChild);
×
1552
    metaFetchEntryFree(&pSuper);
×
1553
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1554
  }
1555

1556
  // do check if tag name exists
1557
  SHashObj *pTagTable =
1558
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
3,470✔
1559
  if (pTagTable == NULL) {
3,470✔
1560
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1561
              __LINE__, tstrerror(terrno), version);
1562
    metaFetchEntryFree(&pChild);
×
1563
    metaFetchEntryFree(&pSuper);
×
1564
    TAOS_RETURN(terrno);
×
1565
  }
1566

1567
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
24,290✔
1568
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
20,820✔
1569
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
20,820✔
1570
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1571
                __LINE__, tstrerror(terrno), version);
1572
      taosHashCleanup(pTagTable);
×
1573
      metaFetchEntryFree(&pChild);
×
1574
      metaFetchEntryFree(&pSuper);
×
1575
      TAOS_RETURN(terrno);
×
1576
    }
1577
  }
1578

1579
  int32_t numOfChangedTags = 0;
3,470✔
1580
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,760✔
1581
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
24,290✔
1582
        ? numOfChangedTags++
20,820✔
1583
        : 0;
24,290✔
1584
  }
1585
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,470✔
1586
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1587
              __FILE__, __LINE__, version);
1588
    taosHashCleanup(pTagTable);
×
1589
    metaFetchEntryFree(&pChild);
×
1590
    metaFetchEntryFree(&pSuper);
×
1591
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1592
  }
1593

1594
  // do change tag value
1595
  pChild->version = version;
3,470✔
1596
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
3,470✔
1597
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
3,470✔
1598
  if (NULL == pTagArray) {
3,470✔
1599
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1600
              __LINE__, tstrerror(terrno), version);
1601
    taosHashCleanup(pTagTable);
×
1602
    metaFetchEntryFree(&pChild);
×
1603
    metaFetchEntryFree(&pSuper);
×
1604
    TAOS_RETURN(terrno);
×
1605
  }
1606

1607
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
27,760✔
1608
    SSchema *pCol = &pTagSchema->pSchema[i];
24,290✔
1609
    STagVal  value = {
24,290✔
1610
         .cid = pCol->colId,
24,290✔
1611
    };
1612

1613
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
24,290✔
1614
    if (pTagVal == NULL) {
24,290✔
1615
      if (!tTagGet(pOldTag, &value)) {
3,470✔
1616
        continue;
×
1617
      }
1618
    } else {
1619
      value.type = pCol->type;
20,820✔
1620
      if (pTagVal->isNull) {
20,820✔
1621
        continue;
4,164✔
1622
      }
1623

1624
      if (IS_VAR_DATA_TYPE(pCol->type)) {
16,656✔
1625
        value.pData = pTagVal->pTagVal;
2,776✔
1626
        value.nData = pTagVal->nTagVal;
2,776✔
1627
      } else {
1628
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
13,880✔
1629
      }
1630
    }
1631

1632
    if (taosArrayPush(pTagArray, &value) == NULL) {
20,126✔
1633
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1634
                __LINE__, tstrerror(terrno), version);
1635
      taosHashCleanup(pTagTable);
×
1636
      taosArrayDestroy(pTagArray);
×
1637
      metaFetchEntryFree(&pChild);
×
1638
      metaFetchEntryFree(&pSuper);
×
1639
      TAOS_RETURN(terrno);
×
1640
    }
1641
  }
1642

1643
  STag *pNewTag = NULL;
3,470✔
1644
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
3,470✔
1645
  if (code) {
3,470✔
1646
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1647
              __LINE__, tstrerror(code), version);
1648
    taosHashCleanup(pTagTable);
×
1649
    taosArrayDestroy(pTagArray);
×
1650
    metaFetchEntryFree(&pChild);
×
1651
    metaFetchEntryFree(&pSuper);
×
1652
    TAOS_RETURN(code);
×
1653
  }
1654
  taosArrayDestroy(pTagArray);
3,470✔
1655
  taosMemoryFree(pChild->ctbEntry.pTags);
3,470✔
1656
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
3,470✔
1657

1658
  // do handle entry
1659
  code = metaHandleEntry2(pMeta, pChild);
3,470✔
1660
  if (code) {
3,470✔
1661
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1662
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1663
    taosHashCleanup(pTagTable);
×
1664
    metaFetchEntryFree(&pChild);
×
1665
    metaFetchEntryFree(&pSuper);
×
1666
    TAOS_RETURN(code);
×
1667
  } else {
1668
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,470✔
1669
             pChild->uid, version);
1670
  }
1671

1672
  taosHashCleanup(pTagTable);
3,470✔
1673
  metaFetchEntryFree(&pChild);
3,470✔
1674
  metaFetchEntryFree(&pSuper);
3,470✔
1675
  TAOS_RETURN(code);
3,470✔
1676
}
1677

1678
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
17,802✔
1679
  int32_t code = TSDB_CODE_SUCCESS;
17,802✔
1680

1681
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
17,802✔
1682
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1683
              __FILE__, __LINE__, version);
1684
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1685
  }
1686

1687
  return code;
17,802✔
1688
}
1689

1690
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
17,802✔
1691
  int32_t code = 0;
17,802✔
1692

1693
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
17,802✔
1694
  if (code) {
17,802✔
1695
    TAOS_RETURN(code);
×
1696
  }
1697

1698
  // fetch entry
1699
  SMetaEntry *pEntry = NULL;
17,802✔
1700
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
17,802✔
1701
  if (code) {
17,802✔
1702
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1703
              __FILE__, __LINE__, pReq->tbName, version);
1704
    TAOS_RETURN(code);
×
1705
  }
1706

1707
  // do change the entry
1708
  pEntry->version = version;
17,802✔
1709
  if (pEntry->type == TSDB_CHILD_TABLE) {
17,802✔
1710
    if (pReq->updateTTL) {
8,627✔
1711
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
3,726✔
1712
    }
1713
    if (pReq->newCommentLen >= 0) {
8,627✔
1714
      char *pNewComment = NULL;
4,901✔
1715
      if (pReq->newCommentLen) {
4,901✔
1716
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
2,695✔
1717
        if (NULL == pNewComment) {
2,695✔
1718
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1719
                    __LINE__, tstrerror(terrno), version);
1720
          metaFetchEntryFree(&pEntry);
×
1721
          TAOS_RETURN(terrno);
×
1722
        }
1723
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
2,695✔
1724
      } else {
1725
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,206✔
1726
      }
1727
      pEntry->ctbEntry.comment = pNewComment;
4,901✔
1728
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
4,901✔
1729
    }
1730
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
9,175✔
1731
    if (pReq->updateTTL) {
9,175✔
1732
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,280✔
1733
    }
1734
    if (pReq->newCommentLen >= 0) {
9,175✔
1735
      char *pNewComment = NULL;
4,895✔
1736
      if (pReq->newCommentLen > 0) {
4,895✔
1737
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
2,689✔
1738
        if (NULL == pNewComment) {
2,689✔
1739
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1740
                    __LINE__, tstrerror(terrno), version);
1741
          metaFetchEntryFree(&pEntry);
×
1742
          TAOS_RETURN(terrno);
×
1743
        }
1744
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
2,689✔
1745
      } else {
1746
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,206✔
1747
      }
1748
      pEntry->ntbEntry.comment = pNewComment;
4,895✔
1749
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
4,895✔
1750
    }
1751
  } else {
1752
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1753
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1754
    metaFetchEntryFree(&pEntry);
×
1755
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1756
  }
1757

1758
  // do handle entry
1759
  code = metaHandleEntry2(pMeta, pEntry);
17,802✔
1760
  if (code) {
17,802✔
1761
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1762
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1763
    metaFetchEntryFree(&pEntry);
×
1764
    TAOS_RETURN(code);
×
1765
  } else {
1766
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
17,802✔
1767
             pEntry->uid, version);
1768
  }
1769

1770
  metaFetchEntryFree(&pEntry);
17,802✔
1771
  TAOS_RETURN(code);
17,802✔
1772
}
1773

1774
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,672✔
1775
  int32_t code = TSDB_CODE_SUCCESS;
6,672✔
1776

1777
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,672✔
1778
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1779
              __FILE__, __LINE__, version);
1780
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1781
  }
1782

1783
  SMetaEntry *pEntry = NULL;
6,672✔
1784
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,672✔
1785
  if (code) {
6,672✔
1786
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1787
              __FILE__, __LINE__, pReq->tbName, version);
1788
    TAOS_RETURN(code);
×
1789
  }
1790

1791
  if (pEntry->version >= version) {
6,672✔
1792
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1793
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1794
    metaFetchEntryFree(&pEntry);
×
1795
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1796
  }
1797

1798
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,672✔
1799
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1800
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1801
    metaFetchEntryFree(&pEntry);
×
1802
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1803
  }
1804

1805
  // do change the entry
1806
  int8_t           updated = 0;
6,672✔
1807
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,672✔
1808
  for (int32_t i = 0; i < wp->nCols; i++) {
53,376✔
1809
    SColCmpr *p = &wp->pColCmpr[i];
46,704✔
1810
    if (p->id == pReq->colId) {
46,704✔
1811
      uint32_t dst = 0;
6,672✔
1812
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,672✔
1813
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1814
      if (updated > 0) {
6,672✔
1815
        p->alg = dst;
6,672✔
1816
      }
1817
    }
1818
  }
1819

1820
  if (updated == 0) {
6,672✔
1821
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1822
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1823
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1824
    metaFetchEntryFree(&pEntry);
×
1825
    TAOS_RETURN(code);
×
1826
  } else if (updated < 0) {
6,672✔
1827
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1828
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1829
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1830
    metaFetchEntryFree(&pEntry);
×
1831
    TAOS_RETURN(code);
×
1832
  }
1833

1834
  pEntry->version = version;
6,672✔
1835

1836
  // do handle entry
1837
  code = metaHandleEntry2(pMeta, pEntry);
6,672✔
1838
  if (code) {
6,672✔
1839
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1840
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1841
    metaFetchEntryFree(&pEntry);
×
1842
    TAOS_RETURN(code);
×
1843
  } else {
1844
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
6,672✔
1845
             pEntry->uid, version);
1846
  }
1847

1848
  metaFetchEntryFree(&pEntry);
6,672✔
1849
  TAOS_RETURN(code);
6,672✔
1850
}
1851

1852
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
81,978✔
1853
  int32_t code = TSDB_CODE_SUCCESS;
81,978✔
1854

1855
  // check request
1856
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
81,978✔
1857
  if (code) {
81,978✔
1858
    TAOS_RETURN(code);
×
1859
  }
1860

1861
  if (NULL == pReq->refDbName) {
81,978✔
1862
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1863
              __func__, __FILE__, __LINE__, version);
1864
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1865
  }
1866

1867
  if (NULL == pReq->refTbName) {
81,978✔
1868
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1869
              __func__, __FILE__, __LINE__, version);
1870
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1871
  }
1872

1873
  if (NULL == pReq->refColName) {
81,978✔
1874
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1875
              __func__, __FILE__, __LINE__, version);
1876
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1877
  }
1878

1879
  // fetch old entry
1880
  SMetaEntry *pEntry = NULL;
81,978✔
1881
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
81,978✔
1882
  if (code) {
81,978✔
1883
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1884
              __FILE__, __LINE__, pReq->tbName, version);
1885
    TAOS_RETURN(code);
×
1886
  }
1887

1888
  if (pEntry->version >= version) {
81,978✔
1889
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1890
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1891
    metaFetchEntryFree(&pEntry);
×
1892
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1893
  }
1894

1895
  // fetch super entry
1896
  SMetaEntry *pSuper = NULL;
81,978✔
1897
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
81,978✔
1898
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
30,226✔
1899
    if (code) {
30,226✔
1900
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1901
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1902
      metaFetchEntryFree(&pEntry);
×
1903
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1904
    }
1905
  }
1906

1907
  // search the column to update
1908
  SSchemaWrapper *pSchema =
81,978✔
1909
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
81,978✔
1910
  SColRef *pColRef = NULL;
81,978✔
1911
  int32_t  iColumn = 0;
81,978✔
1912
  for (int32_t i = 0; i < pSchema->nCols; i++) {
402,265✔
1913
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
402,265✔
1914
      pColRef = &pEntry->colRef.pColRef[i];
81,978✔
1915
      iColumn = i;
81,978✔
1916
      break;
81,978✔
1917
    }
1918
  }
1919

1920
  if (NULL == pColRef) {
81,978✔
1921
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1922
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1923
    metaFetchEntryFree(&pEntry);
×
1924
    metaFetchEntryFree(&pSuper);
×
1925
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1926
  }
1927

1928
  // do update column name
1929
  pEntry->version = version;
81,978✔
1930
  pColRef->hasRef = true;
81,978✔
1931
  pColRef->id = pSchema->pSchema[iColumn].colId;
81,978✔
1932
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
81,978✔
1933
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
81,978✔
1934
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
81,978✔
1935
  pSchema->version++;
81,978✔
1936
  pEntry->colRef.version++;
81,978✔
1937

1938
  // do handle entry
1939
  code = metaHandleEntry2(pMeta, pEntry);
81,978✔
1940
  if (code) {
81,978✔
1941
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1942
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1943
    metaFetchEntryFree(&pEntry);
×
1944
    metaFetchEntryFree(&pSuper);
×
1945
    TAOS_RETURN(code);
×
1946
  } else {
1947
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
81,978✔
1948
             pEntry->uid, version);
1949
  }
1950

1951
  // build response
1952
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
81,978✔
1953
  if (code) {
81,978✔
1954
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1955
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1956
  } else {
1957
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
604,708✔
1958
      SColRef *p = &pEntry->colRef.pColRef[i];
522,730✔
1959
      pRsp->pColRefs[i].hasRef = p->hasRef;
522,730✔
1960
      pRsp->pColRefs[i].id = p->id;
522,730✔
1961
      if (p->hasRef) {
522,730✔
1962
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
360,145✔
1963
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
360,145✔
1964
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
360,145✔
1965
      }
1966
    }
1967
  }
1968

1969
  metaFetchEntryFree(&pEntry);
81,978✔
1970
  metaFetchEntryFree(&pSuper);
81,978✔
1971
  TAOS_RETURN(code);
81,978✔
1972
}
1973

1974
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
59,198✔
1975
  int32_t code = TSDB_CODE_SUCCESS;
59,198✔
1976

1977
  // check request
1978
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
59,198✔
1979
  if (code) {
59,198✔
1980
    TAOS_RETURN(code);
×
1981
  }
1982

1983
  // fetch old entry
1984
  SMetaEntry *pEntry = NULL;
59,198✔
1985
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
59,198✔
1986
  if (code) {
59,198✔
1987
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1988
              __FILE__, __LINE__, pReq->tbName, version);
1989
    TAOS_RETURN(code);
×
1990
  }
1991

1992
  if (pEntry->version >= version) {
59,198✔
1993
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1994
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1995
    metaFetchEntryFree(&pEntry);
×
1996
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1997
  }
1998

1999
  // fetch super entry
2000
  SMetaEntry *pSuper = NULL;
59,198✔
2001
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
59,198✔
2002
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
30,000✔
2003
    if (code) {
30,000✔
2004
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2005
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2006
      metaFetchEntryFree(&pEntry);
×
2007
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2008
    }
2009
  }
2010

2011
  // search the column to update
2012
  SSchemaWrapper *pSchema =
59,198✔
2013
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
59,198✔
2014
  SColRef *pColRef = NULL;
59,198✔
2015
  int32_t  iColumn = 0;
59,198✔
2016
  for (int32_t i = 0; i < pSchema->nCols; i++) {
240,415✔
2017
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
240,415✔
2018
      pColRef = &pEntry->colRef.pColRef[i];
59,198✔
2019
      iColumn = i;
59,198✔
2020
      break;
59,198✔
2021
    }
2022
  }
2023

2024
  if (NULL == pColRef) {
59,198✔
2025
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2026
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2027
    metaFetchEntryFree(&pEntry);
×
2028
    metaFetchEntryFree(&pSuper);
×
2029
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2030
  }
2031

2032
  // do update column name
2033
  pEntry->version = version;
59,198✔
2034
  pColRef->hasRef = false;
59,198✔
2035
  pColRef->id = pSchema->pSchema[iColumn].colId;
59,198✔
2036
  pSchema->version++;
59,198✔
2037
  pEntry->colRef.version++;
59,198✔
2038

2039
  // do handle entry
2040
  code = metaHandleEntry2(pMeta, pEntry);
59,198✔
2041
  if (code) {
59,198✔
2042
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2043
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2044
    metaFetchEntryFree(&pEntry);
×
2045
    metaFetchEntryFree(&pSuper);
×
2046
    TAOS_RETURN(code);
×
2047
  } else {
2048
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
59,198✔
2049
             pEntry->uid, version);
2050
  }
2051

2052
  // build response
2053
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
59,198✔
2054
  if (code) {
59,198✔
2055
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2056
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2057
  } else {
2058
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
362,370✔
2059
      SColRef *p = &pEntry->colRef.pColRef[i];
303,172✔
2060
      pRsp->pColRefs[i].hasRef = p->hasRef;
303,172✔
2061
      pRsp->pColRefs[i].id = p->id;
303,172✔
2062
      if (p->hasRef) {
303,172✔
2063
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
149,769✔
2064
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
149,769✔
2065
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
149,769✔
2066
      }
2067
    }
2068
  }
2069

2070
  metaFetchEntryFree(&pEntry);
59,198✔
2071
  metaFetchEntryFree(&pSuper);
59,198✔
2072
  TAOS_RETURN(code);
59,198✔
2073
}
2074

2075
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,938✔
2076
  int32_t code = TSDB_CODE_SUCCESS;
5,938✔
2077

2078
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
5,938✔
2079
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2080
              __FILE__, __LINE__, version);
2081
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2082
  }
2083

2084
  SMetaEntry *pEntry = NULL;
5,938✔
2085
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
5,938✔
2086
  if (code) {
5,938✔
2087
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2088
              __FILE__, __LINE__, pReq->name, version);
2089
    TAOS_RETURN(code);
×
2090
  }
2091

2092
  if (pEntry->type != TSDB_SUPER_TABLE) {
5,938✔
2093
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2094
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2095
    metaFetchEntryFree(&pEntry);
×
2096
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2097
  }
2098

2099
  if (pEntry->uid != pReq->suid) {
5,938✔
2100
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2101
              ", version:%" PRId64,
2102
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2103
    metaFetchEntryFree(&pEntry);
×
2104
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2105
  }
2106

2107
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2108
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2109
  //   PRId64,
2110
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2111
  //             pReq->schemaTag.version, version);
2112
  //   metaFetchEntryFree(&pEntry);
2113
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2114
  // }
2115

2116
  // do change the entry
2117
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
5,938✔
2118
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
5,938✔
2119
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5,938✔
2120
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2121
              __func__, __FILE__, __LINE__, pReq->name, version);
2122
    metaFetchEntryFree(&pEntry);
×
2123
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2124
  }
2125

2126
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
5,938✔
2127
    metaError(
×
2128
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2129
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2130
        version);
2131
    metaFetchEntryFree(&pEntry);
×
2132
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2133
  }
2134

2135
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2136
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2137
  //   PRId64,
2138
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2139
  //             pNewTagSchema->version, version);
2140
  //   metaFetchEntryFree(&pEntry);
2141
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2142
  // }
2143

2144
  int32_t numOfChangedTags = 0;
5,938✔
2145
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
30,272✔
2146
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
24,334✔
2147
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
24,334✔
2148

2149
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
24,334✔
2150
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
24,334✔
2151
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2152
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2153
      metaFetchEntryFree(&pEntry);
×
2154
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2155
    }
2156

2157
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
24,334✔
2158
      numOfChangedTags++;
5,938✔
2159
      SSCHMEA_SET_IDX_ON(pOldColumn);
5,938✔
2160
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
18,396✔
2161
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2162
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2163
      metaFetchEntryFree(&pEntry);
×
2164
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2165
    }
2166
  }
2167

2168
  if (numOfChangedTags != 1) {
5,938✔
2169
    metaError(
×
2170
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2171
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2172
    metaFetchEntryFree(&pEntry);
×
2173
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2174
  }
2175

2176
  pEntry->version = version;
5,938✔
2177
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
5,938✔
2178

2179
  // do handle the entry
2180
  code = metaHandleEntry2(pMeta, pEntry);
5,938✔
2181
  if (code) {
5,938✔
2182
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2183
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2184
    metaFetchEntryFree(&pEntry);
×
2185
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2186
  } else {
2187
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
5,938✔
2188
             pEntry->uid, version);
2189
  }
2190

2191
  metaFetchEntryFree(&pEntry);
5,938✔
2192
  TAOS_RETURN(code);
5,938✔
2193
}
2194

2195
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
3,956✔
2196
  int32_t code = TSDB_CODE_SUCCESS;
3,956✔
2197

2198
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
3,956✔
2199
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2200
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2201
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2202
  }
2203

2204
  SMetaEntry *pEntry = NULL;
3,956✔
2205
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
3,956✔
2206
  if (code) {
3,956✔
2207
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2208
              __FILE__, __LINE__, pReq->stb, version);
2209
    TAOS_RETURN(code);
×
2210
  }
2211

2212
  if (TSDB_SUPER_TABLE != pEntry->type) {
3,956✔
2213
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2214
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2215
    metaFetchEntryFree(&pEntry);
×
2216
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2217
  }
2218

2219
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
3,956✔
2220
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
3,956✔
2221
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2222
              __func__, __FILE__, __LINE__, pReq->stb, version);
2223
    metaFetchEntryFree(&pEntry);
×
2224
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2225
  }
2226

2227
  // search and set the tag index off
2228
  int32_t numOfChangedTags = 0;
3,956✔
2229
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
10,364✔
2230
    SSchema *pCol = pTagSchema->pSchema + i;
10,364✔
2231
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
10,364✔
2232
      if (!IS_IDX_ON(pCol)) {
3,956✔
2233
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2234
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2235
        metaFetchEntryFree(&pEntry);
×
2236
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2237
      }
2238
      numOfChangedTags++;
3,956✔
2239
      SSCHMEA_SET_IDX_OFF(pCol);
3,956✔
2240
      break;
3,956✔
2241
    }
2242
  }
2243

2244
  if (numOfChangedTags != 1) {
3,956✔
2245
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2246
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2247
    metaFetchEntryFree(&pEntry);
×
2248
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2249
  }
2250

2251
  // do handle the entry
2252
  pEntry->version = version;
3,956✔
2253
  pTagSchema->version++;
3,956✔
2254
  code = metaHandleEntry2(pMeta, pEntry);
3,956✔
2255
  if (code) {
3,956✔
2256
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2257
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2258
    metaFetchEntryFree(&pEntry);
×
2259
    TAOS_RETURN(code);
×
2260
  } else {
2261
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
3,956✔
2262
             pEntry->uid, version);
2263
  }
2264

2265
  metaFetchEntryFree(&pEntry);
3,956✔
2266
  TAOS_RETURN(code);
3,956✔
2267
}
2268

2269
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
9,025,357✔
2270
  int32_t code = TSDB_CODE_SUCCESS;
9,025,357✔
2271

2272
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
9,025,357✔
2273
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
32✔
2274
              __FILE__, __LINE__, version);
2275
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
32✔
2276
  }
2277

2278
  SMetaEntry *pEntry = NULL;
9,028,914✔
2279
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
9,027,475✔
2280
  if (code) {
9,021,050✔
2281
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2282
              __FILE__, __LINE__, pReq->name, version);
2283
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2284
  }
2285

2286
  if (pEntry->type != TSDB_SUPER_TABLE) {
9,021,050✔
2287
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2288
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2289
    metaFetchEntryFree(&pEntry);
×
2290
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2291
  }
2292

2293
  SMetaEntry entry = {
27,048,454✔
2294
      .version = version,
2295
      .type = TSDB_SUPER_TABLE,
2296
      .uid = pReq->suid,
9,018,921✔
2297
      .name = pReq->name,
9,021,868✔
2298
      .stbEntry.schemaRow = pReq->schemaRow,
2299
      .stbEntry.schemaTag = pReq->schemaTag,
2300
      .stbEntry.keep = pReq->keep,
9,014,791✔
2301
      .colCmpr = pReq->colCmpr,
2302
      .pExtSchemas = pReq->pExtSchemas,
9,013,365✔
2303
  };
2304
  TABLE_SET_COL_COMPRESSED(entry.flags);
9,017,179✔
2305
  if (pReq->virtualStb) {
9,017,179✔
2306
    TABLE_SET_VIRTUAL(entry.flags);
17,466✔
2307
  }
2308
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
9,011,944✔
2309
    TABLE_SET_ROLLUP(entry.flags);
4,776✔
2310
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,776✔
2311
  }
2312

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

2325
  metaFetchEntryFree(&pEntry);
9,027,015✔
2326
  TAOS_RETURN(code);
9,028,946✔
2327
}
2328

2329
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2330
  int32_t code = 0;
×
2331

2332
  if (taosArrayGetSize(uidArray) == 0) {
×
2333
    return TSDB_CODE_SUCCESS;
×
2334
  }
2335

2336
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2337
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2338
    SMetaInfo info;
×
2339
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2340
    if (code) {
×
2341
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2342
                __func__, __FILE__, __LINE__, uid, code);
2343
      return code;
×
2344
    }
2345

2346
    SMetaEntry entry = {
×
2347
        .version = version,
2348
        .uid = uid,
2349
    };
2350

2351
    if (info.suid == 0) {
×
2352
      entry.type = -TSDB_NORMAL_TABLE;
×
2353
    } else if (info.suid == uid) {
×
2354
      entry.type = -TSDB_SUPER_TABLE;
×
2355
    } else {
2356
      entry.type = -TSDB_CHILD_TABLE;
×
2357
    }
2358
    code = metaHandleEntry2(pMeta, &entry);
×
2359
    if (code) {
×
2360
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2361
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2362
      return code;
×
2363
    }
2364
  }
2365
  return code;
×
2366
}
2367

2368
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
23,880✔
2369
  int32_t code = TSDB_CODE_SUCCESS;
23,880✔
2370

2371
  if (NULL == pReq->name || pReq->name[0] == 0) {
23,880✔
2372
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2373
              __LINE__, version);
2374
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2375
  }
2376

2377
  SMetaEntry *pEntry = NULL;
23,880✔
2378
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,880✔
2379
  if (code) {
23,880✔
2380
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2381
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2382
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2383
  }
2384

2385
  if (pEntry->type != TSDB_SUPER_TABLE) {
23,880✔
2386
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2387
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2388
    metaFetchEntryFree(&pEntry);
×
2389
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2390
  }
2391

2392
  if (pEntry->uid != pReq->tbUid) {
23,880✔
2393
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2394
              ", version:%" PRId64,
2395
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2396
    metaFetchEntryFree(&pEntry);
×
2397
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2398
  }
2399

2400
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
23,880✔
2401
    // overwrite the old rsma definition if exists
2402
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2403
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2404
  } else {
2405
    TABLE_SET_ROLLUP(pEntry->flags);
23,880✔
2406
  }
2407

2408
  SMetaEntry entry = *pEntry;
23,880✔
2409
  entry.version = version;
23,880✔
2410
  entry.stbEntry.rsmaParam.name = pReq->name;
23,880✔
2411
  entry.stbEntry.rsmaParam.uid = pReq->uid;
23,880✔
2412
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
23,880✔
2413
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
23,880✔
2414
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
23,880✔
2415
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
23,880✔
2416
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
23,880✔
2417
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
23,880✔
2418

2419
  // do handle the entry
2420
  code = metaHandleEntry2(pMeta, &entry);
23,880✔
2421
  if (code) {
23,880✔
2422
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2423
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2424
    metaFetchEntryFree(&pEntry);
×
2425
    TAOS_RETURN(code);
×
2426
  } else {
2427
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
23,880✔
2428
    pMeta->pVnode->config.isRsma = 1;
23,880✔
2429
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
23,880✔
2430
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2431
  }
2432

2433
  metaFetchEntryFree(&pEntry);
23,880✔
2434
  TAOS_RETURN(code);
23,880✔
2435
}
2436

2437
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,776✔
2438
  int32_t code = TSDB_CODE_SUCCESS;
4,776✔
2439

2440
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,776✔
2441
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2442
              __LINE__, version);
2443
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2444
  }
2445

2446
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
4,776✔
2447
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2448
              __LINE__, version);
2449
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2450
  }
2451

2452
  SMetaEntry *pEntry = NULL;
4,776✔
2453
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,776✔
2454
  if (code) {
4,776✔
2455
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2456
             __func__, __LINE__, pReq->name, pReq->tbName, version);
2457
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2458
  }
2459

2460
  if (pEntry->type != pReq->tbType) {
4,776✔
2461
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2462
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
2463
    metaFetchEntryFree(&pEntry);
×
2464
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2465
  }
2466

2467
  if (pEntry->uid != pReq->tbUid) {
4,776✔
2468
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2469
              ", version:%" PRId64,
2470
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2471
    metaFetchEntryFree(&pEntry);
×
2472
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2473
  }
2474

2475
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
4,776✔
2476
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,776✔
2477
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,776✔
2478
      metaError(
×
2479
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2480
          "uid:%" PRIi64 ", version:%" PRId64,
2481
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2482
          pReq->uid, version);
2483
      metaFetchEntryFree(&pEntry);
×
2484
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2485
    }
2486
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
4,776✔
2487
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,776✔
2488
  } else {
2489
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2490
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
2491
    metaFetchEntryFree(&pEntry);
×
2492
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2493
  }
2494

2495
  SMetaEntry entry = *pEntry;
4,776✔
2496
  entry.version = version;
4,776✔
2497
  TABLE_RESET_ROLLUP(entry.flags);
4,776✔
2498
  entry.stbEntry.rsmaParam.uid = 0;
4,776✔
2499
  entry.stbEntry.rsmaParam.name = NULL;
4,776✔
2500
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,776✔
2501
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,776✔
2502
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,776✔
2503

2504
  // do handle the entry
2505
  code = metaHandleEntry2(pMeta, &entry);
4,776✔
2506
  if (code) {
4,776✔
2507
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2508
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
2509
    metaFetchEntryFree(&pEntry);
×
2510
    TAOS_RETURN(code);
×
2511
  } else {
2512
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
4,776✔
2513
      pMeta->pVnode->config.isRsma = 0;
×
2514
    }
2515
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
4,776✔
2516
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2517
  }
2518

2519
  metaFetchEntryFree(&pEntry);
4,776✔
2520
  TAOS_RETURN(code);
4,776✔
2521
}
2522

2523
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,736✔
2524
  int32_t code = TSDB_CODE_SUCCESS;
12,736✔
2525

2526
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,736✔
2527
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2528
              __LINE__, version);
2529
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2530
  }
2531

2532
  SMetaEntry *pEntry = NULL;
12,736✔
2533
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
12,736✔
2534
  if (code) {
12,736✔
2535
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2536
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2537
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2538
  }
2539

2540
  if (pEntry->uid != pReq->tbUid) {
12,736✔
2541
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2542
              ", version:%" PRId64,
2543
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2544
    metaFetchEntryFree(&pEntry);
×
2545
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2546
  }
2547

2548
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
12,736✔
2549
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
12,736✔
2550
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,736✔
2551
  } else {
2552
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2553
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2554
    metaFetchEntryFree(&pEntry);
×
2555
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2556
  }
2557

2558
  SMetaEntry entry = *pEntry;
12,736✔
2559
  entry.version = version;
12,736✔
2560
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
12,736✔
2561
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
12,736✔
2562
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
12,736✔
2563
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
12,736✔
2564
  }
2565
  // do handle the entry
2566
  code = metaHandleEntry2(pMeta, &entry);
12,736✔
2567
  if (code) {
12,736✔
2568
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2569
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
2570
    metaFetchEntryFree(&pEntry);
×
2571
    TAOS_RETURN(code);
×
2572
  } else {
2573
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
12,736✔
2574
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2575
  }
2576

2577
  metaFetchEntryFree(&pEntry);
12,736✔
2578
  TAOS_RETURN(code);
12,736✔
2579
}
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