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

taosdata / TDengine / #4947

02 Feb 2026 09:27AM UTC coverage: 66.872% (-0.06%) from 66.932%
#4947

push

travis-ci

web-flow
enh: [6690002267] Optimize virtual table query with plenty of columns. (#34341)

527 of 634 new or added lines in 23 files covered. (83.12%)

3610 existing lines in 126 files now uncovered.

205539 of 307364 relevant lines covered (66.87%)

125933663.91 hits per line

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

67.6
/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,774,562✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,774,562✔
31
  void     *value = NULL;
4,796,610✔
32
  int32_t   valueSize = 0;
4,797,716✔
33
  SMetaInfo info;
4,795,450✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,795,442✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
15,526✔
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,780,109✔
43
  if (r == 0) {  // name exists, check uid and type
4,781,158✔
44
    int64_t uid = *(tb_uid_t *)value;
3,999✔
45
    tdbFree(value);
3,999✔
46

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

54
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
1,845✔
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) {
1,845✔
62
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
1,845✔
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,777,159✔
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,778,680✔
79
}
80

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

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

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

117
  return code;
1,357,115✔
118
}
119

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

126
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
874,504✔
UNCOV
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);
875,156✔
133
  if (code) {
873,978✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
718✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
718✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
873,260✔
139
    tdbFreeClear(value);
873,823✔
140

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

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

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
4,778,454✔
169
  if (code != TSDB_CODE_SUCCESS) {
4,787,549✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
3,999✔
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
1,845✔
172
               pReq->name, pReq->suid, version);
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
1,845✔
174
    } else {
175
      TAOS_RETURN(code);
2,154✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
9,546,809✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,780,979✔
184
      .name = pReq->name,
4,778,186✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,775,536✔
188
      .stbEntry.ownerId = pReq->ownerId,
4,762,258✔
189
  };
190
  if (pReq->rollup) {
4,782,974✔
191
    TABLE_SET_ROLLUP(entry.flags);
×
192
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
193
  }
194
  if (pReq->colCmpred) {
4,776,514✔
195
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,781,908✔
196
    entry.colCmpr = pReq->colCmpr;
4,781,908✔
197
  }
198

199
  entry.pExtSchemas = pReq->pExtSchemas;
4,763,568✔
200

201
  if (pReq->virtualStb) {
4,776,354✔
202
    TABLE_SET_VIRTUAL(entry.flags);
63,808✔
203
  }
204

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

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

220
  // check request
221
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
875,156✔
222
  if (code) {
873,994✔
223
    TAOS_RETURN(code);
1,436✔
224
  }
225

226
  // handle entry
227
  SMetaEntry entry = {
872,558✔
228
      .version = verison,
229
      .type = -TSDB_SUPER_TABLE,
230
      .uid = pReq->suid,
872,538✔
231
  };
232
  code = metaHandleEntry2(pMeta, &entry);
871,431✔
233
  if (code) {
874,283✔
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,
874,283✔
238
             pReq->suid, verison);
239
  }
240
  TAOS_RETURN(code);
874,311✔
241
}
242

243
// Alter Super Table
244

245
// Create Child Table
246
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
55,396,964✔
247
  int32_t   code = TSDB_CODE_SUCCESS;
55,396,964✔
248
  void     *value = NULL;
55,396,964✔
249
  int32_t   valueSize = 0;
55,397,871✔
250
  SMetaInfo info;
55,392,978✔
251

252
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
55,391,644✔
253
      pReq->ctb.suid == 0) {
55,396,007✔
254
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
82✔
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) {
55,394,949✔
261
    pReq->uid = *(int64_t *)value;
314,898✔
262
    tdbFreeClear(value);
314,898✔
263

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

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

290
  // check super table existence
291
  SMetaEntry *pStbEntry = NULL;
55,079,607✔
292
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
55,079,256✔
293
  if (code) {
55,071,988✔
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) {
55,071,988✔
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) {
55,062,908✔
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;
55,053,649✔
317
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
55,074,783✔
318
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
55,063,066✔
319
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
244,038,165✔
320
      STagVal tagVal = {
189,208,180✔
321
          .cid = pTagSchema->pSchema[i].colId,
189,195,464✔
322
      };
323

324
      if (tTagGet(pTag, &tagVal)) {
189,204,615✔
325
        if (pTagSchema->pSchema[i].type != tagVal.type) {
148,405,139✔
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);
55,066,676✔
336

337
  // check grant
338
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
55,058,182✔
339
    code = grantCheck(TSDB_GRANT_TIMESERIES);
55,070,260✔
340
    if (TSDB_CODE_SUCCESS != code) {
55,059,053✔
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;
55,062,265✔
346
}
347

348
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
54,863,367✔
349
  int32_t code = TSDB_CODE_SUCCESS;
54,863,367✔
350

351
  if (NULL == ppRsp) {
54,863,367✔
352
    return code;
×
353
  }
354

355
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
54,863,367✔
356
  if (NULL == ppRsp) {
54,845,826✔
357
    return terrno;
×
358
  }
359

360
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
54,845,826✔
361
  (*ppRsp)->tuid = pEntry->uid;
54,864,608✔
362
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
54,859,286✔
363
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
54,873,423✔
364

365
  return code;
54,879,593✔
366
}
367

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

371
  // check request
372
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
55,195,173✔
373
  if (code) {
55,176,890✔
374
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
314,898✔
375
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,728✔
376
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
377
    }
378
    return code;
314,898✔
379
  }
380

381
  SMetaEntry entry = {
54,861,992✔
382
      .version = version,
383
      .type = TSDB_CHILD_TABLE,
384
      .uid = pReq->uid,
54,859,896✔
385
      .name = pReq->name,
54,860,972✔
386
      .ctbEntry.btime = pReq->btime,
54,842,118✔
387
      .ctbEntry.ttlDays = pReq->ttl,
54,846,011✔
388
      .ctbEntry.commentLen = pReq->commentLen,
54,869,114✔
389
      .ctbEntry.comment = pReq->comment,
54,851,666✔
390
      .ctbEntry.suid = pReq->ctb.suid,
54,847,208✔
391
      .ctbEntry.pTags = pReq->ctb.pTag,
54,846,891✔
392
  };
393

394
  // build response
395
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
54,871,902✔
396
  if (code) {
54,876,290✔
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);
54,876,290✔
403
  if (TSDB_CODE_SUCCESS == code) {
54,881,126✔
404
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
54,883,229✔
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,
×
408
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
409
              pReq->ctb.suid, version);
410
  }
411
  return code;
54,887,038✔
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) {
6,730,557✔
420
  int32_t code = 0;
6,730,557✔
421
  void   *value = NULL;
6,730,557✔
422
  int32_t valueSize = 0;
6,730,557✔
423

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

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

447
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
6,570,378✔
448
  int32_t code = TSDB_CODE_SUCCESS;
6,570,378✔
449

450
  if (NULL == ppRsp) {
6,570,378✔
451
    return code;
×
452
  }
453

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

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

465
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
74,833,183✔
466
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
68,262,805✔
467
    (*ppRsp)->pSchemaExt[i].colId = p->id;
68,262,805✔
468
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
68,262,805✔
469
    if (pEntry->pExtSchemas) {
68,262,805✔
470
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
422,542✔
471
    }
472
  }
473

474
  return code;
6,570,378✔
475
}
476

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

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

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

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

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

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

529
  if (NULL == ppRsp) {
130,191✔
530
    return code;
×
531
  }
532

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

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

545
  return code;
130,191✔
546
}
547

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

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

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

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

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

597
  if (NULL == ppRsp) {
201,227✔
598
    return code;
×
599
  }
600

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

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

613
  return code;
201,227✔
614
}
615

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

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

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

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

661
// Drop Normal Table
662

663
// Alter Normal Table
664

665
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
62,128,071✔
666
  int32_t code = TSDB_CODE_SUCCESS;
62,128,071✔
667
  if (TSDB_CHILD_TABLE == pReq->type) {
62,128,071✔
668
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
55,196,204✔
669
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
6,931,784✔
670
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
6,600,366✔
671
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
331,418✔
672
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
130,191✔
673
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
201,227✔
674
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
201,227✔
675
  } else {
676
    code = TSDB_CODE_INVALID_MSG;
×
677
  }
678
  TAOS_RETURN(code);
62,133,415✔
679
}
680

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

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

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

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

706
  if (pReq->isVirtual) {
1,357,115✔
707
    if (pReq->suid == 0) {
53,019✔
708
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,747✔
709
    } else {
710
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,272✔
711
    }
712
  } else {
713
    if (pReq->suid == 0) {
1,304,096✔
714
      entry.type = -TSDB_NORMAL_TABLE;
752,702✔
715
    } else {
716
      entry.type = -TSDB_CHILD_TABLE;
551,394✔
717
    }
718
  }
719
  code = metaHandleEntry2(pMeta, &entry);
1,357,115✔
720
  if (code) {
1,356,932✔
721
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
722
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
723
  } else {
724
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,356,932✔
725
             pReq->uid, version);
726
  }
727
  TAOS_RETURN(code);
1,357,115✔
728
}
729

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1000
  tColumn = *pColumn;
75,711✔
1001

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

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

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

1060
  // build response
1061
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
75,711✔
1062
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,636✔
1063
    if (code) {
28,636✔
1064
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1065
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1066
    } else {
1067
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
33,604,516✔
1068
        SColRef *p = &pEntry->colRef.pColRef[i];
33,575,880✔
1069
        pRsp->pColRefs[i].hasRef = p->hasRef;
33,575,880✔
1070
        pRsp->pColRefs[i].id = p->id;
33,575,880✔
1071
        if (p->hasRef) {
33,575,880✔
1072
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
16,800,064✔
1073
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
16,800,064✔
1074
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
16,800,064✔
1075
        }
1076
      }
1077
    }
1078
  } else {
1079
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
47,075✔
1080
    if (code) {
47,075✔
1081
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1082
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1083
    } else {
1084
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
16,172,574✔
1085
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
16,125,499✔
1086
        pRsp->pSchemaExt[i].colId = p->id;
16,125,499✔
1087
        pRsp->pSchemaExt[i].compress = p->alg;
16,125,499✔
1088
      }
1089
    }
1090
  }
1091

1092
  metaFetchEntryFree(&pEntry);
75,711✔
1093
  TAOS_RETURN(code);
75,711✔
1094
}
1095

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

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

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

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

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

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

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

1146

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

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

1164
  // build response
1165
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
43,467✔
1166
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
26,680✔
1167
    if (code) {
26,680✔
1168
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1169
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1170
    } else {
1171
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
188,580✔
1172
        SColRef *p = &pEntry->colRef.pColRef[i];
161,900✔
1173
        pRsp->pColRefs[i].hasRef = p->hasRef;
161,900✔
1174
        pRsp->pColRefs[i].id = p->id;
161,900✔
1175
        if (p->hasRef) {
161,900✔
1176
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
99,880✔
1177
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
99,880✔
1178
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
99,880✔
1179
        }
1180
      }
1181
    }
1182
  } else {
1183
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
16,787✔
1184
    if (code) {
16,787✔
1185
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1186
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1187
    } else {
1188
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
176,907✔
1189
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
160,120✔
1190
        pRsp->pSchemaExt[i].colId = p->id;
160,120✔
1191
        pRsp->pSchemaExt[i].compress = p->alg;
160,120✔
1192
      }
1193
    }
1194
  }
1195

1196
  metaFetchEntryFree(&pEntry);
43,467✔
1197
  TAOS_RETURN(code);
43,467✔
1198
}
1199

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

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

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

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

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

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

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

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

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

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

1279
  // build response
1280
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
241,148✔
1281
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,930✔
1282
    if (code) {
25,930✔
1283
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1284
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1285
    } else {
1286
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
16,888,350✔
1287
        SColRef *p = &pEntry->colRef.pColRef[i];
16,862,420✔
1288
        pRsp->pColRefs[i].hasRef = p->hasRef;
16,862,420✔
1289
        pRsp->pColRefs[i].id = p->id;
16,862,420✔
1290
        if (p->hasRef) {
16,862,420✔
1291
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
75,640✔
1292
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
75,640✔
1293
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
75,640✔
1294
        }
1295
      }
1296
    }
1297
  } else {
1298
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
215,218✔
1299
    if (code) {
215,218✔
1300
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1301
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1302
    } else {
1303
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,653,716✔
1304
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,438,498✔
1305
        pRsp->pSchemaExt[i].colId = p->id;
8,438,498✔
1306
        pRsp->pSchemaExt[i].compress = p->alg;
8,438,498✔
1307
      }
1308
    }
1309
  }
1310

1311
  metaFetchEntryFree(&pEntry);
241,148✔
1312
  TAOS_RETURN(code);
241,148✔
1313
}
1314

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

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

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

1337
  TAOS_RETURN(code);
7,470,296✔
1338
}
1339

1340
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1341

1342
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,372,949✔
1343
  if (isNull) {
7,372,949✔
1344
    if (!tTagGet(pOldTag, &value)) {
45,665✔
1345
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,710✔
1346
      return true;
19,710✔
1347
    }
1348
    return false;
25,955✔
1349
  }
1350
  if (!tTagGet(pOldTag, &value)){
7,327,284✔
1351
    return false;
176,654✔
1352
  }
1353
  if (IS_VAR_DATA_TYPE(value.type)) {
7,150,630✔
1354
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
57,115✔
1355
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
36,309✔
1356
      return true;
36,309✔
1357
    }
1358
  } else {
1359
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,093,515✔
1360
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
88,672✔
1361
      return true;
88,672✔
1362
    }
1363
  }
1364
  return false;
7,025,649✔
1365
}
1366

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

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

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

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

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

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

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

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

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

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

1453
      if (iColumn == i) {
8,409,621✔
1454
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,353,689✔
1455
          taosArrayDestroy(pTagArray);
135,703✔
1456
          metaFetchEntryFree(&pChild);
135,703✔
1457
          metaFetchEntryFree(&pSuper);
135,703✔
1458
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
135,703✔
1459
        }
1460
        if (pReq->isNull) {
7,217,986✔
1461
          continue;
24,029✔
1462
        }
1463
        if (IS_VAR_DATA_TYPE(value.type)) {
7,193,957✔
1464
          value.pData = pReq->pTagVal;
99,431✔
1465
          value.nData = pReq->nTagVal;
99,431✔
1466
        } else {
1467
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,094,526✔
1468
        }
1469
      } else if (!tTagGet(pOldTag, &value)) {
1,055,932✔
1470
        continue;
287,987✔
1471
      }
1472

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1644
  bool allSame = true;
3,210✔
1645

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

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

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

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

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

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

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

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

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

1738
  return code;
24,118✔
1739
}
1740

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2195
  int32_t numOfChangedTags = 0;
4,378✔
2196
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
22,355✔
2197
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,977✔
2198
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,977✔
2199

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2344
  SMetaEntry entry = {
21,632,521✔
2345
      .version = version,
2346
      .type = TSDB_SUPER_TABLE,
2347
      .uid = pReq->suid,
7,222,270✔
2348
      .name = pReq->name,
7,227,129✔
2349
      .stbEntry.schemaRow = pReq->schemaRow,
2350
      .stbEntry.schemaTag = pReq->schemaTag,
2351
      .stbEntry.keep = pReq->keep,
7,225,529✔
2352
      .stbEntry.ownerId = pReq->ownerId,
7,208,867✔
2353
      .colCmpr = pReq->colCmpr,
2354
      .pExtSchemas = pReq->pExtSchemas,
7,211,127✔
2355
  };
2356
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,223,451✔
2357
  if (pReq->virtualStb) {
7,223,451✔
2358
    TABLE_SET_VIRTUAL(entry.flags);
20,032✔
2359
  }
2360
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,203,096✔
2361
    TABLE_SET_ROLLUP(entry.flags);
4,308✔
2362
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,308✔
2363
  }
2364

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

2377
  metaFetchEntryFree(&pEntry);
7,244,767✔
2378
  TAOS_RETURN(code);
7,245,400✔
2379
}
2380

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

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

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

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

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

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

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

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

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

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

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

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

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

2485
  metaFetchEntryFree(&pEntry);
21,540✔
2486
  TAOS_RETURN(code);
21,540✔
2487
}
2488

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2629
  metaFetchEntryFree(&pEntry);
11,488✔
2630
  TAOS_RETURN(code);
11,488✔
2631
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc