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

taosdata / TDengine / #3843

08 Apr 2025 10:23AM UTC coverage: 63.077% (+0.4%) from 62.696%
#3843

push

travis-ci

web-flow
fix: clear cache when meta abort (#30674)

155571 of 315083 branches covered (49.37%)

Branch coverage included in aggregate %.

241876 of 315013 relevant lines covered (76.78%)

19243431.01 hits per line

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

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

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

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

54
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
116!
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) {
116!
62
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
116✔
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) {
23,815✔
73
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
8!
74
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
75
    return TSDB_CODE_INVALID_MSG;
×
76
  }
77

78
  return TSDB_CODE_SUCCESS;
23,814✔
79
}
80

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

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

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

117
  return code;
2,627✔
118
}
119

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

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

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

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

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

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

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

198
  entry.pExtSchemas = pReq->pExtSchemas;
23,821✔
199

200
  if (pReq->virtualStb) {
23,821✔
201
    TABLE_SET_VIRTUAL(entry.flags);
2✔
202
  }
203

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

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

219
  // check request
220
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
2,413✔
221
  if (code) {
2,409✔
222
    TAOS_RETURN(code);
5✔
223
  }
224

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

242
// Alter Super Table
243

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

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

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

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

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

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

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

314
  // Check tag value
315
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
151,029✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
151,029✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
151,029✔
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
617,692✔
319
      STagVal tagVal = {
466,884✔
320
          .cid = pTagSchema->pSchema[i].colId,
466,884✔
321
      };
322

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

334
  metaFetchEntryFree(&pStbEntry);
151,040✔
335

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

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

350
  if (NULL == ppRsp) {
151,025!
351
    return code;
×
352
  }
353

354
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
151,025!
355
  if (NULL == ppRsp) {
151,073!
356
    return terrno;
×
357
  }
358

359
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
151,073✔
360
  (*ppRsp)->tuid = pEntry->uid;
151,073✔
361
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
151,073✔
362
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
151,073✔
363

364
  return code;
151,073✔
365
}
366

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

370
  // check request
371
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
161,555✔
372
  if (code) {
161,504✔
373
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
10,486✔
374
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
9!
375
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
376
    }
377
    return code;
10,486✔
378
  }
379

380
  SMetaEntry entry = {
151,018✔
381
      .version = version,
382
      .type = TSDB_CHILD_TABLE,
383
      .uid = pReq->uid,
151,018✔
384
      .name = pReq->name,
151,018✔
385
      .ctbEntry.btime = pReq->btime,
151,018✔
386
      .ctbEntry.ttlDays = pReq->ttl,
151,018✔
387
      .ctbEntry.commentLen = pReq->commentLen,
151,018✔
388
      .ctbEntry.comment = pReq->comment,
151,018✔
389
      .ctbEntry.suid = pReq->ctb.suid,
151,018✔
390
      .ctbEntry.pTags = pReq->ctb.pTag,
151,018✔
391
  };
392

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

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

413
// Drop Child Table
414

415
// Alter Child Table
416

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

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

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

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

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

449
  if (NULL == ppRsp) {
14,301!
450
    return code;
×
451
  }
452

453
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
14,301!
454
  if (NULL == *ppRsp) {
14,301!
455
    return terrno;
×
456
  }
457

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

464
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
158,230✔
465
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
143,929✔
466
    (*ppRsp)->pSchemaExt[i].colId = p->id;
143,929✔
467
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
143,929✔
468
    if (pEntry->pExtSchemas) {
143,929✔
469
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
165✔
470
    }
471
  }
472

473
  return code;
14,301✔
474
}
475

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

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

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

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

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

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

527
  if (NULL == ppRsp) {
1!
528
    return code;
×
529
  }
530

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

536
  code = metaUpdateVtbMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_NORMAL_TABLE);
1✔
537
  if (code) {
1!
538
    taosMemoryFreeClear(*ppRsp);
×
539
    return code;
×
540
  }
541

542
  return code;
1✔
543
}
544

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

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

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

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

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

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

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

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

610
  return code;
×
611
}
612

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

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

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

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

659
// Drop Normal Table
660

661
// Alter Normal Table
662

663
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
175,856✔
664
  int32_t code = TSDB_CODE_SUCCESS;
175,856✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
175,856✔
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
161,565✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
14,291!
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
14,307✔
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
×
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
1✔
671
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
×
672
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
×
673
  } else {
674
    code = TSDB_CODE_INVALID_MSG;
×
675
  }
676
  TAOS_RETURN(code);
175,871✔
677
}
678

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

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

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

699
  SMetaEntry entry = {
2,627✔
700
      .version = version,
701
      .uid = pReq->uid,
2,627✔
702
  };
703

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

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

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

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

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

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

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

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

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

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

817
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
172!
818
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
819
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, TSDB_MAX_BYTES_PER_ROW, version);
820
    metaFetchEntryFree(&pEntry);
×
821
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
822
  }
823

824
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
172!
825
  if (NULL == pNewSchema) {
172!
826
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
827
              __LINE__, tstrerror(terrno), version);
828
    metaFetchEntryFree(&pEntry);
×
829
    TAOS_RETURN(terrno);
×
830
  }
831
  pSchema->pSchema = pNewSchema;
172✔
832
  pSchema->version++;
172✔
833
  pSchema->nCols++;
172✔
834
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
172✔
835
  pColumn->bytes = pReq->bytes;
172✔
836
  pColumn->type = pReq->type;
172✔
837
  pColumn->flags = pReq->flags;
172✔
838
  pColumn->colId = pEntry->ntbEntry.ncid++;
172✔
839
  extSchema.typeMod = pReq->typeMod;
172✔
840
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
172✔
841
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
172!
842
    SColRef tmpRef;
843
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
×
844
      tmpRef.hasRef = false;
×
845
      tmpRef.id = pColumn->colId;
×
846
    } else {
847
      tmpRef.hasRef = true;
×
848
      tmpRef.id = pColumn->colId;
×
849
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
×
850
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
×
851
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
×
852
    }
853
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
×
854
    if (code) {
×
855
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
856
                __LINE__, tstrerror(code), version);
857
      metaFetchEntryFree(&pEntry);
×
858
      TAOS_RETURN(code);
×
859
    }
860
  } else {
861
    uint32_t compress;
862
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
172✔
863
      compress = createDefaultColCmprByType(pColumn->type);
131✔
864
    } else {
865
      compress = pReq->compress;
41✔
866
    }
867
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
172✔
868
    if (code) {
172!
869
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
870
                __LINE__, tstrerror(code), version);
871
      metaFetchEntryFree(&pEntry);
×
872
      TAOS_RETURN(code);
×
873
    }
874
  }
875
  code = addTableExtSchema(pEntry, pColumn, pSchema->nCols, &extSchema);
172✔
876
  if (code) {
172!
877
    metaError("vgId:%d, %s failed to add ext schema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
878
              __func__, __FILE__, __LINE__, tstrerror(code), version);
879
    metaFetchEntryFree(&pEntry);
×
880
    TAOS_RETURN(code);
×
881
  }
882

883
  // do handle entry
884
  code = metaHandleEntry2(pMeta, pEntry);
172✔
885
  if (code) {
172!
886
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
887
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
888
    metaFetchEntryFree(&pEntry);
×
889
    TAOS_RETURN(code);
×
890
  } else {
891
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
172!
892
             pEntry->uid, version);
893
  }
894

895
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
172!
896
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
897
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
898
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
899
    } else {
900
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
901
        SColRef *p = &pEntry->colRef.pColRef[i];
×
902
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
903
        pRsp->pColRefs[i].id = p->id;
×
904
        if (p->hasRef) {
×
905
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
906
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
907
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
908
        }
909
      }
910
    }
911
  } else {
912
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
172!
913
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
914
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
915
    } else {
916
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
1,571✔
917
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
1,399✔
918
        pRsp->pSchemaExt[i].colId = p->id;
1,399✔
919
        pRsp->pSchemaExt[i].compress = p->alg;
1,399✔
920
      }
921
    }
922
  }
923

924
  metaFetchEntryFree(&pEntry);
172✔
925
  TAOS_RETURN(code);
172✔
926
}
927

928
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
135✔
929
  int32_t code = TSDB_CODE_SUCCESS;
135✔
930

931
  // check request
932
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
135✔
933
  if (code) {
135!
934
    TAOS_RETURN(code);
×
935
  }
936

937
  // fetch old entry
938
  SMetaEntry *pEntry = NULL;
135✔
939
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
135✔
940
  if (code) {
135!
941
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
942
              __FILE__, __LINE__, pReq->tbName, version);
943
    TAOS_RETURN(code);
×
944
  }
945

946
  if (pEntry->version >= version) {
135!
947
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
948
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
949
    metaFetchEntryFree(&pEntry);
×
950
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
951
  }
952

953
  // search the column to drop
954
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
135✔
955
  SSchema        *pColumn = NULL;
135✔
956
  SSchema         tColumn;
957
  int32_t         iColumn = 0;
135✔
958
  for (; iColumn < pSchema->nCols; iColumn++) {
813!
959
    pColumn = &pSchema->pSchema[iColumn];
813✔
960
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
813✔
961
      break;
135✔
962
    }
963
  }
964

965
  if (iColumn == pSchema->nCols) {
135!
966
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
967
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
968
    metaFetchEntryFree(&pEntry);
×
969
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
970
  }
971

972
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
135!
973
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
974
              __func__, __FILE__, __LINE__, pReq->colName, version);
975
    metaFetchEntryFree(&pEntry);
×
976
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
977
  }
978

979
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
135✔
980
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
981
              __func__, __FILE__, __LINE__, pReq->colName, version);
982
    metaFetchEntryFree(&pEntry);
11✔
983
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
11✔
984
  }
985
  tColumn = *pColumn;
124✔
986

987
  // do drop column
988
  pEntry->version = version;
124✔
989
  if (pSchema->nCols - iColumn - 1 > 0) {
124✔
990
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
82✔
991
  }
992
  pSchema->nCols--;
124✔
993
  pSchema->version++;
124✔
994
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
124!
995
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
×
996
    if (code) {
×
997
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
998
                __LINE__, tstrerror(code), version);
999
      metaFetchEntryFree(&pEntry);
×
1000
      TAOS_RETURN(code);
×
1001
    }
1002
    if (pEntry->colRef.nCols != pSchema->nCols) {
×
1003
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1004
                __func__, __FILE__, __LINE__, version);
1005
      metaFetchEntryFree(&pEntry);
×
1006
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1007
    }
1008
  } else {
1009
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
124✔
1010
    if (code) {
124!
1011
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1012
                __LINE__, tstrerror(code), version);
1013
      metaFetchEntryFree(&pEntry);
×
1014
      TAOS_RETURN(code);
×
1015
    }
1016
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
124!
1017
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1018
                __func__, __FILE__, __LINE__, version);
1019
      metaFetchEntryFree(&pEntry);
×
1020
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1021
    }
1022
  }
1023

1024
  // update column extschema
1025
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
124✔
1026
  if (code) {
124!
1027
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1028
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1029
    metaFetchEntryFree(&pEntry);
×
1030
    TAOS_RETURN(code);
×
1031
  }
1032

1033
  // do handle entry
1034
  code = metaHandleEntry2(pMeta, pEntry);
124✔
1035
  if (code) {
124!
1036
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1037
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1038
    metaFetchEntryFree(&pEntry);
×
1039
  } else {
1040
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
124!
1041
             pEntry->uid, version);
1042
  }
1043

1044
  // build response
1045
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
124!
1046
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1047
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1048
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1049
    } else {
1050
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1051
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1052
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1053
        pRsp->pColRefs[i].id = p->id;
×
1054
        if (p->hasRef) {
×
1055
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1056
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1057
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1058
        }
1059
      }
1060
    }
1061
  } else {
1062
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
124!
1063
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1064
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1065
    } else {
1066
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
1,028✔
1067
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
904✔
1068
        pRsp->pSchemaExt[i].colId = p->id;
904✔
1069
        pRsp->pSchemaExt[i].compress = p->alg;
904✔
1070
      }
1071
    }
1072
  }
1073

1074
  metaFetchEntryFree(&pEntry);
124✔
1075
  TAOS_RETURN(code);
124✔
1076
}
1077

1078
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
32✔
1079
  int32_t code = TSDB_CODE_SUCCESS;
32✔
1080

1081
  // check request
1082
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
32✔
1083
  if (code) {
32!
1084
    TAOS_RETURN(code);
×
1085
  }
1086

1087
  if (NULL == pReq->colNewName) {
32!
1088
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1089
              __func__, __FILE__, __LINE__, version);
1090
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1091
  }
1092

1093
  // fetch old entry
1094
  SMetaEntry *pEntry = NULL;
32✔
1095
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
32✔
1096
  if (code) {
32!
1097
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1098
              __FILE__, __LINE__, pReq->tbName, version);
1099
    TAOS_RETURN(code);
×
1100
  }
1101

1102
  if (pEntry->version >= version) {
32!
1103
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1104
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1105
    metaFetchEntryFree(&pEntry);
×
1106
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1107
  }
1108

1109
  // search the column to update
1110
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
32✔
1111
  SSchema        *pColumn = NULL;
32✔
1112
  int32_t         iColumn = 0;
32✔
1113
  for (int32_t i = 0; i < pSchema->nCols; i++) {
126!
1114
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
126✔
1115
      pColumn = &pSchema->pSchema[i];
32✔
1116
      iColumn = i;
32✔
1117
      break;
32✔
1118
    }
1119
  }
1120

1121
  if (NULL == pColumn) {
32!
1122
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1123
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1124
    metaFetchEntryFree(&pEntry);
×
1125
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1126
  }
1127

1128
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
32✔
1129
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
11!
1130
              __func__, __FILE__, __LINE__, pColumn->name, version);
1131
    metaFetchEntryFree(&pEntry);
11✔
1132
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
11✔
1133
  }
1134

1135
  // do update column name
1136
  pEntry->version = version;
21✔
1137
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
21✔
1138
  pSchema->version++;
21✔
1139

1140
  // do handle entry
1141
  code = metaHandleEntry2(pMeta, pEntry);
21✔
1142
  if (code) {
21!
1143
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1144
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1145
    metaFetchEntryFree(&pEntry);
×
1146
    TAOS_RETURN(code);
×
1147
  } else {
1148
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
21!
1149
             pEntry->uid, version);
1150
  }
1151

1152
  // build response
1153
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
21!
1154
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1155
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1156
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1157
    } else {
1158
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1159
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1160
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1161
        pRsp->pColRefs[i].id = p->id;
×
1162
        if (p->hasRef) {
×
1163
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1164
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1165
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1166
        }
1167
      }
1168
    }
1169
  } else {
1170
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
21!
1171
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1172
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1173
    } else {
1174
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
121✔
1175
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
100✔
1176
        pRsp->pSchemaExt[i].colId = p->id;
100✔
1177
        pRsp->pSchemaExt[i].compress = p->alg;
100✔
1178
      }
1179
    }
1180
  }
1181

1182
  metaFetchEntryFree(&pEntry);
21✔
1183
  TAOS_RETURN(code);
21✔
1184
}
1185

1186
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
42✔
1187
  int32_t code = TSDB_CODE_SUCCESS;
42✔
1188

1189
  // check request
1190
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
42✔
1191
  if (code) {
42!
1192
    TAOS_RETURN(code);
×
1193
  }
1194

1195
  // fetch old entry
1196
  SMetaEntry *pEntry = NULL;
42✔
1197
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
42✔
1198
  if (code) {
42!
1199
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1200
              __FILE__, __LINE__, pReq->tbName, version);
1201
    TAOS_RETURN(code);
×
1202
  }
1203

1204
  if (pEntry->version >= version) {
42!
1205
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1206
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1207
    metaFetchEntryFree(&pEntry);
×
1208
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1209
  }
1210

1211
  // search the column to update
1212
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
42✔
1213
  SSchema        *pColumn = NULL;
42✔
1214
  int32_t         iColumn = 0;
42✔
1215
  int32_t         rowSize = 0;
42✔
1216
  for (int32_t i = 0; i < pSchema->nCols; i++) {
200✔
1217
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
158✔
1218
      pColumn = &pSchema->pSchema[i];
42✔
1219
      iColumn = i;
42✔
1220
    }
1221
    rowSize += pSchema->pSchema[i].bytes;
158✔
1222
  }
1223

1224
  if (NULL == pColumn) {
42!
1225
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1226
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1227
    metaFetchEntryFree(&pEntry);
×
1228
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1229
  }
1230

1231
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
42!
1232
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
×
1233
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1234
              version);
1235
    metaFetchEntryFree(&pEntry);
×
1236
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1237
  }
1238

1239
  if (tqCheckColModifiable(pMeta->pVnode->pTq, pEntry->uid, pColumn->colId) != 0) {
42✔
1240
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
13!
1241
              __func__, __FILE__, __LINE__, pReq->colName, version);
1242
    metaFetchEntryFree(&pEntry);
13✔
1243
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
13✔
1244
  }
1245

1246
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
29!
1247
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1248
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, TSDB_MAX_BYTES_PER_ROW,
1249
              version);
1250
    metaFetchEntryFree(&pEntry);
×
1251
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
×
1252
  }
1253

1254
  // do change the column bytes
1255
  pEntry->version = version;
29✔
1256
  pSchema->version++;
29✔
1257
  pColumn->bytes = pReq->colModBytes;
29✔
1258

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

1271
  // build response
1272
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
29!
1273
    if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1274
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1275
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1276
    } else {
1277
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1278
        SColRef *p = &pEntry->colRef.pColRef[i];
×
1279
        pRsp->pColRefs[i].hasRef = p->hasRef;
×
1280
        pRsp->pColRefs[i].id = p->id;
×
1281
        if (p->hasRef) {
×
1282
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1283
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1284
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1285
        }
1286
      }
1287
    }
1288
  } else {
1289
    if (metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp) < 0) {
29!
1290
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1291
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1292
    } else {
1293
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
137✔
1294
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
108✔
1295
        pRsp->pSchemaExt[i].colId = p->id;
108✔
1296
        pRsp->pSchemaExt[i].compress = p->alg;
108✔
1297
      }
1298
    }
1299
  }
1300

1301
  metaFetchEntryFree(&pEntry);
29✔
1302
  TAOS_RETURN(code);
29✔
1303
}
1304

1305
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
654✔
1306
  int32_t code = 0;
654✔
1307

1308
  // check tag name
1309
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
654!
1310
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1311
              __func__, __FILE__, __LINE__, pReq->tagName, version);
1312
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1313
  }
1314

1315
  // check name
1316
  void   *value = NULL;
654✔
1317
  int32_t valueSize = 0;
654✔
1318
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
654✔
1319
  if (code) {
654✔
1320
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1!
1321
              __FILE__, __LINE__, pReq->tbName, version);
1322
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1✔
1323
    TAOS_RETURN(code);
1✔
1324
  }
1325
  tdbFreeClear(value);
653✔
1326

1327
  TAOS_RETURN(code);
653✔
1328
}
1329

1330
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
654✔
1331
  int32_t code = TSDB_CODE_SUCCESS;
654✔
1332

1333
  // check request
1334
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
654✔
1335
  if (code) {
654✔
1336
    TAOS_RETURN(code);
1✔
1337
  }
1338

1339
  // fetch child entry
1340
  SMetaEntry *pChild = NULL;
653✔
1341
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
653✔
1342
  if (code) {
653!
1343
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1344
              __FILE__, __LINE__, pReq->tbName, version);
1345
    TAOS_RETURN(code);
×
1346
  }
1347

1348
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
653!
1349
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1350
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1351
    metaFetchEntryFree(&pChild);
×
1352
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1353
  }
1354

1355
  // fetch super entry
1356
  SMetaEntry *pSuper = NULL;
653✔
1357
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
653✔
1358
  if (code) {
653!
1359
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1360
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1361
    metaFetchEntryFree(&pChild);
×
1362
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1363
  }
1364

1365
  // search the tag to update
1366
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
653✔
1367
  SSchema        *pColumn = NULL;
653✔
1368
  int32_t         iColumn = 0;
653✔
1369
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
1,548!
1370
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
1,548✔
1371
      pColumn = &pTagSchema->pSchema[i];
653✔
1372
      iColumn = i;
653✔
1373
      break;
653✔
1374
    }
1375
  }
1376

1377
  if (NULL == pColumn) {
653!
1378
    metaError("vgId:%d, %s failed at %s:%d since tag %s not found in table %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1379
              __func__, __FILE__, __LINE__, pReq->tagName, pReq->tbName, version);
1380
    metaFetchEntryFree(&pChild);
×
1381
    metaFetchEntryFree(&pSuper);
×
1382
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1383
  }
1384

1385
  // do change tag value
1386
  pChild->version = version;
653✔
1387
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
653✔
1388
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
17!
1389
    if (NULL == pNewTag) {
17!
1390
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1391
                __LINE__, tstrerror(terrno), version);
1392
      metaFetchEntryFree(&pChild);
×
1393
      metaFetchEntryFree(&pSuper);
×
1394
      TAOS_RETURN(terrno);
×
1395
    }
1396
    pChild->ctbEntry.pTags = pNewTag;
17✔
1397
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
17✔
1398
  } else {
1399
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
636✔
1400

1401
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
636✔
1402
    if (NULL == pTagArray) {
636!
1403
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1404
                __LINE__, tstrerror(terrno), version);
1405
      metaFetchEntryFree(&pChild);
×
1406
      metaFetchEntryFree(&pSuper);
×
1407
      TAOS_RETURN(terrno);
×
1408
    }
1409

1410
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
2,705✔
1411
      STagVal value = {
2,069✔
1412
          .type = pTagSchema->pSchema[i].type,
2,069✔
1413
          .cid = pTagSchema->pSchema[i].colId,
2,069✔
1414
      };
1415

1416
      if (iColumn == i) {
2,069✔
1417
        if (pReq->isNull) {
636✔
1418
          continue;
413✔
1419
        }
1420
        if (IS_VAR_DATA_TYPE(value.type)) {
589!
1421
          value.pData = pReq->pTagVal;
205✔
1422
          value.nData = pReq->nTagVal;
205✔
1423
        } else {
1424
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
384✔
1425
        }
1426
      } else if (!tTagGet(pOldTag, &value)) {
1,433✔
1427
        continue;
366✔
1428
      }
1429

1430
      if (NULL == taosArrayPush(pTagArray, &value)) {
1,656!
1431
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1432
                  __LINE__, tstrerror(terrno), version);
1433
        taosArrayDestroy(pTagArray);
×
1434
        metaFetchEntryFree(&pChild);
×
1435
        metaFetchEntryFree(&pSuper);
×
1436
        TAOS_RETURN(terrno);
×
1437
      }
1438
    }
1439

1440
    STag *pNewTag = NULL;
636✔
1441
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
636✔
1442
    if (code) {
636!
1443
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1444
                __LINE__, tstrerror(code), version);
1445
      taosArrayDestroy(pTagArray);
×
1446
      metaFetchEntryFree(&pChild);
×
1447
      metaFetchEntryFree(&pSuper);
×
1448
      TAOS_RETURN(code);
×
1449
    }
1450
    taosArrayDestroy(pTagArray);
636✔
1451
    taosMemoryFree(pChild->ctbEntry.pTags);
636!
1452
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
636✔
1453
  }
1454

1455
  // do handle entry
1456
  code = metaHandleEntry2(pMeta, pChild);
653✔
1457
  if (code) {
653!
1458
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1459
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1460
    metaFetchEntryFree(&pChild);
×
1461
    metaFetchEntryFree(&pSuper);
×
1462
    TAOS_RETURN(code);
×
1463
  } else {
1464
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
653!
1465
             pChild->uid, version);
1466
  }
1467

1468
  // free resource and return
1469
  metaFetchEntryFree(&pChild);
653✔
1470
  metaFetchEntryFree(&pSuper);
653✔
1471
  TAOS_RETURN(code);
653✔
1472
}
1473

1474
static int32_t metaCheckUpdateTableMultiTagValueReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1475
  int32_t code = 0;
5✔
1476

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

1484
  // check name
1485
  void   *value = NULL;
5✔
1486
  int32_t valueSize = 0;
5✔
1487
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
5✔
1488
  if (code) {
5!
1489
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1490
              __FILE__, __LINE__, pReq->tbName, version);
1491
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1492
    TAOS_RETURN(code);
×
1493
  }
1494
  tdbFreeClear(value);
5✔
1495

1496
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
5!
1497
    metaError("vgId:%d, %s failed at %s:%d since invalid tag name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1498
              __FILE__, __LINE__, version);
1499
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1500
  }
1501

1502
  TAOS_RETURN(code);
5✔
1503
}
1504

1505
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1506
  int32_t code = TSDB_CODE_SUCCESS;
5✔
1507

1508
  code = metaCheckUpdateTableMultiTagValueReq(pMeta, version, pReq);
5✔
1509
  if (code) {
5!
1510
    TAOS_RETURN(code);
×
1511
  }
1512

1513
  // fetch child entry
1514
  SMetaEntry *pChild = NULL;
5✔
1515
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
5✔
1516
  if (code) {
5!
1517
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1518
              __FILE__, __LINE__, pReq->tbName, version);
1519
    TAOS_RETURN(code);
×
1520
  }
1521

1522
  if (pChild->type != TSDB_CHILD_TABLE) {
5!
1523
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64,
×
1524
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
1525
    metaFetchEntryFree(&pChild);
×
1526
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1527
  }
1528

1529
  // fetch super entry
1530
  SMetaEntry *pSuper = NULL;
5✔
1531
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
5✔
1532
  if (code) {
5!
1533
    metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1534
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
1535
    metaFetchEntryFree(&pChild);
×
1536
    TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1537
  }
1538

1539
  // search the tags to update
1540
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
5✔
1541

1542
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
5!
1543
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1544
              __func__, __FILE__, __LINE__, pReq->tbName, version);
1545
    metaFetchEntryFree(&pChild);
×
1546
    metaFetchEntryFree(&pSuper);
×
1547
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1548
  }
1549

1550
  // do check if tag name exists
1551
  SHashObj *pTagTable =
1552
      taosHashInit(pTagSchema->nCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5✔
1553
  if (pTagTable == NULL) {
5!
1554
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1555
              __LINE__, tstrerror(terrno), version);
1556
    metaFetchEntryFree(&pChild);
×
1557
    metaFetchEntryFree(&pSuper);
×
1558
    TAOS_RETURN(terrno);
×
1559
  }
1560

1561
  for (int32_t i = 0; i < taosArrayGetSize(pReq->pMultiTag); i++) {
35✔
1562
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
30✔
1563
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
30!
1564
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1565
                __LINE__, tstrerror(terrno), version);
1566
      taosHashCleanup(pTagTable);
×
1567
      metaFetchEntryFree(&pChild);
×
1568
      metaFetchEntryFree(&pSuper);
×
1569
      TAOS_RETURN(terrno);
×
1570
    }
1571
  }
1572

1573
  int32_t numOfChangedTags = 0;
5✔
1574
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1575
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
35✔
1576
        ? numOfChangedTags++
30✔
1577
        : 0;
35✔
1578
  }
1579
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
5!
1580
    metaError("vgId:%d, %s failed at %s:%d since tag count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1581
              __FILE__, __LINE__, version);
1582
    taosHashCleanup(pTagTable);
×
1583
    metaFetchEntryFree(&pChild);
×
1584
    metaFetchEntryFree(&pSuper);
×
1585
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1586
  }
1587

1588
  // do change tag value
1589
  pChild->version = version;
5✔
1590
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
5✔
1591
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
5✔
1592
  if (NULL == pTagArray) {
5!
1593
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1594
              __LINE__, tstrerror(terrno), version);
1595
    taosHashCleanup(pTagTable);
×
1596
    metaFetchEntryFree(&pChild);
×
1597
    metaFetchEntryFree(&pSuper);
×
1598
    TAOS_RETURN(terrno);
×
1599
  }
1600

1601
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
40✔
1602
    SSchema *pCol = &pTagSchema->pSchema[i];
35✔
1603
    STagVal  value = {
35✔
1604
         .cid = pCol->colId,
35✔
1605
    };
1606

1607
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
35✔
1608
    if (pTagVal == NULL) {
35✔
1609
      if (!tTagGet(pOldTag, &value)) {
5!
1610
        continue;
6✔
1611
      }
1612
    } else {
1613
      value.type = pCol->type;
30✔
1614
      if (pTagVal->isNull) {
30✔
1615
        continue;
6✔
1616
      }
1617

1618
      if (IS_VAR_DATA_TYPE(pCol->type)) {
24!
1619
        value.pData = pTagVal->pTagVal;
4✔
1620
        value.nData = pTagVal->nTagVal;
4✔
1621
      } else {
1622
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
20✔
1623
      }
1624
    }
1625

1626
    if (taosArrayPush(pTagArray, &value) == NULL) {
29!
1627
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1628
                __LINE__, tstrerror(terrno), version);
1629
      taosHashCleanup(pTagTable);
×
1630
      taosArrayDestroy(pTagArray);
×
1631
      metaFetchEntryFree(&pChild);
×
1632
      metaFetchEntryFree(&pSuper);
×
1633
      TAOS_RETURN(terrno);
×
1634
    }
1635
  }
1636

1637
  STag *pNewTag = NULL;
5✔
1638
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
5✔
1639
  if (code) {
5!
1640
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1641
              __LINE__, tstrerror(code), version);
1642
    taosHashCleanup(pTagTable);
×
1643
    taosArrayDestroy(pTagArray);
×
1644
    metaFetchEntryFree(&pChild);
×
1645
    metaFetchEntryFree(&pSuper);
×
1646
    TAOS_RETURN(code);
×
1647
  }
1648
  taosArrayDestroy(pTagArray);
5✔
1649
  taosMemoryFree(pChild->ctbEntry.pTags);
5!
1650
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
5✔
1651

1652
  // do handle entry
1653
  code = metaHandleEntry2(pMeta, pChild);
5✔
1654
  if (code) {
5!
1655
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1656
              __func__, __FILE__, __LINE__, tstrerror(code), pChild->uid, pReq->tbName, version);
1657
    taosHashCleanup(pTagTable);
×
1658
    metaFetchEntryFree(&pChild);
×
1659
    metaFetchEntryFree(&pSuper);
×
1660
    TAOS_RETURN(code);
×
1661
  } else {
1662
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5!
1663
             pChild->uid, version);
1664
  }
1665

1666
  taosHashCleanup(pTagTable);
5✔
1667
  metaFetchEntryFree(&pChild);
5✔
1668
  metaFetchEntryFree(&pSuper);
5✔
1669
  TAOS_RETURN(code);
5✔
1670
}
1671

1672
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
78✔
1673
  int32_t code = TSDB_CODE_SUCCESS;
78✔
1674

1675
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
78!
1676
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1677
              __FILE__, __LINE__, version);
1678
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1679
  }
1680

1681
  return code;
78✔
1682
}
1683

1684
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
78✔
1685
  int32_t code = 0;
78✔
1686

1687
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
78✔
1688
  if (code) {
78!
1689
    TAOS_RETURN(code);
×
1690
  }
1691

1692
  // fetch entry
1693
  SMetaEntry *pEntry = NULL;
78✔
1694
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
78✔
1695
  if (code) {
78!
1696
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1697
              __FILE__, __LINE__, pReq->tbName, version);
1698
    TAOS_RETURN(code);
×
1699
  }
1700

1701
  // do change the entry
1702
  pEntry->version = version;
78✔
1703
  if (pEntry->type == TSDB_CHILD_TABLE) {
78✔
1704
    if (pReq->updateTTL) {
40✔
1705
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
15✔
1706
    }
1707
    if (pReq->newCommentLen >= 0) {
40✔
1708
      char *pNewComment = NULL;
25✔
1709
      if (pReq->newCommentLen) {
25✔
1710
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
20!
1711
        if (NULL == pNewComment) {
20!
1712
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1713
                    __LINE__, tstrerror(terrno), version);
1714
          metaFetchEntryFree(&pEntry);
×
1715
          TAOS_RETURN(terrno);
×
1716
        }
1717
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
20✔
1718
      } else {
1719
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
5!
1720
      }
1721
      pEntry->ctbEntry.comment = pNewComment;
25✔
1722
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
25✔
1723
    }
1724
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
38!
1725
    if (pReq->updateTTL) {
38✔
1726
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
12✔
1727
    }
1728
    if (pReq->newCommentLen >= 0) {
38✔
1729
      char *pNewComment = NULL;
26✔
1730
      if (pReq->newCommentLen > 0) {
26✔
1731
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
21!
1732
        if (NULL == pNewComment) {
21!
1733
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1734
                    __LINE__, tstrerror(terrno), version);
1735
          metaFetchEntryFree(&pEntry);
×
1736
          TAOS_RETURN(terrno);
×
1737
        }
1738
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
21✔
1739
      } else {
1740
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
5!
1741
      }
1742
      pEntry->ntbEntry.comment = pNewComment;
26✔
1743
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
26✔
1744
    }
1745
  } else {
1746
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1747
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1748
    metaFetchEntryFree(&pEntry);
×
1749
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1750
  }
1751

1752
  // do handle entry
1753
  code = metaHandleEntry2(pMeta, pEntry);
78✔
1754
  if (code) {
78!
1755
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1756
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1757
    metaFetchEntryFree(&pEntry);
×
1758
    TAOS_RETURN(code);
×
1759
  } else {
1760
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
78!
1761
             pEntry->uid, version);
1762
  }
1763

1764
  metaFetchEntryFree(&pEntry);
78✔
1765
  TAOS_RETURN(code);
78✔
1766
}
1767

1768
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5✔
1769
  int32_t code = TSDB_CODE_SUCCESS;
5✔
1770

1771
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
5!
1772
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1773
              __FILE__, __LINE__, version);
1774
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1775
  }
1776

1777
  SMetaEntry *pEntry = NULL;
5✔
1778
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
5✔
1779
  if (code) {
5!
1780
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1781
              __FILE__, __LINE__, pReq->tbName, version);
1782
    TAOS_RETURN(code);
×
1783
  }
1784

1785
  if (pEntry->version >= version) {
5!
1786
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1787
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1788
    metaFetchEntryFree(&pEntry);
×
1789
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1790
  }
1791

1792
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
5!
1793
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1794
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
1795
    metaFetchEntryFree(&pEntry);
×
1796
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1797
  }
1798

1799
  // do change the entry
1800
  int8_t           updated = 0;
5✔
1801
  SColCmprWrapper *wp = &pEntry->colCmpr;
5✔
1802
  for (int32_t i = 0; i < wp->nCols; i++) {
40✔
1803
    SColCmpr *p = &wp->pColCmpr[i];
35✔
1804
    if (p->id == pReq->colId) {
35✔
1805
      uint32_t dst = 0;
5✔
1806
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
5✔
1807
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1808
      if (updated > 0) {
5!
1809
        p->alg = dst;
5✔
1810
      }
1811
    }
1812
  }
1813

1814
  if (updated == 0) {
5!
1815
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
1816
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
1817
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1818
    metaFetchEntryFree(&pEntry);
×
1819
    TAOS_RETURN(code);
×
1820
  } else if (updated < 0) {
5!
1821
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
1822
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
1823
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
1824
    metaFetchEntryFree(&pEntry);
×
1825
    TAOS_RETURN(code);
×
1826
  }
1827

1828
  pEntry->version = version;
5✔
1829

1830
  // do handle entry
1831
  code = metaHandleEntry2(pMeta, pEntry);
5✔
1832
  if (code) {
5!
1833
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1834
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1835
    metaFetchEntryFree(&pEntry);
×
1836
    TAOS_RETURN(code);
×
1837
  } else {
1838
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
5!
1839
             pEntry->uid, version);
1840
  }
1841

1842
  metaFetchEntryFree(&pEntry);
5✔
1843
  TAOS_RETURN(code);
5✔
1844
}
1845

1846
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1847
  int32_t code = TSDB_CODE_SUCCESS;
×
1848

1849
  // check request
1850
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1851
  if (code) {
×
1852
    TAOS_RETURN(code);
×
1853
  }
1854

1855
  if (NULL == pReq->refDbName) {
×
1856
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1857
              __func__, __FILE__, __LINE__, version);
1858
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1859
  }
1860

1861
  if (NULL == pReq->refTbName) {
×
1862
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1863
              __func__, __FILE__, __LINE__, version);
1864
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1865
  }
1866

1867
  if (NULL == pReq->refColName) {
×
1868
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1869
              __func__, __FILE__, __LINE__, version);
1870
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1871
  }
1872

1873

1874
  // fetch old entry
1875
  SMetaEntry *pEntry = NULL;
×
1876
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1877
  if (code) {
×
1878
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1879
              __FILE__, __LINE__, pReq->tbName, version);
1880
    TAOS_RETURN(code);
×
1881
  }
1882

1883
  if (pEntry->version >= version) {
×
1884
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1885
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1886
    metaFetchEntryFree(&pEntry);
×
1887
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1888
  }
1889

1890
  // fetch super entry
1891
  SMetaEntry *pSuper = NULL;
×
1892
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
×
1893
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
×
1894
    if (code) {
×
1895
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1896
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1897
      metaFetchEntryFree(&pEntry);
×
1898
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
1899
    }
1900
  }
1901

1902
  // search the column to update
1903
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
×
1904
  SColRef        *pColRef = NULL;
×
1905
  int32_t         iColumn = 0;
×
1906
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1907
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1908
      pColRef = &pEntry->colRef.pColRef[i];
×
1909
      iColumn = i;
×
1910
      break;
×
1911
    }
1912
  }
1913

1914
  if (NULL == pColRef) {
×
1915
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1916
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1917
    metaFetchEntryFree(&pEntry);
×
1918
    metaFetchEntryFree(&pSuper);
×
1919
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1920
  }
1921

1922
  // do update column name
1923
  pEntry->version = version;
×
1924
  pColRef->hasRef = true;
×
1925
  pColRef->id = pSchema->pSchema[iColumn].colId;
×
1926
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
×
1927
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
×
1928
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
×
1929
  pSchema->version++;
×
1930

1931
  // do handle entry
1932
  code = metaHandleEntry2(pMeta, pEntry);
×
1933
  if (code) {
×
1934
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1935
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1936
    metaFetchEntryFree(&pEntry);
×
1937
    metaFetchEntryFree(&pSuper);
×
1938
    TAOS_RETURN(code);
×
1939
  } else {
1940
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
1941
             pEntry->uid, version);
1942
  }
1943

1944
  // build response
1945
  if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
1946
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1947
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1948
  } else {
1949
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
1950
      SColRef *p = &pEntry->colRef.pColRef[i];
×
1951
      pRsp->pColRefs[i].hasRef = p->hasRef;
×
1952
      pRsp->pColRefs[i].id = p->id;
×
1953
      if (p->hasRef) {
×
1954
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
1955
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
1956
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
1957
      }
1958
    }
1959
  }
1960

1961
  metaFetchEntryFree(&pEntry);
×
1962
  metaFetchEntryFree(&pSuper);
×
1963
  TAOS_RETURN(code);
×
1964
}
1965

1966
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
×
1967
  int32_t code = TSDB_CODE_SUCCESS;
×
1968

1969
  // check request
1970
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
×
1971
  if (code) {
×
1972
    TAOS_RETURN(code);
×
1973
  }
1974

1975
  // fetch old entry
1976
  SMetaEntry *pEntry = NULL;
×
1977
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1978
  if (code) {
×
1979
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1980
              __FILE__, __LINE__, pReq->tbName, version);
1981
    TAOS_RETURN(code);
×
1982
  }
1983

1984
  if (pEntry->version >= version) {
×
1985
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1986
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1987
    metaFetchEntryFree(&pEntry);
×
1988
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1989
  }
1990

1991
  // fetch super entry
1992
  SMetaEntry *pSuper = NULL;
×
1993
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
×
1994
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
×
1995
    if (code) {
×
1996
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
1997
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
1998
      metaFetchEntryFree(&pEntry);
×
1999
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2000
    }
2001
  }
2002

2003
  // search the column to update
2004
  SSchemaWrapper *pSchema = pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
×
2005
  SColRef        *pColRef = NULL;
×
2006
  int32_t         iColumn = 0;
×
2007
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
2008
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
2009
      pColRef = &pEntry->colRef.pColRef[i];
×
2010
      iColumn = i;
×
2011
      break;
×
2012
    }
2013
  }
2014

2015
  if (NULL == pColRef) {
×
2016
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2017
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2018
    metaFetchEntryFree(&pEntry);
×
2019
    metaFetchEntryFree(&pSuper);
×
2020
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2021
  }
2022

2023
  // do update column name
2024
  pEntry->version = version;
×
2025
  pColRef->hasRef = false;
×
2026
  pColRef->id = pSchema->pSchema[iColumn].colId;
×
2027
  pSchema->version++;
×
2028

2029
  // do handle entry
2030
  code = metaHandleEntry2(pMeta, pEntry);
×
2031
  if (code) {
×
2032
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2033
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2034
    metaFetchEntryFree(&pEntry);
×
2035
    metaFetchEntryFree(&pSuper);
×
2036
    TAOS_RETURN(code);
×
2037
  } else {
2038
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
×
2039
             pEntry->uid, version);
2040
  }
2041

2042
  // build response
2043
  if (metaUpdateVtbMetaRsp(pEntry->uid, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type) < 0) {
×
2044
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2045
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2046
  } else {
2047
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
×
2048
      SColRef *p = &pEntry->colRef.pColRef[i];
×
2049
      pRsp->pColRefs[i].hasRef = p->hasRef;
×
2050
      pRsp->pColRefs[i].id = p->id;
×
2051
      if (p->hasRef) {
×
2052
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
×
2053
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
×
2054
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
×
2055
      }
2056
    }
2057
  }
2058

2059
  metaFetchEntryFree(&pEntry);
×
2060
  metaFetchEntryFree(&pSuper);
×
2061
  TAOS_RETURN(code);
×
2062
}
2063

2064
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
965✔
2065
  int32_t code = TSDB_CODE_SUCCESS;
965✔
2066

2067
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
965!
2068
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2069
              __FILE__, __LINE__, version);
2070
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2071
  }
2072

2073
  SMetaEntry *pEntry = NULL;
965✔
2074
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
965✔
2075
  if (code) {
965!
2076
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2077
              __FILE__, __LINE__, pReq->name, version);
2078
    TAOS_RETURN(code);
×
2079
  }
2080

2081
  if (pEntry->type != TSDB_SUPER_TABLE) {
965!
2082
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2083
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2084
    metaFetchEntryFree(&pEntry);
×
2085
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2086
  }
2087

2088
  if (pEntry->uid != pReq->suid) {
965!
2089
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2090
              ", version:%" PRId64,
2091
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2092
    metaFetchEntryFree(&pEntry);
×
2093
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2094
  }
2095

2096
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2097
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2098
  //   PRId64,
2099
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2100
  //             pReq->schemaTag.version, version);
2101
  //   metaFetchEntryFree(&pEntry);
2102
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2103
  // }
2104

2105
  // do change the entry
2106
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
965✔
2107
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
965✔
2108
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
965!
2109
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2110
              __func__, __FILE__, __LINE__, pReq->name, version);
2111
    metaFetchEntryFree(&pEntry);
×
2112
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2113
  }
2114

2115
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
965!
2116
    metaError(
×
2117
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2118
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2119
        version);
2120
    metaFetchEntryFree(&pEntry);
×
2121
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2122
  }
2123

2124
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2125
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2126
  //   PRId64,
2127
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2128
  //             pNewTagSchema->version, version);
2129
  //   metaFetchEntryFree(&pEntry);
2130
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2131
  // }
2132

2133
  int32_t numOfChangedTags = 0;
965✔
2134
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
15,270✔
2135
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
14,305✔
2136
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
14,305✔
2137

2138
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
14,305!
2139
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
14,305!
2140
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2141
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2142
      metaFetchEntryFree(&pEntry);
×
2143
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2144
    }
2145

2146
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
14,305✔
2147
      numOfChangedTags++;
965✔
2148
      SSCHMEA_SET_IDX_ON(pOldColumn);
965✔
2149
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
13,340!
2150
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2151
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2152
      metaFetchEntryFree(&pEntry);
×
2153
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2154
    }
2155
  }
2156

2157
  if (numOfChangedTags != 1) {
965!
2158
    metaError(
×
2159
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2160
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2161
    metaFetchEntryFree(&pEntry);
×
2162
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2163
  }
2164

2165
  pEntry->version = version;
965✔
2166
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
965✔
2167

2168
  // do handle the entry
2169
  code = metaHandleEntry2(pMeta, pEntry);
965✔
2170
  if (code) {
965!
2171
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2172
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2173
    metaFetchEntryFree(&pEntry);
×
2174
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2175
  } else {
2176
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
965!
2177
             pEntry->uid, version);
2178
  }
2179

2180
  metaFetchEntryFree(&pEntry);
965✔
2181
  TAOS_RETURN(code);
965✔
2182
}
2183

2184
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,150✔
2185
  int32_t code = TSDB_CODE_SUCCESS;
2,150✔
2186

2187
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
2,150!
2188
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2189
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2190
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2191
  }
2192

2193
  SMetaEntry *pEntry = NULL;
2,150✔
2194
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
2,150✔
2195
  if (code) {
2,150!
2196
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2197
              __FILE__, __LINE__, pReq->stb, version);
2198
    TAOS_RETURN(code);
×
2199
  }
2200

2201
  if (TSDB_SUPER_TABLE != pEntry->type) {
2,150!
2202
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2203
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2204
    metaFetchEntryFree(&pEntry);
×
2205
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2206
  }
2207

2208
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
2,150✔
2209
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
2,150!
2210
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2211
              __func__, __FILE__, __LINE__, pReq->stb, version);
2212
    metaFetchEntryFree(&pEntry);
×
2213
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2214
  }
2215

2216
  // search and set the tag index off
2217
  int32_t numOfChangedTags = 0;
2,150✔
2218
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
6,722!
2219
    SSchema *pCol = pTagSchema->pSchema + i;
6,722✔
2220
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
6,722✔
2221
      if (!IS_IDX_ON(pCol)) {
2,150!
2222
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2223
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2224
        metaFetchEntryFree(&pEntry);
×
2225
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2226
      }
2227
      numOfChangedTags++;
2,150✔
2228
      SSCHMEA_SET_IDX_OFF(pCol);
2,150✔
2229
      break;
2,150✔
2230
    }
2231
  }
2232

2233
  if (numOfChangedTags != 1) {
2,150!
2234
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2235
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2236
    metaFetchEntryFree(&pEntry);
×
2237
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2238
  }
2239

2240
  // do handle the entry
2241
  pEntry->version = version;
2,150✔
2242
  pTagSchema->version++;
2,150✔
2243
  code = metaHandleEntry2(pMeta, pEntry);
2,150✔
2244
  if (code) {
2,150!
2245
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2246
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2247
    metaFetchEntryFree(&pEntry);
×
2248
    TAOS_RETURN(code);
×
2249
  } else {
2250
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
2,150!
2251
             pEntry->uid, version);
2252
  }
2253

2254
  metaFetchEntryFree(&pEntry);
2,150✔
2255
  TAOS_RETURN(code);
2,150✔
2256
}
2257

2258
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,411✔
2259
  int32_t code = TSDB_CODE_SUCCESS;
7,411✔
2260

2261
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,411!
2262
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2263
              __FILE__, __LINE__, version);
2264
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2265
  }
2266

2267
  SMetaEntry *pEntry = NULL;
7,419✔
2268
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,419✔
2269
  if (code) {
7,411!
2270
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2271
              __FILE__, __LINE__, pReq->name, version);
2272
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2273
  }
2274

2275
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,413!
2276
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2277
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2278
    metaFetchEntryFree(&pEntry);
×
2279
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2280
  }
2281

2282
  SMetaEntry entry = {
7,413✔
2283
      .version = version,
2284
      .type = TSDB_SUPER_TABLE,
2285
      .uid = pReq->suid,
7,413✔
2286
      .name = pReq->name,
7,413✔
2287
      .stbEntry.schemaRow = pReq->schemaRow,
2288
      .stbEntry.schemaTag = pReq->schemaTag,
2289
      .stbEntry.keep = pReq->keep,
7,413✔
2290
      .colCmpr = pReq->colCmpr,
2291
      .pExtSchemas = pReq->pExtSchemas,
7,413✔
2292
  };
2293
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,413✔
2294
  if (pReq->virtualStb) {
7,413!
2295
    TABLE_SET_VIRTUAL(entry.flags);
×
2296
  }
2297

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

2310
  metaFetchEntryFree(&pEntry);
7,429✔
2311
  TAOS_RETURN(code);
7,428✔
2312
}
2313

2314
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2315
  int32_t code = 0;
×
2316

2317
  if (taosArrayGetSize(uidArray) == 0) {
×
2318
    return TSDB_CODE_SUCCESS;
×
2319
  }
2320

2321
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2322
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2323
    SMetaInfo info;
2324
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2325
    if (code) {
×
2326
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2327
                __func__, __FILE__, __LINE__, uid, code);
2328
      return code;
×
2329
    }
2330

2331
    SMetaEntry entry = {
×
2332
        .version = version,
2333
        .uid = uid,
2334
    };
2335

2336
    if (info.suid == 0) {
×
2337
      entry.type = -TSDB_NORMAL_TABLE;
×
2338
    } else if (info.suid == uid) {
×
2339
      entry.type = -TSDB_SUPER_TABLE;
×
2340
    } else {
2341
      entry.type = -TSDB_CHILD_TABLE;
×
2342
    }
2343
    code = metaHandleEntry2(pMeta, &entry);
×
2344
    if (code) {
×
2345
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2346
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
2347
      return code;
×
2348
    }
2349
  }
2350
  return code;
×
2351
}
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