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

taosdata / TDengine / #5017

09 Apr 2026 02:37PM UTC coverage: 72.248% (-0.05%) from 72.299%
#5017

push

travis-ci

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

merge: from main to 3.0 branch

499 of 655 new or added lines in 34 files covered. (76.18%)

821 existing lines in 156 files now uncovered.

257359 of 356215 relevant lines covered (72.25%)

132044878.66 hits per line

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

69.09
/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, const SSchemaWrapper *pSchema,
27
                                    const SColRefWrapper *pRef, const SExtSchema *pExtSchemas, int64_t ownerId,
28
                                    STableMetaRsp *pMetaRsp,
29
                                    int8_t tableType);
30
extern int32_t metaFetchEntryByUid(SMeta *pMeta, int64_t uid, SMetaEntry **ppEntry);
31
extern int32_t metaFetchEntryByName(SMeta *pMeta, const char *name, SMetaEntry **ppEntry);
32
extern void    metaFetchEntryFree(SMetaEntry **ppEntry);
33
extern int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress);
34
extern int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema);
35
extern int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum);
36

37
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,401,270✔
38
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,401,270✔
39
  void     *value = NULL;
5,418,308✔
40
  int32_t   valueSize = 0;
5,417,996✔
41
  SMetaInfo info;
5,411,072✔
42

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

50
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
5,417,111✔
51
  if (r == 0) {  // name exists, check uid and type
5,405,703✔
52
    int64_t uid = *(tb_uid_t *)value;
4,782✔
53
    tdbFree(value);
4,782✔
54

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

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

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

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

86
  return TSDB_CODE_SUCCESS;
5,407,030✔
87
}
88

89
static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,732,951✔
90
  int32_t   code = TSDB_CODE_SUCCESS;
1,732,951✔
91
  void     *value = NULL;
1,732,951✔
92
  int32_t   valueSize = 0;
1,732,951✔
93
  SMetaInfo info;
1,732,735✔
94

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

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

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

125
  return code;
1,732,951✔
126
}
127

128
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
943,157✔
129
  int32_t   code = TSDB_CODE_SUCCESS;
943,157✔
130
  void     *value = NULL;
943,157✔
131
  int32_t   valueSize = 0;
943,852✔
132
  SMetaInfo info;
942,200✔
133

134
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
943,157✔
135
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1,811✔
136
              __FILE__, __LINE__, pReq->name, version);
137
    return TSDB_CODE_INVALID_MSG;
×
138
  }
139

140
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
940,729✔
141
  if (code) {
943,157✔
142
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
773✔
143
              __FILE__, __LINE__, pReq->name, version);
144
    return TSDB_CODE_TDB_STB_NOT_EXIST;
773✔
145
  } else {
146
    int64_t uid = *(int64_t *)value;
942,384✔
147
    tdbFreeClear(value);
942,384✔
148

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

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

171
// Create Super Table
172
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,406,348✔
173
  int32_t code = TSDB_CODE_SUCCESS;
5,406,348✔
174

175
  // check request
176
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,406,348✔
177
  if (code != TSDB_CODE_SUCCESS) {
5,411,099✔
178
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
4,782✔
179
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
1,690✔
180
               pReq->name, pReq->suid, version);
181
      TAOS_RETURN(TSDB_CODE_SUCCESS);
1,690✔
182
    } else {
183
      TAOS_RETURN(code);
3,092✔
184
    }
185
  }
186

187
  // handle entry
188
  SMetaEntry entry = {
10,795,168✔
189
      .version = version,
190
      .type = TSDB_SUPER_TABLE,
191
      .uid = pReq->suid,
5,404,911✔
192
      .name = pReq->name,
5,397,738✔
193
      .stbEntry.schemaRow = pReq->schemaRow,
194
      .stbEntry.schemaTag = pReq->schemaTag,
195
      .stbEntry.keep = pReq->keep,
5,405,944✔
196
      .stbEntry.ownerId = pReq->ownerId,
5,397,183✔
197
  };
198
  if (pReq->rollup) {
5,391,434✔
199
    TABLE_SET_ROLLUP(entry.flags);
×
200
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
201
  }
202
  if (pReq->colCmpred) {
5,385,660✔
203
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,398,828✔
204
    entry.colCmpr = pReq->colCmpr;
5,398,828✔
205
  }
206

207
  entry.pExtSchemas = pReq->pExtSchemas;
5,393,180✔
208

209
  if (pReq->virtualStb) {
5,401,191✔
210
    TABLE_SET_VIRTUAL(entry.flags);
119,447✔
211
  }
212

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

224
// Drop Super Table
225
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
943,157✔
226
  int32_t code = TSDB_CODE_SUCCESS;
943,157✔
227

228
  // check request
229
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
943,157✔
230
  if (code) {
940,616✔
231
    TAOS_RETURN(code);
1,546✔
232
  }
233

234
  // handle entry
235
  SMetaEntry entry = {
939,070✔
236
      .version = verison,
237
      .type = -TSDB_SUPER_TABLE,
238
      .uid = pReq->suid,
939,696✔
239
  };
240
  code = metaHandleEntry2(pMeta, &entry);
940,351✔
241
  if (code) {
941,307✔
242
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
243
              tstrerror(code));
244
  } else {
245
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
941,307✔
246
             pReq->suid, verison);
247
  }
248
  TAOS_RETURN(code);
942,306✔
249
}
250

251
// Alter Super Table
252

253
// Create Child Table
254
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
68,084,551✔
255
  int32_t   code = TSDB_CODE_SUCCESS;
68,084,551✔
256
  void     *value = NULL;
68,084,551✔
257
  int32_t   valueSize = 0;
68,085,219✔
258
  SMetaInfo info;
68,080,552✔
259

260
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
68,079,923✔
261
      pReq->ctb.suid == 0) {
68,085,030✔
262
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
8,154✔
263
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
264
    return TSDB_CODE_INVALID_MSG;
×
265
  }
266

267
  // check table existence
268
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
68,072,767✔
269
    pReq->uid = *(int64_t *)value;
339,386✔
270
    tdbFreeClear(value);
339,386✔
271

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

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

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

295
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
337,491✔
296
  }
297

298
  // check super table existence
299
  SMetaEntry *pStbEntry = NULL;
67,745,523✔
300
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
67,744,399✔
301
  if (code) {
67,735,711✔
302
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
303
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
304
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
305
  }
306

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

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

323
  // Check tag value
324
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
67,724,919✔
325
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
67,739,881✔
326
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
67,734,011✔
327
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
287,325,979✔
328
      STagVal tagVal = {
219,869,124✔
329
          .cid = pTagSchema->pSchema[i].colId,
219,859,680✔
330
      };
331

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

343
  metaFetchEntryFree(&pStbEntry);
67,740,964✔
344

345
  // check grant
346
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
67,721,547✔
347
    code = grantCheck(TSDB_GRANT_TIMESERIES);
67,740,857✔
348
    if (TSDB_CODE_SUCCESS != code) {
67,715,084✔
349
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
350
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
351
    }
352
  }
353
  return code;
67,719,309✔
354
}
355

356
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
67,378,246✔
357
  int32_t code = TSDB_CODE_SUCCESS;
67,378,246✔
358

359
  if (NULL == ppRsp) {
67,378,246✔
360
    return code;
×
361
  }
362

363
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
67,378,246✔
364
  if (NULL == ppRsp) {
67,351,481✔
365
    return terrno;
×
366
  }
367

368
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
67,351,481✔
369
  (*ppRsp)->tuid = pEntry->uid;
67,366,781✔
370
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
67,378,321✔
371
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
67,372,880✔
372

373
  return code;
67,388,706✔
374
}
375

376
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
67,735,213✔
377
  int32_t code = TSDB_CODE_SUCCESS;
67,735,213✔
378

379
  // check request
380
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
67,735,213✔
381
  if (code) {
67,712,486✔
382
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
339,348✔
383
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,857✔
384
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
385
    }
386
    return code;
339,348✔
387
  }
388

389
  SMetaEntry entry = {
67,373,138✔
390
      .version = version,
391
      .type = TSDB_CHILD_TABLE,
392
      .uid = pReq->uid,
67,377,679✔
393
      .name = pReq->name,
67,375,500✔
394
      .ctbEntry.btime = pReq->btime,
67,390,278✔
395
      .ctbEntry.ttlDays = pReq->ttl,
67,377,323✔
396
      .ctbEntry.commentLen = pReq->commentLen,
67,359,816✔
397
      .ctbEntry.comment = pReq->comment,
67,360,786✔
398
      .ctbEntry.suid = pReq->ctb.suid,
67,385,823✔
399
      .ctbEntry.pTags = pReq->ctb.pTag,
67,369,747✔
400
  };
401

402
  // build response
403
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
67,364,136✔
404
  if (code) {
67,380,553✔
405
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
406
              tstrerror(code));
407
  }
408

409
  // handle entry
410
  code = metaHandleEntry2(pMeta, &entry);
67,380,553✔
411
  if (TSDB_CODE_SUCCESS == code) {
67,387,181✔
412
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
67,388,391✔
413
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
414
  } else {
415
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
416
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
417
              pReq->ctb.suid, version);
418
  }
419
  return code;
67,400,427✔
420
}
421

422
// Drop Child Table
423

424
// Alter Child Table
425

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

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

438
  // check name
439
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
7,750,384✔
440
    // for auto create table, we return the uid of the existing table
441
    pReq->uid = *(tb_uid_t *)value;
36,739✔
442
    tdbFree(value);
36,739✔
443
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
36,739✔
444
  }
445

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

455
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
7,516,149✔
456
  int32_t code = TSDB_CODE_SUCCESS;
7,516,149✔
457

458
  if (NULL == ppRsp) {
7,516,149✔
459
    return code;
×
460
  }
461

462
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
7,516,149✔
463
  if (NULL == *ppRsp) {
7,516,149✔
464
    return terrno;
×
465
  }
466

467
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, pEntry->ntbEntry.ownerId, *ppRsp);
7,516,149✔
468
  if (code) {
7,516,149✔
469
    taosMemoryFreeClear(*ppRsp);
×
470
    return code;
×
471
  }
472

473
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
86,430,230✔
474
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
78,914,081✔
475
    (*ppRsp)->pSchemaExt[i].colId = p->id;
78,914,081✔
476
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
78,914,081✔
477
    if (pEntry->pExtSchemas) {
78,914,081✔
478
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
572,088✔
479
    }
480
  }
481

482
  return code;
7,516,149✔
483
}
484

485
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
7,552,888✔
486
  int32_t code = TSDB_CODE_SUCCESS;
7,552,888✔
487

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

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

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

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

534
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
197,496✔
535
  int32_t code = TSDB_CODE_SUCCESS;
197,496✔
536

537
  if (NULL == ppRsp) {
197,496✔
538
    return code;
×
539
  }
540

541
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
197,496✔
542
  if (NULL == *ppRsp) {
197,496✔
543
    return terrno;
×
544
  }
545

546
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, pEntry->pExtSchemas,
197,496✔
547
                              pEntry->ntbEntry.ownerId, *ppRsp, TSDB_VIRTUAL_NORMAL_TABLE);
548
  if (code) {
197,496✔
549
    taosMemoryFreeClear(*ppRsp);
×
550
    return code;
×
551
  }
552

553
  return code;
197,496✔
554
}
555

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

567
  SMetaEntry entry = {.version = version,
592,380✔
568
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
569
                      .uid = pReq->uid,
197,496✔
570
                      .name = pReq->name,
197,496✔
571
                      .ntbEntry.btime = pReq->btime,
197,496✔
572
                      .ntbEntry.ttlDays = pReq->ttl,
197,496✔
573
                      .ntbEntry.commentLen = pReq->commentLen,
197,496✔
574
                      .ntbEntry.comment = pReq->comment,
197,496✔
575
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
576
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
197,496✔
577
                      .ntbEntry.ownerId = pReq->ntb.userId,
197,496✔
578
                      .pExtSchemas = pReq->pExtSchemas,
197,496✔
579
                      .colRef = pReq->colRef};
580

581
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
197,496✔
582
  if (code) {
197,496✔
583
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
584
              tstrerror(code));
585
    TAOS_RETURN(code);
×
586
  }
587

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

603
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
349,645✔
604
  int32_t    code = TSDB_CODE_SUCCESS;
349,645✔
605
  SMetaEntry *pSuper = NULL;
349,645✔
606

607
  if (NULL == ppRsp) {
349,645✔
608
    return code;
×
609
  }
610

611
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
349,645✔
612
  if (NULL == *ppRsp) {
349,645✔
613
    return terrno;
×
614
  }
615

616
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
349,645✔
617
  if (code != TSDB_CODE_SUCCESS) {
349,645✔
NEW
618
    taosMemoryFreeClear(*ppRsp);
×
NEW
619
    return code;
×
620
  }
621

622
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pSuper->stbEntry.schemaRow, &pEntry->colRef, pSuper->pExtSchemas,
699,236✔
623
                              pSuper->stbEntry.ownerId, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
349,645✔
624
  if (code) {
349,645✔
NEW
625
    metaFetchEntryFree(&pSuper);
×
626
    taosMemoryFreeClear(*ppRsp);
×
627
    return code;
×
628
  }
629

630
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
349,645✔
631
  metaFetchEntryFree(&pSuper);
349,645✔
632

633
  return code;
349,645✔
634
}
635

636
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
349,645✔
637
  // check request
638
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
349,645✔
639
  if (code) {
349,645✔
640
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
641
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
642
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
643
    }
644
    TAOS_RETURN(code);
×
645
  }
646

647
  SMetaEntry entry = {.version = version,
699,236✔
648
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
649
                      .uid = pReq->uid,
349,645✔
650
                      .name = pReq->name,
349,645✔
651
                      .ctbEntry.btime = pReq->btime,
349,645✔
652
                      .ctbEntry.ttlDays = pReq->ttl,
349,645✔
653
                      .ctbEntry.commentLen = pReq->commentLen,
349,645✔
654
                      .ctbEntry.comment = pReq->comment,
349,645✔
655
                      .ctbEntry.suid = pReq->ctb.suid,
349,645✔
656
                      .ctbEntry.pTags = pReq->ctb.pTag,
349,645✔
657
                      .colRef = pReq->colRef};
658

659
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
349,645✔
660
  if (code) {
349,645✔
661
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
662
              tstrerror(code));
663
    TAOS_RETURN(code);
×
664
  }
665

666
  // handle entry
667
  code = metaHandleEntry2(pMeta, &entry);
349,645✔
668
  if (TSDB_CODE_SUCCESS == code) {
349,645✔
669
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
349,645✔
670
             version, pReq->name, pReq->uid);
671
  } else {
672
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
673
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
674
  }
675
  TAOS_RETURN(code);
349,645✔
676
#if 0
677
  metaTimeSeriesNotifyCheck(pMeta);
678
#endif
679
}
680

681
// Drop Normal Table
682

683
// Alter Normal Table
684

685
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
75,834,088✔
686
  int32_t code = TSDB_CODE_SUCCESS;
75,834,088✔
687
  if (TSDB_CHILD_TABLE == pReq->type) {
75,834,088✔
688
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
67,736,786✔
689
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
8,100,029✔
690
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
7,552,888✔
691
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
547,141✔
692
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
197,496✔
693
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
349,645✔
694
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
349,645✔
695
  } else {
696
    code = TSDB_CODE_INVALID_MSG;
×
697
  }
698
  TAOS_RETURN(code);
75,838,239✔
699
}
700

701
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,732,951✔
702
  int32_t code = TSDB_CODE_SUCCESS;
1,732,951✔
703

704
  // check request
705
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,732,951✔
706
  if (code) {
1,732,951✔
707
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
708
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
709
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
710
    }
711
    TAOS_RETURN(code);
×
712
  }
713

714
  if (pReq->suid == pReq->uid) {
1,732,951✔
715
    code = TSDB_CODE_INVALID_PARA;
×
716
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
717
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
718
    TAOS_RETURN(code);
×
719
  }
720

721
  SMetaEntry entry = {
1,732,951✔
722
      .version = version,
723
      .uid = pReq->uid,
1,732,951✔
724
  };
725

726
  if (pReq->isVirtual) {
1,732,951✔
727
    if (pReq->suid == 0) {
72,048✔
728
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
40,362✔
729
    } else {
730
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
31,686✔
731
    }
732
  } else {
733
    if (pReq->suid == 0) {
1,660,903✔
734
      entry.type = -TSDB_NORMAL_TABLE;
833,214✔
735
    } else {
736
      entry.type = -TSDB_CHILD_TABLE;
827,689✔
737
    }
738
  }
739
  code = metaHandleEntry2(pMeta, &entry);
1,732,951✔
740
  if (code) {
1,732,951✔
741
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
742
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
743
  } else {
744
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,732,951✔
745
             pReq->uid, version);
746
  }
747
  TAOS_RETURN(code);
1,732,951✔
748
}
749

750
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,501,669✔
751
  int32_t code = 0;
4,501,669✔
752

753
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,501,669✔
754
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
755
              __func__, __FILE__, __LINE__, pReq->colName, version);
756
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
757
  }
758

759
  // check name
760
  void   *value = NULL;
4,501,669✔
761
  int32_t valueSize = 0;
4,501,669✔
762
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,501,669✔
763
  if (code) {
4,501,669✔
764
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
765
              __FILE__, __LINE__, pReq->tbName, version);
766
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
767
    TAOS_RETURN(code);
×
768
  }
769
  int64_t uid = *(int64_t *)value;
4,501,669✔
770
  tdbFreeClear(value);
4,501,669✔
771

772
  // check table type
773
  SMetaInfo info;
4,501,453✔
774
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,501,669✔
775
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
776
              " not found, this is an internal error in meta, version:%" PRId64,
777
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
778
    code = TSDB_CODE_INTERNAL_ERROR;
×
779
    TAOS_RETURN(code);
×
780
  }
781
  if (info.suid != 0 && pReq->action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF &&
4,501,669✔
782
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
30,214✔
783
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
784
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
785
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
786
    TAOS_RETURN(code);
×
787
  }
788

789
  // check grant
790
  code = grantCheck(TSDB_GRANT_TIMESERIES);
4,501,669✔
791
  if (code) {
4,501,669✔
792
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
793
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
794
    TAOS_RETURN(code);
×
795
  }
796
  TAOS_RETURN(code);
4,501,669✔
797
}
798

799
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,710,972✔
800
  int32_t code = TSDB_CODE_SUCCESS;
3,710,972✔
801

802
  // check request
803
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,710,972✔
804
  if (code) {
3,710,972✔
805
    TAOS_RETURN(code);
×
806
  }
807

808
  // fetch old entry
809
  SMetaEntry *pEntry = NULL;
3,710,972✔
810
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,710,972✔
811
  if (code) {
3,710,972✔
812
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
813
              __FILE__, __LINE__, pReq->tbName, version);
814
    TAOS_RETURN(code);
×
815
  }
816
  if (pEntry->version >= version) {
3,710,972✔
817
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
818
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
819
    metaFetchEntryFree(&pEntry);
×
820
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
821
  }
822

823
  // do add column
824
  int32_t         rowSize = 0;
3,710,972✔
825
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,710,972✔
826
  SSchema        *pColumn;
827
  SExtSchema      extSchema = {0};
3,710,972✔
828
  pEntry->version = version;
3,710,972✔
829
  for (int32_t i = 0; i < pSchema->nCols; i++) {
2,147,483,647✔
830
    pColumn = &pSchema->pSchema[i];
2,147,483,647✔
831
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
2,147,483,647✔
832
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
330,803✔
833
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
834
      metaFetchEntryFree(&pEntry);
330,803✔
835
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
330,803✔
836
    }
837
    rowSize += pColumn->bytes;
2,147,483,647✔
838
  }
839

840
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
3,380,169✔
841
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,380,169✔
842
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,710✔
843
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
844
    metaFetchEntryFree(&pEntry);
6,710✔
845
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,710✔
846
  }
847

848
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,373,459✔
849
  if (pSchema->nCols + 1 > maxCols) {
3,373,459✔
850
    metaError("vgId:%d, %s failed at %s:%d since column count %d + 1 > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
851
              __func__, __FILE__, __LINE__, pSchema->nCols, maxCols, version);
852
    metaFetchEntryFree(&pEntry);
×
853
    TAOS_RETURN(TSDB_CODE_PAR_TOO_MANY_COLUMNS);
×
854
  }
855

856
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
3,373,459✔
857
  if (NULL == pNewSchema) {
3,373,459✔
858
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
859
              __LINE__, tstrerror(terrno), version);
860
    metaFetchEntryFree(&pEntry);
×
861
    TAOS_RETURN(terrno);
×
862
  }
863
  pSchema->pSchema = pNewSchema;
3,373,459✔
864
  pSchema->version++;
3,373,459✔
865
  pSchema->nCols++;
3,373,459✔
866
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,373,459✔
867
  pColumn->bytes = pReq->bytes;
3,373,459✔
868
  pColumn->type = pReq->type;
3,373,459✔
869
  pColumn->flags = pReq->flags;
3,373,459✔
870
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,373,459✔
871
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
552✔
872
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
873
              version);
874
    metaFetchEntryFree(&pEntry);
552✔
875
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
552✔
876
  }
877
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,372,907✔
878
  extSchema.typeMod = pReq->typeMod;
3,372,907✔
879
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,372,907✔
880
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,372,907✔
881
    SColRef tmpRef;
58,214✔
882
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
58,268✔
883
      tmpRef.hasRef = false;
33,570✔
884
      tmpRef.id = pColumn->colId;
33,570✔
885
    } else {
886
      tmpRef.hasRef = true;
24,698✔
887
      tmpRef.id = pColumn->colId;
24,698✔
888
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
24,698✔
889
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
24,698✔
890
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
24,698✔
891
    }
892
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
58,268✔
893
    if (code) {
58,268✔
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
  } else {
900
    uint32_t compress;
901
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
3,314,639✔
902
      compress = createDefaultColCmprByType(pColumn->type);
3,307,463✔
903
    } else {
904
      compress = pReq->compress;
7,176✔
905
    }
906
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
3,314,639✔
907
    if (code) {
3,314,639✔
908
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
909
                __LINE__, tstrerror(code), version);
910
      metaFetchEntryFree(&pEntry);
×
911
      TAOS_RETURN(code);
×
912
    }
913
  }
914
  code = addTableExtSchema(pEntry, pColumn, pSchema->nCols, &extSchema);
3,372,907✔
915
  if (code) {
3,372,907✔
916
    metaError("vgId:%d, %s failed to add ext schema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
917
              __func__, __FILE__, __LINE__, tstrerror(code), version);
918
    metaFetchEntryFree(&pEntry);
×
919
    TAOS_RETURN(code);
×
920
  }
921

922
  // do handle entry
923
  code = metaHandleEntry2(pMeta, pEntry);
3,372,907✔
924
  if (code) {
3,372,907✔
925
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
926
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
927
    metaFetchEntryFree(&pEntry);
×
928
    TAOS_RETURN(code);
×
929
  } else {
930
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,372,907✔
931
             pEntry->uid, version);
932
  }
933

934
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,372,907✔
935
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
58,268✔
936
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
58,268✔
937
    if (code) {
58,268✔
938
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
939
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
940
    } else {
941
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,514,575✔
942
        SColRef *p = &pEntry->colRef.pColRef[i];
18,456,307✔
943
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,456,307✔
944
        pRsp->pColRefs[i].id = p->id;
18,456,307✔
945
        if (p->hasRef) {
18,456,307✔
946
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
185,973✔
947
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
185,973✔
948
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
185,973✔
949
        }
950
      }
951
    }
952
  } else {
953
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,314,639✔
954
    if (code) {
3,314,639✔
955
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
956
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
957
    } else {
958
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
959
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
960
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
961
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
962
      }
963
    }
964
  }
965

966
  metaFetchEntryFree(&pEntry);
3,372,907✔
967
  TAOS_RETURN(code);
3,372,907✔
968
}
969

970
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
96,108✔
971
  int32_t code = TSDB_CODE_SUCCESS;
96,108✔
972

973
  // check request
974
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
96,108✔
975
  if (code) {
96,108✔
976
    TAOS_RETURN(code);
×
977
  }
978

979
  // fetch old entry
980
  SMetaEntry *pEntry = NULL;
96,108✔
981
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
96,108✔
982
  if (code) {
96,108✔
983
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
984
              __FILE__, __LINE__, pReq->tbName, version);
985
    TAOS_RETURN(code);
×
986
  }
987

988
  if (pEntry->version >= version) {
96,108✔
989
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
990
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
991
    metaFetchEntryFree(&pEntry);
×
992
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
993
  }
994

995
  // search the column to drop
996
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
96,108✔
997
  SSchema        *pColumn = NULL;
96,108✔
998
  SSchema         tColumn;
96,000✔
999
  int32_t         iColumn = 0;
96,108✔
1000
  for (; iColumn < pSchema->nCols; iColumn++) {
64,381,540✔
1001
    pColumn = &pSchema->pSchema[iColumn];
64,381,540✔
1002
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
64,381,540✔
1003
      break;
96,108✔
1004
    }
1005
  }
1006

1007
  if (iColumn == pSchema->nCols) {
96,108✔
1008
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1009
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1010
    metaFetchEntryFree(&pEntry);
×
1011
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1012
  }
1013

1014
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
96,108✔
1015
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1016
              __func__, __FILE__, __LINE__, pReq->colName, version);
1017
    metaFetchEntryFree(&pEntry);
×
1018
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1019
  }
1020

1021
  tColumn = *pColumn;
96,108✔
1022

1023
  // do drop column
1024
  pEntry->version = version;
96,108✔
1025
  if (pSchema->nCols - iColumn - 1 > 0) {
96,108✔
1026
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
72,964✔
1027
  }
1028
  pSchema->nCols--;
96,108✔
1029
  pSchema->version++;
96,108✔
1030
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
96,108✔
1031
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
31,807✔
1032
    if (code) {
31,807✔
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->colRef.nCols != pSchema->nCols) {
31,807✔
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
  } else {
1045
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
64,301✔
1046
    if (code) {
64,301✔
1047
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1048
                __LINE__, tstrerror(code), version);
1049
      metaFetchEntryFree(&pEntry);
×
1050
      TAOS_RETURN(code);
×
1051
    }
1052
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
64,301✔
1053
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1054
                __func__, __FILE__, __LINE__, version);
1055
      metaFetchEntryFree(&pEntry);
×
1056
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1057
    }
1058
  }
1059

1060
  // update column extschema
1061
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
96,108✔
1062
  if (code) {
96,108✔
1063
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1064
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1065
    metaFetchEntryFree(&pEntry);
×
1066
    TAOS_RETURN(code);
×
1067
  }
1068

1069
  // do handle entry
1070
  code = metaHandleEntry2(pMeta, pEntry);
96,108✔
1071
  if (code) {
96,108✔
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
    metaFetchEntryFree(&pEntry);
×
1075
    TAOS_RETURN(code);
×
1076
  } else {
1077
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
96,108✔
1078
             pEntry->uid, version);
1079
  }
1080

1081
  // build response
1082
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
96,108✔
1083
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
31,807✔
1084
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
31,807✔
1085
    if (code) {
31,807✔
1086
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1087
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1088
    } else {
1089
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
36,376,729✔
1090
        SColRef *p = &pEntry->colRef.pColRef[i];
36,344,922✔
1091
        pRsp->pColRefs[i].hasRef = p->hasRef;
36,344,922✔
1092
        pRsp->pColRefs[i].id = p->id;
36,344,922✔
1093
        if (p->hasRef) {
36,344,922✔
1094
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
18,186,595✔
1095
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
18,186,595✔
1096
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
18,186,595✔
1097
        }
1098
      }
1099
    }
1100
  } else {
1101
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
64,301✔
1102
    if (code) {
64,301✔
1103
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1104
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1105
    } else {
1106
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
58,209,884✔
1107
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
58,145,583✔
1108
        pRsp->pSchemaExt[i].colId = p->id;
58,145,583✔
1109
        pRsp->pSchemaExt[i].compress = p->alg;
58,145,583✔
1110
      }
1111
    }
1112
  }
1113

1114
  metaFetchEntryFree(&pEntry);
96,108✔
1115
  TAOS_RETURN(code);
96,108✔
1116
}
1117

1118
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
42,024✔
1119
  int32_t code = TSDB_CODE_SUCCESS;
42,024✔
1120

1121
  // check request
1122
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
42,024✔
1123
  if (code) {
42,024✔
1124
    TAOS_RETURN(code);
×
1125
  }
1126

1127
  if (NULL == pReq->colNewName) {
42,024✔
1128
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1129
              __func__, __FILE__, __LINE__, version);
1130
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1131
  }
1132

1133
  // fetch old entry
1134
  SMetaEntry *pEntry = NULL;
42,024✔
1135
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
42,024✔
1136
  if (code) {
42,024✔
1137
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1138
              __FILE__, __LINE__, pReq->tbName, version);
1139
    TAOS_RETURN(code);
×
1140
  }
1141

1142
  if (pEntry->version >= version) {
42,024✔
1143
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1144
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1145
    metaFetchEntryFree(&pEntry);
×
1146
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1147
  }
1148

1149
  // search the column to update
1150
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
42,024✔
1151
  SSchema        *pColumn = NULL;
42,024✔
1152
  int32_t         iColumn = 0;
42,024✔
1153
  for (int32_t i = 0; i < pSchema->nCols; i++) {
196,165✔
1154
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
196,165✔
1155
      pColumn = &pSchema->pSchema[i];
42,024✔
1156
      iColumn = i;
42,024✔
1157
      break;
42,024✔
1158
    }
1159
  }
1160

1161
  if (NULL == pColumn) {
42,024✔
1162
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1163
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1164
    metaFetchEntryFree(&pEntry);
×
1165
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1166
  }
1167

1168

1169
  // do update column name
1170
  pEntry->version = version;
42,024✔
1171
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
42,024✔
1172
  pSchema->version++;
42,024✔
1173

1174
  // do handle entry
1175
  code = metaHandleEntry2(pMeta, pEntry);
42,024✔
1176
  if (code) {
42,024✔
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
    metaFetchEntryFree(&pEntry);
×
1180
    TAOS_RETURN(code);
×
1181
  } else {
1182
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
42,024✔
1183
             pEntry->uid, version);
1184
  }
1185

1186
  // build response
1187
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
42,024✔
1188
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
29,032✔
1189
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
29,032✔
1190
    if (code) {
29,032✔
1191
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1192
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1193
    } else {
1194
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
205,146✔
1195
        SColRef *p = &pEntry->colRef.pColRef[i];
176,114✔
1196
        pRsp->pColRefs[i].hasRef = p->hasRef;
176,114✔
1197
        pRsp->pColRefs[i].id = p->id;
176,114✔
1198
        if (p->hasRef) {
176,114✔
1199
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
108,664✔
1200
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
108,664✔
1201
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
108,664✔
1202
        }
1203
      }
1204
    }
1205
  } else {
1206
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
12,992✔
1207
    if (code) {
12,992✔
1208
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1209
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1210
    } else {
1211
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
165,362✔
1212
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
152,370✔
1213
        pRsp->pSchemaExt[i].colId = p->id;
152,370✔
1214
        pRsp->pSchemaExt[i].compress = p->alg;
152,370✔
1215
      }
1216
    }
1217
  }
1218

1219
  metaFetchEntryFree(&pEntry);
42,024✔
1220
  TAOS_RETURN(code);
42,024✔
1221
}
1222

1223
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
510,293✔
1224
  int32_t code = TSDB_CODE_SUCCESS;
510,293✔
1225

1226
  // check request
1227
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
510,293✔
1228
  if (code) {
510,293✔
1229
    TAOS_RETURN(code);
×
1230
  }
1231

1232
  // fetch old entry
1233
  SMetaEntry *pEntry = NULL;
510,293✔
1234
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
510,293✔
1235
  if (code) {
510,293✔
1236
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1237
              __FILE__, __LINE__, pReq->tbName, version);
1238
    TAOS_RETURN(code);
×
1239
  }
1240

1241
  if (pEntry->version >= version) {
510,293✔
1242
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1243
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1244
    metaFetchEntryFree(&pEntry);
×
1245
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1246
  }
1247

1248
  // search the column to update
1249
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
510,293✔
1250
  SSchema        *pColumn = NULL;
510,293✔
1251
  int32_t         iColumn = 0;
510,293✔
1252
  int32_t         rowSize = 0;
510,293✔
1253
  for (int32_t i = 0; i < pSchema->nCols; i++) {
38,251,242✔
1254
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
37,740,949✔
1255
      pColumn = &pSchema->pSchema[i];
510,293✔
1256
      iColumn = i;
510,293✔
1257
    }
1258
    rowSize += pSchema->pSchema[i].bytes;
37,740,949✔
1259
  }
1260

1261
  if (NULL == pColumn) {
510,293✔
1262
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1263
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1264
    metaFetchEntryFree(&pEntry);
×
1265
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1266
  }
1267

1268
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
510,293✔
1269
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
205,997✔
1270
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1271
              version);
1272
    metaFetchEntryFree(&pEntry);
205,997✔
1273
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
205,997✔
1274
  }
1275

1276
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
304,296✔
1277
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
304,296✔
1278
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
46,970✔
1279
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1280
              version);
1281
    metaFetchEntryFree(&pEntry);
46,970✔
1282
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
46,970✔
1283
  }
1284

1285
  // do change the column bytes
1286
  pEntry->version = version;
257,326✔
1287
  pSchema->version++;
257,326✔
1288
  pColumn->bytes = pReq->colModBytes;
257,326✔
1289

1290
  // do handle entry
1291
  code = metaHandleEntry2(pMeta, pEntry);
257,326✔
1292
  if (code) {
257,326✔
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
    metaFetchEntryFree(&pEntry);
×
1296
    TAOS_RETURN(code);
×
1297
  } else {
1298
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
257,326✔
1299
             pEntry->uid, version);
1300
  }
1301

1302
  // build response
1303
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
257,326✔
1304
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
28,186✔
1305
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
28,186✔
1306
    if (code) {
28,186✔
1307
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1308
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1309
    } else {
1310
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,280,020✔
1311
        SColRef *p = &pEntry->colRef.pColRef[i];
18,251,834✔
1312
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,251,834✔
1313
        pRsp->pColRefs[i].id = p->id;
18,251,834✔
1314
        if (p->hasRef) {
18,251,834✔
1315
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
82,228✔
1316
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
82,228✔
1317
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
82,228✔
1318
        }
1319
      }
1320
    }
1321
  } else {
1322
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
229,140✔
1323
    if (code) {
229,140✔
1324
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1325
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1326
    } else {
1327
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,346,608✔
1328
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,117,468✔
1329
        pRsp->pSchemaExt[i].colId = p->id;
9,117,468✔
1330
        pRsp->pSchemaExt[i].compress = p->alg;
9,117,468✔
1331
      }
1332
    }
1333
  }
1334

1335
  metaFetchEntryFree(&pEntry);
257,326✔
1336
  TAOS_RETURN(code);
257,326✔
1337
}
1338

1339

1340

1341
static bool tagValueChanged(const SUpdatedTagVal* pNewVal, const STag *pOldTag) {
8,343,531✔
1342
  STagVal oldVal = { .cid = pNewVal->colId };
8,343,531✔
1343

1344
  if (pNewVal->isNull) {
8,343,531✔
1345
    return tTagGet(pOldTag, &oldVal);
70,095✔
1346
  }
1347

1348
  if (!tTagGet(pOldTag, &oldVal)){
8,273,436✔
1349
    return true;
204,354✔
1350
  }
1351

1352
  if (!IS_VAR_DATA_TYPE(oldVal.type)) {
8,069,082✔
1353
    return (memcmp(&oldVal.i64, pNewVal->pTagVal, pNewVal->nTagVal) != 0);
7,831,371✔
1354
  }
1355

1356
  if (oldVal.nData != pNewVal->nTagVal) {
237,711✔
1357
    return true;
180,768✔
1358
  }
1359

1360
  return (memcmp(pNewVal->pTagVal, oldVal.pData, oldVal.nData) != 0);
56,943✔
1361
}
1362

1363

1364

1365
static int32_t updatedTagValueArrayToHashMap(SSchemaWrapper* pTagSchema, SArray* arr, SHashObj **hashMap) {
8,169,277✔
1366
  int32_t numOfTags = arr == NULL ? 0 : taosArrayGetSize(arr);
8,169,277✔
1367
  if (numOfTags == 0) {
8,170,677✔
1368
    metaError("%s failed at %s:%d since no tags specified", __func__, __FILE__, __LINE__);
×
1369
    return TSDB_CODE_INVALID_MSG;
×
1370
  }
1371

1372
  *hashMap = taosHashInit(numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
8,170,677✔
1373
  if (*hashMap == NULL) {
8,169,277✔
1374
    metaError("%s failed at %s:%d since %s", __func__, __FILE__, __LINE__, tstrerror(terrno));
×
1375
    return terrno;
×
1376
  }
1377

1378
  for (int32_t i = 0; i < taosArrayGetSize(arr); i++) {
16,407,377✔
1379
    SUpdatedTagVal *pTagVal = taosArrayGet(arr, i);
8,238,100✔
1380
    if (taosHashGet(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId)) != NULL) {
8,238,800✔
1381
      metaError("%s failed at %s:%d since duplicate tags %s", __func__, __FILE__, __LINE__, pTagVal->tagName);
×
1382
      taosHashCleanup(*hashMap);
×
1383
      return TSDB_CODE_INVALID_MSG;
×
1384
    }
1385

1386
    int32_t code = taosHashPut(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId), pTagVal, sizeof(*pTagVal));
8,239,575✔
1387
    if (code) {
8,238,100✔
1388
      metaError("%s failed at %s:%d since %s", __func__, __FILE__, __LINE__, tstrerror(code));
×
1389
      taosHashCleanup(*hashMap);
×
1390
      return code;
×
1391
    }
1392
  }
1393

1394
  int32_t changed = 0;
8,169,977✔
1395
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,672,319✔
1396
    int32_t schemaColId = pTagSchema->pSchema[i].colId;
9,502,342✔
1397
    if (taosHashGet(*hashMap, &schemaColId, sizeof(schemaColId)) != NULL) {
9,501,017✔
1398
      changed++;
8,240,900✔
1399
    }
1400
  }
1401
  if (changed < numOfTags) {
8,169,977✔
1402
    metaError("%s failed at %s:%d since tag count mismatch, %d:%d", __func__, __FILE__, __LINE__, changed, numOfTags);
×
1403
    taosHashCleanup(*hashMap);
×
1404
    return TSDB_CODE_VND_COL_NOT_EXISTS;
×
1405
  }
1406

1407
  return TSDB_CODE_SUCCESS;
8,169,977✔
1408
}
1409

1410

1411

1412
static int32_t metaUpdateTableJsonTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
125,269✔
1413
  SSchema *pCol = &pTagSchema->pSchema[0];
125,269✔
1414
  int32_t colId = pCol->colId;
125,269✔
1415
  SUpdatedTagVal *pTagVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
125,269✔
1416
  void *pNewTag = taosMemoryRealloc(pTable->ctbEntry.pTags, pTagVal->nTagVal);
125,269✔
1417
  if (pNewTag == NULL) {
125,269✔
1418
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1419
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1420
    return TSDB_CODE_OUT_OF_MEMORY;
×
1421
  }
1422
  pTable->ctbEntry.pTags = pNewTag;
125,269✔
1423
  memcpy(pTable->ctbEntry.pTags, pTagVal->pTagVal, pTagVal->nTagVal);
125,269✔
1424

1425
  int32_t code = metaHandleEntry2(pMeta, pTable);
125,269✔
1426
  if (code) {
125,269✔
1427
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
×
1428
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTable->uid, pTable->name, pTable->version);
×
1429
  } else {
1430
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
125,269✔
1431
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
125,269✔
1432
  }
1433

1434
  return code;
125,269✔
1435
}
1436

1437

1438

1439
static int32_t metaUpdateTableNormalTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
8,275,958✔
1440
  int32_t code = TSDB_CODE_SUCCESS;
8,275,958✔
1441

1442
  const STag *pOldTag = (const STag *)pTable->ctbEntry.pTags;
8,275,958✔
1443
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
8,275,958✔
1444
  if (pTagArray == NULL) {
8,275,958✔
1445
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1446
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1447
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1448
  }
1449

1450
  bool allSame = true;
8,275,958✔
1451

1452
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
18,063,856✔
1453
    SSchema *pCol = &pTagSchema->pSchema[i];
9,791,873✔
1454
    STagVal  value = { .cid = pCol->colId };
9,791,873✔
1455

1456
    int32_t colId = pCol->colId;
9,791,873✔
1457
    SUpdatedTagVal *pNewVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
9,791,873✔
1458
    if (pNewVal == NULL) {
9,792,498✔
1459
      if (!tTagGet(pOldTag, &value)) {
1,448,967✔
1460
        continue;
300,807✔
1461
      }
1462
    } else {
1463
      value.type = pCol->type;
8,343,531✔
1464
      if (tagValueChanged(pNewVal, pOldTag)) {
8,343,531✔
1465
        allSame = false;
8,180,974✔
1466
      }
1467
      if (pNewVal->isNull) {
8,343,531✔
1468
        continue;
70,095✔
1469
      }
1470

1471
      if (IS_VAR_DATA_TYPE(pCol->type)) {
8,273,436✔
1472
        if ((int32_t)pNewVal->nTagVal > (pCol->bytes - VARSTR_HEADER_SIZE)) {
325,938✔
1473
          const char* msgFmt = "vgId:%d, %s failed at %s:%d since value too long for tag %s, version:%" PRId64;
3,975✔
1474
          metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pCol->name, pTable->version);
3,975✔
1475
          taosArrayDestroy(pTagArray);
3,975✔
1476
          TAOS_RETURN(TSDB_CODE_PAR_VALUE_TOO_LONG);
3,975✔
1477
        }
1478
        value.pData = pNewVal->pTagVal;
321,963✔
1479
        value.nData = pNewVal->nTagVal;
321,963✔
1480
      } else {
1481
        memcpy(&value.i64, pNewVal->pTagVal, pNewVal->nTagVal);
7,947,498✔
1482
      }
1483
    }
1484

1485
    if (taosArrayPush(pTagArray, &value) == NULL) {
9,416,996✔
1486
      const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1487
      metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1488
      taosArrayDestroy(pTagArray);
×
1489
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1490
    }
1491
  }
1492

1493
  if (allSame) {
8,271,983✔
1494
    const char* msgFmt = "vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64;
149,586✔
1495
    metaWarn(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pTable->version);
149,586✔
1496
    taosArrayDestroy(pTagArray);
149,586✔
1497
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
149,586✔
1498
  } 
1499

1500
  STag *pNewTag = NULL;
8,122,397✔
1501
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
8,122,397✔
1502
  if (code) {
8,122,397✔
1503
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1504
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTable->version);
×
1505
    taosArrayDestroy(pTagArray);
×
1506
    TAOS_RETURN(code);
×
1507
  }
1508
  taosArrayDestroy(pTagArray);
8,122,397✔
1509
  taosMemoryFree(pTable->ctbEntry.pTags);
8,122,397✔
1510
  pTable->ctbEntry.pTags = (uint8_t *)pNewTag;
8,121,772✔
1511
  return TSDB_CODE_SUCCESS;
8,122,397✔
1512
}
1513

1514

1515
static int32_t regexReplaceBuildSubstitution(const char *pReplace, const regmatch_t *pmatch, int32_t nmatch,
13,986✔
1516
                                               const char *cursor, char **ppResult, size_t *pResultLen,
1517
                                               size_t *pResultCap) {
1518
  char  *result = *ppResult;
13,986✔
1519
  size_t resultLen = *pResultLen;
13,986✔
1520
  size_t resultCap = *pResultCap;
13,986✔
1521

1522
  const char *r = pReplace;
13,986✔
1523
  while (*r != '\0') {
203,460✔
1524
    const char *chunk = NULL;
189,474✔
1525
    size_t      chunkLen = 0;
189,474✔
1526

1527
    if (*r == '$' && r[1] >= '0' && r[1] <= '9') {
189,474✔
1528
      int32_t groupIdx = r[1] - '0';
21,200✔
1529
      if (groupIdx < nmatch && pmatch[groupIdx].rm_so != -1) {
21,200✔
1530
        chunk = cursor + pmatch[groupIdx].rm_so;
21,200✔
1531
        chunkLen = (size_t)(pmatch[groupIdx].rm_eo - pmatch[groupIdx].rm_so);
21,200✔
1532
      }
1533
      r += 2;
21,200✔
1534
    } else if (*r == '$' && r[1] == '$') {
168,274✔
1535
      chunk = "$";
×
1536
      chunkLen = 1;
×
1537
      r += 2;
×
1538
    } else {
1539
      chunk = r;
168,274✔
1540
      chunkLen = 1;
168,274✔
1541
      r += 1;
168,274✔
1542
    }
1543

1544
    if (chunkLen > 0) {
189,474✔
1545
      if (resultLen + chunkLen >= resultCap) {
189,474✔
1546
        resultCap = (resultLen + chunkLen) * 2 + 1;
6,036✔
1547
        char *tmp = taosMemoryRealloc(result, resultCap);
6,036✔
1548
        if (NULL == tmp) {
6,036✔
1549
          taosMemoryFree(result);
×
1550
          *ppResult = NULL;
×
1551
          return TSDB_CODE_OUT_OF_MEMORY;
×
1552
        }
1553
        result = tmp;
6,036✔
1554
      }
1555
      (void)memcpy(result + resultLen, chunk, chunkLen);
189,474✔
1556
      resultLen += chunkLen;
189,474✔
1557
    }
1558
  }
1559

1560
  *ppResult = result;
13,986✔
1561
  *pResultLen = resultLen;
13,986✔
1562
  *pResultCap = resultCap;
13,986✔
1563
  return TSDB_CODE_SUCCESS;
13,986✔
1564
}
1565

1566
static int32_t regexReplace(const char *pStr, const char *pPattern, const char *pReplace, char **ppResult) {
15,311✔
1567
  regex_t *regex = NULL;
15,311✔
1568
  int32_t  code = threadGetRegComp(&regex, pPattern);
15,311✔
1569
  if (code != 0) {
15,311✔
1570
    return code;
×
1571
  }
1572

1573
  size_t strLen = strlen(pStr);
15,311✔
1574
  size_t resultCap = strLen + 1;
15,311✔
1575
  size_t resultLen = 0;
15,311✔
1576
  char  *result = taosMemoryMalloc(resultCap);
15,311✔
1577
  if (NULL == result) {
15,311✔
1578
    return TSDB_CODE_OUT_OF_MEMORY;
×
1579
  }
1580

1581
  const int32_t nmatch = 10;
15,311✔
1582
  const char   *cursor = pStr;
15,311✔
1583
  regmatch_t    pmatch[10];
15,311✔
1584
  int32_t       execFlags = 0;
15,311✔
1585

1586
  while (*cursor != '\0') {
29,297✔
1587
    int ret = regexec(regex, cursor, nmatch, pmatch, execFlags);
15,311✔
1588
    if (ret == REG_NOMATCH) {
15,311✔
1589
      break;
1,325✔
1590
    }
1591
    if (ret != 0) {
13,986✔
1592
      taosMemoryFree(result);
×
1593
      return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
×
1594
    }
1595

1596
    size_t prefixLen = (size_t)pmatch[0].rm_so;
13,986✔
1597
    if (resultLen + prefixLen >= resultCap) {
13,986✔
1598
      resultCap = (resultLen + prefixLen) * 2 + 1;
×
1599
      char *tmp = taosMemoryRealloc(result, resultCap);
×
1600
      if (NULL == tmp) {
×
1601
        taosMemoryFree(result);
×
1602
        return TSDB_CODE_OUT_OF_MEMORY;
×
1603
      }
1604
      result = tmp;
×
1605
    }
1606
    (void)memcpy(result + resultLen, cursor, prefixLen);
13,986✔
1607
    resultLen += prefixLen;
13,986✔
1608

1609
    code = regexReplaceBuildSubstitution(pReplace, pmatch, nmatch, cursor, &result, &resultLen, &resultCap);
13,986✔
1610
    if (code != TSDB_CODE_SUCCESS) {
13,986✔
1611
      return code;
×
1612
    }
1613

1614
    if (pmatch[0].rm_so == pmatch[0].rm_eo) {
13,986✔
1615
      if (resultLen + 1 >= resultCap) {
×
1616
        resultCap = resultCap * 2 + 1;
×
1617
        char *tmp = taosMemoryRealloc(result, resultCap);
×
1618
        if (NULL == tmp) {
×
1619
          taosMemoryFree(result);
×
1620
          return TSDB_CODE_OUT_OF_MEMORY;
×
1621
        }
1622
        result = tmp;
×
1623
      }
1624
      result[resultLen++] = cursor[pmatch[0].rm_eo];
×
1625
      cursor += pmatch[0].rm_eo + 1;
×
1626
    } else {
1627
      cursor += pmatch[0].rm_eo;
13,986✔
1628
    }
1629

1630
    execFlags = REG_NOTBOL;
13,986✔
1631
  }
1632

1633
  size_t tailLen = strlen(cursor);
15,311✔
1634
  if (resultLen + tailLen + 1 > resultCap) {
15,311✔
1635
    resultCap = resultLen + tailLen + 1;
×
1636
    char *tmp = taosMemoryRealloc(result, resultCap);
×
1637
    if (NULL == tmp) {
×
1638
      taosMemoryFree(result);
×
1639
      return terrno;
×
1640
    }
1641
    result = tmp;
×
1642
  }
1643
  (void)memcpy(result + resultLen, cursor, tailLen);
15,311✔
1644
  resultLen += tailLen;
15,311✔
1645
  result[resultLen] = '\0';
15,311✔
1646

1647
  *ppResult = result;
15,311✔
1648
  return TSDB_CODE_SUCCESS;
15,311✔
1649
}
1650

1651

1652

1653
static int32_t computeNewTagValViaRegexReplace(const STag* pOldTag, SUpdatedTagVal* pNewVal) {
19,286✔
1654
  STagVal oldTagVal = {.cid = pNewVal->colId};
19,286✔
1655
  if (!tTagGet(pOldTag, &oldTagVal)) {
19,286✔
1656
    pNewVal->isNull = 1;
3,975✔
1657
    pNewVal->pTagVal = NULL;
3,975✔
1658
    pNewVal->nTagVal = 0;
3,975✔
1659
    return TSDB_CODE_SUCCESS;
3,975✔
1660
  }
1661

1662
  bool isNchar = (pNewVal->tagType == TSDB_DATA_TYPE_NCHAR);
15,311✔
1663
  char* oldStr = NULL;
15,311✔
1664

1665
  if (isNchar) {
15,311✔
1666
    // NCHAR is stored as UCS-4, convert to MBS (VARCHAR) for regex processing
1667
    int32_t mbsLen = oldTagVal.nData + 1;
3,975✔
1668
    oldStr = taosMemoryMalloc(mbsLen);
3,975✔
1669
    if (oldStr == NULL) {
3,975✔
1670
      return TSDB_CODE_OUT_OF_MEMORY;
×
1671
    }
1672
    int32_t ret = taosUcs4ToMbs((TdUcs4*)oldTagVal.pData, oldTagVal.nData, oldStr, NULL);
3,975✔
1673
    if (ret < 0) {
3,975✔
1674
      taosMemoryFree(oldStr);
×
1675
      return ret;
×
1676
    }
1677
    oldStr[ret] = '\0';
3,975✔
1678
  } else {
1679
    // VARCHAR: build null-terminated string from old tag value
1680
    oldStr = taosMemoryMalloc(oldTagVal.nData + 1);
11,336✔
1681
    if (oldStr == NULL) {
11,336✔
1682
      return TSDB_CODE_OUT_OF_MEMORY;
×
1683
    }
1684
    memcpy(oldStr, oldTagVal.pData, oldTagVal.nData);
11,336✔
1685
    oldStr[oldTagVal.nData] = '\0';
11,336✔
1686
  }
1687

1688
  char* newStr = NULL;
15,311✔
1689
  int32_t code = regexReplace(oldStr, pNewVal->regexp, pNewVal->replacement, &newStr);
15,311✔
1690
  taosMemoryFree(oldStr);
15,311✔
1691
  if (code != TSDB_CODE_SUCCESS) {
15,311✔
1692
    return code;
×
1693
  }
1694

1695
  if (isNchar) {
15,311✔
1696
    // Convert regex result back from MBS to UCS-4 (NCHAR)
1697
    int32_t newStrLen = (int32_t)strlen(newStr);
3,975✔
1698
    int32_t ucs4BufLen = (newStrLen + 1) * TSDB_NCHAR_SIZE;
3,975✔
1699
    char*   ucs4Buf = taosMemoryMalloc(ucs4BufLen);
3,975✔
1700
    if (ucs4Buf == NULL) {
3,975✔
1701
      taosMemoryFree(newStr);
×
1702
      return TSDB_CODE_OUT_OF_MEMORY;
×
1703
    }
1704

1705
    int32_t ucs4Len = 0;
3,975✔
1706
    bool    cvtOk = taosMbsToUcs4(newStr, newStrLen, (TdUcs4*)ucs4Buf, ucs4BufLen, &ucs4Len, NULL);
3,975✔
1707
    taosMemoryFree(newStr);
3,975✔
1708
    if (!cvtOk) {
3,975✔
1709
      taosMemoryFree(ucs4Buf);
×
1710
      return terrno;
×
1711
    }
1712

1713
    pNewVal->isNull = 0;
3,975✔
1714
    pNewVal->pTagVal = (uint8_t*)ucs4Buf;
3,975✔
1715
    pNewVal->nTagVal = (uint32_t)ucs4Len;
3,975✔
1716
  } else {
1717
    pNewVal->isNull = 0;
11,336✔
1718
    pNewVal->pTagVal = (uint8_t*)newStr;
11,336✔
1719
    pNewVal->nTagVal = (uint32_t)strlen(newStr);
11,336✔
1720
  }
1721

1722
  return TSDB_CODE_SUCCESS;
15,311✔
1723
}
1724

1725

1726

1727
static int32_t metaUpdateTableTagValueImpl(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
8,401,227✔
1728
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8,401,227✔
1729
    return metaUpdateTableJsonTagValue(pMeta, pTable, pTagSchema, pUpdatedTagVals);
125,269✔
1730
  }
1731
  
1732
  int32_t   code = TSDB_CODE_SUCCESS, lino = 0;
8,275,958✔
1733
  SHashObj* pNewTagVals = pUpdatedTagVals;
8,275,958✔
1734
  SArray*   pRegexResults = NULL;
8,275,958✔
1735

1736
  void* pIter = taosHashIterate(pUpdatedTagVals, NULL);
8,275,958✔
1737
  while (pIter) {
16,600,203✔
1738
    SUpdatedTagVal* pVal = (SUpdatedTagVal*)pIter;
8,335,581✔
1739
    if (pVal->regexp != NULL) {
8,335,581✔
1740
      break;
11,336✔
1741
    }
1742
    pIter = taosHashIterate(pUpdatedTagVals, pIter);
8,324,245✔
1743
  }
1744

1745
  // if there are regular expressions, compute new tag values and store in a new hash map to avoid
1746
  // modifying the original tag values which may be reused for computing other tag values
1747
  if (pIter != NULL) {
8,275,958✔
1748
    taosHashCancelIterate(pUpdatedTagVals, pIter);
11,336✔
1749
    pIter = NULL;
11,336✔
1750
    
1751
    int32_t sz = taosHashGetSize(pUpdatedTagVals);
11,336✔
1752
    pNewTagVals = taosHashInit(sz, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
11,336✔
1753
    if (pNewTagVals == NULL) {
11,336✔
1754
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1755
    }
1756

1757
    pRegexResults = taosArrayInit(sz, sizeof(char*));
11,336✔
1758
    if (pRegexResults == NULL) {
11,336✔
1759
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1760
    }
1761

1762
    pIter = taosHashIterate(pUpdatedTagVals, NULL);
11,336✔
1763
    while (pIter) {
30,622✔
1764
      int32_t       colId = *(int32_t*)taosHashGetKey(pIter, NULL);
19,286✔
1765
      SUpdatedTagVal newVal = *(SUpdatedTagVal *)pIter;
19,286✔
1766
      pIter = taosHashIterate(pUpdatedTagVals, pIter);
19,286✔
1767

1768
      if (newVal.regexp != NULL) {
19,286✔
1769
        const STag* pOldTag = (const STag*)pTable->ctbEntry.pTags;
19,286✔
1770
        TAOS_CHECK_GOTO(computeNewTagValViaRegexReplace(pOldTag, &newVal), &lino, _exit);
19,286✔
1771

1772
        newVal.regexp = NULL;
19,286✔
1773
        newVal.replacement = NULL;
19,286✔
1774
        if (taosArrayPush(pRegexResults, &newVal.pTagVal) == NULL) {
19,286✔
1775
          TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1776
        }
1777
      }
1778

1779
      TAOS_CHECK_GOTO(taosHashPut(pNewTagVals, &colId, sizeof(colId), &newVal, sizeof(newVal)), &lino, _exit);
19,286✔
1780
    }
1781
  }
1782

1783
  TAOS_CHECK_GOTO(metaUpdateTableNormalTagValue(pMeta, pTable, pTagSchema, pNewTagVals), &lino, _exit);
8,275,958✔
1784
  TAOS_CHECK_GOTO(metaHandleEntry2(pMeta, pTable), &lino, _exit);
8,122,397✔
1785

1786
_exit:
8,275,958✔
1787
  if (code) {
8,275,258✔
1788
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
153,561✔
1789
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code), pTable->uid, pTable->name, pTable->version);
153,561✔
1790
  } else {
1791
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
8,121,697✔
1792
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
8,121,697✔
1793
  }
1794

1795
  if (pRegexResults != NULL) {
8,275,958✔
1796
    for (int32_t i = 0; i < taosArrayGetSize(pRegexResults); i++) {
30,622✔
1797
      char** pp = taosArrayGet(pRegexResults, i);
19,286✔
1798
      taosMemoryFree(*pp);
19,286✔
1799
    }
1800
    taosArrayDestroy(pRegexResults);
11,336✔
1801
  }
1802

1803
  if (pNewTagVals != pUpdatedTagVals) {
8,275,958✔
1804
    taosHashCleanup(pNewTagVals);
11,336✔
1805
  }
1806

1807
  if (pIter != NULL) {
8,275,958✔
1808
    taosHashCancelIterate(pUpdatedTagVals, pIter);
×
1809
  }
1810

1811
  TAOS_RETURN(code);
8,275,958✔
1812
}
1813

1814

1815

1816
static int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, const char* tbName, SArray* tags) {
8,110,169✔
1817
  int32_t code = TSDB_CODE_SUCCESS;
8,110,169✔
1818
  SMetaEntry *pChild = NULL;
8,110,169✔
1819
  SMetaEntry *pSuper = NULL;
8,110,169✔
1820
  SHashObj* pUpdatedTagVals = NULL;
8,110,169✔
1821

1822
  // fetch child entry
1823
  code = metaFetchEntryByName(pMeta, tbName, &pChild);
8,110,169✔
1824
  if (code) {
8,110,169✔
1825
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64;
×
1826
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tbName, version);
×
1827
    goto _exit;
×
1828
  }
1829

1830
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
8,110,169✔
1831
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64;
×
1832
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tbName, version);
×
1833
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1834
    goto _exit;
×
1835
  }
1836

1837
  // fetch super entry
1838
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
8,110,169✔
1839
  if (code) {
8,110,169✔
1840
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64;
×
1841
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
×
1842
    code = TSDB_CODE_INTERNAL_ERROR;
×
1843
    goto _exit;
×
1844
  }
1845

1846
  // search the tags to update
1847
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
8,110,169✔
1848

1849
  code = updatedTagValueArrayToHashMap(pTagSchema, tags, &pUpdatedTagVals);
8,110,169✔
1850
  if (code) {
8,110,169✔
1851
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1852
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1853
    goto _exit;
×
1854
  }
1855

1856
  // change tag values
1857
  pChild->version = version;
8,110,169✔
1858
  code = metaUpdateTableTagValueImpl(pMeta, pChild, pTagSchema, pUpdatedTagVals);
8,110,169✔
1859
  if (code) {
8,110,169✔
1860
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, name:%s version:%" PRId64;
146,936✔
1861
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), tbName, version);
146,936✔
1862
    goto _exit;
146,936✔
1863
  }
1864

1865
_exit:
8,110,169✔
1866
  taosHashCleanup(pUpdatedTagVals);
8,110,169✔
1867
  metaFetchEntryFree(&pSuper);
8,109,469✔
1868
  metaFetchEntryFree(&pChild);
8,109,469✔
1869
  TAOS_RETURN(code);
8,109,469✔
1870
}
1871

1872

1873

1874
int32_t metaUpdateTableMultiTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,098,797✔
1875
  int32_t code = TSDB_CODE_SUCCESS;
8,098,797✔
1876
  SArray* uidList = NULL;
8,098,797✔
1877
  SArray* tagListArray = NULL;
8,098,797✔
1878

1879
  // Pre-allocate uidList for batch notification
1880
  int32_t nTables = taosArrayGetSize(pReq->tables);
8,098,797✔
1881
  uidList = taosArrayInit(nTables, sizeof(tb_uid_t));
8,098,797✔
1882
  if (uidList == NULL) {
8,098,797✔
1883
    code = terrno;
×
1884
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1885
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1886
    TAOS_RETURN(code);
×
1887
  }
1888
  tagListArray = taosArrayInit(nTables, sizeof(void*));
8,098,797✔
1889
  if (tagListArray == NULL) {
8,098,797✔
1890
    code = terrno;
×
1891
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1892
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1893
    TAOS_RETURN(code);
×
1894
  }
1895

1896
  for (int32_t i = 0; i < nTables; i++) {
16,208,966✔
1897
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
8,110,169✔
1898
    code = metaUpdateTableTagValue(pMeta, version, pTable->tbName, pTable->tags);
8,110,169✔
1899
    if (code == TSDB_CODE_VND_SAME_TAG) {
8,109,469✔
1900
      // we are updating multiple tables, if one table has same tag,
1901
      // just skip it and continue to update other tables,
1902
      // and return success at the end
1903
      code = TSDB_CODE_SUCCESS;
146,936✔
1904
    } else if (code) {
7,962,533✔
1905
      break;
×
1906
    } else {
1907
      // Collect UID for batch notification
1908
      int64_t uid = metaGetTableEntryUidByName(pMeta, pTable->tbName);
7,962,533✔
1909
      if (uid == 0) {
7,963,233✔
1910
        metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64,
×
1911
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pTable->tbName, version);
1912
        continue;
×
1913
      }
1914
      if (taosArrayPush(uidList, &uid) == NULL){
7,963,233✔
1915
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64,
×
1916
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), version);
1917
        continue;
×
1918
      }
1919
      if (taosArrayPush(tagListArray, &pTable->tags) == NULL){
15,926,466✔
1920
        void* ret = taosArrayPop(uidList);  // make sure the size of uidList and tagListArray are same
×
1921
        metaError("vgId:%d, %s failed at %s:%d since %s, ret:%p, version:%" PRId64,
×
1922
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), ret, version);
1923
        continue;
×
1924
      }
1925
    }
1926
  }
1927

1928
  if (taosArrayGetSize(uidList) > 0) {
8,098,797✔
1929
    vnodeAlterTagForTmq(pMeta->pVnode, uidList, NULL, tagListArray);
7,951,861✔
1930
  }
1931

1932
  taosArrayDestroy(uidList);
8,098,797✔
1933
  taosArrayDestroy(tagListArray);
8,098,797✔
1934
  DestoryThreadLocalRegComp();
8,098,797✔
1935

1936
  if (code) {
8,098,797✔
1937
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1938
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1939
  }
1940

1941
  TAOS_RETURN(code);
8,098,797✔
1942
}
1943

1944

1945
// Context for translating tag column/tbname references to tag values/table name
1946
typedef struct STagToValueCtx {
1947
  int32_t     code;
1948
  const void *pTags;  // STag* blob of the child table
1949
  const char *pName;  // table name of the child table
1950
} STagToValueCtx;
1951

1952
// Translate a tag column reference to the corresponding tag value
1953
static EDealRes tagToValue(SNode **pNode, void *pContext) {
1,700,964✔
1954
  STagToValueCtx *pCtx = (STagToValueCtx *)pContext;
1,700,964✔
1955

1956
  bool isTagCol = false, isTbname = false;
1,700,964✔
1957

1958
  if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
1,700,964✔
1959
    SColumnNode *pCol = (SColumnNode *)*pNode;
566,363✔
1960
    if (pCol->colType == COLUMN_TYPE_TBNAME)
566,363✔
1961
      isTbname = true;
132,500✔
1962
    else
1963
      isTagCol = true;
434,563✔
1964
  } else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
1,135,376✔
1965
    SFunctionNode *pFunc = (SFunctionNode *)*pNode;
×
1966
    if (pFunc->funcType == FUNCTION_TYPE_TBNAME)
×
1967
      isTbname = true;
×
1968
  }
1969

1970
  if (isTagCol) {
1,700,964✔
1971
    SColumnNode *pSColumnNode = *(SColumnNode **)pNode;
433,788✔
1972
    SValueNode  *res = NULL;
433,788✔
1973
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
433,788✔
1974
    if (NULL == res) {
435,188✔
1975
      return DEAL_RES_ERROR;
×
1976
    }
1977

1978
    res->translate = true;
435,188✔
1979
    res->node.resType = pSColumnNode->node.resType;
435,188✔
1980

1981
    STagVal tagVal = {0};
435,188✔
1982
    tagVal.cid = pSColumnNode->colId;
434,488✔
1983
    const char *p = metaGetTableTagVal(pCtx->pTags, pSColumnNode->node.resType.type, &tagVal);
434,488✔
1984
    if (p == NULL) {
433,088✔
1985
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
1986
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
433,088✔
1987
      int32_t len = ((const STag *)p)->len;
×
1988
      res->datum.p = taosMemoryCalloc(len + 1, 1);
×
1989
      if (NULL == res->datum.p) {
×
1990
        pCtx->code = terrno;
×
1991
        return DEAL_RES_ERROR;
×
1992
      }
1993
      memcpy(res->datum.p, p, len);
×
1994
    } else if (IS_VAR_DATA_TYPE(pSColumnNode->node.resType.type)) {
434,488✔
1995
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
2,576✔
1996
      if (NULL == res->datum.p) {
2,944✔
1997
        pCtx->code = terrno;
×
1998
        return DEAL_RES_ERROR;
×
1999
      }
2000
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
2,944✔
2001
      varDataSetLen(res->datum.p, tagVal.nData);
2,944✔
2002
    } else {
2003
      pCtx->code = nodesSetValueNodeValue(res, &(tagVal.i64));
431,544✔
2004
      if (pCtx->code != TSDB_CODE_SUCCESS) {
431,544✔
2005
        return DEAL_RES_ERROR;
×
2006
      }
2007
    }
2008

2009
    nodesDestroyNode(*pNode);
434,488✔
2010
    *pNode = (SNode *)res;
433,788✔
2011

2012
  } else if (isTbname) {
1,267,176✔
2013
    SValueNode *res = NULL;
132,500✔
2014
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
132,500✔
2015
    if (NULL == res) {
132,500✔
2016
      return DEAL_RES_ERROR;
×
2017
    }
2018

2019
    res->translate = true;
132,500✔
2020
    res->node.resType = ((SExprNode *)(*pNode))->resType;
132,500✔
2021

2022
    int32_t len = strlen(pCtx->pName);
132,500✔
2023
    res->datum.p = taosMemoryCalloc(len + VARSTR_HEADER_SIZE + 1, 1);
132,500✔
2024
    if (NULL == res->datum.p) {
132,500✔
2025
      pCtx->code = terrno;
×
2026
      return DEAL_RES_ERROR;
×
2027
    }
2028
    memcpy(varDataVal(res->datum.p), pCtx->pName, len);
132,500✔
2029
    varDataSetLen(res->datum.p, len);
132,500✔
2030
    nodesDestroyNode(*pNode);
132,500✔
2031
    *pNode = (SNode *)res;
132,500✔
2032
  }
2033

2034
  return DEAL_RES_CONTINUE;
1,701,039✔
2035
}
2036

2037

2038
// Check if a single child table qualifies against the tag condition.
2039
static int32_t metaIsChildTableQualified(SMeta *pMeta, tb_uid_t uid, SNode *pTagCond, bool *pQualified) {
566,988✔
2040
  int32_t     code = TSDB_CODE_SUCCESS;
566,988✔
2041
  SMetaEntry *pEntry = NULL;
566,988✔
2042

2043
  *pQualified = false;
567,688✔
2044

2045
  code = metaFetchEntryByUid(pMeta, uid, &pEntry);
567,688✔
2046
  if (code != TSDB_CODE_SUCCESS || pEntry == NULL) {
567,688✔
2047
    return TSDB_CODE_SUCCESS;
×
2048
  }
2049

2050
  // Clone the condition so we can safely rewrite it
2051
  SNode *pTagCondTmp = NULL;
567,688✔
2052
  code = nodesCloneNode(pTagCond, &pTagCondTmp);
567,688✔
2053
  if (code != TSDB_CODE_SUCCESS) {
566,988✔
2054
    metaFetchEntryFree(&pEntry);
×
2055
    return code;
×
2056
  }
2057

2058
  // Replace tag column and tbname references with concrete values
2059
  STagToValueCtx ctx = {.code = TSDB_CODE_SUCCESS, .pTags = pEntry->ctbEntry.pTags, .pName = pEntry->name};
566,988✔
2060
  nodesRewriteExprPostOrder(&pTagCondTmp, tagToValue, &ctx);
566,988✔
2061
  if (ctx.code != TSDB_CODE_SUCCESS) {
566,988✔
2062
    nodesDestroyNode(pTagCondTmp);
×
2063
    metaFetchEntryFree(&pEntry);
×
2064
    return ctx.code;
×
2065
  }
2066

2067
  // Evaluate the fully-resolved expression to a constant boolean
2068
  SNode *pResult = NULL;
566,988✔
2069
  code = scalarCalculateConstants(pTagCondTmp, &pResult);
567,688✔
2070
  if (code != TSDB_CODE_SUCCESS) {
566,288✔
2071
    nodesDestroyNode(pTagCondTmp);
×
2072
    metaFetchEntryFree(&pEntry);
×
2073
    return code;
×
2074
  }
2075

2076
  if (nodeType(pResult) == QUERY_NODE_VALUE) {
566,288✔
2077
    *pQualified = ((SValueNode *)pResult)->datum.b;
566,988✔
2078
  }
2079

2080
  nodesDestroyNode(pResult);
566,288✔
2081
  metaFetchEntryFree(&pEntry);
566,988✔
2082
  return TSDB_CODE_SUCCESS;
567,063✔
2083
}
2084

2085

2086

2087
static int32_t metaGetChildUidsByTagCond(SMeta *pMeta, tb_uid_t suid, SNode *pTagCond, SNode *pTagIndexCond, SArray *pUidList) {
59,108✔
2088
  int32_t       code = TSDB_CODE_SUCCESS;
59,108✔
2089
  int32_t       lino = 0;
59,108✔
2090
  SVnode       *pVnode = pMeta->pVnode;
59,808✔
2091
  SArray       *pCandidateUids = NULL;
59,108✔
2092

2093
  taosArrayClear(pUidList);
59,108✔
2094

2095
  // Step 1: Try index-accelerated pre-filtering with pTagIndexCond
2096
  if (pTagIndexCond != NULL) {
60,508✔
2097
    pCandidateUids = taosArrayInit(64, sizeof(uint64_t));
×
2098
    if (pCandidateUids == NULL) {
×
2099
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2100
    }
2101

2102
    SIndexMetaArg metaArg = {
×
2103
        .metaEx = pVnode,
2104
        .idx = metaGetIdx(pMeta),
×
2105
        .ivtIdx = metaGetIvtIdx(pMeta),
×
2106
        .suid = suid,
2107
    };
2108

2109
    SMetaDataFilterAPI filterAPI = {
×
2110
        .metaFilterTableIds = metaFilterTableIds,
2111
        .metaFilterCreateTime = metaFilterCreateTime,
2112
        .metaFilterTableName = metaFilterTableName,
2113
        .metaFilterTtl = metaFilterTtl,
2114
    };
2115

2116
    SIdxFltStatus idxStatus = SFLT_NOT_INDEX;
×
2117
    code = doFilterTag(pTagIndexCond, &metaArg, pCandidateUids, &idxStatus, &filterAPI);
×
2118
    if (code != TSDB_CODE_SUCCESS || idxStatus == SFLT_NOT_INDEX) {
×
2119
      // Index filtering failed or not supported, fall back to full scan
2120
      const char* msgFmt = "vgId:%d, index filter not used for suid:%" PRId64 ", status:%d code:%s";
×
2121
      metaDebug(msgFmt, TD_VID(pVnode), suid, idxStatus, tstrerror(code));
×
2122
      taosArrayDestroy(pCandidateUids);
×
2123
      pCandidateUids = NULL;
×
2124
      code = TSDB_CODE_SUCCESS;
×
2125
    }
2126
  }
2127

2128
  // Step 2: If index was not used, enumerate all child table UIDs
2129
  if (pCandidateUids == NULL) {
59,808✔
2130
    pCandidateUids = taosArrayInit(256, sizeof(uint64_t));
59,808✔
2131
    if (pCandidateUids == NULL) {
60,508✔
2132
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2133
    }
2134

2135
    SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
60,508✔
2136
    if (pCur == NULL) {
59,808✔
2137
      TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2138
    }
2139

2140
    while (1) {
699,488✔
2141
      tb_uid_t uid = metaCtbCursorNext(pCur);
759,296✔
2142
      if (uid == 0) {
758,746✔
2143
        break;
60,508✔
2144
      }
2145
      if (taosArrayPush(pCandidateUids, &uid) == NULL) {
698,163✔
2146
        code = terrno;
×
2147
        metaCloseCtbCursor(pCur);
×
2148
        TAOS_CHECK_GOTO(code, &lino, _end);
625✔
2149
      }
2150
    }
2151
    metaCloseCtbCursor(pCur);
60,508✔
2152
  }
2153

2154
  // Step 3: Apply pTagCond to filter the candidate UIDs
2155
  if (pTagCond == NULL) {
59,808✔
2156
    // No tag condition — all candidates qualify
2157
    taosArraySwap(pUidList, pCandidateUids);
5,300✔
2158
  } else {
2159
    // Evaluate pTagCond per child table
2160
    for (int32_t i = 0; i < taosArrayGetSize(pCandidateUids); i++) {
621,496✔
2161
      uint64_t uid = *(uint64_t*)taosArrayGet(pCandidateUids, i);
566,988✔
2162
      bool qualified = false;
567,688✔
2163
      code = metaIsChildTableQualified(pMeta, uid, pTagCond, &qualified);
567,688✔
2164
      if (code != TSDB_CODE_SUCCESS) {
567,063✔
2165
        const char* msgFmt = "vgId:%d, %s failed to evaluate tag cond for uid:%" PRId64 " suid:%" PRId64 " since %s";
×
2166
        metaError(msgFmt, TD_VID(pVnode), __func__, uid, suid, tstrerror(code));
×
2167
        TAOS_CHECK_GOTO(code, &lino, _end);
625✔
2168
      }
2169

2170
      if (qualified && taosArrayPush(pUidList, &uid) == NULL) {
725,546✔
2171
        TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2172
      }
2173
    }
2174
  }
2175

2176
_end:
59,808✔
2177
  taosArrayDestroy(pCandidateUids);
60,508✔
2178
  if (code != TSDB_CODE_SUCCESS) {
60,508✔
2179
    const char* msgFmt = "vgId:%d, %s failed at line %d for suid:%" PRId64 " since %s";
×
2180
    metaError(msgFmt, TD_VID(pVnode), __func__, lino, suid, tstrerror(code));
×
2181
  }
2182
  return code;
60,508✔
2183
}
2184

2185

2186
// Convenience wrapper: partition a raw WHERE condition into tag-related parts,
2187
// then call metaGetChildUidsByTagCond to get the filtered child table UIDs.
2188
int32_t metaGetChildUidsByWhere(SMeta *pMeta, tb_uid_t suid, SNode *pWhere, SArray *pUidList) {
59,808✔
2189
  int32_t code = TSDB_CODE_SUCCESS;
59,808✔
2190
  SNode  *pTagCond = NULL;
59,808✔
2191
  SNode  *pTagIndexCond = NULL;
59,808✔
2192

2193
  if (pWhere == NULL) {
59,808✔
2194
    // No WHERE condition — return all child table UIDs
2195
    return metaGetChildUidsByTagCond(pMeta, suid, NULL, NULL, pUidList);
5,300✔
2196
  }
2197

2198
  // Clone pWhere so the caller's node is not destroyed by filterPartitionCond
2199
  SNode *pWhereCopy = NULL;
54,508✔
2200
  code = nodesCloneNode(pWhere, &pWhereCopy);
54,508✔
2201
  if (code != TSDB_CODE_SUCCESS) {
52,483✔
2202
    return code;
×
2203
  }
2204

2205
  // Partition the WHERE condition
2206
  code = filterPartitionCond(&pWhereCopy, NULL, &pTagIndexCond, &pTagCond, NULL);
52,483✔
2207
  if (code != TSDB_CODE_SUCCESS) {
54,508✔
2208
    goto _cleanup;
×
2209
  }
2210

2211
  // Call the core filtering function
2212
  code = metaGetChildUidsByTagCond(pMeta, suid, pTagCond, pTagIndexCond, pUidList);
54,508✔
2213

2214
_cleanup:
55,208✔
2215
  nodesDestroyNode(pTagCond);
55,208✔
2216
  nodesDestroyNode(pTagIndexCond);
55,208✔
2217
  nodesDestroyNode(pWhereCopy);
55,208✔
2218
  return code;
55,208✔
2219
}
2220

2221

2222

2223
int32_t metaUpdateTableChildTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
60,508✔
2224
  int32_t code = TSDB_CODE_SUCCESS;
60,508✔
2225
  SNode* pWhere = NULL;
60,508✔
2226
  SMetaEntry *pSuper = NULL;
60,508✔
2227
  SArray *pUids = NULL;
60,508✔
2228
  SHashObj *pUpdatedTagVals = NULL;
60,508✔
2229
  SArray *uidListForTmq = NULL;
60,508✔
2230

2231
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
60,508✔
UNCOV
2232
    const char* fmt = "vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64;
×
UNCOV
2233
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
2234
    code = TSDB_CODE_INVALID_MSG;
×
2235
    goto _exit;
×
2236
  }
2237

2238
  if (pReq->whereLen > 0) {
60,508✔
2239
    code = nodesMsgToNode(pReq->where, pReq->whereLen, &pWhere);
55,208✔
2240
    if (code) {
54,508✔
2241
      const char* fmt = "vgId:%d, %s failed at %s:%d since invalid where condition, version:%" PRId64;
×
2242
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
2243
      code = TSDB_CODE_INVALID_MSG;
×
2244
      goto _exit;
×
2245
    }
2246
  }
2247

2248
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pSuper);
59,808✔
2249
  if (code) {
59,108✔
2250
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64;
×
2251
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
2252
    goto _exit;
×
2253
  }
2254

2255
  if (pSuper->type != TSDB_SUPER_TABLE) {
59,108✔
2256
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64;
×
2257
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
2258
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
2259
    goto _exit;
×
2260
  }
2261

2262
  code = updatedTagValueArrayToHashMap(&pSuper->stbEntry.schemaTag, pReq->pMultiTag, &pUpdatedTagVals);
59,108✔
2263
  if (code) {
59,183✔
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
  pUids = taosArrayInit(16, sizeof(int64_t));
59,183✔
2270
  if (pUids == NULL) {
59,108✔
2271
    code = terrno;
×
2272
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2273
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2274
    goto _exit;
×
2275
  }
2276
  code = metaGetChildUidsByWhere(pMeta, pSuper->uid, pWhere, pUids);
59,108✔
2277
  if (code) {
60,508✔
2278
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2279
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2280
    goto _exit;
×
2281
  }
2282

2283
  // Pre-allocate uidList for batch notification
2284
  int32_t nUids = taosArrayGetSize(pUids);
60,508✔
2285
  uidListForTmq = taosArrayInit(nUids, sizeof(tb_uid_t));
60,508✔
2286
  if (uidListForTmq == NULL) {
60,508✔
2287
    code = terrno;
×
2288
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2289
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2290
    goto _exit;
×
2291
  }
2292

2293
  for (int32_t i = 0; i < nUids; i++) {
347,591✔
2294
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(pUids, i);
291,058✔
2295
    SMetaEntry *pChild = NULL;
291,058✔
2296
    code = metaFetchEntryByUid(pMeta, uid, &pChild);
291,058✔
2297
    if (code) {
291,058✔
2298
      const char* fmt = "vgId:%d, %s failed at %s:%d since child table uid %" PRId64 " not found, version:%" PRId64;
×
2299
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, uid, version);
×
2300
      goto _exit;
×
2301
    }
2302

2303
    pChild->version = version;
291,058✔
2304
    code = metaUpdateTableTagValueImpl(pMeta, pChild, &pSuper->stbEntry.schemaTag, pUpdatedTagVals);
291,058✔
2305
    if (code == TSDB_CODE_VND_SAME_TAG) {
291,058✔
2306
      // we are updating multiple tables, if one table has same tag,
2307
      // just skip it and continue to update other tables,
2308
      // and return success at the end
2309
      code = TSDB_CODE_SUCCESS;
2,650✔
2310
    } else if (code) {
288,408✔
2311
      const char* fmt = "vgId:%d, %s failed at %s:%d since %s, child table uid %" PRId64 " name %s, version:%" PRId64;
3,975✔
2312
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), uid, pChild->name, version);
3,975✔
2313
      metaFetchEntryFree(&pChild);
3,975✔
2314
      goto _exit;
3,975✔
2315
    } else {
2316
      // Collect UID for batch notification
2317
      if (taosArrayPush(uidListForTmq, &uid) == NULL) {
284,433✔
2318
        const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2319
        metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, terrstr, version);
×
2320
      }
2321
    }
2322

2323
    metaFetchEntryFree(&pChild);
287,083✔
2324
  }
2325

2326
_exit:
60,508✔
2327
  DestoryThreadLocalRegComp();
60,508✔
2328
  if (taosArrayGetSize(uidListForTmq) > 0) {
60,508✔
2329
    vnodeAlterTagForTmq(pMeta->pVnode, uidListForTmq, pReq->pMultiTag, NULL);
22,672✔
2330
  }
2331
  taosArrayDestroy(pUids);
60,508✔
2332
  taosArrayDestroy(uidListForTmq);
60,508✔
2333
  taosHashCleanup(pUpdatedTagVals);
60,508✔
2334
  metaFetchEntryFree(&pSuper);
60,508✔
2335
  nodesDestroyNode(pWhere);
60,508✔
2336
  TAOS_RETURN(code);
60,508✔
2337
}
2338

2339

2340

2341
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,308✔
2342
  int32_t code = TSDB_CODE_SUCCESS;
23,308✔
2343

2344
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
23,308✔
2345
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2346
              __FILE__, __LINE__, version);
2347
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2348
  }
2349

2350
  return code;
23,308✔
2351
}
2352

2353
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
23,308✔
2354
  int32_t code = 0;
23,308✔
2355

2356
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
23,308✔
2357
  if (code) {
23,308✔
2358
    TAOS_RETURN(code);
×
2359
  }
2360

2361
  // fetch entry
2362
  SMetaEntry *pEntry = NULL;
23,308✔
2363
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,308✔
2364
  if (code) {
23,308✔
2365
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2366
              __FILE__, __LINE__, pReq->tbName, version);
2367
    TAOS_RETURN(code);
×
2368
  }
2369

2370
  // do change the entry
2371
  pEntry->version = version;
23,308✔
2372
  if (pEntry->type == TSDB_CHILD_TABLE) {
23,308✔
2373
    if (pReq->updateTTL) {
12,279✔
2374
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,755✔
2375
    }
2376
    if (pReq->newCommentLen >= 0) {
12,279✔
2377
      char *pNewComment = NULL;
7,524✔
2378
      if (pReq->newCommentLen) {
7,524✔
2379
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,797✔
2380
        if (NULL == pNewComment) {
4,797✔
2381
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2382
                    __LINE__, tstrerror(terrno), version);
2383
          metaFetchEntryFree(&pEntry);
×
2384
          TAOS_RETURN(terrno);
×
2385
        }
2386
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,797✔
2387
      } else {
2388
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,727✔
2389
      }
2390
      pEntry->ctbEntry.comment = pNewComment;
7,524✔
2391
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
7,524✔
2392
    }
2393
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
11,029✔
2394
    if (pReq->updateTTL) {
11,029✔
2395
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,885✔
2396
    }
2397
    if (pReq->newCommentLen >= 0) {
11,029✔
2398
      char *pNewComment = NULL;
6,144✔
2399
      if (pReq->newCommentLen > 0) {
6,144✔
2400
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
3,417✔
2401
        if (NULL == pNewComment) {
3,417✔
2402
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2403
                    __LINE__, tstrerror(terrno), version);
2404
          metaFetchEntryFree(&pEntry);
×
2405
          TAOS_RETURN(terrno);
×
2406
        }
2407
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
3,417✔
2408
      } else {
2409
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,727✔
2410
      }
2411
      pEntry->ntbEntry.comment = pNewComment;
6,144✔
2412
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
6,144✔
2413
    }
2414
  } else {
2415
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2416
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
2417
    metaFetchEntryFree(&pEntry);
×
2418
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2419
  }
2420

2421
  // do handle entry
2422
  code = metaHandleEntry2(pMeta, pEntry);
23,308✔
2423
  if (code) {
23,308✔
2424
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2425
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2426
    metaFetchEntryFree(&pEntry);
×
2427
    TAOS_RETURN(code);
×
2428
  } else {
2429
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
23,308✔
2430
             pEntry->uid, version);
2431
  }
2432

2433
  metaFetchEntryFree(&pEntry);
23,308✔
2434
  TAOS_RETURN(code);
23,308✔
2435
}
2436

2437
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,351✔
2438
  int32_t code = TSDB_CODE_SUCCESS;
6,351✔
2439

2440
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,351✔
2441
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2442
              __FILE__, __LINE__, version);
2443
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2444
  }
2445

2446
  SMetaEntry *pEntry = NULL;
6,351✔
2447
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,351✔
2448
  if (code) {
6,351✔
2449
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2450
              __FILE__, __LINE__, pReq->tbName, version);
2451
    TAOS_RETURN(code);
×
2452
  }
2453

2454
  if (pEntry->version >= version) {
6,351✔
2455
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2456
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2457
    metaFetchEntryFree(&pEntry);
×
2458
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2459
  }
2460

2461
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,351✔
2462
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2463
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
2464
    metaFetchEntryFree(&pEntry);
×
2465
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2466
  }
2467

2468
  // do change the entry
2469
  int8_t           updated = 0;
6,351✔
2470
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,351✔
2471
  for (int32_t i = 0; i < wp->nCols; i++) {
50,808✔
2472
    SColCmpr *p = &wp->pColCmpr[i];
44,457✔
2473
    if (p->id == pReq->colId) {
44,457✔
2474
      uint32_t dst = 0;
6,351✔
2475
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,351✔
2476
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2477
      if (updated > 0) {
6,351✔
2478
        p->alg = dst;
6,351✔
2479
      }
2480
    }
2481
  }
2482

2483
  if (updated == 0) {
6,351✔
2484
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
2485
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
2486
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2487
    metaFetchEntryFree(&pEntry);
×
2488
    TAOS_RETURN(code);
×
2489
  } else if (updated < 0) {
6,351✔
2490
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2491
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
2492
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2493
    metaFetchEntryFree(&pEntry);
×
2494
    TAOS_RETURN(code);
×
2495
  }
2496

2497
  pEntry->version = version;
6,351✔
2498

2499
  // do handle entry
2500
  code = metaHandleEntry2(pMeta, pEntry);
6,351✔
2501
  if (code) {
6,351✔
2502
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2503
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2504
    metaFetchEntryFree(&pEntry);
×
2505
    TAOS_RETURN(code);
×
2506
  } else {
2507
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
6,351✔
2508
             pEntry->uid, version);
2509
  }
2510

2511
  metaFetchEntryFree(&pEntry);
6,351✔
2512
  TAOS_RETURN(code);
6,351✔
2513
}
2514

2515
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
81,573✔
2516
  int32_t code = TSDB_CODE_SUCCESS;
81,573✔
2517

2518
  // check request
2519
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
81,573✔
2520
  if (code) {
81,573✔
2521
    TAOS_RETURN(code);
×
2522
  }
2523

2524
  if (NULL == pReq->refDbName) {
81,573✔
2525
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2526
              __func__, __FILE__, __LINE__, version);
2527
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2528
  }
2529

2530
  if (NULL == pReq->refTbName) {
81,573✔
2531
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2532
              __func__, __FILE__, __LINE__, version);
2533
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2534
  }
2535

2536
  if (NULL == pReq->refColName) {
81,573✔
2537
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2538
              __func__, __FILE__, __LINE__, version);
2539
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2540
  }
2541

2542
  // fetch old entry
2543
  SMetaEntry *pEntry = NULL;
81,573✔
2544
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
81,573✔
2545
  if (code) {
81,573✔
2546
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2547
              __FILE__, __LINE__, pReq->tbName, version);
2548
    TAOS_RETURN(code);
×
2549
  }
2550

2551
  if (pEntry->version >= version) {
81,573✔
2552
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2553
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2554
    metaFetchEntryFree(&pEntry);
×
2555
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2556
  }
2557

2558
  // fetch super entry
2559
  SMetaEntry *pSuper = NULL;
81,573✔
2560
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
81,573✔
2561
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
30,074✔
2562
    if (code) {
30,074✔
2563
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2564
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2565
      metaFetchEntryFree(&pEntry);
×
2566
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2567
    }
2568
  }
2569

2570
  // search the column to update
2571
  SSchemaWrapper *pSchema =
81,573✔
2572
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
81,573✔
2573
  SColRef *pColRef = NULL;
81,573✔
2574
  int32_t  iColumn = 0;
81,573✔
2575
  for (int32_t i = 0; i < pSchema->nCols; i++) {
399,772✔
2576
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
399,772✔
2577
      pColRef = &pEntry->colRef.pColRef[i];
81,573✔
2578
      iColumn = i;
81,573✔
2579
      break;
81,573✔
2580
    }
2581
  }
2582

2583
  if (NULL == pColRef) {
81,573✔
2584
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2585
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
2586
    metaFetchEntryFree(&pEntry);
×
2587
    metaFetchEntryFree(&pSuper);
×
2588
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2589
  }
2590

2591
  // do update column name
2592
  pEntry->version = version;
81,573✔
2593
  pColRef->hasRef = true;
81,573✔
2594
  pColRef->id = pSchema->pSchema[iColumn].colId;
81,573✔
2595
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
81,573✔
2596
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
81,573✔
2597
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
81,573✔
2598
  pSchema->version++;
81,573✔
2599
  pEntry->colRef.version++;
81,573✔
2600

2601
  // do handle entry
2602
  code = metaHandleEntry2(pMeta, pEntry);
81,573✔
2603
  if (code) {
81,573✔
2604
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2605
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2606
    metaFetchEntryFree(&pEntry);
×
2607
    metaFetchEntryFree(&pSuper);
×
2608
    TAOS_RETURN(code);
×
2609
  } else {
2610
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
81,573✔
2611
             pEntry->uid, version);
2612
  }
2613

2614
  // build response
2615
  code = metaUpdateVtbMetaRsp(
244,719✔
2616
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
81,573✔
2617
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->pExtSchemas : pEntry->pExtSchemas,
81,573✔
2618
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
81,573✔
2619
      pEntry->type);
81,573✔
2620
  if (code) {
81,573✔
2621
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2622
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2623
  } else {
2624
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
602,793✔
2625
      SColRef *p = &pEntry->colRef.pColRef[i];
521,220✔
2626
      pRsp->pColRefs[i].hasRef = p->hasRef;
521,220✔
2627
      pRsp->pColRefs[i].id = p->id;
521,220✔
2628
      if (p->hasRef) {
521,220✔
2629
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
358,853✔
2630
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
358,853✔
2631
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
358,853✔
2632
      }
2633
    }
2634
  }
2635

2636
  metaFetchEntryFree(&pEntry);
81,573✔
2637
  metaFetchEntryFree(&pSuper);
81,573✔
2638
  TAOS_RETURN(code);
81,573✔
2639
}
2640

2641
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
60,699✔
2642
  int32_t code = TSDB_CODE_SUCCESS;
60,699✔
2643

2644
  // check request
2645
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
60,699✔
2646
  if (code) {
60,699✔
2647
    TAOS_RETURN(code);
×
2648
  }
2649

2650
  // fetch old entry
2651
  SMetaEntry *pEntry = NULL;
60,699✔
2652
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
60,699✔
2653
  if (code) {
60,699✔
2654
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2655
              __FILE__, __LINE__, pReq->tbName, version);
2656
    TAOS_RETURN(code);
×
2657
  }
2658

2659
  if (pEntry->version >= version) {
60,699✔
2660
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2661
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2662
    metaFetchEntryFree(&pEntry);
×
2663
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2664
  }
2665

2666
  // fetch super entry
2667
  SMetaEntry *pSuper = NULL;
60,699✔
2668
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
60,699✔
2669
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
30,214✔
2670
    if (code) {
30,214✔
2671
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2672
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2673
      metaFetchEntryFree(&pEntry);
×
2674
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2675
    }
2676
  }
2677

2678
  // search the column to update
2679
  SSchemaWrapper *pSchema =
60,699✔
2680
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
60,699✔
2681
  SColRef *pColRef = NULL;
60,699✔
2682
  int32_t  iColumn = 0;
60,699✔
2683
  for (int32_t i = 0; i < pSchema->nCols; i++) {
242,730✔
2684
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
242,730✔
2685
      pColRef = &pEntry->colRef.pColRef[i];
60,699✔
2686
      iColumn = i;
60,699✔
2687
      break;
60,699✔
2688
    }
2689
  }
2690

2691
  if (NULL == pColRef) {
60,699✔
2692
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2693
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2694
    metaFetchEntryFree(&pEntry);
×
2695
    metaFetchEntryFree(&pSuper);
×
2696
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2697
  }
2698

2699
  // do update column name
2700
  pEntry->version = version;
60,699✔
2701
  pColRef->hasRef = false;
60,699✔
2702
  pColRef->id = pSchema->pSchema[iColumn].colId;
60,699✔
2703
  pSchema->version++;
60,699✔
2704
  pEntry->colRef.version++;
60,699✔
2705

2706
  // do handle entry
2707
  code = metaHandleEntry2(pMeta, pEntry);
60,699✔
2708
  if (code) {
60,699✔
2709
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2710
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2711
    metaFetchEntryFree(&pEntry);
×
2712
    metaFetchEntryFree(&pSuper);
×
2713
    TAOS_RETURN(code);
×
2714
  } else {
2715
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
60,699✔
2716
             pEntry->uid, version);
2717
  }
2718

2719
  // build response
2720
  code = metaUpdateVtbMetaRsp(
182,097✔
2721
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
60,699✔
2722
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->pExtSchemas : pEntry->pExtSchemas,
60,699✔
2723
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
60,699✔
2724
      pEntry->type);
60,699✔
2725
  if (code) {
60,699✔
2726
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2727
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2728
  } else {
2729
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
377,613✔
2730
      SColRef *p = &pEntry->colRef.pColRef[i];
316,914✔
2731
      pRsp->pColRefs[i].hasRef = p->hasRef;
316,914✔
2732
      pRsp->pColRefs[i].id = p->id;
316,914✔
2733
      if (p->hasRef) {
316,914✔
2734
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
158,954✔
2735
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
158,954✔
2736
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
158,954✔
2737
      }
2738
    }
2739
  }
2740

2741
  metaFetchEntryFree(&pEntry);
60,699✔
2742
  metaFetchEntryFree(&pSuper);
60,699✔
2743
  TAOS_RETURN(code);
60,699✔
2744
}
2745

2746
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
19,097✔
2747
  int32_t code = TSDB_CODE_SUCCESS;
19,097✔
2748

2749
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
19,097✔
2750
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2751
              __FILE__, __LINE__, version);
2752
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2753
  }
2754

2755
  SMetaEntry *pEntry = NULL;
19,097✔
2756
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
19,097✔
2757
  if (code) {
19,097✔
2758
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2759
              __FILE__, __LINE__, pReq->name, version);
2760
    TAOS_RETURN(code);
×
2761
  }
2762

2763
  if (pEntry->type != TSDB_SUPER_TABLE) {
19,097✔
2764
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2765
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2766
    metaFetchEntryFree(&pEntry);
×
2767
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2768
  }
2769

2770
  if (pEntry->uid != pReq->suid) {
19,097✔
2771
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2772
              ", version:%" PRId64,
2773
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2774
    metaFetchEntryFree(&pEntry);
×
2775
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2776
  }
2777

2778
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2779
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2780
  //   PRId64,
2781
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2782
  //             pReq->schemaTag.version, version);
2783
  //   metaFetchEntryFree(&pEntry);
2784
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2785
  // }
2786

2787
  // do change the entry
2788
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
19,097✔
2789
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
19,097✔
2790
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
19,097✔
2791
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2792
              __func__, __FILE__, __LINE__, pReq->name, version);
2793
    metaFetchEntryFree(&pEntry);
×
2794
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2795
  }
2796

2797
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
19,097✔
2798
    metaError(
×
2799
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2800
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2801
        version);
2802
    metaFetchEntryFree(&pEntry);
×
2803
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2804
  }
2805

2806
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2807
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2808
  //   PRId64,
2809
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2810
  //             pNewTagSchema->version, version);
2811
  //   metaFetchEntryFree(&pEntry);
2812
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2813
  // }
2814

2815
  int32_t numOfChangedTags = 0;
19,097✔
2816
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
201,998✔
2817
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
182,901✔
2818
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
182,901✔
2819

2820
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
182,901✔
2821
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
182,901✔
2822
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2823
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2824
      metaFetchEntryFree(&pEntry);
×
2825
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2826
    }
2827

2828
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
182,901✔
2829
      numOfChangedTags++;
19,097✔
2830
      SSCHMEA_SET_IDX_ON(pOldColumn);
19,097✔
2831
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
163,804✔
2832
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2833
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2834
      metaFetchEntryFree(&pEntry);
×
2835
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2836
    }
2837
  }
2838

2839
  if (numOfChangedTags != 1) {
19,097✔
2840
    metaError(
×
2841
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2842
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2843
    metaFetchEntryFree(&pEntry);
×
2844
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2845
  }
2846

2847
  pEntry->version = version;
19,097✔
2848
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
19,097✔
2849

2850
  // do handle the entry
2851
  code = metaHandleEntry2(pMeta, pEntry);
19,097✔
2852
  if (code) {
19,097✔
2853
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2854
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2855
    metaFetchEntryFree(&pEntry);
×
2856
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2857
  } else {
2858
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
19,097✔
2859
             pEntry->uid, version);
2860
  }
2861

2862
  metaFetchEntryFree(&pEntry);
19,097✔
2863
  TAOS_RETURN(code);
19,097✔
2864
}
2865

2866
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
15,126✔
2867
  int32_t code = TSDB_CODE_SUCCESS;
15,126✔
2868

2869
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
15,126✔
2870
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2871
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2872
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2873
  }
2874

2875
  SMetaEntry *pEntry = NULL;
15,126✔
2876
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
15,126✔
2877
  if (code) {
15,126✔
2878
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2879
              __FILE__, __LINE__, pReq->stb, version);
2880
    TAOS_RETURN(code);
×
2881
  }
2882

2883
  if (TSDB_SUPER_TABLE != pEntry->type) {
15,126✔
2884
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2885
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2886
    metaFetchEntryFree(&pEntry);
×
2887
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2888
  }
2889

2890
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
15,126✔
2891
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
15,126✔
2892
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2893
              __func__, __FILE__, __LINE__, pReq->stb, version);
2894
    metaFetchEntryFree(&pEntry);
×
2895
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2896
  }
2897

2898
  // search and set the tag index off
2899
  int32_t numOfChangedTags = 0;
15,126✔
2900
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
93,882✔
2901
    SSchema *pCol = pTagSchema->pSchema + i;
93,882✔
2902
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
93,882✔
2903
      if (!IS_IDX_ON(pCol)) {
15,126✔
2904
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2905
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2906
        metaFetchEntryFree(&pEntry);
×
2907
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2908
      }
2909
      numOfChangedTags++;
15,126✔
2910
      SSCHMEA_SET_IDX_OFF(pCol);
15,126✔
2911
      break;
15,126✔
2912
    }
2913
  }
2914

2915
  if (numOfChangedTags != 1) {
15,126✔
2916
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2917
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2918
    metaFetchEntryFree(&pEntry);
×
2919
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2920
  }
2921

2922
  // do handle the entry
2923
  pEntry->version = version;
15,126✔
2924
  pTagSchema->version++;
15,126✔
2925
  code = metaHandleEntry2(pMeta, pEntry);
15,126✔
2926
  if (code) {
15,126✔
2927
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2928
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2929
    metaFetchEntryFree(&pEntry);
×
2930
    TAOS_RETURN(code);
×
2931
  } else {
2932
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
15,126✔
2933
             pEntry->uid, version);
2934
  }
2935

2936
  metaFetchEntryFree(&pEntry);
15,126✔
2937
  TAOS_RETURN(code);
15,126✔
2938
}
2939

2940
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,896,630✔
2941
  int32_t code = TSDB_CODE_SUCCESS;
7,896,630✔
2942

2943
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,896,630✔
2944
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
25,591✔
2945
              __FILE__, __LINE__, version);
2946
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
25,591✔
2947
  }
2948

2949
  SMetaEntry *pEntry = NULL;
7,883,118✔
2950
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,894,335✔
2951
  if (code) {
7,896,550✔
2952
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2953
              __FILE__, __LINE__, pReq->name, version);
2954
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2955
  }
2956

2957
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,896,550✔
2958
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2959
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2960
    metaFetchEntryFree(&pEntry);
×
2961
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2962
  }
2963

2964
  SMetaEntry entry = {
23,660,180✔
2965
      .version = version,
2966
      .type = TSDB_SUPER_TABLE,
2967
      .uid = pReq->suid,
7,896,567✔
2968
      .name = pReq->name,
7,885,833✔
2969
      .stbEntry.schemaRow = pReq->schemaRow,
2970
      .stbEntry.schemaTag = pReq->schemaTag,
2971
      .stbEntry.keep = pReq->keep,
7,884,745✔
2972
      .stbEntry.ownerId = pReq->ownerId,
7,876,506✔
2973
      .colCmpr = pReq->colCmpr,
2974
      .pExtSchemas = pReq->pExtSchemas,
7,885,651✔
2975
  };
2976
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,881,427✔
2977
  if (pReq->virtualStb) {
7,881,427✔
2978
    TABLE_SET_VIRTUAL(entry.flags);
23,821✔
2979
  }
2980
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,876,453✔
2981
    TABLE_SET_ROLLUP(entry.flags);
4,578✔
2982
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,578✔
2983
  }
2984

2985
  // do handle the entry
2986
  code = metaHandleEntry2(pMeta, &entry);
7,900,039✔
2987
  if (code) {
7,883,876✔
2988
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2989
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
2990
    metaFetchEntryFree(&pEntry);
×
2991
    TAOS_RETURN(code);
×
2992
  } else {
2993
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,883,876✔
2994
             pReq->suid, version);
2995
  }
2996

2997
  metaFetchEntryFree(&pEntry);
7,907,197✔
2998
  TAOS_RETURN(code);
7,910,301✔
2999
}
3000

3001
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
3002
  int32_t code = 0;
×
3003

3004
  if (taosArrayGetSize(uidArray) == 0) {
×
3005
    return TSDB_CODE_SUCCESS;
×
3006
  }
3007

3008
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
3009
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
3010
    SMetaInfo info;
×
3011
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
3012
    if (code) {
×
3013
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
3014
                __func__, __FILE__, __LINE__, uid, code);
3015
      return code;
×
3016
    }
3017

3018
    SMetaEntry entry = {
×
3019
        .version = version,
3020
        .uid = uid,
3021
    };
3022

3023
    if (info.suid == 0) {
×
3024
      entry.type = -TSDB_NORMAL_TABLE;
×
3025
    } else if (info.suid == uid) {
×
3026
      entry.type = -TSDB_SUPER_TABLE;
×
3027
    } else {
3028
      entry.type = -TSDB_CHILD_TABLE;
×
3029
    }
3030
    code = metaHandleEntry2(pMeta, &entry);
×
3031
    if (code) {
×
3032
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
3033
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
3034
      return code;
×
3035
    }
3036
  }
3037
  return code;
×
3038
}
3039

3040
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
40,480✔
3041
  int32_t code = TSDB_CODE_SUCCESS;
40,480✔
3042

3043
  if (NULL == pReq->name || pReq->name[0] == 0) {
40,480✔
3044
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3045
              __LINE__, version);
3046
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3047
  }
3048

3049
  SMetaEntry *pEntry = NULL;
40,480✔
3050
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
40,480✔
3051
  if (code) {
40,480✔
3052
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
3053
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3054
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
3055
  }
3056

3057
  if (pEntry->type != TSDB_SUPER_TABLE) {
40,480✔
3058
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
3059
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
3060
    metaFetchEntryFree(&pEntry);
×
3061
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3062
  }
3063

3064
  if (pEntry->uid != pReq->tbUid) {
40,480✔
3065
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3066
              ", version:%" PRId64,
3067
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3068
    metaFetchEntryFree(&pEntry);
×
3069
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3070
  }
3071

3072
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
40,480✔
3073
    // overwrite the old rsma definition if exists
3074
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
3075
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
3076
  } else {
3077
    TABLE_SET_ROLLUP(pEntry->flags);
40,480✔
3078
  }
3079

3080
  SMetaEntry entry = *pEntry;
40,480✔
3081
  entry.version = version;
40,480✔
3082
  entry.stbEntry.rsmaParam.name = pReq->name;
40,480✔
3083
  entry.stbEntry.rsmaParam.uid = pReq->uid;
39,717✔
3084
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
39,717✔
3085
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
40,480✔
3086
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
40,480✔
3087
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
40,480✔
3088
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
39,717✔
3089
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
40,480✔
3090

3091
  // do handle the entry
3092
  code = metaHandleEntry2(pMeta, &entry);
40,480✔
3093
  if (code) {
40,480✔
3094
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3095
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
3096
    metaFetchEntryFree(&pEntry);
×
3097
    TAOS_RETURN(code);
×
3098
  } else {
3099
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
40,480✔
3100
    pMeta->pVnode->config.isRsma = 1;
40,480✔
3101
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
40,480✔
3102
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3103
  }
3104

3105
  metaFetchEntryFree(&pEntry);
40,480✔
3106
  TAOS_RETURN(code);
40,480✔
3107
}
3108

3109
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
20,374✔
3110
  int32_t code = TSDB_CODE_SUCCESS;
20,374✔
3111

3112
  if (NULL == pReq->name || pReq->name[0] == 0) {
20,374✔
3113
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3114
              __LINE__, version);
3115
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3116
  }
3117

3118
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
20,374✔
3119
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3120
              __LINE__, version);
3121
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3122
  }
3123

3124
  SMetaEntry *pEntry = NULL;
20,374✔
3125
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
20,374✔
3126
  if (code) {
20,374✔
3127
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3128
             __func__, __LINE__, pReq->name, pReq->tbName, version);
3129
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3130
  }
3131

3132
  if (pEntry->type != pReq->tbType) {
20,374✔
3133
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
3134
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
3135
    metaFetchEntryFree(&pEntry);
×
3136
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3137
  }
3138

3139
  if (pEntry->uid != pReq->tbUid) {
20,374✔
3140
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3141
              ", version:%" PRId64,
3142
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3143
    metaFetchEntryFree(&pEntry);
×
3144
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3145
  }
3146

3147
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
20,374✔
3148
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
20,374✔
3149
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
20,374✔
3150
      metaError(
×
3151
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
3152
          "uid:%" PRIi64 ", version:%" PRId64,
3153
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
3154
          pReq->uid, version);
3155
      metaFetchEntryFree(&pEntry);
×
3156
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3157
    }
3158
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
20,374✔
3159
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
20,374✔
3160
  } else {
3161
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
3162
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
3163
    metaFetchEntryFree(&pEntry);
×
3164
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3165
  }
3166

3167
  SMetaEntry entry = *pEntry;
20,374✔
3168
  entry.version = version;
20,374✔
3169
  TABLE_RESET_ROLLUP(entry.flags);
20,374✔
3170
  entry.stbEntry.rsmaParam.uid = 0;
20,374✔
3171
  entry.stbEntry.rsmaParam.name = NULL;
20,374✔
3172
  entry.stbEntry.rsmaParam.nFuncs = 0;
20,374✔
3173
  entry.stbEntry.rsmaParam.funcColIds = NULL;
20,374✔
3174
  entry.stbEntry.rsmaParam.funcIds = NULL;
20,374✔
3175

3176
  // do handle the entry
3177
  code = metaHandleEntry2(pMeta, &entry);
20,374✔
3178
  if (code) {
20,374✔
3179
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
3180
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
3181
    metaFetchEntryFree(&pEntry);
×
3182
    TAOS_RETURN(code);
×
3183
  } else {
3184
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
20,374✔
3185
      pMeta->pVnode->config.isRsma = 0;
×
3186
    }
3187
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
20,374✔
3188
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3189
  }
3190

3191
  metaFetchEntryFree(&pEntry);
20,374✔
3192
  TAOS_RETURN(code);
20,374✔
3193
}
3194

3195
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,476✔
3196
  int32_t code = TSDB_CODE_SUCCESS;
12,476✔
3197

3198
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,476✔
3199
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3200
              __LINE__, version);
3201
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3202
  }
3203

3204
  SMetaEntry *pEntry = NULL;
12,476✔
3205
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
12,476✔
3206
  if (code) {
12,476✔
3207
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
3208
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3209
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
3210
  }
3211

3212
  if (pEntry->uid != pReq->tbUid) {
12,476✔
3213
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3214
              ", version:%" PRId64,
3215
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3216
    metaFetchEntryFree(&pEntry);
×
3217
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3218
  }
3219

3220
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
12,476✔
3221
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
12,476✔
3222
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,476✔
3223
  } else {
3224
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
3225
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3226
    metaFetchEntryFree(&pEntry);
×
3227
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3228
  }
3229

3230
  SMetaEntry entry = *pEntry;
12,476✔
3231
  entry.version = version;
12,476✔
3232
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
12,476✔
3233
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
12,476✔
3234
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
12,476✔
3235
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
12,476✔
3236
  }
3237
  // do handle the entry
3238
  code = metaHandleEntry2(pMeta, &entry);
12,476✔
3239
  if (code) {
12,476✔
3240
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3241
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
3242
    metaFetchEntryFree(&pEntry);
×
3243
    TAOS_RETURN(code);
×
3244
  } else {
3245
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
12,476✔
3246
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3247
  }
3248

3249
  metaFetchEntryFree(&pEntry);
12,476✔
3250
  TAOS_RETURN(code);
12,476✔
3251
}
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