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

taosdata / TDengine / #4997

20 Mar 2026 06:10AM UTC coverage: 71.739% (-0.3%) from 72.069%
#4997

push

travis-ci

web-flow
feat: add query phase tracking for SHOW QUERIES (#34706)

148 of 183 new or added lines in 10 files covered. (80.87%)

9273 existing lines in 172 files now uncovered.

244572 of 340921 relevant lines covered (71.74%)

133392941.95 hits per line

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

79.75
/source/dnode/vnode/src/meta/metaTable.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

18
extern SDmNotifyHandle dmNotifyHdl;
19

20
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
21
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
22
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
23
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
24
int32_t metaUpdateTableMultiTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
25
int32_t metaUpdateTableChildTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
26
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
27
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
28
int32_t metaAlterTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
29
int32_t metaRemoveTableColumnRef(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
30
int32_t metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
31

32
int32_t    metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
33
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs);
34
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl);
35
void       metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
36
// opt ins_tables query
37
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
38
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
39

40
int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) {
3,297,237✔
41
  int32_t nCols = pWp->nCols;
3,297,237✔
42
  int32_t ver = pWp->version;
3,297,237✔
43
  if (add) {
3,297,237✔
44
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
3,238,689✔
45
    if (p == NULL) {
3,238,689✔
46
      return terrno;
×
47
    }
48
    pWp->pColCmpr = p;
3,238,689✔
49

50
    SColCmpr *pCol = p + nCols;
3,238,689✔
51
    pCol->id = pSchema->colId;
3,238,689✔
52
    pCol->alg = compress;
3,238,689✔
53
    pWp->nCols = nCols + 1;
3,238,689✔
54
    pWp->version = ver;
3,238,689✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
32,364,391✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
32,364,391✔
58
      if (pOCmpr->id == pSchema->colId) {
32,364,391✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
58,548✔
60
        if (left) {
58,548✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
37,671✔
62
        }
63
        nCols--;
58,548✔
64
        break;
58,548✔
65
      }
66
    }
67
    pWp->nCols = nCols;
58,548✔
68
    pWp->version = ver;
58,548✔
69
  }
70
  return 0;
3,297,237✔
71
}
72

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
3,293,496✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
3,293,496✔
76
  if (!pEntry->pExtSchemas) {
12,555✔
77
    // add a column which needs ext schema
78
    // set all extschemas to zero for all columns alrady existed
79
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryCalloc(newColNum, sizeof(SExtSchema));
2,205✔
80
  } else {
81
    // already has columns with ext schema
82
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryRealloc(pEntry->pExtSchemas, sizeof(SExtSchema) * newColNum);
10,350✔
83
  }
84
  if (!pEntry->pExtSchemas) return terrno;
12,555✔
85
  pEntry->pExtSchemas[newColNum - 1] = *pExtSchema;
12,555✔
86
  return 0;
12,555✔
87
}
88

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
88,858✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
88,858✔
92
  if (dropColId == newColNum) {
9,600✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
3,690✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
5,910✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
5,910✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
36,840✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
35,370✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,470✔
104
  return 0;
1,470✔
105
}
106

107
int32_t updataTableColRef(SColRefWrapper *pWp, const SSchema *pSchema, int8_t add, SColRef *pColRef) {
94,723✔
108
  int32_t nCols = pWp->nCols;
94,723✔
109
  if (add) {
94,723✔
110
    SColRef *p = taosMemoryRealloc(pWp->pColRef, sizeof(SColRef) * (nCols + 1));
60,536✔
111
    if (p == NULL) {
60,536✔
112
      return terrno;
×
113
    }
114
    pWp->pColRef = p;
60,536✔
115

116
    SColRef *pCol = p + nCols;
60,536✔
117
    if (NULL == pColRef) {
60,536✔
118
      pCol->hasRef = false;
5,729✔
119
      pCol->id = pSchema->colId;
5,729✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
54,807✔
122
      pCol->id = pSchema->colId;
54,807✔
123
      if (pCol->hasRef) {
54,807✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
23,394✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
23,394✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
23,394✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
60,536✔
130
    pWp->version++;
60,536✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
36,036,167✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
36,036,167✔
134
      if (pOColRef->id == pSchema->colId) {
36,036,167✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
34,187✔
136
        if (left) {
34,187✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
31,665✔
138
        }
139
        nCols--;
34,187✔
140
        break;
34,187✔
141
      }
142
    }
143
    pWp->nCols = nCols;
34,187✔
144
    pWp->version++;
34,187✔
145
  }
146
  return 0;
94,723✔
147
}
148

149
int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, int64_t ownerId, STableMetaRsp *pMetaRsp) {
10,607,514✔
150
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
10,607,514✔
151
  if (NULL == pMetaRsp->pSchemas) {
10,607,514✔
152
    return terrno;
×
153
  }
154

155
  pMetaRsp->pSchemaExt = taosMemoryCalloc(1, pSchema->nCols * sizeof(SSchemaExt));
10,607,514✔
156
  if (pMetaRsp->pSchemaExt == NULL) {
10,607,514✔
157
    taosMemoryFree(pMetaRsp->pSchemas);
×
158
    return terrno;
×
159
  }
160

161
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
10,607,514✔
162
  pMetaRsp->numOfColumns = pSchema->nCols;
10,607,514✔
163
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
10,607,514✔
164
  pMetaRsp->sversion = pSchema->version;
10,607,514✔
165
  pMetaRsp->rversion = 1;
10,607,514✔
166
  pMetaRsp->tuid = uid;
10,607,514✔
167
  pMetaRsp->virtualStb = false; // super table will never be processed here
10,607,514✔
168
  if (ownerId != 0) pMetaRsp->ownerId = ownerId;
10,607,514✔
169

170
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
10,607,514✔
171

172
  return 0;
10,607,514✔
173
}
174

175
int32_t metaUpdateVtbMetaRsp(SMetaEntry *pEntry, char *tbName, SSchemaWrapper *pSchema, SColRefWrapper *pRef,
730,150✔
176
                             int64_t ownerId, STableMetaRsp *pMetaRsp, int8_t tableType) {
177
  int32_t   code = TSDB_CODE_SUCCESS;
730,150✔
178
  SHashObj* pColRefHash = NULL;
730,150✔
179
  if (!pRef) {
730,150✔
180
    return TSDB_CODE_INVALID_PARA;
×
181
  }
182
  if (pSchema) {
730,150✔
183
    pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
426,676✔
184
    if (NULL == pMetaRsp->pSchemas) {
426,676✔
185
      code = terrno;
×
186
      goto _return;
×
187
    }
188

189
    pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
426,676✔
190
    if (pMetaRsp->pSchemaExt == NULL) {
426,676✔
191
      code = terrno;
×
192
      goto _return;
×
193
    }
194

195
    pMetaRsp->numOfColumns = pSchema->nCols;
426,676✔
196
    pMetaRsp->sversion = pSchema->version;
426,676✔
197
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
426,676✔
198
  }
199
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
730,150✔
200
  if (NULL == pMetaRsp->pColRefs) {
730,150✔
201
    code = terrno;
×
202
    goto _return;
×
203
  }
204

205
  pColRefHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
730,150✔
206
  if (!pColRefHash) {
730,150✔
207
    code = terrno;
×
208
    goto _return;
×
209
  }
210

211
  for (int32_t i = 0; i < pRef->nCols; i++) {
222,339,394✔
212
    SColRef* pColRef = &pRef->pColRef[i];
221,609,244✔
213
    if (pColRef->hasRef) {
221,609,244✔
214
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
146,995,758✔
215
      if (code) {
146,995,758✔
216
        goto _return;
×
217
      }
218
    }
219
  }
220

221
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
730,150✔
222
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
730,150✔
223
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
730,150✔
224
    pMetaRsp->tuid = pEntry->uid;
367,854✔
225
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
362,296✔
226
    pMetaRsp->tuid = pEntry->uid;
362,296✔
227
    pMetaRsp->suid = pEntry->ctbEntry.suid;
362,296✔
228
  }
229

230
  pMetaRsp->tableType = tableType;
730,150✔
231
  pMetaRsp->virtualStb = false; // super table will never be processed here
730,150✔
232
  pMetaRsp->numOfColRefs = pRef->nCols;
730,150✔
233
  pMetaRsp->rversion = pRef->version;
730,150✔
234
  if (ownerId != 0) pMetaRsp->ownerId = ownerId;
730,150✔
235

236
  taosHashCleanup(pColRefHash);
730,150✔
237
  return code;
730,150✔
238
_return:
×
239
  taosHashCleanup(pColRefHash);
×
240
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
241
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
242
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
243
  return code;
×
244
}
245

246
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
260,521✔
247
  int32_t code = 0;
260,521✔
248

249
#ifdef USE_INVERTED_INDEX
250
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
260,521✔
251
    return TSDB_CODE_INVALID_PARA;
×
252
  }
253
  void       *data = pCtbEntry->ctbEntry.pTags;
260,521✔
254
  const char *tagName = pSchema->name;
260,521✔
255

256
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
260,521✔
257
  tb_uid_t    tuid = pCtbEntry->uid;
260,521✔
258
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
260,521✔
259
  int32_t     nTagData = 0;
260,521✔
260

261
  SArray *pTagVals = NULL;
260,521✔
262
  code = tTagToValArray((const STag *)data, &pTagVals);
260,521✔
263
  if (code) {
260,521✔
264
    return code;
×
265
  }
266

267
  SIndexMultiTerm *terms = indexMultiTermCreate();
260,521✔
268
  if (terms == NULL) {
260,521✔
269
    return terrno;
×
270
  }
271

272
  int16_t nCols = taosArrayGetSize(pTagVals);
260,521✔
273
  for (int i = 0; i < nCols; i++) {
636,087✔
274
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
375,566✔
275
    char     type = pTagVal->type;
375,566✔
276

277
    char   *key = pTagVal->pKey;
375,566✔
278
    int32_t nKey = strlen(key);
375,566✔
279

280
    SIndexTerm *term = NULL;
375,566✔
281
    if (type == TSDB_DATA_TYPE_NULL) {
375,566✔
282
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,874✔
283
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
372,692✔
284
      if (pTagVal->nData > 0) {
355,775✔
285
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
353,859✔
286
        if (val == NULL) {
353,859✔
287
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
288
        }
289
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
353,859✔
290
        if (len < 0) {
353,859✔
291
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
292
        }
293
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
353,859✔
294
        type = TSDB_DATA_TYPE_VARCHAR;
353,859✔
295
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
353,859✔
296
        taosMemoryFree(val);
353,859✔
297
      } else if (pTagVal->nData == 0) {
1,916✔
298
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,916✔
299
      }
300
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
16,917✔
301
      double val = *(double *)(&pTagVal->i64);
13,085✔
302
      int    len = sizeof(val);
13,085✔
303
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,085✔
304
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,832✔
305
      int val = *(int *)(&pTagVal->i64);
3,832✔
306
      int len = sizeof(val);
3,832✔
307
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,832✔
308
    }
309

310
    if (term != NULL) {
375,566✔
311
      int32_t ret = indexMultiTermAdd(terms, term);
375,566✔
312
      if (ret < 0) {
375,566✔
313
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
314
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
315
      }
316
    } else {
317
      code = terrno;
×
318
      goto _exception;
×
319
    }
320
  }
321
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
260,521✔
322
  indexMultiTermDestroy(terms);
260,521✔
323

324
  taosArrayDestroy(pTagVals);
260,521✔
325
  return code;
260,521✔
326
_exception:
×
327
  indexMultiTermDestroy(terms);
×
328
  taosArrayDestroy(pTagVals);
×
329
#endif
330
  return code;
×
331
}
332
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
116,678✔
333
int32_t code = 0;
116,678✔
334
#ifdef USE_INVERTED_INDEX
335
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
116,678✔
336
    return TSDB_CODE_INVALID_PARA;
×
337
  }
338
  void       *data = pCtbEntry->ctbEntry.pTags;
116,678✔
339
  const char *tagName = pSchema->name;
116,678✔
340

341
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
116,678✔
342
  tb_uid_t    tuid = pCtbEntry->uid;
116,678✔
343
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
116,678✔
344
  int32_t     nTagData = 0;
116,678✔
345

346
  SArray *pTagVals = NULL;
116,678✔
347
  code = tTagToValArray((const STag *)data, &pTagVals);
116,678✔
348
  if (code) {
116,678✔
349
    return code;
×
350
  }
351

352
  SIndexMultiTerm *terms = indexMultiTermCreate();
116,678✔
353
  if (terms == NULL) {
116,678✔
354
    return terrno;
×
355
  }
356

357
  int16_t nCols = taosArrayGetSize(pTagVals);
116,678✔
358
  for (int i = 0; i < nCols; i++) {
334,539✔
359
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
217,861✔
360
    char     type = pTagVal->type;
217,861✔
361

362
    char   *key = pTagVal->pKey;
217,861✔
363
    int32_t nKey = strlen(key);
217,861✔
364

365
    SIndexTerm *term = NULL;
217,861✔
366
    if (type == TSDB_DATA_TYPE_NULL) {
217,861✔
367
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
368
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
217,861✔
369
      if (pTagVal->nData > 0) {
215,466✔
370
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
215,466✔
371
        if (val == NULL) {
215,466✔
372
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
373
        }
374
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
215,466✔
375
        if (len < 0) {
215,466✔
376
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
377
        }
378
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
215,466✔
379
        type = TSDB_DATA_TYPE_VARCHAR;
215,466✔
380
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
215,466✔
381
        taosMemoryFree(val);
215,466✔
382
      } else if (pTagVal->nData == 0) {
×
383
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
384
      }
385
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
2,395✔
386
      double val = *(double *)(&pTagVal->i64);
1,437✔
387
      int    len = sizeof(val);
1,437✔
388
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,437✔
389
    } else if (type == TSDB_DATA_TYPE_BOOL) {
958✔
390
      int val = *(int *)(&pTagVal->i64);
958✔
391
      int len = sizeof(val);
958✔
392
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
958✔
393
    }
394
    if (term != NULL) {
217,861✔
395
      int32_t ret = indexMultiTermAdd(terms, term);
217,861✔
396
      if (ret < 0) {
217,861✔
397
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
398
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
399
      }
400
    } else {
401
      code = terrno;
×
402
      goto _exception;
×
403
    }
404
  }
405
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
116,678✔
406
  indexMultiTermDestroy(terms);
116,678✔
407
  taosArrayDestroy(pTagVals);
116,678✔
408
  return code;
116,678✔
409
_exception:
×
410
  indexMultiTermDestroy(terms);
×
411
  taosArrayDestroy(pTagVals);
×
412
#endif
413
  return code;
×
414
}
415

416
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
18,928✔
417
  int32_t code = 0;
18,928✔
418
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
18,928✔
419

420
  int64_t    nCtbDropped = 0;
18,928✔
421
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
18,928✔
422
  if (suidHash == NULL) {
18,928✔
423
    return terrno;
×
424
  }
425

426
  metaWLock(pMeta);
18,928✔
427
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
98,460✔
428
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
79,532✔
429
    tb_uid_t suid = 0;
79,532✔
430
    int8_t   sysTbl = 0;
79,532✔
431
    int      type;
79,532✔
432
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
79,532✔
433
    if (code) return code;
79,532✔
434
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
79,532✔
435
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
75,123✔
436
      if (pVal) {
75,123✔
437
        nCtbDropped = *pVal + 1;
57,862✔
438
      } else {
439
        nCtbDropped = 1;
17,261✔
440
      }
441
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
75,123✔
442
      if (code) return code;
75,123✔
443
    }
444
    /*
445
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
446
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
447
    }
448
    */
449
    metaDebug("batch drop table:%" PRId64, uid);
79,532✔
450
  }
451
  metaULock(pMeta);
18,928✔
452

453
  // update timeseries
454
  void   *pCtbDropped = NULL;
18,928✔
455
  int32_t iter = 0;
18,928✔
456
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
36,189✔
457
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
17,261✔
458
    int32_t      nCols = 0;
17,261✔
459
    int8_t       flags = 0;
17,261✔
460
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
17,261✔
461
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
17,261✔
462
      if (!TABLE_IS_VIRTUAL(flags)) {
17,261✔
463
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
17,261✔
464
      }
465
    }
466
  }
467
  tSimpleHashCleanup(suidHash);
18,928✔
468

469
  pMeta->changed = true;
18,928✔
470
  return 0;
18,928✔
471
}
472

473
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
24,840✔
474
  int32_t code = 0;
24,840✔
475
  // 1, tranverse table's
476
  // 2, validate table name using vnodeValidateTableHash
477
  // 3, push invalidated table's uid into uidList
478

479
  TBC *pCur;
24,840✔
480
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
24,840✔
481
  if (code < 0) {
24,840✔
482
    return code;
×
483
  }
484

485
  code = tdbTbcMoveToFirst(pCur);
24,840✔
486
  if (code) {
24,840✔
487
    tdbTbcClose(pCur);
×
488
    return code;
×
489
  }
490

491
  void *pData = NULL, *pKey = NULL;
24,840✔
492
  int   nData = 0, nKey = 0;
24,840✔
493

494
  while (1) {
177,857✔
495
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
202,697✔
496
    if (ret < 0) {
202,697✔
497
      break;
24,840✔
498
    }
499

500
    SMetaEntry me = {0};
177,857✔
501
    SDecoder   dc = {0};
177,857✔
502
    tDecoderInit(&dc, pData, nData);
177,857✔
503
    code = metaDecodeEntry(&dc, &me);
177,857✔
504
    if (code < 0) {
177,857✔
505
      tDecoderClear(&dc);
×
506
      return code;
×
507
    }
508

509
    if (me.type != TSDB_SUPER_TABLE) {
177,857✔
510
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
159,056✔
511
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
159,056✔
512
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
159,056✔
513
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
159,056✔
514
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
159,056✔
515
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
159,056✔
516
        if (taosArrayPush(uidList, &me.uid) == NULL) {
79,532✔
517
          code = terrno;
×
518
          break;
×
519
        }
520
      }
521
    }
522
    tDecoderClear(&dc);
177,857✔
523
  }
524
  tdbFree(pData);
24,840✔
525
  tdbFree(pKey);
24,840✔
526
  tdbTbcClose(pCur);
24,840✔
527

528
  return 0;
24,840✔
529
}
530

531
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
24,840✔
532
  int32_t code = 0;
24,840✔
533

534
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
24,840✔
535
  if (tbUids == NULL) {
24,840✔
536
    return terrno;
×
537
  }
538

539
  code = metaFilterTableByHash(pMeta, tbUids);
24,840✔
540
  if (code != 0) {
24,840✔
541
    goto end;
×
542
  }
543
  if (TARRAY_SIZE(tbUids) == 0) {
24,840✔
544
    goto end;
5,912✔
545
  }
546

547
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
18,928✔
548
  code = metaDropTables(pMeta, tbUids);
18,928✔
549
  if (code) goto end;
18,928✔
550

551
end:
18,928✔
552
  taosArrayDestroy(tbUids);
24,840✔
553

554
  return code;
24,840✔
555
}
556

557
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
10,664,995✔
558
  metaRLock(pMeta);
10,664,995✔
559

560
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
10,679,575✔
561

562
  metaULock(pMeta);
10,662,983✔
563

564
  if (ret != 0) {
10,669,737✔
565
    metaError("ttl failed to find expired table, ret:%d", ret);
×
566
  }
567

568
  return ret;
10,671,042✔
569
}
570

571
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
79,532✔
572
  int64_t btime;
573
  if (pME->type == TSDB_CHILD_TABLE) {
79,532✔
574
    btime = pME->ctbEntry.btime;
75,123✔
575
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,409✔
576
    btime = pME->ntbEntry.btime;
4,409✔
577
  } else {
578
    return TSDB_CODE_FAILED;
×
579
  }
580

581
  btimeKey->btime = btime;
79,532✔
582
  btimeKey->uid = pME->uid;
79,532✔
583
  return 0;
79,532✔
584
}
585

586
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
4,409✔
587
  if (pME->type == TSDB_NORMAL_TABLE) {
4,409✔
588
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
4,409✔
589
    ncolKey->uid = pME->uid;
4,409✔
590
  } else {
591
    return TSDB_CODE_FAILED;
×
592
  }
593
  return 0;
4,409✔
594
}
595

596
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
79,532✔
597
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
79,532✔
598

599
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
79,532✔
600
  if (pME->type == TSDB_CHILD_TABLE) {
79,532✔
601
    ctx.ttlDays = pME->ctbEntry.ttlDays;
75,123✔
602
  } else {
603
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,409✔
604
  }
605

606
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
79,532✔
607
  if (ret < 0) {
79,532✔
608
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
609
              pME->uid, tstrerror(ret));
610
  }
611
  return;
79,532✔
612
}
613

614
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
79,532✔
615
  void      *pData = NULL;
79,532✔
616
  int        nData = 0;
79,532✔
617
  int        rc = 0;
79,532✔
618
  SMetaEntry e = {0};
79,532✔
619
  SDecoder   dc = {0};
79,532✔
620
  int32_t    ret = 0;
79,532✔
621

622
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
79,532✔
623
  if (rc < 0) {
79,532✔
624
    return rc;
×
625
  }
626
  int64_t version = ((SUidIdxVal *)pData)[0].version;
79,532✔
627

628
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
79,532✔
629
  if (rc < 0) {
79,532✔
630
    tdbFree(pData);
×
631
    return rc;
×
632
  }
633

634
  tDecoderInit(&dc, pData, nData);
79,532✔
635
  rc = metaDecodeEntry(&dc, &e);
79,532✔
636
  if (rc < 0) {
79,532✔
637
    tDecoderClear(&dc);
×
638
    return rc;
×
639
  }
640

641
  if (type) *type = e.type;
79,532✔
642

643
  if (e.type == TSDB_CHILD_TABLE) {
79,532✔
644
    if (pSuid) *pSuid = e.ctbEntry.suid;
75,123✔
645
    void *tData = NULL;
75,123✔
646
    int   tLen = 0;
75,123✔
647

648
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
75,123✔
649
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
75,123✔
650
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
75,123✔
651
        SDecoder   tdc = {0};
75,123✔
652
        SMetaEntry stbEntry = {0};
75,123✔
653

654
        tDecoderInit(&tdc, tData, tLen);
75,123✔
655
        ret = metaDecodeEntry(&tdc, &stbEntry);
75,123✔
656
        if (ret < 0) {
75,123✔
657
          tDecoderClear(&tdc);
×
658
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
659
                    e.ctbEntry.suid, tstrerror(ret));
660
          return ret;
×
661
        }
662

663
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
75,123✔
664
        
665
        ret = metaStableTagFilterCacheUpdateUid(
75,123✔
666
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
667
        if (ret < 0) {
75,123✔
668
          metaError("vgId:%d, failed to update stable tag filter cache:%s "
×
669
            "uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
670
            e.ctbEntry.suid, tstrerror(ret));
671
        }
672

673
        SSchema        *pTagColumn = NULL;
75,123✔
674
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
75,123✔
675
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
75,123✔
676
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
677
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
678
          if (ret < 0) {
×
679
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
680
                      e.name, e.uid, tstrerror(ret));
681
          }
682
        } else {
683
          for (int i = 0; i < pTagSchema->nCols; i++) {
384,652✔
684
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
309,529✔
685
            if (!IS_IDX_ON(pTagColumn)) continue;
309,529✔
686
            STagIdxKey *pTagIdxKey = NULL;
75,123✔
687
            int32_t     nTagIdxKey;
75,123✔
688

689
            const void *pTagData = NULL;
75,123✔
690
            int32_t     nTagData = 0;
75,123✔
691

692
            STagVal tagVal = {.cid = pTagColumn->colId};
75,123✔
693
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
75,123✔
694
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
75,123✔
695
                pTagData = tagVal.pData;
×
696
                nTagData = (int32_t)tagVal.nData;
×
697
              } else {
698
                pTagData = &(tagVal.i64);
75,123✔
699
                nTagData = tDataTypes[pTagColumn->type].bytes;
75,123✔
700
              }
701
            } else {
702
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
703
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
704
              }
705
            }
706

707
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
75,123✔
708
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
709
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
75,123✔
710
              if (ret < 0) {
75,123✔
711
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
712
                          e.name, e.uid, tstrerror(ret));
713
              }
714
            }
715
            metaDestroyTagIdxKey(pTagIdxKey);
75,123✔
716
            pTagIdxKey = NULL;
75,123✔
717
          }
718
        }
719
        tDecoderClear(&tdc);
75,123✔
720
      }
721
      tdbFree(tData);
75,123✔
722
    }
723
  }
724

725
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
79,532✔
726
  if (ret < 0) {
79,532✔
727
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
728
              tstrerror(ret));
729
  }
730
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
79,532✔
731
  if (ret < 0) {
79,532✔
732
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
733
              tstrerror(ret));
734
  }
735
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
79,532✔
736
  if (ret < 0) {
79,532✔
737
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
738
              tstrerror(ret));
739
  }
740

741
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
79,532✔
742
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
79,532✔
743

744
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
79,532✔
745

746
  if (e.type == TSDB_CHILD_TABLE) {
79,532✔
747
    ret =
748
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
75,123✔
749
    if (ret < 0) {
75,123✔
750
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
751
                tstrerror(ret));
752
    }
753

754
    --pMeta->pVnode->config.vndStats.numOfCTables;
75,123✔
755
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
75,123✔
756
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
75,123✔
757
    if (ret < 0) {
75,123✔
758
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
759
                e.ctbEntry.suid, tstrerror(ret));
760
    }
761
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
75,123✔
762
    if (ret < 0) {
75,123✔
763
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
764
                e.ctbEntry.suid, tstrerror(ret));
765
    }
766
    /*
767
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
768
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
769
    }
770
    */
771
  } else if (e.type == TSDB_NORMAL_TABLE) {
4,409✔
772
    // drop schema.db (todo)
773

774
    --pMeta->pVnode->config.vndStats.numOfNTables;
4,409✔
775
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
4,409✔
776

777
    /*
778
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
779
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
780
    }
781
    */
782
  } else if (e.type == TSDB_SUPER_TABLE) {
×
783
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
784
    if (ret < 0) {
×
785
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
786
                tstrerror(ret));
787
    }
788
    // drop schema.db (todo)
789

790
    ret = metaStatsCacheDrop(pMeta, uid);
×
791
    if (ret < 0) {
×
792
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
793
                tstrerror(ret));
794
    }
795
    ret = metaUidCacheClear(pMeta, uid);
×
796
    if (ret < 0) {
×
797
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
798
                tstrerror(ret));
799
    }
800
    ret = metaStableTagFilterCacheDropSTable(pMeta, uid);
×
801
    if (ret < 0) {
×
802
      metaError("vgId:%d, failed to clear stable tag filter cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
803
                e.uid, tstrerror(ret));
804
    }
805
    ret = metaTbGroupCacheClear(pMeta, uid);
×
806
    if (ret < 0) {
×
807
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
808
                e.uid, tstrerror(ret));
809
    }
810
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
811
  }
812

813
  ret = metaCacheDrop(pMeta, uid);
79,532✔
814
  if (ret < 0) {
79,532✔
815
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
79,532✔
816
              tstrerror(ret));
817
  }
818

819
  tDecoderClear(&dc);
79,532✔
820
  tdbFree(pData);
79,532✔
821

822
  return 0;
79,532✔
823
}
824

825
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
79,532✔
826
  SBtimeIdxKey btimeKey = {0};
79,532✔
827
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
79,532✔
828
    return 0;
×
829
  }
830
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
79,532✔
831
}
832

833
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
4,409✔
834
  SNcolIdxKey ncolKey = {0};
4,409✔
835
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
4,409✔
836
    return 0;
×
837
  }
838
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
4,409✔
839
}
840

841
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
12,383,466✔
842
  pMeta->changed = true;
12,383,466✔
843
  switch (pReq->action) {
12,383,466✔
844
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,621,728✔
845
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
846
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
847
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,621,728✔
848
    case TSDB_ALTER_TABLE_DROP_COLUMN:
88,858✔
849
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
88,858✔
850
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
488,257✔
851
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
488,257✔
852
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
40,956✔
853
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
40,956✔
854
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL:
7,918,892✔
855
      return metaUpdateTableMultiTableTagValue(pMeta, version, pReq);
7,918,892✔
856
    case TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL:
57,244✔
857
      return metaUpdateTableChildTableTagValue(pMeta, version, pReq);
57,244✔
858
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
22,782✔
859
      return metaUpdateTableOptions2(pMeta, version, pReq);
22,782✔
860
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
6,190✔
861
      return metaUpdateTableColCompress2(pMeta, version, pReq);
6,190✔
862
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
78,835✔
863
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
78,835✔
864
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
59,066✔
865
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
59,066✔
866
    default:
×
867
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
868
      break;
869
  }
870
}
871

872
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
20,887,910✔
873
  if (!tsTtlChangeOnWrite) return 0;
20,887,910✔
874

875
  if (changeTimeMs <= 0) {
20,887,910✔
876
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
877
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
878
  }
879

880
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
20,887,910✔
881

882
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
20,887,910✔
883
}
884

885
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
622,441,218✔
886
  if (!tsTtlChangeOnWrite) return 0;
622,441,218✔
887

888
  metaWLock(pMeta);
20,902,371✔
889
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
20,887,910✔
890
  metaULock(pMeta);
20,887,910✔
891
  return ret;
20,887,910✔
892
}
893

894
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
100,171,926✔
895
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
896
  if (IS_VAR_DATA_TYPE(type)) {
100,171,926✔
897
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
11,410,086✔
898
  } else {
899
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
88,761,840✔
900
  }
901

902
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
100,221,042✔
903
  if (*ppTagIdxKey == NULL) {
100,217,484✔
904
    return terrno;
×
905
  }
906

907
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
100,210,138✔
908
  (*ppTagIdxKey)->cid = cid;
100,208,275✔
909
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
100,207,416✔
910
  (*ppTagIdxKey)->type = type;
100,206,739✔
911

912
  // refactor
913
  if (IS_VAR_DATA_TYPE(type)) {
100,217,649✔
914
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
11,482,083✔
915
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
11,469,308✔
916
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
11,469,388✔
917
  } else {
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
88,735,566✔
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
88,748,252✔
920
  }
921

922
  return 0;
100,225,704✔
923
}
924

925
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
97,192,982✔
926
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
97,192,982✔
927
}
97,184,403✔
928

929
static void colCompressDebug(SHashObj *pColCmprObj) {
105,033,848✔
930
  void *p = taosHashIterate(pColCmprObj, NULL);
105,033,848✔
931
  while (p) {
901,117,519✔
932
    uint32_t cmprAlg = *(uint32_t *)p;
796,046,743✔
933
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
796,041,638✔
934
    p = taosHashIterate(pColCmprObj, p);
796,054,915✔
935

936
    uint8_t l1, l2, lvl;
796,142,124✔
937
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
796,154,721✔
938

939
    const char *l1str = columnEncodeStr(l1);
796,148,537✔
940
    const char *l2str = columnCompressStr(l2);
796,080,111✔
941
    const char *lvlstr = columnLevelStr(lvl);
796,072,678✔
942
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
796,056,530✔
943
  }
944
  return;
105,070,776✔
945
}
946

947
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
105,094,467✔
948
  int rc = 0;
105,094,467✔
949

950
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
105,094,467✔
951
  if (pColCmprObj == NULL) {
105,083,229✔
952
    pColCmprObj = NULL;
×
953
    return TSDB_CODE_OUT_OF_MEMORY;
×
954
  }
955

956
  void      *pData = NULL;
105,083,229✔
957
  int        nData = 0;
105,091,051✔
958
  SMetaEntry e = {0};
105,089,433✔
959
  SDecoder   dc = {0};
105,093,161✔
960

961
  *ppColCmprObj = NULL;
105,092,262✔
962

963
  metaRLock(pMeta);
105,080,250✔
964
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
105,110,708✔
965
  if (rc < 0) {
105,094,449✔
966
    taosHashCleanup(pColCmprObj);
43,239✔
967
    metaULock(pMeta);
43,239✔
968
    return TSDB_CODE_FAILED;
43,239✔
969
  }
970
  int64_t version = ((SUidIdxVal *)pData)[0].version;
105,051,210✔
971
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
105,044,952✔
972
  if (rc < 0) {
105,041,034✔
973
    metaULock(pMeta);
×
974
    taosHashCleanup(pColCmprObj);
×
975
    metaError("failed to get table entry");
×
976
    return rc;
×
977
  }
978

979
  tDecoderInit(&dc, pData, nData);
105,041,034✔
980
  rc = metaDecodeEntry(&dc, &e);
105,042,276✔
981
  if (rc < 0) {
105,020,342✔
982
    tDecoderClear(&dc);
×
983
    tdbFree(pData);
×
984
    metaULock(pMeta);
×
985
    taosHashCleanup(pColCmprObj);
×
986
    return rc;
×
987
  }
988
  if (withExtSchema(e.type)) {
105,020,342✔
989
    SColCmprWrapper *p = &e.colCmpr;
105,048,040✔
990
    for (int32_t i = 0; i < p->nCols; i++) {
901,151,050✔
991
      SColCmpr *pCmpr = &p->pColCmpr[i];
796,071,378✔
992
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
796,057,707✔
993
      if (rc < 0) {
796,103,010✔
994
        tDecoderClear(&dc);
×
995
        tdbFree(pData);
×
996
        metaULock(pMeta);
×
997
        taosHashCleanup(pColCmprObj);
×
998
        return rc;
×
999
      }
1000
    }
1001
  } else {
UNCOV
1002
    tDecoderClear(&dc);
×
1003
    tdbFree(pData);
×
1004
    metaULock(pMeta);
×
1005
    taosHashCleanup(pColCmprObj);
×
1006
    return 0;
×
1007
  }
1008
  tDecoderClear(&dc);
105,044,393✔
1009
  tdbFree(pData);
105,058,972✔
1010
  metaULock(pMeta);
105,048,714✔
1011

1012
  *ppColCmprObj = pColCmprObj;
105,033,102✔
1013
  colCompressDebug(pColCmprObj);
105,044,037✔
1014

1015
  return 0;
105,070,199✔
1016
}
1017
// refactor later
1018
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
4,108,675✔
1019
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
4,108,675✔
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