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

taosdata / TDengine / #4947

02 Feb 2026 09:27AM UTC coverage: 66.872% (-0.06%) from 66.932%
#4947

push

travis-ci

web-flow
enh: [6690002267] Optimize virtual table query with plenty of columns. (#34341)

527 of 634 new or added lines in 23 files covered. (83.12%)

3610 existing lines in 126 files now uncovered.

205539 of 307364 relevant lines covered (66.87%)

125933663.91 hits per line

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

79.69
/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,090,469✔
41
  int32_t nCols = pWp->nCols;
3,090,469✔
42
  int32_t ver = pWp->version;
3,090,469✔
43
  if (add) {
3,090,469✔
44
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
3,043,405✔
45
    if (p == NULL) {
3,043,405✔
46
      return terrno;
×
47
    }
48
    pWp->pColCmpr = p;
3,043,405✔
49

50
    SColCmpr *pCol = p + nCols;
3,043,405✔
51
    pCol->id = pSchema->colId;
3,043,405✔
52
    pCol->alg = compress;
3,043,405✔
53
    pWp->nCols = nCols + 1;
3,043,405✔
54
    pWp->version = ver;
3,043,405✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
9,212,821✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
9,212,821✔
58
      if (pOCmpr->id == pSchema->colId) {
9,212,821✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
47,064✔
60
        if (left) {
47,064✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
27,372✔
62
        }
63
        nCols--;
47,064✔
64
        break;
47,064✔
65
      }
66
    }
67
    pWp->nCols = nCols;
47,064✔
68
    pWp->version = ver;
47,064✔
69
  }
70
  return 0;
3,090,469✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
75,700✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
75,700✔
92
  if (dropColId == newColNum) {
9,108✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
3,500✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
5,608✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
5,608✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
34,920✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
33,528✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
1,392✔
104
  return 0;
1,392✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
55,508✔
117
    if (NULL == pColRef) {
55,508✔
118
      pCol->hasRef = false;
5,356✔
119
      pCol->id = pSchema->colId;
5,356✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
50,152✔
122
      pCol->id = pSchema->colId;
50,152✔
123
      if (pCol->hasRef) {
50,152✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
20,460✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
20,460✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
20,460✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
55,508✔
130
    pWp->version++;
55,508✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
33,538,922✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
33,538,922✔
134
      if (pOColRef->id == pSchema->colId) {
33,538,922✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
32,244✔
136
        if (left) {
32,244✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
29,984✔
138
        }
139
        nCols--;
32,244✔
140
        break;
32,244✔
141
      }
142
    }
143
    pWp->nCols = nCols;
32,244✔
144
    pWp->version++;
32,244✔
145
  }
146
  return 0;
87,752✔
147
}
148

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

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

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
9,888,335✔
170

171
  return 0;
9,888,335✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
206,613,205✔
211
    SColRef* pColRef = &pRef->pColRef[i];
206,025,395✔
212
    if (pColRef->hasRef) {
206,025,395✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
136,679,788✔
214
      if (code) {
136,679,788✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

220
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
587,810✔
221
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
587,810✔
222
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
587,810✔
223
    pMetaRsp->tuid = pEntry->uid;
334,066✔
224
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
253,744✔
225
    pMetaRsp->tuid = pEntry->uid;
253,744✔
226
    pMetaRsp->suid = pEntry->ctbEntry.suid;
253,744✔
227
  }
228

229
  pMetaRsp->tableType = tableType;
587,810✔
230
  pMetaRsp->virtualStb = false; // super table will never be processed here
587,810✔
231
  pMetaRsp->numOfColRefs = pRef->nCols;
587,810✔
232
  pMetaRsp->rversion = pRef->version;
587,810✔
233

234
  taosHashCleanup(pColRefHash);
587,810✔
235
  return code;
587,810✔
236
_return:
×
UNCOV
237
  taosHashCleanup(pColRefHash);
×
UNCOV
238
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
UNCOV
239
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
UNCOV
240
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
UNCOV
241
  return code;
×
242
}
243

244
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
248,516✔
245
  int32_t code = 0;
248,516✔
246

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

254
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
248,516✔
255
  tb_uid_t    tuid = pCtbEntry->uid;
248,516✔
256
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
248,516✔
257
  int32_t     nTagData = 0;
248,516✔
258

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

265
  SIndexMultiTerm *terms = indexMultiTermCreate();
248,516✔
266
  if (terms == NULL) {
248,516✔
UNCOV
267
    return terrno;
×
268
  }
269

270
  int16_t nCols = taosArrayGetSize(pTagVals);
248,516✔
271
  for (int i = 0; i < nCols; i++) {
607,325✔
272
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
358,809✔
273
    char     type = pTagVal->type;
358,809✔
274

275
    char   *key = pTagVal->pKey;
358,809✔
276
    int32_t nKey = strlen(key);
358,809✔
277

278
    SIndexTerm *term = NULL;
358,809✔
279
    if (type == TSDB_DATA_TYPE_NULL) {
358,809✔
280
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
2,688✔
281
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
356,121✔
282
      if (pTagVal->nData > 0) {
338,707✔
283
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
336,915✔
284
        if (val == NULL) {
336,915✔
UNCOV
285
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
286
        }
287
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
336,915✔
288
        if (len < 0) {
336,915✔
UNCOV
289
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
290
        }
291
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
336,915✔
292
        type = TSDB_DATA_TYPE_VARCHAR;
336,915✔
293
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
336,915✔
294
        taosMemoryFree(val);
336,915✔
295
      } else if (pTagVal->nData == 0) {
1,792✔
296
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
1,792✔
297
      }
298
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
17,414✔
299
      double val = *(double *)(&pTagVal->i64);
13,830✔
300
      int    len = sizeof(val);
13,830✔
301
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
13,830✔
302
    } else if (type == TSDB_DATA_TYPE_BOOL) {
3,584✔
303
      int val = *(int *)(&pTagVal->i64);
3,584✔
304
      int len = sizeof(val);
3,584✔
305
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
3,584✔
306
    }
307

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

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

339
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
110,890✔
340
  tb_uid_t    tuid = pCtbEntry->uid;
110,890✔
341
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
110,890✔
342
  int32_t     nTagData = 0;
110,890✔
343

344
  SArray *pTagVals = NULL;
110,890✔
345
  code = tTagToValArray((const STag *)data, &pTagVals);
110,890✔
346
  if (code) {
110,890✔
347
    return code;
×
348
  }
349

350
  SIndexMultiTerm *terms = indexMultiTermCreate();
110,890✔
351
  if (terms == NULL) {
110,890✔
UNCOV
352
    return terrno;
×
353
  }
354

355
  int16_t nCols = taosArrayGetSize(pTagVals);
110,890✔
356
  for (int i = 0; i < nCols; i++) {
317,949✔
357
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
207,059✔
358
    char     type = pTagVal->type;
207,059✔
359

360
    char   *key = pTagVal->pKey;
207,059✔
361
    int32_t nKey = strlen(key);
207,059✔
362

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

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

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

424
  metaWLock(pMeta);
18,150✔
425
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
96,493✔
426
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
78,343✔
427
    tb_uid_t suid = 0;
78,343✔
428
    int8_t   sysTbl = 0;
78,343✔
429
    int      type;
78,343✔
430
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
78,343✔
431
    if (code) return code;
78,343✔
432
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
78,343✔
433
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
74,156✔
434
      if (pVal) {
74,156✔
435
        nCtbDropped = *pVal + 1;
57,609✔
436
      } else {
437
        nCtbDropped = 1;
16,547✔
438
      }
439
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
74,156✔
440
      if (code) return code;
74,156✔
441
    }
442
    /*
443
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
444
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
445
    }
446
    */
447
    metaDebug("batch drop table:%" PRId64, uid);
78,343✔
448
  }
449
  metaULock(pMeta);
18,150✔
450

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

467
  pMeta->changed = true;
18,150✔
468
  return 0;
18,150✔
469
}
470

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

477
  TBC *pCur;
23,659✔
478
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
23,659✔
479
  if (code < 0) {
23,659✔
480
    return code;
×
481
  }
482

483
  code = tdbTbcMoveToFirst(pCur);
23,659✔
484
  if (code) {
23,659✔
UNCOV
485
    tdbTbcClose(pCur);
×
UNCOV
486
    return code;
×
487
  }
488

489
  void *pData = NULL, *pKey = NULL;
23,659✔
490
  int   nData = 0, nKey = 0;
23,659✔
491

492
  while (1) {
174,581✔
493
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
198,240✔
494
    if (ret < 0) {
198,240✔
495
      break;
23,659✔
496
    }
497

498
    SMetaEntry me = {0};
174,581✔
499
    SDecoder   dc = {0};
174,581✔
500
    tDecoderInit(&dc, pData, nData);
174,581✔
501
    code = metaDecodeEntry(&dc, &me);
174,581✔
502
    if (code < 0) {
174,581✔
UNCOV
503
      tDecoderClear(&dc);
×
UNCOV
504
      return code;
×
505
    }
506

507
    if (me.type != TSDB_SUPER_TABLE) {
174,581✔
508
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
156,662✔
509
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
156,662✔
510
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
156,662✔
511
      if (pMeta->pVnode->mounted) tTrimMountPrefix(tbFName);
156,662✔
512
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
156,662✔
513
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
156,662✔
514
        if (taosArrayPush(uidList, &me.uid) == NULL) {
78,343✔
UNCOV
515
          code = terrno;
×
UNCOV
516
          break;
×
517
        }
518
      }
519
    }
520
    tDecoderClear(&dc);
174,581✔
521
  }
522
  tdbFree(pData);
23,659✔
523
  tdbFree(pKey);
23,659✔
524
  tdbTbcClose(pCur);
23,659✔
525

526
  return 0;
23,659✔
527
}
528

529
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
23,659✔
530
  int32_t code = 0;
23,659✔
531

532
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
23,659✔
533
  if (tbUids == NULL) {
23,659✔
534
    return terrno;
×
535
  }
536

537
  code = metaFilterTableByHash(pMeta, tbUids);
23,659✔
538
  if (code != 0) {
23,659✔
UNCOV
539
    goto end;
×
540
  }
541
  if (TARRAY_SIZE(tbUids) == 0) {
23,659✔
542
    goto end;
5,509✔
543
  }
544

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

549
end:
18,150✔
550
  taosArrayDestroy(tbUids);
23,659✔
551

552
  return code;
23,659✔
553
}
554

555
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
9,639,204✔
556
  metaRLock(pMeta);
9,639,204✔
557

558
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
9,653,868✔
559

560
  metaULock(pMeta);
9,631,151✔
561

562
  if (ret != 0) {
9,641,514✔
UNCOV
563
    metaError("ttl failed to find expired table, ret:%d", ret);
×
564
  }
565

566
  return ret;
9,642,973✔
567
}
568

569
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
78,343✔
570
  int64_t btime;
571
  if (pME->type == TSDB_CHILD_TABLE) {
78,343✔
572
    btime = pME->ctbEntry.btime;
74,156✔
573
  } else if (pME->type == TSDB_NORMAL_TABLE) {
4,187✔
574
    btime = pME->ntbEntry.btime;
4,187✔
575
  } else {
UNCOV
576
    return TSDB_CODE_FAILED;
×
577
  }
578

579
  btimeKey->btime = btime;
78,343✔
580
  btimeKey->uid = pME->uid;
78,343✔
581
  return 0;
78,343✔
582
}
583

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

594
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
78,343✔
595
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
78,343✔
596

597
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
78,343✔
598
  if (pME->type == TSDB_CHILD_TABLE) {
78,343✔
599
    ctx.ttlDays = pME->ctbEntry.ttlDays;
74,156✔
600
  } else {
601
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,187✔
602
  }
603

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

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

620
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
78,343✔
621
  if (rc < 0) {
78,343✔
UNCOV
622
    return rc;
×
623
  }
624
  int64_t version = ((SUidIdxVal *)pData)[0].version;
78,343✔
625

626
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
78,343✔
627
  if (rc < 0) {
78,343✔
UNCOV
628
    tdbFree(pData);
×
UNCOV
629
    return rc;
×
630
  }
631

632
  tDecoderInit(&dc, pData, nData);
78,343✔
633
  rc = metaDecodeEntry(&dc, &e);
78,343✔
634
  if (rc < 0) {
78,343✔
UNCOV
635
    tDecoderClear(&dc);
×
UNCOV
636
    return rc;
×
637
  }
638

639
  if (type) *type = e.type;
78,343✔
640

641
  if (e.type == TSDB_CHILD_TABLE) {
78,343✔
642
    if (pSuid) *pSuid = e.ctbEntry.suid;
74,156✔
643
    void *tData = NULL;
74,156✔
644
    int   tLen = 0;
74,156✔
645

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

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

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

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

687
            const void *pTagData = NULL;
74,156✔
688
            int32_t     nTagData = 0;
74,156✔
689

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

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

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

739
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
78,343✔
740
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
78,343✔
741

742
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
78,343✔
743

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

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

772
    --pMeta->pVnode->config.vndStats.numOfNTables;
4,187✔
773
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
4,187✔
774

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

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

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

817
  tDecoderClear(&dc);
78,343✔
818
  tdbFree(pData);
78,343✔
819

820
  return 0;
78,343✔
821
}
822

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

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

839
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,623,482✔
840
  pMeta->changed = true;
11,623,482✔
841
  switch (pReq->action) {
11,623,482✔
842
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,399,727✔
843
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
844
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
845
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
3,399,727✔
846
    case TSDB_ALTER_TABLE_DROP_COLUMN:
75,700✔
847
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
75,700✔
848
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
476,124✔
849
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
476,124✔
850
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
43,467✔
851
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
43,467✔
852
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,470,068✔
853
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,470,068✔
854
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,205✔
855
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,205✔
856
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
24,086✔
857
      return metaUpdateTableOptions2(pMeta, version, pReq);
24,086✔
858
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,829✔
859
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,829✔
860
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
72,070✔
861
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
72,070✔
862
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
53,206✔
863
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
53,206✔
UNCOV
864
    default:
×
UNCOV
865
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
866
      break;
867
  }
868
}
869

870
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
19,790,140✔
871
  if (!tsTtlChangeOnWrite) return 0;
19,790,140✔
872

873
  if (changeTimeMs <= 0) {
19,790,140✔
UNCOV
874
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
UNCOV
875
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
876
  }
877

878
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
19,790,140✔
879

880
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
19,790,140✔
881
}
882

883
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
692,973,853✔
884
  if (!tsTtlChangeOnWrite) return 0;
692,973,853✔
885

886
  metaWLock(pMeta);
19,808,754✔
887
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
19,790,140✔
888
  metaULock(pMeta);
19,790,140✔
889
  return ret;
19,790,140✔
890
}
891

892
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
81,438,037✔
893
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
894
  if (IS_VAR_DATA_TYPE(type)) {
81,438,037✔
895
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,468,026✔
896
  } else {
897
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
71,970,011✔
898
  }
899

900
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
81,463,255✔
901
  if (*ppTagIdxKey == NULL) {
81,453,375✔
UNCOV
902
    return terrno;
×
903
  }
904

905
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
81,443,186✔
906
  (*ppTagIdxKey)->cid = cid;
81,456,194✔
907
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
81,455,581✔
908
  (*ppTagIdxKey)->type = type;
81,466,795✔
909

910
  // refactor
911
  if (IS_VAR_DATA_TYPE(type)) {
81,453,781✔
912
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,501,656✔
913
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,488,492✔
914
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,485,913✔
915
  } else {
916
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
71,952,125✔
917
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
71,955,793✔
918
  }
919

920
  return 0;
81,442,373✔
921
}
922

923
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
78,455,987✔
924
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
78,455,987✔
925
}
78,458,971✔
926

927
static void colCompressDebug(SHashObj *pColCmprObj) {
94,429,982✔
928
  void *p = taosHashIterate(pColCmprObj, NULL);
94,429,982✔
929
  while (p) {
767,123,526✔
930
    uint32_t cmprAlg = *(uint32_t *)p;
672,662,188✔
931
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
672,670,933✔
932
    p = taosHashIterate(pColCmprObj, p);
672,666,311✔
933

934
    uint8_t l1, l2, lvl;
672,876,493✔
935
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
672,861,291✔
936

937
    const char *l1str = columnEncodeStr(l1);
672,859,964✔
938
    const char *l2str = columnCompressStr(l2);
672,728,604✔
939
    const char *lvlstr = columnLevelStr(lvl);
672,693,718✔
940
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
672,656,993✔
941
  }
942
  return;
94,461,338✔
943
}
944

945
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
94,474,177✔
946
  int rc = 0;
94,474,177✔
947

948
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
94,474,177✔
949
  if (pColCmprObj == NULL) {
94,476,067✔
UNCOV
950
    pColCmprObj = NULL;
×
UNCOV
951
    return TSDB_CODE_OUT_OF_MEMORY;
×
952
  }
953

954
  void      *pData = NULL;
94,476,067✔
955
  int        nData = 0;
94,474,639✔
956
  SMetaEntry e = {0};
94,488,675✔
957
  SDecoder   dc = {0};
94,484,975✔
958

959
  *ppColCmprObj = NULL;
94,487,114✔
960

961
  metaRLock(pMeta);
94,482,211✔
962
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
94,496,667✔
963
  if (rc < 0) {
94,477,939✔
964
    taosHashCleanup(pColCmprObj);
28,977✔
965
    metaULock(pMeta);
28,977✔
966
    return TSDB_CODE_FAILED;
28,977✔
967
  }
968
  int64_t version = ((SUidIdxVal *)pData)[0].version;
94,448,962✔
969
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
94,458,846✔
970
  if (rc < 0) {
94,443,618✔
UNCOV
971
    metaULock(pMeta);
×
UNCOV
972
    taosHashCleanup(pColCmprObj);
×
UNCOV
973
    metaError("failed to get table entry");
×
UNCOV
974
    return rc;
×
975
  }
976

977
  tDecoderInit(&dc, pData, nData);
94,443,618✔
978
  rc = metaDecodeEntry(&dc, &e);
94,459,086✔
979
  if (rc < 0) {
94,412,809✔
UNCOV
980
    tDecoderClear(&dc);
×
UNCOV
981
    tdbFree(pData);
×
UNCOV
982
    metaULock(pMeta);
×
UNCOV
983
    taosHashCleanup(pColCmprObj);
×
UNCOV
984
    return rc;
×
985
  }
986
  if (withExtSchema(e.type)) {
94,412,809✔
987
    SColCmprWrapper *p = &e.colCmpr;
94,444,380✔
988
    for (int32_t i = 0; i < p->nCols; i++) {
767,173,914✔
989
      SColCmpr *pCmpr = &p->pColCmpr[i];
672,719,984✔
990
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
672,718,954✔
991
      if (rc < 0) {
672,729,534✔
UNCOV
992
        tDecoderClear(&dc);
×
UNCOV
993
        tdbFree(pData);
×
UNCOV
994
        metaULock(pMeta);
×
995
        taosHashCleanup(pColCmprObj);
×
996
        return rc;
×
997
      }
998
    }
999
  } else {
UNCOV
1000
    tDecoderClear(&dc);
×
UNCOV
1001
    tdbFree(pData);
×
UNCOV
1002
    metaULock(pMeta);
×
UNCOV
1003
    taosHashCleanup(pColCmprObj);
×
UNCOV
1004
    return 0;
×
1005
  }
1006
  tDecoderClear(&dc);
94,413,019✔
1007
  tdbFree(pData);
94,448,898✔
1008
  metaULock(pMeta);
94,448,062✔
1009

1010
  *ppColCmprObj = pColCmprObj;
94,442,270✔
1011
  colCompressDebug(pColCmprObj);
94,440,790✔
1012

1013
  return 0;
94,461,046✔
1014
}
1015
// refactor later
1016
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,868,133✔
1017
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,868,133✔
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