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

taosdata / TDengine / #5050

12 May 2026 05:36AM UTC coverage: 73.398% (+0.09%) from 73.313%
#5050

push

travis-ci

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

90 of 101 new or added lines in 2 files covered. (89.11%)

489 existing lines in 125 files now uncovered.

281602 of 383662 relevant lines covered (73.4%)

138099127.08 hits per line

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

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

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

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

37
static int32_t metaValidateVirtualChildColRefReq(SMeta *pMeta, int64_t version, const SVCreateTbReq *pReq,
393,045✔
38
                                                 const SMetaEntry *pStbEntry) {
39
  int32_t expectedCols = pStbEntry->stbEntry.schemaRow.nCols;
393,045✔
40

41
  if (pReq->colRef.nCols != expectedCols) {
393,045✔
42
    metaError("vgId:%d, %s failed at %s:%d since virtual child table %s col ref count %d does not match super table "
×
43
              "%s schema count %d, version:%" PRId64,
44
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->colRef.nCols, pReq->ctb.stbName,
45
              expectedCols, version);
46
    return TSDB_CODE_PAR_INVALID_REF_COLUMN;
×
47
  }
48

49
  if (expectedCols > 0 && pReq->colRef.pColRef == NULL) {
393,045✔
50
    metaError("vgId:%d, %s failed at %s:%d since virtual child table %s col ref is null while super table %s has %d "
×
51
              "columns, version:%" PRId64,
52
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, expectedCols,
53
              version);
54
    return TSDB_CODE_PAR_INVALID_REF_COLUMN;
×
55
  }
56

57
  return TSDB_CODE_SUCCESS;
393,045✔
58
}
59

60
static int32_t metaCheckCreateSuperTableReq(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
6,570,102✔
61
  int32_t   vgId = TD_VID(pMeta->pVnode);
6,570,102✔
62
  void     *value = NULL;
6,574,667✔
63
  int32_t   valueSize = 0;
6,571,566✔
64
  SMetaInfo info;
6,570,195✔
65

66
  // check name
67
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
6,571,247✔
68
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, vgId, __func__, __FILE__, __LINE__,
49,318✔
69
              pReq->name, version);
70
    return TSDB_CODE_INVALID_MSG;
×
71
  }
72

73
  int32_t r = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
6,514,823✔
74
  if (r == 0) {  // name exists, check uid and type
6,531,672✔
75
    int64_t uid = *(tb_uid_t *)value;
5,117✔
76
    tdbFree(value);
5,117✔
77

78
    if (pReq->suid != uid) {
5,117✔
79
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " already exists, request uid:%" PRId64
2,466✔
80
                " version:%" PRId64,
81
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, pReq->suid, version);
82
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
2,466✔
83
    }
84

85
    if (metaGetInfo(pMeta, uid, &info, NULL) == TSDB_CODE_NOT_FOUND) {
2,651✔
86
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
87
                " not found, this is an internal error in meta, version:%" PRId64,
88
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
89
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
90
    }
91

92
    if (info.uid == info.suid) {
2,651✔
93
      return TSDB_CODE_TDB_STB_ALREADY_EXIST;
2,651✔
94
    } else {
95
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64
×
96
                " already exists but not a super table, version:%" PRId64,
97
                vgId, __func__, __FILE__, __LINE__, pReq->name, uid, version);
98
      return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
99
    }
100
  }
101

102
  // check suid
103
  if (metaGetInfo(pMeta, pReq->suid, &info, NULL) != TSDB_CODE_NOT_FOUND) {
6,526,555✔
104
    metaError("vgId:%d, %s failed at %s:%d since table with uid:%" PRId64 " already exist, name:%s version:%" PRId64,
×
105
              vgId, __func__, __FILE__, __LINE__, pReq->suid, pReq->name, version);
106
    return TSDB_CODE_INVALID_MSG;
×
107
  }
108

109
  return TSDB_CODE_SUCCESS;
6,553,252✔
110
}
111

112
static int32_t metaCheckDropTableReq(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,893,994✔
113
  int32_t   code = TSDB_CODE_SUCCESS;
1,893,994✔
114
  void     *value = NULL;
1,893,994✔
115
  int32_t   valueSize = 0;
1,893,994✔
116
  SMetaInfo info;
1,893,754✔
117

118
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,893,994✔
119
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
120
              __FILE__, __LINE__, pReq->name, version);
121
    return TSDB_CODE_INVALID_MSG;
×
122
  }
123

124
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
1,893,994✔
125
  if (TSDB_CODE_SUCCESS != code) {
1,893,994✔
126
    if (pReq->igNotExists) {
×
127
      metaTrace("vgId:%d, %s success since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
128
                pReq->name, version);
129
    } else {
130
      metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
131
                __func__, __FILE__, __LINE__, pReq->name, version);
132
    }
133
    return TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
134
  }
135
  pReq->uid = *(tb_uid_t *)value;
1,893,994✔
136
  tdbFreeClear(value);
1,893,994✔
137

138
  code = metaGetInfo(pMeta, pReq->uid, &info, NULL);
1,893,994✔
139
  if (TSDB_CODE_SUCCESS != code) {
1,893,994✔
140
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
141
              " not found, this is an internal error, version:%" PRId64,
142
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, version);
143
    code = TSDB_CODE_INTERNAL_ERROR;
×
144
    return code;
×
145
  }
146
  pReq->suid = info.suid;
1,893,994✔
147

148
  return code;
1,893,994✔
149
}
150

151
static int32_t metaCheckDropSuperTableReq(SMeta *pMeta, int64_t version, SVDropStbReq *pReq) {
1,047,474✔
152
  int32_t   code = TSDB_CODE_SUCCESS;
1,047,474✔
153
  void     *value = NULL;
1,047,474✔
154
  int32_t   valueSize = 0;
1,048,210✔
155
  SMetaInfo info;
1,046,843✔
156

157
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
1,046,743✔
158
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
9,969✔
159
              __FILE__, __LINE__, pReq->name, version);
160
    return TSDB_CODE_INVALID_MSG;
×
161
  }
162

163
  code = tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize);
1,038,297✔
164
  if (code) {
1,043,427✔
165
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
1,270✔
166
              __FILE__, __LINE__, pReq->name, version);
167
    return TSDB_CODE_TDB_STB_NOT_EXIST;
1,270✔
168
  } else {
169
    int64_t uid = *(int64_t *)value;
1,042,157✔
170
    tdbFreeClear(value);
1,042,617✔
171

172
    if (uid != pReq->suid) {
1,043,737✔
173
      metaError("vgId:%d, %s failed at %s:%d since table %s uid:%" PRId64 " not match, version:%" PRId64,
822✔
174
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
175
      return TSDB_CODE_TDB_STB_NOT_EXIST;
822✔
176
    }
177
  }
178

179
  code = metaGetInfo(pMeta, pReq->suid, &info, NULL);
1,042,014✔
180
  if (code) {
1,042,451✔
181
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
182
              " not found, this is an internal error, version:%" PRId64,
183
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
184
    return TSDB_CODE_INTERNAL_ERROR;
×
185
  }
186
  if (info.suid != info.uid) {
1,042,451✔
187
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a super table, version:%" PRId64,
×
188
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->suid, version);
189
    return TSDB_CODE_INVALID_MSG;
×
190
  }
191
  return code;
1,042,451✔
192
}
193

194
// Create Super Table
195
int32_t metaCreateSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
6,535,736✔
196
  int32_t code = TSDB_CODE_SUCCESS;
6,535,736✔
197

198
  // check request
199
  code = metaCheckCreateSuperTableReq(pMeta, version, pReq);
6,535,736✔
200
  if (code != TSDB_CODE_SUCCESS) {
6,560,790✔
201
    if (code == TSDB_CODE_TDB_STB_ALREADY_EXIST) {
5,117✔
202
      metaWarn("vgId:%d, super table %s uid:%" PRId64 " already exists, version:%" PRId64, TD_VID(pMeta->pVnode),
2,651✔
203
               pReq->name, pReq->suid, version);
204
      TAOS_RETURN(TSDB_CODE_SUCCESS);
2,651✔
205
    } else {
206
      TAOS_RETURN(code);
2,466✔
207
    }
208
  }
209

210
  // handle entry
211
  SMetaEntry entry = {
13,052,041✔
212
      .version = version,
213
      .type = TSDB_SUPER_TABLE,
214
      .uid = pReq->suid,
6,545,604✔
215
      .name = pReq->name,
6,534,534✔
216
      .stbEntry.schemaRow = pReq->schemaRow,
217
      .stbEntry.schemaTag = pReq->schemaTag,
218
      .stbEntry.keep = pReq->keep,
6,523,862✔
219
      .stbEntry.ownerId = pReq->ownerId,
6,506,891✔
220
      .stbEntry.securityLevel = pReq->securityLevel,
6,524,485✔
221
  };
222
  if (pReq->rollup) {
6,514,973✔
223
    TABLE_SET_ROLLUP(entry.flags);
×
224
    entry.stbEntry.rsmaParam = pReq->rsmaParam;
×
225
  }
226
  if (pReq->colCmpred) {
6,508,650✔
227
    TABLE_SET_COL_COMPRESSED(entry.flags);
6,500,295✔
228
    entry.colCmpr = pReq->colCmpr;
6,500,295✔
229
  }
230

231
  entry.pExtSchemas = pReq->pExtSchemas;
6,531,978✔
232

233
  if (pReq->virtualStb) {
6,515,096✔
234
    TABLE_SET_VIRTUAL(entry.flags);
134,098✔
235
  }
236

237
  code = metaHandleEntry2(pMeta, &entry);
6,516,158✔
238
  if (TSDB_CODE_SUCCESS == code) {
6,583,667✔
239
    metaInfo("vgId:%d, super table %s suid:%" PRId64 " is created, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
6,583,667✔
240
             pReq->suid, version);
241
  } else {
242
    metaError("vgId:%d, failed to create stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name,
×
243
              pReq->suid, tstrerror(code));
244
  }
245
  TAOS_RETURN(code);
6,583,667✔
246
}
247

248
// Drop Super Table
249
int32_t metaDropSuperTable(SMeta *pMeta, int64_t verison, SVDropStbReq *pReq) {
1,047,474✔
250
  int32_t code = TSDB_CODE_SUCCESS;
1,047,474✔
251

252
  // check request
253
  code = metaCheckDropSuperTableReq(pMeta, verison, pReq);
1,047,474✔
254
  if (code) {
1,043,617✔
255
    TAOS_RETURN(code);
2,092✔
256
  }
257

258
  // handle entry
259
  SMetaEntry entry = {
1,041,525✔
260
      .version = verison,
261
      .type = -TSDB_SUPER_TABLE,
262
      .uid = pReq->suid,
1,042,522✔
263
  };
264
  code = metaHandleEntry2(pMeta, &entry);
1,038,653✔
265
  if (code) {
1,046,408✔
266
    metaError("vgId:%d, failed to drop stb:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pReq->name, pReq->suid,
×
267
              tstrerror(code));
268
  } else {
269
    metaInfo("vgId:%d, super table %s uid:%" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,046,408✔
270
             pReq->suid, verison);
271
  }
272
  TAOS_RETURN(code);
1,047,135✔
273
}
274

275
// Alter Super Table
276

277
// Create Child Table
278
static int32_t metaCheckCreateChildTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
71,799,378✔
279
  int32_t   code = TSDB_CODE_SUCCESS;
71,799,378✔
280
  void     *value = NULL;
71,799,378✔
281
  int32_t   valueSize = 0;
71,800,395✔
282
  SMetaInfo info;
71,799,762✔
283

284
  if (NULL == pReq->name || strlen(pReq->name) == 0 || NULL == pReq->ctb.stbName || strlen(pReq->ctb.stbName) == 0 ||
71,800,925✔
285
      pReq->ctb.suid == 0) {
71,786,941✔
286
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s stb name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
20,559✔
287
              __func__, __FILE__, __LINE__, pReq->name, pReq->ctb.stbName, version);
288
    return TSDB_CODE_INVALID_MSG;
×
289
  }
290

291
  // check table existence
292
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
71,778,598✔
293
    pReq->uid = *(int64_t *)value;
408,530✔
294
    tdbFreeClear(value);
408,530✔
295

296
    if (metaGetInfo(pMeta, pReq->uid, &info, NULL) != 0) {
408,471✔
297
      metaError("vgId:%d, %s failed at %s:%d since cannot find table with uid %" PRId64
×
298
                ", which is an internal error, version:%" PRId64,
299
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
300
      return TSDB_CODE_INTERNAL_ERROR;
×
301
    }
302

303
    // check table type
304
    if (info.suid == info.uid || info.suid == 0) {
408,412✔
305
      metaError("vgId:%d, %s failed at %s:%d since table with uid %" PRId64 " is not a super table, version:%" PRId64,
2,004✔
306
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->uid, version);
307
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
2,004✔
308
    }
309

310
    // check suid
311
    if (info.suid != pReq->ctb.suid) {
406,408✔
312
      metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " exists in another stable with uid %" PRId64
×
313
                " instead of stable with uid %" PRId64 " version:%" PRId64,
314
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pReq->uid, info.suid, pReq->ctb.suid,
315
                version);
316
      return TSDB_CODE_TDB_TABLE_IN_OTHER_STABLE;
×
317
    }
318

319
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
406,408✔
320
  }
321

322
  // check super table existence
323
  SMetaEntry *pStbEntry = NULL;
71,389,166✔
324
  code = metaFetchEntryByName(pMeta, pReq->ctb.stbName, &pStbEntry);
71,386,551✔
325
  if (code) {
71,377,189✔
326
    metaError("vgId:%d, %s failed at %s:%d since super table %s does not exist, version:%" PRId64,
13✔
327
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
328
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
329
  }
330

331
  if (pStbEntry->type != TSDB_SUPER_TABLE) {
71,377,176✔
332
    metaError("vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64,
×
333
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, version);
334
    metaFetchEntryFree(&pStbEntry);
×
335
    return TSDB_CODE_INVALID_MSG;
×
336
  }
337

338
  if (pStbEntry->uid != pReq->ctb.suid) {
71,372,310✔
339
    metaError("vgId:%d, %s failed at %s:%d since super table %s uid %" PRId64 " does not match request uid %" PRId64
×
340
              ", version:%" PRId64,
341
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->ctb.stbName, pStbEntry->uid, pReq->ctb.suid,
342
              version);
343
    metaFetchEntryFree(&pStbEntry);
×
344
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
345
  }
346

347
  // Check tag value
348
  SSchemaWrapper *pTagSchema = &pStbEntry->stbEntry.schemaTag;
71,376,360✔
349
  const STag     *pTag = (const STag *)pReq->ctb.pTag;
71,383,670✔
350
  if (pTagSchema->nCols != 1 || pTagSchema->pSchema[0].type != TSDB_DATA_TYPE_JSON) {
71,381,062✔
351
    for (int32_t i = 0; i < pTagSchema->nCols; ++i) {
312,550,872✔
352
      STagVal tagVal = {
241,499,498✔
353
          .cid = pTagSchema->pSchema[i].colId,
241,486,459✔
354
      };
355

356
      if (tTagGet(pTag, &tagVal)) {
241,500,134✔
357
        if (pTagSchema->pSchema[i].type != tagVal.type) {
193,668,310✔
358
          metaError("vgId:%d, %s failed at %s:%d since child table %s tag type does not match the expected type, version:%" PRId64,
×
359
                    TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, version);
360
          metaFetchEntryFree(&pStbEntry);
×
361
          return TSDB_CODE_INVALID_MSG;
×
362
        }
363
      }
364
    }
365
  }
366

367
  if (pReq->type == TSDB_VIRTUAL_CHILD_TABLE) {
71,370,112✔
368
    code = metaValidateVirtualChildColRefReq(pMeta, version, pReq, pStbEntry);
393,045✔
369
    if (code) {
393,045✔
370
      metaFetchEntryFree(&pStbEntry);
×
371
      return code;
×
372
    }
373
  }
374

375
  metaFetchEntryFree(&pStbEntry);
71,393,608✔
376

377
  // check grant
378
  if (!metaTbInFilterCache(pMeta, pReq->ctb.stbName, 1)) {
71,340,794✔
379
    code = grantCheck(TSDB_GRANT_TIMESERIES);
71,378,442✔
380
    if (TSDB_CODE_SUCCESS != code) {
71,344,318✔
381
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
382
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
383
    }
384
  }
385
  return code;
71,335,400✔
386
}
387

388
static int32_t metaBuildCreateChildTableRsp(SMeta *pMeta, const SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
70,936,805✔
389
  int32_t code = TSDB_CODE_SUCCESS;
70,936,805✔
390

391
  if (NULL == ppRsp) {
70,936,805✔
392
    return code;
×
393
  }
394

395
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
70,936,805✔
396
  if (NULL == ppRsp) {
70,941,967✔
397
    return terrno;
×
398
  }
399

400
  (*ppRsp)->tableType = TSDB_CHILD_TABLE;
70,941,967✔
401
  (*ppRsp)->tuid = pEntry->uid;
70,952,820✔
402
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
70,963,834✔
403
  tstrncpy((*ppRsp)->tbName, pEntry->name, TSDB_TABLE_NAME_LEN);
70,987,880✔
404

405
  return code;
70,978,827✔
406
}
407

408
static int32_t metaCreateChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
71,401,716✔
409
  int32_t code = TSDB_CODE_SUCCESS;
71,401,716✔
410

411
  // check request
412
  code = metaCheckCreateChildTableReq(pMeta, version, pReq);
71,401,716✔
413
  if (code) {
71,369,573✔
414
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
408,412✔
415
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
2,004✔
416
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
417
    }
418
    return code;
408,353✔
419
  }
420

421
  SMetaEntry entry = {
70,961,161✔
422
      .version = version,
423
      .type = TSDB_CHILD_TABLE,
424
      .uid = pReq->uid,
70,966,676✔
425
      .name = pReq->name,
70,963,087✔
426
      .ctbEntry.btime = pReq->btime,
70,956,953✔
427
      .ctbEntry.ttlDays = pReq->ttl,
70,943,306✔
428
      .ctbEntry.commentLen = pReq->commentLen,
70,950,439✔
429
      .ctbEntry.comment = pReq->comment,
70,956,890✔
430
      .ctbEntry.suid = pReq->ctb.suid,
70,955,335✔
431
      .ctbEntry.pTags = pReq->ctb.pTag,
70,941,849✔
432
  };
433

434
  // build response
435
  code = metaBuildCreateChildTableRsp(pMeta, &entry, ppRsp);
70,936,791✔
436
  if (code) {
70,971,699✔
437
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
438
              tstrerror(code));
439
  }
440

441
  // handle entry
442
  code = metaHandleEntry2(pMeta, &entry);
70,971,699✔
443
  if (TSDB_CODE_SUCCESS == code) {
71,000,401✔
444
    metaInfo("vgId:%d, index:%" PRId64 ", child table is created, tb:%s uid:%" PRId64 " suid:%" PRId64,
71,002,479✔
445
             TD_VID(pMeta->pVnode), version, pReq->name, pReq->uid, pReq->ctb.suid);
446
  } else {
447
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s suid:%" PRId64 " version:%" PRId64,
×
448
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name,
449
              pReq->ctb.suid, version);
450
  }
451
  return code;
71,010,862✔
452
}
453

454
// Drop Child Table
455

456
// Alter Child Table
457

458
// Create Normal Table
459
static int32_t metaCheckCreateNormalTableReq(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq) {
8,957,271✔
460
  int32_t code = 0;
8,957,271✔
461
  void   *value = NULL;
8,957,271✔
462
  int32_t valueSize = 0;
8,957,271✔
463

464
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
8,957,271✔
465
    metaError("vgId:%d, %s failed at %s:%d since invalid name:%s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
466
              __FILE__, __LINE__, pReq->name, version);
467
    return TSDB_CODE_INVALID_MSG;
×
468
  }
469

470
  // check name
471
  if (tdbTbGet(pMeta->pNameIdx, pReq->name, strlen(pReq->name) + 1, &value, &valueSize) == 0) {
8,957,271✔
472
    // for auto create table, we return the uid of the existing table
473
    pReq->uid = *(tb_uid_t *)value;
58,870✔
474
    tdbFree(value);
58,870✔
475
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
58,870✔
476
  }
477

478
  // grant check
479
  code = grantCheck(TSDB_GRANT_TIMESERIES);
8,898,401✔
480
  if (code) {
8,898,401✔
481
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
482
              __FILE__, __LINE__, tstrerror(code), version, pReq->name);
483
  }
484
  return code;
8,898,401✔
485
}
486

487
static int32_t metaBuildCreateNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
8,679,257✔
488
  int32_t code = TSDB_CODE_SUCCESS;
8,679,257✔
489

490
  if (NULL == ppRsp) {
8,679,257✔
491
    return code;
×
492
  }
493

494
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
8,679,257✔
495
  if (NULL == *ppRsp) {
8,679,257✔
496
    return terrno;
×
497
  }
498

499
  code = metaUpdateMetaRsp(pEntry->uid, pEntry->name, &pEntry->ntbEntry.schemaRow, pEntry->ntbEntry.ownerId, *ppRsp);
8,679,257✔
500
  if (code) {
8,679,257✔
501
    taosMemoryFreeClear(*ppRsp);
×
502
    return code;
×
503
  }
504

505
  for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
98,712,215✔
506
    SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
90,032,958✔
507
    (*ppRsp)->pSchemaExt[i].colId = p->id;
90,032,958✔
508
    (*ppRsp)->pSchemaExt[i].compress = p->alg;
90,032,958✔
509
    if (pEntry->pExtSchemas) {
90,032,958✔
510
      (*ppRsp)->pSchemaExt[i].typeMod = pEntry->pExtSchemas[i].typeMod;
694,333✔
511
    }
512
  }
513

514
  return code;
8,679,257✔
515
}
516

517
static int32_t metaCreateNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
8,738,127✔
518
  int32_t code = TSDB_CODE_SUCCESS;
8,738,127✔
519

520
  // check request
521
  code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
8,738,127✔
522
  if (code) {
8,738,127✔
523
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
58,870✔
524
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
525
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
526
    }
527
    TAOS_RETURN(code);
58,870✔
528
  }
529

530
  SMetaEntry entry = {
17,358,064✔
531
      .version = version,
532
      .type = TSDB_NORMAL_TABLE,
533
      .uid = pReq->uid,
8,679,257✔
534
      .name = pReq->name,
8,679,257✔
535
      .ntbEntry.btime = pReq->btime,
8,679,257✔
536
      .ntbEntry.ttlDays = pReq->ttl,
8,679,257✔
537
      .ntbEntry.commentLen = pReq->commentLen,
8,679,257✔
538
      .ntbEntry.comment = pReq->comment,
8,679,257✔
539
      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
540
      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
8,679,257✔
541
      .ntbEntry.ownerId = pReq->ntb.userId,
8,679,257✔
542
      .colCmpr = pReq->colCmpr,
543
      .pExtSchemas = pReq->pExtSchemas,
8,679,257✔
544
  };
545
  TABLE_SET_COL_COMPRESSED(entry.flags);
8,679,257✔
546

547
  // build response
548
  code = metaBuildCreateNormalTableRsp(pMeta, &entry, ppRsp);
8,679,257✔
549
  if (code) {
8,679,257✔
550
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
551
              tstrerror(code));
552
  }
553

554
  // handle entry
555
  code = metaHandleEntry2(pMeta, &entry);
8,679,257✔
556
  if (TSDB_CODE_SUCCESS == code) {
8,679,257✔
557
    metaInfo("vgId:%d, index:%" PRId64 ", normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode), version,
8,679,257✔
558
             pReq->name, pReq->uid);
559
  } else {
560
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
561
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
562
  }
563
  TAOS_RETURN(code);
8,679,257✔
564
}
565

566
static int32_t metaBuildCreateVirtualNormalTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
219,144✔
567
  int32_t code = TSDB_CODE_SUCCESS;
219,144✔
568

569
  if (NULL == ppRsp) {
219,144✔
570
    return code;
×
571
  }
572

573
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
219,144✔
574
  if (NULL == *ppRsp) {
219,144✔
575
    return terrno;
×
576
  }
577

578
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pEntry->ntbEntry.schemaRow, &pEntry->colRef, pEntry->pExtSchemas,
219,144✔
579
                              pEntry->ntbEntry.ownerId, *ppRsp, TSDB_VIRTUAL_NORMAL_TABLE);
580
  if (code) {
219,144✔
581
    taosMemoryFreeClear(*ppRsp);
×
582
    return code;
×
583
  }
584

585
  return code;
219,144✔
586
}
587

588
static int32_t metaCreateVirtualNormalTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
219,144✔
589
  // check request
590
  int32_t code = metaCheckCreateNormalTableReq(pMeta, version, pReq);
219,144✔
591
  if (code) {
219,144✔
592
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
593
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
594
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
595
    }
596
    TAOS_RETURN(code);
×
597
  }
598

599
  SMetaEntry entry = {.version = version,
657,312✔
600
                      .type = TSDB_VIRTUAL_NORMAL_TABLE,
601
                      .uid = pReq->uid,
219,144✔
602
                      .name = pReq->name,
219,144✔
603
                      .ntbEntry.btime = pReq->btime,
219,144✔
604
                      .ntbEntry.ttlDays = pReq->ttl,
219,144✔
605
                      .ntbEntry.commentLen = pReq->commentLen,
219,144✔
606
                      .ntbEntry.comment = pReq->comment,
219,144✔
607
                      .ntbEntry.schemaRow = pReq->ntb.schemaRow,
608
                      .ntbEntry.ncid = pReq->ntb.schemaRow.pSchema[pReq->ntb.schemaRow.nCols - 1].colId + 1,
219,144✔
609
                      .ntbEntry.ownerId = pReq->ntb.userId,
219,144✔
610
                      .pExtSchemas = pReq->pExtSchemas,
219,144✔
611
                      .colRef = pReq->colRef};
612

613
  code = metaBuildCreateVirtualNormalTableRsp(pMeta, &entry, ppRsp);
219,144✔
614
  if (code) {
219,144✔
615
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
616
              tstrerror(code));
617
    TAOS_RETURN(code);
×
618
  }
619

620
  // handle entry
621
  code = metaHandleEntry2(pMeta, &entry);
219,144✔
622
  if (TSDB_CODE_SUCCESS == code) {
219,144✔
623
    metaInfo("vgId:%d, index:%" PRId64 ", virtual normal table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
219,144✔
624
             version, pReq->name, pReq->uid);
625
  } else {
626
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
627
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
628
  }
629
  TAOS_RETURN(code);
219,144✔
630
#if 0
631
  metaTimeSeriesNotifyCheck(pMeta);
632
#endif
633
}
634

635
static int32_t metaBuildCreateVirtualChildTableRsp(SMeta *pMeta, SMetaEntry *pEntry, STableMetaRsp **ppRsp) {
393,045✔
636
  int32_t    code = TSDB_CODE_SUCCESS;
393,045✔
637
  SMetaEntry *pSuper = NULL;
393,045✔
638

639
  if (NULL == ppRsp) {
393,045✔
640
    return code;
×
641
  }
642

643
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
393,045✔
644
  if (NULL == *ppRsp) {
393,045✔
645
    return terrno;
×
646
  }
647

648
  code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
393,045✔
649
  if (code != TSDB_CODE_SUCCESS) {
393,045✔
650
    taosMemoryFreeClear(*ppRsp);
×
651
    return code;
×
652
  }
653

654
  code = metaUpdateVtbMetaRsp(pEntry, pEntry->name, &pSuper->stbEntry.schemaRow, &pEntry->colRef, pSuper->pExtSchemas,
786,030✔
655
                              pSuper->stbEntry.ownerId, *ppRsp, TSDB_VIRTUAL_CHILD_TABLE);
393,045✔
656
  if (code) {
393,045✔
657
    metaFetchEntryFree(&pSuper);
×
658
    taosMemoryFreeClear(*ppRsp);
×
659
    return code;
×
660
  }
661

662
  (*ppRsp)->suid = pEntry->ctbEntry.suid;
393,045✔
663
  metaFetchEntryFree(&pSuper);
393,045✔
664

665
  return code;
393,045✔
666
}
667

668
static int32_t metaCreateVirtualChildTable(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
393,045✔
669
  // check request
670
  int32_t code = metaCheckCreateChildTableReq(pMeta, version, pReq);
393,045✔
671
  if (code) {
393,045✔
672
    if (TSDB_CODE_TDB_TABLE_ALREADY_EXIST != code) {
×
673
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
674
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
675
    }
676
    TAOS_RETURN(code);
×
677
  }
678

679
  SMetaEntry entry = {.version = version,
786,030✔
680
                      .type = TSDB_VIRTUAL_CHILD_TABLE,
681
                      .uid = pReq->uid,
393,045✔
682
                      .name = pReq->name,
393,045✔
683
                      .ctbEntry.btime = pReq->btime,
393,045✔
684
                      .ctbEntry.ttlDays = pReq->ttl,
393,045✔
685
                      .ctbEntry.commentLen = pReq->commentLen,
393,045✔
686
                      .ctbEntry.comment = pReq->comment,
393,045✔
687
                      .ctbEntry.suid = pReq->ctb.suid,
393,045✔
688
                      .ctbEntry.pTags = pReq->ctb.pTag,
393,045✔
689
                      .colRef = pReq->colRef};
690

691
  code = metaBuildCreateVirtualChildTableRsp(pMeta, &entry, ppRsp);
393,045✔
692
  if (code) {
393,045✔
693
    metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__,
×
694
              tstrerror(code));
695
    TAOS_RETURN(code);
×
696
  }
697

698
  // handle entry
699
  code = metaHandleEntry2(pMeta, &entry);
393,045✔
700
  if (TSDB_CODE_SUCCESS == code) {
393,045✔
701
    metaInfo("vgId:%d, index:%" PRId64 ", virtual child table is created, tb:%s uid:%" PRId64, TD_VID(pMeta->pVnode),
393,045✔
702
             version, pReq->name, pReq->uid);
703
  } else {
704
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
705
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
706
  }
707
  TAOS_RETURN(code);
393,045✔
708
#if 0
709
  metaTimeSeriesNotifyCheck(pMeta);
710
#endif
711
}
712

713
// Drop Normal Table
714

715
// Alter Normal Table
716

717
int32_t metaCreateTable2(SMeta *pMeta, int64_t version, SVCreateTbReq *pReq, STableMetaRsp **ppRsp) {
80,752,196✔
718
  int32_t code = TSDB_CODE_SUCCESS;
80,752,196✔
719
  if (TSDB_CHILD_TABLE == pReq->type) {
80,752,196✔
720
    code = metaCreateChildTable(pMeta, version, pReq, ppRsp);
71,408,353✔
721
  } else if (TSDB_NORMAL_TABLE == pReq->type) {
9,350,316✔
722
    code = metaCreateNormalTable(pMeta, version, pReq, ppRsp);
8,738,127✔
723
  } else if (TSDB_VIRTUAL_NORMAL_TABLE == pReq->type) {
612,189✔
724
    code = metaCreateVirtualNormalTable(pMeta, version, pReq, ppRsp);
219,144✔
725
  } else if (TSDB_VIRTUAL_CHILD_TABLE == pReq->type) {
393,045✔
726
    code = metaCreateVirtualChildTable(pMeta, version, pReq, ppRsp);
393,045✔
727
  } else {
728
    code = TSDB_CODE_INVALID_MSG;
×
729
  }
730
  TAOS_RETURN(code);
80,769,906✔
731
}
732

733
int32_t metaDropTable2(SMeta *pMeta, int64_t version, SVDropTbReq *pReq) {
1,893,994✔
734
  int32_t code = TSDB_CODE_SUCCESS;
1,893,994✔
735

736
  // check request
737
  code = metaCheckDropTableReq(pMeta, version, pReq);
1,893,994✔
738
  if (code) {
1,893,994✔
739
    if (TSDB_CODE_TDB_TABLE_NOT_EXIST != code) {
×
740
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
741
                __FILE__, __LINE__, tstrerror(code), version, pReq->name);
742
    }
743
    TAOS_RETURN(code);
×
744
  }
745

746
  if (pReq->suid == pReq->uid) {
1,893,994✔
747
    code = TSDB_CODE_INVALID_PARA;
×
748
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
749
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
750
    TAOS_RETURN(code);
×
751
  }
752

753
  SMetaEntry entry = {
1,893,994✔
754
      .version = version,
755
      .uid = pReq->uid,
1,893,994✔
756
  };
757

758
  if (pReq->isVirtual) {
1,893,994✔
759
    if (pReq->suid == 0) {
77,490✔
760
      entry.type = -TSDB_VIRTUAL_NORMAL_TABLE;
43,526✔
761
    } else {
762
      entry.type = -TSDB_VIRTUAL_CHILD_TABLE;
33,964✔
763
    }
764
  } else {
765
    if (pReq->suid == 0) {
1,816,504✔
766
      entry.type = -TSDB_NORMAL_TABLE;
949,633✔
767
    } else {
768
      entry.type = -TSDB_CHILD_TABLE;
866,871✔
769
    }
770
  }
771
  code = metaHandleEntry2(pMeta, &entry);
1,893,994✔
772
  if (code) {
1,893,994✔
773
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
774
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->uid, pReq->name, version);
775
  } else {
776
    metaInfo("vgId:%d, table %s uid %" PRId64 " is dropped, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
1,893,994✔
777
             pReq->uid, version);
778
  }
779
  TAOS_RETURN(code);
1,893,994✔
780
}
781

782
static int32_t metaCheckAlterTableColumnReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
4,864,886✔
783
  int32_t code = 0;
4,864,886✔
784

785
  if (NULL == pReq->colName || strlen(pReq->colName) == 0) {
4,864,886✔
786
    metaError("vgId:%d, %s failed at %s:%d since invalid column name:%s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
787
              __func__, __FILE__, __LINE__, pReq->colName, version);
788
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
789
  }
790

791
  // check name
792
  void   *value = NULL;
4,864,886✔
793
  int32_t valueSize = 0;
4,864,886✔
794
  code = tdbTbGet(pMeta->pNameIdx, pReq->tbName, strlen(pReq->tbName) + 1, &value, &valueSize);
4,864,886✔
795
  if (code) {
4,864,155✔
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
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
799
    TAOS_RETURN(code);
×
800
  }
801
  int64_t uid = *(int64_t *)value;
4,864,155✔
802
  tdbFreeClear(value);
4,864,155✔
803

804
  // check table type
805
  SMetaInfo info;
4,864,646✔
806
  if (metaGetInfo(pMeta, uid, &info, NULL) != 0) {
4,864,886✔
807
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64
×
808
              " not found, this is an internal error in meta, version:%" PRId64,
809
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
810
    code = TSDB_CODE_INTERNAL_ERROR;
×
811
    TAOS_RETURN(code);
×
812
  }
813
  if (info.suid != 0 && pReq->action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF &&
4,864,886✔
814
      pReq->action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
32,362✔
815
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not a normal table, version:%" PRId64,
×
816
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, uid, version);
817
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
818
    TAOS_RETURN(code);
×
819
  }
820

821
  // check grant
822
  code = grantCheck(TSDB_GRANT_TIMESERIES);
4,864,886✔
823
  if (code) {
4,864,155✔
824
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " name:%s", TD_VID(pMeta->pVnode), __func__,
×
825
              __FILE__, __LINE__, tstrerror(code), version, pReq->tbName);
826
    TAOS_RETURN(code);
×
827
  }
828
  TAOS_RETURN(code);
4,864,155✔
829
}
830

831
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
3,989,382✔
832
  int32_t code = TSDB_CODE_SUCCESS;
3,989,382✔
833

834
  // check request
835
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
3,989,382✔
836
  if (code) {
3,989,382✔
837
    TAOS_RETURN(code);
×
838
  }
839

840
  // fetch old entry
841
  SMetaEntry *pEntry = NULL;
3,989,382✔
842
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
3,989,382✔
843
  if (code) {
3,989,382✔
844
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
845
              __FILE__, __LINE__, pReq->tbName, version);
846
    TAOS_RETURN(code);
×
847
  }
848
  if (pEntry->version >= version) {
3,989,382✔
849
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
850
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
851
    metaFetchEntryFree(&pEntry);
×
852
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
853
  }
854

855
  // do add column
856
  int32_t         rowSize = 0;
3,989,382✔
857
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
3,989,382✔
858
  SSchema        *pColumn;
859
  SExtSchema      extSchema = {0};
3,989,382✔
860
  pEntry->version = version;
3,989,382✔
861
  for (int32_t i = 0; i < pSchema->nCols; i++) {
2,147,483,647✔
862
    pColumn = &pSchema->pSchema[i];
2,147,483,647✔
863
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
2,147,483,647✔
864
      metaError("vgId:%d, %s failed at %s:%d since column %s already exists in table %s, version:%" PRId64,
358,921✔
865
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
866
      metaFetchEntryFree(&pEntry);
358,921✔
867
      TAOS_RETURN(TSDB_CODE_VND_COL_ALREADY_EXISTS);
358,921✔
868
    }
869
    rowSize += pColumn->bytes;
2,147,483,647✔
870
  }
871

872
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
3,630,461✔
873
  if (rowSize + pReq->bytes > maxBytesPerRow) {
3,630,461✔
874
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
7,310✔
875
              __func__, __FILE__, __LINE__, rowSize, pReq->bytes, maxBytesPerRow, version);
876
    metaFetchEntryFree(&pEntry);
7,310✔
877
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
7,310✔
878
  }
879

880
  int32_t maxCols = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_COLUMNS : TSDB_MAX_COLUMNS_NON_VIRTUAL;
3,623,151✔
881
  if (pSchema->nCols + 1 > maxCols) {
3,623,151✔
882
    metaError("vgId:%d, %s failed at %s:%d since column count %d + 1 > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
×
883
              __func__, __FILE__, __LINE__, pSchema->nCols, maxCols, version);
884
    metaFetchEntryFree(&pEntry);
×
885
    TAOS_RETURN(TSDB_CODE_PAR_TOO_MANY_COLUMNS);
×
886
  }
887

888
  SSchema *pNewSchema = taosMemoryRealloc(pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols + 1));
3,623,151✔
889
  if (NULL == pNewSchema) {
3,623,151✔
890
    metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
891
              __LINE__, tstrerror(terrno), version);
892
    metaFetchEntryFree(&pEntry);
×
893
    TAOS_RETURN(terrno);
×
894
  }
895
  pSchema->pSchema = pNewSchema;
3,623,151✔
896
  pSchema->version++;
3,623,151✔
897
  pSchema->nCols++;
3,623,151✔
898
  pColumn = &pSchema->pSchema[pSchema->nCols - 1];
3,623,151✔
899
  pColumn->bytes = pReq->bytes;
3,623,151✔
900
  pColumn->type = pReq->type;
3,623,151✔
901
  pColumn->flags = pReq->flags;
3,623,151✔
902
  if (pEntry->ntbEntry.ncid > INT16_MAX) {
3,623,151✔
903
    metaError("vgId:%d, %s failed at %s:%d since column id %d exceeds max column id %d, version:%" PRId64,
634✔
904
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ntbEntry.ncid, INT16_MAX,
905
              version);
906
    metaFetchEntryFree(&pEntry);
634✔
907
    TAOS_RETURN(TSDB_CODE_VND_EXCEED_MAX_COL_ID);
634✔
908
  }
909
  pColumn->colId = pEntry->ntbEntry.ncid++;
3,622,517✔
910
  extSchema.typeMod = pReq->typeMod;
3,622,517✔
911
  tstrncpy(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN);
3,622,517✔
912
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,622,517✔
913
    SColRef tmpRef;
62,606✔
914
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
62,666✔
915
      tmpRef.hasRef = false;
36,124✔
916
      tmpRef.id = pColumn->colId;
36,124✔
917
    } else {
918
      tmpRef.hasRef = true;
26,542✔
919
      tmpRef.id = pColumn->colId;
26,542✔
920
      tstrncpy(tmpRef.refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
26,542✔
921
      tstrncpy(tmpRef.refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
26,542✔
922
      tstrncpy(tmpRef.refColName, pReq->refColName, TSDB_COL_NAME_LEN);
26,542✔
923
    }
924
    code = updataTableColRef(&pEntry->colRef, pColumn, 1, &tmpRef);
62,666✔
925
    if (code) {
62,666✔
926
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
927
                __LINE__, tstrerror(code), version);
928
      metaFetchEntryFree(&pEntry);
×
929
      TAOS_RETURN(code);
×
930
    }
931
  } else {
932
    uint32_t compress;
933
    if (TSDB_ALTER_TABLE_ADD_COLUMN == pReq->action) {
3,559,851✔
934
      compress = createDefaultColCmprByType(pColumn->type);
3,551,935✔
935
    } else {
936
      compress = pReq->compress;
7,916✔
937
    }
938
    code = updataTableColCmpr(&pEntry->colCmpr, pColumn, 1, compress);
3,559,851✔
939
    if (code) {
3,559,851✔
940
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
941
                __LINE__, tstrerror(code), version);
942
      metaFetchEntryFree(&pEntry);
×
943
      TAOS_RETURN(code);
×
944
    }
945
  }
946
  code = addTableExtSchema(pEntry, pColumn, pSchema->nCols, &extSchema);
3,622,517✔
947
  if (code) {
3,622,517✔
948
    metaError("vgId:%d, %s failed to add ext schema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
949
              __func__, __FILE__, __LINE__, tstrerror(code), version);
950
    metaFetchEntryFree(&pEntry);
×
951
    TAOS_RETURN(code);
×
952
  }
953

954
  // do handle entry
955
  code = metaHandleEntry2(pMeta, pEntry);
3,622,517✔
956
  if (code) {
3,622,517✔
957
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
958
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
959
    metaFetchEntryFree(&pEntry);
×
960
    TAOS_RETURN(code);
×
961
  } else {
962
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
3,622,517✔
963
             pEntry->uid, version);
964
  }
965

966
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
3,622,517✔
967
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
62,666✔
968
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
62,666✔
969
    if (code) {
62,666✔
970
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
971
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
972
    } else {
973
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
21,233,412✔
974
        SColRef *p = &pEntry->colRef.pColRef[i];
21,170,746✔
975
        pRsp->pColRefs[i].hasRef = p->hasRef;
21,170,746✔
976
        pRsp->pColRefs[i].id = p->id;
21,170,746✔
977
        if (p->hasRef) {
21,170,746✔
978
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
200,132✔
979
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
200,132✔
980
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
200,132✔
981
        }
982
      }
983
    }
984
  } else {
985
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
3,559,851✔
986
    if (code) {
3,559,851✔
987
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
988
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
989
    } else {
990
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
2,147,483,647✔
991
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
2,147,483,647✔
992
        pRsp->pSchemaExt[i].colId = p->id;
2,147,483,647✔
993
        pRsp->pSchemaExt[i].compress = p->alg;
2,147,483,647✔
994
      }
995
    }
996
  }
997

998
  metaFetchEntryFree(&pEntry);
3,622,517✔
999
  TAOS_RETURN(code);
3,622,517✔
1000
}
1001

1002
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
108,256✔
1003
  int32_t code = TSDB_CODE_SUCCESS;
108,256✔
1004

1005
  // check request
1006
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
108,256✔
1007
  if (code) {
108,256✔
1008
    TAOS_RETURN(code);
×
1009
  }
1010

1011
  // fetch old entry
1012
  SMetaEntry *pEntry = NULL;
108,256✔
1013
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
108,256✔
1014
  if (code) {
108,256✔
1015
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1016
              __FILE__, __LINE__, pReq->tbName, version);
1017
    TAOS_RETURN(code);
×
1018
  }
1019

1020
  if (pEntry->version >= version) {
108,256✔
1021
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1022
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1023
    metaFetchEntryFree(&pEntry);
×
1024
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1025
  }
1026

1027
  // search the column to drop
1028
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
108,256✔
1029
  SSchema        *pColumn = NULL;
108,256✔
1030
  SSchema         tColumn;
108,136✔
1031
  int32_t         iColumn = 0;
108,256✔
1032
  for (; iColumn < pSchema->nCols; iColumn++) {
76,062,251✔
1033
    pColumn = &pSchema->pSchema[iColumn];
76,062,251✔
1034
    if (strncmp(pColumn->name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
76,062,251✔
1035
      break;
108,256✔
1036
    }
1037
  }
1038

1039
  if (iColumn == pSchema->nCols) {
108,256✔
1040
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1041
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1042
    metaFetchEntryFree(&pEntry);
×
1043
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1044
  }
1045

1046
  if (pColumn->colId == 0 || pColumn->flags & COL_IS_KEY) {
108,256✔
1047
    metaError("vgId:%d, %s failed at %s:%d since column %s is primary key, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1048
              __func__, __FILE__, __LINE__, pReq->colName, version);
1049
    metaFetchEntryFree(&pEntry);
×
1050
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1051
  }
1052

1053
  tColumn = *pColumn;
108,256✔
1054

1055
  // do drop column
1056
  pEntry->version = version;
108,256✔
1057
  if (pSchema->nCols - iColumn - 1 > 0) {
108,256✔
1058
    memmove(pColumn, pColumn + 1, (pSchema->nCols - iColumn - 1) * sizeof(SSchema));
82,994✔
1059
  }
1060
  pSchema->nCols--;
108,256✔
1061
  pSchema->version++;
108,256✔
1062
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
108,256✔
1063
    code = updataTableColRef(&pEntry->colRef, &tColumn, 0, NULL);
34,246✔
1064
    if (code) {
34,246✔
1065
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1066
                __LINE__, tstrerror(code), version);
1067
      metaFetchEntryFree(&pEntry);
×
1068
      TAOS_RETURN(code);
×
1069
    }
1070
    if (pEntry->colRef.nCols != pSchema->nCols) {
34,246✔
1071
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1072
                __func__, __FILE__, __LINE__, version);
1073
      metaFetchEntryFree(&pEntry);
×
1074
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1075
    }
1076
  } else {
1077
    code = updataTableColCmpr(&pEntry->colCmpr, &tColumn, 0, 0);
74,010✔
1078
    if (code) {
74,010✔
1079
      metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
1080
                __LINE__, tstrerror(code), version);
1081
      metaFetchEntryFree(&pEntry);
×
1082
      TAOS_RETURN(code);
×
1083
    }
1084
    if (pEntry->colCmpr.nCols != pSchema->nCols) {
74,010✔
1085
      metaError("vgId:%d, %s failed at %s:%d since column count mismatch, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1086
                __func__, __FILE__, __LINE__, version);
1087
      metaFetchEntryFree(&pEntry);
×
1088
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
1089
    }
1090
  }
1091

1092
  // update column extschema
1093
  code = dropTableExtSchema(pEntry, iColumn, pSchema->nCols);
108,256✔
1094
  if (code) {
108,256✔
1095
    metaError("vgId:%d, %s failed to remove extschema at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1096
              __func__, __FILE__, __LINE__, tstrerror(code), version);
1097
    metaFetchEntryFree(&pEntry);
×
1098
    TAOS_RETURN(code);
×
1099
  }
1100

1101
  // do handle entry
1102
  code = metaHandleEntry2(pMeta, pEntry);
108,256✔
1103
  if (code) {
108,256✔
1104
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1105
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1106
    metaFetchEntryFree(&pEntry);
×
1107
    TAOS_RETURN(code);
×
1108
  } else {
1109
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
108,256✔
1110
             pEntry->uid, version);
1111
  }
1112

1113
  // build response
1114
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
108,256✔
1115
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
34,246✔
1116
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
34,246✔
1117
    if (code) {
34,246✔
1118
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1119
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1120
    } else {
1121
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
41,765,242✔
1122
        SColRef *p = &pEntry->colRef.pColRef[i];
41,730,996✔
1123
        pRsp->pColRefs[i].hasRef = p->hasRef;
41,730,996✔
1124
        pRsp->pColRefs[i].id = p->id;
41,730,996✔
1125
        if (p->hasRef) {
41,730,996✔
1126
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
20,880,840✔
1127
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
20,880,840✔
1128
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
20,880,840✔
1129
        }
1130
      }
1131
    }
1132
  } else {
1133
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
74,010✔
1134
    if (code) {
74,010✔
1135
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1136
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1137
    } else {
1138
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
59,308,198✔
1139
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
59,234,188✔
1140
        pRsp->pSchemaExt[i].colId = p->id;
59,234,188✔
1141
        pRsp->pSchemaExt[i].compress = p->alg;
59,234,188✔
1142
      }
1143
    }
1144
  }
1145

1146
  metaFetchEntryFree(&pEntry);
108,256✔
1147
  TAOS_RETURN(code);
108,256✔
1148
}
1149

1150
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
50,278✔
1151
  int32_t code = TSDB_CODE_SUCCESS;
50,278✔
1152

1153
  // check request
1154
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
50,278✔
1155
  if (code) {
50,278✔
1156
    TAOS_RETURN(code);
×
1157
  }
1158

1159
  if (NULL == pReq->colNewName) {
50,278✔
1160
    metaError("vgId:%d, %s failed at %s:%d since invalid new column name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
1161
              __func__, __FILE__, __LINE__, version);
1162
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
1163
  }
1164

1165
  // fetch old entry
1166
  SMetaEntry *pEntry = NULL;
50,278✔
1167
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
50,278✔
1168
  if (code) {
50,278✔
1169
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1170
              __FILE__, __LINE__, pReq->tbName, version);
1171
    TAOS_RETURN(code);
×
1172
  }
1173

1174
  if (pEntry->version >= version) {
50,278✔
1175
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1176
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1177
    metaFetchEntryFree(&pEntry);
×
1178
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1179
  }
1180

1181
  // search the column to update
1182
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
50,278✔
1183
  SSchema        *pColumn = NULL;
50,278✔
1184
  int32_t         iColumn = 0;
50,278✔
1185
  for (int32_t i = 0; i < pSchema->nCols; i++) {
231,030✔
1186
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
231,030✔
1187
      pColumn = &pSchema->pSchema[i];
50,278✔
1188
      iColumn = i;
50,278✔
1189
      break;
50,278✔
1190
    }
1191
  }
1192

1193
  if (NULL == pColumn) {
50,278✔
1194
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
1195
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
1196
    metaFetchEntryFree(&pEntry);
×
1197
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1198
  }
1199

1200

1201
  // do update column name
1202
  pEntry->version = version;
50,278✔
1203
  tstrncpy(pColumn->name, pReq->colNewName, TSDB_COL_NAME_LEN);
50,278✔
1204
  pSchema->version++;
50,278✔
1205

1206
  // do handle entry
1207
  code = metaHandleEntry2(pMeta, pEntry);
50,278✔
1208
  if (code) {
50,278✔
1209
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1210
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1211
    metaFetchEntryFree(&pEntry);
×
1212
    TAOS_RETURN(code);
×
1213
  } else {
1214
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
50,278✔
1215
             pEntry->uid, version);
1216
  }
1217

1218
  // build response
1219
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
50,278✔
1220
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
31,210✔
1221
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
31,210✔
1222
    if (code) {
31,210✔
1223
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1224
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1225
    } else {
1226
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
220,308✔
1227
        SColRef *p = &pEntry->colRef.pColRef[i];
189,098✔
1228
        pRsp->pColRefs[i].hasRef = p->hasRef;
189,098✔
1229
        pRsp->pColRefs[i].id = p->id;
189,098✔
1230
        if (p->hasRef) {
189,098✔
1231
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
116,734✔
1232
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
116,734✔
1233
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
116,734✔
1234
        }
1235
      }
1236
    }
1237
  } else {
1238
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
19,068✔
1239
    if (code) {
19,068✔
1240
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1241
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1242
    } else {
1243
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
204,983✔
1244
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
185,915✔
1245
        pRsp->pSchemaExt[i].colId = p->id;
185,915✔
1246
        pRsp->pSchemaExt[i].compress = p->alg;
185,915✔
1247
      }
1248
    }
1249
  }
1250

1251
  metaFetchEntryFree(&pEntry);
50,278✔
1252
  TAOS_RETURN(code);
50,278✔
1253
}
1254

1255
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
564,009✔
1256
  int32_t code = TSDB_CODE_SUCCESS;
564,009✔
1257

1258
  // check request
1259
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
564,009✔
1260
  if (code) {
564,009✔
1261
    TAOS_RETURN(code);
×
1262
  }
1263

1264
  // fetch old entry
1265
  SMetaEntry *pEntry = NULL;
564,009✔
1266
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
563,278✔
1267
  if (code) {
564,009✔
1268
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
1269
              __FILE__, __LINE__, pReq->tbName, version);
1270
    TAOS_RETURN(code);
×
1271
  }
1272

1273
  if (pEntry->version >= version) {
564,009✔
1274
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
1275
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
1276
    metaFetchEntryFree(&pEntry);
×
1277
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
1278
  }
1279

1280
  // search the column to update
1281
  SSchemaWrapper *pSchema = &pEntry->ntbEntry.schemaRow;
564,009✔
1282
  SSchema        *pColumn = NULL;
564,009✔
1283
  int32_t         iColumn = 0;
564,009✔
1284
  int32_t         rowSize = 0;
564,009✔
1285
  for (int32_t i = 0; i < pSchema->nCols; i++) {
42,915,476✔
1286
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
42,351,467✔
1287
      pColumn = &pSchema->pSchema[i];
564,009✔
1288
      iColumn = i;
564,009✔
1289
    }
1290
    rowSize += pSchema->pSchema[i].bytes;
42,351,467✔
1291
  }
1292

1293
  if (NULL == pColumn) {
564,009✔
1294
    metaError("vgId:%d, %s failed at %s:%d since column %s not found in table %s, version:%" PRId64,
×
1295
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pReq->tbName, version);
1296
    metaFetchEntryFree(&pEntry);
×
1297
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
1298
  }
1299

1300
  if (!IS_VAR_DATA_TYPE(pColumn->type) || pColumn->bytes >= pReq->colModBytes) {
564,009✔
1301
    metaError("vgId:%d, %s failed at %s:%d since column %s is not var data type or bytes %d >= %d, version:%" PRId64,
228,072✔
1302
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colName, pColumn->bytes, pReq->colModBytes,
1303
              version);
1304
    metaFetchEntryFree(&pEntry);
228,072✔
1305
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
228,072✔
1306
  }
1307

1308
  int32_t maxBytesPerRow = pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE ? TSDB_MAX_BYTES_PER_ROW_VIRTUAL : TSDB_MAX_BYTES_PER_ROW;
335,937✔
1309
  if (rowSize + pReq->colModBytes - pColumn->bytes > maxBytesPerRow) {
335,937✔
1310
    metaError("vgId:%d, %s failed at %s:%d since row size %d + %d - %d > %d, version:%" PRId64, TD_VID(pMeta->pVnode),
51,170✔
1311
              __func__, __FILE__, __LINE__, rowSize, pReq->colModBytes, pColumn->bytes, maxBytesPerRow,
1312
              version);
1313
    metaFetchEntryFree(&pEntry);
51,170✔
1314
    TAOS_RETURN(TSDB_CODE_PAR_INVALID_ROW_LENGTH);
51,170✔
1315
  }
1316

1317
  // do change the column bytes
1318
  pEntry->version = version;
284,767✔
1319
  pSchema->version++;
284,767✔
1320
  pColumn->bytes = pReq->colModBytes;
284,767✔
1321

1322
  // do handle entry
1323
  code = metaHandleEntry2(pMeta, pEntry);
284,767✔
1324
  if (code) {
284,767✔
1325
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1326
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1327
    metaFetchEntryFree(&pEntry);
×
1328
    TAOS_RETURN(code);
×
1329
  } else {
1330
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
284,767✔
1331
             pEntry->uid, version);
1332
  }
1333

1334
  // build response
1335
  if (pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
284,767✔
1336
    code = metaUpdateVtbMetaRsp(pEntry, pReq->tbName, pSchema, &pEntry->colRef, pEntry->pExtSchemas,
30,236✔
1337
                                pEntry->ntbEntry.ownerId, pRsp, pEntry->type);
30,236✔
1338
    if (code) {
30,236✔
1339
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1340
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1341
    } else {
1342
      for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
20,980,206✔
1343
        SColRef *p = &pEntry->colRef.pColRef[i];
20,949,970✔
1344
        pRsp->pColRefs[i].hasRef = p->hasRef;
20,949,970✔
1345
        pRsp->pColRefs[i].id = p->id;
20,949,970✔
1346
        if (p->hasRef) {
20,949,970✔
1347
          tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
88,084✔
1348
          tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
88,084✔
1349
          tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
88,084✔
1350
        }
1351
      }
1352
    }
1353
  } else {
1354
    code = metaUpdateMetaRsp(pEntry->uid, pReq->tbName, pSchema, pEntry->ntbEntry.ownerId, pRsp);
254,531✔
1355
    if (code) {
254,531✔
1356
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
1357
                __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
1358
    } else {
1359
      for (int32_t i = 0; i < pEntry->colCmpr.nCols; i++) {
10,207,106✔
1360
        SColCmpr *p = &pEntry->colCmpr.pColCmpr[i];
9,952,575✔
1361
        pRsp->pSchemaExt[i].colId = p->id;
9,952,575✔
1362
        pRsp->pSchemaExt[i].compress = p->alg;
9,952,575✔
1363
      }
1364
    }
1365
  }
1366

1367
  metaFetchEntryFree(&pEntry);
284,767✔
1368
  TAOS_RETURN(code);
284,767✔
1369
}
1370

1371

1372

1373
static bool tagValueChanged(const SUpdatedTagVal* pNewVal, const STag *pOldTag) {
8,956,695✔
1374
  STagVal oldVal = { .cid = pNewVal->colId };
8,956,695✔
1375

1376
  if (pNewVal->isNull) {
8,958,711✔
1377
    return tTagGet(pOldTag, &oldVal);
75,378✔
1378
  }
1379

1380
  if (!tTagGet(pOldTag, &oldVal)){
8,884,839✔
1381
    return true;
219,555✔
1382
  }
1383

1384
  if (!IS_VAR_DATA_TYPE(oldVal.type)) {
8,662,272✔
1385
    return (memcmp(&oldVal.i64, pNewVal->pTagVal, pNewVal->nTagVal) != 0);
8,410,022✔
1386
  }
1387

1388
  if (oldVal.nData != pNewVal->nTagVal) {
252,250✔
1389
    return true;
192,145✔
1390
  }
1391

1392
  return (memcmp(pNewVal->pTagVal, oldVal.pData, oldVal.nData) != 0);
60,858✔
1393
}
1394

1395

1396

1397
static int32_t updatedTagValueArrayToHashMap(SSchemaWrapper* pTagSchema, SArray* arr, SHashObj **hashMap) {
8,774,015✔
1398
  int32_t numOfTags = arr == NULL ? 0 : taosArrayGetSize(arr);
8,774,015✔
1399
  if (numOfTags == 0) {
8,772,590✔
1400
    metaError("%s failed at %s:%d since no tags specified", __func__, __FILE__, __LINE__);
×
1401
    return TSDB_CODE_INVALID_MSG;
×
1402
  }
1403

1404
  *hashMap = taosHashInit(numOfTags, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
8,772,590✔
1405
  if (*hashMap == NULL) {
8,774,199✔
1406
    metaError("%s failed at %s:%d since %s", __func__, __FILE__, __LINE__, tstrerror(terrno));
×
1407
    return terrno;
×
1408
  }
1409

1410
  for (int32_t i = 0; i < taosArrayGetSize(arr); i++) {
17,621,425✔
1411
    SUpdatedTagVal *pTagVal = taosArrayGet(arr, i);
8,849,138✔
1412
    if (taosHashGet(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId)) != NULL) {
8,849,463✔
1413
      metaError("%s failed at %s:%d since duplicate tags %s", __func__, __FILE__, __LINE__, pTagVal->tagName);
×
1414
      taosHashCleanup(*hashMap);
×
1415
      return TSDB_CODE_INVALID_MSG;
×
1416
    }
1417

1418
    int32_t code = taosHashPut(*hashMap, &pTagVal->colId, sizeof(pTagVal->colId), pTagVal, sizeof(*pTagVal));
8,846,022✔
1419
    if (code) {
8,847,226✔
1420
      metaError("%s failed at %s:%d since %s", __func__, __FILE__, __LINE__, tstrerror(code));
×
1421
      taosHashCleanup(*hashMap);
×
1422
      return code;
×
1423
    }
1424
  }
1425

1426
  int32_t changed = 0;
8,772,959✔
1427
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
19,009,914✔
1428
    int32_t schemaColId = pTagSchema->pSchema[i].colId;
10,236,202✔
1429
    if (taosHashGet(*hashMap, &schemaColId, sizeof(schemaColId)) != NULL) {
10,238,195✔
1430
      changed++;
8,849,138✔
1431
    }
1432
  }
1433
  if (changed < numOfTags) {
8,774,465✔
1434
    metaError("%s failed at %s:%d since tag count mismatch, %d:%d", __func__, __FILE__, __LINE__, changed, numOfTags);
×
1435
    taosHashCleanup(*hashMap);
×
1436
    return TSDB_CODE_VND_COL_NOT_EXISTS;
×
1437
  }
1438

1439
  return TSDB_CODE_SUCCESS;
8,774,465✔
1440
}
1441

1442

1443

1444
static int32_t metaUpdateTableJsonTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
134,508✔
1445
  SSchema *pCol = &pTagSchema->pSchema[0];
134,508✔
1446
  int32_t colId = pCol->colId;
134,508✔
1447
  SUpdatedTagVal *pTagVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
134,508✔
1448
  void *pNewTag = taosMemoryRealloc(pTable->ctbEntry.pTags, pTagVal->nTagVal);
134,508✔
1449
  if (pNewTag == NULL) {
134,508✔
1450
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1451
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1452
    return TSDB_CODE_OUT_OF_MEMORY;
×
1453
  }
1454
  pTable->ctbEntry.pTags = pNewTag;
134,508✔
1455
  memcpy(pTable->ctbEntry.pTags, pTagVal->pTagVal, pTagVal->nTagVal);
134,508✔
1456

1457
  int32_t code = metaHandleEntry2(pMeta, pTable);
134,508✔
1458
  if (code) {
134,508✔
1459
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
×
1460
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTable->uid, pTable->name, pTable->version);
×
1461
  } else {
1462
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
134,508✔
1463
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
134,508✔
1464
  }
1465

1466
  return code;
134,508✔
1467
}
1468

1469

1470

1471
static int32_t metaUpdateTableNormalTagValue(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
8,887,641✔
1472
  int32_t code = TSDB_CODE_SUCCESS;
8,887,641✔
1473

1474
  const STag *pOldTag = (const STag *)pTable->ctbEntry.pTags;
8,887,641✔
1475
  SArray     *pTagArray = taosArrayInit(pTagSchema->nCols, sizeof(STagVal));
8,889,147✔
1476
  if (pTagArray == NULL) {
8,886,969✔
1477
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1478
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1479
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1480
  }
1481

1482
  bool allSame = true;
8,886,969✔
1483

1484
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
19,432,465✔
1485
    SSchema *pCol = &pTagSchema->pSchema[i];
10,551,115✔
1486
    STagVal  value = { .cid = pCol->colId };
10,550,524✔
1487

1488
    int32_t colId = pCol->colId;
10,551,949✔
1489
    SUpdatedTagVal *pNewVal = taosHashGet(pUpdatedTagVals, &colId, sizeof(colId));
10,551,277✔
1490
    if (pNewVal == NULL) {
10,549,933✔
1491
      if (!tTagGet(pOldTag, &value)) {
1,590,388✔
1492
        continue;
343,248✔
1493
      }
1494
    } else {
1495
      value.type = pCol->type;
8,959,545✔
1496
      if (tagValueChanged(pNewVal, pOldTag)) {
8,958,792✔
1497
        allSame = false;
8,784,945✔
1498
      }
1499
      if (pNewVal->isNull) {
8,956,695✔
1500
        continue;
75,378✔
1501
      }
1502

1503
      if (IS_VAR_DATA_TYPE(pCol->type)) {
8,882,661✔
1504
        if ((int32_t)pNewVal->nTagVal > (pCol->bytes - VARSTR_HEADER_SIZE)) {
353,000✔
1505
          const char* msgFmt = "vgId:%d, %s failed at %s:%d since value too long for tag %s, version:%" PRId64;
4,275✔
1506
          metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pCol->name, pTable->version);
4,275✔
1507
          taosArrayDestroy(pTagArray);
4,275✔
1508
          TAOS_RETURN(TSDB_CODE_PAR_VALUE_TOO_LONG);
4,275✔
1509
        }
1510
        value.pData = pNewVal->pTagVal;
343,210✔
1511
        value.nData = pNewVal->nTagVal;
343,210✔
1512
      } else {
1513
        memcpy(&value.i64, pNewVal->pTagVal, pNewVal->nTagVal);
8,531,920✔
1514
      }
1515
    }
1516

1517
    if (taosArrayPush(pTagArray, &value) == NULL) {
10,126,870✔
1518
      const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1519
      metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), pTable->version);
×
1520
      taosArrayDestroy(pTagArray);
×
1521
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
1522
    }
1523
  }
1524

1525
  if (allSame) {
8,881,269✔
1526
    const char* msgFmt = "vgId:%d, %s warn at %s:%d all tags are same, version:%" PRId64;
159,095✔
1527
    metaWarn(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pTable->version);
159,095✔
1528
    taosArrayDestroy(pTagArray);
159,095✔
1529
    TAOS_RETURN(TSDB_CODE_VND_SAME_TAG);
159,095✔
1530
  } 
1531

1532
  STag *pNewTag = NULL;
8,722,174✔
1533
  code = tTagNew(pTagArray, pTagSchema->version, false, &pNewTag);
8,722,174✔
1534
  if (code) {
8,720,668✔
1535
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1536
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTable->version);
×
1537
    taosArrayDestroy(pTagArray);
×
1538
    TAOS_RETURN(code);
×
1539
  }
1540
  taosArrayDestroy(pTagArray);
8,720,668✔
1541
  taosMemoryFree(pTable->ctbEntry.pTags);
8,719,590✔
1542
  pTable->ctbEntry.pTags = (uint8_t *)pNewTag;
8,724,618✔
1543
  return TSDB_CODE_SUCCESS;
8,723,865✔
1544
}
1545

1546

1547
static int32_t regexReplaceBuildSubstitution(const char *pReplace, const regmatch_t *pmatch, int32_t nmatch,
15,062✔
1548
                                               const char *cursor, char **ppResult, size_t *pResultLen,
1549
                                               size_t *pResultCap) {
1550
  char  *result = *ppResult;
15,062✔
1551
  size_t resultLen = *pResultLen;
15,062✔
1552
  size_t resultCap = *pResultCap;
15,062✔
1553

1554
  const char *r = pReplace;
15,062✔
1555
  while (*r != '\0') {
219,020✔
1556
    const char *chunk = NULL;
203,958✔
1557
    size_t      chunkLen = 0;
203,958✔
1558

1559
    if (*r == '$' && r[1] >= '0' && r[1] <= '9') {
203,958✔
1560
      int32_t groupIdx = r[1] - '0';
22,800✔
1561
      if (groupIdx < nmatch && pmatch[groupIdx].rm_so != -1) {
22,800✔
1562
        chunk = cursor + pmatch[groupIdx].rm_so;
22,800✔
1563
        chunkLen = (size_t)(pmatch[groupIdx].rm_eo - pmatch[groupIdx].rm_so);
22,800✔
1564
      }
1565
      r += 2;
22,800✔
1566
    } else if (*r == '$' && r[1] == '$') {
181,158✔
1567
      chunk = "$";
×
1568
      chunkLen = 1;
×
1569
      r += 2;
×
1570
    } else {
1571
      chunk = r;
181,158✔
1572
      chunkLen = 1;
181,158✔
1573
      r += 1;
181,158✔
1574
    }
1575

1576
    if (chunkLen > 0) {
203,958✔
1577
      if (resultLen + chunkLen >= resultCap) {
203,958✔
1578
        resultCap = (resultLen + chunkLen) * 2 + 1;
6,512✔
1579
        char *tmp = taosMemoryRealloc(result, resultCap);
6,512✔
1580
        if (NULL == tmp) {
6,512✔
1581
          taosMemoryFree(result);
×
1582
          *ppResult = NULL;
×
1583
          return TSDB_CODE_OUT_OF_MEMORY;
×
1584
        }
1585
        result = tmp;
6,512✔
1586
      }
1587
      (void)memcpy(result + resultLen, chunk, chunkLen);
203,958✔
1588
      resultLen += chunkLen;
203,958✔
1589
    }
1590
  }
1591

1592
  *ppResult = result;
15,062✔
1593
  *pResultLen = resultLen;
15,062✔
1594
  *pResultCap = resultCap;
15,062✔
1595
  return TSDB_CODE_SUCCESS;
15,062✔
1596
}
1597

1598
static int32_t regexReplace(const char *pStr, const char *pPattern, const char *pReplace, char **ppResult) {
16,487✔
1599
  regex_t *regex = NULL;
16,487✔
1600
  int32_t  code = threadGetRegComp(&regex, pPattern);
16,487✔
1601
  if (code != 0) {
16,487✔
1602
    return code;
×
1603
  }
1604

1605
  size_t strLen = strlen(pStr);
16,487✔
1606
  size_t resultCap = strLen + 1;
16,487✔
1607
  size_t resultLen = 0;
16,487✔
1608
  char  *result = taosMemoryMalloc(resultCap);
16,487✔
1609
  if (NULL == result) {
16,487✔
1610
    return TSDB_CODE_OUT_OF_MEMORY;
×
1611
  }
1612

1613
  const int32_t nmatch = 10;
16,487✔
1614
  const char   *cursor = pStr;
16,487✔
1615
  regmatch_t    pmatch[10];
16,487✔
1616
  int32_t       execFlags = 0;
16,487✔
1617

1618
  while (*cursor != '\0') {
31,549✔
1619
    int ret = regexec(regex, cursor, nmatch, pmatch, execFlags);
16,487✔
1620
    if (ret == REG_NOMATCH) {
16,487✔
1621
      break;
1,425✔
1622
    }
1623
    if (ret != 0) {
15,062✔
1624
      taosMemoryFree(result);
×
1625
      return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
×
1626
    }
1627

1628
    size_t prefixLen = (size_t)pmatch[0].rm_so;
15,062✔
1629
    if (resultLen + prefixLen >= resultCap) {
15,062✔
1630
      resultCap = (resultLen + prefixLen) * 2 + 1;
×
1631
      char *tmp = taosMemoryRealloc(result, resultCap);
×
1632
      if (NULL == tmp) {
×
1633
        taosMemoryFree(result);
×
1634
        return TSDB_CODE_OUT_OF_MEMORY;
×
1635
      }
1636
      result = tmp;
×
1637
    }
1638
    (void)memcpy(result + resultLen, cursor, prefixLen);
15,062✔
1639
    resultLen += prefixLen;
15,062✔
1640

1641
    code = regexReplaceBuildSubstitution(pReplace, pmatch, nmatch, cursor, &result, &resultLen, &resultCap);
15,062✔
1642
    if (code != TSDB_CODE_SUCCESS) {
15,062✔
1643
      return code;
×
1644
    }
1645

1646
    if (pmatch[0].rm_so == pmatch[0].rm_eo) {
15,062✔
1647
      if (resultLen + 1 >= resultCap) {
×
1648
        resultCap = resultCap * 2 + 1;
×
1649
        char *tmp = taosMemoryRealloc(result, resultCap);
×
1650
        if (NULL == tmp) {
×
1651
          taosMemoryFree(result);
×
1652
          return TSDB_CODE_OUT_OF_MEMORY;
×
1653
        }
1654
        result = tmp;
×
1655
      }
1656
      result[resultLen++] = cursor[pmatch[0].rm_eo];
×
1657
      cursor += pmatch[0].rm_eo + 1;
×
1658
    } else {
1659
      cursor += pmatch[0].rm_eo;
15,062✔
1660
    }
1661

1662
    execFlags = REG_NOTBOL;
15,062✔
1663
  }
1664

1665
  size_t tailLen = strlen(cursor);
16,487✔
1666
  if (resultLen + tailLen + 1 > resultCap) {
16,487✔
1667
    resultCap = resultLen + tailLen + 1;
×
1668
    char *tmp = taosMemoryRealloc(result, resultCap);
×
1669
    if (NULL == tmp) {
×
1670
      taosMemoryFree(result);
×
1671
      return terrno;
×
1672
    }
1673
    result = tmp;
×
1674
  }
1675
  (void)memcpy(result + resultLen, cursor, tailLen);
16,487✔
1676
  resultLen += tailLen;
16,487✔
1677
  result[resultLen] = '\0';
16,487✔
1678

1679
  *ppResult = result;
16,487✔
1680
  return TSDB_CODE_SUCCESS;
16,487✔
1681
}
1682

1683

1684

1685
static int32_t computeNewTagValViaRegexReplace(const STag* pOldTag, SUpdatedTagVal* pNewVal) {
20,762✔
1686
  STagVal oldTagVal = {.cid = pNewVal->colId};
20,762✔
1687
  if (!tTagGet(pOldTag, &oldTagVal)) {
20,762✔
1688
    pNewVal->isNull = 1;
4,275✔
1689
    pNewVal->pTagVal = NULL;
4,275✔
1690
    pNewVal->nTagVal = 0;
4,275✔
1691
    return TSDB_CODE_SUCCESS;
4,275✔
1692
  }
1693

1694
  bool isNchar = (pNewVal->tagType == TSDB_DATA_TYPE_NCHAR);
16,487✔
1695
  char* oldStr = NULL;
16,487✔
1696

1697
  if (isNchar) {
16,487✔
1698
    // NCHAR is stored as UCS-4, convert to MBS (VARCHAR) for regex processing
1699
    int32_t mbsLen = oldTagVal.nData + 1;
4,275✔
1700
    oldStr = taosMemoryMalloc(mbsLen);
4,275✔
1701
    if (oldStr == NULL) {
4,275✔
1702
      return TSDB_CODE_OUT_OF_MEMORY;
×
1703
    }
1704
    int32_t ret = taosUcs4ToMbs((TdUcs4*)oldTagVal.pData, oldTagVal.nData, oldStr, NULL);
4,275✔
1705
    if (ret < 0) {
4,275✔
1706
      taosMemoryFree(oldStr);
×
1707
      return ret;
×
1708
    }
1709
    oldStr[ret] = '\0';
4,275✔
1710
  } else {
1711
    // VARCHAR: build null-terminated string from old tag value
1712
    oldStr = taosMemoryMalloc(oldTagVal.nData + 1);
12,212✔
1713
    if (oldStr == NULL) {
12,212✔
1714
      return TSDB_CODE_OUT_OF_MEMORY;
×
1715
    }
1716
    memcpy(oldStr, oldTagVal.pData, oldTagVal.nData);
12,212✔
1717
    oldStr[oldTagVal.nData] = '\0';
12,212✔
1718
  }
1719

1720
  char* newStr = NULL;
16,487✔
1721
  int32_t code = regexReplace(oldStr, pNewVal->regexp, pNewVal->replacement, &newStr);
16,487✔
1722
  taosMemoryFree(oldStr);
16,487✔
1723
  if (code != TSDB_CODE_SUCCESS) {
16,487✔
1724
    return code;
×
1725
  }
1726

1727
  if (isNchar) {
16,487✔
1728
    // Convert regex result back from MBS to UCS-4 (NCHAR)
1729
    int32_t newStrLen = (int32_t)strlen(newStr);
4,275✔
1730
    int32_t ucs4BufLen = (newStrLen + 1) * TSDB_NCHAR_SIZE;
4,275✔
1731
    char*   ucs4Buf = taosMemoryMalloc(ucs4BufLen);
4,275✔
1732
    if (ucs4Buf == NULL) {
4,275✔
1733
      taosMemoryFree(newStr);
×
1734
      return TSDB_CODE_OUT_OF_MEMORY;
×
1735
    }
1736

1737
    int32_t ucs4Len = 0;
4,275✔
1738
    bool    cvtOk = taosMbsToUcs4(newStr, newStrLen, (TdUcs4*)ucs4Buf, ucs4BufLen, &ucs4Len, NULL);
4,275✔
1739
    taosMemoryFree(newStr);
4,275✔
1740
    if (!cvtOk) {
4,275✔
1741
      taosMemoryFree(ucs4Buf);
×
1742
      return terrno;
×
1743
    }
1744

1745
    pNewVal->isNull = 0;
4,275✔
1746
    pNewVal->pTagVal = (uint8_t*)ucs4Buf;
4,275✔
1747
    pNewVal->nTagVal = (uint32_t)ucs4Len;
4,275✔
1748
  } else {
1749
    pNewVal->isNull = 0;
12,212✔
1750
    pNewVal->pTagVal = (uint8_t*)newStr;
12,212✔
1751
    pNewVal->nTagVal = (uint32_t)strlen(newStr);
12,212✔
1752
  }
1753

1754
  return TSDB_CODE_SUCCESS;
16,487✔
1755
}
1756

1757

1758

1759
static int32_t metaUpdateTableTagValueImpl(SMeta* pMeta, SMetaEntry* pTable, SSchemaWrapper* pTagSchema, SHashObj* pUpdatedTagVals) {
9,021,396✔
1760
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
9,021,396✔
1761
    return metaUpdateTableJsonTagValue(pMeta, pTable, pTagSchema, pUpdatedTagVals);
134,508✔
1762
  }
1763
  
1764
  int32_t   code = TSDB_CODE_SUCCESS, lino = 0;
8,886,135✔
1765
  SHashObj* pNewTagVals = pUpdatedTagVals;
8,887,560✔
1766
  SArray*   pRegexResults = NULL;
8,887,560✔
1767

1768
  void* pIter = taosHashIterate(pUpdatedTagVals, NULL);
8,887,560✔
1769
  while (pIter) {
17,827,930✔
1770
    SUpdatedTagVal* pVal = (SUpdatedTagVal*)pIter;
8,950,995✔
1771
    if (pVal->regexp != NULL) {
8,950,995✔
1772
      break;
12,212✔
1773
    }
1774
    pIter = taosHashIterate(pUpdatedTagVals, pIter);
8,941,633✔
1775
  }
1776

1777
  // if there are regular expressions, compute new tag values and store in a new hash map to avoid
1778
  // modifying the original tag values which may be reused for computing other tag values
1779
  if (pIter != NULL) {
8,889,147✔
1780
    taosHashCancelIterate(pUpdatedTagVals, pIter);
12,212✔
1781
    pIter = NULL;
12,212✔
1782
    
1783
    int32_t sz = taosHashGetSize(pUpdatedTagVals);
12,212✔
1784
    pNewTagVals = taosHashInit(sz, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
12,212✔
1785
    if (pNewTagVals == NULL) {
12,212✔
1786
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1787
    }
1788

1789
    pRegexResults = taosArrayInit(sz, sizeof(char*));
12,212✔
1790
    if (pRegexResults == NULL) {
12,212✔
1791
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1792
    }
1793

1794
    pIter = taosHashIterate(pUpdatedTagVals, NULL);
12,212✔
1795
    while (pIter) {
32,974✔
1796
      int32_t       colId = *(int32_t*)taosHashGetKey(pIter, NULL);
20,762✔
1797
      SUpdatedTagVal newVal = *(SUpdatedTagVal *)pIter;
20,762✔
1798
      pIter = taosHashIterate(pUpdatedTagVals, pIter);
20,762✔
1799

1800
      if (newVal.regexp != NULL) {
20,762✔
1801
        const STag* pOldTag = (const STag*)pTable->ctbEntry.pTags;
20,762✔
1802
        TAOS_CHECK_GOTO(computeNewTagValViaRegexReplace(pOldTag, &newVal), &lino, _exit);
20,762✔
1803

1804
        newVal.regexp = NULL;
20,762✔
1805
        newVal.replacement = NULL;
20,762✔
1806
        if (taosArrayPush(pRegexResults, &newVal.pTagVal) == NULL) {
20,762✔
1807
          TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
×
1808
        }
1809
      }
1810

1811
      TAOS_CHECK_GOTO(taosHashPut(pNewTagVals, &colId, sizeof(colId), &newVal, sizeof(newVal)), &lino, _exit);
20,762✔
1812
    }
1813
  }
1814

1815
  TAOS_CHECK_GOTO(metaUpdateTableNormalTagValue(pMeta, pTable, pTagSchema, pNewTagVals), &lino, _exit);
8,889,147✔
1816
  TAOS_CHECK_GOTO(metaHandleEntry2(pMeta, pTable), &lino, _exit);
8,721,340✔
1817

1818
_exit:
8,882,370✔
1819
  if (code) {
8,883,876✔
1820
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64;
163,370✔
1821
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, lino, tstrerror(code), pTable->uid, pTable->name, pTable->version);
163,370✔
1822
  } else {
1823
    const char* msgFmt = "vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64;
8,720,506✔
1824
    metaInfo(msgFmt, TD_VID(pMeta->pVnode), pTable->name, pTable->uid, pTable->version);
8,720,506✔
1825
  }
1826

1827
  if (pRegexResults != NULL) {
8,889,819✔
1828
    for (int32_t i = 0; i < taosArrayGetSize(pRegexResults); i++) {
32,974✔
1829
      char** pp = taosArrayGet(pRegexResults, i);
20,762✔
1830
      taosMemoryFree(*pp);
20,762✔
1831
    }
1832
    taosArrayDestroy(pRegexResults);
12,212✔
1833
  }
1834

1835
  if (pNewTagVals != pUpdatedTagVals) {
8,889,819✔
1836
    taosHashCleanup(pNewTagVals);
12,212✔
1837
  }
1838

1839
  if (pIter != NULL) {
8,889,819✔
1840
    taosHashCancelIterate(pUpdatedTagVals, pIter);
×
1841
  }
1842

1843
  TAOS_RETURN(code);
8,889,819✔
1844
}
1845

1846

1847

1848
static int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, const char* tbName, SArray* tags) {
8,709,721✔
1849
  int32_t code = TSDB_CODE_SUCCESS;
8,709,721✔
1850
  SMetaEntry *pChild = NULL;
8,709,721✔
1851
  SMetaEntry *pSuper = NULL;
8,709,721✔
1852
  SHashObj* pUpdatedTagVals = NULL;
8,709,721✔
1853

1854
  // fetch child entry
1855
  code = metaFetchEntryByName(pMeta, tbName, &pChild);
8,709,721✔
1856
  if (code) {
8,709,721✔
1857
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64;
1,048✔
1858
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tbName, version);
1,048✔
1859
    goto _exit;
1,048✔
1860
  }
1861

1862
  if (pChild->type != TSDB_CHILD_TABLE && pChild->type != TSDB_VIRTUAL_CHILD_TABLE) {
8,708,673✔
1863
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since table %s is not a child table, version:%" PRId64;
×
1864
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tbName, version);
×
1865
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
1866
    goto _exit;
×
1867
  }
1868

1869
  // fetch super entry
1870
  code = metaFetchEntryByUid(pMeta, pChild->ctbEntry.suid, &pSuper);
8,708,673✔
1871
  if (code) {
8,708,673✔
1872
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64;
×
1873
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pChild->ctbEntry.suid, version);
×
1874
    code = TSDB_CODE_INTERNAL_ERROR;
×
1875
    goto _exit;
×
1876
  }
1877

1878
  // search the tags to update
1879
  SSchemaWrapper *pTagSchema = &pSuper->stbEntry.schemaTag;
8,708,673✔
1880

1881
  code = updatedTagValueArrayToHashMap(pTagSchema, tags, &pUpdatedTagVals);
8,708,673✔
1882
  if (code) {
8,707,920✔
1883
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1884
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1885
    goto _exit;
×
1886
  }
1887

1888
  // change tag values
1889
  pChild->version = version;
8,707,920✔
1890
  code = metaUpdateTableTagValueImpl(pMeta, pChild, pTagSchema, pUpdatedTagVals);
8,707,920✔
1891
  if (code) {
8,708,673✔
1892
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, name:%s version:%" PRId64;
156,245✔
1893
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), tbName, version);
156,245✔
1894
    goto _exit;
156,245✔
1895
  }
1896

1897
_exit:
8,709,721✔
1898
  taosHashCleanup(pUpdatedTagVals);
8,709,721✔
1899
  metaFetchEntryFree(&pSuper);
8,709,721✔
1900
  metaFetchEntryFree(&pChild);
8,709,721✔
1901
  TAOS_RETURN(code);
8,709,721✔
1902
}
1903

1904

1905

1906
int32_t metaUpdateTableMultiTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
8,695,982✔
1907
  int32_t code = TSDB_CODE_SUCCESS;
8,695,982✔
1908
  SArray* uidList = NULL;
8,695,982✔
1909
  SArray* tagListArray = NULL;
8,695,982✔
1910

1911
  // Pre-allocate uidList for batch notification
1912
  int32_t nTables = taosArrayGetSize(pReq->tables);
8,695,982✔
1913
  uidList = taosArrayInit(nTables, sizeof(tb_uid_t));
8,696,166✔
1914
  if (uidList == NULL) {
8,696,166✔
1915
    code = terrno;
×
1916
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1917
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1918
    TAOS_RETURN(code);
×
1919
  }
1920
  tagListArray = taosArrayInit(nTables, sizeof(void*));
8,696,166✔
1921
  if (tagListArray == NULL) {
8,696,166✔
1922
    code = terrno;
×
1923
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
1924
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
1925
    TAOS_RETURN(code);
×
1926
  }
1927

1928
  for (int32_t i = 0; i < nTables; i++) {
17,404,839✔
1929
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
8,709,721✔
1930
    code = metaUpdateTableTagValue(pMeta, version, pTable->tbName, pTable->tags);
8,709,721✔
1931
    if (code == TSDB_CODE_VND_SAME_TAG) {
8,709,721✔
1932
      // we are updating multiple tables, if one table has same tag,
1933
      // just skip it and continue to update other tables,
1934
      // and return success at the end
1935
      code = TSDB_CODE_SUCCESS;
156,245✔
1936
    } else if (code) {
8,553,476✔
1937
      break;
1,048✔
1938
    } else {
1939
      // Collect UID for batch notification
1940
      int64_t uid = metaGetTableEntryUidByName(pMeta, pTable->tbName);
8,552,428✔
1941
      if (uid == 0) {
8,552,428✔
1942
        metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64,
×
1943
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pTable->tbName, version);
1944
        continue;
×
1945
      }
1946
      if (taosArrayPush(uidList, &uid) == NULL){
8,552,428✔
1947
        metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64,
×
1948
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), version);
1949
        continue;
×
1950
      }
1951
      if (taosArrayPush(tagListArray, &pTable->tags) == NULL){
17,104,856✔
1952
        void* ret = taosArrayPop(uidList);  // make sure the size of uidList and tagListArray are same
×
1953
        metaError("vgId:%d, %s failed at %s:%d since %s, ret:%p, version:%" PRId64,
×
1954
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(terrno), ret, version);
1955
        continue;
×
1956
      }
1957
    }
1958
  }
1959

1960
  if (taosArrayGetSize(uidList) > 0) {
8,696,166✔
1961
    vnodeAlterTagForQuerySub(pMeta->pVnode, uidList, NULL, tagListArray);
8,538,873✔
1962
  }
1963

1964
  taosArrayDestroy(uidList);
8,696,166✔
1965
  taosArrayDestroy(tagListArray);
8,696,166✔
1966
  DestoryThreadLocalRegComp();
8,696,166✔
1967

1968
  if (code) {
8,696,166✔
1969
    const char* msgFmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
1,048✔
1970
    metaError(msgFmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
1,048✔
1971
  }
1972

1973
  TAOS_RETURN(code);
8,696,166✔
1974
}
1975

1976

1977
// Context for translating tag column/tbname references to tag values/table name
1978
typedef struct STagToValueCtx {
1979
  int32_t     code;
1980
  const void *pTags;  // STag* blob of the child table
1981
  const char *pName;  // table name of the child table
1982
} STagToValueCtx;
1983

1984
// Translate a tag column reference to the corresponding tag value
1985
static EDealRes tagToValue(SNode **pNode, void *pContext) {
1,847,990✔
1986
  STagToValueCtx *pCtx = (STagToValueCtx *)pContext;
1,847,990✔
1987

1988
  bool isTagCol = false, isTbname = false;
1,847,990✔
1989

1990
  if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
1,847,990✔
1991
    SColumnNode *pCol = (SColumnNode *)*pNode;
618,982✔
1992
    if (pCol->colType == COLUMN_TYPE_TBNAME)
616,885✔
1993
      isTbname = true;
142,500✔
1994
    else
1995
      isTagCol = true;
477,073✔
1996
  } else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
1,237,396✔
1997
    SFunctionNode *pFunc = (SFunctionNode *)*pNode;
×
1998
    if (pFunc->funcType == FUNCTION_TYPE_TBNAME)
×
1999
      isTbname = true;
×
2000
  }
2001

2002
  if (isTagCol) {
1,851,269✔
2003
    SColumnNode *pSColumnNode = *(SColumnNode **)pNode;
472,879✔
2004
    SValueNode  *res = NULL;
474,976✔
2005
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
475,648✔
2006
    if (NULL == res) {
474,976✔
2007
      return DEAL_RES_ERROR;
×
2008
    }
2009

2010
    res->translate = true;
474,976✔
2011
    res->node.resType = pSColumnNode->node.resType;
476,401✔
2012

2013
    STagVal tagVal = {0};
475,648✔
2014
    tagVal.cid = pSColumnNode->colId;
475,648✔
2015
    const char *p = metaGetTableTagVal(pCtx->pTags, pSColumnNode->node.resType.type, &tagVal);
475,648✔
2016
    if (p == NULL) {
474,223✔
2017
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
2018
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
474,223✔
2019
      int32_t len = ((const STag *)p)->len;
×
2020
      res->datum.p = taosMemoryCalloc(len + 1, 1);
×
2021
      if (NULL == res->datum.p) {
×
2022
        pCtx->code = terrno;
×
2023
        return DEAL_RES_ERROR;
×
2024
      }
2025
      memcpy(res->datum.p, p, len);
×
2026
    } else if (IS_VAR_DATA_TYPE(pSColumnNode->node.resType.type)) {
472,126✔
2027
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
3,920✔
2028
      if (NULL == res->datum.p) {
3,248✔
2029
        pCtx->code = terrno;
×
2030
        return DEAL_RES_ERROR;
×
2031
      }
2032
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
2,842✔
2033
      varDataSetLen(res->datum.p, tagVal.nData);
3,248✔
2034
    } else {
2035
      pCtx->code = nodesSetValueNodeValue(res, &(tagVal.i64));
470,222✔
2036
      if (pCtx->code != TSDB_CODE_SUCCESS) {
469,631✔
2037
        return DEAL_RES_ERROR;
×
2038
      }
2039
    }
2040

2041
    nodesDestroyNode(*pNode);
475,648✔
2042
    *pNode = (SNode *)res;
474,304✔
2043

2044
  } else if (isTbname) {
1,378,390✔
2045
    SValueNode *res = NULL;
142,500✔
2046
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode **)&res);
142,500✔
2047
    if (NULL == res) {
142,500✔
2048
      return DEAL_RES_ERROR;
×
2049
    }
2050

2051
    res->translate = true;
142,500✔
2052
    res->node.resType = ((SExprNode *)(*pNode))->resType;
142,500✔
2053

2054
    int32_t len = strlen(pCtx->pName);
142,500✔
2055
    res->datum.p = taosMemoryCalloc(len + VARSTR_HEADER_SIZE + 1, 1);
142,500✔
2056
    if (NULL == res->datum.p) {
142,500✔
2057
      pCtx->code = terrno;
×
2058
      return DEAL_RES_ERROR;
×
2059
    }
2060
    memcpy(varDataVal(res->datum.p), pCtx->pName, len);
142,500✔
2061
    varDataSetLen(res->datum.p, len);
142,500✔
2062
    nodesDestroyNode(*pNode);
142,500✔
2063
    *pNode = (SNode *)res;
142,500✔
2064
  }
2065

2066
  return DEAL_RES_CONTINUE;
1,847,747✔
2067
}
2068

2069

2070
// Check if a single child table qualifies against the tag condition.
2071
static int32_t metaIsChildTableQualified(SMeta *pMeta, tb_uid_t uid, SNode *pTagCond, bool *pQualified) {
615,217✔
2072
  int32_t     code = TSDB_CODE_SUCCESS;
615,217✔
2073
  SMetaEntry *pEntry = NULL;
615,217✔
2074

2075
  *pQualified = false;
616,642✔
2076

2077
  code = metaFetchEntryByUid(pMeta, uid, &pEntry);
616,642✔
2078
  if (code != TSDB_CODE_SUCCESS || pEntry == NULL) {
614,464✔
2079
    return TSDB_CODE_SUCCESS;
×
2080
  }
2081

2082
  // Clone the condition so we can safely rewrite it
2083
  SNode *pTagCondTmp = NULL;
614,464✔
2084
  code = nodesCloneNode(pTagCond, &pTagCondTmp);
614,464✔
2085
  if (code != TSDB_CODE_SUCCESS) {
618,982✔
2086
    metaFetchEntryFree(&pEntry);
×
2087
    return code;
×
2088
  }
2089

2090
  // Replace tag column and tbname references with concrete values
2091
  STagToValueCtx ctx = {.code = TSDB_CODE_SUCCESS, .pTags = pEntry->ctbEntry.pTags, .pName = pEntry->name};
618,982✔
2092
  nodesRewriteExprPostOrder(&pTagCondTmp, tagToValue, &ctx);
619,654✔
2093
  if (ctx.code != TSDB_CODE_SUCCESS) {
615,970✔
2094
    nodesDestroyNode(pTagCondTmp);
×
2095
    metaFetchEntryFree(&pEntry);
×
2096
    return ctx.code;
×
2097
  }
2098

2099
  // Evaluate the fully-resolved expression to a constant boolean
2100
  SNode *pResult = NULL;
615,970✔
2101
  code = scalarCalculateConstants(pTagCondTmp, &pResult);
617,314✔
2102
  if (code != TSDB_CODE_SUCCESS) {
612,367✔
2103
    nodesDestroyNode(pTagCondTmp);
×
2104
    metaFetchEntryFree(&pEntry);
×
2105
    return code;
×
2106
  }
2107

2108
  if (nodeType(pResult) == QUERY_NODE_VALUE) {
612,367✔
2109
    *pQualified = ((SValueNode *)pResult)->datum.b;
615,970✔
2110
  }
2111

2112
  nodesDestroyNode(pResult);
613,873✔
2113
  metaFetchEntryFree(&pEntry);
613,711✔
2114
  return TSDB_CODE_SUCCESS;
616,723✔
2115
}
2116

2117

2118

2119
static int32_t metaGetChildUidsByTagCond(SMeta *pMeta, tb_uid_t suid, SNode *pTagCond, SNode *pTagIndexCond, SArray *pUidList) {
64,773✔
2120
  int32_t       code = TSDB_CODE_SUCCESS;
64,773✔
2121
  int32_t       lino = 0;
64,773✔
2122
  SVnode       *pVnode = pMeta->pVnode;
64,773✔
2123
  SArray       *pCandidateUids = NULL;
64,182✔
2124

2125
  taosArrayClear(pUidList);
64,182✔
2126

2127
  // Step 1: Try index-accelerated pre-filtering with pTagIndexCond
2128
  if (pTagIndexCond != NULL) {
65,120✔
2129
    pCandidateUids = taosArrayInit(64, sizeof(uint64_t));
×
2130
    if (pCandidateUids == NULL) {
×
2131
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2132
    }
2133

2134
    SIndexMetaArg metaArg = {
×
2135
        .metaEx = pVnode,
2136
        .idx = metaGetIdx(pMeta),
×
2137
        .ivtIdx = metaGetIvtIdx(pMeta),
×
2138
        .suid = suid,
2139
    };
2140

2141
    SMetaDataFilterAPI filterAPI = {
×
2142
        .metaFilterTableIds = metaFilterTableIds,
2143
        .metaFilterCreateTime = metaFilterCreateTime,
2144
        .metaFilterTableName = metaFilterTableName,
2145
        .metaFilterTtl = metaFilterTtl,
2146
    };
2147

2148
    SIdxFltStatus idxStatus = SFLT_NOT_INDEX;
×
2149
    code = doFilterTag(pTagIndexCond, &metaArg, pCandidateUids, &idxStatus, &filterAPI);
×
2150
    if (code != TSDB_CODE_SUCCESS || idxStatus == SFLT_NOT_INDEX) {
×
2151
      // Index filtering failed or not supported, fall back to full scan
2152
      const char* msgFmt = "vgId:%d, index filter not used for suid:%" PRId64 ", status:%d code:%s";
×
2153
      metaDebug(msgFmt, TD_VID(pVnode), suid, idxStatus, tstrerror(code));
×
2154
      taosArrayDestroy(pCandidateUids);
×
2155
      pCandidateUids = NULL;
×
2156
      code = TSDB_CODE_SUCCESS;
×
2157
    }
2158
  }
2159

2160
  // Step 2: If index was not used, enumerate all child table UIDs
2161
  if (pCandidateUids == NULL) {
64,367✔
2162
    pCandidateUids = taosArrayInit(256, sizeof(uint64_t));
64,367✔
2163
    if (pCandidateUids == NULL) {
66,279✔
2164
      TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _end);
×
2165
    }
2166

2167
    SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1);
66,279✔
2168
    if (pCur == NULL) {
65,526✔
2169
      TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2170
    }
2171

2172
    while (1) {
748,576✔
2173
      tb_uid_t uid = metaCtbCursorNext(pCur);
813,430✔
2174
      if (uid == 0) {
816,952✔
2175
        break;
67,704✔
2176
      }
2177
      if (taosArrayPush(pCandidateUids, &uid) == NULL) {
747,823✔
2178
        code = terrno;
×
2179
        metaCloseCtbCursor(pCur);
×
2180
        TAOS_CHECK_GOTO(code, &lino, _end);
×
2181
      }
2182
    }
2183
    metaCloseCtbCursor(pCur);
67,704✔
2184
  }
2185

2186
  // Step 3: Apply pTagCond to filter the candidate UIDs
2187
  if (pTagCond == NULL) {
67,704✔
2188
    // No tag condition — all candidates qualify
2189
    taosArraySwap(pUidList, pCandidateUids);
5,700✔
2190
  } else {
2191
    // Evaluate pTagCond per child table
2192
    for (int32_t i = 0; i < taosArrayGetSize(pCandidateUids); i++) {
677,302✔
2193
      uint64_t uid = *(uint64_t*)taosArrayGet(pCandidateUids, i);
614,545✔
2194
      bool qualified = false;
614,545✔
2195
      code = metaIsChildTableQualified(pMeta, uid, pTagCond, &qualified);
617,395✔
2196
      if (code != TSDB_CODE_SUCCESS) {
612,286✔
2197
        const char* msgFmt = "vgId:%d, %s failed to evaluate tag cond for uid:%" PRId64 " suid:%" PRId64 " since %s";
×
2198
        metaError(msgFmt, TD_VID(pVnode), __func__, uid, suid, tstrerror(code));
×
UNCOV
2199
        TAOS_CHECK_GOTO(code, &lino, _end);
×
2200
      }
2201

2202
      if (qualified && taosArrayPush(pUidList, &uid) == NULL) {
780,331✔
2203
        TAOS_CHECK_GOTO(terrno, &lino, _end);
×
2204
      }
2205
    }
2206
  }
2207

2208
_end:
61,008✔
2209
  taosArrayDestroy(pCandidateUids);
67,704✔
2210
  if (code != TSDB_CODE_SUCCESS) {
67,704✔
2211
    const char* msgFmt = "vgId:%d, %s failed at line %d for suid:%" PRId64 " since %s";
×
2212
    metaError(msgFmt, TD_VID(pVnode), __func__, lino, suid, tstrerror(code));
×
2213
  }
2214
  return code;
67,704✔
2215
}
2216

2217

2218
// Convenience wrapper: partition a raw WHERE condition into tag-related parts,
2219
// then call metaGetChildUidsByTagCond to get the filtered child table UIDs.
2220
int32_t metaGetChildUidsByWhere(SMeta *pMeta, tb_uid_t suid, SNode *pWhere, SArray *pUidList) {
65,526✔
2221
  int32_t code = TSDB_CODE_SUCCESS;
65,526✔
2222
  SNode  *pTagCond = NULL;
65,526✔
2223
  SNode  *pTagIndexCond = NULL;
63,429✔
2224

2225
  if (pWhere == NULL) {
65,526✔
2226
    // No WHERE condition — return all child table UIDs
2227
    return metaGetChildUidsByTagCond(pMeta, suid, NULL, NULL, pUidList);
4,947✔
2228
  }
2229

2230
  // Clone pWhere so the caller's node is not destroyed by filterPartitionCond
2231
  SNode *pWhereCopy = NULL;
60,579✔
2232
  code = nodesCloneNode(pWhere, &pWhereCopy);
60,579✔
2233
  if (code != TSDB_CODE_SUCCESS) {
60,579✔
2234
    return code;
×
2235
  }
2236

2237
  // Partition the WHERE condition
2238
  code = filterPartitionCond(&pWhereCopy, NULL, &pTagIndexCond, &pTagCond, NULL);
60,579✔
2239
  if (code != TSDB_CODE_SUCCESS) {
59,154✔
2240
    goto _cleanup;
×
2241
  }
2242

2243
  // Call the core filtering function
2244
  code = metaGetChildUidsByTagCond(pMeta, suid, pTagCond, pTagIndexCond, pUidList);
59,154✔
2245

2246
_cleanup:
62,004✔
2247
  nodesDestroyNode(pTagCond);
62,004✔
2248
  nodesDestroyNode(pTagIndexCond);
62,004✔
2249
  nodesDestroyNode(pWhereCopy);
62,004✔
2250
  return code;
62,004✔
2251
}
2252

2253

2254

2255
int32_t metaUpdateTableChildTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
67,704✔
2256
  int32_t code = TSDB_CODE_SUCCESS;
67,704✔
2257
  SNode* pWhere = NULL;
67,704✔
2258
  SMetaEntry *pSuper = NULL;
67,704✔
2259
  SArray *pUids = NULL;
67,704✔
2260
  SHashObj *pUpdatedTagVals = NULL;
67,704✔
2261
  SArray *uidListForTmq = NULL;
67,704✔
2262

2263
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
67,704✔
2264
    const char* fmt = "vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64;
×
2265
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
2266
    code = TSDB_CODE_INVALID_MSG;
×
2267
    goto _exit;
×
2268
  }
2269

2270
  if (pReq->whereLen > 0) {
67,704✔
2271
    code = nodesMsgToNode(pReq->where, pReq->whereLen, &pWhere);
61,251✔
2272
    if (code) {
62,004✔
2273
      const char* fmt = "vgId:%d, %s failed at %s:%d since invalid where condition, version:%" PRId64;
×
2274
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
×
2275
      code = TSDB_CODE_INVALID_MSG;
×
2276
      goto _exit;
×
2277
    }
2278
  }
2279

2280
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pSuper);
67,704✔
2281
  if (code) {
65,526✔
2282
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64;
×
2283
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
2284
    goto _exit;
×
2285
  }
2286

2287
  if (pSuper->type != TSDB_SUPER_TABLE) {
65,526✔
2288
    const char* fmt = "vgId:%d, %s failed at %s:%d since table %s is not a super table, version:%" PRId64;
×
2289
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, version);
×
2290
    code = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
2291
    goto _exit;
×
2292
  }
2293

2294
  code = updatedTagValueArrayToHashMap(&pSuper->stbEntry.schemaTag, pReq->pMultiTag, &pUpdatedTagVals);
63,429✔
2295
  if (code) {
63,776✔
2296
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2297
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2298
    goto _exit;
×
2299
  }
2300

2301
  pUids = taosArrayInit(16, sizeof(int64_t));
63,776✔
2302
  if (pUids == NULL) {
62,838✔
2303
    code = terrno;
×
2304
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2305
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2306
    goto _exit;
×
2307
  }
2308
  code = metaGetChildUidsByWhere(pMeta, pSuper->uid, pWhere, pUids);
62,838✔
2309
  if (code) {
67,704✔
2310
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2311
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2312
    goto _exit;
×
2313
  }
2314

2315
  // Pre-allocate uidList for batch notification
2316
  int32_t nUids = taosArrayGetSize(pUids);
67,704✔
2317
  uidListForTmq = taosArrayInit(nUids, sizeof(tb_uid_t));
67,704✔
2318
  if (uidListForTmq == NULL) {
67,704✔
2319
    code = terrno;
×
2320
    const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2321
    metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version);
×
2322
    goto _exit;
×
2323
  }
2324

2325
  for (int32_t i = 0; i < nUids; i++) {
379,083✔
2326
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(pUids, i);
315,654✔
2327
    SMetaEntry *pChild = NULL;
315,654✔
2328
    code = metaFetchEntryByUid(pMeta, uid, &pChild);
315,654✔
2329
    if (code) {
314,148✔
2330
      const char* fmt = "vgId:%d, %s failed at %s:%d since child table uid %" PRId64 " not found, version:%" PRId64;
×
2331
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, uid, version);
×
2332
      goto _exit;
×
2333
    }
2334

2335
    pChild->version = version;
314,148✔
2336
    code = metaUpdateTableTagValueImpl(pMeta, pChild, &pSuper->stbEntry.schemaTag, pUpdatedTagVals);
314,148✔
2337
    if (code == TSDB_CODE_VND_SAME_TAG) {
315,654✔
2338
      // we are updating multiple tables, if one table has same tag,
2339
      // just skip it and continue to update other tables,
2340
      // and return success at the end
2341
      code = TSDB_CODE_SUCCESS;
2,850✔
2342
    } else if (code) {
312,804✔
2343
      const char* fmt = "vgId:%d, %s failed at %s:%d since %s, child table uid %" PRId64 " name %s, version:%" PRId64;
4,275✔
2344
      metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, tstrerror(code), uid, pChild->name, version);
4,275✔
2345
      metaFetchEntryFree(&pChild);
4,275✔
2346
      goto _exit;
4,275✔
2347
    } else {
2348
      // Collect UID for batch notification
2349
      if (taosArrayPush(uidListForTmq, &uid) == NULL) {
308,529✔
2350
        const char* fmt = "vgId:%d, %s failed at %s:%d since %s, version:%" PRId64;
×
2351
        metaError(fmt, TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, terrstr, version);
×
2352
      }
2353
    }
2354

2355
    metaFetchEntryFree(&pChild);
311,379✔
2356
  }
2357

2358
_exit:
67,704✔
2359
  DestoryThreadLocalRegComp();
67,704✔
2360
  if (taosArrayGetSize(uidListForTmq) > 0) {
67,704✔
2361
    vnodeAlterTagForQuerySub(pMeta->pVnode, uidListForTmq, pReq->pMultiTag, NULL);
26,992✔
2362
  }
2363
  taosArrayDestroy(pUids);
67,704✔
2364
  taosArrayDestroy(uidListForTmq);
67,704✔
2365
  taosHashCleanup(pUpdatedTagVals);
67,704✔
2366
  metaFetchEntryFree(&pSuper);
67,704✔
2367
  nodesDestroyNode(pWhere);
67,704✔
2368
  TAOS_RETURN(code);
67,704✔
2369
}
2370

2371

2372

2373
static int32_t metaCheckUpdateTableOptionsReq(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
28,410✔
2374
  int32_t code = TSDB_CODE_SUCCESS;
28,410✔
2375

2376
  if (pReq->tbName == NULL || strlen(pReq->tbName) == 0) {
28,410✔
2377
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2378
              __FILE__, __LINE__, version);
2379
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2380
  }
2381

2382
  return code;
28,410✔
2383
}
2384

2385
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
28,410✔
2386
  int32_t code = 0;
28,410✔
2387

2388
  code = metaCheckUpdateTableOptionsReq(pMeta, version, pReq);
28,410✔
2389
  if (code) {
28,410✔
2390
    TAOS_RETURN(code);
×
2391
  }
2392

2393
  // fetch entry
2394
  SMetaEntry *pEntry = NULL;
28,410✔
2395
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
28,410✔
2396
  if (code) {
28,410✔
2397
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2398
              __FILE__, __LINE__, pReq->tbName, version);
2399
    TAOS_RETURN(code);
×
2400
  }
2401

2402
  // do change the entry
2403
  pEntry->version = version;
28,410✔
2404
  if (pEntry->type == TSDB_CHILD_TABLE) {
28,410✔
2405
    if (pReq->updateTTL) {
13,258✔
2406
      pEntry->ctbEntry.ttlDays = pReq->newTTL;
5,137✔
2407
    }
2408
    if (pReq->newCommentLen >= 0) {
13,258✔
2409
      char *pNewComment = NULL;
8,121✔
2410
      if (pReq->newCommentLen) {
8,121✔
2411
        pNewComment = taosMemoryRealloc(pEntry->ctbEntry.comment, pReq->newCommentLen + 1);
5,172✔
2412
        if (NULL == pNewComment) {
5,172✔
2413
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2414
                    __LINE__, tstrerror(terrno), version);
2415
          metaFetchEntryFree(&pEntry);
×
2416
          TAOS_RETURN(terrno);
×
2417
        }
2418
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
5,172✔
2419
      } else {
2420
        taosMemoryFreeClear(pEntry->ctbEntry.comment);
2,949✔
2421
      }
2422
      pEntry->ctbEntry.comment = pNewComment;
8,121✔
2423
      pEntry->ctbEntry.commentLen = pReq->newCommentLen;
8,121✔
2424
    }
2425
  } else if (pEntry->type == TSDB_NORMAL_TABLE) {
15,152✔
2426
    if (pReq->updateTTL) {
15,152✔
2427
      pEntry->ntbEntry.ttlDays = pReq->newTTL;
5,303✔
2428
    }
2429
    if (pReq->newCommentLen >= 0) {
15,152✔
2430
      char *pNewComment = NULL;
9,849✔
2431
      if (pReq->newCommentLen > 0) {
9,849✔
2432
        pNewComment = taosMemoryRealloc(pEntry->ntbEntry.comment, pReq->newCommentLen + 1);
6,900✔
2433
        if (NULL == pNewComment) {
6,900✔
2434
          metaError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pMeta->pVnode), __func__, __FILE__,
×
2435
                    __LINE__, tstrerror(terrno), version);
2436
          metaFetchEntryFree(&pEntry);
×
2437
          TAOS_RETURN(terrno);
×
2438
        }
2439
        memcpy(pNewComment, pReq->newComment, pReq->newCommentLen + 1);
6,900✔
2440
      } else {
2441
        taosMemoryFreeClear(pEntry->ntbEntry.comment);
2,949✔
2442
      }
2443
      pEntry->ntbEntry.comment = pNewComment;
9,849✔
2444
      pEntry->ntbEntry.commentLen = pReq->newCommentLen;
9,849✔
2445
    }
2446
  } else {
2447
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2448
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
2449
    metaFetchEntryFree(&pEntry);
×
2450
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2451
  }
2452

2453
  // do handle entry
2454
  code = metaHandleEntry2(pMeta, pEntry);
28,410✔
2455
  if (code) {
28,410✔
2456
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2457
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2458
    metaFetchEntryFree(&pEntry);
×
2459
    TAOS_RETURN(code);
×
2460
  } else {
2461
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
28,410✔
2462
             pEntry->uid, version);
2463
  }
2464

2465
  metaFetchEntryFree(&pEntry);
28,410✔
2466
  TAOS_RETURN(code);
28,410✔
2467
}
2468

2469
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq) {
6,948✔
2470
  int32_t code = TSDB_CODE_SUCCESS;
6,948✔
2471

2472
  if (NULL == pReq->tbName || strlen(pReq->tbName) == 0) {
6,948✔
2473
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2474
              __FILE__, __LINE__, version);
2475
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2476
  }
2477

2478
  SMetaEntry *pEntry = NULL;
6,948✔
2479
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
6,948✔
2480
  if (code) {
6,948✔
2481
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2482
              __FILE__, __LINE__, pReq->tbName, version);
2483
    TAOS_RETURN(code);
×
2484
  }
2485

2486
  if (pEntry->version >= version) {
6,948✔
2487
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2488
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2489
    metaFetchEntryFree(&pEntry);
×
2490
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2491
  }
2492

2493
  if (pEntry->type != TSDB_NORMAL_TABLE && pEntry->type != TSDB_SUPER_TABLE) {
6,948✔
2494
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2495
              __func__, __FILE__, __LINE__, pReq->tbName, pEntry->type, version);
2496
    metaFetchEntryFree(&pEntry);
×
2497
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2498
  }
2499

2500
  // do change the entry
2501
  int8_t           updated = 0;
6,948✔
2502
  SColCmprWrapper *wp = &pEntry->colCmpr;
6,948✔
2503
  for (int32_t i = 0; i < wp->nCols; i++) {
55,584✔
2504
    SColCmpr *p = &wp->pColCmpr[i];
48,636✔
2505
    if (p->id == pReq->colId) {
48,636✔
2506
      uint32_t dst = 0;
6,948✔
2507
      updated = tUpdateCompress(p->alg, pReq->compress, TSDB_COLVAL_COMPRESS_DISABLED, TSDB_COLVAL_LEVEL_DISABLED,
6,948✔
2508
                                TSDB_COLVAL_LEVEL_MEDIUM, &dst);
2509
      if (updated > 0) {
6,948✔
2510
        p->alg = dst;
6,948✔
2511
      }
2512
    }
2513
  }
2514

2515
  if (updated == 0) {
6,948✔
2516
    code = TSDB_CODE_VND_COLUMN_COMPRESS_ALREADY_EXIST;
×
2517
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is not changed, version:%" PRId64,
×
2518
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2519
    metaFetchEntryFree(&pEntry);
×
2520
    TAOS_RETURN(code);
×
2521
  } else if (updated < 0) {
6,948✔
2522
    code = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2523
    metaError("vgId:%d, %s failed at %s:%d since column %d compress level is invalid, version:%" PRId64,
×
2524
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, version);
2525
    metaFetchEntryFree(&pEntry);
×
2526
    TAOS_RETURN(code);
×
2527
  }
2528

2529
  pEntry->version = version;
6,948✔
2530

2531
  // do handle entry
2532
  code = metaHandleEntry2(pMeta, pEntry);
6,948✔
2533
  if (code) {
6,948✔
2534
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2535
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2536
    metaFetchEntryFree(&pEntry);
×
2537
    TAOS_RETURN(code);
×
2538
  } else {
2539
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
6,948✔
2540
             pEntry->uid, version);
2541
  }
2542

2543
  metaFetchEntryFree(&pEntry);
6,948✔
2544
  TAOS_RETURN(code);
6,948✔
2545
}
2546

2547
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
87,859✔
2548
  int32_t code = TSDB_CODE_SUCCESS;
87,859✔
2549

2550
  // check request
2551
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
87,859✔
2552
  if (code) {
87,859✔
2553
    TAOS_RETURN(code);
×
2554
  }
2555

2556
  if (NULL == pReq->refDbName) {
87,859✔
2557
    metaError("vgId:%d, %s failed at %s:%d since invalid ref db name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2558
              __func__, __FILE__, __LINE__, version);
2559
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2560
  }
2561

2562
  if (NULL == pReq->refTbName) {
87,859✔
2563
    metaError("vgId:%d, %s failed at %s:%d since invalid ref table name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2564
              __func__, __FILE__, __LINE__, version);
2565
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2566
  }
2567

2568
  if (NULL == pReq->refColName) {
87,859✔
2569
    metaError("vgId:%d, %s failed at %s:%d since invalid ref Col name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2570
              __func__, __FILE__, __LINE__, version);
2571
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2572
  }
2573

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

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

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

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

2615
  if (NULL == pColRef) {
87,859✔
2616
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2617
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->colId, pReq->tbName, version);
2618
    metaFetchEntryFree(&pEntry);
×
2619
    metaFetchEntryFree(&pSuper);
×
2620
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2621
  }
2622

2623
  // do update column name
2624
  pEntry->version = version;
87,859✔
2625
  pColRef->hasRef = true;
87,859✔
2626
  pColRef->id = pSchema->pSchema[iColumn].colId;
87,859✔
2627
  tstrncpy(pColRef->refDbName, pReq->refDbName, TSDB_DB_NAME_LEN);
87,859✔
2628
  tstrncpy(pColRef->refTableName, pReq->refTbName, TSDB_TABLE_NAME_LEN);
87,859✔
2629
  tstrncpy(pColRef->refColName, pReq->refColName, TSDB_COL_NAME_LEN);
87,859✔
2630
  pSchema->version++;
87,859✔
2631
  pEntry->colRef.version++;
87,859✔
2632

2633
  // do handle entry
2634
  code = metaHandleEntry2(pMeta, pEntry);
87,859✔
2635
  if (code) {
87,859✔
2636
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2637
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2638
    metaFetchEntryFree(&pEntry);
×
2639
    metaFetchEntryFree(&pSuper);
×
2640
    TAOS_RETURN(code);
×
2641
  } else {
2642
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
87,859✔
2643
             pEntry->uid, version);
2644
  }
2645

2646
  // build response
2647
  code = metaUpdateVtbMetaRsp(
263,577✔
2648
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
87,859✔
2649
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->pExtSchemas : pEntry->pExtSchemas,
87,859✔
2650
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
87,859✔
2651
      pEntry->type);
87,859✔
2652
  if (code) {
87,859✔
2653
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2654
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2655
  } else {
2656
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
649,855✔
2657
      SColRef *p = &pEntry->colRef.pColRef[i];
561,996✔
2658
      pRsp->pColRefs[i].hasRef = p->hasRef;
561,996✔
2659
      pRsp->pColRefs[i].id = p->id;
561,996✔
2660
      if (p->hasRef) {
561,996✔
2661
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
386,801✔
2662
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
386,801✔
2663
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
386,801✔
2664
      }
2665
    }
2666
  }
2667

2668
  metaFetchEntryFree(&pEntry);
87,859✔
2669
  metaFetchEntryFree(&pSuper);
87,859✔
2670
  TAOS_RETURN(code);
87,859✔
2671
}
2672

2673
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp) {
65,102✔
2674
  int32_t code = TSDB_CODE_SUCCESS;
65,102✔
2675

2676
  // check request
2677
  code = metaCheckAlterTableColumnReq(pMeta, version, pReq);
65,102✔
2678
  if (code) {
65,102✔
2679
    TAOS_RETURN(code);
×
2680
  }
2681

2682
  // fetch old entry
2683
  SMetaEntry *pEntry = NULL;
65,102✔
2684
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
65,102✔
2685
  if (code) {
65,102✔
2686
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2687
              __FILE__, __LINE__, pReq->tbName, version);
2688
    TAOS_RETURN(code);
×
2689
  }
2690

2691
  if (pEntry->version >= version) {
65,102✔
2692
    metaError("vgId:%d, %s failed at %s:%d since table %s version %" PRId64 " is not less than %" PRId64,
×
2693
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->tbName, pEntry->version, version);
2694
    metaFetchEntryFree(&pEntry);
×
2695
    TAOS_RETURN(TSDB_CODE_INVALID_PARA);
×
2696
  }
2697

2698
  // fetch super entry
2699
  SMetaEntry *pSuper = NULL;
65,102✔
2700
  if (pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
65,102✔
2701
    code = metaFetchEntryByUid(pMeta, pEntry->ctbEntry.suid, &pSuper);
32,362✔
2702
    if (code) {
32,362✔
2703
      metaError("vgId:%d, %s failed at %s:%d since super table uid %" PRId64 " not found, version:%" PRId64,
×
2704
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pEntry->ctbEntry.suid, version);
2705
      metaFetchEntryFree(&pEntry);
×
2706
      TAOS_RETURN(TSDB_CODE_INTERNAL_ERROR);
×
2707
    }
2708
  }
2709

2710
  // search the column to update
2711
  SSchemaWrapper *pSchema =
65,102✔
2712
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? &pSuper->stbEntry.schemaRow : &pEntry->ntbEntry.schemaRow;
65,102✔
2713
  SColRef *pColRef = NULL;
65,102✔
2714
  int32_t  iColumn = 0;
65,102✔
2715
  for (int32_t i = 0; i < pSchema->nCols; i++) {
260,273✔
2716
    if (strncmp(pSchema->pSchema[i].name, pReq->colName, TSDB_COL_NAME_LEN) == 0) {
260,273✔
2717
      pColRef = &pEntry->colRef.pColRef[i];
65,102✔
2718
      iColumn = i;
65,102✔
2719
      break;
65,102✔
2720
    }
2721
  }
2722

2723
  if (NULL == pColRef) {
65,102✔
2724
    metaError("vgId:%d, %s failed at %s:%d since column id %d not found in table %s, version:%" PRId64,
×
2725
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, iColumn + 1, pReq->tbName, version);
2726
    metaFetchEntryFree(&pEntry);
×
2727
    metaFetchEntryFree(&pSuper);
×
2728
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2729
  }
2730

2731
  // do update column name
2732
  pEntry->version = version;
65,102✔
2733
  pColRef->hasRef = false;
65,102✔
2734
  pColRef->id = pSchema->pSchema[iColumn].colId;
65,102✔
2735
  pSchema->version++;
65,102✔
2736
  pEntry->colRef.version++;
65,102✔
2737

2738
  // do handle entry
2739
  code = metaHandleEntry2(pMeta, pEntry);
65,102✔
2740
  if (code) {
65,102✔
2741
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2742
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2743
    metaFetchEntryFree(&pEntry);
×
2744
    metaFetchEntryFree(&pSuper);
×
2745
    TAOS_RETURN(code);
×
2746
  } else {
2747
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->tbName,
65,102✔
2748
             pEntry->uid, version);
2749
  }
2750

2751
  // build response
2752
  code = metaUpdateVtbMetaRsp(
195,306✔
2753
      pEntry, pReq->tbName, pSchema, &pEntry->colRef,
65,102✔
2754
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->pExtSchemas : pEntry->pExtSchemas,
65,102✔
2755
      pEntry->type == TSDB_VIRTUAL_CHILD_TABLE ? pSuper->stbEntry.ownerId : pEntry->ntbEntry.ownerId, pRsp,
65,102✔
2756
      pEntry->type);
65,102✔
2757
  if (code) {
65,102✔
2758
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2759
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->tbName, version);
2760
  } else {
2761
    for (int32_t i = 0; i < pEntry->colRef.nCols; i++) {
405,128✔
2762
      SColRef *p = &pEntry->colRef.pColRef[i];
340,026✔
2763
      pRsp->pColRefs[i].hasRef = p->hasRef;
340,026✔
2764
      pRsp->pColRefs[i].id = p->id;
340,026✔
2765
      if (p->hasRef) {
340,026✔
2766
        tstrncpy(pRsp->pColRefs[i].refDbName, p->refDbName, TSDB_DB_NAME_LEN);
170,521✔
2767
        tstrncpy(pRsp->pColRefs[i].refTableName, p->refTableName, TSDB_TABLE_NAME_LEN);
170,521✔
2768
        tstrncpy(pRsp->pColRefs[i].refColName, p->refColName, TSDB_COL_NAME_LEN);
170,521✔
2769
      }
2770
    }
2771
  }
2772

2773
  metaFetchEntryFree(&pEntry);
65,102✔
2774
  metaFetchEntryFree(&pSuper);
65,102✔
2775
  TAOS_RETURN(code);
65,102✔
2776
}
2777

2778
int32_t metaAddIndexToSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
20,527✔
2779
  int32_t code = TSDB_CODE_SUCCESS;
20,527✔
2780

2781
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
20,527✔
2782
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2783
              __FILE__, __LINE__, version);
2784
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2785
  }
2786

2787
  SMetaEntry *pEntry = NULL;
20,527✔
2788
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
20,527✔
2789
  if (code) {
20,527✔
2790
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2791
              __FILE__, __LINE__, pReq->name, version);
2792
    TAOS_RETURN(code);
×
2793
  }
2794

2795
  if (pEntry->type != TSDB_SUPER_TABLE) {
20,527✔
2796
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2797
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2798
    metaFetchEntryFree(&pEntry);
×
2799
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2800
  }
2801

2802
  if (pEntry->uid != pReq->suid) {
20,527✔
2803
    metaError("vgId:%d, %s failed at %s:%d since table %s uid %" PRId64 " is not equal to %" PRId64
×
2804
              ", version:%" PRId64,
2805
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->uid, pReq->suid, version);
2806
    metaFetchEntryFree(&pEntry);
×
2807
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2808
  }
2809

2810
  // if (pEntry->stbEntry.schemaTag.version >= pReq->schemaTag.version) {
2811
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2812
  //   PRId64,
2813
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pEntry->stbEntry.schemaTag.version,
2814
  //             pReq->schemaTag.version, version);
2815
  //   metaFetchEntryFree(&pEntry);
2816
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2817
  // }
2818

2819
  // do change the entry
2820
  SSchemaWrapper *pOldTagSchema = &pEntry->stbEntry.schemaTag;
20,527✔
2821
  SSchemaWrapper *pNewTagSchema = &pReq->schemaTag;
20,527✔
2822
  if (pOldTagSchema->nCols == 1 && pOldTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
20,527✔
2823
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2824
              __func__, __FILE__, __LINE__, pReq->name, version);
2825
    metaFetchEntryFree(&pEntry);
×
2826
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2827
  }
2828

2829
  if (pOldTagSchema->nCols != pNewTagSchema->nCols) {
20,527✔
2830
    metaError(
×
2831
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to %d, version:%" PRId64,
2832
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->nCols, pNewTagSchema->nCols,
2833
        version);
2834
    metaFetchEntryFree(&pEntry);
×
2835
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2836
  }
2837

2838
  // if (pOldTagSchema->version >= pNewTagSchema->version) {
2839
  //   metaError("vgId:%d, %s failed at %s:%d since table %s tag schema version %d is not less than %d, version:%"
2840
  //   PRId64,
2841
  //             TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, pOldTagSchema->version,
2842
  //             pNewTagSchema->version, version);
2843
  //   metaFetchEntryFree(&pEntry);
2844
  //   TAOS_RETURN(TSDB_CODE_INVALID_MSG);
2845
  // }
2846

2847
  int32_t numOfChangedTags = 0;
20,527✔
2848
  for (int32_t i = 0; i < pOldTagSchema->nCols; i++) {
220,467✔
2849
    SSchema *pOldColumn = pOldTagSchema->pSchema + i;
199,940✔
2850
    SSchema *pNewColumn = pNewTagSchema->pSchema + i;
199,940✔
2851

2852
    if (pOldColumn->type != pNewColumn->type || pOldColumn->colId != pNewColumn->colId ||
199,940✔
2853
        strncmp(pOldColumn->name, pNewColumn->name, sizeof(pNewColumn->name))) {
199,940✔
2854
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2855
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2856
      metaFetchEntryFree(&pEntry);
×
2857
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2858
    }
2859

2860
    if (IS_IDX_ON(pNewColumn) && !IS_IDX_ON(pOldColumn)) {
199,940✔
2861
      numOfChangedTags++;
20,527✔
2862
      SSCHMEA_SET_IDX_ON(pOldColumn);
20,527✔
2863
    } else if (!IS_IDX_ON(pNewColumn) && IS_IDX_ON(pOldColumn)) {
179,413✔
2864
      metaError("vgId:%d, %s failed at %s:%d since table %s tag schema column %d is not equal, version:%" PRId64,
×
2865
                TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, i, version);
2866
      metaFetchEntryFree(&pEntry);
×
2867
      TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2868
    }
2869
  }
2870

2871
  if (numOfChangedTags != 1) {
20,527✔
2872
    metaError(
×
2873
        "vgId:%d, %s failed at %s:%d since table %s tag schema column count %d is not equal to 1, version:%" PRId64,
2874
        TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->name, numOfChangedTags, version);
2875
    metaFetchEntryFree(&pEntry);
×
2876
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2877
  }
2878

2879
  pEntry->version = version;
20,527✔
2880
  pEntry->stbEntry.schemaTag.version = pNewTagSchema->version;
20,527✔
2881

2882
  // do handle the entry
2883
  code = metaHandleEntry2(pMeta, pEntry);
20,527✔
2884
  if (code) {
20,527✔
2885
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2886
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->name, version);
2887
    metaFetchEntryFree(&pEntry);
×
2888
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2889
  } else {
2890
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
20,527✔
2891
             pEntry->uid, version);
2892
  }
2893

2894
  metaFetchEntryFree(&pEntry);
20,527✔
2895
  TAOS_RETURN(code);
20,527✔
2896
}
2897

2898
int32_t metaDropIndexFromSuperTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) {
16,669✔
2899
  int32_t code = TSDB_CODE_SUCCESS;
16,669✔
2900

2901
  if (strlen(pReq->colName) == 0 || strlen(pReq->stb) == 0) {
16,669✔
2902
    metaError("vgId:%d, %s failed at %s:%d since invalid table name or column name, version:%" PRId64,
×
2903
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, version);
2904
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
2905
  }
2906

2907
  SMetaEntry *pEntry = NULL;
16,669✔
2908
  code = metaFetchEntryByUid(pMeta, pReq->stbUid, &pEntry);
16,669✔
2909
  if (code) {
16,669✔
2910
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
2911
              __FILE__, __LINE__, pReq->stb, version);
2912
    TAOS_RETURN(code);
×
2913
  }
2914

2915
  if (TSDB_SUPER_TABLE != pEntry->type) {
16,669✔
2916
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2917
              __func__, __FILE__, __LINE__, pReq->stb, pEntry->type, version);
2918
    metaFetchEntryFree(&pEntry);
×
2919
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2920
  }
2921

2922
  SSchemaWrapper *pTagSchema = &pEntry->stbEntry.schemaTag;
16,669✔
2923
  if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
16,669✔
2924
    metaError("vgId:%d, %s failed at %s:%d since table %s has no tag, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2925
              __func__, __FILE__, __LINE__, pReq->stb, version);
2926
    metaFetchEntryFree(&pEntry);
×
2927
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2928
  }
2929

2930
  // search and set the tag index off
2931
  int32_t numOfChangedTags = 0;
16,669✔
2932
  for (int32_t i = 0; i < pTagSchema->nCols; i++) {
103,136✔
2933
    SSchema *pCol = pTagSchema->pSchema + i;
103,136✔
2934
    if (0 == strncmp(pCol->name, pReq->colName, sizeof(pReq->colName))) {
103,136✔
2935
      if (!IS_IDX_ON(pCol)) {
16,669✔
2936
        metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not indexed, version:%" PRId64,
×
2937
                  TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2938
        metaFetchEntryFree(&pEntry);
×
2939
        TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2940
      }
2941
      numOfChangedTags++;
16,669✔
2942
      SSCHMEA_SET_IDX_OFF(pCol);
16,669✔
2943
      break;
16,669✔
2944
    }
2945
  }
2946

2947
  if (numOfChangedTags != 1) {
16,669✔
2948
    metaError("vgId:%d, %s failed at %s:%d since table %s column %s is not found, version:%" PRId64,
×
2949
              TD_VID(pMeta->pVnode), __func__, __FILE__, __LINE__, pReq->stb, pReq->colName, version);
2950
    metaFetchEntryFree(&pEntry);
×
2951
    TAOS_RETURN(TSDB_CODE_VND_COL_NOT_EXISTS);
×
2952
  }
2953

2954
  // do handle the entry
2955
  pEntry->version = version;
16,669✔
2956
  pTagSchema->version++;
16,669✔
2957
  code = metaHandleEntry2(pMeta, pEntry);
16,669✔
2958
  if (code) {
16,669✔
2959
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
2960
              __func__, __FILE__, __LINE__, tstrerror(code), pEntry->uid, pReq->stb, version);
2961
    metaFetchEntryFree(&pEntry);
×
2962
    TAOS_RETURN(code);
×
2963
  } else {
2964
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->stb,
16,669✔
2965
             pEntry->uid, version);
2966
  }
2967

2968
  metaFetchEntryFree(&pEntry);
16,669✔
2969
  TAOS_RETURN(code);
16,669✔
2970
}
2971

2972
int32_t metaAlterSuperTable(SMeta *pMeta, int64_t version, SVCreateStbReq *pReq) {
8,955,581✔
2973
  int32_t code = TSDB_CODE_SUCCESS;
8,955,581✔
2974

2975
  if (NULL == pReq->name || strlen(pReq->name) == 0) {
8,955,581✔
2976
    metaError("vgId:%d, %s failed at %s:%d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
24,486✔
2977
              __FILE__, __LINE__, version);
2978
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
24,486✔
2979
  }
2980

2981
  SMetaEntry *pEntry = NULL;
8,930,475✔
2982
  code = metaFetchEntryByName(pMeta, pReq->name, &pEntry);
8,936,565✔
2983
  if (code) {
8,950,820✔
2984
    metaError("vgId:%d, %s failed at %s:%d since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
20✔
2985
              __FILE__, __LINE__, pReq->name, version);
2986
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
20✔
2987
  }
2988

2989
  if (pEntry->type != TSDB_SUPER_TABLE) {
8,950,800✔
2990
    metaError("vgId:%d, %s failed at %s:%d since table %s type %d is invalid, version:%" PRId64, TD_VID(pMeta->pVnode),
×
2991
              __func__, __FILE__, __LINE__, pReq->name, pEntry->type, version);
2992
    metaFetchEntryFree(&pEntry);
×
2993
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
2994
  }
2995

2996
  SMetaEntry entry = {
26,742,691✔
2997
      .version = version,
2998
      .type = TSDB_SUPER_TABLE,
2999
      .uid = pReq->suid,
8,927,554✔
3000
      .name = pReq->name,
8,926,793✔
3001
      .stbEntry.schemaRow = pReq->schemaRow,
3002
      .stbEntry.schemaTag = pReq->schemaTag,
3003
      .stbEntry.keep = pReq->keep,
8,916,685✔
3004
      .stbEntry.ownerId = pReq->ownerId,
8,899,858✔
3005
      .stbEntry.securityLevel = pReq->securityLevel,
8,903,927✔
3006
      .colCmpr = pReq->colCmpr,
3007
      .pExtSchemas = pReq->pExtSchemas,
8,904,455✔
3008
  };
3009
  TABLE_SET_COL_COMPRESSED(entry.flags);
8,903,578✔
3010
  if (pReq->virtualStb) {
8,903,578✔
3011
    TABLE_SET_VIRTUAL(entry.flags);
25,244✔
3012
  }
3013
  if(TABLE_IS_ROLLUP(pEntry->flags)) {
8,888,269✔
3014
    TABLE_SET_ROLLUP(entry.flags);
4,968✔
3015
    entry.stbEntry.rsmaParam = pEntry->stbEntry.rsmaParam;
4,968✔
3016
  }
3017

3018
  // do handle the entry
3019
  code = metaHandleEntry2(pMeta, &entry);
8,910,377✔
3020
  if (code) {
8,930,601✔
3021
    metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " name:%s version:%" PRId64, TD_VID(pMeta->pVnode),
×
3022
              __func__, __FILE__, __LINE__, tstrerror(code), pReq->suid, pReq->name, version);
3023
    metaFetchEntryFree(&pEntry);
×
3024
    TAOS_RETURN(code);
×
3025
  } else {
3026
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated, version:%" PRId64, TD_VID(pMeta->pVnode), pReq->name,
8,930,601✔
3027
             pReq->suid, version);
3028
  }
3029

3030
  metaFetchEntryFree(&pEntry);
9,000,435✔
3031
  TAOS_RETURN(code);
8,974,294✔
3032
}
3033

3034
int32_t metaDropMultipleTables(SMeta *pMeta, int64_t version, SArray *uidArray) {
×
3035
  int32_t code = 0;
×
3036

3037
  if (taosArrayGetSize(uidArray) == 0) {
×
3038
    return TSDB_CODE_SUCCESS;
×
3039
  }
3040

3041
  for (int32_t i = 0; i < taosArrayGetSize(uidArray); i++) {
×
3042
    tb_uid_t  uid = *(tb_uid_t *)taosArrayGet(uidArray, i);
×
3043
    SMetaInfo info;
×
3044
    code = metaGetInfo(pMeta, uid, &info, NULL);
×
3045
    if (code) {
×
3046
      metaError("vgId:%d, %s failed at %s:%d since table uid %" PRId64 " not found, code:%d", TD_VID(pMeta->pVnode),
×
3047
                __func__, __FILE__, __LINE__, uid, code);
3048
      return code;
×
3049
    }
3050

3051
    SMetaEntry entry = {
×
3052
        .version = version,
3053
        .uid = uid,
3054
    };
3055

3056
    if (info.suid == 0) {
×
3057
      entry.type = -TSDB_NORMAL_TABLE;
×
3058
    } else if (info.suid == uid) {
×
3059
      entry.type = -TSDB_SUPER_TABLE;
×
3060
    } else {
3061
      entry.type = -TSDB_CHILD_TABLE;
×
3062
    }
3063
    code = metaHandleEntry2(pMeta, &entry);
×
3064
    if (code) {
×
3065
      metaError("vgId:%d, %s failed at %s:%d since %s, uid:%" PRId64 " version:%" PRId64, TD_VID(pMeta->pVnode),
×
3066
                __func__, __FILE__, __LINE__, tstrerror(code), uid, version);
3067
      return code;
×
3068
    }
3069
  }
3070
  return code;
×
3071
}
3072

3073
int metaCreateRsma(SMeta *pMeta, int64_t version, SVCreateRsmaReq *pReq) {
44,425✔
3074
  int32_t code = TSDB_CODE_SUCCESS;
44,425✔
3075

3076
  if (NULL == pReq->name || pReq->name[0] == 0) {
44,425✔
3077
    metaError("vgId:%d, failed at %d to create rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3078
              __LINE__, version);
3079
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3080
  }
3081

3082
  SMetaEntry *pEntry = NULL;
44,425✔
3083
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
44,425✔
3084
  if (code) {
44,425✔
3085
    metaError("vgId:%d, failed at %d to create rsma %s since table %s not found, version:%" PRId64,
×
3086
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3087
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
3088
  }
3089

3090
  if (pEntry->type != TSDB_SUPER_TABLE) {
44,425✔
3091
    metaError("vgId:%d, failed at %d to create rsma %s since table %s type %d is invalid, version:%" PRId64,
×
3092
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
3093
    metaFetchEntryFree(&pEntry);
×
3094
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3095
  }
3096

3097
  if (pEntry->uid != pReq->tbUid) {
44,425✔
3098
    metaError("vgId:%d, failed at %d to create rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3099
              ", version:%" PRId64,
3100
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3101
    metaFetchEntryFree(&pEntry);
×
3102
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3103
  }
3104

3105
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
44,425✔
3106
    // overwrite the old rsma definition if exists
3107
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
×
3108
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
×
3109
  } else {
3110
    TABLE_SET_ROLLUP(pEntry->flags);
44,425✔
3111
  }
3112

3113
  SMetaEntry entry = *pEntry;
44,425✔
3114
  entry.version = version;
44,425✔
3115
  entry.stbEntry.rsmaParam.name = pReq->name;
44,425✔
3116
  entry.stbEntry.rsmaParam.uid = pReq->uid;
44,425✔
3117
  entry.stbEntry.rsmaParam.interval[0] = pReq->interval[0];
44,425✔
3118
  entry.stbEntry.rsmaParam.interval[1] = pReq->interval[1];
44,425✔
3119
  entry.stbEntry.rsmaParam.intervalUnit = pReq->intervalUnit;
44,425✔
3120
  entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
44,425✔
3121
  entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
44,425✔
3122
  entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
44,425✔
3123

3124
  // do handle the entry
3125
  code = metaHandleEntry2(pMeta, &entry);
44,425✔
3126
  if (code) {
44,425✔
3127
    metaError("vgId:%d, failed at %d to create rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3128
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
3129
    metaFetchEntryFree(&pEntry);
×
3130
    TAOS_RETURN(code);
×
3131
  } else {
3132
    pMeta->pVnode->config.vndStats.numOfRSMAs++;
44,425✔
3133
    pMeta->pVnode->config.isRsma = 1;
44,425✔
3134
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
44,425✔
3135
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3136
  }
3137

3138
  metaFetchEntryFree(&pEntry);
44,425✔
3139
  TAOS_RETURN(code);
44,425✔
3140
}
3141

3142
int metaDropRsma(SMeta *pMeta, int64_t version, SVDropRsmaReq *pReq) {
22,206✔
3143
  int32_t code = TSDB_CODE_SUCCESS;
22,206✔
3144

3145
  if (NULL == pReq->name || pReq->name[0] == 0) {
22,206✔
3146
    metaError("vgId:%d, %s failed at %d since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3147
              __LINE__, version);
3148
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3149
  }
3150

3151
  if (NULL == pReq->tbName || pReq->tbName[0] == 0) {
22,206✔
3152
    metaError("vgId:%d, %s failed at %d since invalid table name, version:%" PRId64, TD_VID(pMeta->pVnode), __func__,
×
3153
              __LINE__, version);
3154
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3155
  }
3156

3157
  SMetaEntry *pEntry = NULL;
22,206✔
3158
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
22,206✔
3159
  if (code) {
22,206✔
3160
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s not found, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3161
             __func__, __LINE__, pReq->name, pReq->tbName, version);
3162
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3163
  }
3164

3165
  if (pEntry->type != pReq->tbType) {
22,206✔
3166
    metaError("vgId:%d, %s failed at %d to drop %s since table %s type %d is invalid, version:%" PRId64,
×
3167
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->type, version);
3168
    metaFetchEntryFree(&pEntry);
×
3169
    TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3170
  }
3171

3172
  if (pEntry->uid != pReq->tbUid) {
22,206✔
3173
    metaError("vgId:%d, %s failed at %d %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3174
              ", version:%" PRId64,
3175
              TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3176
    metaFetchEntryFree(&pEntry);
×
3177
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3178
  }
3179

3180
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
21,378✔
3181
    if (pEntry->stbEntry.rsmaParam.uid != pReq->uid ||
22,206✔
3182
        strncmp(pEntry->stbEntry.rsmaParam.name, pReq->name, TSDB_TABLE_NAME_LEN) != 0) {
21,378✔
3183
      metaError(
1,656✔
3184
          "vgId:%d, %s failed at line %d to drop %s since table %s is rollup table with different rsma name %s or "
3185
          "uid:%" PRIi64 ", version:%" PRId64,
3186
          TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, pEntry->stbEntry.rsmaParam.name,
3187
          pReq->uid, version);
3188
      metaFetchEntryFree(&pEntry);
1,656✔
3189
      TAOS_RETURN(TSDB_CODE_VND_INVALID_TABLE_ACTION);
×
3190
    }
3191
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
21,378✔
3192
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
22,206✔
3193
  } else {
3194
    metaWarn("vgId:%d, %s no need at %d to drop %s since table %s is not rollup table, version:%" PRId64,
×
3195
             TD_VID(pMeta->pVnode), __func__, __LINE__, pReq->name, pReq->tbName, version);
3196
    metaFetchEntryFree(&pEntry);
×
3197
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3198
  }
3199

3200
  SMetaEntry entry = *pEntry;
22,206✔
3201
  entry.version = version;
21,378✔
3202
  TABLE_RESET_ROLLUP(entry.flags);
21,378✔
3203
  entry.stbEntry.rsmaParam.uid = 0;
21,378✔
3204
  entry.stbEntry.rsmaParam.name = NULL;
21,378✔
3205
  entry.stbEntry.rsmaParam.nFuncs = 0;
21,378✔
3206
  entry.stbEntry.rsmaParam.funcColIds = NULL;
21,378✔
3207
  entry.stbEntry.rsmaParam.funcIds = NULL;
21,378✔
3208

3209
  // do handle the entry
3210
  code = metaHandleEntry2(pMeta, &entry);
21,378✔
3211
  if (code) {
22,206✔
3212
    metaError("vgId:%d, %s failed at %d to drop %s since %s, uid:%" PRId64 ", version:%" PRId64, TD_VID(pMeta->pVnode),
×
3213
              __func__, __LINE__, pReq->name, tstrerror(code), pReq->uid, version);
3214
    metaFetchEntryFree(&pEntry);
×
3215
    TAOS_RETURN(code);
×
3216
  } else {
3217
    if (--pMeta->pVnode->config.vndStats.numOfRSMAs <= 0) {
22,206✔
3218
      pMeta->pVnode->config.isRsma = 0;
×
3219
    }
3220
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma created %s:%" PRIi64 ", version:%" PRId64,
22,206✔
3221
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3222
  }
3223

3224
  metaFetchEntryFree(&pEntry);
22,206✔
3225
  TAOS_RETURN(code);
22,206✔
3226
}
3227

3228
int metaAlterRsma(SMeta *pMeta, int64_t version, SVAlterRsmaReq *pReq) {
13,587✔
3229
  int32_t code = TSDB_CODE_SUCCESS;
13,587✔
3230

3231
  if (NULL == pReq->name || pReq->name[0] == 0) {
13,587✔
3232
    metaError("vgId:%d, failed at %d to alter rsma since invalid rsma name, version:%" PRId64, TD_VID(pMeta->pVnode),
×
3233
              __LINE__, version);
3234
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3235
  }
3236

3237
  SMetaEntry *pEntry = NULL;
13,587✔
3238
  code = metaFetchEntryByName(pMeta, pReq->tbName, &pEntry);
13,587✔
3239
  if (code) {
13,587✔
3240
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s not found, version:%" PRId64,
×
3241
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3242
    TAOS_RETURN(TSDB_CODE_TDB_STB_NOT_EXIST);
×
3243
  }
3244

3245
  if (pEntry->uid != pReq->tbUid) {
13,587✔
3246
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s uid %" PRId64 " is not equal to %" PRId64
×
3247
              ", version:%" PRId64,
3248
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, pEntry->uid, pReq->tbUid, version);
3249
    metaFetchEntryFree(&pEntry);
×
3250
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
3251
  }
3252

3253
  if (TABLE_IS_ROLLUP(pEntry->flags)) {
13,587✔
3254
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcColIds);
13,587✔
3255
    taosMemoryFreeClear(pEntry->stbEntry.rsmaParam.funcIds);
13,587✔
3256
  } else {
3257
    metaError("vgId:%d, failed at %d to alter rsma %s since table %s is not rollup table, version:%" PRId64,
×
3258
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, pReq->tbName, version);
3259
    metaFetchEntryFree(&pEntry);
×
3260
    TAOS_RETURN(TSDB_CODE_RSMA_NOT_EXIST);
×
3261
  }
3262

3263
  SMetaEntry entry = *pEntry;
13,587✔
3264
  entry.version = version;
13,587✔
3265
  if (pReq->alterType == TSDB_ALTER_RSMA_FUNCTION) {
13,587✔
3266
    entry.stbEntry.rsmaParam.nFuncs = pReq->nFuncs;
13,587✔
3267
    entry.stbEntry.rsmaParam.funcColIds = pReq->funcColIds;
13,587✔
3268
    entry.stbEntry.rsmaParam.funcIds = pReq->funcIds;
13,587✔
3269
  }
3270
  // do handle the entry
3271
  code = metaHandleEntry2(pMeta, &entry);
13,587✔
3272
  if (code) {
13,587✔
3273
    metaError("vgId:%d, failed at %d to alter rsma %s since %s, uid:%" PRId64 ", version:%" PRId64,
×
3274
              TD_VID(pMeta->pVnode), __LINE__, pReq->name, tstrerror(code), pReq->tbUid, version);
3275
    metaFetchEntryFree(&pEntry);
×
3276
    TAOS_RETURN(code);
×
3277
  } else {
3278
    metaInfo("vgId:%d, table %s uid %" PRId64 " is updated since rsma altered %s:%" PRIi64 ", version:%" PRId64,
13,587✔
3279
             TD_VID(pMeta->pVnode), pReq->tbName, pReq->tbUid, pReq->name, pReq->uid, version);
3280
  }
3281

3282
  metaFetchEntryFree(&pEntry);
13,587✔
3283
  TAOS_RETURN(code);
13,587✔
3284
}
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