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

taosdata / TDengine / #4914

06 Jan 2026 01:30AM UTC coverage: 64.876% (-0.008%) from 64.884%
#4914

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

3475 existing lines in 124 files now uncovered.

194993 of 300563 relevant lines covered (64.88%)

116239151.85 hits per line

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

67.8
/source/dnode/vnode/src/meta/metaTable2.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "meta.h"
17

18
extern int32_t metaHandleEntry2(SMeta *pMeta, const SMetaEntry *pEntry);
19
extern int32_t metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp);
20
extern int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
21
                                    STableMetaRsp *pMetaRsp, int8_t tableType);
22
extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
23
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
24
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
25
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
26
extern int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema);
27
extern int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum);
28

29
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,722,930✔
30
  int32_t   vgId = TD_VID(pMeta->pVnode);
4,722,930✔
31
  void     *value = NULL;
4,736,391✔
32
  int32_t   valueSize = 0;
4,736,494✔
33
  SMetaInfo info;
4,735,383✔
34

35
  // check name
36
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
4,734,698✔
37
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
9,412✔
38
              pReq->name, version);
39
    return TSDB_CODE_INVALID_MSG;
×
40
  }
41

42
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
4,725,594✔
43
  if (r == 0) {  // name exists, check uid and type
4,727,113✔
44
    int64_t uid = *(tb_uid_t *)value;
2,924✔
45
    tdbFree(value);
2,924✔
46

47
    if (pReq->suid != uid) {
2,924✔
48
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
2,924✔
49
                " version:%" PRId64,
50
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
51
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
2,924✔
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) {
4,724,189✔
73
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
×
74
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
75
    return TSDB_CODE_INVALID_MSG;
×
76
  }
77

78
  return TSDB_CODE_SUCCESS;
4,728,361✔
79
}
80

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

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

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

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

117
  return code;
1,366,919✔
118
}
119

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

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

132
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
865,669✔
133
  if (code) {
864,737✔
134
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
731✔
135
              __FILE__, __LINE__, pReq->name, version);
136
    return TSDB_CODE_TDB_STB_NOT_EXIST;
731✔
137
  } else {
138
    int64_t uid = *(int64_t *)value;
864,006✔
139
    tdbFreeClear(value);
864,384✔
140

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

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

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

167
  // check request
168
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
4,727,263✔
169
  if (code != TSDB_CODE_SUCCESS) {
4,732,687✔
170
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
2,924✔
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);
2,924✔
176
    }
177
  }
178

179
  // handle entry
180
  SMetaEntry entry = {
9,450,045✔
181
      .version = version,
182
      .type = TSDB_SUPER_TABLE,
183
      .uid = pReq->suid,
4,729,450✔
184
      .name = pReq->name,
4,725,528✔
185
      .stbEntry.schemaRow = pReq->schemaRow,
186
      .stbEntry.schemaTag = pReq->schemaTag,
187
      .stbEntry.keep = pReq->keep,
4,722,893✔
188
      .stbEntry.ownerId = pReq->ownerId,
4,716,821✔
189
  };
190
  if (pReq->rollup) {
4,721,803✔
191
    TABLE_SET_ROLLUP(entry.flags);
×
192
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
193
  }
194
  if (pReq->colCmpred) {
4,718,252✔
195
    TABLE_SET_COL_COMPRESSED(entry.flags);
4,717,277✔
196
    entry.colCmpr = pReq->colCmpr;
4,717,277✔
197
  }
198

199
  entry.pExtSchemas = pReq->pExtSchemas;
4,701,685✔
200

201
  if (pReq->virtualStb) {
4,705,638✔
202
    TABLE_SET_VIRTUAL(entry.flags);
42,746✔
203
  }
204

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

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

220
  // check request
221
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
865,669✔
222
  if (code) {
863,000✔
223
    TAOS_RETURN(code);
1,462✔
224
  }
225

226
  // handle entry
227
  SMetaEntry entry = {
861,538✔
228
      .version = verison,
229
      .type = -TSDB_SUPER_TABLE,
230
      .uid = pReq->suid,
864,207✔
231
  };
232
  code = metaHandleEntry2(pMeta, &entry);
862,006✔
233
  if (code) {
863,652✔
234
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
235
              tstrerror(code));
236
  } else {
237
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
863,652✔
238
             pReq->suid, verison);
239
  }
240
  TAOS_RETURN(code);
864,207✔
241
}
242

243
// Alter Super Table
244

245
// Create Child Table
246
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
52,019,482✔
247
  int32_t   code = TSDB_CODE_SUCCESS;
52,019,482✔
248
  void     *value = NULL;
52,019,482✔
249
  int32_t   valueSize = 0;
52,018,638✔
250
  SMetaInfo info;
52,012,108✔
251

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

259
  // check table existence
260
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
52,009,589✔
261
    pReq->uid = *(int64_t *)value;
326,918✔
262
    tdbFreeClear(value);
326,918✔
263

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

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

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

287
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
325,034✔
288
  }
289

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

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

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

315
  // Check tag value
316
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
51,676,728✔
317
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
51,686,134✔
318
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
51,683,273✔
319
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
236,131,763✔
320
      STagVal tagVal = {
184,708,302✔
321
          .cid = pTagSchema->pSchema[i].colId,
184,695,358✔
322
      };
323

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

335
  metaFetchEntryFree(&pStbEntry);
51,690,941✔
336

337
  // check grant
338
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
51,673,340✔
339
    code = grantCheck(TSDB_GRANT_TIMESERIES);
51,681,811✔
340
    if (TSDB_CODE_SUCCESS != code) {
51,670,120✔
341
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
342
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
343
    }
344
  }
345
  return code;
51,669,712✔
346
}
347

348
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
51,504,043✔
349
  int32_t code = TSDB_CODE_SUCCESS;
51,504,043✔
350

351
  if (NULL == ppRsp) {
51,504,043✔
352
    return code;
×
353
  }
354

355
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
51,504,043✔
356
  if (NULL == ppRsp) {
51,510,674✔
357
    return terrno;
×
358
  }
359

360
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
51,510,674✔
361
  (*ppRsp)->tuid = pEntry->uid;
51,514,790✔
362
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
51,523,496✔
363
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
51,528,401✔
364

365
  return code;
51,538,263✔
366
}
367

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

371
  // check request
372
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
51,868,125✔
373
  if (code) {
51,851,990✔
374
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
326,873✔
375
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,749✔
376
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
377
    }
378
    return code;
326,783✔
379
  }
380

381
  SMetaEntry entry = {
51,525,117✔
382
      .version = version,
383
      .type = TSDB_CHILD_TABLE,
384
      .uid = pReq->uid,
51,521,853✔
385
      .name = pReq->name,
51,519,037✔
386
      .ctbEntry.btime = pReq->btime,
51,515,446✔
387
      .ctbEntry.ttlDays = pReq->ttl,
51,512,713✔
388
      .ctbEntry.commentLen = pReq->commentLen,
51,514,612✔
389
      .ctbEntry.comment = pReq->comment,
51,510,822✔
390
      .ctbEntry.suid = pReq->ctb.suid,
51,508,986✔
391
      .ctbEntry.pTags = pReq->ctb.pTag,
51,507,693✔
392
  };
393

394
  // build response
395
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
51,515,940✔
396
  if (code) {
51,530,470✔
397
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
398
              tstrerror(code));
399
  }
400

401
  // handle entry
402
  code = metaHandleEntry2(pMeta, &entry);
51,530,470✔
403
  if (TSDB_CODE_SUCCESS == code) {
51,536,524✔
404
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
51,537,969✔
405
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
406
  } else {
407
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
408
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
409
              pReq->ctb.suid, version);
410
  }
411
  return code;
51,548,701✔
412
}
413

414
// Drop Child Table
415

416
// Alter Child Table
417

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

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

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

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

447
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
18,975,875✔
448
  int32_t code = TSDB_CODE_SUCCESS;
18,975,875✔
449

450
  if (NULL == ppRsp) {
18,975,875✔
451
    return code;
×
452
  }
453

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

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

465
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
106,149,549✔
466
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
87,173,674✔
467
    (*ppRsp)->pSchemaExt[i].colId = p->id;
87,173,674✔
468
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
87,173,674✔
469
    if (pEntry->pExtSchemas) {
87,173,674✔
470
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
436,724✔
471
    }
472
  }
473

474
  return code;
18,975,875✔
475
}
476

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

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

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

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

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

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

528
  if (NULL == ppRsp) {
98,427✔
529
    return code;
×
530
  }
531

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

537
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, *ppRsp,
98,427✔
538
                              TSDB_VIRTUAL_NORMAL_TABLE);
539
  if (code) {
98,427✔
540
    taosMemoryFreeClear(*ppRsp);
464✔
541
    return code;
464✔
542
  }
543

544
  return code;
97,963✔
545
}
546

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

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

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

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

592
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
152,498✔
593
  int32_t code = TSDB_CODE_SUCCESS;
152,498✔
594

595
  if (NULL == ppRsp) {
152,498✔
596
    return code;
×
597
  }
598

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

604
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
152,498✔
605
  if (code) {
152,498✔
606
    taosMemoryFreeClear(*ppRsp);
464✔
607
    return code;
464✔
608
  }
609
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
152,034✔
610

611
  return code;
152,034✔
612
}
613

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

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

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

644
  // handle entry
645
  code = metaHandleEntry2(pMeta, &entry);
152,034✔
646
  if (TSDB_CODE_SUCCESS == code) {
152,034✔
647
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
152,034✔
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);
152,034✔
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) {
71,123,598✔
664
  int32_t code = TSDB_CODE_SUCCESS;
71,123,598✔
665
  if (TSDB_CHILD_TABLE == pReq->type) {
71,123,598✔
666
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
51,871,042✔
667
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
19,254,652✔
668
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
19,003,727✔
669
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
250,925✔
670
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
98,427✔
671
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
152,498✔
672
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
152,498✔
673
  } else {
674
    code = TSDB_CODE_INVALID_MSG;
×
675
  }
676
  TAOS_RETURN(code);
71,129,627✔
677
}
678

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

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

704
  if (pReq->isVirtual) {
1,366,919✔
705
    if (pReq->suid == 0) {
52,742✔
706
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
26,584✔
707
    } else {
708
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
26,158✔
709
    }
710
  } else {
711
    if (pReq->suid == 0) {
1,314,177✔
712
      entry.type = -TSDB_NORMAL_TABLE;
759,389✔
713
    } else {
714
      entry.type = -TSDB_CHILD_TABLE;
554,788✔
715
    }
716
  }
717
  code = metaHandleEntry2(pMeta, &entry);
1,366,919✔
718
  if (code) {
1,366,919✔
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,
1,366,919✔
723
             pReq->uid, version);
724
  }
725
  TAOS_RETURN(code);
1,366,919✔
726
}
727

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

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

750
  // check table type
751
  SMetaInfo info;
4,141,048✔
752
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,141,664✔
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 &&
4,141,048✔
760
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
26,696✔
761
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
762
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
763
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
764
    TAOS_RETURN(code);
×
765
  }
766

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

777
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,414,197✔
778
  int32_t code = TSDB_CODE_SUCCESS;
3,414,197✔
779

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

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

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

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

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

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

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

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

943
  metaFetchEntryFree(&pEntry);
3,104,501✔
944
  TAOS_RETURN(code);
3,104,501✔
945
}
946

947
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
86,575✔
948
  int32_t code = TSDB_CODE_SUCCESS;
86,575✔
949

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

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

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

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

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

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

998
  tColumn = *pColumn;
86,575✔
999

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

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

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

1057
  // build response
1058
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
86,575✔
1059
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
28,373✔
1060
    if (code) {
28,373✔
1061
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1062
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1063
    } else {
1064
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
30,589,307✔
1065
        SColRef *p = &pEntry->colRef.pColRef[i];
30,560,934✔
1066
        pRsp->pColRefs[i].hasRef = p->hasRef;
30,560,934✔
1067
        pRsp->pColRefs[i].id = p->id;
30,560,934✔
1068
        if (p->hasRef) {
30,560,934✔
1069
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
15,292,319✔
1070
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
15,292,319✔
1071
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
15,292,319✔
1072
        }
1073
      }
1074
    }
1075
  } else {
1076
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
58,202✔
1077
    if (code) {
58,202✔
1078
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1079
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1080
    } else {
1081
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
42,830,391✔
1082
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
42,772,189✔
1083
        pRsp->pSchemaExt[i].colId = p->id;
42,772,189✔
1084
        pRsp->pSchemaExt[i].compress = p->alg;
42,772,189✔
1085
      }
1086
    }
1087
  }
1088

1089
  metaFetchEntryFree(&pEntry);
86,575✔
1090
  TAOS_RETURN(code);
86,575✔
1091
}
1092

1093
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
43,164✔
1094
  int32_t code = TSDB_CODE_SUCCESS;
43,164✔
1095

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

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

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

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

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

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

1143

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

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

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

1193
  metaFetchEntryFree(&pEntry);
43,164✔
1194
  TAOS_RETURN(code);
43,164✔
1195
}
1196

1197
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
472,972✔
1198
  int32_t code = TSDB_CODE_SUCCESS;
472,972✔
1199

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

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

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

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

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

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

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

1259
  // do change the column bytes
1260
  pEntry->version = version;
239,508✔
1261
  pSchema->version++;
239,508✔
1262
  pColumn->bytes = pReq->colModBytes;
239,508✔
1263

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

1276
  // build response
1277
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
239,508✔
1278
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pRsp, pEntry->type);
25,720✔
1279
    if (code) {
25,720✔
1280
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1281
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1282
    } else {
1283
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
15,380,352✔
1284
        SColRef *p = &pEntry->colRef.pColRef[i];
15,354,632✔
1285
        pRsp->pColRefs[i].hasRef = p->hasRef;
15,354,632✔
1286
        pRsp->pColRefs[i].id = p->id;
15,354,632✔
1287
        if (p->hasRef) {
15,354,632✔
1288
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
75,152✔
1289
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
75,152✔
1290
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
75,152✔
1291
        }
1292
      }
1293
    }
1294
  } else {
1295
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pRsp);
213,788✔
1296
    if (code) {
213,788✔
1297
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1298
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1299
    } else {
1300
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
8,597,632✔
1301
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,383,844✔
1302
        pRsp->pSchemaExt[i].colId = p->id;
8,383,844✔
1303
        pRsp->pSchemaExt[i].compress = p->alg;
8,383,844✔
1304
      }
1305
    }
1306
  }
1307

1308
  metaFetchEntryFree(&pEntry);
239,508✔
1309
  TAOS_RETURN(code);
239,508✔
1310
}
1311

1312
static int32_t metaCheckUpdateTableTagValReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,468,736✔
1313
  int32_t code = 0;
7,468,736✔
1314

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

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

1334
  TAOS_RETURN(code);
7,468,481✔
1335
}
1336

1337
      // TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
1338

1339
static bool checkSameTag(uint32_t nTagVal, uint8_t* pTagVal, bool isNull, STagVal value, const STag *pOldTag) {
7,370,810✔
1340
  if (isNull) {
7,370,810✔
1341
    if (!tTagGet(pOldTag, &value)) {
45,311✔
1342
      metaWarn("%s warn at %s:%d same tag null", __func__, __FILE__, __LINE__);
19,485✔
1343
      return true;
19,485✔
1344
    }
1345
    return false;
25,826✔
1346
  }
1347
  if (!tTagGet(pOldTag, &value)){
7,325,499✔
1348
    return false;
177,368✔
1349
  }
1350
  if (IS_VAR_DATA_TYPE(value.type)) {
7,148,131✔
1351
    if (nTagVal == value.nData && memcmp(pTagVal, value.pData, value.nData) == 0) {
56,613✔
1352
      metaWarn("%s warn at %s:%d same tag var", __func__, __FILE__, __LINE__);
35,819✔
1353
      return true;
35,819✔
1354
    }
1355
  } else {
1356
    if (memcmp(&value.i64, pTagVal, nTagVal) == 0) {
7,091,518✔
1357
      metaWarn("%s warn at %s:%d same tag fixed", __func__, __FILE__, __LINE__);
87,638✔
1358
      return true;
87,638✔
1359
    }
1360
  }
1361
  return false;
7,024,674✔
1362
}
1363

1364
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,468,736✔
1365
  int32_t code = TSDB_CODE_SUCCESS;
7,468,736✔
1366

1367
  // check request
1368
  code = metaCheckUpdateTableTagValReq(pMeta, version, pReq);
7,468,736✔
1369
  if (code) {
7,468,736✔
1370
    TAOS_RETURN(code);
255✔
1371
  }
1372

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

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

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

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

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

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

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

1444
    for (int32_t i = 0; i < pTagSchema->nCols; i++) {
15,623,765✔
1445
      STagVal value = {
8,406,249✔
1446
          .type = pTagSchema->pSchema[i].type,
8,406,249✔
1447
          .cid = pTagSchema->pSchema[i].colId,
8,406,249✔
1448
      };
1449

1450
      if (iColumn == i) {
8,406,249✔
1451
        if (checkSameTag(pReq->nTagVal, pReq->pTagVal, pReq->isNull, value, pOldTag)) {
7,351,400✔
1452
          taosArrayDestroy(pTagArray);
133,884✔
1453
          metaFetchEntryFree(&pChild);
133,884✔
1454
          metaFetchEntryFree(&pSuper);
133,884✔
1455
          TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
133,884✔
1456
        }
1457
        if (pReq->isNull) {
7,217,516✔
1458
          continue;
23,885✔
1459
        }
1460
        if (IS_VAR_DATA_TYPE(value.type)) {
7,193,631✔
1461
          value.pData = pReq->pTagVal;
99,844✔
1462
          value.nData = pReq->nTagVal;
99,844✔
1463
        } else {
1464
          memcpy(&value.i64, pReq->pTagVal, pReq->nTagVal);
7,093,787✔
1465
        }
1466
      } else if (!tTagGet(pOldTag, &value)) {
1,054,849✔
1467
        continue;
286,496✔
1468
      }
1469

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

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

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

1508
  // free resource and return
1509
  metaFetchEntryFree(&pChild);
7,334,597✔
1510
  metaFetchEntryFree(&pSuper);
7,334,597✔
1511
  TAOS_RETURN(code);
7,334,597✔
1512
}
1513

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

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

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

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

1542
  TAOS_RETURN(code);
3,235✔
1543
}
1544

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

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

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

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

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

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

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

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

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

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

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

1641
  bool allSame = true;
3,235✔
1642

1643
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
25,880✔
1644
    SSchema *pCol = &pTagSchema->pSchema[i];
22,645✔
1645
    STagVal  value = {
22,645✔
1646
         .cid = pCol->colId,
22,645✔
1647
    };
1648

1649
    SMultiTagUpateVal *pTagVal = taosHashGet(pTagTable, pCol->name, strlen(pCol->name));
22,645✔
1650
    if (pTagVal == NULL) {
22,645✔
1651
      if (!tTagGet(pOldTag, &value)) {
3,235✔
1652
        continue;
×
1653
      }
1654
    } else {
1655
      value.type = pCol->type;
19,410✔
1656
      if (!checkSameTag(pTagVal->nTagVal, pTagVal->pTagVal, pTagVal->isNull, value, pOldTag)) {
19,410✔
1657
        allSame = false;
10,352✔
1658
      }
1659
      if (pTagVal->isNull) {
19,410✔
1660
        continue;
3,882✔
1661
      }
1662

1663
      if (IS_VAR_DATA_TYPE(pCol->type)) {
15,528✔
1664
        value.pData = pTagVal->pTagVal;
2,588✔
1665
        value.nData = pTagVal->nTagVal;
2,588✔
1666
      } else {
1667
        memcpy(&value.i64, pTagVal->pTagVal, pTagVal->nTagVal);
12,940✔
1668
      }
1669
    }
1670

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

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

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

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

1726
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,158✔
1727
  int32_t code = TSDB_CODE_SUCCESS;
24,158✔
1728

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

1735
  return code;
24,158✔
1736
}
1737

1738
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
24,158✔
1739
  int32_t code = 0;
24,158✔
1740

1741
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
24,158✔
1742
  if (code) {
24,158✔
1743
    TAOS_RETURN(code);
×
1744
  }
1745

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

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

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

1818
  metaFetchEntryFree(&pEntry);
24,158✔
1819
  TAOS_RETURN(code);
24,158✔
1820
}
1821

1822
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
5,912✔
1823
  int32_t code = TSDB_CODE_SUCCESS;
5,912✔
1824

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

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

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

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

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

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

1882
  pEntry->version = version;
5,912✔
1883

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

1896
  metaFetchEntryFree(&pEntry);
5,912✔
1897
  TAOS_RETURN(code);
5,912✔
1898
}
1899

1900
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
71,776✔
1901
  int32_t code = TSDB_CODE_SUCCESS;
71,776✔
1902

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

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

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

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

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

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

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

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

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

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

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

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

2017
  metaFetchEntryFree(&pEntry);
71,776✔
2018
  metaFetchEntryFree(&pSuper);
71,776✔
2019
  TAOS_RETURN(code);
71,776✔
2020
}
2021

2022
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
52,980✔
2023
  int32_t code = TSDB_CODE_SUCCESS;
52,980✔
2024

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

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

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

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

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

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

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

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

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

2118
  metaFetchEntryFree(&pEntry);
52,980✔
2119
  metaFetchEntryFree(&pSuper);
52,980✔
2120
  TAOS_RETURN(code);
52,980✔
2121
}
2122

2123
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
4,343✔
2124
  int32_t code = TSDB_CODE_SUCCESS;
4,343✔
2125

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

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

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

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

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

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

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

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

2192
  int32_t numOfChangedTags = 0;
4,343✔
2193
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
22,215✔
2194
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
17,872✔
2195
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
17,872✔
2196

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

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

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

2224
  pEntry->version = version;
4,343✔
2225
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
4,343✔
2226

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

2239
  metaFetchEntryFree(&pEntry);
4,343✔
2240
  TAOS_RETURN(code);
4,343✔
2241
}
2242

2243
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
2,888✔
2244
  int32_t code = TSDB_CODE_SUCCESS;
2,888✔
2245

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

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

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

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

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

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

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

2313
  metaFetchEntryFree(&pEntry);
2,888✔
2314
  TAOS_RETURN(code);
2,888✔
2315
}
2316

2317
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,209,212✔
2318
  int32_t code = TSDB_CODE_SUCCESS;
7,209,212✔
2319

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

2326
  SMetaEntry *pEntry = NULL;
7,194,711✔
2327
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,198,978✔
2328
  if (code) {
7,197,236✔
2329
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2330
              __FILE__, __LINE__, pReq->name, version);
2331
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2332
  }
2333

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

2341
  SMetaEntry entry = {
21,541,695✔
2342
      .version = version,
2343
      .type = TSDB_SUPER_TABLE,
2344
      .uid = pReq->suid,
7,189,640✔
2345
      .name = pReq->name,
7,186,786✔
2346
      .stbEntry.schemaRow = pReq->schemaRow,
2347
      .stbEntry.schemaTag = pReq->schemaTag,
2348
      .stbEntry.keep = pReq->keep,
7,182,521✔
2349
      .stbEntry.ownerId = pReq->ownerId,
7,185,565✔
2350
      .colCmpr = pReq->colCmpr,
2351
      .pExtSchemas = pReq->pExtSchemas,
7,177,672✔
2352
  };
2353
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,171,794✔
2354
  if (pReq->virtualStb) {
7,171,794✔
2355
    TABLE_SET_VIRTUAL(entry.flags);
19,566✔
2356
  }
2357
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,184,894✔
2358
    TABLE_SET_ROLLUP(entry.flags);
4,386✔
2359
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,386✔
2360
  }
2361

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

2374
  metaFetchEntryFree(&pEntry);
7,205,195✔
2375
  TAOS_RETURN(code);
7,216,178✔
2376
}
2377

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

2381
  if (taosArrayGetSize(uidArray) == 0) {
×
2382
    return TSDB_CODE_SUCCESS;
×
2383
  }
2384

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

2395
    SMetaEntry entry = {
×
2396
        .version = version,
2397
        .uid = uid,
2398
    };
2399

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

2417
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
21,930✔
2418
  int32_t code = TSDB_CODE_SUCCESS;
21,930✔
2419

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

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

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

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

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

2457
  SMetaEntry entry = *pEntry;
21,930✔
2458
  entry.version = version;
21,930✔
2459
  entry.stbEntry.rsmaParam.name = pReq->name;
21,930✔
2460
  entry.stbEntry.rsmaParam.uid = pReq->uid;
21,930✔
2461
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
21,199✔
2462
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
21,930✔
2463
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
21,930✔
2464
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
21,199✔
2465
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
21,930✔
2466
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
21,930✔
2467

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

2482
  metaFetchEntryFree(&pEntry);
21,930✔
2483
  TAOS_RETURN(code);
21,930✔
2484
}
2485

2486
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
4,386✔
2487
  int32_t code = TSDB_CODE_SUCCESS;
4,386✔
2488

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

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

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

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

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

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

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

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

2568
  metaFetchEntryFree(&pEntry);
4,386✔
2569
  TAOS_RETURN(code);
4,386✔
2570
}
2571

2572
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
11,696✔
2573
  int32_t code = TSDB_CODE_SUCCESS;
11,696✔
2574

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

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

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

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

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

2626
  metaFetchEntryFree(&pEntry);
11,696✔
2627
  TAOS_RETURN(code);
11,696✔
2628
}
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