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

taosdata / TDengine / #4932

19 Jan 2026 12:29PM UTC coverage: 66.646% (-0.1%) from 66.749%
#4932

push

travis-ci

web-flow
chore: upgrade taospy (#34272)

202981 of 304565 relevant lines covered (66.65%)

126831443.51 hits per line

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

79.48
/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 metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
25
int32_t metaUpdateTableMultiTagValue(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,095,948✔
41
  int32_t nCols = pWp->nCols;
3,095,948✔
42
  int32_t ver = pWp->version;
3,095,948✔
43
  if (add) {
3,095,948✔
44
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
3,040,787✔
45
    if (p == NULL) {
3,040,787✔
46
      return terrno;
×
47
    }
48
    pWp->pColCmpr = p;
3,040,787✔
49

50
    SColCmpr *pCol = p + nCols;
3,040,787✔
51
    pCol->id = pSchema->colId;
3,040,787✔
52
    pCol->alg = compress;
3,040,787✔
53
    pWp->nCols = nCols + 1;
3,040,787✔
54
    pWp->version = ver;
3,040,787✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
25,956,983✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
25,956,983✔
58
      if (pOCmpr->id == pSchema->colId) {
25,956,983✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
55,161✔
60
        if (left) {
55,161✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
35,350✔
62
        }
63
        nCols--;
55,161✔
64
        break;
55,161✔
65
      }
66
    }
67
    pWp->nCols = nCols;
55,161✔
68
    pWp->version = ver;
55,161✔
69
  }
70
  return 0;
3,095,948✔
71
}
72

73
int32_t addTableExtSchema(SMetaEntry *pEntry, const SSchema *pColumn, int32_t newColNum, SExtSchema *pExtSchema) {
3,090,288✔
74
  // no need to add ext schema when no column needs ext schemas
75
  if (!HAS_TYPE_MOD(pColumn) && !pEntry->pExtSchemas) return 0;
3,090,288✔
76
  if (!pEntry->pExtSchemas) {
12,108✔
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,148✔
80
  } else {
81
    // already has columns with ext schema
82
    pEntry->pExtSchemas = (SExtSchema *)taosMemoryRealloc(pEntry->pExtSchemas, sizeof(SExtSchema) * newColNum);
9,960✔
83
  }
84
  if (!pEntry->pExtSchemas) return terrno;
12,108✔
85
  pEntry->pExtSchemas[newColNum - 1] = *pExtSchema;
12,108✔
86
  return 0;
12,108✔
87
}
88

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
82,706✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
82,706✔
92
  if (dropColId == newColNum) {
9,260✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
3,564✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
5,696✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
5,696✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
35,704✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
34,272✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,432✔
104
  return 0;
1,432✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
54,856✔
117
    if (NULL == pColRef) {
54,856✔
118
      pCol->hasRef = false;
5,355✔
119
      pCol->id = pSchema->colId;
5,355✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
49,501✔
122
      pCol->id = pSchema->colId;
49,501✔
123
      if (pCol->hasRef) {
49,501✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
20,395✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
20,395✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
20,395✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
54,856✔
130
    pWp->version++;
54,856✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
121,927✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
121,927✔
134
      if (pOColRef->id == pSchema->colId) {
121,927✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
31,151✔
136
        if (left) {
31,151✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
29,913✔
138
        }
139
        nCols--;
31,151✔
140
        break;
31,151✔
141
      }
142
    }
143
    pWp->nCols = nCols;
31,151✔
144
    pWp->version++;
31,151✔
145
  }
146
  return 0;
86,007✔
147
}
148

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

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

161
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
7,251,208✔
162
  pMetaRsp->numOfColumns = pSchema->nCols;
7,251,208✔
163
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
7,251,208✔
164
  pMetaRsp->sversion = pSchema->version;
7,251,208✔
165
  pMetaRsp->rversion = 1;
7,251,208✔
166
  pMetaRsp->tuid = uid;
7,251,208✔
167
  pMetaRsp->virtualStb = false; // super table will never be processed here
7,251,208✔
168

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
7,251,208✔
170

171
  return 0;
7,251,208✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
5,835,107✔
211
    SColRef* pColRef = &pRef->pColRef[i];
5,268,877✔
212
    if (pColRef->hasRef) {
5,268,877✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
2,873,913✔
214
      if (code) {
2,873,913✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  if (taosHashGetSize(pColRefHash) > 2000) {
566,230✔
221
    code = TSDB_CODE_VTABLE_TOO_MANY_REFERENCE;
×
222
    goto _return;
×
223
  }
224

225
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
566,230✔
226
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
566,230✔
227
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
566,230✔
228
    pMetaRsp->tuid = pEntry->uid;
324,036✔
229
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
242,194✔
230
    pMetaRsp->tuid = pEntry->uid;
242,194✔
231
    pMetaRsp->suid = pEntry->ctbEntry.suid;
242,194✔
232
  }
233

234
  pMetaRsp->tableType = tableType;
566,230✔
235
  pMetaRsp->virtualStb = false; // super table will never be processed here
566,230✔
236
  pMetaRsp->numOfColRefs = pRef->nCols;
566,230✔
237
  pMetaRsp->rversion = pRef->version;
566,230✔
238

239
  taosHashCleanup(pColRefHash);
566,230✔
240
  return code;
566,230✔
241
_return:
×
242
  taosHashCleanup(pColRefHash);
×
243
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
244
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
245
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
246
  return code;
×
247
}
248

249
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
249,843✔
250
  int32_t code = 0;
249,843✔
251

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

259
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
249,843✔
260
  tb_uid_t    tuid = pCtbEntry->uid;
249,843✔
261
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
249,843✔
262
  int32_t     nTagData = 0;
249,843✔
263

264
  SArray *pTagVals = NULL;
249,843✔
265
  code = tTagToValArray((const STag *)data, &pTagVals);
249,843✔
266
  if (code) {
249,843✔
267
    return code;
×
268
  }
269

270
  SIndexMultiTerm *terms = indexMultiTermCreate();
249,843✔
271
  if (terms == NULL) {
249,843✔
272
    return terrno;
×
273
  }
274

275
  int16_t nCols = taosArrayGetSize(pTagVals);
249,843✔
276
  for (int i = 0; i < nCols; i++) {
610,729✔
277
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
360,886✔
278
    char     type = pTagVal->type;
360,886✔
279

280
    char   *key = pTagVal->pKey;
360,886✔
281
    int32_t nKey = strlen(key);
360,886✔
282

283
    SIndexTerm *term = NULL;
360,886✔
284
    if (type == TSDB_DATA_TYPE_NULL) {
360,886✔
285
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,670✔
286
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
358,216✔
287
      if (pTagVal->nData > 0) {
340,770✔
288
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
338,990✔
289
        if (val == NULL) {
338,990✔
290
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
291
        }
292
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
338,990✔
293
        if (len < 0) {
338,990✔
294
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
295
        }
296
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
338,990✔
297
        type = TSDB_DATA_TYPE_VARCHAR;
338,990✔
298
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
338,990✔
299
        taosMemoryFree(val);
338,990✔
300
      } else if (pTagVal->nData == 0) {
1,780✔
301
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,780✔
302
      }
303
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
17,446✔
304
      double val = *(double *)(&pTagVal->i64);
13,886✔
305
      int    len = sizeof(val);
13,886✔
306
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,886✔
307
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,560✔
308
      int val = *(int *)(&pTagVal->i64);
3,560✔
309
      int len = sizeof(val);
3,560✔
310
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,560✔
311
    }
312

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

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

344
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
111,442✔
345
  tb_uid_t    tuid = pCtbEntry->uid;
111,442✔
346
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
111,442✔
347
  int32_t     nTagData = 0;
111,442✔
348

349
  SArray *pTagVals = NULL;
111,442✔
350
  code = tTagToValArray((const STag *)data, &pTagVals);
111,442✔
351
  if (code) {
111,442✔
352
    return code;
×
353
  }
354

355
  SIndexMultiTerm *terms = indexMultiTermCreate();
111,442✔
356
  if (terms == NULL) {
111,442✔
357
    return terrno;
×
358
  }
359

360
  int16_t nCols = taosArrayGetSize(pTagVals);
111,442✔
361
  for (int i = 0; i < nCols; i++) {
319,530✔
362
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
208,088✔
363
    char     type = pTagVal->type;
208,088✔
364

365
    char   *key = pTagVal->pKey;
208,088✔
366
    int32_t nKey = strlen(key);
208,088✔
367

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

419
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
18,473✔
420
  int32_t code = 0;
18,473✔
421
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
18,473✔
422

423
  int64_t    nCtbDropped = 0;
18,473✔
424
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
18,473✔
425
  if (suidHash == NULL) {
18,473✔
426
    return terrno;
×
427
  }
428

429
  metaWLock(pMeta);
18,473✔
430
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
98,893✔
431
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
80,420✔
432
    tb_uid_t suid = 0;
80,420✔
433
    int8_t   sysTbl = 0;
80,420✔
434
    int      type;
80,420✔
435
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
80,420✔
436
    if (code) return code;
80,420✔
437
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
80,420✔
438
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
75,498✔
439
      if (pVal) {
75,498✔
440
        nCtbDropped = *pVal + 1;
59,287✔
441
      } else {
442
        nCtbDropped = 1;
16,211✔
443
      }
444
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
75,498✔
445
      if (code) return code;
75,498✔
446
    }
447
    /*
448
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
449
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
450
    }
451
    */
452
    metaDebug("batch drop table:%" PRId64, uid);
80,420✔
453
  }
454
  metaULock(pMeta);
18,473✔
455

456
  // update timeseries
457
  void   *pCtbDropped = NULL;
18,473✔
458
  int32_t iter = 0;
18,473✔
459
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
34,684✔
460
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
16,211✔
461
    int32_t      nCols = 0;
16,211✔
462
    int8_t       flags = 0;
16,211✔
463
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
16,211✔
464
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
16,211✔
465
      if (!TABLE_IS_VIRTUAL(flags)) {
16,211✔
466
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
16,211✔
467
      }
468
    }
469
  }
470
  tSimpleHashCleanup(suidHash);
18,473✔
471

472
  pMeta->changed = true;
18,473✔
473
  return 0;
18,473✔
474
}
475

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

482
  TBC *pCur;
24,204✔
483
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
24,204✔
484
  if (code < 0) {
24,204✔
485
    return code;
×
486
  }
487

488
  code = tdbTbcMoveToFirst(pCur);
24,204✔
489
  if (code) {
24,204✔
490
    tdbTbcClose(pCur);
×
491
    return code;
×
492
  }
493

494
  void *pData = NULL, *pKey = NULL;
24,204✔
495
  int   nData = 0, nKey = 0;
24,204✔
496

497
  while (1) {
179,198✔
498
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
203,402✔
499
    if (ret < 0) {
203,402✔
500
      break;
24,204✔
501
    }
502

503
    SMetaEntry me = {0};
179,198✔
504
    SDecoder   dc = {0};
179,198✔
505
    tDecoderInit(&dc, pData, nData);
179,198✔
506
    code = metaDecodeEntry(&dc, &me);
179,198✔
507
    if (code < 0) {
179,198✔
508
      tDecoderClear(&dc);
×
509
      return code;
×
510
    }
511

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

531
  return 0;
24,204✔
532
}
533

534
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
24,204✔
535
  int32_t code = 0;
24,204✔
536

537
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
24,204✔
538
  if (tbUids == NULL) {
24,204✔
539
    return terrno;
×
540
  }
541

542
  code = metaFilterTableByHash(pMeta, tbUids);
24,204✔
543
  if (code != 0) {
24,204✔
544
    goto end;
×
545
  }
546
  if (TARRAY_SIZE(tbUids) == 0) {
24,204✔
547
    goto end;
5,731✔
548
  }
549

550
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
18,473✔
551
  code = metaDropTables(pMeta, tbUids);
18,473✔
552
  if (code) goto end;
18,473✔
553

554
end:
18,473✔
555
  taosArrayDestroy(tbUids);
24,204✔
556

557
  return code;
24,204✔
558
}
559

560
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
14,112,985✔
561
  metaRLock(pMeta);
14,112,985✔
562

563
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
14,120,903✔
564

565
  metaULock(pMeta);
14,108,366✔
566

567
  if (ret != 0) {
14,116,507✔
568
    metaError("ttl failed to find expired table, ret:%d", ret);
×
569
  }
570

571
  return ret;
14,117,158✔
572
}
573

574
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
80,420✔
575
  int64_t btime;
576
  if (pME->type == TSDB_CHILD_TABLE) {
80,420✔
577
    btime = pME->ctbEntry.btime;
75,498✔
578
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,922✔
579
    btime = pME->ntbEntry.btime;
4,922✔
580
  } else {
581
    return TSDB_CODE_FAILED;
×
582
  }
583

584
  btimeKey->btime = btime;
80,420✔
585
  btimeKey->uid = pME->uid;
80,420✔
586
  return 0;
80,420✔
587
}
588

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

599
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
80,420✔
600
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
80,420✔
601

602
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
80,420✔
603
  if (pME->type == TSDB_CHILD_TABLE) {
80,420✔
604
    ctx.ttlDays = pME->ctbEntry.ttlDays;
75,498✔
605
  } else {
606
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,922✔
607
  }
608

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

617
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
80,420✔
618
  void      *pData = NULL;
80,420✔
619
  int        nData = 0;
80,420✔
620
  int        rc = 0;
80,420✔
621
  SMetaEntry e = {0};
80,420✔
622
  SDecoder   dc = {0};
80,420✔
623
  int32_t    ret = 0;
80,420✔
624

625
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
80,420✔
626
  if (rc < 0) {
80,420✔
627
    return rc;
×
628
  }
629
  int64_t version = ((SUidIdxVal *)pData)[0].version;
80,420✔
630

631
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
80,420✔
632
  if (rc < 0) {
80,420✔
633
    tdbFree(pData);
×
634
    return rc;
×
635
  }
636

637
  tDecoderInit(&dc, pData, nData);
80,420✔
638
  rc = metaDecodeEntry(&dc, &e);
80,420✔
639
  if (rc < 0) {
80,420✔
640
    tDecoderClear(&dc);
×
641
    return rc;
×
642
  }
643

644
  if (type) *type = e.type;
80,420✔
645

646
  if (e.type == TSDB_CHILD_TABLE) {
80,420✔
647
    if (pSuid) *pSuid = e.ctbEntry.suid;
75,498✔
648
    void *tData = NULL;
75,498✔
649
    int   tLen = 0;
75,498✔
650

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

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

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

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

692
            const void *pTagData = NULL;
75,498✔
693
            int32_t     nTagData = 0;
75,498✔
694

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

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

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

744
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
80,420✔
745
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
80,420✔
746

747
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
80,420✔
748

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

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

777
    --pMeta->pVnode->config.vndStats.numOfNTables;
4,922✔
778
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
4,922✔
779

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

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

816
  ret = metaCacheDrop(pMeta, uid);
80,420✔
817
  if (ret < 0) {
80,420✔
818
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
80,420✔
819
              tstrerror(ret));
820
  }
821

822
  tDecoderClear(&dc);
80,420✔
823
  tdbFree(pData);
80,420✔
824

825
  return 0;
80,420✔
826
}
827

828
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
80,420✔
829
  SBtimeIdxKey btimeKey = {0};
80,420✔
830
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
80,420✔
831
    return 0;
×
832
  }
833
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
80,420✔
834
}
835

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

844
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,571,553✔
845
  pMeta->changed = true;
11,571,553✔
846
  switch (pReq->action) {
11,571,553✔
847
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,403,657✔
848
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
849
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
850
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,403,657✔
851
    case TSDB_ALTER_TABLE_DROP_COLUMN:
82,706✔
852
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
82,706✔
853
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
472,767✔
854
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
472,767✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
43,505✔
856
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
43,505✔
857
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,410,215✔
858
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,410,215✔
859
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,190✔
860
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,190✔
861
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
24,252✔
862
      return metaUpdateTableOptions2(pMeta, version, pReq);
24,252✔
863
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,958✔
864
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,958✔
865
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
72,115✔
866
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
72,115✔
867
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
53,188✔
868
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
53,188✔
869
    default:
×
870
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
871
      break;
872
  }
873
}
874

875
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
20,214,585✔
876
  if (!tsTtlChangeOnWrite) return 0;
20,214,585✔
877

878
  if (changeTimeMs <= 0) {
20,214,585✔
879
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
880
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
881
  }
882

883
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
20,214,585✔
884

885
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
20,214,585✔
886
}
887

888
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
664,028,698✔
889
  if (!tsTtlChangeOnWrite) return 0;
664,028,698✔
890

891
  metaWLock(pMeta);
20,223,750✔
892
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
20,214,585✔
893
  metaULock(pMeta);
20,214,585✔
894
  return ret;
20,214,585✔
895
}
896

897
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
77,726,435✔
898
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
899
  if (IS_VAR_DATA_TYPE(type)) {
77,726,435✔
900
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,493,540✔
901
  } else {
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
68,232,895✔
903
  }
904

905
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
77,741,524✔
906
  if (*ppTagIdxKey == NULL) {
77,734,107✔
907
    return terrno;
×
908
  }
909

910
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
77,733,768✔
911
  (*ppTagIdxKey)->cid = cid;
77,727,038✔
912
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
77,730,271✔
913
  (*ppTagIdxKey)->type = type;
77,735,639✔
914

915
  // refactor
916
  if (IS_VAR_DATA_TYPE(type)) {
77,733,261✔
917
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,523,936✔
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,510,918✔
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,512,274✔
920
  } else {
921
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
68,209,325✔
922
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
68,220,270✔
923
  }
924

925
  return 0;
77,734,361✔
926
}
927

928
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
74,694,745✔
929
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
74,694,745✔
930
}
74,699,626✔
931

932
static void colCompressDebug(SHashObj *pColCmprObj) {
79,244,953✔
933
  void *p = taosHashIterate(pColCmprObj, NULL);
79,244,953✔
934
  while (p) {
676,573,523✔
935
    uint32_t cmprAlg = *(uint32_t *)p;
597,308,741✔
936
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
597,304,510✔
937
    p = taosHashIterate(pColCmprObj, p);
597,306,442✔
938

939
    uint8_t l1, l2, lvl;
597,408,815✔
940
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
597,419,195✔
941

942
    const char *l1str = columnEncodeStr(l1);
597,432,148✔
943
    const char *l2str = columnCompressStr(l2);
597,331,571✔
944
    const char *lvlstr = columnLevelStr(lvl);
597,307,330✔
945
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
597,301,222✔
946
  }
947
  return;
79,264,782✔
948
}
949

950
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
79,268,337✔
951
  int rc = 0;
79,268,337✔
952

953
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
79,268,337✔
954
  if (pColCmprObj == NULL) {
79,264,632✔
955
    pColCmprObj = NULL;
×
956
    return TSDB_CODE_OUT_OF_MEMORY;
×
957
  }
958

959
  void      *pData = NULL;
79,264,632✔
960
  int        nData = 0;
79,265,723✔
961
  SMetaEntry e = {0};
79,267,407✔
962
  SDecoder   dc = {0};
79,268,656✔
963

964
  *ppColCmprObj = NULL;
79,270,619✔
965

966
  metaRLock(pMeta);
79,263,829✔
967
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
79,276,031✔
968
  if (rc < 0) {
79,265,946✔
969
    taosHashCleanup(pColCmprObj);
15,318✔
970
    metaULock(pMeta);
15,318✔
971
    return TSDB_CODE_FAILED;
15,318✔
972
  }
973
  int64_t version = ((SUidIdxVal *)pData)[0].version;
79,250,628✔
974
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
79,253,004✔
975
  if (rc < 0) {
79,251,970✔
976
    metaULock(pMeta);
×
977
    taosHashCleanup(pColCmprObj);
×
978
    metaError("failed to get table entry");
×
979
    return rc;
×
980
  }
981

982
  tDecoderInit(&dc, pData, nData);
79,251,970✔
983
  rc = metaDecodeEntry(&dc, &e);
79,258,269✔
984
  if (rc < 0) {
79,242,100✔
985
    tDecoderClear(&dc);
×
986
    tdbFree(pData);
×
987
    metaULock(pMeta);
×
988
    taosHashCleanup(pColCmprObj);
×
989
    return rc;
×
990
  }
991
  if (withExtSchema(e.type)) {
79,242,100✔
992
    SColCmprWrapper *p = &e.colCmpr;
79,251,276✔
993
    for (int32_t i = 0; i < p->nCols; i++) {
676,620,959✔
994
      SColCmpr *pCmpr = &p->pColCmpr[i];
597,362,253✔
995
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
597,359,895✔
996
      if (rc < 0) {
597,369,683✔
997
        tDecoderClear(&dc);
×
998
        tdbFree(pData);
×
999
        metaULock(pMeta);
×
1000
        taosHashCleanup(pColCmprObj);
×
1001
        return rc;
×
1002
      }
1003
    }
1004
  } else {
1005
    tDecoderClear(&dc);
×
1006
    tdbFree(pData);
×
1007
    metaULock(pMeta);
×
1008
    taosHashCleanup(pColCmprObj);
×
1009
    return 0;
×
1010
  }
1011
  tDecoderClear(&dc);
79,229,242✔
1012
  tdbFree(pData);
79,259,099✔
1013
  metaULock(pMeta);
79,255,543✔
1014

1015
  *ppColCmprObj = pColCmprObj;
79,251,574✔
1016
  colCompressDebug(pColCmprObj);
79,254,305✔
1017

1018
  return 0;
79,264,309✔
1019
}
1020
// refactor later
1021
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,904,955✔
1022
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,904,955✔
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