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

taosdata / TDengine / #5012

03 Apr 2026 03:59PM UTC coverage: 72.305% (+0.005%) from 72.3%
#5012

push

travis-ci

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

4053 of 5985 new or added lines in 68 files covered. (67.72%)

13105 existing lines in 173 files now uncovered.

257446 of 356056 relevant lines covered (72.3%)

131779375.07 hits per line

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

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

16
#include "meta.h"
17
#include "scalar.h"
18
#include "tarray.h"
19
#include "tdatablock.h"
20
#include "querynodes.h"
21
#include "thash.h"
22

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

35
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,722,502✔
36
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,722,502✔
37
  void     *value = NULL;
5,722,103✔
38
  int32_t   valueSize = 0;
5,723,088✔
39
  SMetaInfo info;
5,716,853✔
40

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

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

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

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

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

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

84
  return TSDB_CODE_SUCCESS;
5,711,716✔
85
}
86

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

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

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

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

123
  return code;
1,869,493✔
124
}
125

126
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
965,458✔
127
  int32_t   code = TSDB_CODE_SUCCESS;
965,458✔
128
  void     *value = NULL;
965,458✔
129
  int32_t   valueSize = 0;
966,035✔
130
  SMetaInfo info;
964,462✔
131

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

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

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

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

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

173
  // check request
174
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,707,228✔
175
  if (code != TSDB_CODE_SUCCESS) {
5,715,582✔
176
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
4,124✔
177
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
1,736✔
178
               pReq->name, pReq->suid, version);
179
      TAOS_RETURN(TSDB_CODE_SUCCESS);
1,736✔
180
    } else {
181
      TAOS_RETURN(code);
2,388✔
182
    }
183
  }
184

185
  // handle entry
186
  SMetaEntry entry = {
11,408,745✔
187
      .version = version,
188
      .type = TSDB_SUPER_TABLE,
189
      .uid = pReq->suid,
5,711,190✔
190
      .name = pReq->name,
5,707,226✔
191
      .stbEntry.schemaRow = pReq->schemaRow,
192
      .stbEntry.schemaTag = pReq->schemaTag,
193
      .stbEntry.keep = pReq->keep,
5,704,094✔
194
      .stbEntry.ownerId = pReq->ownerId,
5,695,825✔
195
  };
196
  if (pReq->rollup) {
5,700,337✔
197
    TABLE_SET_ROLLUP(entry.flags);
×
198
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
199
  }
200
  if (pReq->colCmpred) {
5,694,967✔
201
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,700,328✔
202
    entry.colCmpr = pReq->colCmpr;
5,700,328✔
203
  }
204

205
  entry.pExtSchemas = pReq->pExtSchemas;
5,701,241✔
206

207
  if (pReq->virtualStb) {
5,706,013✔
208
    TABLE_SET_VIRTUAL(entry.flags);
113,196✔
209
  }
210

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

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

226
  // check request
227
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
965,458✔
228
  if (code) {
964,749✔
229
    TAOS_RETURN(code);
1,592✔
230
  }
231

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

249
// Alter Super Table
250

251
// Create Child Table
252
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
69,460,305✔
253
  int32_t   code = TSDB_CODE_SUCCESS;
69,460,305✔
254
  void     *value = NULL;
69,460,305✔
255
  int32_t   valueSize = 0;
69,463,947✔
256
  SMetaInfo info;
69,452,765✔
257

258
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
69,450,564✔
259
      pReq->ctb.suid == 0) {
69,463,600✔
260
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
35,304✔
261
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
262
    return TSDB_CODE_INVALID_MSG;
×
263
  }
264

265
  // check table existence
266
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
69,438,399✔
267
    pReq->uid = *(int64_t *)value;
358,308✔
268
    tdbFreeClear(value);
358,308✔
269

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

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

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

293
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
356,516✔
294
  }
295

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

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

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

321
  // Check tag value
322
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
69,069,263✔
323
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
69,097,845✔
324
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
69,089,163✔
325
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
298,265,266✔
326
      STagVal tagVal = {
229,472,605✔
327
          .cid = pTagSchema->pSchema[i].colId,
229,457,716✔
328
      };
329

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

341
  metaFetchEntryFree(&pStbEntry);
69,106,962✔
342

343
  // check grant
344
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
69,065,442✔
345
    code = grantCheck(TSDB_GRANT_TIMESERIES);
69,096,512✔
346
    if (TSDB_CODE_SUCCESS != code) {
69,048,077✔
347
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
348
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
349
    }
350
  }
351
  return code;
69,053,477✔
352
}
353

354
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
68,721,779✔
355
  int32_t code = TSDB_CODE_SUCCESS;
68,721,779✔
356

357
  if (NULL == ppRsp) {
68,721,779✔
358
    return code;
×
359
  }
360

361
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
68,721,779✔
362
  if (NULL == ppRsp) {
68,689,718✔
363
    return terrno;
×
364
  }
365

366
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
68,689,718✔
367
  (*ppRsp)->tuid = pEntry->uid;
68,697,519✔
368
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
68,716,926✔
369
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
68,715,700✔
370

371
  return code;
68,733,606✔
372
}
373

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

377
  // check request
378
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
69,104,079✔
379
  if (code) {
69,073,949✔
380
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
358,398✔
381
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,882✔
382
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
383
    }
384
    return code;
358,443✔
385
  }
386

387
  SMetaEntry entry = {
68,715,551✔
388
      .version = version,
389
      .type = TSDB_CHILD_TABLE,
390
      .uid = pReq->uid,
68,718,779✔
391
      .name = pReq->name,
68,722,272✔
392
      .ctbEntry.btime = pReq->btime,
68,727,784✔
393
      .ctbEntry.ttlDays = pReq->ttl,
68,709,639✔
394
      .ctbEntry.commentLen = pReq->commentLen,
68,699,178✔
395
      .ctbEntry.comment = pReq->comment,
68,692,664✔
396
      .ctbEntry.suid = pReq->ctb.suid,
68,697,520✔
397
      .ctbEntry.pTags = pReq->ctb.pTag,
68,684,996✔
398
  };
399

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

407
  // handle entry
408
  code = metaHandleEntry2(pMeta, &entry);
68,719,182✔
409
  if (TSDB_CODE_SUCCESS == code) {
68,750,556✔
410
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
68,749,359✔
411
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
412
  } else {
413
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
1,197✔
414
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
415
              pReq->ctb.suid, version);
416
  }
417
  return code;
68,759,197✔
418
}
419

420
// Drop Child Table
421

422
// Alter Child Table
423

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

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

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

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

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

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

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

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

471
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
90,072,414✔
472
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
82,210,600✔
473
    (*ppRsp)->pSchemaExt[i].colId = p->id;
82,210,600✔
474
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
82,210,600✔
475
    if (pEntry->pExtSchemas) {
82,210,600✔
476
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
528,941✔
477
    }
478
  }
479

480
  return code;
7,861,814✔
481
}
482

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

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

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

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

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

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

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

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

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

551
  return code;
198,163✔
552
}
553

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

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

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

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

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

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

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

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

619
  return code;
353,719✔
620
}
621

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

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

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

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

667
// Drop Normal Table
668

669
// Alter Normal Table
670

671
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
77,788,528✔
672
  int32_t code = TSDB_CODE_SUCCESS;
77,788,528✔
673
  if (TSDB_CHILD_TABLE == pReq->type) {
77,788,528✔
674
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
69,107,449✔
675
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
8,685,043✔
676
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
8,133,161✔
677
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
551,882✔
678
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
198,163✔
679
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
353,719✔
680
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
353,719✔
681
  } else {
682
    code = TSDB_CODE_INVALID_MSG;
×
683
  }
684
  TAOS_RETURN(code);
77,802,158✔
685
}
686

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

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

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

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

712
  if (pReq->isVirtual) {
1,869,493✔
713
    if (pReq->suid == 0) {
72,577✔
714
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
40,646✔
715
    } else {
716
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
31,931✔
717
    }
718
  } else {
719
    if (pReq->suid == 0) {
1,796,916✔
720
      entry.type = -TSDB_NORMAL_TABLE;
867,781✔
721
    } else {
722
      entry.type = -TSDB_CHILD_TABLE;
929,135✔
723
    }
724
  }
725
  code = metaHandleEntry2(pMeta, &entry);
1,869,493✔
726
  if (code) {
1,869,493✔
727
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
728
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
729
  } else {
730
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,869,493✔
731
             pReq->uid, version);
732
  }
733
  TAOS_RETURN(code);
1,869,493✔
734
}
735

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1007
  tColumn = *pColumn;
88,147✔
1008

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

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

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

1067
  // build response
1068
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
88,147✔
1069
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
31,532✔
1070
                                pEntry->type);
31,532✔
1071
    if (code) {
31,532✔
1072
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1073
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1074
    } else {
1075
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
38,406,180✔
1076
        SColRef *p = &pEntry->colRef.pColRef[i];
38,374,648✔
1077
        pRsp->pColRefs[i].hasRef = p->hasRef;
38,374,648✔
1078
        pRsp->pColRefs[i].id = p->id;
38,374,648✔
1079
        if (p->hasRef) {
38,374,648✔
1080
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
19,200,766✔
1081
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
19,200,766✔
1082
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
19,200,766✔
1083
        }
1084
      }
1085
    }
1086
  } else {
1087
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
56,615✔
1088
    if (code) {
56,615✔
1089
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1090
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1091
    } else {
1092
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
23,613,799✔
1093
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
23,557,184✔
1094
        pRsp->pSchemaExt[i].colId = p->id;
23,557,184✔
1095
        pRsp->pSchemaExt[i].compress = p->alg;
23,557,184✔
1096
      }
1097
    }
1098
  }
1099

1100
  metaFetchEntryFree(&pEntry);
88,147✔
1101
  TAOS_RETURN(code);
88,147✔
1102
}
1103

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

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

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

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

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

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

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

1154

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

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

1172
  // build response
1173
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
42,576✔
1174
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
29,340✔
1175
                                pEntry->type);
29,340✔
1176
    if (code) {
29,340✔
1177
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1178
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1179
    } else {
1180
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
207,254✔
1181
        SColRef *p = &pEntry->colRef.pColRef[i];
177,914✔
1182
        pRsp->pColRefs[i].hasRef = p->hasRef;
177,914✔
1183
        pRsp->pColRefs[i].id = p->id;
177,914✔
1184
        if (p->hasRef) {
177,914✔
1185
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
109,812✔
1186
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
109,812✔
1187
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
109,812✔
1188
        }
1189
      }
1190
    }
1191
  } else {
1192
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
13,236✔
1193
    if (code) {
13,236✔
1194
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1195
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1196
    } else {
1197
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
167,931✔
1198
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
154,695✔
1199
        pRsp->pSchemaExt[i].colId = p->id;
154,695✔
1200
        pRsp->pSchemaExt[i].compress = p->alg;
154,695✔
1201
      }
1202
    }
1203
  }
1204

1205
  metaFetchEntryFree(&pEntry);
42,576✔
1206
  TAOS_RETURN(code);
42,576✔
1207
}
1208

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

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

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

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

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

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

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

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

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

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

1288
  // build response
1289
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
261,070✔
1290
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
28,465✔
1291
                                pEntry->type);
28,465✔
1292
    if (code) {
28,465✔
1293
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1294
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1295
    } else {
1296
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
19,297,295✔
1297
        SColRef *p = &pEntry->colRef.pColRef[i];
19,268,830✔
1298
        pRsp->pColRefs[i].hasRef = p->hasRef;
19,268,830✔
1299
        pRsp->pColRefs[i].id = p->id;
19,268,830✔
1300
        if (p->hasRef) {
19,268,830✔
1301
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
82,996✔
1302
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
82,996✔
1303
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
82,996✔
1304
        }
1305
      }
1306
    }
1307
  } else {
1308
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
232,605✔
1309
    if (code) {
232,605✔
1310
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1311
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1312
    } else {
1313
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,486,069✔
1314
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,253,464✔
1315
        pRsp->pSchemaExt[i].colId = p->id;
9,253,464✔
1316
        pRsp->pSchemaExt[i].compress = p->alg;
9,253,464✔
1317
      }
1318
    }
1319
  }
1320

1321
  metaFetchEntryFree(&pEntry);
261,070✔
1322
  TAOS_RETURN(code);
261,070✔
1323
}
1324

1325

1326

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

1330
  if (pNewVal->isNull) {
8,424,175✔
1331
    return tTagGet(pOldTag, &oldVal);
71,005✔
1332
  }
1333

1334
  if (!tTagGet(pOldTag, &oldVal)){
8,352,791✔
1335
    return true;
209,039✔
1336
  }
1337

1338
  if (!IS_VAR_DATA_TYPE(oldVal.type)) {
8,143,127✔
1339
    return (memcmp(&oldVal.i64, pNewVal->pTagVal, pNewVal->nTagVal) != 0);
7,903,816✔
1340
  }
1341

1342
  if (oldVal.nData != pNewVal->nTagVal) {
239,311✔
1343
    return true;
182,943✔
1344
  }
1345

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

1349

1350

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

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

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

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

1380
  int32_t changed = 0;
8,245,632✔
1381
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,850,633✔
1382
    int32_t schemaColId = pTagSchema->pSchema[i].colId;
9,605,001✔
1383
    if (taosHashGet(*hashMap, &schemaColId, sizeof(schemaColId)) != NULL) {
9,605,001✔
1384
      changed++;
8,318,722✔
1385
    }
1386
  }
1387
  if (changed < numOfTags) {
8,246,011✔
1388
    metaError("%s failed at %s:%d since tag count mismatch, %d:%d", __func__, __FILE__, __LINE__, changed, numOfTags);
×
1389
    taosHashCleanup(*hashMap);
×
1390
    return TSDB_CODE_VND_COL_NOT_EXISTS;
×
1391
  }
1392

1393
  return TSDB_CODE_SUCCESS;
8,246,011✔
1394
}
1395

1396

1397

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

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

1420
  return code;
125,480✔
1421
}
1422

1423

1424

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

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

1436
  bool allSame = true;
8,355,191✔
1437

1438
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
18,252,726✔
1439
    SSchema *pCol = &pTagSchema->pSchema[i];
9,901,931✔
1440
    STagVal  value = { .cid = pCol->colId };
9,902,177✔
1441

1442
    int32_t colId = pCol->colId;
9,902,556✔
1443
    SUpdatedTagVal *pNewVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
9,901,931✔
1444
    if (pNewVal == NULL) {
9,901,842✔
1445
      if (!tTagGet(pOldTag, &value)) {
1,477,667✔
1446
        continue;
304,854✔
1447
      }
1448
    } else {
1449
      value.type = pCol->type;
8,424,175✔
1450
      if (tagValueChanged(pNewVal, pOldTag)) {
8,424,175✔
1451
        allSame = false;
8,261,722✔
1452
      }
1453
      if (pNewVal->isNull) {
8,424,175✔
1454
        continue;
71,005✔
1455
      }
1456

1457
      if (IS_VAR_DATA_TYPE(pCol->type)) {
8,353,170✔
1458
        if ((int32_t)pNewVal->nTagVal > (pCol->bytes - VARSTR_HEADER_SIZE)) {
331,120✔
1459
          const char* msgFmt = "vgId:%d, %s failed at %s:%d since value too long for tag %s, version:%" PRId64;
4,017✔
1460
          metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pCol->name, pTable->version);
4,017✔
1461
          taosArrayDestroy(pTagArray);
4,017✔
1462
          TAOS_RETURN(TSDB_CODE_PAR_VALUE_TOO_LONG);
4,017✔
1463
        }
1464
        value.pData = pNewVal->pTagVal;
326,724✔
1465
        value.nData = pNewVal->nTagVal;
326,724✔
1466
      } else {
1467
        memcpy(&value.i64, pNewVal->pTagVal, pNewVal->nTagVal);
8,021,425✔
1468
      }
1469
    }
1470

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

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

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

1500

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

1508
  const char *r = pReplace;
14,148✔
1509
  while (*r != '\0') {
205,752✔
1510
    const char *chunk = NULL;
191,604✔
1511
    size_t      chunkLen = 0;
191,604✔
1512

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

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

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

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

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

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

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

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

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

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

1616
    execFlags = REG_NOTBOL;
14,148✔
1617
  }
1618

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

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

1637

1638

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

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

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

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

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

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

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

1708
  return TSDB_CODE_SUCCESS;
15,487✔
1709
}
1710

1711

1712

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

1722
  void* pIter = taosHashIterate(pUpdatedTagVals, NULL);
8,355,570✔
1723
  while (pIter) {
16,759,862✔
1724
    SUpdatedTagVal* pVal = (SUpdatedTagVal*)pIter;
8,415,762✔
1725
    if (pVal->regexp != NULL) {
8,415,762✔
1726
      break;
11,470✔
1727
    }
1728
    pIter = taosHashIterate(pUpdatedTagVals, pIter);
8,404,671✔
1729
  }
1730

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

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

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

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

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

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

1769
  TAOS_CHECK_GOTO(metaUpdateTableNormalTagValue(pMeta, pTable, pTagSchema, pNewTagVals), &lino, _exit);
8,355,570✔
1770
  TAOS_CHECK_GOTO(metaHandleEntry2(pMeta, pTable), &lino, _exit);
8,202,347✔
1771

1772
_exit:
8,353,428✔
1773
  if (code) {
8,353,428✔
1774
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
153,223✔
1775
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code), pTable->uid, pTable->name, pTable->version);
153,223✔
1776
  } else {
1777
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
8,200,205✔
1778
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
8,200,205✔
1779
  }
1780

1781
  if (pRegexResults != NULL) {
8,355,570✔
1782
    for (int32_t i = 0; i < taosArrayGetSize(pRegexResults); i++) {
30,974✔
1783
      char** pp = taosArrayGet(pRegexResults, i);
19,504✔
1784
      taosMemoryFree(*pp);
19,504✔
1785
    }
1786
    taosArrayDestroy(pRegexResults);
11,470✔
1787
  }
1788

1789
  if (pNewTagVals != pUpdatedTagVals) {
8,355,570✔
1790
    taosHashCleanup(pNewTagVals);
11,470✔
1791
  }
1792

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

1797
  TAOS_RETURN(code);
8,355,570✔
1798
}
1799

1800

1801

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

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

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

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

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

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

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

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

1858

1859

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

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

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

1914
  if (taosArrayGetSize(uidList) > 0) {
8,175,360✔
1915
    vnodeAlterTagForTmq(pMeta->pVnode, uidList, NULL, tagListArray);
8,028,832✔
1916
  }
1917

1918
  taosArrayDestroy(uidList);
8,175,360✔
1919
  taosArrayDestroy(tagListArray);
8,175,360✔
1920
  DestoryThreadLocalRegComp();
8,175,360✔
1921

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

1927
  TAOS_RETURN(code);
8,175,360✔
1928
}
1929

1930

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

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

1942
  bool isTagCol = false, isTbname = false;
1,707,252✔
1943

1944
  if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
1,707,252✔
1945
    SColumnNode *pCol = (SColumnNode *)*pNode;
567,105✔
1946
    if (pCol->colType == COLUMN_TYPE_TBNAME)
566,927✔
1947
      isTbname = true;
131,936✔
1948
    else
1949
      isTagCol = true;
439,900✔
1950
  } else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
1,144,922✔
1951
    SFunctionNode *pFunc = (SFunctionNode *)*pNode;
×
1952
    if (pFunc->funcType == FUNCTION_TYPE_TBNAME)
×
1953
      isTbname = true;
×
1954
  }
1955

1956
  if (isTagCol) {
1,709,417✔
1957
    SColumnNode *pSColumnNode = *(SColumnNode **)pNode;
436,330✔
1958
    SValueNode  *res = NULL;
436,330✔
1959
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
437,044✔
1960
    if (NULL == res) {
439,900✔
1961
      return DEAL_RES_ERROR;
×
1962
    }
1963

1964
    res->translate = true;
439,900✔
1965
    res->node.resType = pSColumnNode->node.resType;
439,186✔
1966

1967
    STagVal tagVal = {0};
439,186✔
1968
    tagVal.cid = pSColumnNode->colId;
439,186✔
1969
    const char *p = metaGetTableTagVal(pCtx->pTags, pSColumnNode->node.resType.type, &tagVal);
438,472✔
1970
    if (p == NULL) {
437,758✔
1971
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
1972
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
437,758✔
1973
      int32_t len = ((const STag *)p)->len;
×
1974
      res->datum.p = taosMemoryCalloc(len + 1, 1);
×
1975
      if (NULL == res->datum.p) {
×
1976
        pCtx->code = terrno;
×
1977
        return DEAL_RES_ERROR;
×
1978
      }
1979
      memcpy(res->datum.p, p, len);
×
1980
    } else if (IS_VAR_DATA_TYPE(pSColumnNode->node.resType.type)) {
435,616✔
1981
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
2,274✔
1982
      if (NULL == res->datum.p) {
3,032✔
1983
        pCtx->code = terrno;
×
1984
        return DEAL_RES_ERROR;
×
1985
      }
1986
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
2,653✔
1987
      varDataSetLen(res->datum.p, tagVal.nData);
2,653✔
1988
    } else {
1989
      pCtx->code = nodesSetValueNodeValue(res, &(tagVal.i64));
433,298✔
1990
      if (pCtx->code != TSDB_CODE_SUCCESS) {
434,012✔
1991
        return DEAL_RES_ERROR;
×
1992
      }
1993
    }
1994

1995
    nodesDestroyNode(*pNode);
437,044✔
1996
    *pNode = (SNode *)res;
439,186✔
1997

1998
  } else if (isTbname) {
1,273,087✔
1999
    SValueNode *res = NULL;
127,383✔
2000
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
127,383✔
2001
    if (NULL == res) {
133,900✔
2002
      return DEAL_RES_ERROR;
×
2003
    }
2004

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

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

2020
  return DEAL_RES_CONTINUE;
1,711,917✔
2021
}
2022

2023

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

2029
  *pQualified = false;
571,033✔
2030

2031
  code = metaFetchEntryByUid(pMeta, uid, &pEntry);
571,033✔
2032
  if (code != TSDB_CODE_SUCCESS || pEntry == NULL) {
560,054✔
2033
    return TSDB_CODE_SUCCESS;
×
2034
  }
2035

2036
  // Clone the condition so we can safely rewrite it
2037
  SNode *pTagCondTmp = NULL;
560,054✔
2038
  code = nodesCloneNode(pTagCond, &pTagCondTmp);
563,535✔
2039
  if (code != TSDB_CODE_SUCCESS) {
569,694✔
2040
    metaFetchEntryFree(&pEntry);
×
2041
    return code;
×
2042
  }
2043

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

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

2062
  if (nodeType(pResult) == QUERY_NODE_VALUE) {
561,751✔
2063
    *pQualified = ((SValueNode *)pResult)->datum.b;
567,552✔
2064
  }
2065

2066
  nodesDestroyNode(pResult);
571,122✔
2067
  metaFetchEntryFree(&pEntry);
568,980✔
2068
  return TSDB_CODE_SUCCESS;
567,374✔
2069
}
2070

2071

2072

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

2079
  taosArrayClear(pUidList);
60,565✔
2080

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

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

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

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

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

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

2126
    while (1) {
703,594✔
2127
      tb_uid_t uid = metaCtbCursorNext(pCur);
762,820✔
2128
      if (uid == 0) {
763,534✔
2129
        break;
61,190✔
2130
      }
2131
      if (taosArrayPush(pCandidateUids, &uid) == NULL) {
704,219✔
2132
        code = terrno;
×
2133
        metaCloseCtbCursor(pCur);
×
2134
        TAOS_CHECK_GOTO(code, &lino, _end);
×
2135
      }
2136
    }
2137
    metaCloseCtbCursor(pCur);
61,190✔
2138
  }
2139

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

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

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

2171

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

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

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

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

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

2200
_cleanup:
55,834✔
2201
  nodesDestroyNode(pTagCond);
55,834✔
2202
  nodesDestroyNode(pTagIndexCond);
55,120✔
2203
  nodesDestroyNode(pWhereCopy);
55,120✔
2204
  return code;
55,120✔
2205
}
2206

2207

2208

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

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

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

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

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

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

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

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

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

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

2309
    metaFetchEntryFree(&pChild);
289,445✔
2310
  }
2311

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

2325

2326

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

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

2336
  return code;
23,750✔
2337
}
2338

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2799
  int32_t numOfChangedTags = 0;
19,031✔
2800
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
204,896✔
2801
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
185,865✔
2802
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
185,865✔
2803

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

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

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

2831
  pEntry->version = version;
19,031✔
2832
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
19,031✔
2833

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

2846
  metaFetchEntryFree(&pEntry);
19,031✔
2847
  TAOS_RETURN(code);
19,031✔
2848
}
2849

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

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

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

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

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

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

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

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

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

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

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

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

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

2948
  SMetaEntry entry = {
23,596,681✔
2949
      .version = version,
2950
      .type = TSDB_SUPER_TABLE,
2951
      .uid = pReq->suid,
7,880,173✔
2952
      .name = pReq->name,
7,875,069✔
2953
      .stbEntry.schemaRow = pReq->schemaRow,
2954
      .stbEntry.schemaTag = pReq->schemaTag,
2955
      .stbEntry.keep = pReq->keep,
7,870,368✔
2956
      .stbEntry.ownerId = pReq->ownerId,
7,872,220✔
2957
      .colCmpr = pReq->colCmpr,
2958
      .pExtSchemas = pReq->pExtSchemas,
7,861,732✔
2959
  };
2960
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,872,013✔
2961
  if (pReq->virtualStb) {
7,872,013✔
2962
    TABLE_SET_VIRTUAL(entry.flags);
22,408✔
2963
  }
2964
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,862,068✔
2965
    TABLE_SET_ROLLUP(entry.flags);
4,614✔
2966
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,614✔
2967
  }
2968

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

2981
  metaFetchEntryFree(&pEntry);
7,882,691✔
2982
  TAOS_RETURN(code);
7,893,701✔
2983
}
2984

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

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

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

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

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

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

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

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

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

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

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

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

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

3089
  metaFetchEntryFree(&pEntry);
40,882✔
3090
  TAOS_RETURN(code);
40,882✔
3091
}
3092

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3233
  metaFetchEntryFree(&pEntry);
12,602✔
3234
  TAOS_RETURN(code);
12,602✔
3235
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc