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

taosdata / TDengine / #4896

24 Dec 2025 07:36AM UTC coverage: 65.929% (+0.4%) from 65.513%
#4896

push

travis-ci

web-flow
enh: [TS-7591] Some code refactor and add more log. (#34022)

326 of 537 new or added lines in 4 files covered. (60.71%)

370 existing lines in 111 files now uncovered.

185828 of 281861 relevant lines covered (65.93%)

116309824.55 hits per line

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

67.69
/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) {
6,823,465✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
6,823,465✔
31
  void     *value = NULL;
6,833,053✔
32
  int32_t   valueSize = 0;
6,834,826✔
33
  SMetaInfo info;
6,834,047✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
6,834,191✔
UNCOV
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
×
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);
6,832,469✔
43
  if (r == 0) {  // name exists, check uid and type
6,824,175✔
44
    int64_t uid = *(tb_uid_t *)value;
3,156✔
45
    tdbFree(value);
3,156✔
46

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

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

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

71
  // check suid
72
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
6,821,019✔
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;
6,823,686✔
79
}
80

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

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

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

117
  return code;
995,579✔
118
}
119

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

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

132
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
1,024,080✔
133
  if (code) {
1,020,959✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
789✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
789✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
1,020,170✔
139
    tdbFreeClear(value);
1,022,029✔
140

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

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

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
6,824,298✔
169
  if (code != TSDB_CODE_SUCCESS) {
6,827,510✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
3,156✔
171
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
×
172
               pReq->name, pReq->suid, version);
173
      TAOS_RETURN(TSDB_CODE_SUCCESS);
×
174
    } else {
175
      TAOS_RETURN(code);
3,156✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
13,643,637✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
6,821,896✔
184
      .name = pReq->name,
6,820,627✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
6,818,431✔
188
  };
189
  if (pReq->rollup) {
6,818,477✔
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
6,823,727✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
6,824,788✔
195
    entry.colCmpr = pReq->colCmpr;
6,824,788✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
6,805,927✔
199

200
  if (pReq->virtualStb) {
6,819,094✔
201
    TABLE_SET_VIRTUAL(entry.flags);
60,053✔
202
  }
203

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

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
1,023,449✔
221
  if (code) {
1,022,131✔
222
    TAOS_RETURN(code);
1,578✔
223
  }
224

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

242
// Alter Super Table
243

244
// Create Child Table
245
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
53,001,005✔
246
  int32_t   code = TSDB_CODE_SUCCESS;
53,001,005✔
247
  void     *value = NULL;
53,001,005✔
248
  int32_t   valueSize = 0;
53,001,580✔
249
  SMetaInfo info;
52,998,676✔
250

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

258
  // check table existence
259
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
52,999,143✔
260
    pReq->uid = *(int64_t *)value;
714,033✔
261
    tdbFreeClear(value);
714,033✔
262

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

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

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

286
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
712,950✔
287
  }
288

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

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

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

314
  // Check tag value
315
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
52,278,637✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
52,286,123✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
52,285,195✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
236,942,936✔
319
      STagVal tagVal = {
184,986,676✔
320
          .cid = pTagSchema->pSchema[i].colId,
184,984,801✔
321
      };
322

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

334
  metaFetchEntryFree(&pStbEntry);
52,282,286✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
52,282,942✔
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
52,287,128✔
339
    if (TSDB_CODE_SUCCESS != code) {
52,283,446✔
340
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
341
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
342
    }
343
  }
344
  return code;
52,286,456✔
345
}
346

347
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
52,065,027✔
348
  int32_t code = TSDB_CODE_SUCCESS;
52,065,027✔
349

350
  if (NULL == ppRsp) {
52,065,027✔
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
52,065,027✔
355
  if (NULL == ppRsp) {
52,059,645✔
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
52,059,645✔
360
  (*ppRsp)->tuid = pEntry->uid;
52,066,129✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
52,066,943✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
52,070,340✔
363

364
  return code;
52,067,355✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
52,785,704✔
372
  if (code) {
52,782,302✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
714,033✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,083✔
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
714,033✔
378
  }
379

380
  SMetaEntry entry = {
52,068,269✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
52,067,115✔
384
      .name = pReq->name,
52,067,759✔
385
      .ctbEntry.btime = pReq->btime,
52,064,224✔
386
      .ctbEntry.ttlDays = pReq->ttl,
52,063,447✔
387
      .ctbEntry.commentLen = pReq->commentLen,
52,069,724✔
388
      .ctbEntry.comment = pReq->comment,
52,065,537✔
389
      .ctbEntry.suid = pReq->ctb.suid,
52,061,315✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
52,060,337✔
391
  };
392

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

400
  // handle entry
401
  code = metaHandleEntry2(pMeta, &entry);
52,065,136✔
402
  if (TSDB_CODE_SUCCESS == code) {
52,074,128✔
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
52,074,616✔
404
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
405
  } else {
406
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
407
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
408
              pReq->ctb.suid, version);
409
  }
410
  return code;
52,075,282✔
411
}
412

413
// Drop Child Table
414

415
// Alter Child Table
416

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

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

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

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

446
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
15,865,469✔
447
  int32_t code = TSDB_CODE_SUCCESS;
15,865,469✔
448

449
  if (NULL == ppRsp) {
15,865,469✔
450
    return code;
×
451
  }
452

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

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
91,594,006✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
75,728,537✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
75,728,537✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
75,728,537✔
468
    if (pEntry->pExtSchemas) {
75,728,537✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
800,315✔
470
    }
471
  }
472

473
  return code;
15,865,469✔
474
}
475

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

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

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

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

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

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

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

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

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

543
  return code;
118,148✔
544
}
545

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

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

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

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

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

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

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

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

610
  return code;
215,317✔
611
}
612

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

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

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

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

658
// Drop Normal Table
659

660
// Alter Normal Table
661

662
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
69,027,311✔
663
  int32_t code = TSDB_CODE_SUCCESS;
69,027,311✔
664
  if (TSDB_CHILD_TABLE == pReq->type) {
69,027,311✔
665
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
52,786,452✔
666
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
16,248,553✔
667
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
15,914,266✔
668
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
334,287✔
669
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
118,559✔
670
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
215,728✔
671
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
215,728✔
672
  } else {
673
    code = TSDB_CODE_INVALID_MSG;
×
674
  }
675
  TAOS_RETURN(code);
69,037,811✔
676
}
677

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

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

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

698
  SMetaEntry entry = {
995,579✔
699
      .version = version,
700
      .uid = pReq->uid,
995,579✔
701
  };
702

703
  if (pReq->isVirtual) {
995,579✔
704
    if (pReq->suid == 0) {
58,127✔
705
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
29,217✔
706
    } else {
707
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
28,910✔
708
    }
709
  } else {
710
    if (pReq->suid == 0) {
937,452✔
711
      entry.type = -TSDB_NORMAL_TABLE;
526,501✔
712
    } else {
713
      entry.type = -TSDB_CHILD_TABLE;
410,951✔
714
    }
715
  }
716
  code = metaHandleEntry2(pMeta, &entry);
995,579✔
717
  if (code) {
995,579✔
718
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
719
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
720
  } else {
721
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
995,579✔
722
             pReq->uid, version);
723
  }
724
  TAOS_RETURN(code);
995,579✔
725
}
726

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

997
  tColumn = *pColumn;
89,712✔
998

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

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

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

1056
  // build response
1057
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
89,712✔
1058
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
31,541✔
1059
    if (code) {
31,541✔
1060
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1061
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1062
    } else {
1063
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
27,138,531✔
1064
        SColRef *p = &pEntry->colRef.pColRef[i];
27,106,990✔
1065
        pRsp->pColRefs[i].hasRef = p->hasRef;
27,106,990✔
1066
        pRsp->pColRefs[i].id = p->id;
27,106,990✔
1067
        if (p->hasRef) {
27,106,990✔
1068
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
13,566,100✔
1069
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
13,566,100✔
1070
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
13,566,100✔
1071
        }
1072
      }
1073
    }
1074
  } else {
1075
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
58,171✔
1076
    if (code) {
58,171✔
1077
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1078
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1079
    } else {
1080
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
53,129,231✔
1081
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
53,071,060✔
1082
        pRsp->pSchemaExt[i].colId = p->id;
53,071,060✔
1083
        pRsp->pSchemaExt[i].compress = p->alg;
53,071,060✔
1084
      }
1085
    }
1086
  }
1087

1088
  metaFetchEntryFree(&pEntry);
89,712✔
1089
  TAOS_RETURN(code);
89,712✔
1090
}
1091

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

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

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

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

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

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

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

1142

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

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

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

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

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

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

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

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

1221
  // search the column to update
1222
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
498,865✔
1223
  SSchema        *pColumn = NULL;
498,865✔
1224
  int32_t         iColumn = 0;
498,865✔
1225
  int32_t         rowSize = 0;
498,865✔
1226
  for (int32_t i = 0; i < pSchema->nCols; i++) {
33,201,678✔
1227
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
32,702,813✔
1228
      pColumn = &pSchema->pSchema[i];
498,865✔
1229
      iColumn = i;
498,865✔
1230
    }
1231
    rowSize += pSchema->pSchema[i].bytes;
32,702,813✔
1232
  }
1233

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

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

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

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

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

1275
  // build response
1276
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
262,195✔
1277
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,338✔
1278
    if (code) {
28,338✔
1279
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1280
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1281
    } else {
1282
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
13,663,473✔
1283
        SColRef *p = &pEntry->colRef.pColRef[i];
13,635,135✔
1284
        pRsp->pColRefs[i].hasRef = p->hasRef;
13,635,135✔
1285
        pRsp->pColRefs[i].id = p->id;
13,635,135✔
1286
        if (p->hasRef) {
13,635,135✔
1287
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
83,190✔
1288
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
83,190✔
1289
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
83,190✔
1290
        }
1291
      }
1292
    }
1293
  } else {
1294
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
233,857✔
1295
    if (code) {
233,857✔
1296
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1297
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1298
    } else {
1299
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,602,895✔
1300
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,369,038✔
1301
        pRsp->pSchemaExt[i].colId = p->id;
9,369,038✔
1302
        pRsp->pSchemaExt[i].compress = p->alg;
9,369,038✔
1303
      }
1304
    }
1305
  }
1306

1307
  metaFetchEntryFree(&pEntry);
262,195✔
1308
  TAOS_RETURN(code);
262,195✔
1309
}
1310

1311
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,267,905✔
1312
  int32_t code = 0;
8,267,905✔
1313

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

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

1333
  TAOS_RETURN(code);
8,267,880✔
1334
}
1335

1336
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1337

1338
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
8,157,511✔
1339
  if (isNull) {
8,157,511✔
1340
    if (!tTagGet(pOldTag, &value)) {
50,730✔
1341
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
21,429✔
1342
      return true;
21,429✔
1343
    }
1344
    return false;
29,301✔
1345
  }
1346
  if (!tTagGet(pOldTag, &value)){
8,106,781✔
1347
    return false;
195,061✔
1348
  }
1349
  if (IS_VAR_DATA_TYPE(value.type)) {
7,911,720✔
1350
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
60,926✔
1351
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
39,445✔
1352
      return true;
39,445✔
1353
    }
1354
  } else {
1355
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,850,794✔
1356
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
96,107✔
1357
      return true;
96,107✔
1358
    }
1359
  }
1360
  return false;
7,776,168✔
1361
}
1362

1363
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,267,905✔
1364
  int32_t code = TSDB_CODE_SUCCESS;
8,267,905✔
1365

1366
  // check request
1367
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
8,267,905✔
1368
  if (code) {
8,267,905✔
1369
    TAOS_RETURN(code);
25✔
1370
  }
1371

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

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

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

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

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

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

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

1443
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,282,451✔
1444
      STagVal value = {
9,293,169✔
1445
          .type = pTagSchema->pSchema[i].type,
9,293,169✔
1446
          .cid = pTagSchema->pSchema[i].colId,
9,293,169✔
1447
      };
1448

1449
      if (iColumn == i) {
9,293,169✔
1450
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
8,136,421✔
1451
          taosArrayDestroy(pTagArray);
147,139✔
1452
          metaFetchEntryFree(&pChild);
147,139✔
1453
          metaFetchEntryFree(&pSuper);
147,139✔
1454
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
147,139✔
1455
        }
1456
        if (pReq->isNull) {
7,989,282✔
1457
          continue;
27,192✔
1458
        }
1459
        if (IS_VAR_DATA_TYPE(value.type)) {
7,962,090✔
1460
          value.pData = pReq->pTagVal;
108,196✔
1461
          value.nData = pReq->nTagVal;
108,196✔
1462
        } else {
1463
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,853,894✔
1464
        }
1465
      } else if (!tTagGet(pOldTag, &value)) {
1,156,748✔
1466
        continue;
309,755✔
1467
      }
1468

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

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

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

1507
  // free resource and return
1508
  metaFetchEntryFree(&pChild);
8,120,741✔
1509
  metaFetchEntryFree(&pSuper);
8,120,741✔
1510
  TAOS_RETURN(code);
8,120,741✔
1511
}
1512

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

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

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

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

1541
  TAOS_RETURN(code);
3,515✔
1542
}
1543

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

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

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

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

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

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

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

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

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

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

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

1640
  bool allSame = true;
3,515✔
1641

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

1648
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
24,605✔
1649
    if (pTagVal == NULL) {
24,605✔
1650
      if (!tTagGet(pOldTag, &value)) {
3,515✔
1651
        continue;
×
1652
      }
1653
    } else {
1654
      value.type = pCol->type;
21,090✔
1655
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
21,090✔
1656
        allSame = false;
11,248✔
1657
      }
1658
      if (pTagVal->isNull) {
21,090✔
1659
        continue;
4,218✔
1660
      }
1661

1662
      if (IS_VAR_DATA_TYPE(pCol->type)) {
16,872✔
1663
        value.pData = pTagVal->pTagVal;
2,812✔
1664
        value.nData = pTagVal->nTagVal;
2,812✔
1665
      } else {
1666
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
14,060✔
1667
      }
1668
    }
1669

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

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

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

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

1725
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
17,960✔
1726
  int32_t code = TSDB_CODE_SUCCESS;
17,960✔
1727

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

1734
  return code;
17,960✔
1735
}
1736

1737
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
17,960✔
1738
  int32_t code = 0;
17,960✔
1739

1740
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
17,960✔
1741
  if (code) {
17,960✔
1742
    TAOS_RETURN(code);
×
1743
  }
1744

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

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

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

1817
  metaFetchEntryFree(&pEntry);
17,960✔
1818
  TAOS_RETURN(code);
17,960✔
1819
}
1820

1821
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,786✔
1822
  int32_t code = TSDB_CODE_SUCCESS;
6,786✔
1823

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

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

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

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

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

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

1881
  pEntry->version = version;
6,786✔
1882

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

1895
  metaFetchEntryFree(&pEntry);
6,786✔
1896
  TAOS_RETURN(code);
6,786✔
1897
}
1898

1899
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
82,499✔
1900
  int32_t code = TSDB_CODE_SUCCESS;
82,499✔
1901

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

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

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

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

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

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

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

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

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

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

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

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

2016
  metaFetchEntryFree(&pEntry);
82,499✔
2017
  metaFetchEntryFree(&pSuper);
82,499✔
2018
  TAOS_RETURN(code);
82,499✔
2019
}
2020

2021
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
59,580✔
2022
  int32_t code = TSDB_CODE_SUCCESS;
59,580✔
2023

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

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

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

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

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

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

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

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

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

2117
  metaFetchEntryFree(&pEntry);
59,580✔
2118
  metaFetchEntryFree(&pSuper);
59,580✔
2119
  TAOS_RETURN(code);
59,580✔
2120
}
2121

2122
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,916✔
2123
  int32_t code = TSDB_CODE_SUCCESS;
5,916✔
2124

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

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

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

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

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

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

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

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

2191
  int32_t numOfChangedTags = 0;
5,916✔
2192
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
30,196✔
2193
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
24,280✔
2194
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
24,280✔
2195

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

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

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

2223
  pEntry->version = version;
5,916✔
2224
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
5,916✔
2225

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

2238
  metaFetchEntryFree(&pEntry);
5,916✔
2239
  TAOS_RETURN(code);
5,916✔
2240
}
2241

2242
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
3,948✔
2243
  int32_t code = TSDB_CODE_SUCCESS;
3,948✔
2244

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

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

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

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

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

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

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

2312
  metaFetchEntryFree(&pEntry);
3,948✔
2313
  TAOS_RETURN(code);
3,948✔
2314
}
2315

2316
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
8,590,472✔
2317
  int32_t code = TSDB_CODE_SUCCESS;
8,590,472✔
2318

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

2325
  SMetaEntry *pEntry = NULL;
8,579,589✔
2326
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
8,593,305✔
2327
  if (code) {
8,590,390✔
2328
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2329
              __FILE__, __LINE__, pReq->name, version);
2330
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2331
  }
2332

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

2340
  SMetaEntry entry = {
25,710,322✔
2341
      .version = version,
2342
      .type = TSDB_SUPER_TABLE,
2343
      .uid = pReq->suid,
8,585,534✔
2344
      .name = pReq->name,
8,586,794✔
2345
      .stbEntry.schemaRow = pReq->schemaRow,
2346
      .stbEntry.schemaTag = pReq->schemaTag,
2347
      .stbEntry.keep = pReq->keep,
8,588,308✔
2348
      .colCmpr = pReq->colCmpr,
2349
      .pExtSchemas = pReq->pExtSchemas,
8,565,786✔
2350
  };
2351
  TABLE_SET_COL_COMPRESSED(entry.flags);
8,562,573✔
2352
  if (pReq->virtualStb) {
8,562,573✔
2353
    TABLE_SET_VIRTUAL(entry.flags);
21,012✔
2354
  }
2355
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
8,584,861✔
2356
    TABLE_SET_ROLLUP(entry.flags);
4,734✔
2357
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,734✔
2358
  }
2359

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

2372
  metaFetchEntryFree(&pEntry);
8,589,276✔
2373
  TAOS_RETURN(code);
8,598,824✔
2374
}
2375

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

2379
  if (taosArrayGetSize(uidArray) == 0) {
×
2380
    return TSDB_CODE_SUCCESS;
×
2381
  }
2382

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

2393
    SMetaEntry entry = {
×
2394
        .version = version,
2395
        .uid = uid,
2396
    };
2397

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

2415
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
23,670✔
2416
  int32_t code = TSDB_CODE_SUCCESS;
23,670✔
2417

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

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

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

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

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

2455
  SMetaEntry entry = *pEntry;
23,670✔
2456
  entry.version = version;
23,670✔
2457
  entry.stbEntry.rsmaParam.name = pReq->name;
23,670✔
2458
  entry.stbEntry.rsmaParam.uid = pReq->uid;
23,670✔
2459
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
23,670✔
2460
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
23,670✔
2461
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
23,670✔
2462
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
23,670✔
2463
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
23,670✔
2464
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
23,670✔
2465

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

2480
  metaFetchEntryFree(&pEntry);
23,670✔
2481
  TAOS_RETURN(code);
23,670✔
2482
}
2483

2484
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,734✔
2485
  int32_t code = TSDB_CODE_SUCCESS;
4,734✔
2486

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

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

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

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

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

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

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

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

2566
  metaFetchEntryFree(&pEntry);
4,734✔
2567
  TAOS_RETURN(code);
4,734✔
2568
}
2569

2570
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,624✔
2571
  int32_t code = TSDB_CODE_SUCCESS;
12,624✔
2572

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

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

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

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

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

2624
  metaFetchEntryFree(&pEntry);
12,624✔
2625
  TAOS_RETURN(code);
12,624✔
2626
}
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