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

taosdata / TDengine / #4944

30 Jan 2026 06:19AM UTC coverage: 66.849% (+0.1%) from 66.718%
#4944

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1124 of 2018 new or added lines in 72 files covered. (55.7%)

13677 existing lines in 155 files now uncovered.

205211 of 306978 relevant lines covered (66.85%)

125657591.7 hits per line

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

68.27
/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) {
5,040,571✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,040,571✔
31
  void     *value = NULL;
5,061,301✔
32
  int32_t   valueSize = 0;
5,064,093✔
33
  SMetaInfo info;
5,062,158✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
5,063,231✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
16,589✔
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);
5,044,696✔
43
  if (r == 0) {  // name exists, check uid and type
5,051,639✔
44
    int64_t uid = *(tb_uid_t *)value;
5,413✔
45
    tdbFree(value);
5,413✔
46

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

54
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
3,199✔
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) {
3,199✔
62
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
3,199✔
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) {
5,046,226✔
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;
5,046,986✔
79
}
80

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

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

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

117
  return code;
1,394,732✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
892,137✔
127
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
104✔
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);
891,459✔
133
  if (code) {
887,123✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1,119✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
1,119✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
886,004✔
139
    tdbFreeClear(value);
889,132✔
140

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

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

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,045,528✔
169
  if (code != TSDB_CODE_SUCCESS) {
5,057,007✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
5,413✔
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
3,199✔
172
               pReq->name, pReq->suid, version);
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
3,199✔
174
    } else {
175
      TAOS_RETURN(code);
2,214✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
10,078,919✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
5,045,166✔
184
      .name = pReq->name,
5,041,478✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
5,038,432✔
188
      .stbEntry.ownerId = pReq->ownerId,
5,029,615✔
189
  };
190
  if (pReq->rollup) {
5,041,011✔
191
    TABLE_SET_ROLLUP(entry.flags);
×
192
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
193
  }
194
  if (pReq->colCmpred) {
5,019,735✔
195
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,041,868✔
196
    entry.colCmpr = pReq->colCmpr;
5,041,868✔
197
  }
198

199
  entry.pExtSchemas = pReq->pExtSchemas;
5,026,795✔
200

201
  if (pReq->virtualStb) {
5,031,024✔
202
    TABLE_SET_VIRTUAL(entry.flags);
64,376✔
203
  }
204

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

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

220
  // check request
221
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
892,711✔
222
  if (code) {
884,200✔
223
    TAOS_RETURN(code);
1,857✔
224
  }
225

226
  // handle entry
227
  SMetaEntry entry = {
882,343✔
228
      .version = verison,
229
      .type = -TSDB_SUPER_TABLE,
230
      .uid = pReq->suid,
881,143✔
231
  };
232
  code = metaHandleEntry2(pMeta, &entry);
886,014✔
233
  if (code) {
889,677✔
234
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
235
              tstrerror(code));
236
  } else {
237
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
889,677✔
238
             pReq->suid, verison);
239
  }
240
  TAOS_RETURN(code);
890,854✔
241
}
242

243
// Alter Super Table
244

245
// Create Child Table
246
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
56,691,768✔
247
  int32_t   code = TSDB_CODE_SUCCESS;
56,691,768✔
248
  void     *value = NULL;
56,691,768✔
249
  int32_t   valueSize = 0;
56,691,808✔
250
  SMetaInfo info;
56,688,997✔
251

252
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
56,687,744✔
253
      pReq->ctb.suid == 0) {
56,687,191✔
254
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
2,619✔
255
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
256
    return TSDB_CODE_INVALID_MSG;
×
257
  }
258

259
  // check table existence
260
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
56,681,623✔
261
    pReq->uid = *(int64_t *)value;
340,355✔
262
    tdbFreeClear(value);
340,355✔
263

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

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

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

287
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
338,649✔
288
  }
289

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

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

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

315
  // Check tag value
316
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
56,331,911✔
317
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
56,344,070✔
318
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
56,344,459✔
319
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
248,616,345✔
320
      STagVal tagVal = {
192,534,091✔
321
          .cid = pTagSchema->pSchema[i].colId,
192,523,086✔
322
      };
323

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

335
  metaFetchEntryFree(&pStbEntry);
56,341,674✔
336

337
  // check grant
338
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
56,324,665✔
339
    code = grantCheck(TSDB_GRANT_TIMESERIES);
56,348,194✔
340
    if (TSDB_CODE_SUCCESS != code) {
56,334,310✔
341
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
342
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
343
    }
344
  }
345
  return code;
56,333,505✔
346
}
347

348
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
56,110,191✔
349
  int32_t code = TSDB_CODE_SUCCESS;
56,110,191✔
350

351
  if (NULL == ppRsp) {
56,110,191✔
352
    return code;
×
353
  }
354

355
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
56,110,191✔
356
  if (NULL == ppRsp) {
56,106,142✔
357
    return terrno;
×
358
  }
359

360
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
56,106,142✔
361
  (*ppRsp)->tuid = pEntry->uid;
56,111,657✔
362
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
56,120,914✔
363
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
56,124,631✔
364

365
  return code;
56,128,789✔
366
}
367

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

371
  // check request
372
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
56,484,745✔
373
  if (code) {
56,472,838✔
374
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
340,401✔
375
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,752✔
376
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
377
    }
378
    return code;
340,355✔
379
  }
380

381
  SMetaEntry entry = {
56,132,437✔
382
      .version = version,
383
      .type = TSDB_CHILD_TABLE,
384
      .uid = pReq->uid,
56,128,304✔
385
      .name = pReq->name,
56,120,553✔
386
      .ctbEntry.btime = pReq->btime,
56,136,205✔
387
      .ctbEntry.ttlDays = pReq->ttl,
56,124,980✔
388
      .ctbEntry.commentLen = pReq->commentLen,
56,120,833✔
389
      .ctbEntry.comment = pReq->comment,
56,115,384✔
390
      .ctbEntry.suid = pReq->ctb.suid,
56,125,382✔
391
      .ctbEntry.pTags = pReq->ctb.pTag,
56,105,635✔
392
  };
393

394
  // build response
395
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
56,120,281✔
396
  if (code) {
56,129,855✔
397
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
398
              tstrerror(code));
399
  }
400

401
  // handle entry
402
  code = metaHandleEntry2(pMeta, &entry);
56,129,855✔
403
  if (TSDB_CODE_SUCCESS == code) {
56,142,491✔
404
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
56,143,485✔
405
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
406
  } else {
407
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
153✔
408
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
409
              pReq->ctb.suid, version);
410
  }
411
  return code;
56,150,031✔
412
}
413

414
// Drop Child Table
415

416
// Alter Child Table
417

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

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

430
  // check name
431
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
5,900,162✔
432
    // for auto create table, we return the uid of the existing table
433
    pReq->uid = *(tb_uid_t *)value;
41,322✔
434
    tdbFree(value);
41,322✔
435
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
41,322✔
436
  }
437

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

447
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
5,726,038✔
448
  int32_t code = TSDB_CODE_SUCCESS;
5,726,038✔
449

450
  if (NULL == ppRsp) {
5,726,038✔
451
    return code;
×
452
  }
453

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

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

465
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
73,688,972✔
466
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
67,962,934✔
467
    (*ppRsp)->pSchemaExt[i].colId = p->id;
67,962,934✔
468
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
67,962,934✔
469
    if (pEntry->pExtSchemas) {
67,962,934✔
470
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
443,745✔
471
    }
472
  }
473

474
  return code;
5,726,038✔
475
}
476

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

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

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

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

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

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

529
  if (NULL == ppRsp) {
132,802✔
UNCOV
530
    return code;
×
531
  }
532

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

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

545
  return code;
132,276✔
546
}
547

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

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

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

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

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

597
  if (NULL == ppRsp) {
207,023✔
UNCOV
598
    return code;
×
599
  }
600

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

606
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
207,023✔
607
  if (code) {
207,023✔
608
    taosMemoryFreeClear(*ppRsp);
526✔
609
    return code;
526✔
610
  }
611
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
206,497✔
612

613
  return code;
206,497✔
614
}
615

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

627
  SMetaEntry entry = {.version = version,
414,046✔
628
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
629
                      .uid = pReq->uid,
207,023✔
630
                      .name = pReq->name,
207,023✔
631
                      .ctbEntry.btime = pReq->btime,
207,023✔
632
                      .ctbEntry.ttlDays = pReq->ttl,
207,023✔
633
                      .ctbEntry.commentLen = pReq->commentLen,
207,023✔
634
                      .ctbEntry.comment = pReq->comment,
207,023✔
635
                      .ctbEntry.suid = pReq->ctb.suid,
207,023✔
636
                      .ctbEntry.pTags = pReq->ctb.pTag,
207,023✔
637
                      .colRef = pReq->colRef};
638

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

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

661
// Drop Normal Table
662

663
// Alter Normal Table
664

665
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
62,590,141✔
666
  int32_t code = TSDB_CODE_SUCCESS;
62,590,141✔
667
  if (TSDB_CHILD_TABLE == pReq->type) {
62,590,141✔
668
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
56,485,156✔
669
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
6,107,185✔
670
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
5,767,360✔
671
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
339,825✔
672
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
132,802✔
673
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
207,023✔
674
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
207,023✔
675
  } else {
UNCOV
676
    code = TSDB_CODE_INVALID_MSG;
×
677
  }
678
  TAOS_RETURN(code);
62,597,621✔
679
}
680

681
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,394,732✔
682
  int32_t code = TSDB_CODE_SUCCESS;
1,394,732✔
683

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

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

701
  SMetaEntry entry = {
1,394,732✔
702
      .version = version,
703
      .uid = pReq->uid,
1,394,732✔
704
  };
705

706
  if (pReq->isVirtual) {
1,394,732✔
707
    if (pReq->suid == 0) {
53,714✔
708
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
27,091✔
709
    } else {
710
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,623✔
711
    }
712
  } else {
713
    if (pReq->suid == 0) {
1,341,018✔
714
      entry.type = -TSDB_NORMAL_TABLE;
776,301✔
715
    } else {
716
      entry.type = -TSDB_CHILD_TABLE;
564,717✔
717
    }
718
  }
719
  code = metaHandleEntry2(pMeta, &entry);
1,394,732✔
720
  if (code) {
1,394,732✔
UNCOV
721
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
722
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
723
  } else {
724
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,394,732✔
725
             pReq->uid, version);
726
  }
727
  TAOS_RETURN(code);
1,394,732✔
728
}
729

730
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,209,123✔
731
  int32_t code = 0;
4,209,123✔
732

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

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

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

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

779
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,473,195✔
780
  int32_t code = TSDB_CODE_SUCCESS;
3,473,195✔
781

782
  // check request
783
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,473,195✔
784
  if (code) {
3,473,195✔
UNCOV
785
    TAOS_RETURN(code);
×
786
  }
787

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

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

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

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

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

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

914
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,159,297✔
915
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
50,800✔
916
    if (code) {
50,800✔
UNCOV
917
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
918
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
919
    } else {
920
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
17,613,908✔
921
        SColRef *p = &pEntry->colRef.pColRef[i];
17,563,108✔
922
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,563,108✔
923
        pRsp->pColRefs[i].id = p->id;
17,563,108✔
924
        if (p->hasRef) {
17,563,108✔
925
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
163,768✔
926
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
163,768✔
927
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
163,768✔
928
        }
929
      }
930
    }
931
  } else {
932
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
3,108,497✔
933
    if (code) {
3,108,497✔
UNCOV
934
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
935
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
936
    } else {
937
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
938
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
939
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
940
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
941
      }
942
    }
943
  }
944

945
  metaFetchEntryFree(&pEntry);
3,159,297✔
946
  TAOS_RETURN(code);
3,159,297✔
947
}
948

949
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
87,791✔
950
  int32_t code = TSDB_CODE_SUCCESS;
87,791✔
951

952
  // check request
953
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
87,791✔
954
  if (code) {
87,791✔
UNCOV
955
    TAOS_RETURN(code);
×
956
  }
957

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

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

974
  // search the column to drop
975
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
87,791✔
976
  SSchema        *pColumn = NULL;
87,791✔
977
  SSchema         tColumn;
87,791✔
978
  int32_t         iColumn = 0;
87,791✔
979
  for (; iColumn < pSchema->nCols; iColumn++) {
66,841,780✔
980
    pColumn = &pSchema->pSchema[iColumn];
66,841,780✔
981
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
66,841,780✔
982
      break;
87,791✔
983
    }
984
  }
985

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

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

1000
  tColumn = *pColumn;
87,791✔
1001

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

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

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

1060
  // build response
1061
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
87,791✔
1062
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
29,022✔
1063
    if (code) {
29,022✔
UNCOV
1064
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1065
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1066
    } else {
1067
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
34,655,240✔
1068
        SColRef *p = &pEntry->colRef.pColRef[i];
34,626,218✔
1069
        pRsp->pColRefs[i].hasRef = p->hasRef;
34,626,218✔
1070
        pRsp->pColRefs[i].id = p->id;
34,626,218✔
1071
        if (p->hasRef) {
34,626,218✔
1072
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
17,325,432✔
1073
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
17,325,432✔
1074
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
17,325,432✔
1075
        }
1076
      }
1077
    }
1078
  } else {
1079
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
58,769✔
1080
    if (code) {
58,769✔
UNCOV
1081
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1082
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1083
    } else {
1084
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
59,955,472✔
1085
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
59,896,703✔
1086
        pRsp->pSchemaExt[i].colId = p->id;
59,896,703✔
1087
        pRsp->pSchemaExt[i].compress = p->alg;
59,896,703✔
1088
      }
1089
    }
1090
  }
1091

1092
  metaFetchEntryFree(&pEntry);
87,791✔
1093
  TAOS_RETURN(code);
87,791✔
1094
}
1095

1096
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
44,183✔
1097
  int32_t code = TSDB_CODE_SUCCESS;
44,183✔
1098

1099
  // check request
1100
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
44,183✔
1101
  if (code) {
44,183✔
UNCOV
1102
    TAOS_RETURN(code);
×
1103
  }
1104

1105
  if (NULL == pReq->colNewName) {
44,183✔
UNCOV
1106
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1107
              __func__, __FILE__, __LINE__, version);
UNCOV
1108
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1109
  }
1110

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

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

1127
  // search the column to update
1128
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
44,183✔
1129
  SSchema        *pColumn = NULL;
44,183✔
1130
  int32_t         iColumn = 0;
44,183✔
1131
  for (int32_t i = 0; i < pSchema->nCols; i++) {
201,707✔
1132
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
201,707✔
1133
      pColumn = &pSchema->pSchema[i];
44,183✔
1134
      iColumn = i;
44,183✔
1135
      break;
44,183✔
1136
    }
1137
  }
1138

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

1146

1147
  // do update column name
1148
  pEntry->version = version;
44,183✔
1149
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
44,183✔
1150
  pSchema->version++;
44,183✔
1151

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

1164
  // build response
1165
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
44,183✔
1166
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
27,032✔
1167
    if (code) {
27,032✔
UNCOV
1168
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1169
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1170
    } else {
1171
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
191,052✔
1172
        SColRef *p = &pEntry->colRef.pColRef[i];
164,020✔
1173
        pRsp->pColRefs[i].hasRef = p->hasRef;
164,020✔
1174
        pRsp->pColRefs[i].id = p->id;
164,020✔
1175
        if (p->hasRef) {
164,020✔
1176
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
101,192✔
1177
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
101,192✔
1178
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
101,192✔
1179
        }
1180
      }
1181
    }
1182
  } else {
1183
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
17,151✔
1184
    if (code) {
17,151✔
UNCOV
1185
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1186
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1187
    } else {
1188
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
179,800✔
1189
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
162,649✔
1190
        pRsp->pSchemaExt[i].colId = p->id;
162,649✔
1191
        pRsp->pSchemaExt[i].compress = p->alg;
162,649✔
1192
      }
1193
    }
1194
  }
1195

1196
  metaFetchEntryFree(&pEntry);
44,183✔
1197
  TAOS_RETURN(code);
44,183✔
1198
}
1199

1200
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
476,770✔
1201
  int32_t code = TSDB_CODE_SUCCESS;
476,770✔
1202

1203
  // check request
1204
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
476,770✔
1205
  if (code) {
476,770✔
UNCOV
1206
    TAOS_RETURN(code);
×
1207
  }
1208

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

1218
  if (pEntry->version >= version) {
476,770✔
UNCOV
1219
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1220
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
1221
    metaFetchEntryFree(&pEntry);
×
1222
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1223
  }
1224

1225
  // search the column to update
1226
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
476,770✔
1227
  SSchema        *pColumn = NULL;
476,770✔
1228
  int32_t         iColumn = 0;
476,770✔
1229
  int32_t         rowSize = 0;
476,770✔
1230
  for (int32_t i = 0; i < pSchema->nCols; i++) {
35,939,966✔
1231
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
35,463,196✔
1232
      pColumn = &pSchema->pSchema[i];
476,770✔
1233
      iColumn = i;
476,770✔
1234
    }
1235
    rowSize += pSchema->pSchema[i].bytes;
35,463,196✔
1236
  }
1237

1238
  if (NULL == pColumn) {
476,770✔
UNCOV
1239
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1240
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
UNCOV
1241
    metaFetchEntryFree(&pEntry);
×
1242
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1243
  }
1244

1245
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
476,770✔
1246
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
188,400✔
1247
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1248
              version);
1249
    metaFetchEntryFree(&pEntry);
188,400✔
1250
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
188,400✔
1251
  }
1252

1253
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
288,370✔
1254
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
288,370✔
1255
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
43,960✔
1256
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1257
              version);
1258
    metaFetchEntryFree(&pEntry);
43,960✔
1259
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
43,960✔
1260
  }
1261

1262
  // do change the column bytes
1263
  pEntry->version = version;
244,410✔
1264
  pSchema->version++;
244,410✔
1265
  pColumn->bytes = pReq->colModBytes;
244,410✔
1266

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

1279
  // build response
1280
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
244,410✔
1281
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,274✔
1282
    if (code) {
26,274✔
UNCOV
1283
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1284
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1285
    } else {
1286
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
17,414,814✔
1287
        SColRef *p = &pEntry->colRef.pColRef[i];
17,388,540✔
1288
        pRsp->pColRefs[i].hasRef = p->hasRef;
17,388,540✔
1289
        pRsp->pColRefs[i].id = p->id;
17,388,540✔
1290
        if (p->hasRef) {
17,388,540✔
1291
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
76,616✔
1292
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
76,616✔
1293
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
76,616✔
1294
        }
1295
      }
1296
    }
1297
  } else {
1298
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
218,136✔
1299
    if (code) {
218,136✔
UNCOV
1300
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1301
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1302
    } else {
1303
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,766,032✔
1304
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,547,896✔
1305
        pRsp->pSchemaExt[i].colId = p->id;
8,547,896✔
1306
        pRsp->pSchemaExt[i].compress = p->alg;
8,547,896✔
1307
      }
1308
    }
1309
  }
1310

1311
  metaFetchEntryFree(&pEntry);
244,410✔
1312
  TAOS_RETURN(code);
244,410✔
1313
}
1314

1315
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,555,874✔
1316
  int32_t code = 0;
7,555,874✔
1317

1318
  // check tag name
1319
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
7,555,874✔
UNCOV
1320
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1321
              __func__, __FILE__, __LINE__, pReq->tagName, version);
UNCOV
1322
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1323
  }
1324

1325
  // check name
1326
  void   *value = NULL;
7,555,874✔
1327
  int32_t valueSize = 0;
7,555,874✔
1328
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
7,555,874✔
1329
  if (code) {
7,555,874✔
1330
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
270✔
1331
              __FILE__, __LINE__, pReq->tbName, version);
1332
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
270✔
1333
    TAOS_RETURN(code);
270✔
1334
  }
1335
  tdbFreeClear(value);
7,555,604✔
1336

1337
  TAOS_RETURN(code);
7,555,604✔
1338
}
1339

1340
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1341

1342
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,457,332✔
1343
  if (isNull) {
7,457,332✔
1344
    if (!tTagGet(pOldTag, &value)) {
45,853✔
1345
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,677✔
1346
      return true;
19,677✔
1347
    }
1348
    return false;
26,176✔
1349
  }
1350
  if (!tTagGet(pOldTag, &value)){
7,411,479✔
1351
    return false;
179,824✔
1352
  }
1353
  if (IS_VAR_DATA_TYPE(value.type)) {
7,231,655✔
1354
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
57,285✔
1355
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
36,162✔
1356
      return true;
36,162✔
1357
    }
1358
  } else {
1359
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,174,370✔
1360
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
87,881✔
1361
      return true;
87,881✔
1362
    }
1363
  }
1364
  return false;
7,107,612✔
1365
}
1366

1367
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,555,874✔
1368
  int32_t code = TSDB_CODE_SUCCESS;
7,555,874✔
1369

1370
  // check request
1371
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,555,874✔
1372
  if (code) {
7,555,874✔
1373
    TAOS_RETURN(code);
270✔
1374
  }
1375

1376
  // fetch child entry
1377
  SMetaEntry *pChild = NULL;
7,555,604✔
1378
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
7,555,604✔
1379
  if (code) {
7,555,604✔
UNCOV
1380
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1381
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1382
    TAOS_RETURN(code);
×
1383
  }
1384

1385
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
7,555,604✔
UNCOV
1386
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1387
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1388
    metaFetchEntryFree(&pChild);
×
1389
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1390
  }
1391

1392
  // fetch super entry
1393
  SMetaEntry *pSuper = NULL;
7,555,604✔
1394
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
7,555,604✔
1395
  if (code) {
7,555,604✔
UNCOV
1396
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1397
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
UNCOV
1398
    metaFetchEntryFree(&pChild);
×
1399
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1400
  }
1401

1402
  // search the tag to update
1403
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,555,604✔
1404
  SSchema        *pColumn = NULL;
7,555,604✔
1405
  int32_t         iColumn = 0;
7,555,604✔
1406
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,219,690✔
1407
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8,219,690✔
1408
      pColumn = &pTagSchema->pSchema[i];
7,555,604✔
1409
      iColumn = i;
7,555,604✔
1410
      break;
7,555,604✔
1411
    }
1412
  }
1413

1414
  if (NULL == pColumn) {
7,555,604✔
UNCOV
1415
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1416
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
UNCOV
1417
    metaFetchEntryFree(&pChild);
×
1418
    metaFetchEntryFree(&pSuper);
×
UNCOV
1419
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1420
  }
1421

1422
  // do change tag value
1423
  pChild->version = version;
7,555,604✔
1424
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
7,555,604✔
1425
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
117,922✔
1426
    if (NULL == pNewTag) {
117,922✔
UNCOV
1427
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1428
                __LINE__, tstrerror(terrno), version);
UNCOV
1429
      metaFetchEntryFree(&pChild);
×
1430
      metaFetchEntryFree(&pSuper);
×
UNCOV
1431
      TAOS_RETURN(terrno);
×
1432
    }
1433
    pChild->ctbEntry.pTags = pNewTag;
117,922✔
1434
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
117,922✔
1435
  } else {
1436
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
7,437,682✔
1437

1438
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
7,437,682✔
1439
    if (NULL == pTagArray) {
7,437,682✔
UNCOV
1440
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1441
                __LINE__, tstrerror(terrno), version);
UNCOV
1442
      metaFetchEntryFree(&pChild);
×
1443
      metaFetchEntryFree(&pSuper);
×
UNCOV
1444
      TAOS_RETURN(terrno);
×
1445
    }
1446

1447
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,813,216✔
1448
      STagVal value = {
8,510,084✔
1449
          .type = pTagSchema->pSchema[i].type,
8,510,084✔
1450
          .cid = pTagSchema->pSchema[i].colId,
8,510,084✔
1451
      };
1452

1453
      if (iColumn == i) {
8,510,084✔
1454
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,437,682✔
1455
          taosArrayDestroy(pTagArray);
134,550✔
1456
          metaFetchEntryFree(&pChild);
134,550✔
1457
          metaFetchEntryFree(&pSuper);
134,550✔
1458
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
134,550✔
1459
        }
1460
        if (pReq->isNull) {
7,303,132✔
1461
          continue;
24,211✔
1462
        }
1463
        if (IS_VAR_DATA_TYPE(value.type)) {
7,278,921✔
1464
          value.pData = pReq->pTagVal;
101,272✔
1465
          value.nData = pReq->nTagVal;
101,272✔
1466
        } else {
1467
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,177,649✔
1468
        }
1469
      } else if (!tTagGet(pOldTag, &value)) {
1,072,402✔
1470
        continue;
291,355✔
1471
      }
1472

1473
      if (NULL == taosArrayPush(pTagArray, &value)) {
8,059,968✔
UNCOV
1474
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1475
                  __LINE__, tstrerror(terrno), version);
UNCOV
1476
        taosArrayDestroy(pTagArray);
×
1477
        metaFetchEntryFree(&pChild);
×
UNCOV
1478
        metaFetchEntryFree(&pSuper);
×
1479
        TAOS_RETURN(terrno);
×
1480
      }
1481
    }
1482

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

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

1511
  // free resource and return
1512
  metaFetchEntryFree(&pChild);
7,421,054✔
1513
  metaFetchEntryFree(&pSuper);
7,421,054✔
1514
  TAOS_RETURN(code);
7,421,054✔
1515
}
1516

1517
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,275✔
1518
  int32_t code = 0;
3,275✔
1519

1520
  // check tag name
1521
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
3,275✔
UNCOV
1522
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1523
              __FILE__, __LINE__, version);
UNCOV
1524
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1525
  }
1526

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

1539
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
3,275✔
UNCOV
1540
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1541
              __FILE__, __LINE__, version);
UNCOV
1542
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1543
  }
1544

1545
  TAOS_RETURN(code);
3,275✔
1546
}
1547

1548
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,275✔
1549
  int32_t code = TSDB_CODE_SUCCESS;
3,275✔
1550

1551
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,275✔
1552
  if (code) {
3,275✔
UNCOV
1553
    TAOS_RETURN(code);
×
1554
  }
1555

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

1565
  if (pChild->type != TSDB_CHILD_TABLE) {
3,275✔
UNCOV
1566
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1567
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1568
    metaFetchEntryFree(&pChild);
×
1569
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1570
  }
1571

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

1582
  // search the tags to update
1583
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,275✔
1584

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

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

1604
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
22,925✔
1605
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
19,650✔
1606
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
19,650✔
UNCOV
1607
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1608
                __LINE__, tstrerror(terrno), version);
UNCOV
1609
      taosHashCleanup(pTagTable);
×
1610
      metaFetchEntryFree(&pChild);
×
UNCOV
1611
      metaFetchEntryFree(&pSuper);
×
1612
      TAOS_RETURN(terrno);
×
1613
    }
1614
  }
1615

1616
  int32_t numOfChangedTags = 0;
3,275✔
1617
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
26,200✔
1618
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
22,925✔
1619
        ? numOfChangedTags++
19,650✔
1620
        : 0;
22,925✔
1621
  }
1622
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,275✔
UNCOV
1623
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1624
              __FILE__, __LINE__, version);
UNCOV
1625
    taosHashCleanup(pTagTable);
×
1626
    metaFetchEntryFree(&pChild);
×
UNCOV
1627
    metaFetchEntryFree(&pSuper);
×
1628
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1629
  }
1630

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

1644
  bool allSame = true;
3,275✔
1645

1646
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
26,200✔
1647
    SSchema *pCol = &pTagSchema->pSchema[i];
22,925✔
1648
    STagVal  value = {
22,925✔
1649
         .cid = pCol->colId,
22,925✔
1650
    };
1651

1652
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
22,925✔
1653
    if (pTagVal == NULL) {
22,925✔
1654
      if (!tTagGet(pOldTag, &value)) {
3,275✔
UNCOV
1655
        continue;
×
1656
      }
1657
    } else {
1658
      value.type = pCol->type;
19,650✔
1659
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
19,650✔
1660
        allSame = false;
10,480✔
1661
      }
1662
      if (pTagVal->isNull) {
19,650✔
1663
        continue;
3,930✔
1664
      }
1665

1666
      if (IS_VAR_DATA_TYPE(pCol->type)) {
15,720✔
1667
        value.pData = pTagVal->pTagVal;
2,620✔
1668
        value.nData = pTagVal->nTagVal;
2,620✔
1669
      } else {
1670
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
13,100✔
1671
      }
1672
    }
1673

1674
    if (taosArrayPush(pTagArray, &value) == NULL) {
18,995✔
UNCOV
1675
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1676
                __LINE__, tstrerror(terrno), version);
UNCOV
1677
      taosHashCleanup(pTagTable);
×
1678
      taosArrayDestroy(pTagArray);
×
UNCOV
1679
      metaFetchEntryFree(&pChild);
×
1680
      metaFetchEntryFree(&pSuper);
×
1681
      TAOS_RETURN(terrno);
×
1682
    }
1683
  }
1684

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

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

1723
  taosHashCleanup(pTagTable);
2,620✔
1724
  metaFetchEntryFree(&pChild);
2,620✔
1725
  metaFetchEntryFree(&pSuper);
2,620✔
1726
  TAOS_RETURN(code);
2,620✔
1727
}
1728

1729
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,302✔
1730
  int32_t code = TSDB_CODE_SUCCESS;
24,302✔
1731

1732
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
24,302✔
UNCOV
1733
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1734
              __FILE__, __LINE__, version);
UNCOV
1735
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1736
  }
1737

1738
  return code;
24,302✔
1739
}
1740

1741
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,302✔
1742
  int32_t code = 0;
24,302✔
1743

1744
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
24,302✔
1745
  if (code) {
24,302✔
UNCOV
1746
    TAOS_RETURN(code);
×
1747
  }
1748

1749
  // fetch entry
1750
  SMetaEntry *pEntry = NULL;
24,302✔
1751
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
24,302✔
1752
  if (code) {
24,302✔
UNCOV
1753
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1754
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1755
    TAOS_RETURN(code);
×
1756
  }
1757

1758
  // do change the entry
1759
  pEntry->version = version;
24,302✔
1760
  if (pEntry->type == TSDB_CHILD_TABLE) {
24,302✔
1761
    if (pReq->updateTTL) {
11,345✔
1762
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,403✔
1763
    }
1764
    if (pReq->newCommentLen >= 0) {
11,345✔
1765
      char *pNewComment = NULL;
6,942✔
1766
      if (pReq->newCommentLen) {
6,942✔
1767
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,407✔
1768
        if (NULL == pNewComment) {
4,407✔
UNCOV
1769
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1770
                    __LINE__, tstrerror(terrno), version);
UNCOV
1771
          metaFetchEntryFree(&pEntry);
×
1772
          TAOS_RETURN(terrno);
×
1773
        }
1774
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,407✔
1775
      } else {
1776
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,535✔
1777
      }
1778
      pEntry->ctbEntry.comment = pNewComment;
6,942✔
1779
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
6,942✔
1780
    }
1781
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
12,957✔
1782
    if (pReq->updateTTL) {
12,957✔
1783
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,563✔
1784
    }
1785
    if (pReq->newCommentLen >= 0) {
12,957✔
1786
      char *pNewComment = NULL;
8,394✔
1787
      if (pReq->newCommentLen > 0) {
8,394✔
1788
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
5,859✔
1789
        if (NULL == pNewComment) {
5,859✔
UNCOV
1790
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1791
                    __LINE__, tstrerror(terrno), version);
UNCOV
1792
          metaFetchEntryFree(&pEntry);
×
1793
          TAOS_RETURN(terrno);
×
1794
        }
1795
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
5,859✔
1796
      } else {
1797
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,535✔
1798
      }
1799
      pEntry->ntbEntry.comment = pNewComment;
8,394✔
1800
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
8,394✔
1801
    }
1802
  } else {
UNCOV
1803
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1804
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
UNCOV
1805
    metaFetchEntryFree(&pEntry);
×
1806
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1807
  }
1808

1809
  // do handle entry
1810
  code = metaHandleEntry2(pMeta, pEntry);
24,302✔
1811
  if (code) {
24,302✔
UNCOV
1812
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1813
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
1814
    metaFetchEntryFree(&pEntry);
×
1815
    TAOS_RETURN(code);
×
1816
  } else {
1817
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
24,302✔
1818
             pEntry->uid, version);
1819
  }
1820

1821
  metaFetchEntryFree(&pEntry);
24,302✔
1822
  TAOS_RETURN(code);
24,302✔
1823
}
1824

1825
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5,902✔
1826
  int32_t code = TSDB_CODE_SUCCESS;
5,902✔
1827

1828
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5,902✔
UNCOV
1829
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1830
              __FILE__, __LINE__, version);
UNCOV
1831
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1832
  }
1833

1834
  SMetaEntry *pEntry = NULL;
5,902✔
1835
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5,902✔
1836
  if (code) {
5,902✔
UNCOV
1837
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1838
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1839
    TAOS_RETURN(code);
×
1840
  }
1841

1842
  if (pEntry->version >= version) {
5,902✔
UNCOV
1843
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1844
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
1845
    metaFetchEntryFree(&pEntry);
×
1846
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1847
  }
1848

1849
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
5,902✔
UNCOV
1850
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1851
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
UNCOV
1852
    metaFetchEntryFree(&pEntry);
×
1853
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1854
  }
1855

1856
  // do change the entry
1857
  int8_t           updated = 0;
5,902✔
1858
  SColCmprWrapper *wp = &pEntry->colCmpr;
5,902✔
1859
  for (int32_t i = 0; i < wp->nCols; i++) {
47,216✔
1860
    SColCmpr *p = &wp->pColCmpr[i];
41,314✔
1861
    if (p->id == pReq->colId) {
41,314✔
1862
      uint32_t dst = 0;
5,902✔
1863
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5,902✔
1864
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1865
      if (updated > 0) {
5,902✔
1866
        p->alg = dst;
5,902✔
1867
      }
1868
    }
1869
  }
1870

1871
  if (updated == 0) {
5,902✔
UNCOV
1872
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
UNCOV
1873
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1874
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1875
    metaFetchEntryFree(&pEntry);
×
1876
    TAOS_RETURN(code);
×
1877
  } else if (updated < 0) {
5,902✔
1878
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1879
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1880
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1881
    metaFetchEntryFree(&pEntry);
×
1882
    TAOS_RETURN(code);
×
1883
  }
1884

1885
  pEntry->version = version;
5,902✔
1886

1887
  // do handle entry
1888
  code = metaHandleEntry2(pMeta, pEntry);
5,902✔
1889
  if (code) {
5,902✔
UNCOV
1890
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1891
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
1892
    metaFetchEntryFree(&pEntry);
×
1893
    TAOS_RETURN(code);
×
1894
  } else {
1895
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5,902✔
1896
             pEntry->uid, version);
1897
  }
1898

1899
  metaFetchEntryFree(&pEntry);
5,902✔
1900
  TAOS_RETURN(code);
5,902✔
1901
}
1902

1903
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
73,188✔
1904
  int32_t code = TSDB_CODE_SUCCESS;
73,188✔
1905

1906
  // check request
1907
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
73,188✔
1908
  if (code) {
73,188✔
UNCOV
1909
    TAOS_RETURN(code);
×
1910
  }
1911

1912
  if (NULL == pReq->refDbName) {
73,188✔
UNCOV
1913
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1914
              __func__, __FILE__, __LINE__, version);
UNCOV
1915
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1916
  }
1917

1918
  if (NULL == pReq->refTbName) {
73,188✔
UNCOV
1919
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1920
              __func__, __FILE__, __LINE__, version);
UNCOV
1921
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1922
  }
1923

1924
  if (NULL == pReq->refColName) {
73,188✔
UNCOV
1925
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1926
              __func__, __FILE__, __LINE__, version);
UNCOV
1927
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1928
  }
1929

1930
  // fetch old entry
1931
  SMetaEntry *pEntry = NULL;
73,188✔
1932
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
73,188✔
1933
  if (code) {
73,188✔
UNCOV
1934
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1935
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
1936
    TAOS_RETURN(code);
×
1937
  }
1938

1939
  if (pEntry->version >= version) {
73,188✔
UNCOV
1940
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1941
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
1942
    metaFetchEntryFree(&pEntry);
×
1943
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1944
  }
1945

1946
  // fetch super entry
1947
  SMetaEntry *pSuper = NULL;
73,188✔
1948
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
73,188✔
1949
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
26,420✔
1950
    if (code) {
26,420✔
UNCOV
1951
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1952
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
UNCOV
1953
      metaFetchEntryFree(&pEntry);
×
1954
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1955
    }
1956
  }
1957

1958
  // search the column to update
1959
  SSchemaWrapper *pSchema =
73,188✔
1960
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
73,188✔
1961
  SColRef *pColRef = NULL;
73,188✔
1962
  int32_t  iColumn = 0;
73,188✔
1963
  for (int32_t i = 0; i < pSchema->nCols; i++) {
362,510✔
1964
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
362,510✔
1965
      pColRef = &pEntry->colRef.pColRef[i];
73,188✔
1966
      iColumn = i;
73,188✔
1967
      break;
73,188✔
1968
    }
1969
  }
1970

1971
  if (NULL == pColRef) {
73,188✔
UNCOV
1972
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1973
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
UNCOV
1974
    metaFetchEntryFree(&pEntry);
×
1975
    metaFetchEntryFree(&pSuper);
×
UNCOV
1976
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1977
  }
1978

1979
  // do update column name
1980
  pEntry->version = version;
73,188✔
1981
  pColRef->hasRef = true;
73,188✔
1982
  pColRef->id = pSchema->pSchema[iColumn].colId;
73,188✔
1983
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
73,188✔
1984
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
73,188✔
1985
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
73,188✔
1986
  pSchema->version++;
73,188✔
1987
  pEntry->colRef.version++;
73,188✔
1988

1989
  // do handle entry
1990
  code = metaHandleEntry2(pMeta, pEntry);
73,188✔
1991
  if (code) {
73,188✔
UNCOV
1992
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1993
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
1994
    metaFetchEntryFree(&pEntry);
×
1995
    metaFetchEntryFree(&pSuper);
×
UNCOV
1996
    TAOS_RETURN(code);
×
1997
  } else {
1998
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
73,188✔
1999
             pEntry->uid, version);
2000
  }
2001

2002
  // build response
2003
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
73,188✔
2004
  if (code) {
73,188✔
UNCOV
2005
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2006
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2007
  } else {
2008
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
546,472✔
2009
      SColRef *p = &pEntry->colRef.pColRef[i];
473,284✔
2010
      pRsp->pColRefs[i].hasRef = p->hasRef;
473,284✔
2011
      pRsp->pColRefs[i].id = p->id;
473,284✔
2012
      if (p->hasRef) {
473,284✔
2013
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
326,678✔
2014
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
326,678✔
2015
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
326,678✔
2016
      }
2017
    }
2018
  }
2019

2020
  metaFetchEntryFree(&pEntry);
73,188✔
2021
  metaFetchEntryFree(&pSuper);
73,188✔
2022
  TAOS_RETURN(code);
73,188✔
2023
}
2024

2025
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
53,996✔
2026
  int32_t code = TSDB_CODE_SUCCESS;
53,996✔
2027

2028
  // check request
2029
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
53,996✔
2030
  if (code) {
53,996✔
UNCOV
2031
    TAOS_RETURN(code);
×
2032
  }
2033

2034
  // fetch old entry
2035
  SMetaEntry *pEntry = NULL;
53,996✔
2036
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
53,996✔
2037
  if (code) {
53,996✔
UNCOV
2038
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2039
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
2040
    TAOS_RETURN(code);
×
2041
  }
2042

2043
  if (pEntry->version >= version) {
53,996✔
UNCOV
2044
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2045
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
2046
    metaFetchEntryFree(&pEntry);
×
2047
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2048
  }
2049

2050
  // fetch super entry
2051
  SMetaEntry *pSuper = NULL;
53,996✔
2052
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
53,996✔
2053
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
27,192✔
2054
    if (code) {
27,192✔
UNCOV
2055
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2056
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
UNCOV
2057
      metaFetchEntryFree(&pEntry);
×
2058
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2059
    }
2060
  }
2061

2062
  // search the column to update
2063
  SSchemaWrapper *pSchema =
53,996✔
2064
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
53,996✔
2065
  SColRef *pColRef = NULL;
53,996✔
2066
  int32_t  iColumn = 0;
53,996✔
2067
  for (int32_t i = 0; i < pSchema->nCols; i++) {
219,778✔
2068
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
219,778✔
2069
      pColRef = &pEntry->colRef.pColRef[i];
53,996✔
2070
      iColumn = i;
53,996✔
2071
      break;
53,996✔
2072
    }
2073
  }
2074

2075
  if (NULL == pColRef) {
53,996✔
UNCOV
2076
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2077
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
UNCOV
2078
    metaFetchEntryFree(&pEntry);
×
2079
    metaFetchEntryFree(&pSuper);
×
UNCOV
2080
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2081
  }
2082

2083
  // do update column name
2084
  pEntry->version = version;
53,996✔
2085
  pColRef->hasRef = false;
53,996✔
2086
  pColRef->id = pSchema->pSchema[iColumn].colId;
53,996✔
2087
  pSchema->version++;
53,996✔
2088
  pEntry->colRef.version++;
53,996✔
2089

2090
  // do handle entry
2091
  code = metaHandleEntry2(pMeta, pEntry);
53,996✔
2092
  if (code) {
53,996✔
UNCOV
2093
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2094
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
2095
    metaFetchEntryFree(&pEntry);
×
2096
    metaFetchEntryFree(&pSuper);
×
UNCOV
2097
    TAOS_RETURN(code);
×
2098
  } else {
2099
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
53,996✔
2100
             pEntry->uid, version);
2101
  }
2102

2103
  // build response
2104
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
53,996✔
2105
  if (code) {
53,996✔
UNCOV
2106
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2107
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2108
  } else {
2109
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
331,564✔
2110
      SColRef *p = &pEntry->colRef.pColRef[i];
277,568✔
2111
      pRsp->pColRefs[i].hasRef = p->hasRef;
277,568✔
2112
      pRsp->pColRefs[i].id = p->id;
277,568✔
2113
      if (p->hasRef) {
277,568✔
2114
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
137,534✔
2115
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
137,534✔
2116
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
137,534✔
2117
      }
2118
    }
2119
  }
2120

2121
  metaFetchEntryFree(&pEntry);
53,996✔
2122
  metaFetchEntryFree(&pSuper);
53,996✔
2123
  TAOS_RETURN(code);
53,996✔
2124
}
2125

2126
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,441✔
2127
  int32_t code = TSDB_CODE_SUCCESS;
4,441✔
2128

2129
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,441✔
UNCOV
2130
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2131
              __FILE__, __LINE__, version);
UNCOV
2132
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2133
  }
2134

2135
  SMetaEntry *pEntry = NULL;
4,441✔
2136
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
4,441✔
2137
  if (code) {
4,441✔
UNCOV
2138
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2139
              __FILE__, __LINE__, pReq->name, version);
UNCOV
2140
    TAOS_RETURN(code);
×
2141
  }
2142

2143
  if (pEntry->type != TSDB_SUPER_TABLE) {
4,441✔
UNCOV
2144
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2145
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
UNCOV
2146
    metaFetchEntryFree(&pEntry);
×
2147
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2148
  }
2149

2150
  if (pEntry->uid != pReq->suid) {
4,441✔
UNCOV
2151
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2152
              ", version:%" PRId64,
2153
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2154
    metaFetchEntryFree(&pEntry);
×
UNCOV
2155
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2156
  }
2157

2158
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2159
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2160
  //   PRId64,
2161
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2162
  //             pReq->schemaTag.version, version);
2163
  //   metaFetchEntryFree(&pEntry);
2164
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2165
  // }
2166

2167
  // do change the entry
2168
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
4,441✔
2169
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
4,441✔
2170
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4,441✔
UNCOV
2171
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2172
              __func__, __FILE__, __LINE__, pReq->name, version);
UNCOV
2173
    metaFetchEntryFree(&pEntry);
×
2174
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2175
  }
2176

2177
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
4,441✔
UNCOV
2178
    metaError(
×
2179
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2180
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2181
        version);
UNCOV
2182
    metaFetchEntryFree(&pEntry);
×
UNCOV
2183
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2184
  }
2185

2186
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2187
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2188
  //   PRId64,
2189
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2190
  //             pNewTagSchema->version, version);
2191
  //   metaFetchEntryFree(&pEntry);
2192
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2193
  // }
2194

2195
  int32_t numOfChangedTags = 0;
4,441✔
2196
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
22,686✔
2197
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
18,245✔
2198
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
18,245✔
2199

2200
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
18,245✔
2201
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
18,245✔
UNCOV
2202
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2203
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
UNCOV
2204
      metaFetchEntryFree(&pEntry);
×
2205
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2206
    }
2207

2208
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
18,245✔
2209
      numOfChangedTags++;
4,441✔
2210
      SSCHMEA_SET_IDX_ON(pOldColumn);
4,441✔
2211
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
13,804✔
UNCOV
2212
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2213
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
UNCOV
2214
      metaFetchEntryFree(&pEntry);
×
2215
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2216
    }
2217
  }
2218

2219
  if (numOfChangedTags != 1) {
4,441✔
UNCOV
2220
    metaError(
×
2221
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2222
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2223
    metaFetchEntryFree(&pEntry);
×
UNCOV
2224
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2225
  }
2226

2227
  pEntry->version = version;
4,441✔
2228
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,441✔
2229

2230
  // do handle the entry
2231
  code = metaHandleEntry2(pMeta, pEntry);
4,441✔
2232
  if (code) {
4,441✔
UNCOV
2233
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2234
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
UNCOV
2235
    metaFetchEntryFree(&pEntry);
×
2236
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2237
  } else {
2238
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,441✔
2239
             pEntry->uid, version);
2240
  }
2241

2242
  metaFetchEntryFree(&pEntry);
4,441✔
2243
  TAOS_RETURN(code);
4,441✔
2244
}
2245

2246
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,944✔
2247
  int32_t code = TSDB_CODE_SUCCESS;
2,944✔
2248

2249
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,944✔
UNCOV
2250
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2251
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
UNCOV
2252
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2253
  }
2254

2255
  SMetaEntry *pEntry = NULL;
2,944✔
2256
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,944✔
2257
  if (code) {
2,944✔
UNCOV
2258
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2259
              __FILE__, __LINE__, pReq->stb, version);
UNCOV
2260
    TAOS_RETURN(code);
×
2261
  }
2262

2263
  if (TSDB_SUPER_TABLE != pEntry->type) {
2,944✔
UNCOV
2264
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2265
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
UNCOV
2266
    metaFetchEntryFree(&pEntry);
×
2267
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2268
  }
2269

2270
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
2,944✔
2271
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,944✔
UNCOV
2272
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2273
              __func__, __FILE__, __LINE__, pReq->stb, version);
UNCOV
2274
    metaFetchEntryFree(&pEntry);
×
2275
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2276
  }
2277

2278
  // search and set the tag index off
2279
  int32_t numOfChangedTags = 0;
2,944✔
2280
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8,096✔
2281
    SSchema *pCol = pTagSchema->pSchema + i;
8,096✔
2282
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
8,096✔
2283
      if (!IS_IDX_ON(pCol)) {
2,944✔
UNCOV
2284
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2285
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
UNCOV
2286
        metaFetchEntryFree(&pEntry);
×
2287
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2288
      }
2289
      numOfChangedTags++;
2,944✔
2290
      SSCHMEA_SET_IDX_OFF(pCol);
2,944✔
2291
      break;
2,944✔
2292
    }
2293
  }
2294

2295
  if (numOfChangedTags != 1) {
2,944✔
UNCOV
2296
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2297
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
UNCOV
2298
    metaFetchEntryFree(&pEntry);
×
2299
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2300
  }
2301

2302
  // do handle the entry
2303
  pEntry->version = version;
2,944✔
2304
  pTagSchema->version++;
2,944✔
2305
  code = metaHandleEntry2(pMeta, pEntry);
2,944✔
2306
  if (code) {
2,944✔
UNCOV
2307
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2308
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
UNCOV
2309
    metaFetchEntryFree(&pEntry);
×
2310
    TAOS_RETURN(code);
×
2311
  } else {
2312
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
2,944✔
2313
             pEntry->uid, version);
2314
  }
2315

2316
  metaFetchEntryFree(&pEntry);
2,944✔
2317
  TAOS_RETURN(code);
2,944✔
2318
}
2319

2320
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,518,557✔
2321
  int32_t code = TSDB_CODE_SUCCESS;
7,518,557✔
2322

2323
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,518,557✔
2324
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
30,160✔
2325
              __FILE__, __LINE__, version);
2326
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
30,160✔
2327
  }
2328

2329
  SMetaEntry *pEntry = NULL;
7,493,347✔
2330
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,498,084✔
2331
  if (code) {
7,498,228✔
UNCOV
2332
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2333
              __FILE__, __LINE__, pReq->name, version);
UNCOV
2334
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2335
  }
2336

2337
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,498,228✔
UNCOV
2338
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2339
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
UNCOV
2340
    metaFetchEntryFree(&pEntry);
×
2341
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2342
  }
2343

2344
  SMetaEntry entry = {
22,458,195✔
2345
      .version = version,
2346
      .type = TSDB_SUPER_TABLE,
2347
      .uid = pReq->suid,
7,494,075✔
2348
      .name = pReq->name,
7,498,863✔
2349
      .stbEntry.schemaRow = pReq->schemaRow,
2350
      .stbEntry.schemaTag = pReq->schemaTag,
2351
      .stbEntry.keep = pReq->keep,
7,465,912✔
2352
      .stbEntry.ownerId = pReq->ownerId,
7,481,417✔
2353
      .colCmpr = pReq->colCmpr,
2354
      .pExtSchemas = pReq->pExtSchemas,
7,476,714✔
2355
  };
2356
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,473,993✔
2357
  if (pReq->virtualStb) {
7,473,993✔
2358
    TABLE_SET_VIRTUAL(entry.flags);
20,356✔
2359
  }
2360
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,482,336✔
2361
    TABLE_SET_ROLLUP(entry.flags);
4,428✔
2362
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,428✔
2363
  }
2364

2365
  // do handle the entry
2366
  code = metaHandleEntry2(pMeta, &entry);
7,504,545✔
2367
  if (code) {
7,500,706✔
UNCOV
2368
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2369
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
UNCOV
2370
    metaFetchEntryFree(&pEntry);
×
2371
    TAOS_RETURN(code);
×
2372
  } else {
2373
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,500,706✔
2374
             pReq->suid, version);
2375
  }
2376

2377
  metaFetchEntryFree(&pEntry);
7,534,674✔
2378
  TAOS_RETURN(code);
7,531,155✔
2379
}
2380

UNCOV
2381
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
UNCOV
2382
  int32_t code = 0;
×
2383

2384
  if (taosArrayGetSize(uidArray) == 0) {
×
2385
    return TSDB_CODE_SUCCESS;
×
2386
  }
2387

2388
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
UNCOV
2389
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
UNCOV
2390
    SMetaInfo info;
×
2391
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2392
    if (code) {
×
2393
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2394
                __func__, __FILE__, __LINE__, uid, code);
2395
      return code;
×
2396
    }
2397

2398
    SMetaEntry entry = {
×
2399
        .version = version,
2400
        .uid = uid,
2401
    };
2402

UNCOV
2403
    if (info.suid == 0) {
×
UNCOV
2404
      entry.type = -TSDB_NORMAL_TABLE;
×
UNCOV
2405
    } else if (info.suid == uid) {
×
2406
      entry.type = -TSDB_SUPER_TABLE;
×
2407
    } else {
2408
      entry.type = -TSDB_CHILD_TABLE;
×
2409
    }
UNCOV
2410
    code = metaHandleEntry2(pMeta, &entry);
×
2411
    if (code) {
×
UNCOV
2412
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2413
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2414
      return code;
×
2415
    }
2416
  }
2417
  return code;
×
2418
}
2419

2420
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
22,140✔
2421
  int32_t code = TSDB_CODE_SUCCESS;
22,140✔
2422

2423
  if (NULL == pReq->name || pReq->name[0] == 0) {
22,140✔
UNCOV
2424
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2425
              __LINE__, version);
UNCOV
2426
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2427
  }
2428

2429
  SMetaEntry *pEntry = NULL;
22,140✔
2430
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
22,140✔
2431
  if (code) {
22,140✔
UNCOV
2432
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2433
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2434
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2435
  }
2436

2437
  if (pEntry->type != TSDB_SUPER_TABLE) {
22,140✔
UNCOV
2438
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2439
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
UNCOV
2440
    metaFetchEntryFree(&pEntry);
×
2441
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2442
  }
2443

2444
  if (pEntry->uid != pReq->tbUid) {
21,402✔
UNCOV
2445
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2446
              ", version:%" PRId64,
2447
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2448
    metaFetchEntryFree(&pEntry);
×
UNCOV
2449
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2450
  }
2451

2452
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
20,664✔
2453
    // overwrite the old rsma definition if exists
UNCOV
2454
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
UNCOV
2455
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2456
  } else {
2457
    TABLE_SET_ROLLUP(pEntry->flags);
21,402✔
2458
  }
2459

2460
  SMetaEntry entry = *pEntry;
21,402✔
2461
  entry.version = version;
22,140✔
2462
  entry.stbEntry.rsmaParam.name = pReq->name;
22,140✔
2463
  entry.stbEntry.rsmaParam.uid = pReq->uid;
22,140✔
2464
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
22,140✔
2465
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
21,402✔
2466
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
22,140✔
2467
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
21,402✔
2468
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
20,664✔
2469
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
20,664✔
2470

2471
  // do handle the entry
2472
  code = metaHandleEntry2(pMeta, &entry);
20,664✔
2473
  if (code) {
22,140✔
UNCOV
2474
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2475
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
UNCOV
2476
    metaFetchEntryFree(&pEntry);
×
2477
    TAOS_RETURN(code);
×
2478
  } else {
2479
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
22,140✔
2480
    pMeta->pVnode->config.isRsma = 1;
22,140✔
2481
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
22,140✔
2482
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2483
  }
2484

2485
  metaFetchEntryFree(&pEntry);
22,140✔
2486
  TAOS_RETURN(code);
22,140✔
2487
}
2488

2489
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,428✔
2490
  int32_t code = TSDB_CODE_SUCCESS;
4,428✔
2491

2492
  if (NULL == pReq->name || pReq->name[0] == 0) {
4,428✔
UNCOV
2493
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2494
              __LINE__, version);
UNCOV
2495
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2496
  }
2497

2498
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
4,428✔
UNCOV
2499
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2500
              __LINE__, version);
UNCOV
2501
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2502
  }
2503

2504
  SMetaEntry *pEntry = NULL;
4,428✔
2505
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
4,428✔
2506
  if (code) {
4,428✔
UNCOV
2507
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2508
             __func__, __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2509
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2510
  }
2511

2512
  if (pEntry->type != pReq->tbType) {
4,428✔
UNCOV
2513
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
2514
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
UNCOV
2515
    metaFetchEntryFree(&pEntry);
×
2516
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2517
  }
2518

2519
  if (pEntry->uid != pReq->tbUid) {
4,428✔
UNCOV
2520
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2521
              ", version:%" PRId64,
2522
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2523
    metaFetchEntryFree(&pEntry);
×
UNCOV
2524
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2525
  }
2526

2527
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
4,428✔
2528
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
4,428✔
2529
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
4,428✔
UNCOV
2530
      metaError(
×
2531
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
2532
          "uid:%" PRIi64 ", version:%" PRId64,
2533
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
2534
          pReq->uid, version);
UNCOV
2535
      metaFetchEntryFree(&pEntry);
×
UNCOV
2536
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2537
    }
2538
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
4,428✔
2539
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
4,428✔
2540
  } else {
UNCOV
2541
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
2542
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2543
    metaFetchEntryFree(&pEntry);
×
2544
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2545
  }
2546

2547
  SMetaEntry entry = *pEntry;
4,428✔
2548
  entry.version = version;
4,428✔
2549
  TABLE_RESET_ROLLUP(entry.flags);
4,428✔
2550
  entry.stbEntry.rsmaParam.uid = 0;
4,428✔
2551
  entry.stbEntry.rsmaParam.name = NULL;
4,428✔
2552
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,428✔
2553
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,428✔
2554
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,428✔
2555

2556
  // do handle the entry
2557
  code = metaHandleEntry2(pMeta, &entry);
4,428✔
2558
  if (code) {
4,428✔
UNCOV
2559
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
2560
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
UNCOV
2561
    metaFetchEntryFree(&pEntry);
×
2562
    TAOS_RETURN(code);
×
2563
  } else {
2564
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
4,428✔
2565
      pMeta->pVnode->config.isRsma = 0;
×
2566
    }
2567
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
4,428✔
2568
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2569
  }
2570

2571
  metaFetchEntryFree(&pEntry);
4,428✔
2572
  TAOS_RETURN(code);
4,428✔
2573
}
2574

2575
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,808✔
2576
  int32_t code = TSDB_CODE_SUCCESS;
11,808✔
2577

2578
  if (NULL == pReq->name || pReq->name[0] == 0) {
11,808✔
UNCOV
2579
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2580
              __LINE__, version);
UNCOV
2581
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2582
  }
2583

2584
  SMetaEntry *pEntry = NULL;
11,808✔
2585
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
11,808✔
2586
  if (code) {
11,808✔
UNCOV
2587
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
2588
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2589
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2590
  }
2591

2592
  if (pEntry->uid != pReq->tbUid) {
11,808✔
UNCOV
2593
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2594
              ", version:%" PRId64,
2595
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
2596
    metaFetchEntryFree(&pEntry);
×
UNCOV
2597
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2598
  }
2599

2600
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
11,808✔
2601
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
11,808✔
2602
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,546✔
2603
  } else {
UNCOV
2604
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
2605
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2606
    metaFetchEntryFree(&pEntry);
×
2607
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
2608
  }
2609

2610
  SMetaEntry entry = *pEntry;
11,808✔
2611
  entry.version = version;
11,808✔
2612
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
11,808✔
2613
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
11,070✔
2614
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
11,808✔
2615
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
11,808✔
2616
  }
2617
  // do handle the entry
2618
  code = metaHandleEntry2(pMeta, &entry);
11,808✔
2619
  if (code) {
11,808✔
UNCOV
2620
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
2621
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
UNCOV
2622
    metaFetchEntryFree(&pEntry);
×
2623
    TAOS_RETURN(code);
×
2624
  } else {
2625
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
11,808✔
2626
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
2627
  }
2628

2629
  metaFetchEntryFree(&pEntry);
11,808✔
2630
  TAOS_RETURN(code);
11,808✔
2631
}
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