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

taosdata / TDengine / #5020

10 Apr 2026 06:58AM UTC coverage: 72.28% (+0.03%) from 72.254%
#5020

push

travis-ci

web-flow
merge: from main to 3.0 #35103

77 of 116 new or added lines in 3 files covered. (66.38%)

2742 existing lines in 124 files now uncovered.

257507 of 356263 relevant lines covered (72.28%)

135195138.8 hits per line

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

69.3
/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,554,926✔
38
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,554,926✔
39
  void     *value = NULL;
5,561,373✔
40
  int32_t   valueSize = 0;
5,562,388✔
41
  SMetaInfo info;
5,551,931✔
42

43
  // check name
44
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
5,559,703✔
45
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
13,763✔
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,509,274✔
51
  if (r == 0) {  // name exists, check uid and type
5,529,354✔
52
    int64_t uid = *(tb_uid_t *)value;
2,629✔
53
    tdbFree(value);
2,629✔
54

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

62
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
337✔
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) {
337✔
70
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
337✔
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,526,725✔
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,539,679✔
87
}
88

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

95
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,623,344✔
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,623,344✔
102
  if (TSDB_CODE_SUCCESS != code) {
1,623,344✔
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,623,344✔
113
  tdbFreeClear(value);
1,623,344✔
114

115
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
1,623,344✔
116
  if (TSDB_CODE_SUCCESS != code) {
1,623,344✔
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,623,344✔
124

125
  return code;
1,623,344✔
126
}
127

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

134
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
938,803✔
135
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
9,658✔
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);
929,276✔
141
  if (code) {
938,262✔
142
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
764✔
143
              __FILE__, __LINE__, pReq->name, version);
144
    return TSDB_CODE_TDB_STB_NOT_EXIST;
764✔
145
  } else {
146
    int64_t uid = *(int64_t *)value;
937,498✔
147
    tdbFreeClear(value);
938,098✔
148

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

156
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
931,496✔
157
  if (code) {
935,315✔
UNCOV
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) {
935,315✔
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;
935,315✔
169
}
170

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

175
  // check request
176
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,515,103✔
177
  if (code != TSDB_CODE_SUCCESS) {
5,542,147✔
178
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
2,629✔
179
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
337✔
180
               pReq->name, pReq->suid, version);
181
      TAOS_RETURN(TSDB_CODE_SUCCESS);
337✔
182
    } else {
183
      TAOS_RETURN(code);
2,292✔
184
    }
185
  }
186

187
  // handle entry
188
  SMetaEntry entry = {
11,053,389✔
189
      .version = version,
190
      .type = TSDB_SUPER_TABLE,
191
      .uid = pReq->suid,
5,537,169✔
192
      .name = pReq->name,
5,527,474✔
193
      .stbEntry.schemaRow = pReq->schemaRow,
194
      .stbEntry.schemaTag = pReq->schemaTag,
195
      .stbEntry.keep = pReq->keep,
5,527,869✔
196
      .stbEntry.ownerId = pReq->ownerId,
5,504,379✔
197
  };
198
  if (pReq->rollup) {
5,516,826✔
199
    TABLE_SET_ROLLUP(entry.flags);
×
200
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
201
  }
202
  if (pReq->colCmpred) {
5,488,143✔
203
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,517,182✔
204
    entry.colCmpr = pReq->colCmpr;
5,517,182✔
205
  }
206

207
  entry.pExtSchemas = pReq->pExtSchemas;
5,496,418✔
208

209
  if (pReq->virtualStb) {
5,525,114✔
210
    TABLE_SET_VIRTUAL(entry.flags);
121,240✔
211
  }
212

213
  code = metaHandleEntry2(pMeta, &entry);
5,510,942✔
214
  if (TSDB_CODE_SUCCESS == code) {
5,567,748✔
215
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
5,568,350✔
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,567,475✔
222
}
223

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

228
  // check request
229
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
938,428✔
230
  if (code) {
931,738✔
231
    TAOS_RETURN(code);
1,528✔
232
  }
233

234
  // handle entry
235
  SMetaEntry entry = {
930,210✔
236
      .version = verison,
237
      .type = -TSDB_SUPER_TABLE,
238
      .uid = pReq->suid,
930,897✔
239
  };
240
  code = metaHandleEntry2(pMeta, &entry);
933,365✔
241
  if (code) {
939,144✔
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,
939,144✔
246
             pReq->suid, verison);
247
  }
248
  TAOS_RETURN(code);
939,144✔
249
}
250

251
// Alter Super Table
252

253
// Create Child Table
254
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
64,597,781✔
255
  int32_t   code = TSDB_CODE_SUCCESS;
64,597,781✔
256
  void     *value = NULL;
64,597,781✔
257
  int32_t   valueSize = 0;
64,599,008✔
258
  SMetaInfo info;
64,594,652✔
259

260
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
64,594,115✔
261
      pReq->ctb.suid == 0) {
64,598,385✔
UNCOV
262
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
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) {
64,585,578✔
269
    pReq->uid = *(int64_t *)value;
330,008✔
270
    tdbFreeClear(value);
330,008✔
271

272
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
329,938✔
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) {
329,973✔
281
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
1,840✔
282
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
283
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
1,840✔
284
    }
285

286
    // check suid
287
    if (info.suid != pReq->ctb.suid) {
328,133✔
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;
328,063✔
296
  }
297

298
  // check super table existence
299
  SMetaEntry *pStbEntry = NULL;
64,267,567✔
300
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
64,266,000✔
301
  if (code) {
64,255,141✔
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) {
64,255,141✔
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) {
64,250,238✔
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;
64,241,495✔
325
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
64,258,942✔
326
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
64,254,344✔
327
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
282,093,746✔
328
      STagVal tagVal = {
218,144,698✔
329
          .cid = pTagSchema->pSchema[i].colId,
218,120,319✔
330
      };
331

332
      if (tTagGet(pTag, &tagVal)) {
218,117,100✔
333
        if (pTagSchema->pSchema[i].type != tagVal.type) {
174,678,629✔
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);
64,218,667✔
344

345
  // check grant
346
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
64,224,129✔
347
    code = grantCheck(TSDB_GRANT_TIMESERIES);
64,254,092✔
348
    if (TSDB_CODE_SUCCESS != code) {
64,211,968✔
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;
64,217,672✔
354
}
355

356
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
63,851,001✔
357
  int32_t code = TSDB_CODE_SUCCESS;
63,851,001✔
358

359
  if (NULL == ppRsp) {
63,851,001✔
360
    return code;
×
361
  }
362

363
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
63,851,001✔
364
  if (NULL == ppRsp) {
63,835,018✔
365
    return terrno;
×
366
  }
367

368
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
63,835,018✔
369
  (*ppRsp)->tuid = pEntry->uid;
63,843,285✔
370
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
63,863,106✔
371
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
63,867,729✔
372

373
  return code;
63,885,636✔
374
}
375

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

379
  // check request
380
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
64,241,718✔
381
  if (code) {
64,203,801✔
382
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
329,938✔
383
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,840✔
384
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
385
    }
386
    return code;
329,938✔
387
  }
388

389
  SMetaEntry entry = {
63,873,863✔
390
      .version = version,
391
      .type = TSDB_CHILD_TABLE,
392
      .uid = pReq->uid,
63,876,186✔
393
      .name = pReq->name,
63,879,265✔
394
      .ctbEntry.btime = pReq->btime,
63,882,692✔
395
      .ctbEntry.ttlDays = pReq->ttl,
63,869,326✔
396
      .ctbEntry.commentLen = pReq->commentLen,
63,842,196✔
397
      .ctbEntry.comment = pReq->comment,
63,856,775✔
398
      .ctbEntry.suid = pReq->ctb.suid,
63,861,364✔
399
      .ctbEntry.pTags = pReq->ctb.pTag,
63,847,782✔
400
  };
401

402
  // build response
403
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
63,843,276✔
404
  if (code) {
63,874,726✔
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);
63,874,726✔
411
  if (TSDB_CODE_SUCCESS == code) {
63,902,352✔
412
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
63,904,015✔
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,
6✔
416
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
417
              pReq->ctb.suid, version);
418
  }
419
  return code;
63,922,778✔
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,963,079✔
428
  int32_t code = 0;
7,963,079✔
429
  void   *value = NULL;
7,963,079✔
430
  int32_t valueSize = 0;
7,963,079✔
431

432
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,963,079✔
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,963,079✔
440
    // for auto create table, we return the uid of the existing table
441
    pReq->uid = *(tb_uid_t *)value;
40,090✔
442
    tdbFree(value);
40,090✔
443
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
40,090✔
444
  }
445

446
  // grant check
447
  code = grantCheck(TSDB_GRANT_TIMESERIES);
7,922,989✔
448
  if (code) {
7,922,989✔
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,922,989✔
453
}
454

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

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

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

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

473
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
89,186,932✔
474
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
81,464,507✔
475
    (*ppRsp)->pSchemaExt[i].colId = p->id;
81,464,507✔
476
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
81,464,507✔
477
    if (pEntry->pExtSchemas) {
81,464,507✔
478
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
571,650✔
479
    }
480
  }
481

482
  return code;
7,722,425✔
483
}
484

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

488
  // check request
489
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
7,762,515✔
490
  if (code) {
7,762,515✔
491
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
40,090✔
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);
40,090✔
496
  }
497

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

515
  // build response
516
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
7,722,425✔
517
  if (code) {
7,722,425✔
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,722,425✔
524
  if (TSDB_CODE_SUCCESS == code) {
7,722,425✔
525
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
7,722,425✔
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,722,425✔
532
}
533

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

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

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

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

553
  return code;
200,564✔
554
}
555

556
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
200,564✔
557
  // check request
558
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
200,564✔
559
  if (code) {
200,564✔
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,
601,584✔
568
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
569
                      .uid = pReq->uid,
200,564✔
570
                      .name = pReq->name,
200,564✔
571
                      .ntbEntry.btime = pReq->btime,
200,564✔
572
                      .ntbEntry.ttlDays = pReq->ttl,
200,564✔
573
                      .ntbEntry.commentLen = pReq->commentLen,
200,564✔
574
                      .ntbEntry.comment = pReq->comment,
200,564✔
575
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
576
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
200,564✔
577
                      .ntbEntry.ownerId = pReq->ntb.userId,
200,564✔
578
                      .pExtSchemas = pReq->pExtSchemas,
200,564✔
579
                      .colRef = pReq->colRef};
580

581
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
200,564✔
582
  if (code) {
200,564✔
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);
200,564✔
590
  if (TSDB_CODE_SUCCESS == code) {
200,564✔
591
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
200,564✔
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);
200,564✔
598
#if 0
599
  metaTimeSeriesNotifyCheck(pMeta);
600
#endif
601
}
602

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

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

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

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

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

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

633
  return code;
359,048✔
634
}
635

636
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
359,048✔
637
  // check request
638
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
359,048✔
639
  if (code) {
359,048✔
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,
718,042✔
648
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
649
                      .uid = pReq->uid,
359,048✔
650
                      .name = pReq->name,
359,048✔
651
                      .ctbEntry.btime = pReq->btime,
359,048✔
652
                      .ctbEntry.ttlDays = pReq->ttl,
359,048✔
653
                      .ctbEntry.commentLen = pReq->commentLen,
359,048✔
654
                      .ctbEntry.comment = pReq->comment,
359,048✔
655
                      .ctbEntry.suid = pReq->ctb.suid,
359,048✔
656
                      .ctbEntry.pTags = pReq->ctb.pTag,
359,048✔
657
                      .colRef = pReq->colRef};
658

659
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
359,048✔
660
  if (code) {
359,048✔
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);
359,048✔
668
  if (TSDB_CODE_SUCCESS == code) {
359,048✔
669
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
359,048✔
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);
359,048✔
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) {
72,564,588✔
686
  int32_t code = TSDB_CODE_SUCCESS;
72,564,588✔
687
  if (TSDB_CHILD_TABLE == pReq->type) {
72,564,588✔
688
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
64,244,893✔
689
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
8,322,127✔
690
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
7,762,515✔
691
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
559,612✔
692
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
200,564✔
693
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
359,048✔
694
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
359,048✔
695
  } else {
696
    code = TSDB_CODE_INVALID_MSG;
×
697
  }
698
  TAOS_RETURN(code);
72,574,767✔
699
}
700

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

704
  // check request
705
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,623,344✔
706
  if (code) {
1,623,344✔
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,623,344✔
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,623,344✔
722
      .version = version,
723
      .uid = pReq->uid,
1,623,344✔
724
  };
725

726
  if (pReq->isVirtual) {
1,623,344✔
727
    if (pReq->suid == 0) {
71,386✔
728
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
40,113✔
729
    } else {
730
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
31,273✔
731
    }
732
  } else {
733
    if (pReq->suid == 0) {
1,551,958✔
734
      entry.type = -TSDB_NORMAL_TABLE;
836,990✔
735
    } else {
736
      entry.type = -TSDB_CHILD_TABLE;
714,968✔
737
    }
738
  }
739
  code = metaHandleEntry2(pMeta, &entry);
1,623,344✔
740
  if (code) {
1,623,344✔
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,623,344✔
745
             pReq->uid, version);
746
  }
747
  TAOS_RETURN(code);
1,623,344✔
748
}
749

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

753
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,485,550✔
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,485,550✔
761
  int32_t valueSize = 0;
4,485,550✔
762
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,485,550✔
763
  if (code) {
4,485,550✔
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,485,550✔
770
  tdbFreeClear(value);
4,485,550✔
771

772
  // check table type
773
  SMetaInfo info;
4,485,334✔
774
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,485,550✔
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,485,550✔
782
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
29,749✔
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,485,550✔
791
  if (code) {
4,484,875✔
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,484,875✔
797
}
798

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

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

808
  // fetch old entry
809
  SMetaEntry *pEntry = NULL;
3,686,179✔
810
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,686,179✔
811
  if (code) {
3,686,179✔
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,686,179✔
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,686,179✔
825
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,686,179✔
826
  SSchema        *pColumn;
827
  SExtSchema      extSchema = {0};
3,686,179✔
828
  pEntry->version = version;
3,686,179✔
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,
334,800✔
833
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
834
      metaFetchEntryFree(&pEntry);
334,800✔
835
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
334,800✔
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,351,379✔
841
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,351,379✔
842
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
6,750✔
843
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
844
    metaFetchEntryFree(&pEntry);
6,750✔
845
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
6,750✔
846
  }
847

848
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,344,629✔
849
  if (pSchema->nCols + 1 > maxCols) {
3,344,629✔
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,344,629✔
857
  if (NULL == pNewSchema) {
3,344,629✔
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,344,629✔
864
  pSchema->version++;
3,344,629✔
865
  pSchema->nCols++;
3,344,629✔
866
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,344,629✔
867
  pColumn->bytes = pReq->bytes;
3,344,629✔
868
  pColumn->type = pReq->type;
3,344,629✔
869
  pColumn->flags = pReq->flags;
3,344,629✔
870
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,344,629✔
871
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
578✔
872
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
873
              version);
874
    metaFetchEntryFree(&pEntry);
578✔
875
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
578✔
876
  }
877
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,344,051✔
878
  extSchema.typeMod = pReq->typeMod;
3,344,051✔
879
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,344,051✔
880
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,344,051✔
881
    SColRef tmpRef;
57,650✔
882
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
57,704✔
883
      tmpRef.hasRef = false;
33,270✔
884
      tmpRef.id = pColumn->colId;
33,270✔
885
    } else {
886
      tmpRef.hasRef = true;
24,434✔
887
      tmpRef.id = pColumn->colId;
24,434✔
888
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
24,434✔
889
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
24,434✔
890
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
24,434✔
891
    }
892
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
57,704✔
893
    if (code) {
57,704✔
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,286,347✔
902
      compress = createDefaultColCmprByType(pColumn->type);
3,279,009✔
903
    } else {
904
      compress = pReq->compress;
7,338✔
905
    }
906
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
3,286,347✔
907
    if (code) {
3,286,347✔
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,344,051✔
915
  if (code) {
3,344,051✔
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,344,051✔
924
  if (code) {
3,344,051✔
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,344,051✔
931
             pEntry->uid, version);
932
  }
933

934
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,344,051✔
935
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
57,704✔
936
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
57,704✔
937
    if (code) {
57,704✔
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++) {
19,362,171✔
942
        SColRef *p = &pEntry->colRef.pColRef[i];
19,304,467✔
943
        pRsp->pColRefs[i].hasRef = p->hasRef;
19,304,467✔
944
        pRsp->pColRefs[i].id = p->id;
19,304,467✔
945
        if (p->hasRef) {
19,304,467✔
946
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
184,260✔
947
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
184,260✔
948
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
184,260✔
949
        }
950
      }
951
    }
952
  } else {
953
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,286,347✔
954
    if (code) {
3,286,347✔
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,344,051✔
967
  TAOS_RETURN(code);
3,344,051✔
968
}
969

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

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

979
  // fetch old entry
980
  SMetaEntry *pEntry = NULL;
102,318✔
981
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
102,318✔
982
  if (code) {
102,318✔
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) {
102,318✔
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;
102,318✔
997
  SSchema        *pColumn = NULL;
102,318✔
998
  SSchema         tColumn;
102,210✔
999
  int32_t         iColumn = 0;
102,318✔
1000
  for (; iColumn < pSchema->nCols; iColumn++) {
80,797,960✔
1001
    pColumn = &pSchema->pSchema[iColumn];
80,797,960✔
1002
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
80,797,960✔
1003
      break;
102,318✔
1004
    }
1005
  }
1006

1007
  if (iColumn == pSchema->nCols) {
102,318✔
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) {
102,318✔
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;
102,318✔
1022

1023
  // do drop column
1024
  pEntry->version = version;
102,318✔
1025
  if (pSchema->nCols - iColumn - 1 > 0) {
102,318✔
1026
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
79,016✔
1027
  }
1028
  pSchema->nCols--;
102,318✔
1029
  pSchema->version++;
102,318✔
1030
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
102,318✔
1031
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
31,531✔
1032
    if (code) {
31,531✔
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,531✔
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);
70,787✔
1046
    if (code) {
70,787✔
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) {
70,787✔
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);
102,318✔
1062
  if (code) {
102,318✔
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);
102,318✔
1071
  if (code) {
102,318✔
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,
102,318✔
1078
             pEntry->uid, version);
1079
  }
1080

1081
  // build response
1082
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
102,318✔
1083
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
31,531✔
1084
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
31,531✔
1085
    if (code) {
31,531✔
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++) {
38,078,284✔
1090
        SColRef *p = &pEntry->colRef.pColRef[i];
38,046,753✔
1091
        pRsp->pColRefs[i].hasRef = p->hasRef;
38,046,753✔
1092
        pRsp->pColRefs[i].id = p->id;
38,046,753✔
1093
        if (p->hasRef) {
38,046,753✔
1094
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
19,037,511✔
1095
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
19,037,511✔
1096
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
19,037,511✔
1097
        }
1098
      }
1099
    }
1100
  } else {
1101
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
70,787✔
1102
    if (code) {
70,787✔
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++) {
83,288,627✔
1107
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
83,217,840✔
1108
        pRsp->pSchemaExt[i].colId = p->id;
83,217,840✔
1109
        pRsp->pSchemaExt[i].compress = p->alg;
83,217,840✔
1110
      }
1111
    }
1112
  }
1113

1114
  metaFetchEntryFree(&pEntry);
102,318✔
1115
  TAOS_RETURN(code);
102,318✔
1116
}
1117

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

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

1127
  if (NULL == pReq->colNewName) {
41,686✔
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;
41,686✔
1135
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
41,686✔
1136
  if (code) {
41,686✔
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) {
41,686✔
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;
41,686✔
1151
  SSchema        *pColumn = NULL;
41,686✔
1152
  int32_t         iColumn = 0;
41,686✔
1153
  for (int32_t i = 0; i < pSchema->nCols; i++) {
194,953✔
1154
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
194,953✔
1155
      pColumn = &pSchema->pSchema[i];
41,686✔
1156
      iColumn = i;
41,686✔
1157
      break;
41,686✔
1158
    }
1159
  }
1160

1161
  if (NULL == pColumn) {
41,686✔
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;
41,686✔
1171
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
41,686✔
1172
  pSchema->version++;
41,686✔
1173

1174
  // do handle entry
1175
  code = metaHandleEntry2(pMeta, pEntry);
41,686✔
1176
  if (code) {
41,686✔
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,
41,686✔
1183
             pEntry->uid, version);
1184
  }
1185

1186
  // build response
1187
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
41,686✔
1188
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
28,699✔
1189
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
28,699✔
1190
    if (code) {
28,699✔
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++) {
202,786✔
1195
        SColRef *p = &pEntry->colRef.pColRef[i];
174,087✔
1196
        pRsp->pColRefs[i].hasRef = p->hasRef;
174,087✔
1197
        pRsp->pColRefs[i].id = p->id;
174,087✔
1198
        if (p->hasRef) {
174,087✔
1199
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
107,425✔
1200
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
107,425✔
1201
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
107,425✔
1202
        }
1203
      }
1204
    }
1205
  } else {
1206
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
12,987✔
1207
    if (code) {
12,987✔
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,580✔
1212
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
152,593✔
1213
        pRsp->pSchemaExt[i].colId = p->id;
152,593✔
1214
        pRsp->pSchemaExt[i].compress = p->alg;
152,593✔
1215
      }
1216
    }
1217
  }
1218

1219
  metaFetchEntryFree(&pEntry);
41,686✔
1220
  TAOS_RETURN(code);
41,686✔
1221
}
1222

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

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

1232
  // fetch old entry
1233
  SMetaEntry *pEntry = NULL;
514,156✔
1234
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
514,156✔
1235
  if (code) {
514,831✔
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) {
514,831✔
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;
514,831✔
1250
  SSchema        *pColumn = NULL;
514,831✔
1251
  int32_t         iColumn = 0;
514,831✔
1252
  int32_t         rowSize = 0;
514,831✔
1253
  for (int32_t i = 0; i < pSchema->nCols; i++) {
39,304,368✔
1254
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
38,789,537✔
1255
      pColumn = &pSchema->pSchema[i];
514,831✔
1256
      iColumn = i;
514,831✔
1257
    }
1258
    rowSize += pSchema->pSchema[i].bytes;
38,789,537✔
1259
  }
1260

1261
  if (NULL == pColumn) {
514,831✔
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) {
514,831✔
1269
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
209,250✔
1270
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1271
              version);
1272
    metaFetchEntryFree(&pEntry);
209,250✔
1273
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
209,250✔
1274
  }
1275

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

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

1290
  // do handle entry
1291
  code = metaHandleEntry2(pMeta, pEntry);
258,331✔
1292
  if (code) {
258,331✔
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,
258,331✔
1299
             pEntry->uid, version);
1300
  }
1301

1302
  // build response
1303
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
258,331✔
1304
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
27,885✔
1305
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
27,885✔
1306
    if (code) {
27,885✔
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++) {
19,129,372✔
1311
        SColRef *p = &pEntry->colRef.pColRef[i];
19,101,487✔
1312
        pRsp->pColRefs[i].hasRef = p->hasRef;
19,101,487✔
1313
        pRsp->pColRefs[i].id = p->id;
19,101,487✔
1314
        if (p->hasRef) {
19,101,487✔
1315
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
81,270✔
1316
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
81,270✔
1317
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
81,270✔
1318
        }
1319
      }
1320
    }
1321
  } else {
1322
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
230,446✔
1323
    if (code) {
230,446✔
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,401,996✔
1328
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,171,550✔
1329
        pRsp->pSchemaExt[i].colId = p->id;
9,171,550✔
1330
        pRsp->pSchemaExt[i].compress = p->alg;
9,171,550✔
1331
      }
1332
    }
1333
  }
1334

1335
  metaFetchEntryFree(&pEntry);
258,331✔
1336
  TAOS_RETURN(code);
258,331✔
1337
}
1338

1339

1340

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

1344
  if (pNewVal->isNull) {
8,315,362✔
1345
    return tTagGet(pOldTag, &oldVal);
69,769✔
1346
  }
1347

1348
  if (!tTagGet(pOldTag, &oldVal)){
8,246,969✔
1349
    return true;
202,054✔
1350
  }
1351

1352
  if (!IS_VAR_DATA_TYPE(oldVal.type)) {
8,043,539✔
1353
    return (memcmp(&oldVal.i64, pNewVal->pTagVal, pNewVal->nTagVal) != 0);
7,807,932✔
1354
  }
1355

1356
  if (oldVal.nData != pNewVal->nTagVal) {
235,607✔
1357
    return true;
179,098✔
1358
  }
1359

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

1363

1364

1365
static int32_t updatedTagValueArrayToHashMap(SSchemaWrapper* pTagSchema, SArray* arr, SHashObj **hashMap) {
8,141,988✔
1366
  int32_t numOfTags = arr == NULL ? 0 : taosArrayGetSize(arr);
8,141,988✔
1367
  if (numOfTags == 0) {
8,140,998✔
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,140,998✔
1373
  if (*hashMap == NULL) {
8,137,300✔
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,346,815✔
1379
    SUpdatedTagVal *pTagVal = taosArrayGet(arr, i);
8,207,149✔
1380
    if (taosHashGet(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId)) != NULL) {
8,207,149✔
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,205,535✔
1387
    if (code) {
8,206,827✔
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,137,236✔
1395
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,600,803✔
1396
    int32_t schemaColId = pTagSchema->pSchema[i].colId;
9,454,449✔
1397
    if (taosHashGet(*hashMap, &schemaColId, sizeof(schemaColId)) != NULL) {
9,458,771✔
1398
      changed++;
8,212,955✔
1399
    }
1400
  }
1401
  if (changed < numOfTags) {
8,143,364✔
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,143,364✔
1408
}
1409

1410

1411

1412
static int32_t metaUpdateTableJsonTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
123,193✔
1413
  SSchema *pCol = &pTagSchema->pSchema[0];
123,193✔
1414
  int32_t colId = pCol->colId;
123,193✔
1415
  SUpdatedTagVal *pTagVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
123,193✔
1416
  void *pNewTag = taosMemoryRealloc(pTable->ctbEntry.pTags, pTagVal->nTagVal);
123,193✔
1417
  if (pNewTag == NULL) {
123,193✔
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;
123,193✔
1423
  memcpy(pTable->ctbEntry.pTags, pTagVal->pTagVal, pTagVal->nTagVal);
123,193✔
1424

1425
  int32_t code = metaHandleEntry2(pMeta, pTable);
123,193✔
1426
  if (code) {
123,193✔
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;
123,193✔
1431
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
123,193✔
1432
  }
1433

1434
  return code;
123,193✔
1435
}
1436

1437

1438

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

1442
  const STag *pOldTag = (const STag *)pTable->ctbEntry.pTags;
8,249,771✔
1443
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
8,249,083✔
1444
  if (pTagArray == NULL) {
8,249,771✔
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,249,771✔
1451

1452
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
18,006,801✔
1453
    SSchema *pCol = &pTagSchema->pSchema[i];
9,760,278✔
1454
    STagVal  value = { .cid = pCol->colId };
9,759,590✔
1455

1456
    int32_t colId = pCol->colId;
9,760,278✔
1457
    SUpdatedTagVal *pNewVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
9,760,278✔
1458
    if (pNewVal == NULL) {
9,760,278✔
1459
      if (!tTagGet(pOldTag, &value)) {
1,443,540✔
1460
        continue;
298,687✔
1461
      }
1462
    } else {
1463
      value.type = pCol->type;
8,316,738✔
1464
      if (tagValueChanged(pNewVal, pOldTag)) {
8,316,738✔
1465
        allSame = false;
8,153,798✔
1466
      }
1467
      if (pNewVal->isNull) {
8,315,362✔
1468
        continue;
69,769✔
1469
      }
1470

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

1485
    if (taosArrayPush(pTagArray, &value) == NULL) {
9,388,574✔
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,245,835✔
1494
    const char* msgFmt = "vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64;
148,697✔
1495
    metaWarn(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pTable->version);
148,697✔
1496
    taosArrayDestroy(pTagArray);
148,697✔
1497
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
148,697✔
1498
  } 
1499

1500
  STag *pNewTag = NULL;
8,097,138✔
1501
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
8,097,138✔
1502
  if (code) {
8,097,138✔
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,097,138✔
1509
  taosMemoryFree(pTable->ctbEntry.pTags);
8,096,450✔
1510
  pTable->ctbEntry.pTags = (uint8_t *)pNewTag;
8,096,450✔
1511
  return TSDB_CODE_SUCCESS;
8,096,450✔
1512
}
1513

1514

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

1522
  const char *r = pReplace;
13,852✔
1523
  while (*r != '\0') {
201,496✔
1524
    const char *chunk = NULL;
187,644✔
1525
    size_t      chunkLen = 0;
187,644✔
1526

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

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

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

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

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

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

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

1596
    size_t prefixLen = (size_t)pmatch[0].rm_so;
13,852✔
1597
    if (resultLen + prefixLen >= resultCap) {
13,852✔
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,852✔
1607
    resultLen += prefixLen;
13,852✔
1608

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

1614
    if (pmatch[0].rm_so == pmatch[0].rm_eo) {
13,852✔
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,852✔
1628
    }
1629

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

1633
  size_t tailLen = strlen(cursor);
15,164✔
1634
  if (resultLen + tailLen + 1 > resultCap) {
15,164✔
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,164✔
1644
  resultLen += tailLen;
15,164✔
1645
  result[resultLen] = '\0';
15,164✔
1646

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

1651

1652

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

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

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

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

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

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

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

1722
  return TSDB_CODE_SUCCESS;
15,164✔
1723
}
1724

1725

1726

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

1736
  void* pIter = taosHashIterate(pUpdatedTagVals, NULL);
8,249,771✔
1737
  while (pIter) {
16,547,409✔
1738
    SUpdatedTagVal* pVal = (SUpdatedTagVal*)pIter;
8,308,866✔
1739
    if (pVal->regexp != NULL) {
8,308,866✔
1740
      break;
11,228✔
1741
    }
1742
    pIter = taosHashIterate(pUpdatedTagVals, pIter);
8,297,638✔
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,249,771✔
1748
    taosHashCancelIterate(pUpdatedTagVals, pIter);
11,228✔
1749
    pIter = NULL;
11,228✔
1750
    
1751
    int32_t sz = taosHashGetSize(pUpdatedTagVals);
11,228✔
1752
    pNewTagVals = taosHashInit(sz, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
11,228✔
1753
    if (pNewTagVals == NULL) {
11,228✔
1754
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1755
    }
1756

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

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

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

1772
        newVal.regexp = NULL;
19,100✔
1773
        newVal.replacement = NULL;
19,100✔
1774
        if (taosArrayPush(pRegexResults, &newVal.pTagVal) == NULL) {
19,100✔
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,100✔
1780
    }
1781
  }
1782

1783
  TAOS_CHECK_GOTO(metaUpdateTableNormalTagValue(pMeta, pTable, pTagSchema, pNewTagVals), &lino, _exit);
8,249,771✔
1784
  TAOS_CHECK_GOTO(metaHandleEntry2(pMeta, pTable), &lino, _exit);
8,096,450✔
1785

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

1795
  if (pRegexResults != NULL) {
8,249,771✔
1796
    for (int32_t i = 0; i < taosArrayGetSize(pRegexResults); i++) {
30,328✔
1797
      char** pp = taosArrayGet(pRegexResults, i);
19,100✔
1798
      taosMemoryFree(*pp);
19,100✔
1799
    }
1800
    taosArrayDestroy(pRegexResults);
11,228✔
1801
  }
1802

1803
  if (pNewTagVals != pUpdatedTagVals) {
8,249,771✔
1804
    taosHashCleanup(pNewTagVals);
11,228✔
1805
  }
1806

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

1811
  TAOS_RETURN(code);
8,249,771✔
1812
}
1813

1814

1815

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

1822
  // fetch child entry
1823
  code = metaFetchEntryByName(pMeta, tbName, &pChild);
8,084,752✔
1824
  if (code) {
8,084,752✔
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,084,752✔
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,084,752✔
1839
  if (code) {
8,084,752✔
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,084,752✔
1848

1849
  code = updatedTagValueArrayToHashMap(pTagSchema, tags, &pUpdatedTagVals);
8,084,752✔
1850
  if (code) {
8,084,752✔
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,084,752✔
1858
  code = metaUpdateTableTagValueImpl(pMeta, pChild, pTagSchema, pUpdatedTagVals);
8,084,752✔
1859
  if (code) {
8,084,752✔
1860
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, name:%s version:%" PRId64;
146,073✔
1861
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), tbName, version);
146,073✔
1862
    goto _exit;
146,073✔
1863
  }
1864

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

1872

1873

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

1879
  // Pre-allocate uidList for batch notification
1880
  int32_t nTables = taosArrayGetSize(pReq->tables);
8,073,480✔
1881
  uidList = taosArrayInit(nTables, sizeof(tb_uid_t));
8,073,480✔
1882
  if (uidList == NULL) {
8,073,480✔
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,073,480✔
1889
  if (tagListArray == NULL) {
8,073,480✔
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,158,232✔
1897
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
8,084,752✔
1898
    code = metaUpdateTableTagValue(pMeta, version, pTable->tbName, pTable->tags);
8,084,752✔
1899
    if (code == TSDB_CODE_VND_SAME_TAG) {
8,084,752✔
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,073✔
1904
    } else if (code) {
7,938,679✔
1905
      break;
×
1906
    } else {
1907
      // Collect UID for batch notification
1908
      int64_t uid = metaGetTableEntryUidByName(pMeta, pTable->tbName);
7,938,679✔
1909
      if (uid == 0) {
7,938,679✔
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,938,679✔
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,877,358✔
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,073,480✔
1929
    vnodeAlterTagForTmq(pMeta->pVnode, uidList, NULL, tagListArray);
7,927,407✔
1930
  }
1931

1932
  taosArrayDestroy(uidList);
8,073,480✔
1933
  taosArrayDestroy(tagListArray);
8,073,480✔
1934
  DestoryThreadLocalRegComp();
8,073,480✔
1935

1936
  if (code) {
8,073,480✔
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,073,480✔
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,675,314✔
1954
  STagToValueCtx *pCtx = (STagToValueCtx *)pContext;
1,675,314✔
1955

1956
  bool isTagCol = false, isTbname = false;
1,675,314✔
1957

1958
  if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
1,675,314✔
1959
    SColumnNode *pCol = (SColumnNode *)*pNode;
558,896✔
1960
    if (pCol->colType == COLUMN_TYPE_TBNAME)
556,960✔
1961
      isTbname = true;
129,952✔
1962
    else
1963
      isTagCol = true;
427,632✔
1964
  } else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
1,124,288✔
1965
    SFunctionNode *pFunc = (SFunctionNode *)*pNode;
×
1966
    if (pFunc->funcType == FUNCTION_TYPE_TBNAME)
×
1967
      isTbname = true;
×
1968
  }
1969

1970
  if (isTagCol) {
1,679,634✔
1971
    SColumnNode *pSColumnNode = *(SColumnNode **)pNode;
427,008✔
1972
    SValueNode  *res = NULL;
428,320✔
1973
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
428,320✔
1974
    if (NULL == res) {
427,330✔
1975
      return DEAL_RES_ERROR;
×
1976
    }
1977

1978
    res->translate = true;
427,330✔
1979
    res->node.resType = pSColumnNode->node.resType;
428,384✔
1980

1981
    STagVal tagVal = {0};
429,632✔
1982
    tagVal.cid = pSColumnNode->colId;
427,696✔
1983
    const char *p = metaGetTableTagVal(pCtx->pTags, pSColumnNode->node.resType.type, &tagVal);
425,890✔
1984
    if (p == NULL) {
425,504✔
1985
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
1986
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
425,504✔
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)) {
426,816✔
1995
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
9,854✔
1996
      if (NULL == res->datum.p) {
2,928✔
1997
        pCtx->code = terrno;
×
1998
        return DEAL_RES_ERROR;
×
1999
      }
2000
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
2,928✔
2001
      varDataSetLen(res->datum.p, tagVal.nData);
2,928✔
2002
    } else {
2003
      pCtx->code = nodesSetValueNodeValue(res, &(tagVal.i64));
422,338✔
2004
      if (pCtx->code != TSDB_CODE_SUCCESS) {
425,392✔
2005
        return DEAL_RES_ERROR;
×
2006
      }
2007
    }
2008

2009
    nodesDestroyNode(*pNode);
428,320✔
2010
    *pNode = (SNode *)res;
427,008✔
2011

2012
  } else if (isTbname) {
1,252,626✔
2013
    SValueNode *res = NULL;
129,952✔
2014
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
129,952✔
2015
    if (NULL == res) {
131,200✔
2016
      return DEAL_RES_ERROR;
×
2017
    }
2018

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

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

2034
  return DEAL_RES_CONTINUE;
1,680,580✔
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) {
560,208✔
2040
  int32_t     code = TSDB_CODE_SUCCESS;
560,208✔
2041
  SMetaEntry *pEntry = NULL;
560,208✔
2042

2043
  *pQualified = false;
559,218✔
2044

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

2050
  // Clone the condition so we can safely rewrite it
2051
  SNode *pTagCondTmp = NULL;
560,208✔
2052
  code = nodesCloneNode(pTagCond, &pTagCondTmp);
560,208✔
2053
  if (code != TSDB_CODE_SUCCESS) {
559,392✔
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};
559,392✔
2060
  nodesRewriteExprPostOrder(&pTagCondTmp, tagToValue, &ctx);
560,080✔
2061
  if (ctx.code != TSDB_CODE_SUCCESS) {
561,456✔
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;
561,456✔
2069
  code = scalarCalculateConstants(pTagCondTmp, &pResult);
561,456✔
2070
  if (code != TSDB_CODE_SUCCESS) {
560,272✔
2071
    nodesDestroyNode(pTagCondTmp);
×
2072
    metaFetchEntryFree(&pEntry);
×
2073
    return code;
×
2074
  }
2075

2076
  if (nodeType(pResult) == QUERY_NODE_VALUE) {
560,272✔
2077
    *pQualified = ((SValueNode *)pResult)->datum.b;
558,594✔
2078
  }
2079

2080
  nodesDestroyNode(pResult);
560,960✔
2081
  metaFetchEntryFree(&pEntry);
558,594✔
2082
  return TSDB_CODE_SUCCESS;
559,842✔
2083
}
2084

2085

2086

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

2093
  taosArrayClear(pUidList);
58,548✔
2094

2095
  // Step 1: Try index-accelerated pre-filtering with pTagIndexCond
2096
  if (pTagIndexCond != NULL) {
56,548✔
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) {
56,548✔
2130
    pCandidateUids = taosArrayInit(256, sizeof(uint64_t));
56,548✔
2131
    if (pCandidateUids == NULL) {
59,924✔
2132
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2133
    }
2134

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

2140
    while (1) {
686,354✔
2141
      tb_uid_t uid = metaCtbCursorNext(pCur);
744,902✔
2142
      if (uid == 0) {
737,076✔
2143
        break;
59,924✔
2144
      }
2145
      if (taosArrayPush(pCandidateUids, &uid) == NULL) {
685,344✔
2146
        code = terrno;
×
2147
        metaCloseCtbCursor(pCur);
×
2148
        TAOS_CHECK_GOTO(code, &lino, _end);
322✔
2149
      }
2150
    }
2151
    metaCloseCtbCursor(pCur);
59,924✔
2152
  }
2153

2154
  // Step 3: Apply pTagCond to filter the candidate UIDs
2155
  if (pTagCond == NULL) {
59,558✔
2156
    // No tag condition — all candidates qualify
2157
    taosArraySwap(pUidList, pCandidateUids);
5,248✔
2158
  } else {
2159
    // Evaluate pTagCond per child table
2160
    for (int32_t i = 0; i < taosArrayGetSize(pCandidateUids); i++) {
614,454✔
2161
      uint64_t uid = *(uint64_t*)taosArrayGet(pCandidateUids, i);
560,768✔
2162
      bool qualified = false;
560,080✔
2163
      code = metaIsChildTableQualified(pMeta, uid, pTagCond, &qualified);
560,832✔
2164
      if (code != TSDB_CODE_SUCCESS) {
559,218✔
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);
×
2168
      }
2169

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

2176
_end:
54,804✔
2177
  taosArrayDestroy(pCandidateUids);
59,924✔
2178
  if (code != TSDB_CODE_SUCCESS) {
59,924✔
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;
59,924✔
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) {
53,860✔
2189
  int32_t code = TSDB_CODE_SUCCESS;
53,860✔
2190
  SNode  *pTagCond = NULL;
53,860✔
2191
  SNode  *pTagIndexCond = NULL;
53,860✔
2192

2193
  if (pWhere == NULL) {
50,548✔
2194
    // No WHERE condition — return all child table UIDs
2195
    return metaGetChildUidsByTagCond(pMeta, suid, NULL, NULL, pUidList);
4,560✔
2196
  }
2197

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

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

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

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

2221

2222

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

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

2238
  if (pReq->whereLen > 0) {
59,236✔
2239
    code = nodesMsgToNode(pReq->where, pReq->whereLen, &pWhere);
54,052✔
2240
    if (code) {
52,676✔
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);
57,236✔
2249
  if (code) {
57,236✔
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) {
57,236✔
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);
54,612✔
2263
  if (code) {
57,558✔
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));
57,558✔
2270
  if (pUids == NULL) {
53,494✔
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);
53,494✔
2277
  if (code) {
59,924✔
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);
59,924✔
2285
  uidListForTmq = taosArrayInit(nUids, sizeof(tb_uid_t));
59,924✔
2286
  if (uidListForTmq == NULL) {
59,236✔
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++) {
343,512✔
2294
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(pUids, i);
288,212✔
2295
    SMetaEntry *pChild = NULL;
288,212✔
2296
    code = metaFetchEntryByUid(pMeta, uid, &pChild);
288,212✔
2297
    if (code) {
288,212✔
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;
288,212✔
2304
    code = metaUpdateTableTagValueImpl(pMeta, pChild, &pSuper->stbEntry.schemaTag, pUpdatedTagVals);
288,212✔
2305
    if (code == TSDB_CODE_VND_SAME_TAG) {
288,212✔
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,624✔
2310
    } else if (code) {
285,588✔
2311
      const char* fmt = "vgId:%d, %s failed at %s:%d since %s, child table uid %" PRId64 " name %s, version:%" PRId64;
3,936✔
2312
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), uid, pChild->name, version);
3,936✔
2313
      metaFetchEntryFree(&pChild);
3,936✔
2314
      goto _exit;
3,936✔
2315
    } else {
2316
      // Collect UID for batch notification
2317
      if (taosArrayPush(uidListForTmq, &uid) == NULL) {
281,652✔
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);
284,276✔
2324
  }
2325

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

2339

2340

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

2344
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
23,256✔
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,256✔
2351
}
2352

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

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

2361
  // fetch entry
2362
  SMetaEntry *pEntry = NULL;
23,256✔
2363
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
23,256✔
2364
  if (code) {
23,256✔
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,256✔
2372
  if (pEntry->type == TSDB_CHILD_TABLE) {
23,256✔
2373
    if (pReq->updateTTL) {
12,258✔
2374
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,749✔
2375
    }
2376
    if (pReq->newCommentLen >= 0) {
12,258✔
2377
      char *pNewComment = NULL;
7,509✔
2378
      if (pReq->newCommentLen) {
7,509✔
2379
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,785✔
2380
        if (NULL == pNewComment) {
4,785✔
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,785✔
2387
      } else {
2388
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,724✔
2389
      }
2390
      pEntry->ctbEntry.comment = pNewComment;
7,509✔
2391
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
7,509✔
2392
    }
2393
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
10,998✔
2394
    if (pReq->updateTTL) {
10,998✔
2395
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,863✔
2396
    }
2397
    if (pReq->newCommentLen >= 0) {
10,998✔
2398
      char *pNewComment = NULL;
6,135✔
2399
      if (pReq->newCommentLen > 0) {
6,135✔
2400
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
3,411✔
2401
        if (NULL == pNewComment) {
3,411✔
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,411✔
2408
      } else {
2409
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,724✔
2410
      }
2411
      pEntry->ntbEntry.comment = pNewComment;
6,135✔
2412
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
6,135✔
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,256✔
2423
  if (code) {
23,256✔
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,256✔
2430
             pEntry->uid, version);
2431
  }
2432

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

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

2440
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,333✔
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,333✔
2447
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,333✔
2448
  if (code) {
6,333✔
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,333✔
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,333✔
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,333✔
2470
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,333✔
2471
  for (int32_t i = 0; i < wp->nCols; i++) {
50,664✔
2472
    SColCmpr *p = &wp->pColCmpr[i];
44,331✔
2473
    if (p->id == pReq->colId) {
44,331✔
2474
      uint32_t dst = 0;
6,333✔
2475
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,333✔
2476
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2477
      if (updated > 0) {
6,333✔
2478
        p->alg = dst;
6,333✔
2479
      }
2480
    }
2481
  }
2482

2483
  if (updated == 0) {
6,333✔
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,333✔
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,333✔
2498

2499
  // do handle entry
2500
  code = metaHandleEntry2(pMeta, pEntry);
6,333✔
2501
  if (code) {
6,333✔
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,333✔
2508
             pEntry->uid, version);
2509
  }
2510

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

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

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

2524
  if (NULL == pReq->refDbName) {
80,646✔
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) {
80,646✔
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) {
80,646✔
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;
80,646✔
2544
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
80,646✔
2545
  if (code) {
80,646✔
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) {
80,646✔
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;
80,646✔
2560
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
80,646✔
2561
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
29,684✔
2562
    if (code) {
29,684✔
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 =
80,646✔
2572
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
80,646✔
2573
  SColRef *pColRef = NULL;
80,646✔
2574
  int32_t  iColumn = 0;
80,646✔
2575
  for (int32_t i = 0; i < pSchema->nCols; i++) {
396,277✔
2576
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
396,277✔
2577
      pColRef = &pEntry->colRef.pColRef[i];
80,646✔
2578
      iColumn = i;
80,646✔
2579
      break;
80,646✔
2580
    }
2581
  }
2582

2583
  if (NULL == pColRef) {
80,646✔
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;
80,646✔
2593
  pColRef->hasRef = true;
80,646✔
2594
  pColRef->id = pSchema->pSchema[iColumn].colId;
80,646✔
2595
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
80,646✔
2596
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
80,646✔
2597
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
80,646✔
2598
  pSchema->version++;
80,646✔
2599
  pEntry->colRef.version++;
80,646✔
2600

2601
  // do handle entry
2602
  code = metaHandleEntry2(pMeta, pEntry);
80,646✔
2603
  if (code) {
80,646✔
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,
80,646✔
2611
             pEntry->uid, version);
2612
  }
2613

2614
  // build response
2615
  code = metaUpdateVtbMetaRsp(
241,938✔
2616
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
80,646✔
2617
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->pExtSchemas : pEntry->pExtSchemas,
80,646✔
2618
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
80,646✔
2619
      pEntry->type);
80,646✔
2620
  if (code) {
80,646✔
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++) {
596,994✔
2625
      SColRef *p = &pEntry->colRef.pColRef[i];
516,348✔
2626
      pRsp->pColRefs[i].hasRef = p->hasRef;
516,348✔
2627
      pRsp->pColRefs[i].id = p->id;
516,348✔
2628
      if (p->hasRef) {
516,348✔
2629
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
355,488✔
2630
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
355,488✔
2631
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
355,488✔
2632
      }
2633
    }
2634
  }
2635

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

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

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

2650
  // fetch old entry
2651
  SMetaEntry *pEntry = NULL;
59,890✔
2652
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
59,890✔
2653
  if (code) {
59,890✔
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) {
59,890✔
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;
59,890✔
2668
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
59,890✔
2669
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
29,749✔
2670
    if (code) {
29,749✔
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 =
59,890✔
2680
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
59,890✔
2681
  SColRef *pColRef = NULL;
59,890✔
2682
  int32_t  iColumn = 0;
59,890✔
2683
  for (int32_t i = 0; i < pSchema->nCols; i++) {
239,625✔
2684
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
239,625✔
2685
      pColRef = &pEntry->colRef.pColRef[i];
59,890✔
2686
      iColumn = i;
59,890✔
2687
      break;
59,890✔
2688
    }
2689
  }
2690

2691
  if (NULL == pColRef) {
59,890✔
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;
59,890✔
2701
  pColRef->hasRef = false;
59,890✔
2702
  pColRef->id = pSchema->pSchema[iColumn].colId;
59,890✔
2703
  pSchema->version++;
59,890✔
2704
  pEntry->colRef.version++;
59,890✔
2705

2706
  // do handle entry
2707
  code = metaHandleEntry2(pMeta, pEntry);
59,890✔
2708
  if (code) {
59,890✔
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,
59,890✔
2716
             pEntry->uid, version);
2717
  }
2718

2719
  // build response
2720
  code = metaUpdateVtbMetaRsp(
179,670✔
2721
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
59,890✔
2722
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->pExtSchemas : pEntry->pExtSchemas,
59,890✔
2723
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
59,890✔
2724
      pEntry->type);
59,890✔
2725
  if (code) {
59,890✔
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++) {
372,933✔
2730
      SColRef *p = &pEntry->colRef.pColRef[i];
313,043✔
2731
      pRsp->pColRefs[i].hasRef = p->hasRef;
313,043✔
2732
      pRsp->pColRefs[i].id = p->id;
313,043✔
2733
      if (p->hasRef) {
313,043✔
2734
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
157,111✔
2735
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
157,111✔
2736
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
157,111✔
2737
      }
2738
    }
2739
  }
2740

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

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

2749
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
18,378✔
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;
18,378✔
2756
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
18,378✔
2757
  if (code) {
18,378✔
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) {
18,378✔
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) {
18,378✔
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;
18,378✔
2789
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
18,378✔
2790
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
18,378✔
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) {
18,378✔
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;
18,378✔
2816
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
197,808✔
2817
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
179,430✔
2818
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
179,430✔
2819

2820
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
179,430✔
2821
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
179,430✔
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)) {
179,430✔
2829
      numOfChangedTags++;
18,378✔
2830
      SSCHMEA_SET_IDX_ON(pOldColumn);
18,378✔
2831
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
161,052✔
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) {
18,378✔
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;
18,378✔
2848
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
18,378✔
2849

2850
  // do handle the entry
2851
  code = metaHandleEntry2(pMeta, pEntry);
18,378✔
2852
  if (code) {
18,378✔
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,
18,378✔
2859
             pEntry->uid, version);
2860
  }
2861

2862
  metaFetchEntryFree(&pEntry);
18,378✔
2863
  TAOS_RETURN(code);
18,378✔
2864
}
2865

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

2869
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
14,958✔
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;
14,958✔
2876
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
14,958✔
2877
  if (code) {
14,958✔
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) {
14,958✔
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;
14,958✔
2891
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
14,958✔
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;
14,958✔
2900
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
92,604✔
2901
    SSchema *pCol = pTagSchema->pSchema + i;
92,604✔
2902
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
92,604✔
2903
      if (!IS_IDX_ON(pCol)) {
14,958✔
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++;
14,958✔
2910
      SSCHMEA_SET_IDX_OFF(pCol);
14,958✔
2911
      break;
14,958✔
2912
    }
2913
  }
2914

2915
  if (numOfChangedTags != 1) {
14,958✔
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;
14,958✔
2924
  pTagSchema->version++;
14,958✔
2925
  code = metaHandleEntry2(pMeta, pEntry);
14,958✔
2926
  if (code) {
14,958✔
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,
14,958✔
2933
             pEntry->uid, version);
2934
  }
2935

2936
  metaFetchEntryFree(&pEntry);
14,958✔
2937
  TAOS_RETURN(code);
14,958✔
2938
}
2939

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

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

2949
  SMetaEntry *pEntry = NULL;
7,873,262✔
2950
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,864,464✔
2951
  if (code) {
7,878,942✔
2952
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
97✔
2953
              __FILE__, __LINE__, pReq->name, version);
2954
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
97✔
2955
  }
2956

2957
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,878,845✔
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,582,774✔
2965
      .version = version,
2966
      .type = TSDB_SUPER_TABLE,
2967
      .uid = pReq->suid,
7,869,594✔
2968
      .name = pReq->name,
7,866,300✔
2969
      .stbEntry.schemaRow = pReq->schemaRow,
2970
      .stbEntry.schemaTag = pReq->schemaTag,
2971
      .stbEntry.keep = pReq->keep,
7,853,943✔
2972
      .stbEntry.ownerId = pReq->ownerId,
7,845,804✔
2973
      .colCmpr = pReq->colCmpr,
2974
      .pExtSchemas = pReq->pExtSchemas,
7,855,192✔
2975
  };
2976
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,839,323✔
2977
  if (pReq->virtualStb) {
7,839,323✔
2978
    TABLE_SET_VIRTUAL(entry.flags);
23,873✔
2979
  }
2980
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,845,859✔
2981
    TABLE_SET_ROLLUP(entry.flags);
4,602✔
2982
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,602✔
2983
  }
2984

2985
  // do handle the entry
2986
  code = metaHandleEntry2(pMeta, &entry);
7,839,774✔
2987
  if (code) {
7,851,884✔
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,851,884✔
2994
             pReq->suid, version);
2995
  }
2996

2997
  metaFetchEntryFree(&pEntry);
7,904,527✔
2998
  TAOS_RETURN(code);
7,895,867✔
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,712✔
3041
  int32_t code = TSDB_CODE_SUCCESS;
40,712✔
3042

3043
  if (NULL == pReq->name || pReq->name[0] == 0) {
40,712✔
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,712✔
3050
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
40,712✔
3051
  if (code) {
40,712✔
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,712✔
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,712✔
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,712✔
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,712✔
3078
  }
3079

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

3091
  // do handle the entry
3092
  code = metaHandleEntry2(pMeta, &entry);
40,712✔
3093
  if (code) {
40,712✔
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,712✔
3100
    pMeta->pVnode->config.isRsma = 1;
40,712✔
3101
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
40,712✔
3102
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3103
  }
3104

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

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

3112
  if (NULL == pReq->name || pReq->name[0] == 0) {
20,494✔
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,494✔
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,494✔
3125
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
20,494✔
3126
  if (code) {
20,494✔
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,494✔
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,494✔
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,494✔
3148
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
20,494✔
3149
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
20,494✔
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,494✔
3159
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
20,494✔
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,494✔
3168
  entry.version = version;
20,494✔
3169
  TABLE_RESET_ROLLUP(entry.flags);
20,494✔
3170
  entry.stbEntry.rsmaParam.uid = 0;
20,494✔
3171
  entry.stbEntry.rsmaParam.name = NULL;
20,494✔
3172
  entry.stbEntry.rsmaParam.nFuncs = 0;
20,494✔
3173
  entry.stbEntry.rsmaParam.funcColIds = NULL;
20,494✔
3174
  entry.stbEntry.rsmaParam.funcIds = NULL;
20,494✔
3175

3176
  // do handle the entry
3177
  code = metaHandleEntry2(pMeta, &entry);
20,494✔
3178
  if (code) {
20,494✔
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,494✔
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,494✔
3188
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3189
  }
3190

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

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

3198
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,548✔
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,548✔
3205
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
12,548✔
3206
  if (code) {
12,548✔
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,548✔
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,548✔
3221
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
12,548✔
3222
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,548✔
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,548✔
3231
  entry.version = version;
12,548✔
3232
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
12,548✔
3233
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
12,548✔
3234
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
12,548✔
3235
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
12,548✔
3236
  }
3237
  // do handle the entry
3238
  code = metaHandleEntry2(pMeta, &entry);
12,548✔
3239
  if (code) {
12,548✔
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,548✔
3246
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3247
  }
3248

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