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

taosdata / TDengine / #5007

29 Mar 2026 04:32AM UTC coverage: 72.25% (-0.02%) from 72.274%
#5007

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253624 of 351039 relevant lines covered (72.25%)

132531546.97 hits per line

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

69.06
/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
#include "scalar.h"
18
#include "tarray.h"
19
#include "tdatablock.h"
20
#include "querynodes.h"
21
#include "thash.h"
22

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

35
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,292,811✔
36
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,292,811✔
37
  void     *value = NULL;
5,293,333✔
38
  int32_t   valueSize = 0;
5,295,740✔
39
  SMetaInfo info;
5,290,073✔
40

41
  // check name
42
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
5,293,778✔
43
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
3,488✔
44
              pReq->name, version);
45
    return TSDB_CODE_INVALID_MSG;
×
46
  }
47

48
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
5,291,613✔
49
  if (r == 0) {  // name exists, check uid and type
5,280,366✔
50
    int64_t uid = *(tb_uid_t *)value;
4,898✔
51
    tdbFree(value);
4,898✔
52

53
    if (pReq->suid != uid) {
4,898✔
54
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
3,064✔
55
                " version:%" PRId64,
56
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
57
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
3,064✔
58
    }
59

60
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
1,834✔
61
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
62
                " not found, this is an internal error in meta, version:%" PRId64,
63
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
64
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
65
    }
66

67
    if (info.uid == info.suid) {
1,834✔
68
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
1,834✔
69
    } else {
70
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
71
                " already exists but not a super table, version:%" PRId64,
72
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
73
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
74
    }
75
  }
76

77
  // check suid
78
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
5,275,468✔
79
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
×
80
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
81
    return TSDB_CODE_INVALID_MSG;
×
82
  }
83

84
  return TSDB_CODE_SUCCESS;
5,285,650✔
85
}
86

87
static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,552,524✔
88
  int32_t   code = TSDB_CODE_SUCCESS;
1,552,524✔
89
  void     *value = NULL;
1,552,524✔
90
  int32_t   valueSize = 0;
1,552,524✔
91
  SMetaInfo info;
1,552,308✔
92

93
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,552,524✔
94
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
95
              __FILE__, __LINE__, pReq->name, version);
96
    return TSDB_CODE_INVALID_MSG;
×
97
  }
98

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

113
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
1,552,524✔
114
  if (TSDB_CODE_SUCCESS != code) {
1,552,524✔
115
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
116
              " not found, this is an internal error, version:%" PRId64,
117
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, version);
118
    code = TSDB_CODE_INTERNAL_ERROR;
×
119
    return code;
×
120
  }
121
  pReq->suid = info.suid;
1,552,524✔
122

123
  return code;
1,552,524✔
124
}
125

126
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
921,314✔
127
  int32_t   code = TSDB_CODE_SUCCESS;
921,314✔
128
  void     *value = NULL;
921,314✔
129
  int32_t   valueSize = 0;
921,314✔
130
  SMetaInfo info;
921,301✔
131

132
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
920,642✔
133
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
507✔
134
              __FILE__, __LINE__, pReq->name, version);
135
    return TSDB_CODE_INVALID_MSG;
×
136
  }
137

138
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
920,825✔
139
  if (code) {
921,143✔
140
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
766✔
141
              __FILE__, __LINE__, pReq->name, version);
142
    return TSDB_CODE_TDB_STB_NOT_EXIST;
766✔
143
  } else {
144
    int64_t uid = *(int64_t *)value;
920,377✔
145
    tdbFreeClear(value);
921,143✔
146

147
    if (uid != pReq->suid) {
919,705✔
148
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " not match, version:%" PRId64,
766✔
149
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
150
      return TSDB_CODE_TDB_STB_NOT_EXIST;
766✔
151
    }
152
  }
153

154
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
918,269✔
155
  if (code) {
918,533✔
156
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
157
              " not found, this is an internal error, version:%" PRId64,
158
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
159
    return TSDB_CODE_INTERNAL_ERROR;
×
160
  }
161
  if (info.suid != info.uid) {
918,533✔
162
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a super table, version:%" PRId64,
×
163
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
164
    return TSDB_CODE_INVALID_MSG;
×
165
  }
166
  return code;
918,533✔
167
}
168

169
// Create Super Table
170
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,278,593✔
171
  int32_t code = TSDB_CODE_SUCCESS;
5,278,593✔
172

173
  // check request
174
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,278,593✔
175
  if (code != TSDB_CODE_SUCCESS) {
5,289,478✔
176
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
4,898✔
177
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
1,834✔
178
               pReq->name, pReq->suid, version);
179
      TAOS_RETURN(TSDB_CODE_SUCCESS);
1,834✔
180
    } else {
181
      TAOS_RETURN(code);
3,064✔
182
    }
183
  }
184

185
  // handle entry
186
  SMetaEntry entry = {
10,557,109✔
187
      .version = version,
188
      .type = TSDB_SUPER_TABLE,
189
      .uid = pReq->suid,
5,285,642✔
190
      .name = pReq->name,
5,283,478✔
191
      .stbEntry.schemaRow = pReq->schemaRow,
192
      .stbEntry.schemaTag = pReq->schemaTag,
193
      .stbEntry.keep = pReq->keep,
5,286,866✔
194
      .stbEntry.ownerId = pReq->ownerId,
5,272,037✔
195
  };
196
  if (pReq->rollup) {
5,271,251✔
197
    TABLE_SET_ROLLUP(entry.flags);
×
198
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
199
  }
200
  if (pReq->colCmpred) {
5,267,752✔
201
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,283,162✔
202
    entry.colCmpr = pReq->colCmpr;
5,283,162✔
203
  }
204

205
  entry.pExtSchemas = pReq->pExtSchemas;
5,262,801✔
206

207
  if (pReq->virtualStb) {
5,284,179✔
208
    TABLE_SET_VIRTUAL(entry.flags);
101,006✔
209
  }
210

211
  code = metaHandleEntry2(pMeta, &entry);
5,274,027✔
212
  if (TSDB_CODE_SUCCESS == code) {
5,293,537✔
213
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
5,293,537✔
214
             pReq->suid, version);
215
  } else {
216
    metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
217
              pReq->suid, tstrerror(code));
218
  }
219
  TAOS_RETURN(code);
5,293,537✔
220
}
221

222
// Drop Super Table
223
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
921,314✔
224
  int32_t code = TSDB_CODE_SUCCESS;
921,314✔
225

226
  // check request
227
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
921,314✔
228
  if (code) {
918,757✔
229
    TAOS_RETURN(code);
1,532✔
230
  }
231

232
  // handle entry
233
  SMetaEntry entry = {
917,225✔
234
      .version = verison,
235
      .type = -TSDB_SUPER_TABLE,
236
      .uid = pReq->suid,
916,011✔
237
  };
238
  code = metaHandleEntry2(pMeta, &entry);
919,187✔
239
  if (code) {
920,972✔
240
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
241
              tstrerror(code));
242
  } else {
243
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
920,972✔
244
             pReq->suid, verison);
245
  }
246
  TAOS_RETURN(code);
920,972✔
247
}
248

249
// Alter Super Table
250

251
// Create Child Table
252
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
62,452,623✔
253
  int32_t   code = TSDB_CODE_SUCCESS;
62,452,623✔
254
  void     *value = NULL;
62,452,623✔
255
  int32_t   valueSize = 0;
62,450,523✔
256
  SMetaInfo info;
62,445,733✔
257

258
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
62,453,191✔
259
      pReq->ctb.suid == 0) {
62,455,414✔
260
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
13,511✔
261
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
262
    return TSDB_CODE_INVALID_MSG;
×
263
  }
264

265
  // check table existence
266
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
62,444,555✔
267
    pReq->uid = *(int64_t *)value;
336,412✔
268
    tdbFreeClear(value);
336,412✔
269

270
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
336,412✔
271
      metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
272
                ", which is an internal error, version:%" PRId64,
273
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
274
      return TSDB_CODE_INTERNAL_ERROR;
×
275
    }
276

277
    // check table type
278
    if (info.suid == info.uid || info.suid == 0) {
336,412✔
279
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
1,884✔
280
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
281
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
1,844✔
282
    }
283

284
    // check suid
285
    if (info.suid != pReq->ctb.suid) {
334,528✔
286
      metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " exists in another stable with uid %" PRId64
×
287
                " instead of stable with uid %" PRId64 " version:%" PRId64,
288
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, info.suid, pReq->ctb.suid,
289
                version);
290
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
291
    }
292

293
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
334,528✔
294
  }
295

296
  // check super table existence
297
  SMetaEntry *pStbEntry = NULL;
62,116,372✔
298
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
62,115,373✔
299
  if (code) {
62,103,642✔
300
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
301
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
302
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
303
  }
304

305
  if (pStbEntry->type != TSDB_SUPER_TABLE) {
62,103,642✔
306
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64,
×
307
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
308
    metaFetchEntryFree(&pStbEntry);
×
309
    return TSDB_CODE_INVALID_MSG;
×
310
  }
311

312
  if (pStbEntry->uid != pReq->ctb.suid) {
62,097,765✔
313
    metaError("vgId:%d, %s failed at %s:%d since super table %s uid %" PRId64 " does not match request uid %" PRId64
×
314
              ", version:%" PRId64,
315
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, pStbEntry->uid, pReq->ctb.suid,
316
              version);
317
    metaFetchEntryFree(&pStbEntry);
×
318
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
319
  }
320

321
  // Check tag value
322
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
62,095,132✔
323
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
62,108,681✔
324
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
62,096,015✔
325
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
274,077,004✔
326
      STagVal tagVal = {
212,250,500✔
327
          .cid = pTagSchema->pSchema[i].colId,
212,245,772✔
328
      };
329

330
      if (tTagGet(pTag, &tagVal)) {
212,236,894✔
331
        if (pTagSchema->pSchema[i].type != tagVal.type) {
168,731,147✔
332
          metaError("vgId:%d, %s failed at %s:%d since child table %s tag type does not match the expected type, version:%" PRId64,
×
333
                    TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, version);
334
          metaFetchEntryFree(&pStbEntry);
×
335
          return TSDB_CODE_INVALID_MSG;
×
336
        }
337
      }
338
    }
339
  }
340

341
  metaFetchEntryFree(&pStbEntry);
62,122,294✔
342

343
  // check grant
344
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
62,092,502✔
345
    code = grantCheck(TSDB_GRANT_TIMESERIES);
62,108,542✔
346
    if (TSDB_CODE_SUCCESS != code) {
62,067,068✔
347
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
348
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
349
    }
350
  }
351
  return code;
62,085,132✔
352
}
353

354
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
61,764,859✔
355
  int32_t code = TSDB_CODE_SUCCESS;
61,764,859✔
356

357
  if (NULL == ppRsp) {
61,764,859✔
358
    return code;
×
359
  }
360

361
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
61,764,859✔
362
  if (NULL == ppRsp) {
61,754,093✔
363
    return terrno;
×
364
  }
365

366
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
61,754,093✔
367
  (*ppRsp)->tuid = pEntry->uid;
61,766,756✔
368
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
61,777,828✔
369
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
61,773,467✔
370

371
  return code;
61,785,746✔
372
}
373

374
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
62,126,471✔
375
  int32_t code = TSDB_CODE_SUCCESS;
62,126,471✔
376

377
  // check request
378
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
62,126,471✔
379
  if (code) {
62,097,567✔
380
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
336,412✔
381
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,844✔
382
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
383
    }
384
    return code;
336,372✔
385
  }
386

387
  SMetaEntry entry = {
61,761,155✔
388
      .version = version,
389
      .type = TSDB_CHILD_TABLE,
390
      .uid = pReq->uid,
61,767,226✔
391
      .name = pReq->name,
61,761,794✔
392
      .ctbEntry.btime = pReq->btime,
61,774,486✔
393
      .ctbEntry.ttlDays = pReq->ttl,
61,761,293✔
394
      .ctbEntry.commentLen = pReq->commentLen,
61,760,068✔
395
      .ctbEntry.comment = pReq->comment,
61,752,654✔
396
      .ctbEntry.suid = pReq->ctb.suid,
61,770,292✔
397
      .ctbEntry.pTags = pReq->ctb.pTag,
61,759,103✔
398
  };
399

400
  // build response
401
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
61,751,310✔
402
  if (code) {
61,781,968✔
403
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
404
              tstrerror(code));
405
  }
406

407
  // handle entry
408
  code = metaHandleEntry2(pMeta, &entry);
61,781,968✔
409
  if (TSDB_CODE_SUCCESS == code) {
61,790,170✔
410
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
61,791,353✔
411
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
412
  } else {
413
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
414
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
415
              pReq->ctb.suid, version);
416
  }
417
  return code;
61,797,464✔
418
}
419

420
// Drop Child Table
421

422
// Alter Child Table
423

424
// Create Normal Table
425
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
7,501,363✔
426
  int32_t code = 0;
7,501,363✔
427
  void   *value = NULL;
7,501,363✔
428
  int32_t valueSize = 0;
7,501,363✔
429

430
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,501,363✔
431
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
432
              __FILE__, __LINE__, pReq->name, version);
433
    return TSDB_CODE_INVALID_MSG;
×
434
  }
435

436
  // check name
437
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
7,501,363✔
438
    // for auto create table, we return the uid of the existing table
439
    pReq->uid = *(tb_uid_t *)value;
33,228✔
440
    tdbFree(value);
33,228✔
441
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
33,228✔
442
  }
443

444
  // grant check
445
  code = grantCheck(TSDB_GRANT_TIMESERIES);
7,468,135✔
446
  if (code) {
7,468,135✔
447
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
448
              __FILE__, __LINE__, tstrerror(code), version, pReq->name);
449
  }
450
  return code;
7,468,135✔
451
}
452

453
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
7,287,363✔
454
  int32_t code = TSDB_CODE_SUCCESS;
7,287,363✔
455

456
  if (NULL == ppRsp) {
7,287,363✔
457
    return code;
×
458
  }
459

460
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
7,287,363✔
461
  if (NULL == *ppRsp) {
7,287,363✔
462
    return terrno;
×
463
  }
464

465
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, pEntry->ntbEntry.ownerId, *ppRsp);
7,287,363✔
466
  if (code) {
7,287,363✔
467
    taosMemoryFreeClear(*ppRsp);
×
468
    return code;
×
469
  }
470

471
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
82,746,379✔
472
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
75,459,016✔
473
    (*ppRsp)->pSchemaExt[i].colId = p->id;
75,459,016✔
474
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
75,459,016✔
475
    if (pEntry->pExtSchemas) {
75,459,016✔
476
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
468,581✔
477
    }
478
  }
479

480
  return code;
7,287,363✔
481
}
482

483
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
7,320,591✔
484
  int32_t code = TSDB_CODE_SUCCESS;
7,320,591✔
485

486
  // check request
487
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
7,320,591✔
488
  if (code) {
7,320,591✔
489
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
33,228✔
490
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
491
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
492
    }
493
    TAOS_RETURN(code);
33,228✔
494
  }
495

496
  SMetaEntry entry = {
14,574,300✔
497
      .version = version,
498
      .type = TSDB_NORMAL_TABLE,
499
      .uid = pReq->uid,
7,287,363✔
500
      .name = pReq->name,
7,287,363✔
501
      .ntbEntry.btime = pReq->btime,
7,287,363✔
502
      .ntbEntry.ttlDays = pReq->ttl,
7,287,363✔
503
      .ntbEntry.commentLen = pReq->commentLen,
7,287,363✔
504
      .ntbEntry.comment = pReq->comment,
7,287,363✔
505
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
506
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
7,287,363✔
507
      .ntbEntry.ownerId = pReq->ntb.userId,
7,287,363✔
508
      .colCmpr = pReq->colCmpr,
509
      .pExtSchemas = pReq->pExtSchemas,
7,287,363✔
510
  };
511
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,287,363✔
512

513
  // build response
514
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
7,287,363✔
515
  if (code) {
7,287,363✔
516
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
517
              tstrerror(code));
518
  }
519

520
  // handle entry
521
  code = metaHandleEntry2(pMeta, &entry);
7,287,363✔
522
  if (TSDB_CODE_SUCCESS == code) {
7,287,363✔
523
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
7,287,363✔
524
             pReq->name, pReq->uid);
525
  } else {
526
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
527
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
528
  }
529
  TAOS_RETURN(code);
7,287,363✔
530
}
531

532
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
180,772✔
533
  int32_t code = TSDB_CODE_SUCCESS;
180,772✔
534

535
  if (NULL == ppRsp) {
180,772✔
536
    return code;
×
537
  }
538

539
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
180,772✔
540
  if (NULL == *ppRsp) {
180,772✔
541
    return terrno;
×
542
  }
543

544
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, pEntry->ntbEntry.ownerId, *ppRsp,
180,772✔
545
                              TSDB_VIRTUAL_NORMAL_TABLE);
546
  if (code) {
180,772✔
547
    taosMemoryFreeClear(*ppRsp);
×
548
    return code;
×
549
  }
550

551
  return code;
180,772✔
552
}
553

554
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
180,772✔
555
  // check request
556
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
180,772✔
557
  if (code) {
180,772✔
558
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
559
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
560
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
561
    }
562
    TAOS_RETURN(code);
×
563
  }
564

565
  SMetaEntry entry = {.version = version,
361,490✔
566
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
567
                      .uid = pReq->uid,
180,772✔
568
                      .name = pReq->name,
180,772✔
569
                      .ntbEntry.btime = pReq->btime,
180,772✔
570
                      .ntbEntry.ttlDays = pReq->ttl,
180,772✔
571
                      .ntbEntry.commentLen = pReq->commentLen,
180,772✔
572
                      .ntbEntry.comment = pReq->comment,
180,772✔
573
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
574
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
180,772✔
575
                      .ntbEntry.ownerId = pReq->ntb.userId,
180,772✔
576
                      .colRef = pReq->colRef};
577

578
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
180,772✔
579
  if (code) {
180,772✔
580
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
581
              tstrerror(code));
582
    TAOS_RETURN(code);
×
583
  }
584

585
  // handle entry
586
  code = metaHandleEntry2(pMeta, &entry);
180,772✔
587
  if (TSDB_CODE_SUCCESS == code) {
180,772✔
588
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
180,772✔
589
             version, pReq->name, pReq->uid);
590
  } else {
591
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
592
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
593
  }
594
  TAOS_RETURN(code);
180,772✔
595
#if 0
596
  metaTimeSeriesNotifyCheck(pMeta);
597
#endif
598
}
599

600
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
324,607✔
601
  int32_t code = TSDB_CODE_SUCCESS;
324,607✔
602

603
  if (NULL == ppRsp) {
324,607✔
604
    return code;
×
605
  }
606

607
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
324,607✔
608
  if (NULL == *ppRsp) {
324,607✔
609
    return terrno;
×
610
  }
611

612
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, 0, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
324,607✔
613
  if (code) {
324,607✔
614
    taosMemoryFreeClear(*ppRsp);
×
615
    return code;
×
616
  }
617
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
324,607✔
618

619
  return code;
324,607✔
620
}
621

622
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
324,607✔
623
  // check request
624
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
324,607✔
625
  if (code) {
324,607✔
626
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
627
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
628
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
629
    }
630
    TAOS_RETURN(code);
×
631
  }
632

633
  SMetaEntry entry = {.version = version,
649,160✔
634
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
635
                      .uid = pReq->uid,
324,607✔
636
                      .name = pReq->name,
324,607✔
637
                      .ctbEntry.btime = pReq->btime,
324,607✔
638
                      .ctbEntry.ttlDays = pReq->ttl,
324,607✔
639
                      .ctbEntry.commentLen = pReq->commentLen,
324,607✔
640
                      .ctbEntry.comment = pReq->comment,
324,607✔
641
                      .ctbEntry.suid = pReq->ctb.suid,
324,607✔
642
                      .ctbEntry.pTags = pReq->ctb.pTag,
324,607✔
643
                      .colRef = pReq->colRef};
644

645
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
324,607✔
646
  if (code) {
324,607✔
647
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
648
              tstrerror(code));
649
    TAOS_RETURN(code);
×
650
  }
651

652
  // handle entry
653
  code = metaHandleEntry2(pMeta, &entry);
324,607✔
654
  if (TSDB_CODE_SUCCESS == code) {
324,607✔
655
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
324,607✔
656
             version, pReq->name, pReq->uid);
657
  } else {
658
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
659
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
660
  }
661
  TAOS_RETURN(code);
324,607✔
662
#if 0
663
  metaTimeSeriesNotifyCheck(pMeta);
664
#endif
665
}
666

667
// Drop Normal Table
668

669
// Alter Normal Table
670

671
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
69,951,435✔
672
  int32_t code = TSDB_CODE_SUCCESS;
69,951,435✔
673
  if (TSDB_CHILD_TABLE == pReq->type) {
69,951,435✔
674
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
62,128,791✔
675
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
7,825,970✔
676
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
7,320,591✔
677
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
505,379✔
678
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
180,772✔
679
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
324,607✔
680
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
324,607✔
681
  } else {
682
    code = TSDB_CODE_INVALID_MSG;
×
683
  }
684
  TAOS_RETURN(code);
69,959,049✔
685
}
686

687
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,552,524✔
688
  int32_t code = TSDB_CODE_SUCCESS;
1,552,524✔
689

690
  // check request
691
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,552,524✔
692
  if (code) {
1,552,524✔
693
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
694
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
695
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
696
    }
697
    TAOS_RETURN(code);
×
698
  }
699

700
  if (pReq->suid == pReq->uid) {
1,552,524✔
701
    code = TSDB_CODE_INVALID_PARA;
×
702
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
703
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
704
    TAOS_RETURN(code);
×
705
  }
706

707
  SMetaEntry entry = {
1,552,524✔
708
      .version = version,
709
      .uid = pReq->uid,
1,552,524✔
710
  };
711

712
  if (pReq->isVirtual) {
1,552,524✔
713
    if (pReq->suid == 0) {
71,025✔
714
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
39,773✔
715
    } else {
716
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
31,252✔
717
    }
718
  } else {
719
    if (pReq->suid == 0) {
1,481,499✔
720
      entry.type = -TSDB_NORMAL_TABLE;
824,729✔
721
    } else {
722
      entry.type = -TSDB_CHILD_TABLE;
656,770✔
723
    }
724
  }
725
  code = metaHandleEntry2(pMeta, &entry);
1,552,524✔
726
  if (code) {
1,552,524✔
727
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
728
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
729
  } else {
730
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,552,524✔
731
             pReq->uid, version);
732
  }
733
  TAOS_RETURN(code);
1,552,524✔
734
}
735

736
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,440,557✔
737
  int32_t code = 0;
4,440,557✔
738

739
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,440,557✔
740
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
741
              __func__, __FILE__, __LINE__, pReq->colName, version);
742
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
743
  }
744

745
  // check name
746
  void   *value = NULL;
4,440,557✔
747
  int32_t valueSize = 0;
4,440,557✔
748
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,440,557✔
749
  if (code) {
4,440,557✔
750
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
751
              __FILE__, __LINE__, pReq->tbName, version);
752
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
753
    TAOS_RETURN(code);
×
754
  }
755
  int64_t uid = *(int64_t *)value;
4,440,557✔
756
  tdbFreeClear(value);
4,440,557✔
757

758
  // check table type
759
  SMetaInfo info;
4,440,341✔
760
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,440,557✔
761
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
762
              " not found, this is an internal error in meta, version:%" PRId64,
763
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
764
    code = TSDB_CODE_INTERNAL_ERROR;
×
765
    TAOS_RETURN(code);
×
766
  }
767
  if (info.suid != 0 && pReq->action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF &&
4,440,557✔
768
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
29,773✔
769
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
770
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
771
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
772
    TAOS_RETURN(code);
×
773
  }
774

775
  // check grant
776
  code = grantCheck(TSDB_GRANT_TIMESERIES);
4,440,557✔
777
  if (code) {
4,440,557✔
778
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
779
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
780
    TAOS_RETURN(code);
×
781
  }
782
  TAOS_RETURN(code);
4,440,557✔
783
}
784

785
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,655,219✔
786
  int32_t code = TSDB_CODE_SUCCESS;
3,655,219✔
787

788
  // check request
789
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,655,219✔
790
  if (code) {
3,655,219✔
791
    TAOS_RETURN(code);
×
792
  }
793

794
  // fetch old entry
795
  SMetaEntry *pEntry = NULL;
3,655,219✔
796
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,655,219✔
797
  if (code) {
3,655,219✔
798
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
799
              __FILE__, __LINE__, pReq->tbName, version);
800
    TAOS_RETURN(code);
×
801
  }
802
  if (pEntry->version >= version) {
3,655,219✔
803
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
804
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
805
    metaFetchEntryFree(&pEntry);
×
806
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
807
  }
808

809
  // do add column
810
  int32_t         rowSize = 0;
3,655,219✔
811
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,655,219✔
812
  SSchema        *pColumn;
813
  SExtSchema      extSchema = {0};
3,655,219✔
814
  pEntry->version = version;
3,655,219✔
815
  for (int32_t i = 0; i < pSchema->nCols; i++) {
2,147,483,647✔
816
    pColumn = &pSchema->pSchema[i];
2,147,483,647✔
817
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
2,147,483,647✔
818
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
323,190✔
819
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
820
      metaFetchEntryFree(&pEntry);
323,190✔
821
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
323,190✔
822
    }
823
    rowSize += pColumn->bytes;
2,147,483,647✔
824
  }
825

826
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
3,332,029✔
827
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,332,029✔
828
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,650✔
829
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
830
    metaFetchEntryFree(&pEntry);
6,650✔
831
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,650✔
832
  }
833

834
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,325,379✔
835
  if (pSchema->nCols + 1 > maxCols) {
3,325,379✔
836
    metaError("vgId:%d, %s failed at %s:%d since column count %d + 1 > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
837
              __func__, __FILE__, __LINE__, pSchema->nCols, maxCols, version);
838
    metaFetchEntryFree(&pEntry);
×
839
    TAOS_RETURN(TSDB_CODE_PAR_TOO_MANY_COLUMNS);
×
840
  }
841

842
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
3,325,379✔
843
  if (NULL == pNewSchema) {
3,325,379✔
844
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
845
              __LINE__, tstrerror(terrno), version);
846
    metaFetchEntryFree(&pEntry);
×
847
    TAOS_RETURN(terrno);
×
848
  }
849
  pSchema->pSchema = pNewSchema;
3,325,379✔
850
  pSchema->version++;
3,325,379✔
851
  pSchema->nCols++;
3,325,379✔
852
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,325,379✔
853
  pColumn->bytes = pReq->bytes;
3,325,379✔
854
  pColumn->type = pReq->type;
3,325,379✔
855
  pColumn->flags = pReq->flags;
3,325,379✔
856
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,325,379✔
857
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
549✔
858
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
859
              version);
860
    metaFetchEntryFree(&pEntry);
549✔
861
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
549✔
862
  }
863
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,324,830✔
864
  extSchema.typeMod = pReq->typeMod;
3,324,830✔
865
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,324,830✔
866
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,324,830✔
867
    SColRef tmpRef;
55,385✔
868
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
55,439✔
869
      tmpRef.hasRef = false;
31,776✔
870
      tmpRef.id = pColumn->colId;
31,776✔
871
    } else {
872
      tmpRef.hasRef = true;
23,663✔
873
      tmpRef.id = pColumn->colId;
23,663✔
874
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
23,663✔
875
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
23,663✔
876
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
23,663✔
877
    }
878
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
55,439✔
879
    if (code) {
55,439✔
880
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
881
                __LINE__, tstrerror(code), version);
882
      metaFetchEntryFree(&pEntry);
×
883
      TAOS_RETURN(code);
×
884
    }
885
  } else {
886
    uint32_t compress;
887
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
3,269,391✔
888
      compress = createDefaultColCmprByType(pColumn->type);
3,259,521✔
889
    } else {
890
      compress = pReq->compress;
9,870✔
891
    }
892
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
3,269,391✔
893
    if (code) {
3,269,391✔
894
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
895
                __LINE__, tstrerror(code), version);
896
      metaFetchEntryFree(&pEntry);
×
897
      TAOS_RETURN(code);
×
898
    }
899
  }
900
  code = addTableExtSchema(pEntry, pColumn, pSchema->nCols, &extSchema);
3,324,830✔
901
  if (code) {
3,324,830✔
902
    metaError("vgId:%d, %s failed to add ext schema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
903
              __func__, __FILE__, __LINE__, tstrerror(code), version);
904
    metaFetchEntryFree(&pEntry);
×
905
    TAOS_RETURN(code);
×
906
  }
907

908
  // do handle entry
909
  code = metaHandleEntry2(pMeta, pEntry);
3,324,830✔
910
  if (code) {
3,324,830✔
911
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
912
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
913
    metaFetchEntryFree(&pEntry);
×
914
    TAOS_RETURN(code);
×
915
  } else {
916
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,324,830✔
917
             pEntry->uid, version);
918
  }
919

920
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,324,830✔
921
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
55,439✔
922
                                pEntry->type);
55,439✔
923
    if (code) {
55,439✔
924
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
925
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
926
    } else {
927
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,397,474✔
928
        SColRef *p = &pEntry->colRef.pColRef[i];
18,342,035✔
929
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,342,035✔
930
        pRsp->pColRefs[i].id = p->id;
18,342,035✔
931
        if (p->hasRef) {
18,342,035✔
932
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
178,042✔
933
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
178,042✔
934
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
178,042✔
935
        }
936
      }
937
    }
938
  } else {
939
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,269,391✔
940
    if (code) {
3,269,391✔
941
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
942
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
943
    } else {
944
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
945
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
946
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
947
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
948
      }
949
    }
950
  }
951

952
  metaFetchEntryFree(&pEntry);
3,324,830✔
953
  TAOS_RETURN(code);
3,324,830✔
954
}
955

956
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
95,695✔
957
  int32_t code = TSDB_CODE_SUCCESS;
95,695✔
958

959
  // check request
960
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
95,695✔
961
  if (code) {
95,695✔
962
    TAOS_RETURN(code);
×
963
  }
964

965
  // fetch old entry
966
  SMetaEntry *pEntry = NULL;
95,695✔
967
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
95,695✔
968
  if (code) {
95,695✔
969
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
970
              __FILE__, __LINE__, pReq->tbName, version);
971
    TAOS_RETURN(code);
×
972
  }
973

974
  if (pEntry->version >= version) {
95,695✔
975
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
976
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
977
    metaFetchEntryFree(&pEntry);
×
978
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
979
  }
980

981
  // search the column to drop
982
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
95,695✔
983
  SSchema        *pColumn = NULL;
95,695✔
984
  SSchema         tColumn;
95,587✔
985
  int32_t         iColumn = 0;
95,695✔
986
  for (; iColumn < pSchema->nCols; iColumn++) {
58,320,924✔
987
    pColumn = &pSchema->pSchema[iColumn];
58,320,924✔
988
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
58,320,924✔
989
      break;
95,695✔
990
    }
991
  }
992

993
  if (iColumn == pSchema->nCols) {
95,695✔
994
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
995
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
996
    metaFetchEntryFree(&pEntry);
×
997
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
998
  }
999

1000
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
95,695✔
1001
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1002
              __func__, __FILE__, __LINE__, pReq->colName, version);
1003
    metaFetchEntryFree(&pEntry);
×
1004
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1005
  }
1006

1007
  tColumn = *pColumn;
95,695✔
1008

1009
  // do drop column
1010
  pEntry->version = version;
95,695✔
1011
  if (pSchema->nCols - iColumn - 1 > 0) {
95,695✔
1012
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
70,687✔
1013
  }
1014
  pSchema->nCols--;
95,695✔
1015
  pSchema->version++;
95,695✔
1016
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
95,695✔
1017
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
30,657✔
1018
    if (code) {
30,657✔
1019
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1020
                __LINE__, tstrerror(code), version);
1021
      metaFetchEntryFree(&pEntry);
×
1022
      TAOS_RETURN(code);
×
1023
    }
1024
    if (pEntry->colRef.nCols != pSchema->nCols) {
30,657✔
1025
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1026
                __func__, __FILE__, __LINE__, version);
1027
      metaFetchEntryFree(&pEntry);
×
1028
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1029
    }
1030
  } else {
1031
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
65,038✔
1032
    if (code) {
65,038✔
1033
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1034
                __LINE__, tstrerror(code), version);
1035
      metaFetchEntryFree(&pEntry);
×
1036
      TAOS_RETURN(code);
×
1037
    }
1038
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
65,038✔
1039
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1040
                __func__, __FILE__, __LINE__, version);
1041
      metaFetchEntryFree(&pEntry);
×
1042
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1043
    }
1044
  }
1045

1046
  // update column extschema
1047
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
95,695✔
1048
  if (code) {
95,695✔
1049
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1050
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1051
    metaFetchEntryFree(&pEntry);
×
1052
    TAOS_RETURN(code);
×
1053
  }
1054

1055
  // do handle entry
1056
  code = metaHandleEntry2(pMeta, pEntry);
95,695✔
1057
  if (code) {
95,695✔
1058
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1059
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1060
    metaFetchEntryFree(&pEntry);
×
1061
    TAOS_RETURN(code);
×
1062
  } else {
1063
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
95,695✔
1064
             pEntry->uid, version);
1065
  }
1066

1067
  // build response
1068
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
95,695✔
1069
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
30,657✔
1070
                                pEntry->type);
30,657✔
1071
    if (code) {
30,657✔
1072
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1073
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1074
    } else {
1075
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
36,172,998✔
1076
        SColRef *p = &pEntry->colRef.pColRef[i];
36,142,341✔
1077
        pRsp->pColRefs[i].hasRef = p->hasRef;
36,142,341✔
1078
        pRsp->pColRefs[i].id = p->id;
36,142,341✔
1079
        if (p->hasRef) {
36,142,341✔
1080
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
18,084,176✔
1081
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
18,084,176✔
1082
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
18,084,176✔
1083
        }
1084
      }
1085
    }
1086
  } else {
1087
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
65,038✔
1088
    if (code) {
65,038✔
1089
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1090
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1091
    } else {
1092
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
43,092,978✔
1093
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
43,027,940✔
1094
        pRsp->pSchemaExt[i].colId = p->id;
43,027,940✔
1095
        pRsp->pSchemaExt[i].compress = p->alg;
43,027,940✔
1096
      }
1097
    }
1098
  }
1099

1100
  metaFetchEntryFree(&pEntry);
95,695✔
1101
  TAOS_RETURN(code);
95,695✔
1102
}
1103

1104
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
41,426✔
1105
  int32_t code = TSDB_CODE_SUCCESS;
41,426✔
1106

1107
  // check request
1108
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
41,426✔
1109
  if (code) {
41,426✔
1110
    TAOS_RETURN(code);
×
1111
  }
1112

1113
  if (NULL == pReq->colNewName) {
41,426✔
1114
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1115
              __func__, __FILE__, __LINE__, version);
1116
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1117
  }
1118

1119
  // fetch old entry
1120
  SMetaEntry *pEntry = NULL;
41,426✔
1121
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
41,426✔
1122
  if (code) {
41,426✔
1123
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1124
              __FILE__, __LINE__, pReq->tbName, version);
1125
    TAOS_RETURN(code);
×
1126
  }
1127

1128
  if (pEntry->version >= version) {
41,426✔
1129
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1130
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1131
    metaFetchEntryFree(&pEntry);
×
1132
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1133
  }
1134

1135
  // search the column to update
1136
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
41,426✔
1137
  SSchema        *pColumn = NULL;
41,426✔
1138
  int32_t         iColumn = 0;
41,426✔
1139
  for (int32_t i = 0; i < pSchema->nCols; i++) {
193,581✔
1140
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
193,581✔
1141
      pColumn = &pSchema->pSchema[i];
41,426✔
1142
      iColumn = i;
41,426✔
1143
      break;
41,426✔
1144
    }
1145
  }
1146

1147
  if (NULL == pColumn) {
41,426✔
1148
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1149
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1150
    metaFetchEntryFree(&pEntry);
×
1151
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1152
  }
1153

1154

1155
  // do update column name
1156
  pEntry->version = version;
41,426✔
1157
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
41,426✔
1158
  pSchema->version++;
41,426✔
1159

1160
  // do handle entry
1161
  code = metaHandleEntry2(pMeta, pEntry);
41,426✔
1162
  if (code) {
41,426✔
1163
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1164
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1165
    metaFetchEntryFree(&pEntry);
×
1166
    TAOS_RETURN(code);
×
1167
  } else {
1168
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
41,426✔
1169
             pEntry->uid, version);
1170
  }
1171

1172
  // build response
1173
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
41,426✔
1174
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
28,530✔
1175
                                pEntry->type);
28,530✔
1176
    if (code) {
28,530✔
1177
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1178
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1179
    } else {
1180
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
201,602✔
1181
        SColRef *p = &pEntry->colRef.pColRef[i];
173,072✔
1182
        pRsp->pColRefs[i].hasRef = p->hasRef;
173,072✔
1183
        pRsp->pColRefs[i].id = p->id;
173,072✔
1184
        if (p->hasRef) {
173,072✔
1185
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
106,806✔
1186
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
106,806✔
1187
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
106,806✔
1188
        }
1189
      }
1190
    }
1191
  } else {
1192
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
12,896✔
1193
    if (code) {
12,896✔
1194
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1195
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1196
    } else {
1197
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
164,143✔
1198
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
151,247✔
1199
        pRsp->pSchemaExt[i].colId = p->id;
151,247✔
1200
        pRsp->pSchemaExt[i].compress = p->alg;
151,247✔
1201
      }
1202
    }
1203
  }
1204

1205
  metaFetchEntryFree(&pEntry);
41,426✔
1206
  TAOS_RETURN(code);
41,426✔
1207
}
1208

1209
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
508,817✔
1210
  int32_t code = TSDB_CODE_SUCCESS;
508,817✔
1211

1212
  // check request
1213
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
508,817✔
1214
  if (code) {
508,817✔
1215
    TAOS_RETURN(code);
×
1216
  }
1217

1218
  // fetch old entry
1219
  SMetaEntry *pEntry = NULL;
508,817✔
1220
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
508,817✔
1221
  if (code) {
508,817✔
1222
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1223
              __FILE__, __LINE__, pReq->tbName, version);
1224
    TAOS_RETURN(code);
×
1225
  }
1226

1227
  if (pEntry->version >= version) {
508,817✔
1228
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1229
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1230
    metaFetchEntryFree(&pEntry);
×
1231
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1232
  }
1233

1234
  // search the column to update
1235
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
508,817✔
1236
  SSchema        *pColumn = NULL;
508,817✔
1237
  int32_t         iColumn = 0;
508,817✔
1238
  int32_t         rowSize = 0;
508,817✔
1239
  for (int32_t i = 0; i < pSchema->nCols; i++) {
38,110,605✔
1240
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
37,601,788✔
1241
      pColumn = &pSchema->pSchema[i];
508,817✔
1242
      iColumn = i;
508,817✔
1243
    }
1244
    rowSize += pSchema->pSchema[i].bytes;
37,601,788✔
1245
  }
1246

1247
  if (NULL == pColumn) {
508,817✔
1248
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1249
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1250
    metaFetchEntryFree(&pEntry);
×
1251
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1252
  }
1253

1254
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
508,817✔
1255
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
207,480✔
1256
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1257
              version);
1258
    metaFetchEntryFree(&pEntry);
207,480✔
1259
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
207,480✔
1260
  }
1261

1262
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
301,337✔
1263
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
301,337✔
1264
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
46,550✔
1265
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1266
              version);
1267
    metaFetchEntryFree(&pEntry);
46,550✔
1268
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
46,550✔
1269
  }
1270

1271
  // do change the column bytes
1272
  pEntry->version = version;
254,787✔
1273
  pSchema->version++;
254,787✔
1274
  pColumn->bytes = pReq->colModBytes;
254,787✔
1275

1276
  // do handle entry
1277
  code = metaHandleEntry2(pMeta, pEntry);
254,787✔
1278
  if (code) {
254,787✔
1279
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1280
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1281
    metaFetchEntryFree(&pEntry);
×
1282
    TAOS_RETURN(code);
×
1283
  } else {
1284
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
254,787✔
1285
             pEntry->uid, version);
1286
  }
1287

1288
  // build response
1289
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
254,787✔
1290
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
27,693✔
1291
                                pEntry->type);
27,693✔
1292
    if (code) {
27,693✔
1293
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1294
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1295
    } else {
1296
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,178,265✔
1297
        SColRef *p = &pEntry->colRef.pColRef[i];
18,150,572✔
1298
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,150,572✔
1299
        pRsp->pColRefs[i].id = p->id;
18,150,572✔
1300
        if (p->hasRef) {
18,150,572✔
1301
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
80,800✔
1302
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
80,800✔
1303
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
80,800✔
1304
        }
1305
      }
1306
    }
1307
  } else {
1308
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
227,094✔
1309
    if (code) {
227,094✔
1310
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1311
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1312
    } else {
1313
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,263,080✔
1314
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,035,986✔
1315
        pRsp->pSchemaExt[i].colId = p->id;
9,035,986✔
1316
        pRsp->pSchemaExt[i].compress = p->alg;
9,035,986✔
1317
      }
1318
    }
1319
  }
1320

1321
  metaFetchEntryFree(&pEntry);
254,787✔
1322
  TAOS_RETURN(code);
254,787✔
1323
}
1324

1325

1326

1327
static bool tagValueChanged(const SUpdatedTagVal* pNewVal, const STag *pOldTag) {
8,293,767✔
1328
  STagVal oldVal = { .cid = pNewVal->colId };
8,293,767✔
1329

1330
  if (pNewVal->isNull) {
8,293,767✔
1331
    return tTagGet(pOldTag, &oldVal);
69,448✔
1332
  }
1333

1334
  if (!tTagGet(pOldTag, &oldVal)){
8,224,319✔
1335
    return true;
202,256✔
1336
  }
1337

1338
  if (!IS_VAR_DATA_TYPE(oldVal.type)) {
8,021,444✔
1339
    return (memcmp(&oldVal.i64, pNewVal->pTagVal, pNewVal->nTagVal) != 0);
7,786,519✔
1340
  }
1341

1342
  if (oldVal.nData != pNewVal->nTagVal) {
234,925✔
1343
    return true;
179,124✔
1344
  }
1345

1346
  return (memcmp(pNewVal->pTagVal, oldVal.pData, oldVal.nData) != 0);
56,420✔
1347
}
1348

1349

1350

1351
static int32_t updatedTagValueArrayToHashMap(SSchemaWrapper* pTagSchema, SArray* arr, SHashObj **hashMap) {
8,121,419✔
1352
  int32_t numOfTags = arr == NULL ? 0 : taosArrayGetSize(arr);
8,121,419✔
1353
  if (numOfTags == 0) {
8,121,748✔
1354
    metaError("%s failed at %s:%d since no tags specified", __func__, __FILE__, __LINE__);
×
1355
    return TSDB_CODE_INVALID_MSG;
×
1356
  }
1357

1358
  *hashMap = taosHashInit(numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
8,121,748✔
1359
  if (*hashMap == NULL) {
8,122,113✔
1360
    metaError("%s failed at %s:%d since %s", __func__, __FILE__, __LINE__, tstrerror(terrno));
×
1361
    return terrno;
×
1362
  }
1363

1364
  for (int32_t i = 0; i < taosArrayGetSize(arr); i++) {
16,313,800✔
1365
    SUpdatedTagVal *pTagVal = taosArrayGet(arr, i);
8,191,687✔
1366
    if (taosHashGet(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId)) != NULL) {
8,192,381✔
1367
      metaError("%s failed at %s:%d since duplicate tags %s", __func__, __FILE__, __LINE__, pTagVal->tagName);
×
1368
      taosHashCleanup(*hashMap);
×
1369
      return TSDB_CODE_INVALID_MSG;
×
1370
    }
1371

1372
    int32_t code = taosHashPut(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId), pTagVal, sizeof(*pTagVal));
8,191,687✔
1373
    if (code) {
8,191,687✔
1374
      metaError("%s failed at %s:%d since %s", __func__, __FILE__, __LINE__, tstrerror(code));
×
1375
      taosHashCleanup(*hashMap);
×
1376
      return code;
×
1377
    }
1378
  }
1379

1380
  int32_t changed = 0;
8,122,113✔
1381
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,563,105✔
1382
    int32_t schemaColId = pTagSchema->pSchema[i].colId;
9,440,298✔
1383
    if (taosHashGet(*hashMap, &schemaColId, sizeof(schemaColId)) != NULL) {
9,440,298✔
1384
      changed++;
8,192,381✔
1385
    }
1386
  }
1387
  if (changed < numOfTags) {
8,122,807✔
1388
    metaError("%s failed at %s:%d since tag count mismatch, %d:%d", __func__, __FILE__, __LINE__, changed, numOfTags);
×
1389
    taosHashCleanup(*hashMap);
×
1390
    return TSDB_CODE_VND_COL_NOT_EXISTS;
×
1391
  }
1392

1393
  return TSDB_CODE_SUCCESS;
8,122,807✔
1394
}
1395

1396

1397

1398
static int32_t metaUpdateTableJsonTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
124,450✔
1399
  SSchema *pCol = &pTagSchema->pSchema[0];
124,450✔
1400
  int32_t colId = pCol->colId;
124,450✔
1401
  SUpdatedTagVal *pTagVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
124,450✔
1402
  void *pNewTag = taosMemoryRealloc(pTable->ctbEntry.pTags, pTagVal->nTagVal);
124,450✔
1403
  if (pNewTag == NULL) {
124,450✔
1404
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1405
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1406
    return TSDB_CODE_OUT_OF_MEMORY;
×
1407
  }
1408
  pTable->ctbEntry.pTags = pNewTag;
124,450✔
1409
  memcpy(pTable->ctbEntry.pTags, pTagVal->pTagVal, pTagVal->nTagVal);
124,450✔
1410

1411
  int32_t code = metaHandleEntry2(pMeta, pTable);
124,450✔
1412
  if (code) {
124,450✔
1413
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
×
1414
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTable->uid, pTable->name, pTable->version);
×
1415
  } else {
1416
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
124,450✔
1417
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
124,450✔
1418
  }
1419

1420
  return code;
124,450✔
1421
}
1422

1423

1424

1425
static int32_t metaUpdateTableNormalTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
8,226,819✔
1426
  int32_t code = TSDB_CODE_SUCCESS;
8,226,819✔
1427

1428
  const STag *pOldTag = (const STag *)pTable->ctbEntry.pTags;
8,226,819✔
1429
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
8,226,819✔
1430
  if (pTagArray == NULL) {
8,226,819✔
1431
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1432
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1433
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1434
  }
1435

1436
  bool allSame = true;
8,226,819✔
1437

1438
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,950,316✔
1439
    SSchema *pCol = &pTagSchema->pSchema[i];
9,728,130✔
1440
    STagVal  value = { .cid = pCol->colId };
9,727,400✔
1441

1442
    int32_t colId = pCol->colId;
9,728,130✔
1443
    SUpdatedTagVal *pNewVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
9,726,781✔
1444
    if (pNewVal == NULL) {
9,728,130✔
1445
      if (!tTagGet(pOldTag, &value)) {
1,434,363✔
1446
        continue;
298,018✔
1447
      }
1448
    } else {
1449
      value.type = pCol->type;
8,293,767✔
1450
      if (tagValueChanged(pNewVal, pOldTag)) {
8,293,767✔
1451
        allSame = false;
8,132,309✔
1452
      }
1453
      if (pNewVal->isNull) {
8,293,148✔
1454
        continue;
69,448✔
1455
      }
1456

1457
      if (IS_VAR_DATA_TYPE(pCol->type)) {
8,223,954✔
1458
        if ((int32_t)pNewVal->nTagVal > (pCol->bytes - VARSTR_HEADER_SIZE)) {
322,506✔
1459
          const char* msgFmt = "vgId:%d, %s failed at %s:%d since value too long for tag %s, version:%" PRId64;
3,939✔
1460
          metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pCol->name, pTable->version);
3,939✔
1461
          taosArrayDestroy(pTagArray);
3,939✔
1462
          TAOS_RETURN(TSDB_CODE_PAR_VALUE_TOO_LONG);
3,939✔
1463
        }
1464
        value.pData = pNewVal->pTagVal;
318,567✔
1465
        value.nData = pNewVal->nTagVal;
318,932✔
1466
      } else {
1467
        memcpy(&value.i64, pNewVal->pTagVal, pNewVal->nTagVal);
7,901,448✔
1468
      }
1469
    }
1470

1471
    if (taosArrayPush(pTagArray, &value) == NULL) {
9,356,031✔
1472
      const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1473
      metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1474
      taosArrayDestroy(pTagArray);
×
1475
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1476
    }
1477
  }
1478

1479
  if (allSame) {
8,222,186✔
1480
    const char* msgFmt = "vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64;
147,995✔
1481
    metaWarn(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pTable->version);
147,995✔
1482
    taosArrayDestroy(pTagArray);
147,995✔
1483
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
147,995✔
1484
  } 
1485

1486
  STag *pNewTag = NULL;
8,074,191✔
1487
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
8,074,191✔
1488
  if (code) {
8,074,191✔
1489
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1490
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTable->version);
×
1491
    taosArrayDestroy(pTagArray);
×
1492
    TAOS_RETURN(code);
×
1493
  }
1494
  taosArrayDestroy(pTagArray);
8,074,191✔
1495
  taosMemoryFree(pTable->ctbEntry.pTags);
8,073,901✔
1496
  pTable->ctbEntry.pTags = (uint8_t *)pNewTag;
8,074,520✔
1497
  return TSDB_CODE_SUCCESS;
8,074,885✔
1498
}
1499

1500

1501
static int32_t regexReplaceBuildSubstitution(const char *pReplace, const regmatch_t *pmatch, int32_t nmatch,
13,860✔
1502
                                               const char *cursor, char **ppResult, size_t *pResultLen,
1503
                                               size_t *pResultCap) {
1504
  char  *result = *ppResult;
13,860✔
1505
  size_t resultLen = *pResultLen;
13,860✔
1506
  size_t resultCap = *pResultCap;
13,860✔
1507

1508
  const char *r = pReplace;
13,860✔
1509
  while (*r != '\0') {
201,624✔
1510
    const char *chunk = NULL;
187,764✔
1511
    size_t      chunkLen = 0;
187,764✔
1512

1513
    if (*r == '$' && r[1] >= '0' && r[1] <= '9') {
187,764✔
1514
      int32_t groupIdx = r[1] - '0';
21,008✔
1515
      if (groupIdx < nmatch && pmatch[groupIdx].rm_so != -1) {
21,008✔
1516
        chunk = cursor + pmatch[groupIdx].rm_so;
21,008✔
1517
        chunkLen = (size_t)(pmatch[groupIdx].rm_eo - pmatch[groupIdx].rm_so);
21,008✔
1518
      }
1519
      r += 2;
21,008✔
1520
    } else if (*r == '$' && r[1] == '$') {
166,756✔
1521
      chunk = "$";
×
1522
      chunkLen = 1;
×
1523
      r += 2;
×
1524
    } else {
1525
      chunk = r;
166,756✔
1526
      chunkLen = 1;
166,756✔
1527
      r += 1;
166,756✔
1528
    }
1529

1530
    if (chunkLen > 0) {
187,764✔
1531
      if (resultLen + chunkLen >= resultCap) {
187,764✔
1532
        resultCap = (resultLen + chunkLen) * 2 + 1;
5,982✔
1533
        char *tmp = taosMemoryRealloc(result, resultCap);
5,982✔
1534
        if (NULL == tmp) {
5,982✔
1535
          taosMemoryFree(result);
×
1536
          *ppResult = NULL;
×
1537
          return TSDB_CODE_OUT_OF_MEMORY;
×
1538
        }
1539
        result = tmp;
5,982✔
1540
      }
1541
      (void)memcpy(result + resultLen, chunk, chunkLen);
187,764✔
1542
      resultLen += chunkLen;
187,764✔
1543
    }
1544
  }
1545

1546
  *ppResult = result;
13,860✔
1547
  *pResultLen = resultLen;
13,860✔
1548
  *pResultCap = resultCap;
13,860✔
1549
  return TSDB_CODE_SUCCESS;
13,860✔
1550
}
1551

1552
static int32_t regexReplace(const char *pStr, const char *pPattern, const char *pReplace, char **ppResult) {
15,173✔
1553
  regex_t *regex = NULL;
15,173✔
1554
  int32_t  code = threadGetRegComp(&regex, pPattern);
15,173✔
1555
  if (code != 0) {
15,173✔
1556
    return code;
×
1557
  }
1558

1559
  size_t strLen = strlen(pStr);
15,173✔
1560
  size_t resultCap = strLen + 1;
15,173✔
1561
  size_t resultLen = 0;
15,173✔
1562
  char  *result = taosMemoryMalloc(resultCap);
15,173✔
1563
  if (NULL == result) {
15,173✔
1564
    return TSDB_CODE_OUT_OF_MEMORY;
×
1565
  }
1566

1567
  const int32_t nmatch = 10;
15,173✔
1568
  const char   *cursor = pStr;
15,173✔
1569
  regmatch_t    pmatch[10];
15,173✔
1570
  int32_t       execFlags = 0;
15,173✔
1571

1572
  while (*cursor != '\0') {
29,033✔
1573
    int ret = regexec(regex, cursor, nmatch, pmatch, execFlags);
15,173✔
1574
    if (ret == REG_NOMATCH) {
15,173✔
1575
      break;
1,313✔
1576
    }
1577
    if (ret != 0) {
13,860✔
1578
      taosMemoryFree(result);
×
1579
      return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
×
1580
    }
1581

1582
    size_t prefixLen = (size_t)pmatch[0].rm_so;
13,860✔
1583
    if (resultLen + prefixLen >= resultCap) {
13,860✔
1584
      resultCap = (resultLen + prefixLen) * 2 + 1;
×
1585
      char *tmp = taosMemoryRealloc(result, resultCap);
×
1586
      if (NULL == tmp) {
×
1587
        taosMemoryFree(result);
×
1588
        return TSDB_CODE_OUT_OF_MEMORY;
×
1589
      }
1590
      result = tmp;
×
1591
    }
1592
    (void)memcpy(result + resultLen, cursor, prefixLen);
13,860✔
1593
    resultLen += prefixLen;
13,860✔
1594

1595
    code = regexReplaceBuildSubstitution(pReplace, pmatch, nmatch, cursor, &result, &resultLen, &resultCap);
13,860✔
1596
    if (code != TSDB_CODE_SUCCESS) {
13,860✔
1597
      return code;
×
1598
    }
1599

1600
    if (pmatch[0].rm_so == pmatch[0].rm_eo) {
13,860✔
1601
      if (resultLen + 1 >= resultCap) {
×
1602
        resultCap = resultCap * 2 + 1;
×
1603
        char *tmp = taosMemoryRealloc(result, resultCap);
×
1604
        if (NULL == tmp) {
×
1605
          taosMemoryFree(result);
×
1606
          return TSDB_CODE_OUT_OF_MEMORY;
×
1607
        }
1608
        result = tmp;
×
1609
      }
1610
      result[resultLen++] = cursor[pmatch[0].rm_eo];
×
1611
      cursor += pmatch[0].rm_eo + 1;
×
1612
    } else {
1613
      cursor += pmatch[0].rm_eo;
13,860✔
1614
    }
1615

1616
    execFlags = REG_NOTBOL;
13,860✔
1617
  }
1618

1619
  size_t tailLen = strlen(cursor);
15,173✔
1620
  if (resultLen + tailLen + 1 > resultCap) {
15,173✔
1621
    resultCap = resultLen + tailLen + 1;
×
1622
    char *tmp = taosMemoryRealloc(result, resultCap);
×
1623
    if (NULL == tmp) {
×
1624
      taosMemoryFree(result);
×
1625
      return terrno;
×
1626
    }
1627
    result = tmp;
×
1628
  }
1629
  (void)memcpy(result + resultLen, cursor, tailLen);
15,173✔
1630
  resultLen += tailLen;
15,173✔
1631
  result[resultLen] = '\0';
15,173✔
1632

1633
  *ppResult = result;
15,173✔
1634
  return TSDB_CODE_SUCCESS;
15,173✔
1635
}
1636

1637

1638

1639
static int32_t computeNewTagValViaRegexReplace(const STag* pOldTag, SUpdatedTagVal* pNewVal) {
19,112✔
1640
  STagVal oldTagVal = {.cid = pNewVal->colId};
19,112✔
1641
  if (!tTagGet(pOldTag, &oldTagVal)) {
19,112✔
1642
    pNewVal->isNull = 1;
3,939✔
1643
    pNewVal->pTagVal = NULL;
3,939✔
1644
    pNewVal->nTagVal = 0;
3,939✔
1645
    return TSDB_CODE_SUCCESS;
3,939✔
1646
  }
1647

1648
  bool isNchar = (pNewVal->tagType == TSDB_DATA_TYPE_NCHAR);
15,173✔
1649
  char* oldStr = NULL;
15,173✔
1650

1651
  if (isNchar) {
15,173✔
1652
    // NCHAR is stored as UCS-4, convert to MBS (VARCHAR) for regex processing
1653
    int32_t mbsLen = oldTagVal.nData + 1;
3,939✔
1654
    oldStr = taosMemoryMalloc(mbsLen);
3,939✔
1655
    if (oldStr == NULL) {
3,939✔
1656
      return TSDB_CODE_OUT_OF_MEMORY;
×
1657
    }
1658
    int32_t ret = taosUcs4ToMbs((TdUcs4*)oldTagVal.pData, oldTagVal.nData, oldStr, NULL);
3,939✔
1659
    if (ret < 0) {
3,939✔
1660
      taosMemoryFree(oldStr);
×
1661
      return ret;
×
1662
    }
1663
    oldStr[ret] = '\0';
3,939✔
1664
  } else {
1665
    // VARCHAR: build null-terminated string from old tag value
1666
    oldStr = taosMemoryMalloc(oldTagVal.nData + 1);
11,234✔
1667
    if (oldStr == NULL) {
11,234✔
1668
      return TSDB_CODE_OUT_OF_MEMORY;
×
1669
    }
1670
    memcpy(oldStr, oldTagVal.pData, oldTagVal.nData);
11,234✔
1671
    oldStr[oldTagVal.nData] = '\0';
11,234✔
1672
  }
1673

1674
  char* newStr = NULL;
15,173✔
1675
  int32_t code = regexReplace(oldStr, pNewVal->regexp, pNewVal->replacement, &newStr);
15,173✔
1676
  taosMemoryFree(oldStr);
15,173✔
1677
  if (code != TSDB_CODE_SUCCESS) {
15,173✔
1678
    return code;
×
1679
  }
1680

1681
  if (isNchar) {
15,173✔
1682
    // Convert regex result back from MBS to UCS-4 (NCHAR)
1683
    int32_t newStrLen = (int32_t)strlen(newStr);
3,939✔
1684
    int32_t ucs4BufLen = (newStrLen + 1) * TSDB_NCHAR_SIZE;
3,939✔
1685
    char*   ucs4Buf = taosMemoryMalloc(ucs4BufLen);
3,939✔
1686
    if (ucs4Buf == NULL) {
3,939✔
1687
      taosMemoryFree(newStr);
×
1688
      return TSDB_CODE_OUT_OF_MEMORY;
×
1689
    }
1690

1691
    int32_t ucs4Len = 0;
3,939✔
1692
    bool    cvtOk = taosMbsToUcs4(newStr, newStrLen, (TdUcs4*)ucs4Buf, ucs4BufLen, &ucs4Len, NULL);
3,939✔
1693
    taosMemoryFree(newStr);
3,939✔
1694
    if (!cvtOk) {
3,939✔
1695
      taosMemoryFree(ucs4Buf);
×
1696
      return terrno;
×
1697
    }
1698

1699
    pNewVal->isNull = 0;
3,939✔
1700
    pNewVal->pTagVal = (uint8_t*)ucs4Buf;
3,939✔
1701
    pNewVal->nTagVal = (uint32_t)ucs4Len;
3,939✔
1702
  } else {
1703
    pNewVal->isNull = 0;
11,234✔
1704
    pNewVal->pTagVal = (uint8_t*)newStr;
11,234✔
1705
    pNewVal->nTagVal = (uint32_t)strlen(newStr);
11,234✔
1706
  }
1707

1708
  return TSDB_CODE_SUCCESS;
15,173✔
1709
}
1710

1711

1712

1713
static int32_t metaUpdateTableTagValueImpl(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
8,351,269✔
1714
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8,351,269✔
1715
    return metaUpdateTableJsonTagValue(pMeta, pTable, pTagSchema, pUpdatedTagVals);
124,450✔
1716
  }
1717
  
1718
  int32_t   code = TSDB_CODE_SUCCESS, lino = 0;
8,226,819✔
1719
  SHashObj* pNewTagVals = pUpdatedTagVals;
8,226,819✔
1720
  SArray*   pRegexResults = NULL;
8,226,819✔
1721

1722
  void* pIter = taosHashIterate(pUpdatedTagVals, NULL);
8,226,819✔
1723
  while (pIter) {
16,501,474✔
1724
    SUpdatedTagVal* pVal = (SUpdatedTagVal*)pIter;
8,285,889✔
1725
    if (pVal->regexp != NULL) {
8,285,889✔
1726
      break;
11,234✔
1727
    }
1728
    pIter = taosHashIterate(pUpdatedTagVals, pIter);
8,274,655✔
1729
  }
1730

1731
  // if there are regular expressions, compute new tag values and store in a new hash map to avoid
1732
  // modifying the original tag values which may be reused for computing other tag values
1733
  if (pIter != NULL) {
8,226,819✔
1734
    taosHashCancelIterate(pUpdatedTagVals, pIter);
11,234✔
1735
    pIter = NULL;
11,234✔
1736
    
1737
    int32_t sz = taosHashGetSize(pUpdatedTagVals);
11,234✔
1738
    pNewTagVals = taosHashInit(sz, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
11,234✔
1739
    if (pNewTagVals == NULL) {
11,234✔
1740
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1741
    }
1742

1743
    pRegexResults = taosArrayInit(sz, sizeof(char*));
11,234✔
1744
    if (pRegexResults == NULL) {
11,234✔
1745
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1746
    }
1747

1748
    pIter = taosHashIterate(pUpdatedTagVals, NULL);
11,234✔
1749
    while (pIter) {
30,346✔
1750
      int32_t       colId = *(int32_t*)taosHashGetKey(pIter, NULL);
19,112✔
1751
      SUpdatedTagVal newVal = *(SUpdatedTagVal *)pIter;
19,112✔
1752
      pIter = taosHashIterate(pUpdatedTagVals, pIter);
19,112✔
1753

1754
      if (newVal.regexp != NULL) {
19,112✔
1755
        const STag* pOldTag = (const STag*)pTable->ctbEntry.pTags;
19,112✔
1756
        TAOS_CHECK_GOTO(computeNewTagValViaRegexReplace(pOldTag, &newVal), &lino, _exit);
19,112✔
1757

1758
        newVal.regexp = NULL;
19,112✔
1759
        newVal.replacement = NULL;
19,112✔
1760
        if (taosArrayPush(pRegexResults, &newVal.pTagVal) == NULL) {
19,112✔
1761
          TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1762
        }
1763
      }
1764

1765
      TAOS_CHECK_GOTO(taosHashPut(pNewTagVals, &colId, sizeof(colId), &newVal, sizeof(newVal)), &lino, _exit);
19,112✔
1766
    }
1767
  }
1768

1769
  TAOS_CHECK_GOTO(metaUpdateTableNormalTagValue(pMeta, pTable, pTagSchema, pNewTagVals), &lino, _exit);
8,226,819✔
1770
  TAOS_CHECK_GOTO(metaHandleEntry2(pMeta, pTable), &lino, _exit);
8,074,885✔
1771

1772
_exit:
8,226,125✔
1773
  if (code) {
8,225,760✔
1774
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
151,934✔
1775
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code), pTable->uid, pTable->name, pTable->version);
151,934✔
1776
  } else {
1777
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
8,073,826✔
1778
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
8,073,826✔
1779
  }
1780

1781
  if (pRegexResults != NULL) {
8,226,819✔
1782
    for (int32_t i = 0; i < taosArrayGetSize(pRegexResults); i++) {
30,346✔
1783
      char** pp = taosArrayGet(pRegexResults, i);
19,112✔
1784
      taosMemoryFree(*pp);
19,112✔
1785
    }
1786
    taosArrayDestroy(pRegexResults);
11,234✔
1787
  }
1788

1789
  if (pNewTagVals != pUpdatedTagVals) {
8,226,819✔
1790
    taosHashCleanup(pNewTagVals);
11,234✔
1791
  }
1792

1793
  if (pIter != NULL) {
8,226,819✔
1794
    taosHashCancelIterate(pUpdatedTagVals, pIter);
×
1795
  }
1796

1797
  TAOS_RETURN(code);
8,226,819✔
1798
}
1799

1800

1801

1802
static int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, const char* tbName, SArray* tags) {
8,062,845✔
1803
  int32_t code = TSDB_CODE_SUCCESS;
8,062,845✔
1804
  SMetaEntry *pChild = NULL;
8,062,845✔
1805
  SMetaEntry *pSuper = NULL;
8,062,845✔
1806
  SHashObj* pUpdatedTagVals = NULL;
8,062,845✔
1807

1808
  // fetch child entry
1809
  code = metaFetchEntryByName(pMeta, tbName, &pChild);
8,062,845✔
1810
  if (code) {
8,062,480✔
1811
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64;
×
1812
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tbName, version);
×
1813
    goto _exit;
×
1814
  }
1815

1816
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
8,062,480✔
1817
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64;
×
1818
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tbName, version);
×
1819
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1820
    goto _exit;
×
1821
  }
1822

1823
  // fetch super entry
1824
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
8,062,845✔
1825
  if (code) {
8,062,480✔
1826
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64;
×
1827
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
×
1828
    code = TSDB_CODE_INTERNAL_ERROR;
×
1829
    goto _exit;
×
1830
  }
1831

1832
  // search the tags to update
1833
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
8,062,480✔
1834

1835
  code = updatedTagValueArrayToHashMap(pTagSchema, tags, &pUpdatedTagVals);
8,062,480✔
1836
  if (code) {
8,062,845✔
1837
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1838
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1839
    goto _exit;
×
1840
  }
1841

1842
  // change tag values
1843
  pChild->version = version;
8,062,845✔
1844
  code = metaUpdateTableTagValueImpl(pMeta, pChild, pTagSchema, pUpdatedTagVals);
8,062,845✔
1845
  if (code) {
8,062,845✔
1846
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, name:%s version:%" PRId64;
145,369✔
1847
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), tbName, version);
145,369✔
1848
    goto _exit;
145,369✔
1849
  }
1850

1851
_exit:
8,062,845✔
1852
  taosHashCleanup(pUpdatedTagVals);
8,062,845✔
1853
  metaFetchEntryFree(&pSuper);
8,062,845✔
1854
  metaFetchEntryFree(&pChild);
8,062,845✔
1855
  TAOS_RETURN(code);
8,062,845✔
1856
}
1857

1858

1859

1860
int32_t metaUpdateTableMultiTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,051,575✔
1861
  int32_t code = TSDB_CODE_SUCCESS;
8,051,575✔
1862
  SArray* uidList = NULL;
8,051,575✔
1863
  SArray* tagListArray = NULL;
8,051,575✔
1864

1865
  // Pre-allocate uidList for batch notification
1866
  int32_t nTables = taosArrayGetSize(pReq->tables);
8,051,575✔
1867
  uidList = taosArrayInit(nTables, sizeof(tb_uid_t));
8,051,575✔
1868
  if (uidList == NULL) {
8,051,575✔
1869
    code = terrno;
×
1870
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1871
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1872
    TAOS_RETURN(code);
×
1873
  }
1874
  tagListArray = taosArrayInit(nTables, sizeof(void*));
8,051,575✔
1875
  if (tagListArray == NULL) {
8,051,575✔
1876
    code = terrno;
×
1877
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1878
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1879
    TAOS_RETURN(code);
×
1880
  }
1881

1882
  for (int32_t i = 0; i < nTables; i++) {
16,114,420✔
1883
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
8,062,845✔
1884
    code = metaUpdateTableTagValue(pMeta, version, pTable->tbName, pTable->tags);
8,062,845✔
1885
    if (code == TSDB_CODE_VND_SAME_TAG) {
8,062,845✔
1886
      // we are updating multiple tables, if one table has same tag,
1887
      // just skip it and continue to update other tables,
1888
      // and return success at the end
1889
      code = TSDB_CODE_SUCCESS;
145,369✔
1890
    } else if (code) {
7,917,476✔
1891
      break;
×
1892
    } else {
1893
      // Collect UID for batch notification
1894
      int64_t uid = metaGetTableEntryUidByName(pMeta, pTable->tbName);
7,917,476✔
1895
      if (uid == 0) {
7,917,476✔
1896
        metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64,
×
1897
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pTable->tbName, version);
1898
        continue;
×
1899
      }
1900
      if (taosArrayPush(uidList, &uid) == NULL){
7,917,476✔
1901
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64,
×
1902
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), version);
1903
        continue;
×
1904
      }
1905
      if (taosArrayPush(tagListArray, &pTable->tags) == NULL){
15,834,952✔
1906
        void* ret = taosArrayPop(uidList);  // make sure the size of uidList and tagListArray are same
×
1907
        metaError("vgId:%d, %s failed at %s:%d since %s, ret:%p, version:%" PRId64,
×
1908
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), ret, version);
1909
        continue;
×
1910
      }
1911
    }
1912
  }
1913

1914
  if (taosArrayGetSize(uidList) > 0) {
8,051,575✔
1915
    vnodeAlterTagForTmq(pMeta->pVnode, uidList, NULL, tagListArray);
7,906,206✔
1916
  }
1917

1918
  taosArrayDestroy(uidList);
8,051,575✔
1919
  taosArrayDestroy(tagListArray);
8,051,575✔
1920
  DestoryThreadLocalRegComp();
8,051,575✔
1921

1922
  if (code) {
8,051,575✔
1923
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1924
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1925
  }
1926

1927
  TAOS_RETURN(code);
8,051,575✔
1928
}
1929

1930

1931
// Context for translating tag column/tbname references to tag values/table name
1932
typedef struct STagToValueCtx {
1933
  int32_t     code;
1934
  const void *pTags;  // STag* blob of the child table
1935
  const char *pName;  // table name of the child table
1936
} STagToValueCtx;
1937

1938
// Translate a tag column reference to the corresponding tag value
1939
static EDealRes tagToValue(SNode **pNode, void *pContext) {
1,683,642✔
1940
  STagToValueCtx *pCtx = (STagToValueCtx *)pContext;
1,683,642✔
1941

1942
  bool isTagCol = false, isTbname = false;
1,683,642✔
1943

1944
  if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
1,683,642✔
1945
    SColumnNode *pCol = (SColumnNode *)*pNode;
561,164✔
1946
    if (pCol->colType == COLUMN_TYPE_TBNAME)
560,470✔
1947
      isTbname = true;
131,300✔
1948
    else
1949
      isTagCol = true;
429,939✔
1950
  } else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
1,125,104✔
1951
    SFunctionNode *pFunc = (SFunctionNode *)*pNode;
×
1952
    if (pFunc->funcType == FUNCTION_TYPE_TBNAME)
×
1953
      isTbname = true;
×
1954
  }
1955

1956
  if (isTagCol) {
1,684,880✔
1957
    SColumnNode *pSColumnNode = *(SColumnNode **)pNode;
429,170✔
1958
    SValueNode  *res = NULL;
429,170✔
1959
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
429,864✔
1960
    if (NULL == res) {
430,633✔
1961
      return DEAL_RES_ERROR;
×
1962
    }
1963

1964
    res->translate = true;
430,633✔
1965
    res->node.resType = pSColumnNode->node.resType;
431,252✔
1966

1967
    STagVal tagVal = {0};
430,633✔
1968
    tagVal.cid = pSColumnNode->colId;
430,633✔
1969
    const char *p = metaGetTableTagVal(pCtx->pTags, pSColumnNode->node.resType.type, &tagVal);
430,014✔
1970
    if (p == NULL) {
430,558✔
1971
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
1972
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
430,558✔
1973
      int32_t len = ((const STag *)p)->len;
×
1974
      res->datum.p = taosMemoryCalloc(len + 1, 1);
×
1975
      if (NULL == res->datum.p) {
×
1976
        pCtx->code = terrno;
×
1977
        return DEAL_RES_ERROR;
×
1978
      }
1979
      memcpy(res->datum.p, p, len);
×
1980
    } else if (IS_VAR_DATA_TYPE(pSColumnNode->node.resType.type)) {
430,558✔
1981
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
3,539✔
1982
      if (NULL == res->datum.p) {
2,920✔
1983
        pCtx->code = terrno;
×
1984
        return DEAL_RES_ERROR;
×
1985
      }
1986
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
2,920✔
1987
      varDataSetLen(res->datum.p, tagVal.nData);
2,920✔
1988
    } else {
1989
      pCtx->code = nodesSetValueNodeValue(res, &(tagVal.i64));
427,713✔
1990
      if (pCtx->code != TSDB_CODE_SUCCESS) {
428,332✔
1991
        return DEAL_RES_ERROR;
×
1992
      }
1993
    }
1994

1995
    nodesDestroyNode(*pNode);
431,252✔
1996
    *pNode = (SNode *)res;
430,633✔
1997

1998
  } else if (isTbname) {
1,255,710✔
1999
    SValueNode *res = NULL;
131,300✔
2000
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
131,300✔
2001
    if (NULL == res) {
131,300✔
2002
      return DEAL_RES_ERROR;
×
2003
    }
2004

2005
    res->translate = true;
131,300✔
2006
    res->node.resType = ((SExprNode *)(*pNode))->resType;
131,300✔
2007

2008
    int32_t len = strlen(pCtx->pName);
131,300✔
2009
    res->datum.p = taosMemoryCalloc(len + VARSTR_HEADER_SIZE + 1, 1);
131,300✔
2010
    if (NULL == res->datum.p) {
131,300✔
2011
      pCtx->code = terrno;
×
2012
      return DEAL_RES_ERROR;
×
2013
    }
2014
    memcpy(varDataVal(res->datum.p), pCtx->pName, len);
131,300✔
2015
    varDataSetLen(res->datum.p, len);
131,300✔
2016
    nodesDestroyNode(*pNode);
131,300✔
2017
    *pNode = (SNode *)res;
131,300✔
2018
  }
2019

2020
  return DEAL_RES_CONTINUE;
1,686,962✔
2021
}
2022

2023

2024
// Check if a single child table qualifies against the tag condition.
2025
static int32_t metaIsChildTableQualified(SMeta *pMeta, tb_uid_t uid, SNode *pTagCond, bool *pQualified) {
558,388✔
2026
  int32_t     code = TSDB_CODE_SUCCESS;
558,388✔
2027
  SMetaEntry *pEntry = NULL;
558,388✔
2028

2029
  *pQualified = false;
559,082✔
2030

2031
  code = metaFetchEntryByUid(pMeta, uid, &pEntry);
561,858✔
2032
  if (code != TSDB_CODE_SUCCESS || pEntry == NULL) {
554,299✔
2033
    return TSDB_CODE_SUCCESS;
×
2034
  }
2035

2036
  // Clone the condition so we can safely rewrite it
2037
  SNode *pTagCondTmp = NULL;
554,299✔
2038
  code = nodesCloneNode(pTagCond, &pTagCondTmp);
555,687✔
2039
  if (code != TSDB_CODE_SUCCESS) {
562,552✔
2040
    metaFetchEntryFree(&pEntry);
×
2041
    return code;
×
2042
  }
2043

2044
  // Replace tag column and tbname references with concrete values
2045
  STagToValueCtx ctx = {.code = TSDB_CODE_SUCCESS, .pTags = pEntry->ctbEntry.pTags, .pName = pEntry->name};
562,552✔
2046
  nodesRewriteExprPostOrder(&pTagCondTmp, tagToValue, &ctx);
562,552✔
2047
  if (ctx.code != TSDB_CODE_SUCCESS) {
562,552✔
2048
    nodesDestroyNode(pTagCondTmp);
×
2049
    metaFetchEntryFree(&pEntry);
×
2050
    return ctx.code;
×
2051
  }
2052

2053
  // Evaluate the fully-resolved expression to a constant boolean
2054
  SNode *pResult = NULL;
562,552✔
2055
  code = scalarCalculateConstants(pTagCondTmp, &pResult);
562,552✔
2056
  if (code != TSDB_CODE_SUCCESS) {
557,694✔
2057
    nodesDestroyNode(pTagCondTmp);
×
2058
    metaFetchEntryFree(&pEntry);
×
2059
    return code;
×
2060
  }
2061

2062
  if (nodeType(pResult) == QUERY_NODE_VALUE) {
557,694✔
2063
    *pQualified = ((SValueNode *)pResult)->datum.b;
560,470✔
2064
  }
2065

2066
  nodesDestroyNode(pResult);
558,388✔
2067
  metaFetchEntryFree(&pEntry);
559,851✔
2068
  return TSDB_CODE_SUCCESS;
558,613✔
2069
}
2070

2071

2072

2073
static int32_t metaGetChildUidsByTagCond(SMeta *pMeta, tb_uid_t suid, SNode *pTagCond, SNode *pTagIndexCond, SArray *pUidList) {
57,955✔
2074
  int32_t       code = TSDB_CODE_SUCCESS;
57,955✔
2075
  int32_t       lino = 0;
57,955✔
2076
  SVnode       *pVnode = pMeta->pVnode;
58,649✔
2077
  SArray       *pCandidateUids = NULL;
59,343✔
2078

2079
  taosArrayClear(pUidList);
59,343✔
2080

2081
  // Step 1: Try index-accelerated pre-filtering with pTagIndexCond
2082
  if (pTagIndexCond != NULL) {
59,962✔
2083
    pCandidateUids = taosArrayInit(64, sizeof(uint64_t));
×
2084
    if (pCandidateUids == NULL) {
×
2085
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2086
    }
2087

2088
    SIndexMetaArg metaArg = {
×
2089
        .metaEx = pVnode,
2090
        .idx = metaGetIdx(pMeta),
×
2091
        .ivtIdx = metaGetIvtIdx(pMeta),
×
2092
        .suid = suid,
2093
    };
2094

2095
    SMetaDataFilterAPI filterAPI = {
×
2096
        .metaFilterTableIds = metaFilterTableIds,
2097
        .metaFilterCreateTime = metaFilterCreateTime,
2098
        .metaFilterTableName = metaFilterTableName,
2099
        .metaFilterTtl = metaFilterTtl,
2100
    };
2101

2102
    SIdxFltStatus idxStatus = SFLT_NOT_INDEX;
×
2103
    code = doFilterTag(pTagIndexCond, &metaArg, pCandidateUids, &idxStatus, &filterAPI);
×
2104
    if (code != TSDB_CODE_SUCCESS || idxStatus == SFLT_NOT_INDEX) {
×
2105
      // Index filtering failed or not supported, fall back to full scan
2106
      const char* msgFmt = "vgId:%d, index filter not used for suid:%" PRId64 ", status:%d code:%s";
×
2107
      metaDebug(msgFmt, TD_VID(pVnode), suid, idxStatus, tstrerror(code));
×
2108
      taosArrayDestroy(pCandidateUids);
×
2109
      pCandidateUids = NULL;
×
2110
      code = TSDB_CODE_SUCCESS;
×
2111
    }
2112
  }
2113

2114
  // Step 2: If index was not used, enumerate all child table UIDs
2115
  if (pCandidateUids == NULL) {
59,268✔
2116
    pCandidateUids = taosArrayInit(256, sizeof(uint64_t));
59,268✔
2117
    if (pCandidateUids == NULL) {
59,962✔
2118
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2119
    }
2120

2121
    SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
59,962✔
2122
    if (pCur == NULL) {
59,268✔
2123
      TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2124
    }
2125

2126
    while (1) {
693,233✔
2127
      tb_uid_t uid = metaCtbCursorNext(pCur);
752,501✔
2128
      if (uid == 0) {
752,576✔
2129
        break;
59,343✔
2130
      }
2131
      if (taosArrayPush(pCandidateUids, &uid) == NULL) {
693,233✔
2132
        code = terrno;
×
2133
        metaCloseCtbCursor(pCur);
×
2134
        TAOS_CHECK_GOTO(code, &lino, _end);
×
2135
      }
2136
    }
2137
    metaCloseCtbCursor(pCur);
59,343✔
2138
  }
2139

2140
  // Step 3: Apply pTagCond to filter the candidate UIDs
2141
  if (pTagCond == NULL) {
59,962✔
2142
    // No tag condition — all candidates qualify
2143
    taosArraySwap(pUidList, pCandidateUids);
5,252✔
2144
  } else {
2145
    // Evaluate pTagCond per child table
2146
    for (int32_t i = 0; i < taosArrayGetSize(pCandidateUids); i++) {
613,942✔
2147
      uint64_t uid = *(uint64_t*)taosArrayGet(pCandidateUids, i);
562,552✔
2148
      bool qualified = false;
561,858✔
2149
      code = metaIsChildTableQualified(pMeta, uid, pTagCond, &qualified);
561,858✔
2150
      if (code != TSDB_CODE_SUCCESS) {
553,755✔
2151
        const char* msgFmt = "vgId:%d, %s failed to evaluate tag cond for uid:%" PRId64 " suid:%" PRId64 " since %s";
×
2152
        metaError(msgFmt, TD_VID(pVnode), __func__, uid, suid, tstrerror(code));
×
2153
        TAOS_CHECK_GOTO(code, &lino, _end);
1,313✔
2154
      }
2155

2156
      if (qualified && taosArrayPush(pUidList, &uid) == NULL) {
712,192✔
2157
        TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2158
      }
2159
    }
2160
  }
2161

2162
_end:
60,581✔
2163
  taosArrayDestroy(pCandidateUids);
59,962✔
2164
  if (code != TSDB_CODE_SUCCESS) {
59,343✔
2165
    const char* msgFmt = "vgId:%d, %s failed at line %d for suid:%" PRId64 " since %s";
×
2166
    metaError(msgFmt, TD_VID(pVnode), __func__, lino, suid, tstrerror(code));
×
2167
  }
2168
  return code;
59,343✔
2169
}
2170

2171

2172
// Convenience wrapper: partition a raw WHERE condition into tag-related parts,
2173
// then call metaGetChildUidsByTagCond to get the filtered child table UIDs.
2174
int32_t metaGetChildUidsByWhere(SMeta *pMeta, tb_uid_t suid, SNode *pWhere, SArray *pUidList) {
59,268✔
2175
  int32_t code = TSDB_CODE_SUCCESS;
59,268✔
2176
  SNode  *pTagCond = NULL;
59,268✔
2177
  SNode  *pTagIndexCond = NULL;
59,268✔
2178

2179
  if (pWhere == NULL) {
59,268✔
2180
    // No WHERE condition — return all child table UIDs
2181
    return metaGetChildUidsByTagCond(pMeta, suid, NULL, NULL, pUidList);
5,252✔
2182
  }
2183

2184
  // Clone pWhere so the caller's node is not destroyed by filterPartitionCond
2185
  SNode *pWhereCopy = NULL;
54,016✔
2186
  code = nodesCloneNode(pWhere, &pWhereCopy);
54,016✔
2187
  if (code != TSDB_CODE_SUCCESS) {
52,159✔
2188
    return code;
×
2189
  }
2190

2191
  // Partition the WHERE condition
2192
  code = filterPartitionCond(&pWhereCopy, NULL, &pTagIndexCond, &pTagCond, NULL);
52,159✔
2193
  if (code != TSDB_CODE_SUCCESS) {
52,703✔
2194
    goto _cleanup;
×
2195
  }
2196

2197
  // Call the core filtering function
2198
  code = metaGetChildUidsByTagCond(pMeta, suid, pTagCond, pTagIndexCond, pUidList);
52,703✔
2199

2200
_cleanup:
54,091✔
2201
  nodesDestroyNode(pTagCond);
54,091✔
2202
  nodesDestroyNode(pTagIndexCond);
54,710✔
2203
  nodesDestroyNode(pWhereCopy);
54,710✔
2204
  return code;
54,710✔
2205
}
2206

2207

2208

2209
int32_t metaUpdateTableChildTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
59,962✔
2210
  int32_t code = TSDB_CODE_SUCCESS;
59,962✔
2211
  SNode* pWhere = NULL;
59,962✔
2212
  SMetaEntry *pSuper = NULL;
59,962✔
2213
  SArray *pUids = NULL;
59,962✔
2214
  SHashObj *pUpdatedTagVals = NULL;
59,962✔
2215
  SArray *uidListForTmq = NULL;
59,962✔
2216

2217
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
59,962✔
2218
    const char* fmt = "vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64;
×
2219
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
2220
    code = TSDB_CODE_INVALID_MSG;
×
2221
    goto _exit;
×
2222
  }
2223

2224
  if (pReq->whereLen > 0) {
59,962✔
2225
    code = nodesMsgToNode(pReq->where, pReq->whereLen, &pWhere);
54,710✔
2226
    if (code) {
54,710✔
2227
      const char* fmt = "vgId:%d, %s failed at %s:%d since invalid where condition, version:%" PRId64;
×
2228
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
2229
      code = TSDB_CODE_INVALID_MSG;
×
2230
      goto _exit;
×
2231
    }
2232
  }
2233

2234
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pSuper);
59,962✔
2235
  if (code) {
58,574✔
2236
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64;
×
2237
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
2238
    goto _exit;
×
2239
  }
2240

2241
  if (pSuper->type != TSDB_SUPER_TABLE) {
58,574✔
2242
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64;
×
2243
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
2244
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
2245
    goto _exit;
×
2246
  }
2247

2248
  code = updatedTagValueArrayToHashMap(&pSuper->stbEntry.schemaTag, pReq->pMultiTag, &pUpdatedTagVals);
57,955✔
2249
  if (code) {
59,268✔
2250
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2251
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2252
    goto _exit;
×
2253
  }
2254

2255
  pUids = taosArrayInit(16, sizeof(int64_t));
59,268✔
2256
  if (pUids == NULL) {
59,268✔
2257
    code = terrno;
×
2258
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2259
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2260
    goto _exit;
×
2261
  }
2262
  code = metaGetChildUidsByWhere(pMeta, pSuper->uid, pWhere, pUids);
59,268✔
2263
  if (code) {
59,962✔
2264
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2265
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2266
    goto _exit;
×
2267
  }
2268

2269
  // Pre-allocate uidList for batch notification
2270
  int32_t nUids = taosArrayGetSize(pUids);
59,962✔
2271
  uidListForTmq = taosArrayInit(nUids, sizeof(tb_uid_t));
59,962✔
2272
  if (uidListForTmq == NULL) {
59,962✔
2273
    code = terrno;
×
2274
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2275
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2276
    goto _exit;
×
2277
  }
2278

2279
  for (int32_t i = 0; i < nUids; i++) {
344,447✔
2280
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(pUids, i);
288,424✔
2281
    SMetaEntry *pChild = NULL;
288,424✔
2282
    code = metaFetchEntryByUid(pMeta, uid, &pChild);
288,424✔
2283
    if (code) {
288,424✔
2284
      const char* fmt = "vgId:%d, %s failed at %s:%d since child table uid %" PRId64 " not found, version:%" PRId64;
×
2285
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, uid, version);
×
2286
      goto _exit;
×
2287
    }
2288

2289
    pChild->version = version;
288,424✔
2290
    code = metaUpdateTableTagValueImpl(pMeta, pChild, &pSuper->stbEntry.schemaTag, pUpdatedTagVals);
288,424✔
2291
    if (code == TSDB_CODE_VND_SAME_TAG) {
288,424✔
2292
      // we are updating multiple tables, if one table has same tag,
2293
      // just skip it and continue to update other tables,
2294
      // and return success at the end
2295
      code = TSDB_CODE_SUCCESS;
2,626✔
2296
    } else if (code) {
285,798✔
2297
      const char* fmt = "vgId:%d, %s failed at %s:%d since %s, child table uid %" PRId64 " name %s, version:%" PRId64;
3,939✔
2298
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), uid, pChild->name, version);
3,939✔
2299
      metaFetchEntryFree(&pChild);
3,939✔
2300
      goto _exit;
3,939✔
2301
    } else {
2302
      // Collect UID for batch notification
2303
      if (taosArrayPush(uidListForTmq, &uid) == NULL) {
281,859✔
2304
        const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2305
        metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, terrstr, version);
×
2306
      }
2307
    }
2308

2309
    metaFetchEntryFree(&pChild);
284,485✔
2310
  }
2311

2312
_exit:
59,962✔
2313
  DestoryThreadLocalRegComp();
59,962✔
2314
  if (taosArrayGetSize(uidListForTmq) > 0) {
59,962✔
2315
    vnodeAlterTagForTmq(pMeta->pVnode, uidListForTmq, pReq->pMultiTag, NULL);
22,468✔
2316
  }
2317
  taosArrayDestroy(pUids);
59,962✔
2318
  taosArrayDestroy(uidListForTmq);
59,962✔
2319
  taosHashCleanup(pUpdatedTagVals);
59,962✔
2320
  metaFetchEntryFree(&pSuper);
59,962✔
2321
  nodesDestroyNode(pWhere);
59,962✔
2322
  TAOS_RETURN(code);
59,962✔
2323
}
2324

2325

2326

2327
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,060✔
2328
  int32_t code = TSDB_CODE_SUCCESS;
23,060✔
2329

2330
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
23,060✔
2331
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2332
              __FILE__, __LINE__, version);
2333
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2334
  }
2335

2336
  return code;
23,060✔
2337
}
2338

2339
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,060✔
2340
  int32_t code = 0;
23,060✔
2341

2342
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
23,060✔
2343
  if (code) {
23,060✔
2344
    TAOS_RETURN(code);
×
2345
  }
2346

2347
  // fetch entry
2348
  SMetaEntry *pEntry = NULL;
23,060✔
2349
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,060✔
2350
  if (code) {
23,060✔
2351
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2352
              __FILE__, __LINE__, pReq->tbName, version);
2353
    TAOS_RETURN(code);
×
2354
  }
2355

2356
  // do change the entry
2357
  pEntry->version = version;
23,060✔
2358
  if (pEntry->type == TSDB_CHILD_TABLE) {
23,060✔
2359
    if (pReq->updateTTL) {
12,143✔
2360
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,696✔
2361
    }
2362
    if (pReq->newCommentLen >= 0) {
12,143✔
2363
      char *pNewComment = NULL;
7,447✔
2364
      if (pReq->newCommentLen) {
7,447✔
2365
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,748✔
2366
        if (NULL == pNewComment) {
4,748✔
2367
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2368
                    __LINE__, tstrerror(terrno), version);
2369
          metaFetchEntryFree(&pEntry);
×
2370
          TAOS_RETURN(terrno);
×
2371
        }
2372
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,748✔
2373
      } else {
2374
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,699✔
2375
      }
2376
      pEntry->ctbEntry.comment = pNewComment;
7,447✔
2377
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
7,447✔
2378
    }
2379
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
10,917✔
2380
    if (pReq->updateTTL) {
10,917✔
2381
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,836✔
2382
    }
2383
    if (pReq->newCommentLen >= 0) {
10,917✔
2384
      char *pNewComment = NULL;
6,081✔
2385
      if (pReq->newCommentLen > 0) {
6,081✔
2386
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
3,382✔
2387
        if (NULL == pNewComment) {
3,382✔
2388
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2389
                    __LINE__, tstrerror(terrno), version);
2390
          metaFetchEntryFree(&pEntry);
×
2391
          TAOS_RETURN(terrno);
×
2392
        }
2393
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
3,382✔
2394
      } else {
2395
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,699✔
2396
      }
2397
      pEntry->ntbEntry.comment = pNewComment;
6,081✔
2398
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
6,081✔
2399
    }
2400
  } else {
2401
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2402
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
2403
    metaFetchEntryFree(&pEntry);
×
2404
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2405
  }
2406

2407
  // do handle entry
2408
  code = metaHandleEntry2(pMeta, pEntry);
23,060✔
2409
  if (code) {
23,060✔
2410
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2411
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2412
    metaFetchEntryFree(&pEntry);
×
2413
    TAOS_RETURN(code);
×
2414
  } else {
2415
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
23,060✔
2416
             pEntry->uid, version);
2417
  }
2418

2419
  metaFetchEntryFree(&pEntry);
23,060✔
2420
  TAOS_RETURN(code);
23,060✔
2421
}
2422

2423
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,274✔
2424
  int32_t code = TSDB_CODE_SUCCESS;
6,274✔
2425

2426
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,274✔
2427
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2428
              __FILE__, __LINE__, version);
2429
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2430
  }
2431

2432
  SMetaEntry *pEntry = NULL;
6,274✔
2433
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,274✔
2434
  if (code) {
6,274✔
2435
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2436
              __FILE__, __LINE__, pReq->tbName, version);
2437
    TAOS_RETURN(code);
×
2438
  }
2439

2440
  if (pEntry->version >= version) {
6,274✔
2441
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2442
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2443
    metaFetchEntryFree(&pEntry);
×
2444
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2445
  }
2446

2447
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,274✔
2448
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2449
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
2450
    metaFetchEntryFree(&pEntry);
×
2451
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2452
  }
2453

2454
  // do change the entry
2455
  int8_t           updated = 0;
6,274✔
2456
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,274✔
2457
  for (int32_t i = 0; i < wp->nCols; i++) {
50,192✔
2458
    SColCmpr *p = &wp->pColCmpr[i];
43,918✔
2459
    if (p->id == pReq->colId) {
43,918✔
2460
      uint32_t dst = 0;
6,274✔
2461
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,274✔
2462
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2463
      if (updated > 0) {
6,274✔
2464
        p->alg = dst;
6,274✔
2465
      }
2466
    }
2467
  }
2468

2469
  if (updated == 0) {
6,274✔
2470
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
2471
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
2472
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2473
    metaFetchEntryFree(&pEntry);
×
2474
    TAOS_RETURN(code);
×
2475
  } else if (updated < 0) {
6,274✔
2476
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2477
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
2478
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2479
    metaFetchEntryFree(&pEntry);
×
2480
    TAOS_RETURN(code);
×
2481
  }
2482

2483
  pEntry->version = version;
6,274✔
2484

2485
  // do handle entry
2486
  code = metaHandleEntry2(pMeta, pEntry);
6,274✔
2487
  if (code) {
6,274✔
2488
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2489
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2490
    metaFetchEntryFree(&pEntry);
×
2491
    TAOS_RETURN(code);
×
2492
  } else {
2493
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
6,274✔
2494
             pEntry->uid, version);
2495
  }
2496

2497
  metaFetchEntryFree(&pEntry);
6,274✔
2498
  TAOS_RETURN(code);
6,274✔
2499
}
2500

2501
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
79,663✔
2502
  int32_t code = TSDB_CODE_SUCCESS;
79,663✔
2503

2504
  // check request
2505
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
79,663✔
2506
  if (code) {
79,663✔
2507
    TAOS_RETURN(code);
×
2508
  }
2509

2510
  if (NULL == pReq->refDbName) {
79,663✔
2511
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2512
              __func__, __FILE__, __LINE__, version);
2513
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2514
  }
2515

2516
  if (NULL == pReq->refTbName) {
79,663✔
2517
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2518
              __func__, __FILE__, __LINE__, version);
2519
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2520
  }
2521

2522
  if (NULL == pReq->refColName) {
79,663✔
2523
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2524
              __func__, __FILE__, __LINE__, version);
2525
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2526
  }
2527

2528
  // fetch old entry
2529
  SMetaEntry *pEntry = NULL;
79,663✔
2530
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
79,663✔
2531
  if (code) {
79,663✔
2532
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2533
              __FILE__, __LINE__, pReq->tbName, version);
2534
    TAOS_RETURN(code);
×
2535
  }
2536

2537
  if (pEntry->version >= version) {
79,663✔
2538
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2539
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2540
    metaFetchEntryFree(&pEntry);
×
2541
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2542
  }
2543

2544
  // fetch super entry
2545
  SMetaEntry *pSuper = NULL;
79,663✔
2546
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
79,663✔
2547
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
29,651✔
2548
    if (code) {
29,651✔
2549
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2550
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2551
      metaFetchEntryFree(&pEntry);
×
2552
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2553
    }
2554
  }
2555

2556
  // search the column to update
2557
  SSchemaWrapper *pSchema =
79,663✔
2558
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
79,663✔
2559
  SColRef *pColRef = NULL;
79,663✔
2560
  int32_t  iColumn = 0;
79,663✔
2561
  for (int32_t i = 0; i < pSchema->nCols; i++) {
391,935✔
2562
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
391,935✔
2563
      pColRef = &pEntry->colRef.pColRef[i];
79,663✔
2564
      iColumn = i;
79,663✔
2565
      break;
79,663✔
2566
    }
2567
  }
2568

2569
  if (NULL == pColRef) {
79,663✔
2570
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2571
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
2572
    metaFetchEntryFree(&pEntry);
×
2573
    metaFetchEntryFree(&pSuper);
×
2574
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2575
  }
2576

2577
  // do update column name
2578
  pEntry->version = version;
79,663✔
2579
  pColRef->hasRef = true;
79,663✔
2580
  pColRef->id = pSchema->pSchema[iColumn].colId;
79,663✔
2581
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
79,663✔
2582
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
79,663✔
2583
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
79,663✔
2584
  pSchema->version++;
79,663✔
2585
  pEntry->colRef.version++;
79,663✔
2586

2587
  // do handle entry
2588
  code = metaHandleEntry2(pMeta, pEntry);
79,663✔
2589
  if (code) {
79,663✔
2590
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2591
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2592
    metaFetchEntryFree(&pEntry);
×
2593
    metaFetchEntryFree(&pSuper);
×
2594
    TAOS_RETURN(code);
×
2595
  } else {
2596
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
79,663✔
2597
             pEntry->uid, version);
2598
  }
2599

2600
  // build response
2601
  code = metaUpdateVtbMetaRsp(
159,326✔
2602
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
79,663✔
2603
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
79,663✔
2604
      pEntry->type);
79,663✔
2605
  if (code) {
79,663✔
2606
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2607
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2608
  } else {
2609
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
589,085✔
2610
      SColRef *p = &pEntry->colRef.pColRef[i];
509,422✔
2611
      pRsp->pColRefs[i].hasRef = p->hasRef;
509,422✔
2612
      pRsp->pColRefs[i].id = p->id;
509,422✔
2613
      if (p->hasRef) {
509,422✔
2614
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
350,814✔
2615
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
350,814✔
2616
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
350,814✔
2617
      }
2618
    }
2619
  }
2620

2621
  metaFetchEntryFree(&pEntry);
79,663✔
2622
  metaFetchEntryFree(&pSuper);
79,663✔
2623
  TAOS_RETURN(code);
79,663✔
2624
}
2625

2626
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
59,737✔
2627
  int32_t code = TSDB_CODE_SUCCESS;
59,737✔
2628

2629
  // check request
2630
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
59,737✔
2631
  if (code) {
59,737✔
2632
    TAOS_RETURN(code);
×
2633
  }
2634

2635
  // fetch old entry
2636
  SMetaEntry *pEntry = NULL;
59,737✔
2637
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
59,737✔
2638
  if (code) {
59,737✔
2639
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2640
              __FILE__, __LINE__, pReq->tbName, version);
2641
    TAOS_RETURN(code);
×
2642
  }
2643

2644
  if (pEntry->version >= version) {
59,737✔
2645
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2646
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2647
    metaFetchEntryFree(&pEntry);
×
2648
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2649
  }
2650

2651
  // fetch super entry
2652
  SMetaEntry *pSuper = NULL;
59,737✔
2653
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
59,737✔
2654
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
29,773✔
2655
    if (code) {
29,773✔
2656
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2657
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2658
      metaFetchEntryFree(&pEntry);
×
2659
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2660
    }
2661
  }
2662

2663
  // search the column to update
2664
  SSchemaWrapper *pSchema =
59,737✔
2665
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
59,737✔
2666
  SColRef *pColRef = NULL;
59,737✔
2667
  int32_t  iColumn = 0;
59,737✔
2668
  for (int32_t i = 0; i < pSchema->nCols; i++) {
238,857✔
2669
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
238,857✔
2670
      pColRef = &pEntry->colRef.pColRef[i];
59,737✔
2671
      iColumn = i;
59,737✔
2672
      break;
59,737✔
2673
    }
2674
  }
2675

2676
  if (NULL == pColRef) {
59,737✔
2677
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2678
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2679
    metaFetchEntryFree(&pEntry);
×
2680
    metaFetchEntryFree(&pSuper);
×
2681
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2682
  }
2683

2684
  // do update column name
2685
  pEntry->version = version;
59,737✔
2686
  pColRef->hasRef = false;
59,737✔
2687
  pColRef->id = pSchema->pSchema[iColumn].colId;
59,737✔
2688
  pSchema->version++;
59,737✔
2689
  pEntry->colRef.version++;
59,737✔
2690

2691
  // do handle entry
2692
  code = metaHandleEntry2(pMeta, pEntry);
59,737✔
2693
  if (code) {
59,737✔
2694
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2695
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2696
    metaFetchEntryFree(&pEntry);
×
2697
    metaFetchEntryFree(&pSuper);
×
2698
    TAOS_RETURN(code);
×
2699
  } else {
2700
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
59,737✔
2701
             pEntry->uid, version);
2702
  }
2703

2704
  // build response
2705
  code = metaUpdateVtbMetaRsp(
119,474✔
2706
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
59,737✔
2707
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
59,737✔
2708
      pEntry->type);
59,737✔
2709
  if (code) {
59,737✔
2710
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2711
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2712
  } else {
2713
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
371,604✔
2714
      SColRef *p = &pEntry->colRef.pColRef[i];
311,867✔
2715
      pRsp->pColRefs[i].hasRef = p->hasRef;
311,867✔
2716
      pRsp->pColRefs[i].id = p->id;
311,867✔
2717
      if (p->hasRef) {
311,867✔
2718
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
156,454✔
2719
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
156,454✔
2720
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
156,454✔
2721
      }
2722
    }
2723
  }
2724

2725
  metaFetchEntryFree(&pEntry);
59,737✔
2726
  metaFetchEntryFree(&pSuper);
59,737✔
2727
  TAOS_RETURN(code);
59,737✔
2728
}
2729

2730
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
18,589✔
2731
  int32_t code = TSDB_CODE_SUCCESS;
18,589✔
2732

2733
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
18,589✔
2734
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2735
              __FILE__, __LINE__, version);
2736
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2737
  }
2738

2739
  SMetaEntry *pEntry = NULL;
18,589✔
2740
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
18,589✔
2741
  if (code) {
18,589✔
2742
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2743
              __FILE__, __LINE__, pReq->name, version);
2744
    TAOS_RETURN(code);
×
2745
  }
2746

2747
  if (pEntry->type != TSDB_SUPER_TABLE) {
18,589✔
2748
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2749
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2750
    metaFetchEntryFree(&pEntry);
×
2751
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2752
  }
2753

2754
  if (pEntry->uid != pReq->suid) {
18,589✔
2755
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2756
              ", version:%" PRId64,
2757
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2758
    metaFetchEntryFree(&pEntry);
×
2759
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2760
  }
2761

2762
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2763
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2764
  //   PRId64,
2765
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2766
  //             pReq->schemaTag.version, version);
2767
  //   metaFetchEntryFree(&pEntry);
2768
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2769
  // }
2770

2771
  // do change the entry
2772
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
18,589✔
2773
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
18,589✔
2774
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
18,589✔
2775
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2776
              __func__, __FILE__, __LINE__, pReq->name, version);
2777
    metaFetchEntryFree(&pEntry);
×
2778
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2779
  }
2780

2781
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
18,589✔
2782
    metaError(
×
2783
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2784
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2785
        version);
2786
    metaFetchEntryFree(&pEntry);
×
2787
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2788
  }
2789

2790
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2791
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2792
  //   PRId64,
2793
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2794
  //             pNewTagSchema->version, version);
2795
  //   metaFetchEntryFree(&pEntry);
2796
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2797
  // }
2798

2799
  int32_t numOfChangedTags = 0;
18,589✔
2800
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
201,082✔
2801
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
182,493✔
2802
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
182,493✔
2803

2804
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
182,493✔
2805
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
182,493✔
2806
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2807
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2808
      metaFetchEntryFree(&pEntry);
×
2809
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2810
    }
2811

2812
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
182,493✔
2813
      numOfChangedTags++;
18,589✔
2814
      SSCHMEA_SET_IDX_ON(pOldColumn);
18,589✔
2815
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
163,904✔
2816
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2817
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2818
      metaFetchEntryFree(&pEntry);
×
2819
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2820
    }
2821
  }
2822

2823
  if (numOfChangedTags != 1) {
18,589✔
2824
    metaError(
×
2825
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2826
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2827
    metaFetchEntryFree(&pEntry);
×
2828
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2829
  }
2830

2831
  pEntry->version = version;
18,589✔
2832
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
18,589✔
2833

2834
  // do handle the entry
2835
  code = metaHandleEntry2(pMeta, pEntry);
18,589✔
2836
  if (code) {
18,589✔
2837
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2838
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2839
    metaFetchEntryFree(&pEntry);
×
2840
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2841
  } else {
2842
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
18,589✔
2843
             pEntry->uid, version);
2844
  }
2845

2846
  metaFetchEntryFree(&pEntry);
18,589✔
2847
  TAOS_RETURN(code);
18,589✔
2848
}
2849

2850
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
15,160✔
2851
  int32_t code = TSDB_CODE_SUCCESS;
15,160✔
2852

2853
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
15,160✔
2854
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2855
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2856
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2857
  }
2858

2859
  SMetaEntry *pEntry = NULL;
15,160✔
2860
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
15,160✔
2861
  if (code) {
15,160✔
2862
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2863
              __FILE__, __LINE__, pReq->stb, version);
2864
    TAOS_RETURN(code);
×
2865
  }
2866

2867
  if (TSDB_SUPER_TABLE != pEntry->type) {
15,160✔
2868
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2869
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2870
    metaFetchEntryFree(&pEntry);
×
2871
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2872
  }
2873

2874
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
15,160✔
2875
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
15,160✔
2876
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2877
              __func__, __FILE__, __LINE__, pReq->stb, version);
2878
    metaFetchEntryFree(&pEntry);
×
2879
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2880
  }
2881

2882
  // search and set the tag index off
2883
  int32_t numOfChangedTags = 0;
15,160✔
2884
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
94,244✔
2885
    SSchema *pCol = pTagSchema->pSchema + i;
94,244✔
2886
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
94,244✔
2887
      if (!IS_IDX_ON(pCol)) {
15,160✔
2888
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2889
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2890
        metaFetchEntryFree(&pEntry);
×
2891
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2892
      }
2893
      numOfChangedTags++;
15,160✔
2894
      SSCHMEA_SET_IDX_OFF(pCol);
15,160✔
2895
      break;
15,160✔
2896
    }
2897
  }
2898

2899
  if (numOfChangedTags != 1) {
15,160✔
2900
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2901
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2902
    metaFetchEntryFree(&pEntry);
×
2903
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2904
  }
2905

2906
  // do handle the entry
2907
  pEntry->version = version;
15,160✔
2908
  pTagSchema->version++;
15,160✔
2909
  code = metaHandleEntry2(pMeta, pEntry);
15,160✔
2910
  if (code) {
15,160✔
2911
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2912
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2913
    metaFetchEntryFree(&pEntry);
×
2914
    TAOS_RETURN(code);
×
2915
  } else {
2916
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
15,160✔
2917
             pEntry->uid, version);
2918
  }
2919

2920
  metaFetchEntryFree(&pEntry);
15,160✔
2921
  TAOS_RETURN(code);
15,160✔
2922
}
2923

2924
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,710,857✔
2925
  int32_t code = TSDB_CODE_SUCCESS;
7,710,857✔
2926

2927
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,710,857✔
2928
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
17,497✔
2929
              __FILE__, __LINE__, version);
2930
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
17,497✔
2931
  }
2932

2933
  SMetaEntry *pEntry = NULL;
7,697,545✔
2934
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,709,114✔
2935
  if (code) {
7,704,784✔
2936
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2937
              __FILE__, __LINE__, pReq->name, version);
2938
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2939
  }
2940

2941
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,704,784✔
2942
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2943
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2944
    metaFetchEntryFree(&pEntry);
×
2945
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2946
  }
2947

2948
  SMetaEntry entry = {
23,065,932✔
2949
      .version = version,
2950
      .type = TSDB_SUPER_TABLE,
2951
      .uid = pReq->suid,
7,700,455✔
2952
      .name = pReq->name,
7,690,016✔
2953
      .stbEntry.schemaRow = pReq->schemaRow,
2954
      .stbEntry.schemaTag = pReq->schemaTag,
2955
      .stbEntry.keep = pReq->keep,
7,701,785✔
2956
      .stbEntry.ownerId = pReq->ownerId,
7,681,270✔
2957
      .colCmpr = pReq->colCmpr,
2958
      .pExtSchemas = pReq->pExtSchemas,
7,688,015✔
2959
  };
2960
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,705,463✔
2961
  if (pReq->virtualStb) {
7,705,463✔
2962
    TABLE_SET_VIRTUAL(entry.flags);
21,686✔
2963
  }
2964
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,683,064✔
2965
    TABLE_SET_ROLLUP(entry.flags);
4,524✔
2966
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,524✔
2967
  }
2968

2969
  // do handle the entry
2970
  code = metaHandleEntry2(pMeta, &entry);
7,679,416✔
2971
  if (code) {
7,697,850✔
2972
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2973
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2974
    metaFetchEntryFree(&pEntry);
×
2975
    TAOS_RETURN(code);
×
2976
  } else {
2977
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,697,850✔
2978
             pReq->suid, version);
2979
  }
2980

2981
  metaFetchEntryFree(&pEntry);
7,711,820✔
2982
  TAOS_RETURN(code);
7,715,078✔
2983
}
2984

2985
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
2986
  int32_t code = 0;
×
2987

2988
  if (taosArrayGetSize(uidArray) == 0) {
×
2989
    return TSDB_CODE_SUCCESS;
×
2990
  }
2991

2992
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
2993
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
2994
    SMetaInfo info;
×
2995
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
2996
    if (code) {
×
2997
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2998
                __func__, __FILE__, __LINE__, uid, code);
2999
      return code;
×
3000
    }
3001

3002
    SMetaEntry entry = {
×
3003
        .version = version,
3004
        .uid = uid,
3005
    };
3006

3007
    if (info.suid == 0) {
×
3008
      entry.type = -TSDB_NORMAL_TABLE;
×
3009
    } else if (info.suid == uid) {
×
3010
      entry.type = -TSDB_SUPER_TABLE;
×
3011
    } else {
3012
      entry.type = -TSDB_CHILD_TABLE;
×
3013
    }
3014
    code = metaHandleEntry2(pMeta, &entry);
×
3015
    if (code) {
×
3016
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
3017
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
3018
      return code;
×
3019
    }
3020
  }
3021
  return code;
×
3022
}
3023

3024
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
39,994✔
3025
  int32_t code = TSDB_CODE_SUCCESS;
39,994✔
3026

3027
  if (NULL == pReq->name || pReq->name[0] == 0) {
39,994✔
3028
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3029
              __LINE__, version);
3030
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3031
  }
3032

3033
  SMetaEntry *pEntry = NULL;
39,994✔
3034
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
39,994✔
3035
  if (code) {
39,994✔
3036
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
3037
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3038
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
3039
  }
3040

3041
  if (pEntry->type != TSDB_SUPER_TABLE) {
39,994✔
3042
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
3043
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
3044
    metaFetchEntryFree(&pEntry);
×
3045
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3046
  }
3047

3048
  if (pEntry->uid != pReq->tbUid) {
39,994✔
3049
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3050
              ", version:%" PRId64,
3051
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3052
    metaFetchEntryFree(&pEntry);
×
3053
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3054
  }
3055

3056
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
39,994✔
3057
    // overwrite the old rsma definition if exists
3058
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
3059
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
3060
  } else {
3061
    TABLE_SET_ROLLUP(pEntry->flags);
39,994✔
3062
  }
3063

3064
  SMetaEntry entry = *pEntry;
39,994✔
3065
  entry.version = version;
39,994✔
3066
  entry.stbEntry.rsmaParam.name = pReq->name;
39,994✔
3067
  entry.stbEntry.rsmaParam.uid = pReq->uid;
39,994✔
3068
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
39,994✔
3069
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
39,994✔
3070
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
39,994✔
3071
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
39,994✔
3072
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
39,994✔
3073
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
39,994✔
3074

3075
  // do handle the entry
3076
  code = metaHandleEntry2(pMeta, &entry);
39,994✔
3077
  if (code) {
39,994✔
3078
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3079
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
3080
    metaFetchEntryFree(&pEntry);
×
3081
    TAOS_RETURN(code);
×
3082
  } else {
3083
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
39,994✔
3084
    pMeta->pVnode->config.isRsma = 1;
39,994✔
3085
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
39,994✔
3086
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3087
  }
3088

3089
  metaFetchEntryFree(&pEntry);
39,994✔
3090
  TAOS_RETURN(code);
39,994✔
3091
}
3092

3093
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
20,128✔
3094
  int32_t code = TSDB_CODE_SUCCESS;
20,128✔
3095

3096
  if (NULL == pReq->name || pReq->name[0] == 0) {
20,128✔
3097
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3098
              __LINE__, version);
3099
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3100
  }
3101

3102
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
20,128✔
3103
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3104
              __LINE__, version);
3105
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3106
  }
3107

3108
  SMetaEntry *pEntry = NULL;
20,128✔
3109
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
20,128✔
3110
  if (code) {
20,128✔
3111
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3112
             __func__, __LINE__, pReq->name, pReq->tbName, version);
3113
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3114
  }
3115

3116
  if (pEntry->type != pReq->tbType) {
20,128✔
3117
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
3118
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
3119
    metaFetchEntryFree(&pEntry);
×
3120
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3121
  }
3122

3123
  if (pEntry->uid != pReq->tbUid) {
20,128✔
3124
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3125
              ", version:%" PRId64,
3126
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3127
    metaFetchEntryFree(&pEntry);
×
3128
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3129
  }
3130

3131
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
20,128✔
3132
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
20,128✔
3133
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
20,128✔
3134
      metaError(
×
3135
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
3136
          "uid:%" PRIi64 ", version:%" PRId64,
3137
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
3138
          pReq->uid, version);
3139
      metaFetchEntryFree(&pEntry);
×
3140
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3141
    }
3142
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
20,128✔
3143
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
20,128✔
3144
  } else {
3145
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
3146
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
3147
    metaFetchEntryFree(&pEntry);
×
3148
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3149
  }
3150

3151
  SMetaEntry entry = *pEntry;
20,128✔
3152
  entry.version = version;
20,128✔
3153
  TABLE_RESET_ROLLUP(entry.flags);
20,128✔
3154
  entry.stbEntry.rsmaParam.uid = 0;
20,128✔
3155
  entry.stbEntry.rsmaParam.name = NULL;
20,128✔
3156
  entry.stbEntry.rsmaParam.nFuncs = 0;
20,128✔
3157
  entry.stbEntry.rsmaParam.funcColIds = NULL;
20,128✔
3158
  entry.stbEntry.rsmaParam.funcIds = NULL;
20,128✔
3159

3160
  // do handle the entry
3161
  code = metaHandleEntry2(pMeta, &entry);
20,128✔
3162
  if (code) {
20,128✔
3163
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
3164
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
3165
    metaFetchEntryFree(&pEntry);
×
3166
    TAOS_RETURN(code);
×
3167
  } else {
3168
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
20,128✔
3169
      pMeta->pVnode->config.isRsma = 0;
×
3170
    }
3171
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
20,128✔
3172
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3173
  }
3174

3175
  metaFetchEntryFree(&pEntry);
20,128✔
3176
  TAOS_RETURN(code);
20,128✔
3177
}
3178

3179
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,326✔
3180
  int32_t code = TSDB_CODE_SUCCESS;
12,326✔
3181

3182
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,326✔
3183
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3184
              __LINE__, version);
3185
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3186
  }
3187

3188
  SMetaEntry *pEntry = NULL;
12,326✔
3189
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
12,326✔
3190
  if (code) {
12,326✔
3191
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
3192
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3193
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
3194
  }
3195

3196
  if (pEntry->uid != pReq->tbUid) {
12,326✔
3197
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3198
              ", version:%" PRId64,
3199
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3200
    metaFetchEntryFree(&pEntry);
×
3201
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3202
  }
3203

3204
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
12,326✔
3205
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
12,326✔
3206
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,326✔
3207
  } else {
3208
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
3209
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3210
    metaFetchEntryFree(&pEntry);
×
3211
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3212
  }
3213

3214
  SMetaEntry entry = *pEntry;
12,326✔
3215
  entry.version = version;
12,326✔
3216
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
12,326✔
3217
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
12,326✔
3218
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
12,326✔
3219
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
12,326✔
3220
  }
3221
  // do handle the entry
3222
  code = metaHandleEntry2(pMeta, &entry);
12,326✔
3223
  if (code) {
12,326✔
3224
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3225
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
3226
    metaFetchEntryFree(&pEntry);
×
3227
    TAOS_RETURN(code);
×
3228
  } else {
3229
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
12,326✔
3230
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3231
  }
3232

3233
  metaFetchEntryFree(&pEntry);
12,326✔
3234
  TAOS_RETURN(code);
12,326✔
3235
}
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