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

taosdata / TDengine / #4912

04 Jan 2026 09:05AM UTC coverage: 64.888% (-0.1%) from 65.028%
#4912

push

travis-ci

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

1206 of 4524 new or added lines in 22 files covered. (26.66%)

5351 existing lines in 123 files now uncovered.

194856 of 300296 relevant lines covered (64.89%)

118198896.2 hits per line

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

67.87
/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) {
4,475,837✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,475,837✔
31
  void     *value = NULL;
4,474,907✔
32
  int32_t   valueSize = 0;
4,476,262✔
33
  SMetaInfo info;
4,474,036✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,473,993✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
79✔
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);
4,472,050✔
43
  if (r == 0) {  // name exists, check uid and type
4,465,674✔
44
    int64_t uid = *(tb_uid_t *)value;
1,410✔
45
    tdbFree(value);
1,410✔
46

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

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

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

71
  // check suid
72
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
4,464,264✔
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;
4,470,575✔
79
}
80

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

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

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

117
  return code;
1,292,802✔
118
}
119

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

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

132
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
836,046✔
133
  if (code) {
832,896✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
705✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
705✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
832,191✔
139
    tdbFreeClear(value);
833,340✔
140

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

148
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
830,171✔
149
  if (code) {
832,568✔
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) {
832,568✔
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;
832,568✔
161
}
162

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
4,467,908✔
169
  if (code != TSDB_CODE_SUCCESS) {
4,471,477✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
1,410✔
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
×
172
               pReq->name, pReq->suid, version);
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
×
174
    } else {
175
      TAOS_RETURN(code);
1,410✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
8,931,790✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,469,800✔
184
      .name = pReq->name,
4,464,288✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,470,973✔
188
      .stbEntry.ownerId = pReq->ownerId,
4,455,643✔
189
  };
190
  if (pReq->rollup) {
4,462,645✔
191
    TABLE_SET_ROLLUP(entry.flags);
×
192
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
193
  }
194
  if (pReq->colCmpred) {
4,458,328✔
195
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,461,941✔
196
    entry.colCmpr = pReq->colCmpr;
4,461,941✔
197
  }
198

199
  entry.pExtSchemas = pReq->pExtSchemas;
4,451,203✔
200

201
  if (pReq->virtualStb) {
4,468,807✔
202
    TABLE_SET_VIRTUAL(entry.flags);
40,563✔
203
  }
204

205
  code = metaHandleEntry2(pMeta, &entry);
4,461,769✔
206
  if (TSDB_CODE_SUCCESS == code) {
4,476,756✔
207
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
4,476,756✔
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);
4,476,756✔
214
}
215

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

220
  // check request
221
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
835,505✔
222
  if (code) {
833,370✔
223
    TAOS_RETURN(code);
1,410✔
224
  }
225

226
  // handle entry
227
  SMetaEntry entry = {
831,960✔
228
      .version = verison,
229
      .type = -TSDB_SUPER_TABLE,
230
      .uid = pReq->suid,
832,469✔
231
  };
232
  code = metaHandleEntry2(pMeta, &entry);
833,176✔
233
  if (code) {
834,636✔
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,
834,636✔
238
             pReq->suid, verison);
239
  }
240
  TAOS_RETURN(code);
834,636✔
241
}
242

243
// Alter Super Table
244

245
// Create Child Table
246
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
49,354,541✔
247
  int32_t   code = TSDB_CODE_SUCCESS;
49,354,541✔
248
  void     *value = NULL;
49,354,541✔
249
  int32_t   valueSize = 0;
49,356,025✔
250
  SMetaInfo info;
49,336,143✔
251

252
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
49,340,015✔
253
      pReq->ctb.suid == 0) {
49,351,787✔
254
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
24,512✔
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) {
49,340,814✔
261
    pReq->uid = *(int64_t *)value;
305,147✔
262
    tdbFreeClear(value);
305,147✔
263

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

278
    // check suid
279
    if (info.suid != pReq->ctb.suid) {
303,456✔
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;
303,456✔
288
  }
289

290
  // check super table existence
291
  SMetaEntry *pStbEntry = NULL;
49,047,477✔
292
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
49,046,204✔
293
  if (code) {
49,038,138✔
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) {
49,038,138✔
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) {
49,040,653✔
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;
49,022,249✔
317
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
49,046,729✔
318
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
49,032,884✔
319
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
215,817,624✔
320
      STagVal tagVal = {
167,029,506✔
321
          .cid = pTagSchema->pSchema[i].colId,
167,020,003✔
322
      };
323

324
      if (tTagGet(pTag, &tagVal)) {
167,020,167✔
325
        if (pTagSchema->pSchema[i].type != tagVal.type) {
127,202,732✔
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);
49,053,167✔
336

337
  // check grant
338
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
49,017,080✔
339
    code = grantCheck(TSDB_GRANT_TIMESERIES);
49,042,779✔
340
    if (TSDB_CODE_SUCCESS != code) {
49,015,797✔
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;
49,020,535✔
346
}
347

348
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
48,884,919✔
349
  int32_t code = TSDB_CODE_SUCCESS;
48,884,919✔
350

351
  if (NULL == ppRsp) {
48,884,919✔
352
    return code;
×
353
  }
354

355
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
48,884,919✔
356
  if (NULL == ppRsp) {
48,873,162✔
357
    return terrno;
×
358
  }
359

360
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
48,873,162✔
361
  (*ppRsp)->tuid = pEntry->uid;
48,880,043✔
362
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
48,895,755✔
363
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
48,885,516✔
364

365
  return code;
48,893,958✔
366
}
367

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

371
  // check request
372
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
49,208,323✔
373
  if (code) {
49,177,006✔
374
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
305,147✔
375
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,691✔
376
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
377
    }
378
    return code;
304,954✔
379
  }
380

381
  SMetaEntry entry = {
48,871,859✔
382
      .version = version,
383
      .type = TSDB_CHILD_TABLE,
384
      .uid = pReq->uid,
48,879,403✔
385
      .name = pReq->name,
48,874,443✔
386
      .ctbEntry.btime = pReq->btime,
48,891,598✔
387
      .ctbEntry.ttlDays = pReq->ttl,
48,883,109✔
388
      .ctbEntry.commentLen = pReq->commentLen,
48,872,417✔
389
      .ctbEntry.comment = pReq->comment,
48,869,742✔
390
      .ctbEntry.suid = pReq->ctb.suid,
48,893,947✔
391
      .ctbEntry.pTags = pReq->ctb.pTag,
48,881,667✔
392
  };
393

394
  // build response
395
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
48,874,987✔
396
  if (code) {
48,894,527✔
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);
48,894,527✔
403
  if (TSDB_CODE_SUCCESS == code) {
48,903,667✔
404
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
48,902,989✔
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,
678✔
408
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
409
              pReq->ctb.suid, version);
410
  }
411
  return code;
48,910,496✔
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) {
14,456,482✔
420
  int32_t code = 0;
14,456,482✔
421
  void   *value = NULL;
14,456,482✔
422
  int32_t valueSize = 0;
14,456,482✔
423

424
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
14,456,482✔
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) {
14,456,482✔
432
    // for auto create table, we return the uid of the existing table
433
    pReq->uid = *(tb_uid_t *)value;
29,705✔
434
    tdbFree(value);
29,705✔
435
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
29,705✔
436
  }
437

438
  // grant check
439
  code = grantCheck(TSDB_GRANT_TIMESERIES);
14,426,777✔
440
  if (code) {
14,426,777✔
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;
14,426,777✔
445
}
446

447
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
14,332,467✔
448
  int32_t code = TSDB_CODE_SUCCESS;
14,332,467✔
449

450
  if (NULL == ppRsp) {
14,332,467✔
451
    return code;
×
452
  }
453

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

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

465
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
86,997,365✔
466
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
72,664,898✔
467
    (*ppRsp)->pSchemaExt[i].colId = p->id;
72,664,898✔
468
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
72,664,898✔
469
    if (pEntry->pExtSchemas) {
72,664,898✔
470
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
401,157✔
471
    }
472
  }
473

474
  return code;
14,332,467✔
475
}
476

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

480
  // check request
481
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
14,362,172✔
482
  if (code) {
14,362,172✔
483
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
29,705✔
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);
29,705✔
488
  }
489

490
  SMetaEntry entry = {
28,664,632✔
491
      .version = version,
492
      .type = TSDB_NORMAL_TABLE,
493
      .uid = pReq->uid,
14,332,467✔
494
      .name = pReq->name,
14,332,467✔
495
      .ntbEntry.btime = pReq->btime,
14,332,467✔
496
      .ntbEntry.ttlDays = pReq->ttl,
14,332,467✔
497
      .ntbEntry.commentLen = pReq->commentLen,
14,332,467✔
498
      .ntbEntry.comment = pReq->comment,
14,332,467✔
499
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
500
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
14,332,467✔
501
      .colCmpr = pReq->colCmpr,
502
      .pExtSchemas = pReq->pExtSchemas,
14,332,467✔
503
  };
504
  TABLE_SET_COL_COMPRESSED(entry.flags);
14,332,467✔
505

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

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

525
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
94,310✔
526
  int32_t code = TSDB_CODE_SUCCESS;
94,310✔
527

528
  if (NULL == ppRsp) {
94,310✔
529
    return code;
×
530
  }
531

532
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
94,310✔
533
  if (NULL == *ppRsp) {
94,310✔
534
    return terrno;
×
535
  }
536

537
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, *ppRsp,
94,310✔
538
                              TSDB_VIRTUAL_NORMAL_TABLE);
539
  if (code) {
94,310✔
540
    taosMemoryFreeClear(*ppRsp);
328✔
541
    return code;
328✔
542
  }
543

544
  return code;
93,982✔
545
}
546

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

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

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

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

592
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
145,070✔
593
  int32_t code = TSDB_CODE_SUCCESS;
145,070✔
594

595
  if (NULL == ppRsp) {
145,070✔
596
    return code;
×
597
  }
598

599
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
145,070✔
600
  if (NULL == *ppRsp) {
145,070✔
601
    return terrno;
×
602
  }
603

604
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
145,070✔
605
  if (code) {
145,070✔
606
    taosMemoryFreeClear(*ppRsp);
328✔
607
    return code;
328✔
608
  }
609
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
144,742✔
610

611
  return code;
144,742✔
612
}
613

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

625
  SMetaEntry entry = {.version = version,
290,140✔
626
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
627
                      .uid = pReq->uid,
145,070✔
628
                      .name = pReq->name,
145,070✔
629
                      .ctbEntry.btime = pReq->btime,
145,070✔
630
                      .ctbEntry.ttlDays = pReq->ttl,
145,070✔
631
                      .ctbEntry.commentLen = pReq->commentLen,
145,070✔
632
                      .ctbEntry.comment = pReq->comment,
145,070✔
633
                      .ctbEntry.suid = pReq->ctb.suid,
145,070✔
634
                      .ctbEntry.pTags = pReq->ctb.pTag,
145,070✔
635
                      .colRef = pReq->colRef};
636

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

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

659
// Drop Normal Table
660

661
// Alter Normal Table
662

663
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
63,807,284✔
664
  int32_t code = TSDB_CODE_SUCCESS;
63,807,284✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
63,807,284✔
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
49,209,012✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
14,601,552✔
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
14,362,172✔
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
239,380✔
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
94,310✔
671
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
145,070✔
672
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
145,070✔
673
  } else {
674
    code = TSDB_CODE_INVALID_MSG;
×
675
  }
676
  TAOS_RETURN(code);
63,726,397✔
677
}
678

679
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,292,802✔
680
  int32_t code = TSDB_CODE_SUCCESS;
1,292,802✔
681

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

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

699
  SMetaEntry entry = {
1,292,802✔
700
      .version = version,
701
      .uid = pReq->uid,
1,292,802✔
702
  };
703

704
  if (pReq->isVirtual) {
1,292,802✔
705
    if (pReq->suid == 0) {
51,204✔
706
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
25,876✔
707
    } else {
708
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
25,328✔
709
    }
710
  } else {
711
    if (pReq->suid == 0) {
1,241,598✔
712
      entry.type = -TSDB_NORMAL_TABLE;
714,523✔
713
    } else {
714
      entry.type = -TSDB_CHILD_TABLE;
527,075✔
715
    }
716
  }
717
  code = metaHandleEntry2(pMeta, &entry);
1,292,802✔
718
  if (code) {
1,292,802✔
719
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
720
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
721
  } else {
722
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,292,802✔
723
             pReq->uid, version);
724
  }
725
  TAOS_RETURN(code);
1,292,802✔
726
}
727

728
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,023,024✔
729
  int32_t code = 0;
4,023,024✔
730

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

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

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

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

777
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,330,457✔
778
  int32_t code = TSDB_CODE_SUCCESS;
3,330,457✔
779

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

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

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

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

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

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

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

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

943
  metaFetchEntryFree(&pEntry);
3,031,259✔
944
  TAOS_RETURN(code);
3,031,259✔
945
}
946

947
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
76,956✔
948
  int32_t code = TSDB_CODE_SUCCESS;
76,956✔
949

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

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

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

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

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

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

998
  tColumn = *pColumn;
76,956✔
999

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

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

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

1057
  // build response
1058
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
76,956✔
1059
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
27,384✔
1060
    if (code) {
27,384✔
1061
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1062
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1063
    } else {
1064
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
21,673,392✔
1065
        SColRef *p = &pEntry->colRef.pColRef[i];
21,646,008✔
1066
        pRsp->pColRefs[i].hasRef = p->hasRef;
21,646,008✔
1067
        pRsp->pColRefs[i].id = p->id;
21,646,008✔
1068
        if (p->hasRef) {
21,646,008✔
1069
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
10,833,992✔
1070
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
10,833,992✔
1071
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
10,833,992✔
1072
        }
1073
      }
1074
    }
1075
  } else {
1076
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
49,572✔
1077
    if (code) {
49,572✔
1078
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1079
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1080
    } else {
1081
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
13,155,748✔
1082
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
13,106,176✔
1083
        pRsp->pSchemaExt[i].colId = p->id;
13,106,176✔
1084
        pRsp->pSchemaExt[i].compress = p->alg;
13,106,176✔
1085
      }
1086
    }
1087
  }
1088

1089
  metaFetchEntryFree(&pEntry);
76,956✔
1090
  TAOS_RETURN(code);
76,956✔
1091
}
1092

1093
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
41,964✔
1094
  int32_t code = TSDB_CODE_SUCCESS;
41,964✔
1095

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

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

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

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

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

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

1143

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

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

1161
  // build response
1162
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
41,964✔
1163
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,800✔
1164
    if (code) {
25,800✔
1165
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1166
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1167
    } else {
1168
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
182,400✔
1169
        SColRef *p = &pEntry->colRef.pColRef[i];
156,600✔
1170
        pRsp->pColRefs[i].hasRef = p->hasRef;
156,600✔
1171
        pRsp->pColRefs[i].id = p->id;
156,600✔
1172
        if (p->hasRef) {
156,600✔
1173
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
96,600✔
1174
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
96,600✔
1175
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
96,600✔
1176
        }
1177
      }
1178
    }
1179
  } else {
1180
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,164✔
1181
    if (code) {
16,164✔
1182
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1183
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1184
    } else {
1185
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
171,163✔
1186
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
154,999✔
1187
        pRsp->pSchemaExt[i].colId = p->id;
154,999✔
1188
        pRsp->pSchemaExt[i].compress = p->alg;
154,999✔
1189
      }
1190
    }
1191
  }
1192

1193
  metaFetchEntryFree(&pEntry);
41,964✔
1194
  TAOS_RETURN(code);
41,964✔
1195
}
1196

1197
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
452,600✔
1198
  int32_t code = TSDB_CODE_SUCCESS;
452,600✔
1199

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

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

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

1222
  // search the column to update
1223
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
452,600✔
1224
  SSchema        *pColumn = NULL;
452,600✔
1225
  int32_t         iColumn = 0;
452,600✔
1226
  int32_t         rowSize = 0;
452,600✔
1227
  for (int32_t i = 0; i < pSchema->nCols; i++) {
28,511,928✔
1228
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
28,058,723✔
1229
      pColumn = &pSchema->pSchema[i];
452,600✔
1230
      iColumn = i;
452,600✔
1231
    }
1232
    rowSize += pSchema->pSchema[i].bytes;
28,059,328✔
1233
  }
1234

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

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

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

1259
  // do change the column bytes
1260
  pEntry->version = version;
234,800✔
1261
  pSchema->version++;
234,800✔
1262
  pColumn->bytes = pReq->colModBytes;
234,800✔
1263

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

1276
  // build response
1277
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
234,800✔
1278
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
24,928✔
1279
    if (code) {
24,928✔
1280
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1281
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1282
    } else {
1283
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
10,920,696✔
1284
        SColRef *p = &pEntry->colRef.pColRef[i];
10,895,768✔
1285
        pRsp->pColRefs[i].hasRef = p->hasRef;
10,895,768✔
1286
        pRsp->pColRefs[i].id = p->id;
10,895,768✔
1287
        if (p->hasRef) {
10,895,768✔
1288
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
73,200✔
1289
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
73,200✔
1290
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
73,200✔
1291
        }
1292
      }
1293
    }
1294
  } else {
1295
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
209,872✔
1296
    if (code) {
209,872✔
1297
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1298
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1299
    } else {
1300
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,443,632✔
1301
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,233,760✔
1302
        pRsp->pSchemaExt[i].colId = p->id;
8,233,760✔
1303
        pRsp->pSchemaExt[i].compress = p->alg;
8,233,760✔
1304
      }
1305
    }
1306
  }
1307

1308
  metaFetchEntryFree(&pEntry);
234,800✔
1309
  TAOS_RETURN(code);
234,800✔
1310
}
1311

1312
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,373,351✔
1313
  int32_t code = 0;
7,373,351✔
1314

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

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

1334
  TAOS_RETURN(code);
7,373,102✔
1335
}
1336

1337
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1338

1339
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,279,541✔
1340
  if (isNull) {
7,279,541✔
1341
    if (!tTagGet(pOldTag, &value)) {
44,647✔
1342
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,353✔
1343
      return true;
19,353✔
1344
    }
1345
    return false;
25,294✔
1346
  }
1347
  if (!tTagGet(pOldTag, &value)){
7,234,894✔
1348
    return false;
170,723✔
1349
  }
1350
  if (IS_VAR_DATA_TYPE(value.type)) {
7,064,171✔
1351
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
55,829✔
1352
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
35,721✔
1353
      return true;
35,721✔
1354
    }
1355
  } else {
1356
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,008,342✔
1357
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
86,460✔
1358
      return true;
86,460✔
1359
    }
1360
  }
1361
  return false;
6,941,990✔
1362
}
1363

1364
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,373,351✔
1365
  int32_t code = TSDB_CODE_SUCCESS;
7,373,351✔
1366

1367
  // check request
1368
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,373,351✔
1369
  if (code) {
7,373,351✔
1370
    TAOS_RETURN(code);
249✔
1371
  }
1372

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

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

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

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

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

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

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

1444
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,413,820✔
1445
      STagVal value = {
8,285,717✔
1446
          .type = pTagSchema->pSchema[i].type,
8,285,717✔
1447
          .cid = pTagSchema->pSchema[i].colId,
8,285,717✔
1448
      };
1449

1450
      if (iColumn == i) {
8,285,717✔
1451
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,260,971✔
1452
          taosArrayDestroy(pTagArray);
132,868✔
1453
          metaFetchEntryFree(&pChild);
132,868✔
1454
          metaFetchEntryFree(&pSuper);
132,868✔
1455
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
132,868✔
1456
        }
1457
        if (pReq->isNull) {
7,128,103✔
1458
          continue;
23,437✔
1459
        }
1460
        if (IS_VAR_DATA_TYPE(value.type)) {
7,104,666✔
1461
          value.pData = pReq->pTagVal;
96,010✔
1462
          value.nData = pReq->nTagVal;
96,010✔
1463
        } else {
1464
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,008,656✔
1465
        }
1466
      } else if (!tTagGet(pOldTag, &value)) {
1,024,746✔
1467
        continue;
279,936✔
1468
      }
1469

1470
      if (NULL == taosArrayPush(pTagArray, &value)) {
7,849,476✔
1471
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1472
                  __LINE__, tstrerror(terrno), version);
1473
        taosArrayDestroy(pTagArray);
×
1474
        metaFetchEntryFree(&pChild);
×
1475
        metaFetchEntryFree(&pSuper);
×
1476
        TAOS_RETURN(terrno);
×
1477
      }
1478
    }
1479

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

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

1508
  // free resource and return
1509
  metaFetchEntryFree(&pChild);
7,240,234✔
1510
  metaFetchEntryFree(&pSuper);
7,240,234✔
1511
  TAOS_RETURN(code);
7,240,234✔
1512
}
1513

1514
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,095✔
1515
  int32_t code = 0;
3,095✔
1516

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

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

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

1542
  TAOS_RETURN(code);
3,095✔
1543
}
1544

1545
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
3,095✔
1546
  int32_t code = TSDB_CODE_SUCCESS;
3,095✔
1547

1548
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
3,095✔
1549
  if (code) {
3,095✔
1550
    TAOS_RETURN(code);
×
1551
  }
1552

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

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

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

1579
  // search the tags to update
1580
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
3,095✔
1581

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

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

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

1613
  int32_t numOfChangedTags = 0;
3,095✔
1614
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
24,760✔
1615
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
21,665✔
1616
        ? numOfChangedTags++
18,570✔
1617
        : 0;
21,665✔
1618
  }
1619
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
3,095✔
1620
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1621
              __FILE__, __LINE__, version);
1622
    taosHashCleanup(pTagTable);
×
1623
    metaFetchEntryFree(&pChild);
×
1624
    metaFetchEntryFree(&pSuper);
×
1625
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1626
  }
1627

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

1641
  bool allSame = true;
3,095✔
1642

1643
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
24,760✔
1644
    SSchema *pCol = &pTagSchema->pSchema[i];
21,665✔
1645
    STagVal  value = {
21,665✔
1646
         .cid = pCol->colId,
21,665✔
1647
    };
1648

1649
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
21,665✔
1650
    if (pTagVal == NULL) {
21,665✔
1651
      if (!tTagGet(pOldTag, &value)) {
3,095✔
1652
        continue;
×
1653
      }
1654
    } else {
1655
      value.type = pCol->type;
18,570✔
1656
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
18,570✔
1657
        allSame = false;
9,904✔
1658
      }
1659
      if (pTagVal->isNull) {
18,570✔
1660
        continue;
3,714✔
1661
      }
1662

1663
      if (IS_VAR_DATA_TYPE(pCol->type)) {
14,856✔
1664
        value.pData = pTagVal->pTagVal;
2,476✔
1665
        value.nData = pTagVal->nTagVal;
2,476✔
1666
      } else {
1667
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
12,380✔
1668
      }
1669
    }
1670

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

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

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

1720
  taosHashCleanup(pTagTable);
2,476✔
1721
  metaFetchEntryFree(&pChild);
2,476✔
1722
  metaFetchEntryFree(&pSuper);
2,476✔
1723
  TAOS_RETURN(code);
2,476✔
1724
}
1725

1726
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,468✔
1727
  int32_t code = TSDB_CODE_SUCCESS;
23,468✔
1728

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

1735
  return code;
23,468✔
1736
}
1737

1738
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,468✔
1739
  int32_t code = 0;
23,468✔
1740

1741
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
23,468✔
1742
  if (code) {
23,468✔
1743
    TAOS_RETURN(code);
×
1744
  }
1745

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

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

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

1818
  metaFetchEntryFree(&pEntry);
23,468✔
1819
  TAOS_RETURN(code);
23,468✔
1820
}
1821

1822
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5,777✔
1823
  int32_t code = TSDB_CODE_SUCCESS;
5,777✔
1824

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

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

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

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

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

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

1882
  pEntry->version = version;
5,777✔
1883

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

1896
  metaFetchEntryFree(&pEntry);
5,777✔
1897
  TAOS_RETURN(code);
5,777✔
1898
}
1899

1900
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
69,639✔
1901
  int32_t code = TSDB_CODE_SUCCESS;
69,639✔
1902

1903
  // check request
1904
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
69,639✔
1905
  if (code) {
69,639✔
1906
    TAOS_RETURN(code);
×
1907
  }
1908

1909
  if (NULL == pReq->refDbName) {
69,639✔
1910
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1911
              __func__, __FILE__, __LINE__, version);
1912
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1913
  }
1914

1915
  if (NULL == pReq->refTbName) {
69,639✔
1916
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1917
              __func__, __FILE__, __LINE__, version);
1918
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1919
  }
1920

1921
  if (NULL == pReq->refColName) {
69,639✔
1922
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1923
              __func__, __FILE__, __LINE__, version);
1924
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1925
  }
1926

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

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

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

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

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

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

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

1999
  // build response
2000
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
69,639✔
2001
  if (code) {
69,639✔
2002
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2003
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2004
  } else {
2005
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
521,487✔
2006
      SColRef *p = &pEntry->colRef.pColRef[i];
451,848✔
2007
      pRsp->pColRefs[i].hasRef = p->hasRef;
451,848✔
2008
      pRsp->pColRefs[i].id = p->id;
451,848✔
2009
      if (p->hasRef) {
451,848✔
2010
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
311,928✔
2011
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
311,928✔
2012
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
311,928✔
2013
      }
2014
    }
2015
  }
2016

2017
  metaFetchEntryFree(&pEntry);
69,639✔
2018
  metaFetchEntryFree(&pSuper);
69,639✔
2019
  TAOS_RETURN(code);
69,639✔
2020
}
2021

2022
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
51,408✔
2023
  int32_t code = TSDB_CODE_SUCCESS;
51,408✔
2024

2025
  // check request
2026
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
51,408✔
2027
  if (code) {
51,408✔
2028
    TAOS_RETURN(code);
×
2029
  }
2030

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

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

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

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

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

2080
  // do update column name
2081
  pEntry->version = version;
51,408✔
2082
  pColRef->hasRef = false;
51,408✔
2083
  pColRef->id = pSchema->pSchema[iColumn].colId;
51,408✔
2084
  pSchema->version++;
51,408✔
2085
  pEntry->colRef.version++;
51,408✔
2086

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

2100
  // build response
2101
  code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
51,408✔
2102
  if (code) {
51,408✔
2103
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2104
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2105
  } else {
2106
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
315,960✔
2107
      SColRef *p = &pEntry->colRef.pColRef[i];
264,552✔
2108
      pRsp->pColRefs[i].hasRef = p->hasRef;
264,552✔
2109
      pRsp->pColRefs[i].id = p->id;
264,552✔
2110
      if (p->hasRef) {
264,552✔
2111
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
131,172✔
2112
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
131,172✔
2113
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
131,172✔
2114
      }
2115
    }
2116
  }
2117

2118
  metaFetchEntryFree(&pEntry);
51,408✔
2119
  metaFetchEntryFree(&pSuper);
51,408✔
2120
  TAOS_RETURN(code);
51,408✔
2121
}
2122

2123
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,133✔
2124
  int32_t code = TSDB_CODE_SUCCESS;
4,133✔
2125

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

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

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

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

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

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

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

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

2192
  int32_t numOfChangedTags = 0;
4,133✔
2193
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
21,082✔
2194
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
16,949✔
2195
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
16,949✔
2196

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

2205
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
16,949✔
2206
      numOfChangedTags++;
4,133✔
2207
      SSCHMEA_SET_IDX_ON(pOldColumn);
4,133✔
2208
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
12,816✔
2209
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2210
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2211
      metaFetchEntryFree(&pEntry);
×
2212
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2213
    }
2214
  }
2215

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

2224
  pEntry->version = version;
4,133✔
2225
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,133✔
2226

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

2239
  metaFetchEntryFree(&pEntry);
4,133✔
2240
  TAOS_RETURN(code);
4,133✔
2241
}
2242

2243
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,726✔
2244
  int32_t code = TSDB_CODE_SUCCESS;
2,726✔
2245

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

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

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

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

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

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

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

2313
  metaFetchEntryFree(&pEntry);
2,726✔
2314
  TAOS_RETURN(code);
2,726✔
2315
}
2316

2317
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,161,785✔
2318
  int32_t code = TSDB_CODE_SUCCESS;
7,161,785✔
2319

2320
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,161,785✔
2321
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
15,731✔
2322
              __FILE__, __LINE__, version);
2323
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
15,731✔
2324
  }
2325

2326
  SMetaEntry *pEntry = NULL;
7,156,576✔
2327
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,162,434✔
2328
  if (code) {
7,161,546✔
2329
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2330
              __FILE__, __LINE__, pReq->name, version);
2331
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2332
  }
2333

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

2341
  SMetaEntry entry = {
21,454,982✔
2342
      .version = version,
2343
      .type = TSDB_SUPER_TABLE,
2344
      .uid = pReq->suid,
7,156,558✔
2345
      .name = pReq->name,
7,159,605✔
2346
      .stbEntry.schemaRow = pReq->schemaRow,
2347
      .stbEntry.schemaTag = pReq->schemaTag,
2348
      .stbEntry.keep = pReq->keep,
7,144,158✔
2349
      .stbEntry.ownerId = pReq->ownerId,
7,143,163✔
2350
      .colCmpr = pReq->colCmpr,
2351
      .pExtSchemas = pReq->pExtSchemas,
7,151,519✔
2352
  };
2353
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,146,142✔
2354
  if (pReq->virtualStb) {
7,146,142✔
2355
    TABLE_SET_VIRTUAL(entry.flags);
18,080✔
2356
  }
2357
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,130,950✔
2358
    TABLE_SET_ROLLUP(entry.flags);
4,242✔
2359
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,242✔
2360
  }
2361

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

2374
  metaFetchEntryFree(&pEntry);
7,179,519✔
2375
  TAOS_RETURN(code);
7,175,979✔
2376
}
2377

2378
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2379
  int32_t code = 0;
×
2380

2381
  if (taosArrayGetSize(uidArray) == 0) {
×
2382
    return TSDB_CODE_SUCCESS;
×
2383
  }
2384

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

2395
    SMetaEntry entry = {
×
2396
        .version = version,
2397
        .uid = uid,
2398
    };
2399

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

2417
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
21,210✔
2418
  int32_t code = TSDB_CODE_SUCCESS;
21,210✔
2419

2420
  if (NULL == pReq->name || pReq->name[0] == 0) {
21,210✔
2421
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2422
              __LINE__, version);
2423
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2424
  }
2425

2426
  SMetaEntry *pEntry = NULL;
21,210✔
2427
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
21,210✔
2428
  if (code) {
20,503✔
2429
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2430
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
2431
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2432
  }
2433

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

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

2449
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
21,210✔
2450
    // overwrite the old rsma definition if exists
2451
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
2452
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2453
  } else {
2454
    TABLE_SET_ROLLUP(pEntry->flags);
21,210✔
2455
  }
2456

2457
  SMetaEntry entry = *pEntry;
21,210✔
2458
  entry.version = version;
21,210✔
2459
  entry.stbEntry.rsmaParam.name = pReq->name;
21,210✔
2460
  entry.stbEntry.rsmaParam.uid = pReq->uid;
21,210✔
2461
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
21,210✔
2462
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
21,210✔
2463
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
20,503✔
2464
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
19,796✔
2465
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
20,503✔
2466
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
21,210✔
2467

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

2482
  metaFetchEntryFree(&pEntry);
21,210✔
2483
  TAOS_RETURN(code);
21,210✔
2484
}
2485

2486
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,242✔
2487
  int32_t code = TSDB_CODE_SUCCESS;
4,242✔
2488

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

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

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

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

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

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

2544
  SMetaEntry entry = *pEntry;
4,242✔
2545
  entry.version = version;
4,242✔
2546
  TABLE_RESET_ROLLUP(entry.flags);
4,242✔
2547
  entry.stbEntry.rsmaParam.uid = 0;
4,242✔
2548
  entry.stbEntry.rsmaParam.name = NULL;
4,242✔
2549
  entry.stbEntry.rsmaParam.nFuncs = 0;
4,242✔
2550
  entry.stbEntry.rsmaParam.funcColIds = NULL;
4,242✔
2551
  entry.stbEntry.rsmaParam.funcIds = NULL;
4,242✔
2552

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

2568
  metaFetchEntryFree(&pEntry);
4,242✔
2569
  TAOS_RETURN(code);
4,242✔
2570
}
2571

2572
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,312✔
2573
  int32_t code = TSDB_CODE_SUCCESS;
11,312✔
2574

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

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

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

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

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

2626
  metaFetchEntryFree(&pEntry);
11,312✔
2627
  TAOS_RETURN(code);
11,312✔
2628
}
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