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

taosdata / TDengine / #4995

18 Mar 2026 06:26AM UTC coverage: 71.996% (-0.2%) from 72.244%
#4995

push

travis-ci

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

2 of 4 new or added lines in 2 files covered. (50.0%)

5312 existing lines in 167 files now uncovered.

244665 of 339830 relevant lines covered (72.0%)

135013613.72 hits per line

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

69.28
/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 "tdatablock.h"
19
#include "querynodes.h"
20

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

33
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,134,851✔
34
  int32_t   vgId = TD_VID(pMeta->pVnode);
5,134,851✔
35
  void     *value = NULL;
5,151,258✔
36
  int32_t   valueSize = 0;
5,150,746✔
37
  SMetaInfo info;
5,144,316✔
38

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

46
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
5,141,743✔
47
  if (r == 0) {  // name exists, check uid and type
5,139,094✔
48
    int64_t uid = *(tb_uid_t *)value;
3,322✔
49
    tdbFree(value);
3,322✔
50

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

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

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

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

82
  return TSDB_CODE_SUCCESS;
5,144,018✔
83
}
84

85
static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,574,409✔
86
  int32_t   code = TSDB_CODE_SUCCESS;
1,574,409✔
87
  void     *value = NULL;
1,574,409✔
88
  int32_t   valueSize = 0;
1,574,409✔
89
  SMetaInfo info;
1,574,193✔
90

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

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

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

121
  return code;
1,574,409✔
122
}
123

124
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
900,295✔
125
  int32_t   code = TSDB_CODE_SUCCESS;
900,295✔
126
  void     *value = NULL;
900,295✔
127
  int32_t   valueSize = 0;
900,713✔
128
  SMetaInfo info;
899,737✔
129

130
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
900,713✔
131
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
418✔
132
              __FILE__, __LINE__, pReq->name, version);
UNCOV
133
    return TSDB_CODE_INVALID_MSG;
×
134
  }
135

136
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
900,295✔
137
  if (code) {
897,919✔
138
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
752✔
139
              __FILE__, __LINE__, pReq->name, version);
140
    return TSDB_CODE_TDB_STB_NOT_EXIST;
752✔
141
  } else {
142
    int64_t uid = *(int64_t *)value;
897,167✔
143
    tdbFreeClear(value);
897,167✔
144

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

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

167
// Create Super Table
168
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
5,138,459✔
169
  int32_t code = TSDB_CODE_SUCCESS;
5,138,459✔
170

171
  // check request
172
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
5,138,459✔
173
  if (code != TSDB_CODE_SUCCESS) {
5,149,003✔
174
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
3,322✔
175
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
314✔
176
               pReq->name, pReq->suid, version);
177
      TAOS_RETURN(TSDB_CODE_SUCCESS);
314✔
178
    } else {
179
      TAOS_RETURN(code);
3,008✔
180
    }
181
  }
182

183
  // handle entry
184
  SMetaEntry entry = {
10,275,327✔
185
      .version = version,
186
      .type = TSDB_SUPER_TABLE,
187
      .uid = pReq->suid,
5,143,827✔
188
      .name = pReq->name,
5,140,531✔
189
      .stbEntry.schemaRow = pReq->schemaRow,
190
      .stbEntry.schemaTag = pReq->schemaTag,
191
      .stbEntry.keep = pReq->keep,
5,138,282✔
192
      .stbEntry.ownerId = pReq->ownerId,
5,130,631✔
193
  };
194
  if (pReq->rollup) {
5,143,369✔
UNCOV
195
    TABLE_SET_ROLLUP(entry.flags);
×
UNCOV
196
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
197
  }
198
  if (pReq->colCmpred) {
5,132,335✔
199
    TABLE_SET_COL_COMPRESSED(entry.flags);
5,144,544✔
200
    entry.colCmpr = pReq->colCmpr;
5,144,544✔
201
  }
202

203
  entry.pExtSchemas = pReq->pExtSchemas;
5,114,296✔
204

205
  if (pReq->virtualStb) {
5,132,185✔
206
    TABLE_SET_VIRTUAL(entry.flags);
93,383✔
207
  }
208

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

220
// Drop Super Table
221
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
899,869✔
222
  int32_t code = TSDB_CODE_SUCCESS;
899,869✔
223

224
  // check request
225
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
899,869✔
226
  if (code) {
900,135✔
227
    TAOS_RETURN(code);
1,504✔
228
  }
229

230
  // handle entry
231
  SMetaEntry entry = {
898,631✔
232
      .version = verison,
233
      .type = -TSDB_SUPER_TABLE,
234
      .uid = pReq->suid,
897,478✔
235
  };
236
  code = metaHandleEntry2(pMeta, &entry);
896,166✔
237
  if (code) {
899,790✔
UNCOV
238
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
239
              tstrerror(code));
240
  } else {
241
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
899,790✔
242
             pReq->suid, verison);
243
  }
244
  TAOS_RETURN(code);
899,790✔
245
}
246

247
// Alter Super Table
248

249
// Create Child Table
250
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
60,196,342✔
251
  int32_t   code = TSDB_CODE_SUCCESS;
60,196,342✔
252
  void     *value = NULL;
60,196,342✔
253
  int32_t   valueSize = 0;
60,197,569✔
254
  SMetaInfo info;
60,193,025✔
255

256
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
60,195,203✔
257
      pReq->ctb.suid == 0) {
60,195,844✔
258
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
7,408✔
259
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
UNCOV
260
    return TSDB_CODE_INVALID_MSG;
×
261
  }
262

263
  // check table existence
264
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
60,195,211✔
265
    pReq->uid = *(int64_t *)value;
320,660✔
266
    tdbFreeClear(value);
320,660✔
267

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

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

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

291
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
318,856✔
292
  }
293

294
  // check super table existence
295
  SMetaEntry *pStbEntry = NULL;
59,872,364✔
296
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
59,872,253✔
297
  if (code) {
59,870,631✔
UNCOV
298
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
×
299
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
UNCOV
300
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
301
  }
302

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

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

319
  // Check tag value
320
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
59,863,935✔
321
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
59,876,256✔
322
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
59,870,007✔
323
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
263,213,495✔
324
      STagVal tagVal = {
203,605,284✔
325
          .cid = pTagSchema->pSchema[i].colId,
203,600,356✔
326
      };
327

328
      if (tTagGet(pTag, &tagVal)) {
203,601,980✔
329
        if (pTagSchema->pSchema[i].type != tagVal.type) {
161,371,353✔
330
          metaError("vgId:%d, %s failed at %s:%d since child table %s tag type does not match the expected type, version:%" PRId64,
×
331
                    TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, version);
UNCOV
332
          metaFetchEntryFree(&pStbEntry);
×
UNCOV
333
          return TSDB_CODE_INVALID_MSG;
×
334
        }
335
      }
336
    }
337
  }
338

339
  metaFetchEntryFree(&pStbEntry);
59,851,416✔
340

341
  // check grant
342
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
59,864,676✔
343
    code = grantCheck(TSDB_GRANT_TIMESERIES);
59,865,512✔
344
    if (TSDB_CODE_SUCCESS != code) {
59,856,379✔
UNCOV
345
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
346
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
347
    }
348
  }
349
  return code;
59,859,910✔
350
}
351

352
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
59,554,213✔
353
  int32_t code = TSDB_CODE_SUCCESS;
59,554,213✔
354

355
  if (NULL == ppRsp) {
59,554,213✔
UNCOV
356
    return code;
×
357
  }
358

359
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
59,554,213✔
360
  if (NULL == ppRsp) {
59,540,303✔
UNCOV
361
    return terrno;
×
362
  }
363

364
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
59,540,303✔
365
  (*ppRsp)->tuid = pEntry->uid;
59,551,954✔
366
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
59,549,356✔
367
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
59,560,982✔
368

369
  return code;
59,564,776✔
370
}
371

372
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
59,887,930✔
373
  int32_t code = TSDB_CODE_SUCCESS;
59,887,930✔
374

375
  // check request
376
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
59,887,930✔
377
  if (code) {
59,868,245✔
378
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
320,660✔
379
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
1,804✔
380
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
381
    }
382
    return code;
320,660✔
383
  }
384

385
  SMetaEntry entry = {
59,547,585✔
386
      .version = version,
387
      .type = TSDB_CHILD_TABLE,
388
      .uid = pReq->uid,
59,550,931✔
389
      .name = pReq->name,
59,551,481✔
390
      .ctbEntry.btime = pReq->btime,
59,544,288✔
391
      .ctbEntry.ttlDays = pReq->ttl,
59,542,423✔
392
      .ctbEntry.commentLen = pReq->commentLen,
59,558,287✔
393
      .ctbEntry.comment = pReq->comment,
59,549,071✔
394
      .ctbEntry.suid = pReq->ctb.suid,
59,546,720✔
395
      .ctbEntry.pTags = pReq->ctb.pTag,
59,542,825✔
396
  };
397

398
  // build response
399
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
59,561,037✔
400
  if (code) {
59,557,794✔
UNCOV
401
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
402
              tstrerror(code));
403
  }
404

405
  // handle entry
406
  code = metaHandleEntry2(pMeta, &entry);
59,557,794✔
407
  if (TSDB_CODE_SUCCESS == code) {
59,567,352✔
408
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
59,568,306✔
409
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
410
  } else {
UNCOV
411
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
412
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
413
              pReq->ctb.suid, version);
414
  }
415
  return code;
59,573,181✔
416
}
417

418
// Drop Child Table
419

420
// Alter Child Table
421

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

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

434
  // check name
435
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
7,364,337✔
436
    // for auto create table, we return the uid of the existing table
437
    pReq->uid = *(tb_uid_t *)value;
46,064✔
438
    tdbFree(value);
46,064✔
439
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
46,064✔
440
  }
441

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

451
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
7,168,642✔
452
  int32_t code = TSDB_CODE_SUCCESS;
7,168,642✔
453

454
  if (NULL == ppRsp) {
7,168,642✔
UNCOV
455
    return code;
×
456
  }
457

458
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
7,168,642✔
459
  if (NULL == *ppRsp) {
7,168,642✔
UNCOV
460
    return terrno;
×
461
  }
462

463
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, pEntry->ntbEntry.ownerId, *ppRsp);
7,168,642✔
464
  if (code) {
7,168,642✔
UNCOV
465
    taosMemoryFreeClear(*ppRsp);
×
UNCOV
466
    return code;
×
467
  }
468

469
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
81,718,401✔
470
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
74,549,759✔
471
    (*ppRsp)->pSchemaExt[i].colId = p->id;
74,549,759✔
472
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
74,549,759✔
473
    if (pEntry->pExtSchemas) {
74,549,759✔
474
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
454,062✔
475
    }
476
  }
477

478
  return code;
7,168,642✔
479
}
480

481
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
7,214,706✔
482
  int32_t code = TSDB_CODE_SUCCESS;
7,214,706✔
483

484
  // check request
485
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
7,214,706✔
486
  if (code) {
7,214,706✔
487
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
46,064✔
UNCOV
488
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
489
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
490
    }
491
    TAOS_RETURN(code);
46,064✔
492
  }
493

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

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

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

530
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
149,631✔
531
  int32_t code = TSDB_CODE_SUCCESS;
149,631✔
532

533
  if (NULL == ppRsp) {
149,631✔
UNCOV
534
    return code;
×
535
  }
536

537
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
149,631✔
538
  if (NULL == *ppRsp) {
149,631✔
UNCOV
539
    return terrno;
×
540
  }
541

542
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, pEntry->ntbEntry.ownerId, *ppRsp,
149,631✔
543
                              TSDB_VIRTUAL_NORMAL_TABLE);
544
  if (code) {
149,631✔
UNCOV
545
    taosMemoryFreeClear(*ppRsp);
×
UNCOV
546
    return code;
×
547
  }
548

549
  return code;
149,631✔
550
}
551

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

563
  SMetaEntry entry = {.version = version,
299,208✔
564
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
565
                      .uid = pReq->uid,
149,631✔
566
                      .name = pReq->name,
149,631✔
567
                      .ntbEntry.btime = pReq->btime,
149,631✔
568
                      .ntbEntry.ttlDays = pReq->ttl,
149,631✔
569
                      .ntbEntry.commentLen = pReq->commentLen,
149,631✔
570
                      .ntbEntry.comment = pReq->comment,
149,631✔
571
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
572
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
149,631✔
573
                      .ntbEntry.ownerId = pReq->ntb.userId,
149,631✔
574
                      .colRef = pReq->colRef};
575

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

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

598
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
308,704✔
599
  int32_t code = TSDB_CODE_SUCCESS;
308,704✔
600

601
  if (NULL == ppRsp) {
308,704✔
UNCOV
602
    return code;
×
603
  }
604

605
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
308,704✔
606
  if (NULL == *ppRsp) {
308,704✔
UNCOV
607
    return terrno;
×
608
  }
609

610
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, NULL, &pEntry->colRef, 0, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
308,704✔
611
  if (code) {
308,704✔
UNCOV
612
    taosMemoryFreeClear(*ppRsp);
×
UNCOV
613
    return code;
×
614
  }
615
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
308,704✔
616

617
  return code;
308,704✔
618
}
619

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

631
  SMetaEntry entry = {.version = version,
617,354✔
632
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
633
                      .uid = pReq->uid,
308,704✔
634
                      .name = pReq->name,
308,704✔
635
                      .ctbEntry.btime = pReq->btime,
308,704✔
636
                      .ctbEntry.ttlDays = pReq->ttl,
308,704✔
637
                      .ctbEntry.commentLen = pReq->commentLen,
308,704✔
638
                      .ctbEntry.comment = pReq->comment,
308,704✔
639
                      .ctbEntry.suid = pReq->ctb.suid,
308,704✔
640
                      .ctbEntry.pTags = pReq->ctb.pTag,
308,704✔
641
                      .colRef = pReq->colRef};
642

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

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

665
// Drop Normal Table
666

667
// Alter Normal Table
668

669
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
67,560,662✔
670
  int32_t code = TSDB_CODE_SUCCESS;
67,560,662✔
671
  if (TSDB_CHILD_TABLE == pReq->type) {
67,560,662✔
672
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
59,890,392✔
673
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
7,673,041✔
674
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
7,214,706✔
675
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
458,335✔
676
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
149,631✔
677
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
308,704✔
678
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
308,704✔
679
  } else {
UNCOV
680
    code = TSDB_CODE_INVALID_MSG;
×
681
  }
682
  TAOS_RETURN(code);
67,566,740✔
683
}
684

685
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,574,409✔
686
  int32_t code = TSDB_CODE_SUCCESS;
1,574,409✔
687

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

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

705
  SMetaEntry entry = {
1,574,409✔
706
      .version = version,
707
      .uid = pReq->uid,
1,574,409✔
708
  };
709

710
  if (pReq->isVirtual) {
1,574,409✔
711
    if (pReq->suid == 0) {
54,993✔
712
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
27,799✔
713
    } else {
714
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
27,194✔
715
    }
716
  } else {
717
    if (pReq->suid == 0) {
1,519,416✔
718
      entry.type = -TSDB_NORMAL_TABLE;
785,148✔
719
    } else {
720
      entry.type = -TSDB_CHILD_TABLE;
734,268✔
721
    }
722
  }
723
  code = metaHandleEntry2(pMeta, &entry);
1,574,409✔
724
  if (code) {
1,574,409✔
UNCOV
725
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
726
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
727
  } else {
728
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,574,409✔
729
             pReq->uid, version);
730
  }
731
  TAOS_RETURN(code);
1,574,409✔
732
}
733

734
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,316,060✔
735
  int32_t code = 0;
4,316,060✔
736

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

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

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

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

783
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,574,668✔
784
  int32_t code = TSDB_CODE_SUCCESS;
3,574,668✔
785

786
  // check request
787
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,574,668✔
788
  if (code) {
3,574,668✔
UNCOV
789
    TAOS_RETURN(code);
×
790
  }
791

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

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

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

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

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

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

918
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,248,107✔
919
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
53,862✔
920
                                pEntry->type);
53,862✔
921
    if (code) {
53,862✔
UNCOV
922
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
923
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
924
    } else {
925
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,778,556✔
926
        SColRef *p = &pEntry->colRef.pColRef[i];
18,724,694✔
927
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,724,694✔
928
        pRsp->pColRefs[i].id = p->id;
18,724,694✔
929
        if (p->hasRef) {
18,724,694✔
930
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
172,870✔
931
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
172,870✔
932
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
172,870✔
933
        }
934
      }
935
    }
936
  } else {
937
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,194,245✔
938
    if (code) {
3,194,245✔
UNCOV
939
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
940
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
941
    } else {
942
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
943
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
944
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
945
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
946
      }
947
    }
948
  }
949

950
  metaFetchEntryFree(&pEntry);
3,248,107✔
951
  TAOS_RETURN(code);
3,248,107✔
952
}
953

954
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
77,864✔
955
  int32_t code = TSDB_CODE_SUCCESS;
77,864✔
956

957
  // check request
958
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
77,864✔
959
  if (code) {
77,864✔
UNCOV
960
    TAOS_RETURN(code);
×
961
  }
962

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

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

979
  // search the column to drop
980
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
77,864✔
981
  SSchema        *pColumn = NULL;
77,864✔
982
  SSchema         tColumn;
77,756✔
983
  int32_t         iColumn = 0;
77,864✔
984
  for (; iColumn < pSchema->nCols; iColumn++) {
45,501,809✔
985
    pColumn = &pSchema->pSchema[iColumn];
45,501,809✔
986
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
45,501,809✔
987
      break;
77,864✔
988
    }
989
  }
990

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

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

1005
  tColumn = *pColumn;
77,864✔
1006

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

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

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

1065
  // build response
1066
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
77,864✔
1067
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
29,837✔
1068
                                pEntry->type);
29,837✔
1069
    if (code) {
29,837✔
UNCOV
1070
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1071
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1072
    } else {
1073
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
36,953,500✔
1074
        SColRef *p = &pEntry->colRef.pColRef[i];
36,923,663✔
1075
        pRsp->pColRefs[i].hasRef = p->hasRef;
36,923,663✔
1076
        pRsp->pColRefs[i].id = p->id;
36,923,663✔
1077
        if (p->hasRef) {
36,923,663✔
1078
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
18,474,589✔
1079
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
18,474,589✔
1080
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
18,474,589✔
1081
        }
1082
      }
1083
    }
1084
  } else {
1085
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
48,027✔
1086
    if (code) {
48,027✔
UNCOV
1087
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1088
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1089
    } else {
1090
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
11,376,459✔
1091
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
11,328,432✔
1092
        pRsp->pSchemaExt[i].colId = p->id;
11,328,432✔
1093
        pRsp->pSchemaExt[i].compress = p->alg;
11,328,432✔
1094
      }
1095
    }
1096
  }
1097

1098
  metaFetchEntryFree(&pEntry);
77,864✔
1099
  TAOS_RETURN(code);
77,864✔
1100
}
1101

1102
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
40,270✔
1103
  int32_t code = TSDB_CODE_SUCCESS;
40,270✔
1104

1105
  // check request
1106
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
40,270✔
1107
  if (code) {
40,270✔
UNCOV
1108
    TAOS_RETURN(code);
×
1109
  }
1110

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

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

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

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

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

1152

1153
  // do update column name
1154
  pEntry->version = version;
40,270✔
1155
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
40,270✔
1156
  pSchema->version++;
40,270✔
1157

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

1170
  // build response
1171
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
40,270✔
1172
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
27,727✔
1173
                                pEntry->type);
27,727✔
1174
    if (code) {
27,727✔
UNCOV
1175
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1176
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1177
    } else {
1178
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
195,896✔
1179
        SColRef *p = &pEntry->colRef.pColRef[i];
168,169✔
1180
        pRsp->pColRefs[i].hasRef = p->hasRef;
168,169✔
1181
        pRsp->pColRefs[i].id = p->id;
168,169✔
1182
        if (p->hasRef) {
168,169✔
1183
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
103,789✔
1184
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
103,789✔
1185
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
103,789✔
1186
        }
1187
      }
1188
    }
1189
  } else {
1190
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
12,543✔
1191
    if (code) {
12,543✔
UNCOV
1192
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1193
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1194
    } else {
1195
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
159,660✔
1196
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
147,117✔
1197
        pRsp->pSchemaExt[i].colId = p->id;
147,117✔
1198
        pRsp->pSchemaExt[i].compress = p->alg;
147,117✔
1199
      }
1200
    }
1201
  }
1202

1203
  metaFetchEntryFree(&pEntry);
40,270✔
1204
  TAOS_RETURN(code);
40,270✔
1205
}
1206

1207
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
488,137✔
1208
  int32_t code = TSDB_CODE_SUCCESS;
488,137✔
1209

1210
  // check request
1211
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
488,137✔
1212
  if (code) {
488,137✔
UNCOV
1213
    TAOS_RETURN(code);
×
1214
  }
1215

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

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

1232
  // search the column to update
1233
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
488,137✔
1234
  SSchema        *pColumn = NULL;
487,485✔
1235
  int32_t         iColumn = 0;
487,485✔
1236
  int32_t         rowSize = 0;
487,485✔
1237
  for (int32_t i = 0; i < pSchema->nCols; i++) {
37,667,262✔
1238
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
37,179,777✔
1239
      pColumn = &pSchema->pSchema[i];
488,137✔
1240
      iColumn = i;
488,137✔
1241
    }
1242
    rowSize += pSchema->pSchema[i].bytes;
37,177,169✔
1243
  }
1244

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

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

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

1269
  // do change the column bytes
1270
  pEntry->version = version;
249,505✔
1271
  pSchema->version++;
249,505✔
1272
  pColumn->bytes = pReq->colModBytes;
249,505✔
1273

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

1286
  // build response
1287
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
249,505✔
1288
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->ntbEntry.ownerId, pRsp,
26,926✔
1289
                                pEntry->type);
26,926✔
1290
    if (code) {
26,926✔
UNCOV
1291
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1292
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1293
    } else {
1294
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
18,565,763✔
1295
        SColRef *p = &pEntry->colRef.pColRef[i];
18,538,837✔
1296
        pRsp->pColRefs[i].hasRef = p->hasRef;
18,538,837✔
1297
        pRsp->pColRefs[i].id = p->id;
18,538,837✔
1298
        if (p->hasRef) {
18,538,837✔
1299
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
78,482✔
1300
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
78,482✔
1301
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
78,482✔
1302
        }
1303
      }
1304
    }
1305
  } else {
1306
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
222,579✔
1307
    if (code) {
222,579✔
UNCOV
1308
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1309
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1310
    } else {
1311
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
9,081,563✔
1312
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
8,858,984✔
1313
        pRsp->pSchemaExt[i].colId = p->id;
8,858,984✔
1314
        pRsp->pSchemaExt[i].compress = p->alg;
8,858,984✔
1315
      }
1316
    }
1317
  }
1318

1319
  metaFetchEntryFree(&pEntry);
249,505✔
1320
  TAOS_RETURN(code);
249,505✔
1321
}
1322

1323

1324

1325
static bool tagValueChanged(const SUpdatedTagVal* pNewVal, const STag *pOldTag) {
8,121,128✔
1326
  STagVal oldVal = { .cid = pNewVal->colId };
8,121,128✔
1327

1328
  if (pNewVal->isNull) {
8,121,128✔
1329
    return tTagGet(pOldTag, &oldVal);
68,183✔
1330
  }
1331

1332
  if (!tTagGet(pOldTag, &oldVal)){
8,052,945✔
1333
    return true;
196,864✔
1334
  }
1335

1336
  if (!IS_VAR_DATA_TYPE(oldVal.type)) {
7,856,081✔
1337
    return (memcmp(&oldVal.i64, pNewVal->pTagVal, pNewVal->nTagVal) != 0);
7,631,882✔
1338
  }
1339

1340
  if (oldVal.nData != pNewVal->nTagVal) {
224,199✔
1341
    return true;
169,533✔
1342
  }
1343

1344
  return (memcmp(pNewVal->pTagVal, oldVal.pData, oldVal.nData) != 0);
54,666✔
1345
}
1346

1347

1348

1349
static int32_t updatedTagValueArrayToHashMap(SSchemaWrapper* pTagSchema, SArray* arr, SHashObj **hashMap) {
7,956,634✔
1350
  int32_t numOfTags = arr == NULL ? 0 : taosArrayGetSize(arr);
7,956,634✔
1351
  if (numOfTags == 0) {
7,956,634✔
UNCOV
1352
    metaError("%s failed at %s:%d since no tags specified", __func__, __FILE__, __LINE__);
×
UNCOV
1353
    return TSDB_CODE_INVALID_MSG;
×
1354
  }
1355

1356
  *hashMap = taosHashInit(numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
7,956,634✔
1357
  if (*hashMap == NULL) {
7,957,239✔
UNCOV
1358
    metaError("%s failed at %s:%d since %s", __func__, __FILE__, __LINE__, tstrerror(terrno));
×
UNCOV
1359
    return terrno;
×
1360
  }
1361

1362
  for (int32_t i = 0; i < taosArrayGetSize(arr); i++) {
15,979,953✔
1363
    SUpdatedTagVal *pTagVal = taosArrayGet(arr, i);
8,022,714✔
1364
    if (taosHashGet(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId)) != NULL) {
8,022,714✔
UNCOV
1365
      metaError("%s failed at %s:%d since duplicate tags %s", __func__, __FILE__, __LINE__, pTagVal->tagName);
×
UNCOV
1366
      taosHashCleanup(*hashMap);
×
UNCOV
1367
      return TSDB_CODE_INVALID_MSG;
×
1368
    }
1369

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

1378
  int32_t changed = 0;
7,957,239✔
1379
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,197,806✔
1380
    int32_t schemaColId = pTagSchema->pSchema[i].colId;
9,240,567✔
1381
    if (taosHashGet(*hashMap, &schemaColId, sizeof(schemaColId)) != NULL) {
9,240,567✔
1382
      changed++;
8,022,714✔
1383
    }
1384
  }
1385
  if (changed < numOfTags) {
7,957,239✔
UNCOV
1386
    metaError("%s failed at %s:%d since tag count mismatch, %d:%d", __func__, __FILE__, __LINE__, changed, numOfTags);
×
1387
    taosHashCleanup(*hashMap);
×
UNCOV
1388
    return TSDB_CODE_VND_COL_NOT_EXISTS;
×
1389
  }
1390

1391
  return TSDB_CODE_SUCCESS;
7,957,239✔
1392
}
1393

1394

1395

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

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

1418
  return code;
120,886✔
1419
}
1420

1421

1422

1423
static int32_t metaUpdateTableNormalTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
8,058,203✔
1424
  int32_t code = TSDB_CODE_SUCCESS;
8,058,203✔
1425

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

1434
  bool allSame = true;
8,058,203✔
1435

1436
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
17,573,739✔
1437
    SSchema *pCol = &pTagSchema->pSchema[i];
9,519,361✔
1438
    STagVal  value = { .cid = pCol->colId };
9,519,361✔
1439

1440
    int32_t colId = pCol->colId;
9,519,361✔
1441
    SUpdatedTagVal *pNewVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
9,519,361✔
1442
    if (pNewVal == NULL) {
9,519,361✔
1443
      if (!tTagGet(pOldTag, &value)) {
1,398,233✔
1444
        continue;
290,530✔
1445
      }
1446
    } else {
1447
      value.type = pCol->type;
8,121,128✔
1448
      if (tagValueChanged(pNewVal, pOldTag)) {
8,121,128✔
1449
        allSame = false;
7,963,305✔
1450
      }
1451
      if (pNewVal->isNull) {
8,121,128✔
1452
        continue;
68,183✔
1453
      }
1454

1455
      if (IS_VAR_DATA_TYPE(pCol->type)) {
8,052,945✔
1456
        if ((int32_t)pNewVal->nTagVal > (pCol->bytes - VARSTR_HEADER_SIZE)) {
308,744✔
1457
          const char* msgFmt = "vgId:%d, %s failed at %s:%d since value too long for tag %s, version:%" PRId64;
3,825✔
1458
          metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pCol->name, pTable->version);
3,825✔
1459
          taosArrayDestroy(pTagArray);
3,825✔
1460
          TAOS_RETURN(TSDB_CODE_PAR_VALUE_TOO_LONG);
3,825✔
1461
        }
1462
        value.pData = pNewVal->pTagVal;
304,919✔
1463
        value.nData = pNewVal->nTagVal;
304,919✔
1464
      } else {
1465
        memcpy(&value.i64, pNewVal->pTagVal, pNewVal->nTagVal);
7,744,201✔
1466
      }
1467
    }
1468

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

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

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

1498

1499
static int32_t regexReplaceBuildSubstitution(const char *pReplace, const regmatch_t *pmatch, int32_t nmatch,
12,750✔
1500
                                               const char *cursor, char **ppResult, size_t *pResultLen,
1501
                                               size_t *pResultCap) {
1502
  char  *result = *ppResult;
12,750✔
1503
  size_t resultLen = *pResultLen;
12,750✔
1504
  size_t resultCap = *pResultCap;
12,750✔
1505

1506
  const char *r = pReplace;
12,750✔
1507
  while (*r != '\0') {
188,700✔
1508
    const char *chunk = NULL;
175,950✔
1509
    size_t      chunkLen = 0;
175,950✔
1510

1511
    if (*r == '$' && r[1] >= '0' && r[1] <= '9') {
175,950✔
1512
      int32_t groupIdx = r[1] - '0';
20,400✔
1513
      if (groupIdx < nmatch && pmatch[groupIdx].rm_so != -1) {
20,400✔
1514
        chunk = cursor + pmatch[groupIdx].rm_so;
20,400✔
1515
        chunkLen = (size_t)(pmatch[groupIdx].rm_eo - pmatch[groupIdx].rm_so);
20,400✔
1516
      }
1517
      r += 2;
20,400✔
1518
    } else if (*r == '$' && r[1] == '$') {
155,550✔
UNCOV
1519
      chunk = "$";
×
UNCOV
1520
      chunkLen = 1;
×
UNCOV
1521
      r += 2;
×
1522
    } else {
1523
      chunk = r;
155,550✔
1524
      chunkLen = 1;
155,550✔
1525
      r += 1;
155,550✔
1526
    }
1527

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

1544
  *ppResult = result;
12,750✔
1545
  *pResultLen = resultLen;
12,750✔
1546
  *pResultCap = resultCap;
12,750✔
1547
  return TSDB_CODE_SUCCESS;
12,750✔
1548
}
1549

1550
static int32_t regexReplace(const char *pStr, const char *pPattern, const char *pReplace, char **ppResult) {
14,025✔
1551
  regex_t *regex = NULL;
14,025✔
1552
  int32_t  code = threadGetRegComp(&regex, pPattern);
14,025✔
1553
  if (code != 0) {
14,025✔
UNCOV
1554
    return code;
×
1555
  }
1556

1557
  size_t strLen = strlen(pStr);
14,025✔
1558
  size_t resultCap = strLen + 1;
14,025✔
1559
  size_t resultLen = 0;
14,025✔
1560
  char  *result = taosMemoryMalloc(resultCap);
14,025✔
1561
  if (NULL == result) {
14,025✔
UNCOV
1562
    return TSDB_CODE_OUT_OF_MEMORY;
×
1563
  }
1564

1565
  const int32_t nmatch = 10;
14,025✔
1566
  const char   *cursor = pStr;
14,025✔
1567
  regmatch_t    pmatch[10];
14,025✔
1568
  int32_t       execFlags = 0;
14,025✔
1569

1570
  while (*cursor != '\0') {
26,775✔
1571
    int ret = regexec(regex, cursor, nmatch, pmatch, execFlags);
14,025✔
1572
    if (ret == REG_NOMATCH) {
14,025✔
1573
      break;
1,275✔
1574
    }
1575
    if (ret != 0) {
12,750✔
UNCOV
1576
      taosMemoryFree(result);
×
UNCOV
1577
      return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
×
1578
    }
1579

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

1593
    code = regexReplaceBuildSubstitution(pReplace, pmatch, nmatch, cursor, &result, &resultLen, &resultCap);
12,750✔
1594
    if (code != TSDB_CODE_SUCCESS) {
12,750✔
1595
      return code;
×
1596
    }
1597

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

1614
    execFlags = REG_NOTBOL;
12,750✔
1615
  }
1616

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

1631
  *ppResult = result;
14,025✔
1632
  return TSDB_CODE_SUCCESS;
14,025✔
1633
}
1634

1635

1636

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

1646
  bool isNchar = (pNewVal->tagType == TSDB_DATA_TYPE_NCHAR);
14,025✔
1647
  char* oldStr = NULL;
14,025✔
1648

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

1672
  char* newStr = NULL;
14,025✔
1673
  int32_t code = regexReplace(oldStr, pNewVal->regexp, pNewVal->replacement, &newStr);
14,025✔
1674
  taosMemoryFree(oldStr);
14,025✔
1675
  if (code != TSDB_CODE_SUCCESS) {
14,025✔
UNCOV
1676
    return code;
×
1677
  }
1678

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

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

1697
    pNewVal->isNull = 0;
3,825✔
1698
    pNewVal->pTagVal = (uint8_t*)ucs4Buf;
3,825✔
1699
    pNewVal->nTagVal = (uint32_t)ucs4Len;
3,825✔
1700
  } else {
1701
    pNewVal->isNull = 0;
10,200✔
1702
    pNewVal->pTagVal = (uint8_t*)newStr;
10,200✔
1703
    pNewVal->nTagVal = (uint32_t)strlen(newStr);
10,200✔
1704
  }
1705

1706
  return TSDB_CODE_SUCCESS;
14,025✔
1707
}
1708

1709

1710

1711
static int32_t metaUpdateTableTagValueImpl(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
8,179,089✔
1712
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
8,179,089✔
1713
    return metaUpdateTableJsonTagValue(pMeta, pTable, pTagSchema, pUpdatedTagVals);
120,886✔
1714
  }
1715
  
1716
  int32_t   code = TSDB_CODE_SUCCESS, lino = 0;
8,058,203✔
1717
  SHashObj* pNewTagVals = pUpdatedTagVals;
8,058,203✔
1718
  SArray*   pRegexResults = NULL;
8,058,203✔
1719

1720
  void* pIter = taosHashIterate(pUpdatedTagVals, NULL);
8,058,203✔
1721
  while (pIter) {
16,161,481✔
1722
    SUpdatedTagVal* pVal = (SUpdatedTagVal*)pIter;
8,113,478✔
1723
    if (pVal->regexp != NULL) {
8,113,478✔
1724
      break;
10,200✔
1725
    }
1726
    pIter = taosHashIterate(pUpdatedTagVals, pIter);
8,103,278✔
1727
  }
1728

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

1741
    pRegexResults = taosArrayInit(sz, sizeof(char*));
10,200✔
1742
    if (pRegexResults == NULL) {
10,200✔
UNCOV
1743
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1744
    }
1745

1746
    pIter = taosHashIterate(pUpdatedTagVals, NULL);
10,200✔
1747
    while (pIter) {
28,050✔
1748
      int32_t       colId = *(int32_t*)taosHashGetKey(pIter, NULL);
17,850✔
1749
      SUpdatedTagVal newVal = *(SUpdatedTagVal *)pIter;
17,850✔
1750
      pIter = taosHashIterate(pUpdatedTagVals, pIter);
17,850✔
1751

1752
      if (newVal.regexp != NULL) {
17,850✔
1753
        const STag* pOldTag = (const STag*)pTable->ctbEntry.pTags;
17,850✔
1754
        TAOS_CHECK_GOTO(computeNewTagValViaRegexReplace(pOldTag, &newVal), &lino, _exit);
17,850✔
1755

1756
        newVal.regexp = NULL;
17,850✔
1757
        newVal.replacement = NULL;
17,850✔
1758
        if (taosArrayPush(pRegexResults, &newVal.pTagVal) == NULL) {
17,850✔
UNCOV
1759
          TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1760
        }
1761
      }
1762

1763
      TAOS_CHECK_GOTO(taosHashPut(pNewTagVals, &colId, sizeof(colId), &newVal, sizeof(newVal)), &lino, _exit);
17,850✔
1764
    }
1765
  }
1766

1767
  TAOS_CHECK_GOTO(metaUpdateTableNormalTagValue(pMeta, pTable, pTagSchema, pNewTagVals), &lino, _exit);
8,058,203✔
1768
  TAOS_CHECK_GOTO(metaHandleEntry2(pMeta, pTable), &lino, _exit);
7,909,051✔
1769

1770
_exit:
8,058,203✔
1771
  if (code) {
8,058,203✔
1772
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
149,152✔
1773
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code), pTable->uid, pTable->name, pTable->version);
149,152✔
1774
  } else {
1775
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
7,909,051✔
1776
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
7,909,051✔
1777
  }
1778

1779
  if (pRegexResults != NULL) {
8,058,203✔
1780
    for (int32_t i = 0; i < taosArrayGetSize(pRegexResults); i++) {
28,050✔
1781
      char** pp = taosArrayGet(pRegexResults, i);
17,850✔
1782
      taosMemoryFree(*pp);
17,850✔
1783
    }
1784
    taosArrayDestroy(pRegexResults);
10,200✔
1785
  }
1786

1787
  if (pNewTagVals != pUpdatedTagVals) {
8,058,203✔
1788
    taosHashCleanup(pNewTagVals);
10,200✔
1789
  }
1790

1791
  if (pIter != NULL) {
8,058,203✔
UNCOV
1792
    taosHashCancelIterate(pUpdatedTagVals, pIter);
×
1793
  }
1794

1795
  TAOS_RETURN(code);
8,058,203✔
1796
}
1797

1798

1799

1800
static int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, const char* tbName, SArray* tags) {
7,901,139✔
1801
  int32_t code = TSDB_CODE_SUCCESS;
7,901,139✔
1802
  SMetaEntry *pChild = NULL;
7,901,139✔
1803
  SMetaEntry *pSuper = NULL;
7,901,139✔
1804
  SHashObj* pUpdatedTagVals = NULL;
7,901,139✔
1805

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

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

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

1830
  // search the tags to update
1831
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
7,901,139✔
1832

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

1840
  // change tag values
1841
  pChild->version = version;
7,901,139✔
1842
  code = metaUpdateTableTagValueImpl(pMeta, pChild, pTagSchema, pUpdatedTagVals);
7,901,139✔
1843
  if (code) {
7,901,139✔
1844
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, name:%s version:%" PRId64;
142,777✔
1845
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), tbName, version);
142,777✔
1846
    goto _exit;
142,777✔
1847
  }
1848

1849
_exit:
7,901,139✔
1850
  taosHashCleanup(pUpdatedTagVals);
7,901,139✔
1851
  metaFetchEntryFree(&pSuper);
7,901,139✔
1852
  metaFetchEntryFree(&pChild);
7,901,139✔
1853
  TAOS_RETURN(code);
7,901,139✔
1854
}
1855

1856

1857

1858
int32_t metaUpdateTableMultiTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
7,891,609✔
1859
  int32_t code = TSDB_CODE_SUCCESS;
7,891,609✔
1860
  for (int32_t i = 0; i < taosArrayGetSize(pReq->tables); i++) {
15,792,748✔
1861
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
7,901,139✔
1862
    code = metaUpdateTableTagValue(pMeta, version, pTable->tbName, pTable->tags);
7,901,139✔
1863
    if (code == TSDB_CODE_VND_SAME_TAG) {
7,901,139✔
1864
      // we are updating multiple tables, if one table has same tag,
1865
      // just skip it and continue to update other tables,
1866
      // and return success at the end
1867
      code = TSDB_CODE_SUCCESS;
142,777✔
1868
    } else if (code) {
7,758,362✔
UNCOV
1869
      break;
×
1870
    } else {
1871
      vnodeAlterTagForTmq(pMeta->pVnode, pTable->tbName, pTable->tags);
7,758,362✔
1872
    }
1873
  }
1874

1875
  DestoryThreadLocalRegComp();
7,891,609✔
1876

1877
  if (code) {
7,891,609✔
1878
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
UNCOV
1879
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1880
  }
1881

1882
  TAOS_RETURN(code);
7,891,609✔
1883
}
1884

1885

1886
// Context for translating tag column/tbname references to tag values/table name
1887
typedef struct STagToValueCtx {
1888
  int32_t     code;
1889
  const void *pTags;  // STag* blob of the child table
1890
  const char *pName;  // table name of the child table
1891
} STagToValueCtx;
1892

1893
// Translate a tag column reference to the corresponding tag value
1894
static EDealRes tagToValue(SNode **pNode, void *pContext) {
1,621,195✔
1895
  STagToValueCtx *pCtx = (STagToValueCtx *)pContext;
1,621,195✔
1896

1897
  bool isTagCol = false, isTbname = false;
1,621,195✔
1898

1899
  if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
1,621,195✔
1900
    SColumnNode *pCol = (SColumnNode *)*pNode;
540,600✔
1901
    if (pCol->colType == COLUMN_TYPE_TBNAME)
540,600✔
1902
      isTbname = true;
127,500✔
1903
    else
1904
      isTagCol = true;
413,100✔
1905
  } else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
1,081,200✔
UNCOV
1906
    SFunctionNode *pFunc = (SFunctionNode *)*pNode;
×
UNCOV
1907
    if (pFunc->funcType == FUNCTION_TYPE_TBNAME)
×
UNCOV
1908
      isTbname = true;
×
1909
  }
1910

1911
  if (isTagCol) {
1,621,195✔
1912
    SColumnNode *pSColumnNode = *(SColumnNode **)pNode;
413,100✔
1913
    SValueNode  *res = NULL;
413,100✔
1914
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
413,100✔
1915
    if (NULL == res) {
413,100✔
UNCOV
1916
      return DEAL_RES_ERROR;
×
1917
    }
1918

1919
    res->translate = true;
413,100✔
1920
    res->node.resType = pSColumnNode->node.resType;
413,100✔
1921

1922
    STagVal tagVal = {0};
413,100✔
1923
    tagVal.cid = pSColumnNode->colId;
413,100✔
1924
    const char *p = metaGetTableTagVal(pCtx->pTags, pSColumnNode->node.resType.type, &tagVal);
413,100✔
1925
    if (p == NULL) {
412,495✔
1926
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
1927
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
412,495✔
UNCOV
1928
      int32_t len = ((const STag *)p)->len;
×
UNCOV
1929
      res->datum.p = taosMemoryCalloc(len + 1, 1);
×
1930
      if (NULL == res->datum.p) {
×
UNCOV
1931
        pCtx->code = terrno;
×
1932
        return DEAL_RES_ERROR;
×
1933
      }
UNCOV
1934
      memcpy(res->datum.p, p, len);
×
1935
    } else if (IS_VAR_DATA_TYPE(pSColumnNode->node.resType.type)) {
412,495✔
1936
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
605✔
UNCOV
1937
      if (NULL == res->datum.p) {
×
UNCOV
1938
        pCtx->code = terrno;
×
1939
        return DEAL_RES_ERROR;
×
1940
      }
1941
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
×
UNCOV
1942
      varDataSetLen(res->datum.p, tagVal.nData);
×
1943
    } else {
1944
      pCtx->code = nodesSetValueNodeValue(res, &(tagVal.i64));
412,495✔
1945
      if (pCtx->code != TSDB_CODE_SUCCESS) {
413,100✔
UNCOV
1946
        return DEAL_RES_ERROR;
×
1947
      }
1948
    }
1949

1950
    nodesDestroyNode(*pNode);
413,100✔
1951
    *pNode = (SNode *)res;
412,495✔
1952

1953
  } else if (isTbname) {
1,208,095✔
1954
    SValueNode *res = NULL;
127,500✔
1955
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
127,500✔
1956
    if (NULL == res) {
127,500✔
UNCOV
1957
      return DEAL_RES_ERROR;
×
1958
    }
1959

1960
    res->translate = true;
127,500✔
1961
    res->node.resType = ((SExprNode *)(*pNode))->resType;
127,500✔
1962

1963
    int32_t len = strlen(pCtx->pName);
126,895✔
1964
    res->datum.p = taosMemoryCalloc(len + VARSTR_HEADER_SIZE + 1, 1);
127,500✔
1965
    if (NULL == res->datum.p) {
127,500✔
UNCOV
1966
      pCtx->code = terrno;
×
UNCOV
1967
      return DEAL_RES_ERROR;
×
1968
    }
1969
    memcpy(varDataVal(res->datum.p), pCtx->pName, len);
127,500✔
1970
    varDataSetLen(res->datum.p, len);
127,500✔
1971
    nodesDestroyNode(*pNode);
127,500✔
1972
    *pNode = (SNode *)res;
127,500✔
1973
  }
1974

1975
  return DEAL_RES_CONTINUE;
1,620,590✔
1976
}
1977

1978

1979
// Check if a single child table qualifies against the tag condition.
1980
static int32_t metaIsChildTableQualified(SMeta *pMeta, tb_uid_t uid, SNode *pTagCond, bool *pQualified) {
539,995✔
1981
  int32_t     code = TSDB_CODE_SUCCESS;
539,995✔
1982
  SMetaEntry *pEntry = NULL;
539,995✔
1983

1984
  *pQualified = false;
539,995✔
1985

1986
  code = metaFetchEntryByUid(pMeta, uid, &pEntry);
539,995✔
1987
  if (code != TSDB_CODE_SUCCESS || pEntry == NULL) {
538,180✔
UNCOV
1988
    return TSDB_CODE_SUCCESS;
×
1989
  }
1990

1991
  // Clone the condition so we can safely rewrite it
1992
  SNode *pTagCondTmp = NULL;
538,180✔
1993
  code = nodesCloneNode(pTagCond, &pTagCondTmp);
538,180✔
1994
  if (code != TSDB_CODE_SUCCESS) {
540,600✔
UNCOV
1995
    metaFetchEntryFree(&pEntry);
×
UNCOV
1996
    return code;
×
1997
  }
1998

1999
  // Replace tag column and tbname references with concrete values
2000
  STagToValueCtx ctx = {.code = TSDB_CODE_SUCCESS, .pTags = pEntry->ctbEntry.pTags, .pName = pEntry->name};
540,600✔
2001
  nodesRewriteExprPostOrder(&pTagCondTmp, tagToValue, &ctx);
540,600✔
2002
  if (ctx.code != TSDB_CODE_SUCCESS) {
539,995✔
UNCOV
2003
    nodesDestroyNode(pTagCondTmp);
×
UNCOV
2004
    metaFetchEntryFree(&pEntry);
×
UNCOV
2005
    return ctx.code;
×
2006
  }
2007

2008
  // Evaluate the fully-resolved expression to a constant boolean
2009
  SNode *pResult = NULL;
539,995✔
2010
  code = scalarCalculateConstants(pTagCondTmp, &pResult);
539,995✔
2011
  if (code != TSDB_CODE_SUCCESS) {
536,970✔
UNCOV
2012
    nodesDestroyNode(pTagCondTmp);
×
2013
    metaFetchEntryFree(&pEntry);
×
UNCOV
2014
    return code;
×
2015
  }
2016

2017
  if (nodeType(pResult) == QUERY_NODE_VALUE) {
536,970✔
2018
    *pQualified = ((SValueNode *)pResult)->datum.b;
538,180✔
2019
  }
2020

2021
  nodesDestroyNode(pResult);
538,180✔
2022
  metaFetchEntryFree(&pEntry);
538,785✔
2023
  return TSDB_CODE_SUCCESS;
539,390✔
2024
}
2025

2026

2027

2028
static int32_t metaGetChildUidsByTagCond(SMeta *pMeta, tb_uid_t suid, SNode *pTagCond, SNode *pTagIndexCond, SArray *pUidList) {
55,495✔
2029
  int32_t       code = TSDB_CODE_SUCCESS;
55,495✔
2030
  int32_t       lino = 0;
55,495✔
2031
  SVnode       *pVnode = pMeta->pVnode;
55,495✔
2032
  SArray       *pCandidateUids = NULL;
55,495✔
2033

2034
  taosArrayClear(pUidList);
55,495✔
2035

2036
  // Step 1: Try index-accelerated pre-filtering with pTagIndexCond
2037
  if (pTagIndexCond != NULL) {
56,100✔
UNCOV
2038
    pCandidateUids = taosArrayInit(64, sizeof(uint64_t));
×
2039
    if (pCandidateUids == NULL) {
×
UNCOV
2040
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2041
    }
2042

UNCOV
2043
    SIndexMetaArg metaArg = {
×
2044
        .metaEx = pVnode,
UNCOV
2045
        .idx = metaGetIdx(pMeta),
×
2046
        .ivtIdx = metaGetIvtIdx(pMeta),
×
2047
        .suid = suid,
2048
    };
2049

UNCOV
2050
    SMetaDataFilterAPI filterAPI = {
×
2051
        .metaFilterTableIds = metaFilterTableIds,
2052
        .metaFilterCreateTime = metaFilterCreateTime,
2053
        .metaFilterTableName = metaFilterTableName,
2054
        .metaFilterTtl = metaFilterTtl,
2055
    };
2056

UNCOV
2057
    SIdxFltStatus idxStatus = SFLT_NOT_INDEX;
×
UNCOV
2058
    code = doFilterTag(pTagIndexCond, &metaArg, pCandidateUids, &idxStatus, &filterAPI);
×
UNCOV
2059
    if (code != TSDB_CODE_SUCCESS || idxStatus == SFLT_NOT_INDEX) {
×
2060
      // Index filtering failed or not supported, fall back to full scan
UNCOV
2061
      const char* msgFmt = "vgId:%d, index filter not used for suid:%" PRId64 ", status:%d code:%s";
×
UNCOV
2062
      metaDebug(msgFmt, TD_VID(pVnode), suid, idxStatus, tstrerror(code));
×
2063
      taosArrayDestroy(pCandidateUids);
×
UNCOV
2064
      pCandidateUids = NULL;
×
2065
      code = TSDB_CODE_SUCCESS;
×
2066
    }
2067
  }
2068

2069
  // Step 2: If index was not used, enumerate all child table UIDs
2070
  if (pCandidateUids == NULL) {
56,100✔
2071
    pCandidateUids = taosArrayInit(256, sizeof(uint64_t));
56,100✔
2072
    if (pCandidateUids == NULL) {
56,100✔
UNCOV
2073
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2074
    }
2075

2076
    SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
56,100✔
2077
    if (pCur == NULL) {
55,495✔
UNCOV
2078
      TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2079
    }
2080

2081
    while (1) {
666,890✔
2082
      tb_uid_t uid = metaCtbCursorNext(pCur);
722,385✔
2083
      if (uid == 0) {
724,200✔
2084
        break;
56,100✔
2085
      }
2086
      if (taosArrayPush(pCandidateUids, &uid) == NULL) {
666,890✔
2087
        code = terrno;
×
2088
        metaCloseCtbCursor(pCur);
×
UNCOV
2089
        TAOS_CHECK_GOTO(code, &lino, _end);
×
2090
      }
2091
    }
2092
    metaCloseCtbCursor(pCur);
56,100✔
2093
  }
2094

2095
  // Step 3: Apply pTagCond to filter the candidate UIDs
2096
  if (pTagCond == NULL) {
56,100✔
2097
    // No tag condition — all candidates qualify
2098
    taosArraySwap(pUidList, pCandidateUids);
5,100✔
2099
  } else {
2100
    // Evaluate pTagCond per child table
2101
    for (int32_t i = 0; i < taosArrayGetSize(pCandidateUids); i++) {
590,995✔
2102
      uint64_t uid = *(uint64_t*)taosArrayGet(pCandidateUids, i);
540,600✔
2103
      bool qualified = false;
540,600✔
2104
      code = metaIsChildTableQualified(pMeta, uid, pTagCond, &qualified);
540,600✔
2105
      if (code != TSDB_CODE_SUCCESS) {
539,390✔
UNCOV
2106
        const char* msgFmt = "vgId:%d, %s failed to evaluate tag cond for uid:%" PRId64 " suid:%" PRId64 " since %s";
×
UNCOV
2107
        metaError(msgFmt, TD_VID(pVnode), __func__, uid, suid, tstrerror(code));
×
UNCOV
2108
        TAOS_CHECK_GOTO(code, &lino, _end);
×
2109
      }
2110

2111
      if (qualified && taosArrayPush(pUidList, &uid) == NULL) {
688,025✔
UNCOV
2112
        TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2113
      }
2114
    }
2115
  }
2116

2117
_end:
56,100✔
2118
  taosArrayDestroy(pCandidateUids);
56,100✔
2119
  if (code != TSDB_CODE_SUCCESS) {
56,100✔
UNCOV
2120
    const char* msgFmt = "vgId:%d, %s failed at line %d for suid:%" PRId64 " since %s";
×
UNCOV
2121
    metaError(msgFmt, TD_VID(pVnode), __func__, lino, suid, tstrerror(code));
×
2122
  }
2123
  return code;
56,100✔
2124
}
2125

2126

2127
// Convenience wrapper: partition a raw WHERE condition into tag-related parts,
2128
// then call metaGetChildUidsByTagCond to get the filtered child table UIDs.
2129
static int32_t metaGetChildUidsByWhere(SMeta *pMeta, tb_uid_t suid, SNode *pWhere, SArray *pUidList) {
55,495✔
2130
  int32_t code = TSDB_CODE_SUCCESS;
55,495✔
2131
  SNode  *pTagCond = NULL;
55,495✔
2132
  SNode  *pTagIndexCond = NULL;
56,100✔
2133

2134
  if (pWhere == NULL) {
56,100✔
2135
    // No WHERE condition — return all child table UIDs
2136
    return metaGetChildUidsByTagCond(pMeta, suid, NULL, NULL, pUidList);
5,100✔
2137
  }
2138

2139
  // Clone pWhere so the caller's node is not destroyed by filterPartitionCond
2140
  SNode *pWhereCopy = NULL;
51,000✔
2141
  code = nodesCloneNode(pWhere, &pWhereCopy);
51,000✔
2142
  if (code != TSDB_CODE_SUCCESS) {
50,395✔
2143
    return code;
×
2144
  }
2145

2146
  // Partition the WHERE condition
2147
  code = filterPartitionCond(&pWhereCopy, NULL, &pTagIndexCond, &pTagCond, NULL);
50,395✔
2148
  if (code != TSDB_CODE_SUCCESS) {
50,395✔
2149
    goto _cleanup;
×
2150
  }
2151

2152
  // Call the core filtering function
2153
  code = metaGetChildUidsByTagCond(pMeta, suid, pTagCond, pTagIndexCond, pUidList);
50,395✔
2154

2155
_cleanup:
51,000✔
2156
  nodesDestroyNode(pTagCond);
51,000✔
2157
  nodesDestroyNode(pTagIndexCond);
51,000✔
2158
  nodesDestroyNode(pWhereCopy);
51,000✔
2159
  return code;
51,000✔
2160
}
2161

2162

2163

2164
int32_t metaUpdateTableChildTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
56,100✔
2165
  int32_t code = TSDB_CODE_SUCCESS;
56,100✔
2166
  SNode* pWhere = NULL;
56,100✔
2167
  SMetaEntry *pSuper = NULL;
56,100✔
2168
  SArray *pUids = NULL;
56,100✔
2169
  SHashObj *pUpdatedTagVals = NULL;
56,100✔
2170

2171
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
56,100✔
UNCOV
2172
    const char* fmt = "vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64;
×
UNCOV
2173
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
UNCOV
2174
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2175
    goto _exit;
×
2176
  }
2177

2178
  if (pReq->whereLen > 0) {
56,100✔
2179
    code = nodesMsgToNode(pReq->where, pReq->whereLen, &pWhere);
51,000✔
2180
    if (code) {
51,000✔
UNCOV
2181
      const char* fmt = "vgId:%d, %s failed at %s:%d since invalid where condition, version:%" PRId64;
×
2182
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
UNCOV
2183
      code = TSDB_CODE_INVALID_MSG;
×
2184
      goto _exit;
×
2185
    }
2186
  }
2187

2188
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pSuper);
56,100✔
2189
  if (code) {
56,100✔
UNCOV
2190
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64;
×
UNCOV
2191
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
UNCOV
2192
    goto _exit;
×
2193
  }
2194

2195
  if (pSuper->type != TSDB_SUPER_TABLE) {
56,100✔
UNCOV
2196
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64;
×
UNCOV
2197
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
UNCOV
2198
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
UNCOV
2199
    goto _exit;
×
2200
  }
2201

2202
  code = updatedTagValueArrayToHashMap(&pSuper->stbEntry.schemaTag, pReq->pMultiTag, &pUpdatedTagVals);
54,890✔
2203
  if (code) {
56,100✔
UNCOV
2204
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
UNCOV
2205
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
UNCOV
2206
    goto _exit;
×
2207
  }
2208

2209
  pUids = taosArrayInit(16, sizeof(int64_t));
56,100✔
2210
  if (pUids == NULL) {
56,100✔
UNCOV
2211
    code = terrno;
×
UNCOV
2212
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2213
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
UNCOV
2214
    goto _exit;
×
2215
  }
2216
  code = metaGetChildUidsByWhere(pMeta, pSuper->uid, pWhere, pUids);
56,100✔
2217
  if (code) {
56,100✔
UNCOV
2218
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
UNCOV
2219
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
UNCOV
2220
    goto _exit;
×
2221
  }
2222

2223
  for (int32_t i = 0; i < taosArrayGetSize(pUids); i++) {
330,225✔
2224
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(pUids, i);
277,950✔
2225
    SMetaEntry *pChild = NULL;
277,950✔
2226
    code = metaFetchEntryByUid(pMeta, uid, &pChild);
277,950✔
2227
    if (code) {
277,950✔
UNCOV
2228
      const char* fmt = "vgId:%d, %s failed at %s:%d since child table uid %" PRId64 " not found, version:%" PRId64;
×
UNCOV
2229
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, uid, version);
×
UNCOV
2230
      goto _exit;
×
2231
    }
2232

2233
    pChild->version = version;
277,950✔
2234
    code = metaUpdateTableTagValueImpl(pMeta, pChild, &pSuper->stbEntry.schemaTag, pUpdatedTagVals);
277,950✔
2235
    if (code == TSDB_CODE_VND_SAME_TAG) {
277,950✔
2236
      // we are updating multiple tables, if one table has same tag,
2237
      // just skip it and continue to update other tables,
2238
      // and return success at the end
2239
      code = TSDB_CODE_SUCCESS;
2,550✔
2240
    } else if (code) {
275,400✔
2241
      const char* fmt = "vgId:%d, %s failed at %s:%d since %s, child table uid %" PRId64 " name %s, version:%" PRId64;
3,825✔
2242
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), uid, pChild->name, version);
3,825✔
2243
      metaFetchEntryFree(&pChild);
3,825✔
2244
      goto _exit;
3,825✔
2245
    } else {
2246
      vnodeAlterTagForTmq(pMeta->pVnode, pChild->name, pReq->pMultiTag);
271,575✔
2247
    }
2248

2249
    metaFetchEntryFree(&pChild);
274,125✔
2250
  }
2251

2252
_exit:
56,100✔
2253
  DestoryThreadLocalRegComp();
56,100✔
2254
  taosArrayDestroy(pUids);
56,100✔
2255
  taosHashCleanup(pUpdatedTagVals);
56,100✔
2256
  metaFetchEntryFree(&pSuper);
56,100✔
2257
  nodesDestroyNode(pWhere);
56,100✔
2258
  TAOS_RETURN(code);
56,100✔
2259
}
2260

2261

2262

2263
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
22,506✔
2264
  int32_t code = TSDB_CODE_SUCCESS;
22,506✔
2265

2266
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
22,506✔
UNCOV
2267
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2268
              __FILE__, __LINE__, version);
2269
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2270
  }
2271

2272
  return code;
22,506✔
2273
}
2274

2275
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
22,506✔
2276
  int32_t code = 0;
22,506✔
2277

2278
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
22,506✔
2279
  if (code) {
22,506✔
UNCOV
2280
    TAOS_RETURN(code);
×
2281
  }
2282

2283
  // fetch entry
2284
  SMetaEntry *pEntry = NULL;
22,506✔
2285
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
22,506✔
2286
  if (code) {
22,506✔
UNCOV
2287
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2288
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
2289
    TAOS_RETURN(code);
×
2290
  }
2291

2292
  // do change the entry
2293
  pEntry->version = version;
22,506✔
2294
  if (pEntry->type == TSDB_CHILD_TABLE) {
22,506✔
2295
    if (pReq->updateTTL) {
11,857✔
2296
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
4,590✔
2297
    }
2298
    if (pReq->newCommentLen >= 0) {
11,857✔
2299
      char *pNewComment = NULL;
7,267✔
2300
      if (pReq->newCommentLen) {
7,267✔
2301
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
4,631✔
2302
        if (NULL == pNewComment) {
4,631✔
UNCOV
2303
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2304
                    __LINE__, tstrerror(terrno), version);
UNCOV
2305
          metaFetchEntryFree(&pEntry);
×
UNCOV
2306
          TAOS_RETURN(terrno);
×
2307
        }
2308
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
4,631✔
2309
      } else {
2310
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,636✔
2311
      }
2312
      pEntry->ctbEntry.comment = pNewComment;
7,267✔
2313
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
7,267✔
2314
    }
2315
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
10,649✔
2316
    if (pReq->updateTTL) {
10,649✔
2317
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
4,712✔
2318
    }
2319
    if (pReq->newCommentLen >= 0) {
10,649✔
2320
      char *pNewComment = NULL;
5,937✔
2321
      if (pReq->newCommentLen > 0) {
5,937✔
2322
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
3,301✔
2323
        if (NULL == pNewComment) {
3,301✔
UNCOV
2324
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2325
                    __LINE__, tstrerror(terrno), version);
UNCOV
2326
          metaFetchEntryFree(&pEntry);
×
UNCOV
2327
          TAOS_RETURN(terrno);
×
2328
        }
2329
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
3,301✔
2330
      } else {
2331
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,636✔
2332
      }
2333
      pEntry->ntbEntry.comment = pNewComment;
5,937✔
2334
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
5,937✔
2335
    }
2336
  } else {
UNCOV
2337
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2338
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
UNCOV
2339
    metaFetchEntryFree(&pEntry);
×
UNCOV
2340
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2341
  }
2342

2343
  // do handle entry
2344
  code = metaHandleEntry2(pMeta, pEntry);
22,506✔
2345
  if (code) {
22,506✔
UNCOV
2346
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2347
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
2348
    metaFetchEntryFree(&pEntry);
×
2349
    TAOS_RETURN(code);
×
2350
  } else {
2351
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
22,506✔
2352
             pEntry->uid, version);
2353
  }
2354

2355
  metaFetchEntryFree(&pEntry);
22,506✔
2356
  TAOS_RETURN(code);
22,506✔
2357
}
2358

2359
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,147✔
2360
  int32_t code = TSDB_CODE_SUCCESS;
6,147✔
2361

2362
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,147✔
UNCOV
2363
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2364
              __FILE__, __LINE__, version);
UNCOV
2365
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2366
  }
2367

2368
  SMetaEntry *pEntry = NULL;
6,147✔
2369
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,147✔
2370
  if (code) {
6,147✔
UNCOV
2371
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2372
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
2373
    TAOS_RETURN(code);
×
2374
  }
2375

2376
  if (pEntry->version >= version) {
6,147✔
UNCOV
2377
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2378
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2379
    metaFetchEntryFree(&pEntry);
×
UNCOV
2380
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2381
  }
2382

2383
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,147✔
UNCOV
2384
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2385
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
UNCOV
2386
    metaFetchEntryFree(&pEntry);
×
UNCOV
2387
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2388
  }
2389

2390
  // do change the entry
2391
  int8_t           updated = 0;
6,147✔
2392
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,147✔
2393
  for (int32_t i = 0; i < wp->nCols; i++) {
49,176✔
2394
    SColCmpr *p = &wp->pColCmpr[i];
43,029✔
2395
    if (p->id == pReq->colId) {
43,029✔
2396
      uint32_t dst = 0;
6,147✔
2397
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,147✔
2398
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2399
      if (updated > 0) {
6,147✔
2400
        p->alg = dst;
6,147✔
2401
      }
2402
    }
2403
  }
2404

2405
  if (updated == 0) {
6,147✔
2406
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
UNCOV
2407
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
2408
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2409
    metaFetchEntryFree(&pEntry);
×
UNCOV
2410
    TAOS_RETURN(code);
×
2411
  } else if (updated < 0) {
6,147✔
UNCOV
2412
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
UNCOV
2413
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
2414
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2415
    metaFetchEntryFree(&pEntry);
×
2416
    TAOS_RETURN(code);
×
2417
  }
2418

2419
  pEntry->version = version;
6,147✔
2420

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

2433
  metaFetchEntryFree(&pEntry);
6,147✔
2434
  TAOS_RETURN(code);
6,147✔
2435
}
2436

2437
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
77,263✔
2438
  int32_t code = TSDB_CODE_SUCCESS;
77,263✔
2439

2440
  // check request
2441
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
77,263✔
2442
  if (code) {
77,263✔
2443
    TAOS_RETURN(code);
×
2444
  }
2445

2446
  if (NULL == pReq->refDbName) {
77,263✔
UNCOV
2447
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2448
              __func__, __FILE__, __LINE__, version);
2449
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2450
  }
2451

2452
  if (NULL == pReq->refTbName) {
77,263✔
UNCOV
2453
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2454
              __func__, __FILE__, __LINE__, version);
UNCOV
2455
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2456
  }
2457

2458
  if (NULL == pReq->refColName) {
77,263✔
2459
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2460
              __func__, __FILE__, __LINE__, version);
UNCOV
2461
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2462
  }
2463

2464
  // fetch old entry
2465
  SMetaEntry *pEntry = NULL;
77,263✔
2466
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
77,263✔
2467
  if (code) {
77,263✔
UNCOV
2468
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2469
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
2470
    TAOS_RETURN(code);
×
2471
  }
2472

2473
  if (pEntry->version >= version) {
77,263✔
UNCOV
2474
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2475
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
2476
    metaFetchEntryFree(&pEntry);
×
UNCOV
2477
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2478
  }
2479

2480
  // fetch super entry
2481
  SMetaEntry *pSuper = NULL;
77,263✔
2482
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
77,263✔
2483
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
28,677✔
2484
    if (code) {
28,677✔
2485
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2486
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2487
      metaFetchEntryFree(&pEntry);
×
2488
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2489
    }
2490
  }
2491

2492
  // search the column to update
2493
  SSchemaWrapper *pSchema =
77,263✔
2494
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
77,263✔
2495
  SColRef *pColRef = NULL;
77,263✔
2496
  int32_t  iColumn = 0;
77,263✔
2497
  for (int32_t i = 0; i < pSchema->nCols; i++) {
380,215✔
2498
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
380,215✔
2499
      pColRef = &pEntry->colRef.pColRef[i];
77,263✔
2500
      iColumn = i;
77,263✔
2501
      break;
77,263✔
2502
    }
2503
  }
2504

2505
  if (NULL == pColRef) {
77,263✔
2506
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2507
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
UNCOV
2508
    metaFetchEntryFree(&pEntry);
×
UNCOV
2509
    metaFetchEntryFree(&pSuper);
×
2510
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2511
  }
2512

2513
  // do update column name
2514
  pEntry->version = version;
77,263✔
2515
  pColRef->hasRef = true;
77,263✔
2516
  pColRef->id = pSchema->pSchema[iColumn].colId;
77,263✔
2517
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
77,263✔
2518
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
77,263✔
2519
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
77,263✔
2520
  pSchema->version++;
77,263✔
2521
  pEntry->colRef.version++;
77,263✔
2522

2523
  // do handle entry
2524
  code = metaHandleEntry2(pMeta, pEntry);
77,263✔
2525
  if (code) {
77,263✔
2526
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2527
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
2528
    metaFetchEntryFree(&pEntry);
×
UNCOV
2529
    metaFetchEntryFree(&pSuper);
×
UNCOV
2530
    TAOS_RETURN(code);
×
2531
  } else {
2532
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
77,263✔
2533
             pEntry->uid, version);
2534
  }
2535

2536
  // build response
2537
  code = metaUpdateVtbMetaRsp(
154,526✔
2538
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
77,263✔
2539
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
77,263✔
2540
      pEntry->type);
77,263✔
2541
  if (code) {
77,263✔
UNCOV
2542
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2543
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2544
  } else {
2545
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
571,605✔
2546
      SColRef *p = &pEntry->colRef.pColRef[i];
494,342✔
2547
      pRsp->pColRefs[i].hasRef = p->hasRef;
494,342✔
2548
      pRsp->pColRefs[i].id = p->id;
494,342✔
2549
      if (p->hasRef) {
494,342✔
2550
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
340,437✔
2551
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
340,437✔
2552
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
340,437✔
2553
      }
2554
    }
2555
  }
2556

2557
  metaFetchEntryFree(&pEntry);
77,263✔
2558
  metaFetchEntryFree(&pSuper);
77,263✔
2559
  TAOS_RETURN(code);
77,263✔
2560
}
2561

2562
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
57,858✔
2563
  int32_t code = TSDB_CODE_SUCCESS;
57,858✔
2564

2565
  // check request
2566
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
57,858✔
2567
  if (code) {
57,858✔
UNCOV
2568
    TAOS_RETURN(code);
×
2569
  }
2570

2571
  // fetch old entry
2572
  SMetaEntry *pEntry = NULL;
57,858✔
2573
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
57,858✔
2574
  if (code) {
57,858✔
UNCOV
2575
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2576
              __FILE__, __LINE__, pReq->tbName, version);
UNCOV
2577
    TAOS_RETURN(code);
×
2578
  }
2579

2580
  if (pEntry->version >= version) {
57,858✔
UNCOV
2581
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2582
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
UNCOV
2583
    metaFetchEntryFree(&pEntry);
×
UNCOV
2584
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2585
  }
2586

2587
  // fetch super entry
2588
  SMetaEntry *pSuper = NULL;
57,858✔
2589
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
57,858✔
2590
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
28,760✔
2591
    if (code) {
28,760✔
2592
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2593
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
UNCOV
2594
      metaFetchEntryFree(&pEntry);
×
UNCOV
2595
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2596
    }
2597
  }
2598

2599
  // search the column to update
2600
  SSchemaWrapper *pSchema =
57,858✔
2601
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
57,858✔
2602
  SColRef *pColRef = NULL;
57,858✔
2603
  int32_t  iColumn = 0;
57,858✔
2604
  for (int32_t i = 0; i < pSchema->nCols; i++) {
231,526✔
2605
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
231,526✔
2606
      pColRef = &pEntry->colRef.pColRef[i];
57,858✔
2607
      iColumn = i;
57,858✔
2608
      break;
57,858✔
2609
    }
2610
  }
2611

2612
  if (NULL == pColRef) {
57,858✔
UNCOV
2613
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2614
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2615
    metaFetchEntryFree(&pEntry);
×
UNCOV
2616
    metaFetchEntryFree(&pSuper);
×
2617
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2618
  }
2619

2620
  // do update column name
2621
  pEntry->version = version;
57,858✔
2622
  pColRef->hasRef = false;
57,858✔
2623
  pColRef->id = pSchema->pSchema[iColumn].colId;
57,858✔
2624
  pSchema->version++;
57,858✔
2625
  pEntry->colRef.version++;
57,858✔
2626

2627
  // do handle entry
2628
  code = metaHandleEntry2(pMeta, pEntry);
57,858✔
2629
  if (code) {
57,858✔
UNCOV
2630
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2631
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
UNCOV
2632
    metaFetchEntryFree(&pEntry);
×
2633
    metaFetchEntryFree(&pSuper);
×
2634
    TAOS_RETURN(code);
×
2635
  } else {
2636
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
57,858✔
2637
             pEntry->uid, version);
2638
  }
2639

2640
  // build response
2641
  code = metaUpdateVtbMetaRsp(
115,716✔
2642
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
57,858✔
2643
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
57,858✔
2644
      pEntry->type);
57,858✔
2645
  if (code) {
57,858✔
UNCOV
2646
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2647
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2648
  } else {
2649
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
360,242✔
2650
      SColRef *p = &pEntry->colRef.pColRef[i];
302,384✔
2651
      pRsp->pColRefs[i].hasRef = p->hasRef;
302,384✔
2652
      pRsp->pColRefs[i].id = p->id;
302,384✔
2653
      if (p->hasRef) {
302,384✔
2654
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
151,769✔
2655
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
151,769✔
2656
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
151,769✔
2657
      }
2658
    }
2659
  }
2660

2661
  metaFetchEntryFree(&pEntry);
57,858✔
2662
  metaFetchEntryFree(&pSuper);
57,858✔
2663
  TAOS_RETURN(code);
57,858✔
2664
}
2665

2666
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
17,702✔
2667
  int32_t code = TSDB_CODE_SUCCESS;
17,702✔
2668

2669
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
17,702✔
UNCOV
2670
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2671
              __FILE__, __LINE__, version);
UNCOV
2672
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2673
  }
2674

2675
  SMetaEntry *pEntry = NULL;
17,702✔
2676
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
17,702✔
2677
  if (code) {
17,702✔
UNCOV
2678
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2679
              __FILE__, __LINE__, pReq->name, version);
UNCOV
2680
    TAOS_RETURN(code);
×
2681
  }
2682

2683
  if (pEntry->type != TSDB_SUPER_TABLE) {
17,702✔
UNCOV
2684
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2685
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
UNCOV
2686
    metaFetchEntryFree(&pEntry);
×
UNCOV
2687
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2688
  }
2689

2690
  if (pEntry->uid != pReq->suid) {
17,702✔
UNCOV
2691
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2692
              ", version:%" PRId64,
2693
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
UNCOV
2694
    metaFetchEntryFree(&pEntry);
×
UNCOV
2695
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2696
  }
2697

2698
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2699
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2700
  //   PRId64,
2701
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2702
  //             pReq->schemaTag.version, version);
2703
  //   metaFetchEntryFree(&pEntry);
2704
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2705
  // }
2706

2707
  // do change the entry
2708
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
17,702✔
2709
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
17,702✔
2710
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
17,702✔
UNCOV
2711
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2712
              __func__, __FILE__, __LINE__, pReq->name, version);
UNCOV
2713
    metaFetchEntryFree(&pEntry);
×
UNCOV
2714
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2715
  }
2716

2717
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
17,702✔
UNCOV
2718
    metaError(
×
2719
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2720
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2721
        version);
UNCOV
2722
    metaFetchEntryFree(&pEntry);
×
UNCOV
2723
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2724
  }
2725

2726
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2727
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2728
  //   PRId64,
2729
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2730
  //             pNewTagSchema->version, version);
2731
  //   metaFetchEntryFree(&pEntry);
2732
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2733
  // }
2734

2735
  int32_t numOfChangedTags = 0;
17,702✔
2736
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
190,569✔
2737
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
172,867✔
2738
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
172,867✔
2739

2740
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
172,867✔
2741
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
172,867✔
UNCOV
2742
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2743
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
UNCOV
2744
      metaFetchEntryFree(&pEntry);
×
UNCOV
2745
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2746
    }
2747

2748
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
172,867✔
2749
      numOfChangedTags++;
17,702✔
2750
      SSCHMEA_SET_IDX_ON(pOldColumn);
17,702✔
2751
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
155,165✔
UNCOV
2752
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2753
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
UNCOV
2754
      metaFetchEntryFree(&pEntry);
×
UNCOV
2755
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2756
    }
2757
  }
2758

2759
  if (numOfChangedTags != 1) {
17,702✔
UNCOV
2760
    metaError(
×
2761
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2762
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
UNCOV
2763
    metaFetchEntryFree(&pEntry);
×
UNCOV
2764
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2765
  }
2766

2767
  pEntry->version = version;
17,702✔
2768
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
17,702✔
2769

2770
  // do handle the entry
2771
  code = metaHandleEntry2(pMeta, pEntry);
17,702✔
2772
  if (code) {
17,702✔
UNCOV
2773
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2774
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
UNCOV
2775
    metaFetchEntryFree(&pEntry);
×
UNCOV
2776
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2777
  } else {
2778
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
17,702✔
2779
             pEntry->uid, version);
2780
  }
2781

2782
  metaFetchEntryFree(&pEntry);
17,702✔
2783
  TAOS_RETURN(code);
17,702✔
2784
}
2785

2786
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
14,396✔
2787
  int32_t code = TSDB_CODE_SUCCESS;
14,396✔
2788

2789
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
14,396✔
UNCOV
2790
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2791
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
UNCOV
2792
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2793
  }
2794

2795
  SMetaEntry *pEntry = NULL;
14,396✔
2796
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
14,396✔
2797
  if (code) {
14,396✔
UNCOV
2798
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2799
              __FILE__, __LINE__, pReq->stb, version);
UNCOV
2800
    TAOS_RETURN(code);
×
2801
  }
2802

2803
  if (TSDB_SUPER_TABLE != pEntry->type) {
14,396✔
UNCOV
2804
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2805
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
UNCOV
2806
    metaFetchEntryFree(&pEntry);
×
UNCOV
2807
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2808
  }
2809

2810
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
14,396✔
2811
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
14,396✔
UNCOV
2812
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2813
              __func__, __FILE__, __LINE__, pReq->stb, version);
UNCOV
2814
    metaFetchEntryFree(&pEntry);
×
UNCOV
2815
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2816
  }
2817

2818
  // search and set the tag index off
2819
  int32_t numOfChangedTags = 0;
14,396✔
2820
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
89,200✔
2821
    SSchema *pCol = pTagSchema->pSchema + i;
89,200✔
2822
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
89,200✔
2823
      if (!IS_IDX_ON(pCol)) {
14,396✔
UNCOV
2824
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2825
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
UNCOV
2826
        metaFetchEntryFree(&pEntry);
×
UNCOV
2827
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2828
      }
2829
      numOfChangedTags++;
14,396✔
2830
      SSCHMEA_SET_IDX_OFF(pCol);
14,396✔
2831
      break;
14,396✔
2832
    }
2833
  }
2834

2835
  if (numOfChangedTags != 1) {
14,396✔
UNCOV
2836
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2837
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
UNCOV
2838
    metaFetchEntryFree(&pEntry);
×
UNCOV
2839
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2840
  }
2841

2842
  // do handle the entry
2843
  pEntry->version = version;
14,396✔
2844
  pTagSchema->version++;
14,396✔
2845
  code = metaHandleEntry2(pMeta, pEntry);
14,396✔
2846
  if (code) {
14,396✔
UNCOV
2847
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2848
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
UNCOV
2849
    metaFetchEntryFree(&pEntry);
×
UNCOV
2850
    TAOS_RETURN(code);
×
2851
  } else {
2852
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
14,396✔
2853
             pEntry->uid, version);
2854
  }
2855

2856
  metaFetchEntryFree(&pEntry);
14,396✔
2857
  TAOS_RETURN(code);
14,396✔
2858
}
2859

2860
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
7,537,033✔
2861
  int32_t code = TSDB_CODE_SUCCESS;
7,537,033✔
2862

2863
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
7,537,033✔
2864
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
17,040✔
2865
              __FILE__, __LINE__, version);
2866
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
17,040✔
2867
  }
2868

2869
  SMetaEntry *pEntry = NULL;
7,521,314✔
2870
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
7,536,277✔
2871
  if (code) {
7,534,320✔
UNCOV
2872
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2873
              __FILE__, __LINE__, pReq->name, version);
UNCOV
2874
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2875
  }
2876

2877
  if (pEntry->type != TSDB_SUPER_TABLE) {
7,534,320✔
UNCOV
2878
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2879
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
UNCOV
2880
    metaFetchEntryFree(&pEntry);
×
UNCOV
2881
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2882
  }
2883

2884
  SMetaEntry entry = {
22,557,531✔
2885
      .version = version,
2886
      .type = TSDB_SUPER_TABLE,
2887
      .uid = pReq->suid,
7,529,545✔
2888
      .name = pReq->name,
7,529,851✔
2889
      .stbEntry.schemaRow = pReq->schemaRow,
2890
      .stbEntry.schemaTag = pReq->schemaTag,
2891
      .stbEntry.keep = pReq->keep,
7,518,774✔
2892
      .stbEntry.ownerId = pReq->ownerId,
7,508,641✔
2893
      .colCmpr = pReq->colCmpr,
2894
      .pExtSchemas = pReq->pExtSchemas,
7,526,457✔
2895
  };
2896
  TABLE_SET_COL_COMPRESSED(entry.flags);
7,513,966✔
2897
  if (pReq->virtualStb) {
7,513,966✔
2898
    TABLE_SET_VIRTUAL(entry.flags);
21,288✔
2899
  }
2900
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
7,515,089✔
2901
    TABLE_SET_ROLLUP(entry.flags);
4,482✔
2902
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,482✔
2903
  }
2904

2905
  // do handle the entry
2906
  code = metaHandleEntry2(pMeta, &entry);
7,536,091✔
2907
  if (code) {
7,528,758✔
UNCOV
2908
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2909
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
UNCOV
2910
    metaFetchEntryFree(&pEntry);
×
UNCOV
2911
    TAOS_RETURN(code);
×
2912
  } else {
2913
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
7,528,758✔
2914
             pReq->suid, version);
2915
  }
2916

2917
  metaFetchEntryFree(&pEntry);
7,543,901✔
2918
  TAOS_RETURN(code);
7,542,686✔
2919
}
2920

UNCOV
2921
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
UNCOV
2922
  int32_t code = 0;
×
2923

UNCOV
2924
  if (taosArrayGetSize(uidArray) == 0) {
×
UNCOV
2925
    return TSDB_CODE_SUCCESS;
×
2926
  }
2927

UNCOV
2928
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
UNCOV
2929
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
UNCOV
2930
    SMetaInfo info;
×
UNCOV
2931
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
UNCOV
2932
    if (code) {
×
UNCOV
2933
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
2934
                __func__, __FILE__, __LINE__, uid, code);
UNCOV
2935
      return code;
×
2936
    }
2937

UNCOV
2938
    SMetaEntry entry = {
×
2939
        .version = version,
2940
        .uid = uid,
2941
    };
2942

UNCOV
2943
    if (info.suid == 0) {
×
UNCOV
2944
      entry.type = -TSDB_NORMAL_TABLE;
×
UNCOV
2945
    } else if (info.suid == uid) {
×
UNCOV
2946
      entry.type = -TSDB_SUPER_TABLE;
×
2947
    } else {
UNCOV
2948
      entry.type = -TSDB_CHILD_TABLE;
×
2949
    }
UNCOV
2950
    code = metaHandleEntry2(pMeta, &entry);
×
UNCOV
2951
    if (code) {
×
UNCOV
2952
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
2953
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
UNCOV
2954
      return code;
×
2955
    }
2956
  }
UNCOV
2957
  return code;
×
2958
}
2959

2960
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
39,636✔
2961
  int32_t code = TSDB_CODE_SUCCESS;
39,636✔
2962

2963
  if (NULL == pReq->name || pReq->name[0] == 0) {
39,636✔
UNCOV
2964
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2965
              __LINE__, version);
UNCOV
2966
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2967
  }
2968

2969
  SMetaEntry *pEntry = NULL;
39,636✔
2970
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
39,636✔
2971
  if (code) {
39,636✔
UNCOV
2972
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
2973
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
2974
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
2975
  }
2976

2977
  if (pEntry->type != TSDB_SUPER_TABLE) {
39,636✔
UNCOV
2978
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
2979
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
UNCOV
2980
    metaFetchEntryFree(&pEntry);
×
UNCOV
2981
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2982
  }
2983

2984
  if (pEntry->uid != pReq->tbUid) {
39,636✔
UNCOV
2985
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
2986
              ", version:%" PRId64,
2987
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
UNCOV
2988
    metaFetchEntryFree(&pEntry);
×
UNCOV
2989
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2990
  }
2991

2992
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
39,636✔
2993
    // overwrite the old rsma definition if exists
UNCOV
2994
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
UNCOV
2995
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
2996
  } else {
2997
    TABLE_SET_ROLLUP(pEntry->flags);
39,636✔
2998
  }
2999

3000
  SMetaEntry entry = *pEntry;
39,636✔
3001
  entry.version = version;
39,636✔
3002
  entry.stbEntry.rsmaParam.name = pReq->name;
39,636✔
3003
  entry.stbEntry.rsmaParam.uid = pReq->uid;
39,636✔
3004
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
39,636✔
3005
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
39,636✔
3006
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
39,636✔
3007
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
39,636✔
3008
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
39,636✔
3009
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
39,636✔
3010

3011
  // do handle the entry
3012
  code = metaHandleEntry2(pMeta, &entry);
39,636✔
3013
  if (code) {
39,636✔
UNCOV
3014
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3015
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
UNCOV
3016
    metaFetchEntryFree(&pEntry);
×
UNCOV
3017
    TAOS_RETURN(code);
×
3018
  } else {
3019
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
39,636✔
3020
    pMeta->pVnode->config.isRsma = 1;
39,636✔
3021
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
39,636✔
3022
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3023
  }
3024

3025
  metaFetchEntryFree(&pEntry);
39,636✔
3026
  TAOS_RETURN(code);
39,636✔
3027
}
3028

3029
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
19,950✔
3030
  int32_t code = TSDB_CODE_SUCCESS;
19,950✔
3031

3032
  if (NULL == pReq->name || pReq->name[0] == 0) {
19,950✔
UNCOV
3033
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3034
              __LINE__, version);
UNCOV
3035
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3036
  }
3037

3038
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
19,950✔
UNCOV
3039
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3040
              __LINE__, version);
UNCOV
3041
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3042
  }
3043

3044
  SMetaEntry *pEntry = NULL;
19,950✔
3045
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
19,950✔
3046
  if (code) {
19,950✔
UNCOV
3047
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3048
             __func__, __LINE__, pReq->name, pReq->tbName, version);
UNCOV
3049
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3050
  }
3051

3052
  if (pEntry->type != pReq->tbType) {
19,950✔
UNCOV
3053
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
3054
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
UNCOV
3055
    metaFetchEntryFree(&pEntry);
×
UNCOV
3056
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3057
  }
3058

3059
  if (pEntry->uid != pReq->tbUid) {
19,950✔
UNCOV
3060
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3061
              ", version:%" PRId64,
3062
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
UNCOV
3063
    metaFetchEntryFree(&pEntry);
×
UNCOV
3064
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3065
  }
3066

3067
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
19,950✔
3068
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
19,950✔
3069
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
19,950✔
UNCOV
3070
      metaError(
×
3071
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
3072
          "uid:%" PRIi64 ", version:%" PRId64,
3073
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
3074
          pReq->uid, version);
UNCOV
3075
      metaFetchEntryFree(&pEntry);
×
UNCOV
3076
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3077
    }
3078
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
19,203✔
3079
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
18,456✔
3080
  } else {
UNCOV
3081
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
3082
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
UNCOV
3083
    metaFetchEntryFree(&pEntry);
×
UNCOV
3084
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3085
  }
3086

3087
  SMetaEntry entry = *pEntry;
20,697✔
3088
  entry.version = version;
19,950✔
3089
  TABLE_RESET_ROLLUP(entry.flags);
19,950✔
3090
  entry.stbEntry.rsmaParam.uid = 0;
19,950✔
3091
  entry.stbEntry.rsmaParam.name = NULL;
19,950✔
3092
  entry.stbEntry.rsmaParam.nFuncs = 0;
19,950✔
3093
  entry.stbEntry.rsmaParam.funcColIds = NULL;
19,950✔
3094
  entry.stbEntry.rsmaParam.funcIds = NULL;
19,950✔
3095

3096
  // do handle the entry
3097
  code = metaHandleEntry2(pMeta, &entry);
19,950✔
3098
  if (code) {
19,950✔
UNCOV
3099
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
3100
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
UNCOV
3101
    metaFetchEntryFree(&pEntry);
×
UNCOV
3102
    TAOS_RETURN(code);
×
3103
  } else {
3104
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
19,950✔
UNCOV
3105
      pMeta->pVnode->config.isRsma = 0;
×
3106
    }
3107
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
19,950✔
3108
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3109
  }
3110

3111
  metaFetchEntryFree(&pEntry);
19,950✔
3112
  TAOS_RETURN(code);
19,950✔
3113
}
3114

3115
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
12,216✔
3116
  int32_t code = TSDB_CODE_SUCCESS;
12,216✔
3117

3118
  if (NULL == pReq->name || pReq->name[0] == 0) {
12,216✔
UNCOV
3119
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3120
              __LINE__, version);
UNCOV
3121
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3122
  }
3123

3124
  SMetaEntry *pEntry = NULL;
12,216✔
3125
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
12,216✔
3126
  if (code) {
12,216✔
UNCOV
3127
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
3128
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
3129
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
3130
  }
3131

3132
  if (pEntry->uid != pReq->tbUid) {
12,216✔
UNCOV
3133
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3134
              ", version:%" PRId64,
3135
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
UNCOV
3136
    metaFetchEntryFree(&pEntry);
×
UNCOV
3137
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3138
  }
3139

3140
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
12,216✔
3141
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
12,216✔
3142
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
12,216✔
3143
  } else {
UNCOV
3144
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
3145
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
UNCOV
3146
    metaFetchEntryFree(&pEntry);
×
UNCOV
3147
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3148
  }
3149

3150
  SMetaEntry entry = *pEntry;
12,216✔
3151
  entry.version = version;
12,216✔
3152
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
12,216✔
3153
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
12,216✔
3154
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
12,216✔
3155
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
12,216✔
3156
  }
3157
  // do handle the entry
3158
  code = metaHandleEntry2(pMeta, &entry);
12,216✔
3159
  if (code) {
12,216✔
UNCOV
3160
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3161
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
UNCOV
3162
    metaFetchEntryFree(&pEntry);
×
UNCOV
3163
    TAOS_RETURN(code);
×
3164
  } else {
3165
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
12,216✔
3166
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3167
  }
3168

3169
  metaFetchEntryFree(&pEntry);
12,216✔
3170
  TAOS_RETURN(code);
12,216✔
3171
}
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