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

taosdata / TDengine / #3844

10 Apr 2025 08:48AM UTC coverage: 43.852% (-19.2%) from 63.077%
#3844

push

travis-ci

web-flow
fix: the REPLICA parameter supports plural forms when used to create and alter a database (#30732)

104394 of 310659 branches covered (33.6%)

Branch coverage included in aggregate %.

167442 of 309240 relevant lines covered (54.15%)

3866624.94 hits per line

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

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

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

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

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

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

71
  // check suid
72
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
44!
73
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
×
74
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
75
    return TSDB_CODE_INVALID_MSG;
×
76
  }
77

78
  return TSDB_CODE_SUCCESS;
44✔
79
}
80

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

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

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

117
  return code;
×
118
}
119

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

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

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

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

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

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

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

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

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

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

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

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

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

242
// Alter Super Table
243

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

251
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
2,002!
252
      pReq->ctb.suid == 0) {
2,002!
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) {
2,002!
260
    pReq->uid = *(int64_t *)value;
×
261
    tdbFreeClear(value);
×
262

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

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

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

286
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
287
  }
288

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

298
  if (pStbEntry->type != TSDB_SUPER_TABLE) {
2,002!
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) {
2,002!
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;
2,002✔
316
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
2,002✔
317
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
2,002!
318
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
4,004✔
319
      STagVal tagVal = {
2,002✔
320
          .cid = pTagSchema->pSchema[i].colId,
2,002✔
321
      };
322

323
      if (tTagGet(pTag, &tagVal)) {
2,002!
324
        if (pTagSchema->pSchema[i].type != tagVal.type) {
2,002!
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);
2,002✔
335

336
  // check grant
337
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
2,000!
338
    code = grantCheck(TSDB_GRANT_TIMESERIES);
2,000✔
339
    if (TSDB_CODE_SUCCESS != code) {
2,000!
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;
2,000✔
345
}
346

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

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

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

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

364
  return code;
2,000✔
365
}
366

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

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

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

393
  // build response
394
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
2,000✔
395
  if (code) {
2,002!
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);
2,002✔
402
  if (TSDB_CODE_SUCCESS == code) {
2,002!
403
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
2,002!
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;
2,002✔
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) {
×
419
  int32_t code = 0;
×
420
  void   *value = NULL;
×
421
  int32_t valueSize = 0;
×
422

423
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
×
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) {
×
431
    // for auto create table, we return the uid of the existing table
432
    pReq->uid = *(tb_uid_t *)value;
×
433
    tdbFree(value);
×
434
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
435
  }
436

437
  // grant check
438
  code = grantCheck(TSDB_GRANT_TIMESERIES);
×
439
  if (code) {
×
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;
×
444
}
445

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

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

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

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

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

473
  return code;
×
474
}
475

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

479
  // check request
480
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
×
481
  if (code) {
×
482
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
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);
×
487
  }
488

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

505
  // build response
506
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
×
507
  if (code) {
×
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);
×
514
  if (TSDB_CODE_SUCCESS == code) {
×
515
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
×
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);
×
522
}
523

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

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

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

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

542
  return code;
×
543
}
544

545
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
×
546
  // check request
547
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
×
548
  if (code) {
×
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 = {
×
557
      .version = version,
558
      .type = TSDB_VIRTUAL_NORMAL_TABLE,
559
      .uid = pReq->uid,
×
560
      .name = pReq->name,
×
561
      .ntbEntry.btime = pReq->btime,
×
562
      .ntbEntry.ttlDays = pReq->ttl,
×
563
      .ntbEntry.commentLen = pReq->commentLen,
×
564
      .ntbEntry.comment = pReq->comment,
×
565
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
566
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
×
567
      .colRef = pReq->colRef
568
  };
569

570
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
×
571
  if (code) {
×
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);
×
578
  if (TSDB_CODE_SUCCESS == code) {
×
579
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
×
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);
×
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) {
2,002✔
664
  int32_t code = TSDB_CODE_SUCCESS;
2,002✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
2,002!
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
2,002✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
×
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
×
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
×
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
×
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);
2,002✔
677
}
678

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

682
  // check request
683
  code = metaCheckDropTableReq(pMeta, version, pReq);
×
684
  if (code) {
×
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) {
×
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 = {
×
700
      .version = version,
701
      .uid = pReq->uid,
×
702
  };
703

704
  if (pReq->isVirtual) {
×
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) {
×
712
      entry.type = -TSDB_NORMAL_TABLE;
×
713
    } else {
714
      entry.type = -TSDB_CHILD_TABLE;
×
715
    }
716
  }
717
  code = metaHandleEntry2(pMeta, &entry);
×
718
  if (code) {
×
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,
×
723
             pReq->uid, version);
724
  }
725
  TAOS_RETURN(code);
×
726
}
727

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

731
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
×
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;
×
739
  int32_t valueSize = 0;
×
740
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
741
  if (code) {
×
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;
×
748
  tdbFreeClear(value);
×
749

750
  // check table type
751
  SMetaInfo info;
752
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
×
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) {
×
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);
×
768
  if (code) {
×
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);
×
774
}
775

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

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

785
  // fetch old entry
786
  SMetaEntry *pEntry = NULL;
×
787
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
788
  if (code) {
×
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) {
×
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;
×
802
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
×
803
  SSchema        *pColumn;
804
  SExtSchema      extSchema = {0};
×
805
  pEntry->version = version;
×
806
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
807
    pColumn = &pSchema->pSchema[i];
×
808
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
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;
×
815
  }
816

817
  if (rowSize + pReq->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
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));
×
825
  if (NULL == pNewSchema) {
×
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;
×
832
  pSchema->version++;
×
833
  pSchema->nCols++;
×
834
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
×
835
  pColumn->bytes = pReq->bytes;
×
836
  pColumn->type = pReq->type;
×
837
  pColumn->flags = pReq->flags;
×
838
  pColumn->colId = pEntry->ntbEntry.ncid++;
×
839
  extSchema.typeMod = pReq->typeMod;
×
840
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
×
841
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
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) {
×
863
      compress = createDefaultColCmprByType(pColumn->type);
×
864
    } else {
865
      compress = pReq->compress;
×
866
    }
867
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
×
868
    if (code) {
×
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);
×
876
  if (code) {
×
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);
×
885
  if (code) {
×
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,
×
892
             pEntry->uid, version);
893
  }
894

895
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
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) {
×
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++) {
×
917
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
918
        pRsp->pSchemaExt[i].colId = p->id;
×
919
        pRsp->pSchemaExt[i].compress = p->alg;
×
920
      }
921
    }
922
  }
923

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

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

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

937
  // fetch old entry
938
  SMetaEntry *pEntry = NULL;
×
939
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
940
  if (code) {
×
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) {
×
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;
×
955
  SSchema        *pColumn = NULL;
×
956
  SSchema         tColumn;
957
  int32_t         iColumn = 0;
×
958
  for (; iColumn < pSchema->nCols; iColumn++) {
×
959
    pColumn = &pSchema->pSchema[iColumn];
×
960
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
961
      break;
×
962
    }
963
  }
964

965
  if (iColumn == pSchema->nCols) {
×
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) {
×
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) {
×
980
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
×
981
              __func__, __FILE__, __LINE__, pReq->colName, version);
982
    metaFetchEntryFree(&pEntry);
×
983
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
984
  }
985
  tColumn = *pColumn;
×
986

987
  // do drop column
988
  pEntry->version = version;
×
989
  if (pSchema->nCols - iColumn - 1 > 0) {
×
990
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
×
991
  }
992
  pSchema->nCols--;
×
993
  pSchema->version++;
×
994
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
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);
×
1010
    if (code) {
×
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) {
×
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);
×
1026
  if (code) {
×
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);
×
1035
  if (code) {
×
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,
×
1041
             pEntry->uid, version);
1042
  }
1043

1044
  // build response
1045
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
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) {
×
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++) {
×
1067
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
1068
        pRsp->pSchemaExt[i].colId = p->id;
×
1069
        pRsp->pSchemaExt[i].compress = p->alg;
×
1070
      }
1071
    }
1072
  }
1073

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

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

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

1087
  if (NULL == pReq->colNewName) {
×
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;
×
1095
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1096
  if (code) {
×
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) {
×
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;
×
1111
  SSchema        *pColumn = NULL;
×
1112
  int32_t         iColumn = 0;
×
1113
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1114
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1115
      pColumn = &pSchema->pSchema[i];
×
1116
      iColumn = i;
×
1117
      break;
×
1118
    }
1119
  }
1120

1121
  if (NULL == pColumn) {
×
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) {
×
1129
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1130
              __func__, __FILE__, __LINE__, pColumn->name, version);
1131
    metaFetchEntryFree(&pEntry);
×
1132
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
×
1133
  }
1134

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

1140
  // do handle entry
1141
  code = metaHandleEntry2(pMeta, pEntry);
×
1142
  if (code) {
×
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,
×
1149
             pEntry->uid, version);
1150
  }
1151

1152
  // build response
1153
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
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) {
×
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++) {
×
1175
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
1176
        pRsp->pSchemaExt[i].colId = p->id;
×
1177
        pRsp->pSchemaExt[i].compress = p->alg;
×
1178
      }
1179
    }
1180
  }
1181

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

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

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

1195
  // fetch old entry
1196
  SMetaEntry *pEntry = NULL;
×
1197
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1198
  if (code) {
×
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) {
×
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;
×
1213
  SSchema        *pColumn = NULL;
×
1214
  int32_t         iColumn = 0;
×
1215
  int32_t         rowSize = 0;
×
1216
  for (int32_t i = 0; i < pSchema->nCols; i++) {
×
1217
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
×
1218
      pColumn = &pSchema->pSchema[i];
×
1219
      iColumn = i;
×
1220
    }
1221
    rowSize += pSchema->pSchema[i].bytes;
×
1222
  }
1223

1224
  if (NULL == pColumn) {
×
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) {
×
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) {
×
1240
    metaError("vgId:%d, %s failed at %s:%d since column %s is not modifiable, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1241
              __func__, __FILE__, __LINE__, pReq->colName, version);
1242
    metaFetchEntryFree(&pEntry);
×
1243
    TAOS_RETURN(TSDB_CODE_VND_COL_SUBSCRIBED);
×
1244
  }
1245

1246
  if (rowSize + pReq->colModBytes - pColumn->bytes > TSDB_MAX_BYTES_PER_ROW) {
×
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;
×
1256
  pSchema->version++;
×
1257
  pColumn->bytes = pReq->colModBytes;
×
1258

1259
  // do handle entry
1260
  code = metaHandleEntry2(pMeta, pEntry);
×
1261
  if (code) {
×
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,
×
1268
             pEntry->uid, version);
1269
  }
1270

1271
  // build response
1272
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
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) {
×
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++) {
×
1294
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
×
1295
        pRsp->pSchemaExt[i].colId = p->id;
×
1296
        pRsp->pSchemaExt[i].compress = p->alg;
×
1297
      }
1298
    }
1299
  }
1300

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

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

1308
  // check tag name
1309
  if (NULL == pReq->tagName || strlen(pReq->tagName) == 0) {
×
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;
×
1317
  int32_t valueSize = 0;
×
1318
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
1319
  if (code) {
×
1320
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1321
              __FILE__, __LINE__, pReq->tbName, version);
1322
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
1323
    TAOS_RETURN(code);
×
1324
  }
1325
  tdbFreeClear(value);
×
1326

1327
  TAOS_RETURN(code);
×
1328
}
1329

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

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

1339
  // fetch child entry
1340
  SMetaEntry *pChild = NULL;
×
1341
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
×
1342
  if (code) {
×
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) {
×
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;
×
1357
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
1358
  if (code) {
×
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;
×
1367
  SSchema        *pColumn = NULL;
×
1368
  int32_t         iColumn = 0;
×
1369
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
1370
    if (strncmp(pTagSchema->pSchema[i].name, pReq->tagName, TSDB_COL_NAME_LEN) == 0) {
×
1371
      pColumn = &pTagSchema->pSchema[i];
×
1372
      iColumn = i;
×
1373
      break;
×
1374
    }
1375
  }
1376

1377
  if (NULL == pColumn) {
×
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;
×
1387
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
1388
    void *pNewTag = taosMemoryRealloc(pChild->ctbEntry.pTags, pReq->nTagVal);
×
1389
    if (NULL == pNewTag) {
×
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;
×
1397
    memcpy(pChild->ctbEntry.pTags, pReq->pTagVal, pReq->nTagVal);
×
1398
  } else {
1399
    STag *pOldTag = (STag *)pChild->ctbEntry.pTags;
×
1400

1401
    SArray *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
1402
    if (NULL == pTagArray) {
×
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++) {
×
1411
      STagVal value = {
×
1412
          .type = pTagSchema->pSchema[i].type,
×
1413
          .cid = pTagSchema->pSchema[i].colId,
×
1414
      };
1415

1416
      if (iColumn == i) {
×
1417
        if (pReq->isNull) {
×
1418
          continue;
×
1419
        }
1420
        if (IS_VAR_DATA_TYPE(value.type)) {
×
1421
          value.pData = pReq->pTagVal;
×
1422
          value.nData = pReq->nTagVal;
×
1423
        } else {
1424
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
×
1425
        }
1426
      } else if (!tTagGet(pOldTag, &value)) {
×
1427
        continue;
×
1428
      }
1429

1430
      if (NULL == taosArrayPush(pTagArray, &value)) {
×
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;
×
1441
    code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
×
1442
    if (code) {
×
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);
×
1451
    taosMemoryFree(pChild->ctbEntry.pTags);
×
1452
    pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
×
1453
  }
1454

1455
  // do handle entry
1456
  code = metaHandleEntry2(pMeta, pChild);
×
1457
  if (code) {
×
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,
×
1465
             pChild->uid, version);
1466
  }
1467

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

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

1477
  // check tag name
1478
  if (NULL == pReq->pMultiTag || taosArrayGetSize(pReq->pMultiTag) == 0) {
×
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;
×
1486
  int32_t valueSize = 0;
×
1487
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
×
1488
  if (code) {
×
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);
×
1495

1496
  if (taosArrayGetSize(pReq->pMultiTag) == 0) {
×
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);
×
1503
}
1504

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

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

1513
  // fetch child entry
1514
  SMetaEntry *pChild = NULL;
×
1515
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pChild);
×
1516
  if (code) {
×
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) {
×
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;
×
1531
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
×
1532
  if (code) {
×
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;
×
1541

1542
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
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);
×
1553
  if (pTagTable == NULL) {
×
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++) {
×
1562
    SMultiTagUpateVal *pTagVal = taosArrayGet(pReq->pMultiTag, i);
×
1563
    if (taosHashPut(pTagTable, pTagVal->tagName, strlen(pTagVal->tagName), pTagVal, sizeof(*pTagVal)) != 0) {
×
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;
×
1574
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
1575
    taosHashGet(pTagTable, pTagSchema->pSchema[i].name, strlen(pTagSchema->pSchema[i].name)) != NULL
×
1576
        ? numOfChangedTags++
×
1577
        : 0;
×
1578
  }
1579
  if (numOfChangedTags < taosHashGetSize(pTagTable)) {
×
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;
×
1590
  const STag *pOldTag = (const STag *)pChild->ctbEntry.pTags;
×
1591
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
×
1592
  if (NULL == pTagArray) {
×
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++) {
×
1602
    SSchema *pCol = &pTagSchema->pSchema[i];
×
1603
    STagVal  value = {
×
1604
         .cid = pCol->colId,
×
1605
    };
1606

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

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

1626
    if (taosArrayPush(pTagArray, &value) == NULL) {
×
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;
×
1638
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
×
1639
  if (code) {
×
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);
×
1649
  taosMemoryFree(pChild->ctbEntry.pTags);
×
1650
  pChild->ctbEntry.pTags = (uint8_t *)pNewTag;
×
1651

1652
  // do handle entry
1653
  code = metaHandleEntry2(pMeta, pChild);
×
1654
  if (code) {
×
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,
×
1663
             pChild->uid, version);
1664
  }
1665

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

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

1675
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
×
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;
×
1682
}
1683

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

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

1692
  // fetch entry
1693
  SMetaEntry *pEntry = NULL;
×
1694
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1695
  if (code) {
×
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;
×
1703
  if (pEntry->type == TSDB_CHILD_TABLE) {
×
1704
    if (pReq->updateTTL) {
×
1705
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
×
1706
    }
1707
    if (pReq->newCommentLen >= 0) {
×
1708
      char *pNewComment = NULL;
×
1709
      if (pReq->newCommentLen) {
×
1710
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
×
1711
        if (NULL == pNewComment) {
×
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);
×
1718
      } else {
1719
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
×
1720
      }
1721
      pEntry->ctbEntry.comment = pNewComment;
×
1722
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
×
1723
    }
1724
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
×
1725
    if (pReq->updateTTL) {
×
1726
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
×
1727
    }
1728
    if (pReq->newCommentLen >= 0) {
×
1729
      char *pNewComment = NULL;
×
1730
      if (pReq->newCommentLen > 0) {
×
1731
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
×
1732
        if (NULL == pNewComment) {
×
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);
×
1739
      } else {
1740
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
×
1741
      }
1742
      pEntry->ntbEntry.comment = pNewComment;
×
1743
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
×
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);
×
1754
  if (code) {
×
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,
×
1761
             pEntry->uid, version);
1762
  }
1763

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

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

1771
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
×
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;
×
1778
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
×
1779
  if (code) {
×
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) {
×
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) {
×
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;
×
1801
  SColCmprWrapper *wp = &pEntry->colCmpr;
×
1802
  for (int32_t i = 0; i < wp->nCols; i++) {
×
1803
    SColCmpr *p = &wp->pColCmpr[i];
×
1804
    if (p->id == pReq->colId) {
×
1805
      uint32_t dst = 0;
×
1806
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
×
1807
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
1808
      if (updated > 0) {
×
1809
        p->alg = dst;
×
1810
      }
1811
    }
1812
  }
1813

1814
  if (updated == 0) {
×
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) {
×
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;
×
1829

1830
  // do handle entry
1831
  code = metaHandleEntry2(pMeta, pEntry);
×
1832
  if (code) {
×
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,
×
1839
             pEntry->uid, version);
1840
  }
1841

1842
  metaFetchEntryFree(&pEntry);
×
1843
  TAOS_RETURN(code);
×
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) {
×
2065
  int32_t code = TSDB_CODE_SUCCESS;
×
2066

2067
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
×
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;
×
2074
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
×
2075
  if (code) {
×
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) {
×
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) {
×
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;
×
2107
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
×
2108
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
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) {
×
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;
×
2134
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
×
2135
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
×
2136
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
×
2137

2138
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
×
2139
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
×
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)) {
×
2147
      numOfChangedTags++;
×
2148
      SSCHMEA_SET_IDX_ON(pOldColumn);
×
2149
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
×
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) {
×
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;
×
2166
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
×
2167

2168
  // do handle the entry
2169
  code = metaHandleEntry2(pMeta, pEntry);
×
2170
  if (code) {
×
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,
×
2177
             pEntry->uid, version);
2178
  }
2179

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

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

2187
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
×
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;
×
2194
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
×
2195
  if (code) {
×
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) {
×
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;
×
2209
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
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;
×
2218
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
×
2219
    SSchema *pCol = pTagSchema->pSchema + i;
×
2220
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
×
2221
      if (!IS_IDX_ON(pCol)) {
×
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++;
×
2228
      SSCHMEA_SET_IDX_OFF(pCol);
×
2229
      break;
×
2230
    }
2231
  }
2232

2233
  if (numOfChangedTags != 1) {
×
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;
×
2242
  pTagSchema->version++;
×
2243
  code = metaHandleEntry2(pMeta, pEntry);
×
2244
  if (code) {
×
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,
×
2251
             pEntry->uid, version);
2252
  }
2253

2254
  metaFetchEntryFree(&pEntry);
×
2255
  TAOS_RETURN(code);
×
2256
}
2257

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

2261
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
32!
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;
32✔
2268
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
32✔
2269
  if (code) {
32!
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) {
32!
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 = {
32✔
2283
      .version = version,
2284
      .type = TSDB_SUPER_TABLE,
2285
      .uid = pReq->suid,
32✔
2286
      .name = pReq->name,
32✔
2287
      .stbEntry.schemaRow = pReq->schemaRow,
2288
      .stbEntry.schemaTag = pReq->schemaTag,
2289
      .stbEntry.keep = pReq->keep,
32✔
2290
      .colCmpr = pReq->colCmpr,
2291
      .pExtSchemas = pReq->pExtSchemas,
32✔
2292
  };
2293
  TABLE_SET_COL_COMPRESSED(entry.flags);
32✔
2294
  if (pReq->virtualStb) {
32!
2295
    TABLE_SET_VIRTUAL(entry.flags);
×
2296
  }
2297

2298
  // do handle the entry
2299
  code = metaHandleEntry2(pMeta, &entry);
32✔
2300
  if (code) {
32!
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,
32!
2307
             pReq->suid, version);
2308
  }
2309

2310
  metaFetchEntryFree(&pEntry);
32✔
2311
  TAOS_RETURN(code);
32✔
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