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

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

25.08
/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(tb_uid_t uid, 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) {
640✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
640✔
31
  void     *value = NULL;
640✔
32
  int32_t   valueSize = 0;
640✔
33
  SMetaInfo info;
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
640!
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);
640✔
43
  if (r == 0) {  // name exists, check uid and type
638!
44
    int64_t uid = *(tb_uid_t *)value;
×
45
    tdbFree(value);
×
46

47
    if (pReq->suid != uid) {
×
48
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
×
49
                " version:%" PRId64,
50
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
51
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
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) {
638!
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;
640✔
79
}
80

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

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

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

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

117
  return code;
1,222✔
118
}
119

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

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

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

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

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
638✔
169
  if (code != TSDB_CODE_SUCCESS) {
640!
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
×
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);
×
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
640✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
640✔
184
      .name = pReq->name,
640✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
640✔
188
  };
189
  if (pReq->rollup) {
640!
190
    TABLE_SET_ROLLUP(entry.flags);
×
191
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
192
  }
193
  if (pReq->colCmpred) {
640✔
194
    TABLE_SET_COL_COMPRESSED(entry.flags);
596✔
195
    entry.colCmpr = pReq->colCmpr;
596✔
196
  }
197

198
  entry.pExtSchemas = pReq->pExtSchemas;
640✔
199

200
  if (pReq->virtualStb) {
640!
201
    TABLE_SET_VIRTUAL(entry.flags);
×
202
  }
203

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

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
20✔
221
  if (code) {
20!
222
    TAOS_RETURN(code);
×
223
  }
224

225
  // handle entry
226
  SMetaEntry entry = {
20✔
227
      .version = verison,
228
      .type = -TSDB_SUPER_TABLE,
229
      .uid = pReq->suid,
20✔
230
  };
231
  code = metaHandleEntry2(pMeta, &entry);
20✔
232
  if (code) {
20!
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,
20!
237
             pReq->suid, verison);
238
  }
239
  TAOS_RETURN(code);
20✔
240
}
241

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
6,784!
252
      pReq->ctb.suid == 0) {
6,782!
253
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
2!
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) {
6,782!
260
    pReq->uid = *(int64_t *)value;
×
261
    tdbFreeClear(value);
×
262

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

277
    // check suid
278
    if (info.suid != pReq->ctb.suid) {
×
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;
×
287
  }
288

289
  // check super table existence
290
  if (tdbTbGet(pMeta->pNameIdx, pReq->ctb.stbName, strlen(pReq->ctb.stbName) + 1, &value, &valueSize) == 0) {
6,784!
291
    int64_t suid = *(int64_t *)value;
6,784✔
292
    tdbFreeClear(value);
6,784✔
293
    if (suid != pReq->ctb.suid) {
6,784!
294
      metaError("vgId:%d, %s failed at %s:%d since super table %s has uid %" PRId64 " instead of %" PRId64
×
295
                ", version:%" PRId64,
296
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, suid, pReq->ctb.suid, version);
297
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
298
    }
299
  } else {
300
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not eixst, version:%" PRId64,
×
301
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
302
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
303
  }
304

305
  // check super table is a super table
306
  if (metaGetInfo(pMeta, pReq->ctb.suid, &info, NULL) != TSDB_CODE_SUCCESS) {
6,784!
307
    metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
308
              ", which is an internal error, version:%" PRId64,
309
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.suid, version);
310
    return TSDB_CODE_INTERNAL_ERROR;
×
311
  } else if (info.suid != info.uid) {
6,782!
312
    metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
×
313
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.suid, version);
314
    return TSDB_CODE_INVALID_MSG;
×
315
  }
316

317
  // check grant
318
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
6,782!
319
    code = grantCheck(TSDB_GRANT_TIMESERIES);
6,784✔
320
    if (TSDB_CODE_SUCCESS != code) {
6,782!
321
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
322
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
323
    }
324
  }
325
  return code;
6,784✔
326
}
327

328
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
6,782✔
329
  int32_t code = TSDB_CODE_SUCCESS;
6,782✔
330

331
  if (NULL == ppRsp) {
6,782!
332
    return code;
×
333
  }
334

335
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
6,782!
336
  if (NULL == ppRsp) {
6,780!
337
    return terrno;
×
338
  }
339

340
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
6,780✔
341
  (*ppRsp)->tuid = pEntry->uid;
6,780✔
342
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
6,780✔
343
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
6,780✔
344

345
  return code;
6,780✔
346
}
347

348
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
6,784✔
349
  int32_t code = TSDB_CODE_SUCCESS;
6,784✔
350

351
  // check request
352
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
6,784✔
353
  if (code) {
6,782!
354
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
355
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
356
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
357
    }
358
    return code;
×
359
  }
360

361
  SMetaEntry entry = {
6,782✔
362
      .version = version,
363
      .type = TSDB_CHILD_TABLE,
364
      .uid = pReq->uid,
6,782✔
365
      .name = pReq->name,
6,782✔
366
      .ctbEntry.btime = pReq->btime,
6,782✔
367
      .ctbEntry.ttlDays = pReq->ttl,
6,782✔
368
      .ctbEntry.commentLen = pReq->commentLen,
6,782✔
369
      .ctbEntry.comment = pReq->comment,
6,782✔
370
      .ctbEntry.suid = pReq->ctb.suid,
6,782✔
371
      .ctbEntry.pTags = pReq->ctb.pTag,
6,782✔
372
  };
373

374
  // build response
375
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
6,782✔
376
  if (code) {
6,780!
377
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
378
              tstrerror(code));
379
  }
380

381
  // handle entry
382
  code = metaHandleEntry2(pMeta, &entry);
6,780✔
383
  if (TSDB_CODE_SUCCESS == code) {
6,778!
384
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
6,778!
385
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
386
  } else {
387
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
388
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
389
              pReq->ctb.suid, version);
390
  }
391
  return code;
6,784✔
392
}
393

394
// Drop Child Table
395

396
// Alter Child Table
397

398
// Create Normal Table
399
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
94✔
400
  int32_t code = 0;
94✔
401
  void   *value = NULL;
94✔
402
  int32_t valueSize = 0;
94✔
403

404
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
94!
405
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
406
              __FILE__, __LINE__, pReq->name, version);
407
    return TSDB_CODE_INVALID_MSG;
×
408
  }
409

410
  // check name
411
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
94!
412
    // for auto create table, we return the uid of the existing table
413
    pReq->uid = *(tb_uid_t *)value;
×
414
    tdbFree(value);
×
415
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
416
  }
417

418
  // grant check
419
  code = grantCheck(TSDB_GRANT_TIMESERIES);
94✔
420
  if (code) {
94!
421
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
422
              __FILE__, __LINE__, tstrerror(code), version, pReq->name);
423
  }
424
  return code;
94✔
425
}
426

427
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
94✔
428
  int32_t code = TSDB_CODE_SUCCESS;
94✔
429

430
  if (NULL == ppRsp) {
94!
431
    return code;
×
432
  }
433

434
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
94!
435
  if (NULL == *ppRsp) {
94!
436
    return terrno;
×
437
  }
438

439
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, *ppRsp);
94✔
440
  if (code) {
94!
441
    taosMemoryFreeClear(*ppRsp);
×
442
    return code;
×
443
  }
444

445
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
526✔
446
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
432✔
447
    (*ppRsp)->pSchemaExt[i].colId = p->id;
432✔
448
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
432✔
449
    if (pEntry->pExtSchemas) {
432!
450
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
×
451
    }
452
  }
453

454
  return code;
94✔
455
}
456

457
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
94✔
458
  int32_t code = TSDB_CODE_SUCCESS;
94✔
459

460
  // check request
461
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
94✔
462
  if (code) {
94!
463
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
464
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
465
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
466
    }
467
    TAOS_RETURN(code);
×
468
  }
469

470
  SMetaEntry entry = {
94✔
471
    .version = version,
472
    .type = TSDB_NORMAL_TABLE,
473
    .uid = pReq->uid,
94✔
474
    .name = pReq->name,
94✔
475
    .ntbEntry.btime = pReq->btime,
94✔
476
    .ntbEntry.ttlDays = pReq->ttl,
94✔
477
    .ntbEntry.commentLen = pReq->commentLen,
94✔
478
    .ntbEntry.comment = pReq->comment,
94✔
479
    .ntbEntry.schemaRow = pReq->ntb.schemaRow,
480
    .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
94✔
481
    .colCmpr = pReq->colCmpr,
482
    .pExtSchemas = pReq->pExtSchemas,
94✔
483
  };
484
  TABLE_SET_COL_COMPRESSED(entry.flags);
94✔
485

486
  // build response
487
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
94✔
488
  if (code) {
94!
489
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
490
              tstrerror(code));
491
  }
492

493
  // handle entry
494
  code = metaHandleEntry2(pMeta, &entry);
94✔
495
  if (TSDB_CODE_SUCCESS == code) {
94!
496
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
94!
497
             pReq->name, pReq->uid);
498
  } else {
499
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
500
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
501
  }
502
  TAOS_RETURN(code);
94✔
503
}
504

505
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
×
506
  int32_t code = TSDB_CODE_SUCCESS;
×
507

508
  if (NULL == ppRsp) {
×
509
    return code;
×
510
  }
511

512
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
513
  if (NULL == *ppRsp) {
×
514
    return terrno;
×
515
  }
516

517
  code = metaUpdateVtbMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_NORMAL_TABLE);
×
518
  if (code) {
×
519
    taosMemoryFreeClear(*ppRsp);
×
520
    return code;
×
521
  }
522

523
  return code;
×
524
}
525

526
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
×
527
  // check request
528
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
×
529
  if (code) {
×
530
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
531
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
532
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
533
    }
534
    TAOS_RETURN(code);
×
535
  }
536

537
  SMetaEntry entry = {
×
538
      .version = version,
539
      .type = TSDB_VIRTUAL_NORMAL_TABLE,
540
      .uid = pReq->uid,
×
541
      .name = pReq->name,
×
542
      .ntbEntry.btime = pReq->btime,
×
543
      .ntbEntry.ttlDays = pReq->ttl,
×
544
      .ntbEntry.commentLen = pReq->commentLen,
×
545
      .ntbEntry.comment = pReq->comment,
×
546
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
547
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
×
548
      .colRef = pReq->colRef
549
  };
550

551
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
×
552
  if (code) {
×
553
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
554
              tstrerror(code));
555
  }
556

557
  // handle entry
558
  code = metaHandleEntry2(pMeta, &entry);
×
559
  if (TSDB_CODE_SUCCESS == code) {
×
560
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
×
561
             version, pReq->name, pReq->uid);
562
  } else {
563
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
564
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
565
  }
566
  TAOS_RETURN(code);
×
567
#if 0
568
  metaTimeSeriesNotifyCheck(pMeta);
569
#endif
570
}
571

572
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
×
573
  int32_t code = TSDB_CODE_SUCCESS;
×
574

575
  if (NULL == ppRsp) {
×
576
    return code;
×
577
  }
578

579
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
580
  if (NULL == *ppRsp) {
×
581
    return terrno;
×
582
  }
583

584
  code = metaUpdateVtbMetaRsp(pEntry->uid, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
×
585
  if (code) {
×
586
    taosMemoryFreeClear(*ppRsp);
×
587
    return code;
×
588
  }
589
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
×
590

591
  return code;
×
592
}
593

594
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
×
595
  // check request
596
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
×
597
  if (code) {
×
598
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
599
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
600
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
601
    }
602
    TAOS_RETURN(code);
×
603
  }
604

605
  SMetaEntry entry = {
×
606
      .version = version,
607
      .type = TSDB_VIRTUAL_CHILD_TABLE,
608
      .uid = pReq->uid,
×
609
      .name = pReq->name,
×
610
      .ctbEntry.btime = pReq->btime,
×
611
      .ctbEntry.ttlDays = pReq->ttl,
×
612
      .ctbEntry.commentLen = pReq->commentLen,
×
613
      .ctbEntry.comment = pReq->comment,
×
614
      .ctbEntry.suid = pReq->ctb.suid,
×
615
      .ctbEntry.pTags = pReq->ctb.pTag,
×
616
      .colRef = pReq->colRef
617
  };
618

619
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
×
620
  if (code) {
×
621
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
622
              tstrerror(code));
623
  }
624

625
  // handle entry
626
  code = metaHandleEntry2(pMeta, &entry);
×
627
  if (TSDB_CODE_SUCCESS == code) {
×
628
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
×
629
             version, pReq->name, pReq->uid);
630
  } else {
631
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
632
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
633
  }
634
  TAOS_RETURN(code);
×
635
#if 0
636
  metaTimeSeriesNotifyCheck(pMeta);
637
#endif
638
}
639

640
// Drop Normal Table
641

642
// Alter Normal Table
643

644
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
6,878✔
645
  int32_t code = TSDB_CODE_SUCCESS;
6,878✔
646
  if (TSDB_CHILD_TABLE == pReq->type) {
6,878✔
647
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
6,784✔
648
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
94!
649
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
94✔
650
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
×
651
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
×
652
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
×
653
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
×
654
  } else {
655
    code = TSDB_CODE_INVALID_MSG;
×
656
  }
657
  TAOS_RETURN(code);
6,878✔
658
}
659

660
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,222✔
661
  int32_t code = TSDB_CODE_SUCCESS;
1,222✔
662

663
  // check request
664
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,222✔
665
  if (code) {
1,222!
666
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
667
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
668
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
669
    }
670
    TAOS_RETURN(code);
×
671
  }
672

673
  if (pReq->suid == pReq->uid) {
1,222!
674
    code = TSDB_CODE_INVALID_PARA;
×
675
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
676
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
677
    TAOS_RETURN(code);
×
678
  }
679

680
  SMetaEntry entry = {
1,222✔
681
      .version = version,
682
      .uid = pReq->uid,
1,222✔
683
  };
684

685
  if (pReq->isVirtual) {
1,222!
686
    if (pReq->suid == 0) {
×
687
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
×
688
    } else {
689
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
×
690
    }
691
  } else {
692
    if (pReq->suid == 0) {
1,222✔
693
      entry.type = -TSDB_NORMAL_TABLE;
36✔
694
    } else {
695
      entry.type = -TSDB_CHILD_TABLE;
1,186✔
696
    }
697
  }
698
  code = metaHandleEntry2(pMeta, &entry);
1,222✔
699
  if (code) {
1,222!
700
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
701
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
702
  } else {
703
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,222!
704
             pReq->uid, version);
705
  }
706
  TAOS_RETURN(code);
1,222✔
707
}
708

709
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
38✔
710
  int32_t code = 0;
38✔
711

712
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
38!
713
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
714
              __func__, __FILE__, __LINE__, pReq->colName, version);
715
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
716
  }
717

718
  // check name
719
  void   *value = NULL;
38✔
720
  int32_t valueSize = 0;
38✔
721
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
38✔
722
  if (code) {
38!
723
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
724
              __FILE__, __LINE__, pReq->tbName, version);
725
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
726
    TAOS_RETURN(code);
×
727
  }
728
  int64_t uid = *(int64_t *)value;
38✔
729
  tdbFreeClear(value);
38✔
730

731
  // check table type
732
  SMetaInfo info;
733
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
38!
734
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
735
              " not found, this is an internal error in meta, version:%" PRId64,
736
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
737
    code = TSDB_CODE_INTERNAL_ERROR;
×
738
    TAOS_RETURN(code);
×
739
  }
740
  if (info.suid != 0 && pReq->action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF && pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
38!
741
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
742
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
743
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
744
    TAOS_RETURN(code);
×
745
  }
746

747
  // check grant
748
  code = grantCheck(TSDB_GRANT_TIMESERIES);
38✔
749
  if (code) {
38!
750
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
751
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
752
    TAOS_RETURN(code);
×
753
  }
754
  TAOS_RETURN(code);
38✔
755
}
756

757
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
28✔
758
  int32_t code = TSDB_CODE_SUCCESS;
28✔
759

760
  // check request
761
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
28✔
762
  if (code) {
28!
763
    TAOS_RETURN(code);
×
764
  }
765

766
  // fetch old entry
767
  SMetaEntry *pEntry = NULL;
28✔
768
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
28✔
769
  if (code) {
28!
770
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
771
              __FILE__, __LINE__, pReq->tbName, version);
772
    TAOS_RETURN(code);
×
773
  }
774
  if (pEntry->version >= version) {
28!
775
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
776
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
777
    metaFetchEntryFree(&pEntry);
×
778
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
779
  }
780

781
  // do add column
782
  int32_t         rowSize = 0;
28✔
783
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
28✔
784
  SSchema        *pColumn;
785
  SExtSchema      extSchema = {0};
28✔
786
  pEntry->version = version;
28✔
787
  for (int32_t i = 0; i < pSchema->nCols; i++) {
168✔
788
    pColumn = &pSchema->pSchema[i];
140✔
789
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
140!
790
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
×
791
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
792
      metaFetchEntryFree(&pEntry);
×
793
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
×
794
    }
795
    rowSize += pColumn->bytes;
140✔
796
  }
797

798
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
28!
799
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
800
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, TSDB_MAX_BYTES_PER_ROW, version);
801
    metaFetchEntryFree(&pEntry);
×
802
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
803
  }
804

805
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
28!
806
  if (NULL == pNewSchema) {
28!
807
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
808
              __LINE__, tstrerror(terrno), version);
809
    metaFetchEntryFree(&pEntry);
×
810
    TAOS_RETURN(terrno);
×
811
  }
812
  pSchema->pSchema = pNewSchema;
28✔
813
  pSchema->version++;
28✔
814
  pSchema->nCols++;
28✔
815
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
28✔
816
  pColumn->bytes = pReq->bytes;
28✔
817
  pColumn->type = pReq->type;
28✔
818
  pColumn->flags = pReq->flags;
28✔
819
  pColumn->colId = pEntry->ntbEntry.ncid++;
28✔
820
  extSchema.typeMod = pReq->typeMod;
28✔
821
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
28✔
822
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
28!
823
    SColRef tmpRef;
824
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
×
825
      tmpRef.hasRef = false;
×
826
      tmpRef.id = pColumn->colId;
×
827
    } else {
828
      tmpRef.hasRef = true;
×
829
      tmpRef.id = pColumn->colId;
×
830
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
×
831
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
×
832
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
×
833
    }
834
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
×
835
    if (code) {
×
836
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
837
                __LINE__, tstrerror(code), version);
838
      metaFetchEntryFree(&pEntry);
×
839
      TAOS_RETURN(code);
×
840
    }
841
  } else {
842
    uint32_t compress;
843
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
28!
844
      compress = createDefaultColCmprByType(pColumn->type);
28✔
845
    } else {
846
      compress = pReq->compress;
×
847
    }
848
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
28✔
849
    if (code) {
28!
850
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
851
                __LINE__, tstrerror(code), version);
852
      metaFetchEntryFree(&pEntry);
×
853
      TAOS_RETURN(code);
×
854
    }
855
  }
856
  code = addTableExtSchema(pEntry, pColumn, pSchema->nCols, &extSchema);
28✔
857
  if (code) {
28!
858
    metaError("vgId:%d, %s failed to add ext schema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
859
              __func__, __FILE__, __LINE__, tstrerror(code), version);
860
    metaFetchEntryFree(&pEntry);
×
861
    TAOS_RETURN(code);
×
862
  }
863

864
  // do handle entry
865
  code = metaHandleEntry2(pMeta, pEntry);
28✔
866
  if (code) {
28!
867
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
868
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
869
    metaFetchEntryFree(&pEntry);
×
870
    TAOS_RETURN(code);
×
871
  } else {
872
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
28!
873
             pEntry->uid, version);
874
  }
875

876
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
28!
877
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
878
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
879
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
880
    } else {
881
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
882
        SColRef *p = &pEntry->colRef.pColRef[i];
×
883
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
884
        pRsp->pColRefs[i].id = p->id;
×
885
        if (p->hasRef) {
×
886
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
887
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
888
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
889
        }
890
      }
891
    }
892
  } else {
893
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
28!
894
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
895
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
896
    } else {
897
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
196✔
898
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
168✔
899
        pRsp->pSchemaExt[i].colId = p->id;
168✔
900
        pRsp->pSchemaExt[i].compress = p->alg;
168✔
901
      }
902
    }
903
  }
904

905
  metaFetchEntryFree(&pEntry);
28✔
906
  TAOS_RETURN(code);
28✔
907
}
908

909
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
10✔
910
  int32_t code = TSDB_CODE_SUCCESS;
10✔
911

912
  // check request
913
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
10✔
914
  if (code) {
10!
915
    TAOS_RETURN(code);
×
916
  }
917

918
  // fetch old entry
919
  SMetaEntry *pEntry = NULL;
10✔
920
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
10✔
921
  if (code) {
10!
922
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
923
              __FILE__, __LINE__, pReq->tbName, version);
924
    TAOS_RETURN(code);
×
925
  }
926

927
  if (pEntry->version >= version) {
10!
928
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
929
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
930
    metaFetchEntryFree(&pEntry);
×
931
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
932
  }
933

934
  // search the column to drop
935
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
10✔
936
  SSchema        *pColumn = NULL;
10✔
937
  SSchema         tColumn;
938
  int32_t         iColumn = 0;
10✔
939
  for (; iColumn < pSchema->nCols; iColumn++) {
20!
940
    pColumn = &pSchema->pSchema[iColumn];
20✔
941
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
20✔
942
      break;
10✔
943
    }
944
  }
945

946
  if (iColumn == pSchema->nCols) {
10!
947
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
948
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
949
    metaFetchEntryFree(&pEntry);
×
950
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
951
  }
952

953
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
10!
954
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
955
              __func__, __FILE__, __LINE__, pReq->colName, version);
956
    metaFetchEntryFree(&pEntry);
×
957
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
958
  }
959

960
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
10!
961
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
×
962
              __func__, __FILE__, __LINE__, pReq->colName, version);
963
    metaFetchEntryFree(&pEntry);
×
964
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
965
  }
966
  tColumn = *pColumn;
10✔
967

968
  // do drop column
969
  pEntry->version = version;
10✔
970
  if (pSchema->nCols - iColumn - 1 > 0) {
10!
971
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
10✔
972
  }
973
  pSchema->nCols--;
10✔
974
  pSchema->version++;
10✔
975
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
10!
976
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
×
977
    if (code) {
×
978
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
979
                __LINE__, tstrerror(code), version);
980
      metaFetchEntryFree(&pEntry);
×
981
      TAOS_RETURN(code);
×
982
    }
983
    if (pEntry->colRef.nCols != pSchema->nCols) {
×
984
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
985
                __func__, __FILE__, __LINE__, version);
986
      metaFetchEntryFree(&pEntry);
×
987
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
988
    }
989
  } else {
990
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
10✔
991
    if (code) {
10!
992
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
993
                __LINE__, tstrerror(code), version);
994
      metaFetchEntryFree(&pEntry);
×
995
      TAOS_RETURN(code);
×
996
    }
997
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
10!
998
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
999
                __func__, __FILE__, __LINE__, version);
1000
      metaFetchEntryFree(&pEntry);
×
1001
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1002
    }
1003
  }
1004

1005
  // update column extschema
1006
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
10✔
1007
  if (code) {
10!
1008
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1009
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1010
    metaFetchEntryFree(&pEntry);
×
1011
    TAOS_RETURN(code);
×
1012
  }
1013

1014
  // do handle entry
1015
  code = metaHandleEntry2(pMeta, pEntry);
10✔
1016
  if (code) {
10!
1017
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1018
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1019
    metaFetchEntryFree(&pEntry);
×
1020
  } else {
1021
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
10!
1022
             pEntry->uid, version);
1023
  }
1024

1025
  // build response
1026
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
10!
1027
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1028
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1029
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1030
    } else {
1031
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1032
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1033
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1034
        pRsp->pColRefs[i].id = p->id;
×
1035
        if (p->hasRef) {
×
1036
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1037
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1038
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1039
        }
1040
      }
1041
    }
1042
  } else {
1043
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
10!
1044
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1045
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1046
    } else {
1047
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
70✔
1048
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
60✔
1049
        pRsp->pSchemaExt[i].colId = p->id;
60✔
1050
        pRsp->pSchemaExt[i].compress = p->alg;
60✔
1051
      }
1052
    }
1053
  }
1054

1055
  metaFetchEntryFree(&pEntry);
10✔
1056
  TAOS_RETURN(code);
10✔
1057
}
1058

1059
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1060
  int32_t code = TSDB_CODE_SUCCESS;
×
1061

1062
  // check request
1063
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1064
  if (code) {
×
1065
    TAOS_RETURN(code);
×
1066
  }
1067

1068
  if (NULL == pReq->colNewName) {
×
1069
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1070
              __func__, __FILE__, __LINE__, version);
1071
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1072
  }
1073

1074
  // fetch old entry
1075
  SMetaEntry *pEntry = NULL;
×
1076
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1077
  if (code) {
×
1078
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1079
              __FILE__, __LINE__, pReq->tbName, version);
1080
    TAOS_RETURN(code);
×
1081
  }
1082

1083
  if (pEntry->version >= version) {
×
1084
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1085
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1086
    metaFetchEntryFree(&pEntry);
×
1087
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1088
  }
1089

1090
  // search the column to update
1091
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
1092
  SSchema        *pColumn = NULL;
×
1093
  int32_t         iColumn = 0;
×
1094
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1095
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1096
      pColumn = &pSchema->pSchema[i];
×
1097
      iColumn = i;
×
1098
      break;
×
1099
    }
1100
  }
1101

1102
  if (NULL == pColumn) {
×
1103
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1104
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1105
    metaFetchEntryFree(&pEntry);
×
1106
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1107
  }
1108

1109
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
×
1110
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1111
              __func__, __FILE__, __LINE__, pColumn->name, version);
1112
    metaFetchEntryFree(&pEntry);
×
1113
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
×
1114
  }
1115

1116
  // do update column name
1117
  pEntry->version = version;
×
1118
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
×
1119
  pSchema->version++;
×
1120

1121
  // do handle entry
1122
  code = metaHandleEntry2(pMeta, pEntry);
×
1123
  if (code) {
×
1124
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1125
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1126
    metaFetchEntryFree(&pEntry);
×
1127
    TAOS_RETURN(code);
×
1128
  } else {
1129
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1130
             pEntry->uid, version);
1131
  }
1132

1133
  // build response
1134
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
1135
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1136
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1137
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1138
    } else {
1139
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1140
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1141
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1142
        pRsp->pColRefs[i].id = p->id;
×
1143
        if (p->hasRef) {
×
1144
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1145
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1146
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1147
        }
1148
      }
1149
    }
1150
  } else {
1151
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
×
1152
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1153
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1154
    } else {
1155
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
×
1156
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
1157
        pRsp->pSchemaExt[i].colId = p->id;
×
1158
        pRsp->pSchemaExt[i].compress = p->alg;
×
1159
      }
1160
    }
1161
  }
1162

1163
  metaFetchEntryFree(&pEntry);
×
1164
  TAOS_RETURN(code);
×
1165
}
1166

1167
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1168
  int32_t code = TSDB_CODE_SUCCESS;
×
1169

1170
  // check request
1171
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1172
  if (code) {
×
1173
    TAOS_RETURN(code);
×
1174
  }
1175

1176
  // fetch old entry
1177
  SMetaEntry *pEntry = NULL;
×
1178
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1179
  if (code) {
×
1180
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1181
              __FILE__, __LINE__, pReq->tbName, version);
1182
    TAOS_RETURN(code);
×
1183
  }
1184

1185
  if (pEntry->version >= version) {
×
1186
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1187
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1188
    metaFetchEntryFree(&pEntry);
×
1189
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1190
  }
1191

1192
  // search the column to update
1193
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
1194
  SSchema        *pColumn = NULL;
×
1195
  int32_t         iColumn = 0;
×
1196
  int32_t         rowSize = 0;
×
1197
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1198
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1199
      pColumn = &pSchema->pSchema[i];
×
1200
      iColumn = i;
×
1201
    }
1202
    rowSize += pSchema->pSchema[i].bytes;
×
1203
  }
1204

1205
  if (NULL == pColumn) {
×
1206
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1207
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1208
    metaFetchEntryFree(&pEntry);
×
1209
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1210
  }
1211

1212
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
×
1213
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
×
1214
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1215
              version);
1216
    metaFetchEntryFree(&pEntry);
×
1217
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1218
  }
1219

1220
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
×
1221
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1222
              __func__, __FILE__, __LINE__, pReq->colName, version);
1223
    metaFetchEntryFree(&pEntry);
×
1224
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
×
1225
  }
1226

1227
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
1228
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1229
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW,
1230
              version);
1231
    metaFetchEntryFree(&pEntry);
×
1232
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
1233
  }
1234

1235
  // do change the column bytes
1236
  pEntry->version = version;
×
1237
  pSchema->version++;
×
1238
  pColumn->bytes = pReq->colModBytes;
×
1239

1240
  // do handle entry
1241
  code = metaHandleEntry2(pMeta, pEntry);
×
1242
  if (code) {
×
1243
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1244
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1245
    metaFetchEntryFree(&pEntry);
×
1246
    TAOS_RETURN(code);
×
1247
  } else {
1248
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1249
             pEntry->uid, version);
1250
  }
1251

1252
  // build response
1253
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
1254
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1255
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1256
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1257
    } else {
1258
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1259
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1260
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1261
        pRsp->pColRefs[i].id = p->id;
×
1262
        if (p->hasRef) {
×
1263
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1264
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1265
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1266
        }
1267
      }
1268
    }
1269
  } else {
1270
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
×
1271
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1272
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1273
    } else {
1274
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
×
1275
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
1276
        pRsp->pSchemaExt[i].colId = p->id;
×
1277
        pRsp->pSchemaExt[i].compress = p->alg;
×
1278
      }
1279
    }
1280
  }
1281

1282
  metaFetchEntryFree(&pEntry);
×
1283
  TAOS_RETURN(code);
×
1284
}
1285

1286
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4✔
1287
  int32_t code = 0;
4✔
1288

1289
  // check tag name
1290
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
4!
1291
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1292
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1293
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1294
  }
1295

1296
  // check name
1297
  void   *value = NULL;
4✔
1298
  int32_t valueSize = 0;
4✔
1299
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4✔
1300
  if (code) {
4!
1301
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1302
              __FILE__, __LINE__, pReq->tbName, version);
1303
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1304
    TAOS_RETURN(code);
×
1305
  }
1306
  tdbFreeClear(value);
4✔
1307

1308
  TAOS_RETURN(code);
4✔
1309
}
1310

1311
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4✔
1312
  int32_t code = TSDB_CODE_SUCCESS;
4✔
1313

1314
  // check request
1315
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
4✔
1316
  if (code) {
4!
1317
    TAOS_RETURN(code);
×
1318
  }
1319

1320
  // fetch child entry
1321
  SMetaEntry *pChild = NULL;
4✔
1322
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
4✔
1323
  if (code) {
4!
1324
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1325
              __FILE__, __LINE__, pReq->tbName, version);
1326
    TAOS_RETURN(code);
×
1327
  }
1328

1329
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
4!
1330
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1331
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1332
    metaFetchEntryFree(&pChild);
×
1333
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1334
  }
1335

1336
  // fetch super entry
1337
  SMetaEntry *pSuper = NULL;
4✔
1338
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
4✔
1339
  if (code) {
4!
1340
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1341
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1342
    metaFetchEntryFree(&pChild);
×
1343
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1344
  }
1345

1346
  // search the tag to update
1347
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
4✔
1348
  SSchema        *pColumn = NULL;
4✔
1349
  int32_t         iColumn = 0;
4✔
1350
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
8!
1351
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
8✔
1352
      pColumn = &pTagSchema->pSchema[i];
4✔
1353
      iColumn = i;
4✔
1354
      break;
4✔
1355
    }
1356
  }
1357

1358
  if (NULL == pColumn) {
4!
1359
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1360
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1361
    metaFetchEntryFree(&pChild);
×
1362
    metaFetchEntryFree(&pSuper);
×
1363
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1364
  }
1365

1366
  // do change tag value
1367
  pChild->version = version;
4✔
1368
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
4!
1369
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
×
1370
    if (NULL == pNewTag) {
×
1371
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1372
                __LINE__, tstrerror(terrno), version);
1373
      metaFetchEntryFree(&pChild);
×
1374
      metaFetchEntryFree(&pSuper);
×
1375
      TAOS_RETURN(terrno);
×
1376
    }
1377
    pChild->ctbEntry.pTags = pNewTag;
×
1378
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
×
1379
  } else {
1380
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
4✔
1381

1382
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
4✔
1383
    if (NULL == pTagArray) {
4!
1384
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1385
                __LINE__, tstrerror(terrno), version);
1386
      metaFetchEntryFree(&pChild);
×
1387
      metaFetchEntryFree(&pSuper);
×
1388
      TAOS_RETURN(terrno);
×
1389
    }
1390

1391
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
20✔
1392
      STagVal value = {
16✔
1393
          .type = pTagSchema->pSchema[i].type,
16✔
1394
          .cid = pTagSchema->pSchema[i].colId,
16✔
1395
      };
1396

1397
      if (iColumn == i) {
16✔
1398
        if (pReq->isNull) {
4!
1399
          continue;
×
1400
        }
1401
        if (IS_VAR_DATA_TYPE(value.type)) {
4!
1402
          value.pData = pReq->pTagVal;
2✔
1403
          value.nData = pReq->nTagVal;
2✔
1404
        } else {
1405
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
2✔
1406
        }
1407
      } else if (!tTagGet(pOldTag, &value)) {
12!
1408
        continue;
×
1409
      }
1410

1411
      if (NULL == taosArrayPush(pTagArray, &value)) {
16!
1412
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1413
                  __LINE__, tstrerror(terrno), version);
1414
        taosArrayDestroy(pTagArray);
×
1415
        metaFetchEntryFree(&pChild);
×
1416
        metaFetchEntryFree(&pSuper);
×
1417
        TAOS_RETURN(terrno);
×
1418
      }
1419
    }
1420

1421
    STag *pNewTag = NULL;
4✔
1422
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
4✔
1423
    if (code) {
4!
1424
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1425
                __LINE__, tstrerror(code), version);
1426
      taosArrayDestroy(pTagArray);
×
1427
      metaFetchEntryFree(&pChild);
×
1428
      metaFetchEntryFree(&pSuper);
×
1429
      TAOS_RETURN(code);
×
1430
    }
1431
    taosArrayDestroy(pTagArray);
4✔
1432
    taosMemoryFree(pChild->ctbEntry.pTags);
4!
1433
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
4✔
1434
  }
1435

1436
  // do handle entry
1437
  code = metaHandleEntry2(pMeta, pChild);
4✔
1438
  if (code) {
4!
1439
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1440
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1441
    metaFetchEntryFree(&pChild);
×
1442
    metaFetchEntryFree(&pSuper);
×
1443
    TAOS_RETURN(code);
×
1444
  } else {
1445
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
4!
1446
             pChild->uid, version);
1447
  }
1448

1449
  // free resource and return
1450
  metaFetchEntryFree(&pChild);
4✔
1451
  metaFetchEntryFree(&pSuper);
4✔
1452
  TAOS_RETURN(code);
4✔
1453
}
1454

1455
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
1456
  int32_t code = 0;
×
1457

1458
  // check tag name
1459
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
×
1460
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1461
              __FILE__, __LINE__, version);
1462
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1463
  }
1464

1465
  // check name
1466
  void   *value = NULL;
×
1467
  int32_t valueSize = 0;
×
1468
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
1469
  if (code) {
×
1470
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1471
              __FILE__, __LINE__, pReq->tbName, version);
1472
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1473
    TAOS_RETURN(code);
×
1474
  }
1475
  tdbFreeClear(value);
×
1476

1477
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
×
1478
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1479
              __FILE__, __LINE__, version);
1480
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1481
  }
1482

1483
  TAOS_RETURN(code);
×
1484
}
1485

1486
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
1487
  int32_t code = TSDB_CODE_SUCCESS;
×
1488

1489
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
×
1490
  if (code) {
×
1491
    TAOS_RETURN(code);
×
1492
  }
1493

1494
  // fetch child entry
1495
  SMetaEntry *pChild = NULL;
×
1496
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
×
1497
  if (code) {
×
1498
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1499
              __FILE__, __LINE__, pReq->tbName, version);
1500
    TAOS_RETURN(code);
×
1501
  }
1502

1503
  if (pChild->type != TSDB_CHILD_TABLE) {
×
1504
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1505
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1506
    metaFetchEntryFree(&pChild);
×
1507
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1508
  }
1509

1510
  // fetch super entry
1511
  SMetaEntry *pSuper = NULL;
×
1512
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
1513
  if (code) {
×
1514
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1515
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1516
    metaFetchEntryFree(&pChild);
×
1517
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1518
  }
1519

1520
  // search the tags to update
1521
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
×
1522

1523
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
1524
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1525
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1526
    metaFetchEntryFree(&pChild);
×
1527
    metaFetchEntryFree(&pSuper);
×
1528
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1529
  }
1530

1531
  // do check if tag name exists
1532
  SHashObj *pTagTable =
1533
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
×
1534
  if (pTagTable == NULL) {
×
1535
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1536
              __LINE__, tstrerror(terrno), version);
1537
    metaFetchEntryFree(&pChild);
×
1538
    metaFetchEntryFree(&pSuper);
×
1539
    TAOS_RETURN(terrno);
×
1540
  }
1541

1542
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
×
1543
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
×
1544
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
×
1545
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1546
                __LINE__, tstrerror(terrno), version);
1547
      taosHashCleanup(pTagTable);
×
1548
      metaFetchEntryFree(&pChild);
×
1549
      metaFetchEntryFree(&pSuper);
×
1550
      TAOS_RETURN(terrno);
×
1551
    }
1552
  }
1553

1554
  int32_t numOfChangedTags = 0;
×
1555
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
1556
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
×
1557
        ? numOfChangedTags++
×
1558
        : 0;
×
1559
  }
1560
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
×
1561
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1562
              __FILE__, __LINE__, version);
1563
    taosHashCleanup(pTagTable);
×
1564
    metaFetchEntryFree(&pChild);
×
1565
    metaFetchEntryFree(&pSuper);
×
1566
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1567
  }
1568

1569
  // do change tag value
1570
  pChild->version = version;
×
1571
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
×
1572
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
1573
  if (NULL == pTagArray) {
×
1574
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1575
              __LINE__, tstrerror(terrno), version);
1576
    taosHashCleanup(pTagTable);
×
1577
    metaFetchEntryFree(&pChild);
×
1578
    metaFetchEntryFree(&pSuper);
×
1579
    TAOS_RETURN(terrno);
×
1580
  }
1581

1582
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
1583
    SSchema *pCol = &pTagSchema->pSchema[i];
×
1584
    STagVal  value = {
×
1585
         .cid = pCol->colId,
×
1586
    };
1587

1588
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
×
1589
    if (pTagVal == NULL) {
×
1590
      if (!tTagGet(pOldTag, &value)) {
×
1591
        continue;
×
1592
      }
1593
    } else {
1594
      value.type = pCol->type;
×
1595
      if (pTagVal->isNull) {
×
1596
        continue;
×
1597
      }
1598

1599
      if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1600
        value.pData = pTagVal->pTagVal;
×
1601
        value.nData = pTagVal->nTagVal;
×
1602
      } else {
1603
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
×
1604
      }
1605
    }
1606

1607
    if (taosArrayPush(pTagArray, &value) == NULL) {
×
1608
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1609
                __LINE__, tstrerror(terrno), version);
1610
      taosHashCleanup(pTagTable);
×
1611
      taosArrayDestroy(pTagArray);
×
1612
      metaFetchEntryFree(&pChild);
×
1613
      metaFetchEntryFree(&pSuper);
×
1614
      TAOS_RETURN(terrno);
×
1615
    }
1616
  }
1617

1618
  STag *pNewTag = NULL;
×
1619
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
×
1620
  if (code) {
×
1621
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1622
              __LINE__, tstrerror(code), version);
1623
    taosHashCleanup(pTagTable);
×
1624
    taosArrayDestroy(pTagArray);
×
1625
    metaFetchEntryFree(&pChild);
×
1626
    metaFetchEntryFree(&pSuper);
×
1627
    TAOS_RETURN(code);
×
1628
  }
1629
  taosArrayDestroy(pTagArray);
×
1630
  taosMemoryFree(pChild->ctbEntry.pTags);
×
1631
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
×
1632

1633
  // do handle entry
1634
  code = metaHandleEntry2(pMeta, pChild);
×
1635
  if (code) {
×
1636
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1637
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1638
    taosHashCleanup(pTagTable);
×
1639
    metaFetchEntryFree(&pChild);
×
1640
    metaFetchEntryFree(&pSuper);
×
1641
    TAOS_RETURN(code);
×
1642
  } else {
1643
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1644
             pChild->uid, version);
1645
  }
1646

1647
  taosHashCleanup(pTagTable);
×
1648
  metaFetchEntryFree(&pChild);
×
1649
  metaFetchEntryFree(&pSuper);
×
1650
  TAOS_RETURN(code);
×
1651
}
1652

1653
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
1654
  int32_t code = TSDB_CODE_SUCCESS;
×
1655

1656
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
×
1657
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1658
              __FILE__, __LINE__, version);
1659
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1660
  }
1661

1662
  return code;
×
1663
}
1664

1665
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
1666
  int32_t code = 0;
×
1667

1668
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
×
1669
  if (code) {
×
1670
    TAOS_RETURN(code);
×
1671
  }
1672

1673
  // fetch entry
1674
  SMetaEntry *pEntry = NULL;
×
1675
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1676
  if (code) {
×
1677
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1678
              __FILE__, __LINE__, pReq->tbName, version);
1679
    TAOS_RETURN(code);
×
1680
  }
1681

1682
  // do change the entry
1683
  pEntry->version = version;
×
1684
  if (pEntry->type == TSDB_CHILD_TABLE) {
×
1685
    if (pReq->updateTTL) {
×
1686
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
×
1687
    }
1688
    if (pReq->newCommentLen >= 0) {
×
1689
      char *pNewComment = NULL;
×
1690
      if (pReq->newCommentLen) {
×
1691
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
×
1692
        if (NULL == pNewComment) {
×
1693
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1694
                    __LINE__, tstrerror(terrno), version);
1695
          metaFetchEntryFree(&pEntry);
×
1696
          TAOS_RETURN(terrno);
×
1697
        }
1698
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
×
1699
      } else {
1700
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
×
1701
      }
1702
      pEntry->ctbEntry.comment = pNewComment;
×
1703
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
×
1704
    }
1705
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
×
1706
    if (pReq->updateTTL) {
×
1707
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
×
1708
    }
1709
    if (pReq->newCommentLen >= 0) {
×
1710
      char *pNewComment = NULL;
×
1711
      if (pReq->newCommentLen > 0) {
×
1712
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
×
1713
        if (NULL == pNewComment) {
×
1714
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1715
                    __LINE__, tstrerror(terrno), version);
1716
          metaFetchEntryFree(&pEntry);
×
1717
          TAOS_RETURN(terrno);
×
1718
        }
1719
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
×
1720
      } else {
1721
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
×
1722
      }
1723
      pEntry->ntbEntry.comment = pNewComment;
×
1724
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
×
1725
    }
1726
  } else {
1727
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1728
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1729
    metaFetchEntryFree(&pEntry);
×
1730
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1731
  }
1732

1733
  // do handle entry
1734
  code = metaHandleEntry2(pMeta, pEntry);
×
1735
  if (code) {
×
1736
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1737
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1738
    metaFetchEntryFree(&pEntry);
×
1739
    TAOS_RETURN(code);
×
1740
  } else {
1741
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1742
             pEntry->uid, version);
1743
  }
1744

1745
  metaFetchEntryFree(&pEntry);
×
1746
  TAOS_RETURN(code);
×
1747
}
1748

1749
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
×
1750
  int32_t code = TSDB_CODE_SUCCESS;
×
1751

1752
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
×
1753
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1754
              __FILE__, __LINE__, version);
1755
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1756
  }
1757

1758
  SMetaEntry *pEntry = NULL;
×
1759
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1760
  if (code) {
×
1761
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1762
              __FILE__, __LINE__, pReq->tbName, version);
1763
    TAOS_RETURN(code);
×
1764
  }
1765

1766
  if (pEntry->version >= version) {
×
1767
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1768
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1769
    metaFetchEntryFree(&pEntry);
×
1770
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1771
  }
1772

1773
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
×
1774
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1775
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1776
    metaFetchEntryFree(&pEntry);
×
1777
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1778
  }
1779

1780
  // do change the entry
1781
  int8_t           updated = 0;
×
1782
  SColCmprWrapper *wp = &pEntry->colCmpr;
×
1783
  for (int32_t i = 0; i < wp->nCols; i++) {
×
1784
    SColCmpr *p = &wp->pColCmpr[i];
×
1785
    if (p->id == pReq->colId) {
×
1786
      uint32_t dst = 0;
×
1787
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
×
1788
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1789
      if (updated > 0) {
×
1790
        p->alg = dst;
×
1791
      }
1792
    }
1793
  }
1794

1795
  if (updated == 0) {
×
1796
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1797
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1798
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1799
    metaFetchEntryFree(&pEntry);
×
1800
    TAOS_RETURN(code);
×
1801
  } else if (updated < 0) {
×
1802
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1803
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1804
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1805
    metaFetchEntryFree(&pEntry);
×
1806
    TAOS_RETURN(code);
×
1807
  }
1808

1809
  pEntry->version = version;
×
1810

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

1823
  metaFetchEntryFree(&pEntry);
×
1824
  TAOS_RETURN(code);
×
1825
}
1826

1827
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1828
  int32_t code = TSDB_CODE_SUCCESS;
×
1829

1830
  // check request
1831
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1832
  if (code) {
×
1833
    TAOS_RETURN(code);
×
1834
  }
1835

1836
  if (NULL == pReq->refDbName) {
×
1837
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1838
              __func__, __FILE__, __LINE__, version);
1839
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1840
  }
1841

1842
  if (NULL == pReq->refTbName) {
×
1843
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1844
              __func__, __FILE__, __LINE__, version);
1845
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1846
  }
1847

1848
  if (NULL == pReq->refColName) {
×
1849
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1850
              __func__, __FILE__, __LINE__, version);
1851
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1852
  }
1853

1854

1855
  // fetch old entry
1856
  SMetaEntry *pEntry = NULL;
×
1857
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1858
  if (code) {
×
1859
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1860
              __FILE__, __LINE__, pReq->tbName, version);
1861
    TAOS_RETURN(code);
×
1862
  }
1863

1864
  if (pEntry->version >= version) {
×
1865
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1866
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1867
    metaFetchEntryFree(&pEntry);
×
1868
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1869
  }
1870

1871
  // fetch super entry
1872
  SMetaEntry *pSuper = NULL;
×
1873
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
×
1874
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
×
1875
    if (code) {
×
1876
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1877
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1878
      metaFetchEntryFree(&pEntry);
×
1879
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1880
    }
1881
  }
1882

1883
  // search the column to update
1884
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
×
1885
  SColRef        *pColRef = NULL;
×
1886
  int32_t         iColumn = 0;
×
1887
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1888
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1889
      pColRef = &pEntry->colRef.pColRef[i];
×
1890
      iColumn = i;
×
1891
      break;
×
1892
    }
1893
  }
1894

1895
  if (NULL == pColRef) {
×
1896
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1897
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1898
    metaFetchEntryFree(&pEntry);
×
1899
    metaFetchEntryFree(&pSuper);
×
1900
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1901
  }
1902

1903
  // do update column name
1904
  pEntry->version = version;
×
1905
  pColRef->hasRef = true;
×
1906
  pColRef->id = pSchema->pSchema[iColumn].colId;
×
1907
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
×
1908
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
×
1909
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
×
1910
  pSchema->version++;
×
1911

1912
  // do handle entry
1913
  code = metaHandleEntry2(pMeta, pEntry);
×
1914
  if (code) {
×
1915
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1916
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1917
    metaFetchEntryFree(&pEntry);
×
1918
    metaFetchEntryFree(&pSuper);
×
1919
    TAOS_RETURN(code);
×
1920
  } else {
1921
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1922
             pEntry->uid, version);
1923
  }
1924

1925
  // build response
1926
  if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1927
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1928
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1929
  } else {
1930
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1931
      SColRef *p = &pEntry->colRef.pColRef[i];
×
1932
      pRsp->pColRefs[i].hasRef = p->hasRef;
×
1933
      pRsp->pColRefs[i].id = p->id;
×
1934
      if (p->hasRef) {
×
1935
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1936
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1937
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1938
      }
1939
    }
1940
  }
1941

1942
  metaFetchEntryFree(&pEntry);
×
1943
  metaFetchEntryFree(&pSuper);
×
1944
  TAOS_RETURN(code);
×
1945
}
1946

1947
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1948
  int32_t code = TSDB_CODE_SUCCESS;
×
1949

1950
  // check request
1951
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1952
  if (code) {
×
1953
    TAOS_RETURN(code);
×
1954
  }
1955

1956
  // fetch old entry
1957
  SMetaEntry *pEntry = NULL;
×
1958
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1959
  if (code) {
×
1960
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1961
              __FILE__, __LINE__, pReq->tbName, version);
1962
    TAOS_RETURN(code);
×
1963
  }
1964

1965
  if (pEntry->version >= version) {
×
1966
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1967
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1968
    metaFetchEntryFree(&pEntry);
×
1969
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1970
  }
1971

1972
  // fetch super entry
1973
  SMetaEntry *pSuper = NULL;
×
1974
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
×
1975
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
×
1976
    if (code) {
×
1977
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1978
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1979
      metaFetchEntryFree(&pEntry);
×
1980
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1981
    }
1982
  }
1983

1984
  // search the column to update
1985
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
×
1986
  SColRef        *pColRef = NULL;
×
1987
  int32_t         iColumn = 0;
×
1988
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1989
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1990
      pColRef = &pEntry->colRef.pColRef[i];
×
1991
      iColumn = i;
×
1992
      break;
×
1993
    }
1994
  }
1995

1996
  if (NULL == pColRef) {
×
1997
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1998
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
1999
    metaFetchEntryFree(&pEntry);
×
2000
    metaFetchEntryFree(&pSuper);
×
2001
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2002
  }
2003

2004
  // do update column name
2005
  pEntry->version = version;
×
2006
  pColRef->hasRef = false;
×
2007
  pColRef->id = pSchema->pSchema[iColumn].colId;
×
2008
  pSchema->version++;
×
2009

2010
  // do handle entry
2011
  code = metaHandleEntry2(pMeta, pEntry);
×
2012
  if (code) {
×
2013
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2014
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2015
    metaFetchEntryFree(&pEntry);
×
2016
    metaFetchEntryFree(&pSuper);
×
2017
    TAOS_RETURN(code);
×
2018
  } else {
2019
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
2020
             pEntry->uid, version);
2021
  }
2022

2023
  // build response
2024
  if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
2025
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2026
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2027
  } else {
2028
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
2029
      SColRef *p = &pEntry->colRef.pColRef[i];
×
2030
      pRsp->pColRefs[i].hasRef = p->hasRef;
×
2031
      pRsp->pColRefs[i].id = p->id;
×
2032
      if (p->hasRef) {
×
2033
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
2034
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
2035
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
2036
      }
2037
    }
2038
  }
2039

2040
  metaFetchEntryFree(&pEntry);
×
2041
  metaFetchEntryFree(&pSuper);
×
2042
  TAOS_RETURN(code);
×
2043
}
2044

2045
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
56✔
2046
  int32_t code = TSDB_CODE_SUCCESS;
56✔
2047

2048
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
56!
2049
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2050
              __FILE__, __LINE__, version);
2051
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2052
  }
2053

2054
  SMetaEntry *pEntry = NULL;
56✔
2055
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
56✔
2056
  if (code) {
56!
2057
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2058
              __FILE__, __LINE__, pReq->name, version);
2059
    TAOS_RETURN(code);
×
2060
  }
2061

2062
  if (pEntry->type != TSDB_SUPER_TABLE) {
56!
2063
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2064
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2065
    metaFetchEntryFree(&pEntry);
×
2066
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2067
  }
2068

2069
  if (pEntry->uid != pReq->suid) {
56!
2070
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2071
              ", version:%" PRId64,
2072
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2073
    metaFetchEntryFree(&pEntry);
×
2074
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2075
  }
2076

2077
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2078
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2079
  //   PRId64,
2080
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2081
  //             pReq->schemaTag.version, version);
2082
  //   metaFetchEntryFree(&pEntry);
2083
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2084
  // }
2085

2086
  // do change the entry
2087
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
56✔
2088
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
56✔
2089
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
56!
2090
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2091
              __func__, __FILE__, __LINE__, pReq->name, version);
2092
    metaFetchEntryFree(&pEntry);
×
2093
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2094
  }
2095

2096
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
56!
2097
    metaError(
×
2098
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2099
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2100
        version);
2101
    metaFetchEntryFree(&pEntry);
×
2102
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2103
  }
2104

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

2114
  int32_t numOfChangedTags = 0;
56✔
2115
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
840✔
2116
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
784✔
2117
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
784✔
2118

2119
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
784!
2120
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
784!
2121
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2122
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2123
      metaFetchEntryFree(&pEntry);
×
2124
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2125
    }
2126

2127
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
784✔
2128
      numOfChangedTags++;
56✔
2129
      SSCHMEA_SET_IDX_ON(pOldColumn);
56✔
2130
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
728!
2131
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2132
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2133
      metaFetchEntryFree(&pEntry);
×
2134
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2135
    }
2136
  }
2137

2138
  if (numOfChangedTags != 1) {
56!
2139
    metaError(
×
2140
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2141
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2142
    metaFetchEntryFree(&pEntry);
×
2143
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2144
  }
2145

2146
  pEntry->version = version;
56✔
2147
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
56✔
2148

2149
  // do handle the entry
2150
  code = metaHandleEntry2(pMeta, pEntry);
56✔
2151
  if (code) {
56!
2152
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2153
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2154
    metaFetchEntryFree(&pEntry);
×
2155
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2156
  } else {
2157
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
56!
2158
             pEntry->uid, version);
2159
  }
2160

2161
  metaFetchEntryFree(&pEntry);
56✔
2162
  TAOS_RETURN(code);
56✔
2163
}
2164

2165
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
56✔
2166
  int32_t code = TSDB_CODE_SUCCESS;
56✔
2167

2168
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
56!
2169
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2170
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2171
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2172
  }
2173

2174
  SMetaEntry *pEntry = NULL;
56✔
2175
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
56✔
2176
  if (code) {
56!
2177
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2178
              __FILE__, __LINE__, pReq->stb, version);
2179
    TAOS_RETURN(code);
×
2180
  }
2181

2182
  if (TSDB_SUPER_TABLE != pEntry->type) {
56!
2183
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2184
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2185
    metaFetchEntryFree(&pEntry);
×
2186
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2187
  }
2188

2189
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
56✔
2190
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
56!
2191
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2192
              __func__, __FILE__, __LINE__, pReq->stb, version);
2193
    metaFetchEntryFree(&pEntry);
×
2194
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2195
  }
2196

2197
  // search and set the tag index off
2198
  int32_t numOfChangedTags = 0;
56✔
2199
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
428!
2200
    SSchema *pCol = pTagSchema->pSchema + i;
428✔
2201
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
428✔
2202
      if (!IS_IDX_ON(pCol)) {
56!
2203
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2204
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2205
        metaFetchEntryFree(&pEntry);
×
2206
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2207
      }
2208
      numOfChangedTags++;
56✔
2209
      SSCHMEA_SET_IDX_OFF(pCol);
56✔
2210
      break;
56✔
2211
    }
2212
  }
2213

2214
  if (numOfChangedTags != 1) {
56!
2215
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2216
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2217
    metaFetchEntryFree(&pEntry);
×
2218
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2219
  }
2220

2221
  // do handle the entry
2222
  pEntry->version = version;
56✔
2223
  pTagSchema->version++;
56✔
2224
  code = metaHandleEntry2(pMeta, pEntry);
56✔
2225
  if (code) {
56!
2226
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2227
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2228
    metaFetchEntryFree(&pEntry);
×
2229
    TAOS_RETURN(code);
×
2230
  } else {
2231
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
56!
2232
             pEntry->uid, version);
2233
  }
2234

2235
  metaFetchEntryFree(&pEntry);
56✔
2236
  TAOS_RETURN(code);
56✔
2237
}
2238

2239
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
104✔
2240
  int32_t code = TSDB_CODE_SUCCESS;
104✔
2241

2242
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
104!
2243
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2244
              __FILE__, __LINE__, version);
2245
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2246
  }
2247

2248
  SMetaEntry *pEntry = NULL;
104✔
2249
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
104✔
2250
  if (code) {
104!
2251
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2252
              __FILE__, __LINE__, pReq->name, version);
2253
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2254
  }
2255

2256
  if (pEntry->type != TSDB_SUPER_TABLE) {
104!
2257
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2258
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2259
    metaFetchEntryFree(&pEntry);
×
2260
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2261
  }
2262

2263
  SMetaEntry entry = {
104✔
2264
      .version = version,
2265
      .type = TSDB_SUPER_TABLE,
2266
      .uid = pReq->suid,
104✔
2267
      .name = pReq->name,
104✔
2268
      .stbEntry.schemaRow = pReq->schemaRow,
2269
      .stbEntry.schemaTag = pReq->schemaTag,
2270
      .stbEntry.keep = pReq->keep,
104✔
2271
      .colCmpr = pReq->colCmpr,
2272
      .pExtSchemas = pReq->pExtSchemas,
104✔
2273
  };
2274
  TABLE_SET_COL_COMPRESSED(entry.flags);
104✔
2275
  if (pReq->virtualStb) {
104!
2276
    TABLE_SET_VIRTUAL(entry.flags);
×
2277
  }
2278

2279
  // do handle the entry
2280
  code = metaHandleEntry2(pMeta, &entry);
104✔
2281
  if (code) {
104!
2282
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2283
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2284
    metaFetchEntryFree(&pEntry);
×
2285
    TAOS_RETURN(code);
×
2286
  } else {
2287
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
104!
2288
             pReq->suid, version);
2289
  }
2290

2291
  metaFetchEntryFree(&pEntry);
104✔
2292
  TAOS_RETURN(code);
104✔
2293
}
2294

2295
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2296
  int32_t code = 0;
×
2297

2298
  if (taosArrayGetSize(uidArray) == 0) {
×
2299
    return TSDB_CODE_SUCCESS;
×
2300
  }
2301

2302
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2303
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2304
    SMetaInfo info;
2305
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2306
    if (code) {
×
2307
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2308
                __func__, __FILE__, __LINE__, uid, code);
2309
      return code;
×
2310
    }
2311

2312
    SMetaEntry entry = {
×
2313
        .version = version,
2314
        .uid = uid,
2315
    };
2316

2317
    if (info.suid == 0) {
×
2318
      entry.type = -TSDB_NORMAL_TABLE;
×
2319
    } else if (info.suid == uid) {
×
2320
      entry.type = -TSDB_SUPER_TABLE;
×
2321
    } else {
2322
      entry.type = -TSDB_CHILD_TABLE;
×
2323
    }
2324
    code = metaHandleEntry2(pMeta, &entry);
×
2325
    if (code) {
×
2326
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2327
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2328
      return code;
×
2329
    }
2330
  }
2331
  return code;
×
2332
}
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