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

taosdata / TDengine / #4440

04 Jul 2025 02:10AM UTC coverage: 63.29% (-0.4%) from 63.643%
#4440

push

travis-ci

web-flow
fix:(stmt2) heap buffer overflow (#31607)

159782 of 321690 branches covered (49.67%)

Branch coverage included in aggregate %.

19 of 22 new or added lines in 3 files covered. (86.36%)

5735 existing lines in 195 files now uncovered.

246739 of 320626 relevant lines covered (76.96%)

6757056.21 hits per line

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

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

50
    SColCmpr *pCol = p + nCols;
720✔
51
    pCol->id = pSchema->colId;
720✔
52
    pCol->alg = compress;
720✔
53
    pWp->nCols = nCols + 1;
720✔
54
    pWp->version = ver;
720✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
21,527!
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
21,527✔
58
      if (pOCmpr->id == pSchema->colId) {
21,527✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
170✔
60
        if (left) {
170✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
102✔
62
        }
63
        nCols--;
170✔
64
        break;
170✔
65
      }
66
    }
67
    pWp->nCols = nCols;
170✔
68
    pWp->version = ver;
170✔
69
  }
70
  return 0;
890✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
216✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
216✔
92
  if (dropColId == newColNum) {
65✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
25✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
40✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
40✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
250✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
240✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
10!
104
  return 0;
10✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
92✔
117
    if (NULL == pColRef) {
92✔
118
      pCol->hasRef = false;
11✔
119
      pCol->id = pSchema->colId;
11✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
81✔
122
      pCol->id = pSchema->colId;
81✔
123
      if (pCol->hasRef) {
81✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
32✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
32✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
32✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
92✔
130
    pWp->version++;
92✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
165!
133
      SColRef *pOColRef = &pWp->pColRef[i];
165✔
134
      if (pOColRef->id == pSchema->colId) {
165✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
53✔
136
        if (left) {
53✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
47✔
138
        }
139
        nCols--;
53✔
140
        break;
53✔
141
      }
142
    }
143
    pWp->nCols = nCols;
53✔
144
    pWp->version++;
53✔
145
  }
146
  return 0;
145✔
147
}
148

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

155
  pMetaRsp->pSchemaExt = taosMemoryCalloc(1, pSchema->nCols * sizeof(SSchemaExt));
17,773!
156
  if (pMetaRsp->pSchemaExt == NULL) {
17,774!
157
    taosMemoryFree(pMetaRsp->pSchemas);
×
158
    return terrno;
×
159
  }
160

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

169
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
17,774✔
170

171
  return 0;
17,774✔
172
}
173

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

187
    pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
437!
188
    if (pMetaRsp->pSchemaExt == NULL) {
437!
189
      code = terrno;
×
190
      goto _return;
×
191
    }
192

193
    pMetaRsp->numOfColumns = pSchema->nCols;
437✔
194
    pMetaRsp->sversion = pSchema->version;
437✔
195
    memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
437✔
196
  }
197
  pMetaRsp->pColRefs = taosMemoryMalloc(pRef->nCols * sizeof(SColRef));
504!
198
  if (NULL == pMetaRsp->pColRefs) {
504!
199
    code = terrno;
×
200
    goto _return;
×
201
  }
202
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
504✔
203
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
504✔
204
  pMetaRsp->tuid = uid;
504✔
205
  pMetaRsp->tableType = tableType;
504✔
206
  pMetaRsp->virtualStb = false; // super table will never be processed here
504✔
207
  pMetaRsp->numOfColRefs = pRef->nCols;
504✔
208
  pMetaRsp->rversion = pRef->version;
504✔
209

210
  return code;
504✔
211
_return:
×
212
  taosMemoryFreeClear(pMetaRsp->pSchemaExt);
×
213
  taosMemoryFreeClear(pMetaRsp->pSchemas);
×
214
  taosMemoryFreeClear(pMetaRsp->pColRefs);
×
215
  return code;
×
216
}
217

218
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
692✔
219
  int32_t code = 0;
692✔
220

221
#ifdef USE_INVERTED_INDEX
222
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
692!
223
    return TSDB_CODE_INVALID_PARA;
×
224
  }
225
  void       *data = pCtbEntry->ctbEntry.pTags;
692✔
226
  const char *tagName = pSchema->name;
692✔
227

228
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
692✔
229
  tb_uid_t    tuid = pCtbEntry->uid;
692✔
230
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
692✔
231
  int32_t     nTagData = 0;
692✔
232

233
  SArray *pTagVals = NULL;
692✔
234
  code = tTagToValArray((const STag *)data, &pTagVals);
692✔
235
  if (code) {
692!
236
    return code;
×
237
  }
238

239
  SIndexMultiTerm *terms = indexMultiTermCreate();
692✔
240
  if (terms == NULL) {
692!
241
    return terrno;
×
242
  }
243

244
  int16_t nCols = taosArrayGetSize(pTagVals);
692✔
245
  for (int i = 0; i < nCols; i++) {
1,579✔
246
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
887✔
247
    char     type = pTagVal->type;
887✔
248

249
    char   *key = pTagVal->pKey;
887✔
250
    int32_t nKey = strlen(key);
887✔
251

252
    SIndexTerm *term = NULL;
887✔
253
    if (type == TSDB_DATA_TYPE_NULL) {
887✔
254
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
24✔
255
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
863✔
256
      if (pTagVal->nData > 0) {
758✔
257
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
742!
258
        if (val == NULL) {
742!
259
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
260
        }
261
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
742✔
262
        if (len < 0) {
742!
263
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
264
        }
265
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
742✔
266
        type = TSDB_DATA_TYPE_VARCHAR;
742✔
267
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
742✔
268
        taosMemoryFree(val);
742!
269
      } else if (pTagVal->nData == 0) {
16!
270
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
16✔
271
      }
272
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
105✔
273
      double val = *(double *)(&pTagVal->i64);
81✔
274
      int    len = sizeof(val);
81✔
275
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
81✔
276
    } else if (type == TSDB_DATA_TYPE_BOOL) {
24!
277
      int val = *(int *)(&pTagVal->i64);
24✔
278
      int len = sizeof(val);
24✔
279
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
24✔
280
    }
281

282
    if (term != NULL) {
887!
283
      int32_t ret = indexMultiTermAdd(terms, term);
887✔
284
      if (ret < 0) {
887!
285
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
286
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
287
      }
288
    } else {
289
      code = terrno;
×
290
      goto _exception;
×
291
    }
292
  }
293
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
692✔
294
  indexMultiTermDestroy(terms);
692✔
295

296
  taosArrayDestroy(pTagVals);
692✔
297
  return code;
692✔
298
_exception:
×
299
  indexMultiTermDestroy(terms);
×
300
  taosArrayDestroy(pTagVals);
×
301
#endif
302
  return code;
×
303
}
304
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
205✔
305
int32_t code = 0;
205✔
306
#ifdef USE_INVERTED_INDEX
307
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
205!
308
    return TSDB_CODE_INVALID_PARA;
×
309
  }
310
  void       *data = pCtbEntry->ctbEntry.pTags;
205✔
311
  const char *tagName = pSchema->name;
205✔
312

313
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
205✔
314
  tb_uid_t    tuid = pCtbEntry->uid;
205✔
315
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
205✔
316
  int32_t     nTagData = 0;
205✔
317

318
  SArray *pTagVals = NULL;
205✔
319
  code = tTagToValArray((const STag *)data, &pTagVals);
205✔
320
  if (code) {
205!
321
    return code;
×
322
  }
323

324
  SIndexMultiTerm *terms = indexMultiTermCreate();
205✔
325
  if (terms == NULL) {
205!
326
    return terrno;
×
327
  }
328

329
  int16_t nCols = taosArrayGetSize(pTagVals);
205✔
330
  for (int i = 0; i < nCols; i++) {
589✔
331
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
384✔
332
    char     type = pTagVal->type;
384✔
333

334
    char   *key = pTagVal->pKey;
384✔
335
    int32_t nKey = strlen(key);
384✔
336

337
    SIndexTerm *term = NULL;
384✔
338
    if (type == TSDB_DATA_TYPE_NULL) {
384!
339
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
340
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
384✔
341
      if (pTagVal->nData > 0) {
364!
342
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
364!
343
        if (val == NULL) {
364!
344
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
345
        }
346
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
364✔
347
        if (len < 0) {
364!
348
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
349
        }
350
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
364✔
351
        type = TSDB_DATA_TYPE_VARCHAR;
364✔
352
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
364✔
353
        taosMemoryFree(val);
364!
354
      } else if (pTagVal->nData == 0) {
×
355
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
356
      }
357
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
20✔
358
      double val = *(double *)(&pTagVal->i64);
12✔
359
      int    len = sizeof(val);
12✔
360
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
12✔
361
    } else if (type == TSDB_DATA_TYPE_BOOL) {
8!
362
      int val = *(int *)(&pTagVal->i64);
8✔
363
      int len = sizeof(val);
8✔
364
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
8✔
365
    }
366
    if (term != NULL) {
384!
367
      int32_t ret = indexMultiTermAdd(terms, term);
384✔
368
      if (ret < 0) {
384!
369
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
370
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
371
      }
372
    } else {
373
      code = terrno;
×
374
      goto _exception;
×
375
    }
376
  }
377
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
205✔
378
  indexMultiTermDestroy(terms);
205✔
379
  taosArrayDestroy(pTagVals);
205✔
380
  return code;
205✔
381
_exception:
×
382
  indexMultiTermDestroy(terms);
×
383
  taosArrayDestroy(pTagVals);
×
384
#endif
385
  return code;
×
386
}
387

388
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
55✔
389
  int32_t code = 0;
55✔
390
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
55!
391

392
  int64_t    nCtbDropped = 0;
55✔
393
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
55✔
394
  if (suidHash == NULL) {
55!
395
    return terrno;
×
396
  }
397

398
  metaWLock(pMeta);
55✔
399
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
329✔
400
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
274✔
401
    tb_uid_t suid = 0;
274✔
402
    int8_t   sysTbl = 0;
274✔
403
    int      type;
404
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
274✔
405
    if (code) return code;
274!
406
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
274!
407
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
263✔
408
      if (pVal) {
263✔
409
        nCtbDropped = *pVal + 1;
214✔
410
      } else {
411
        nCtbDropped = 1;
49✔
412
      }
413
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
263✔
414
      if (code) return code;
263!
415
    }
416
    /*
417
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
418
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
419
    }
420
    */
421
    metaDebug("batch drop table:%" PRId64, uid);
274✔
422
  }
423
  metaULock(pMeta);
55✔
424

425
  // update timeseries
426
  void   *pCtbDropped = NULL;
55✔
427
  int32_t iter = 0;
55✔
428
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
104✔
429
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
49✔
430
    int32_t      nCols = 0;
49✔
431
    int8_t       flags = 0;
49✔
432
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
49✔
433
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols, &flags) == 0) {
49!
434
      if (!TABLE_IS_VIRTUAL(flags)) {
49!
435
        pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
49✔
436
      }
437
    }
438
  }
439
  tSimpleHashCleanup(suidHash);
55✔
440

441
  pMeta->changed = true;
55✔
442
  return 0;
55✔
443
}
444

445
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
80✔
446
  int32_t code = 0;
80✔
447
  // 1, tranverse table's
448
  // 2, validate table name using vnodeValidateTableHash
449
  // 3, push invalidated table's uid into uidList
450

451
  TBC *pCur;
452
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
80✔
453
  if (code < 0) {
80!
454
    return code;
×
455
  }
456

457
  code = tdbTbcMoveToFirst(pCur);
80✔
458
  if (code) {
80!
459
    tdbTbcClose(pCur);
×
460
    return code;
×
461
  }
462

463
  void *pData = NULL, *pKey = NULL;
80✔
464
  int   nData = 0, nKey = 0;
80✔
465

466
  while (1) {
610✔
467
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
690✔
468
    if (ret < 0) {
690✔
469
      break;
80✔
470
    }
471

472
    SMetaEntry me = {0};
610✔
473
    SDecoder   dc = {0};
610✔
474
    tDecoderInit(&dc, pData, nData);
610✔
475
    code = metaDecodeEntry(&dc, &me);
610✔
476
    if (code < 0) {
610!
477
      tDecoderClear(&dc);
×
478
      return code;
×
479
    }
480

481
    if (me.type != TSDB_SUPER_TABLE) {
610✔
482
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
483
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
548✔
484
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
548✔
485
      ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
548✔
486
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
548!
487
        if (taosArrayPush(uidList, &me.uid) == NULL) {
274!
488
          code = terrno;
×
489
          break;
×
490
        }
491
      }
492
    }
493
    tDecoderClear(&dc);
610✔
494
  }
495
  tdbFree(pData);
80✔
496
  tdbFree(pKey);
80✔
497
  tdbTbcClose(pCur);
80✔
498

499
  return 0;
80✔
500
}
501

502
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
80✔
503
  int32_t code = 0;
80✔
504

505
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
80✔
506
  if (tbUids == NULL) {
80!
507
    return terrno;
×
508
  }
509

510
  code = metaFilterTableByHash(pMeta, tbUids);
80✔
511
  if (code != 0) {
80!
512
    goto end;
×
513
  }
514
  if (TARRAY_SIZE(tbUids) == 0) {
80✔
515
    goto end;
25✔
516
  }
517

518
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
55!
519
  code = metaDropTables(pMeta, tbUids);
55✔
520
  if (code) goto end;
55!
521

522
end:
55✔
523
  taosArrayDestroy(tbUids);
80✔
524

525
  return code;
80✔
526
}
527

528
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
21,850✔
529
  metaRLock(pMeta);
21,850✔
530

531
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
21,870✔
532

533
  metaULock(pMeta);
21,857✔
534

535
  if (ret != 0) {
21,871!
536
    metaError("ttl failed to find expired table, ret:%d", ret);
×
537
  }
538

539
  return ret;
21,874✔
540
}
541

542
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
274✔
543
  int64_t btime;
544
  if (pME->type == TSDB_CHILD_TABLE) {
274✔
545
    btime = pME->ctbEntry.btime;
263✔
546
  } else if (pME->type == TSDB_NORMAL_TABLE) {
11!
547
    btime = pME->ntbEntry.btime;
11✔
548
  } else {
549
    return TSDB_CODE_FAILED;
×
550
  }
551

552
  btimeKey->btime = btime;
274✔
553
  btimeKey->uid = pME->uid;
274✔
554
  return 0;
274✔
555
}
556

557
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
11✔
558
  if (pME->type == TSDB_NORMAL_TABLE) {
11!
559
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
11✔
560
    ncolKey->uid = pME->uid;
11✔
561
  } else {
562
    return TSDB_CODE_FAILED;
×
563
  }
564
  return 0;
11✔
565
}
566

567
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
274✔
568
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
274!
569

570
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
274✔
571
  if (pME->type == TSDB_CHILD_TABLE) {
274✔
572
    ctx.ttlDays = pME->ctbEntry.ttlDays;
263✔
573
  } else {
574
    ctx.ttlDays = pME->ntbEntry.ttlDays;
11✔
575
  }
576

577
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
274✔
578
  if (ret < 0) {
274!
579
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
580
              pME->uid, tstrerror(ret));
581
  }
582
  return;
274✔
583
}
584

585
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
274✔
586
  void      *pData = NULL;
274✔
587
  int        nData = 0;
274✔
588
  int        rc = 0;
274✔
589
  SMetaEntry e = {0};
274✔
590
  SDecoder   dc = {0};
274✔
591
  int32_t    ret = 0;
274✔
592

593
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
274✔
594
  if (rc < 0) {
274!
595
    return rc;
×
596
  }
597
  int64_t version = ((SUidIdxVal *)pData)[0].version;
274✔
598

599
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
274✔
600
  if (rc < 0) {
274!
601
    tdbFree(pData);
×
602
    return rc;
×
603
  }
604

605
  tDecoderInit(&dc, pData, nData);
274✔
606
  rc = metaDecodeEntry(&dc, &e);
274✔
607
  if (rc < 0) {
274!
608
    tDecoderClear(&dc);
×
609
    return rc;
×
610
  }
611

612
  if (type) *type = e.type;
274!
613

614
  if (e.type == TSDB_CHILD_TABLE) {
274✔
615
    if (pSuid) *pSuid = e.ctbEntry.suid;
263!
616
    void *tData = NULL;
263✔
617
    int   tLen = 0;
263✔
618

619
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
263!
620
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
263✔
621
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
263!
622
        SDecoder   tdc = {0};
263✔
623
        SMetaEntry stbEntry = {0};
263✔
624

625
        tDecoderInit(&tdc, tData, tLen);
263✔
626
        ret = metaDecodeEntry(&tdc, &stbEntry);
263✔
627
        if (ret < 0) {
263!
628
          tDecoderClear(&tdc);
×
629
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
630
                    e.ctbEntry.suid, tstrerror(ret));
631
          return ret;
×
632
        }
633

634
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
263!
635

636
        SSchema        *pTagColumn = NULL;
263✔
637
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
263✔
638
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
263!
639
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
640
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
641
          if (ret < 0) {
×
642
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
643
                      e.name, e.uid, tstrerror(ret));
644
          }
645
        } else {
646
          for (int i = 0; i < pTagSchema->nCols; i++) {
1,410✔
647
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
1,147✔
648
            if (!IS_IDX_ON(pTagColumn)) continue;
1,147✔
649
            STagIdxKey *pTagIdxKey = NULL;
263✔
650
            int32_t     nTagIdxKey;
651

652
            const void *pTagData = NULL;
263✔
653
            int32_t     nTagData = 0;
263✔
654

655
            STagVal tagVal = {.cid = pTagColumn->colId};
263✔
656
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
263!
657
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
263!
658
                pTagData = tagVal.pData;
×
659
                nTagData = (int32_t)tagVal.nData;
×
660
              } else {
661
                pTagData = &(tagVal.i64);
263✔
662
                nTagData = tDataTypes[pTagColumn->type].bytes;
263✔
663
              }
664
            } else {
665
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
666
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
667
              }
668
            }
669

670
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
263!
671
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
672
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
263✔
673
              if (ret < 0) {
263!
674
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
675
                          e.name, e.uid, tstrerror(ret));
676
              }
677
            }
678
            metaDestroyTagIdxKey(pTagIdxKey);
263✔
679
            pTagIdxKey = NULL;
263✔
680
          }
681
        }
682
        tDecoderClear(&tdc);
263✔
683
      }
684
      tdbFree(tData);
263✔
685
    }
686
  }
687

688
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
274✔
689
  if (ret < 0) {
274!
690
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
691
              tstrerror(ret));
692
  }
693
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
274✔
694
  if (ret < 0) {
274!
695
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
696
              tstrerror(ret));
697
  }
698
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
274✔
699
  if (ret < 0) {
274!
700
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
701
              tstrerror(ret));
702
  }
703

704
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
274!
705
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
274✔
706

707
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
274!
708

709
  if (e.type == TSDB_CHILD_TABLE) {
274✔
710
    ret =
711
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
263✔
712
    if (ret < 0) {
263!
713
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
714
                tstrerror(ret));
715
    }
716

717
    --pMeta->pVnode->config.vndStats.numOfCTables;
263✔
718
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
263✔
719
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
263✔
720
    if (ret < 0) {
263!
721
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
722
                e.ctbEntry.suid, tstrerror(ret));
723
    }
724
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
263✔
725
    if (ret < 0) {
263!
726
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
727
                e.ctbEntry.suid, tstrerror(ret));
728
    }
729
    /*
730
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
731
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
732
    }
733
    */
734
  } else if (e.type == TSDB_NORMAL_TABLE) {
11!
735
    // drop schema.db (todo)
736

737
    --pMeta->pVnode->config.vndStats.numOfNTables;
11✔
738
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
11✔
739

740
    /*
741
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
742
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
743
    }
744
    */
745
  } else if (e.type == TSDB_SUPER_TABLE) {
×
746
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
747
    if (ret < 0) {
×
748
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
749
                tstrerror(ret));
750
    }
751
    // drop schema.db (todo)
752

753
    ret = metaStatsCacheDrop(pMeta, uid);
×
754
    if (ret < 0) {
×
755
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
756
                tstrerror(ret));
757
    }
758
    ret = metaUidCacheClear(pMeta, uid);
×
759
    if (ret < 0) {
×
760
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
761
                tstrerror(ret));
762
    }
763
    ret = metaTbGroupCacheClear(pMeta, uid);
×
764
    if (ret < 0) {
×
765
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
766
                e.uid, tstrerror(ret));
767
    }
768
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
769
  }
770

771
  ret = metaCacheDrop(pMeta, uid);
274✔
772
  if (ret < 0) {
274!
773
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
274!
774
              tstrerror(ret));
775
  }
776

777
  tDecoderClear(&dc);
274✔
778
  tdbFree(pData);
274✔
779

780
  return 0;
274✔
781
}
782

783
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
274✔
784
  SBtimeIdxKey btimeKey = {0};
274✔
785
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
274!
786
    return 0;
×
787
  }
788
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
274✔
789
}
790

791
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
11✔
792
  SNcolIdxKey ncolKey = {0};
11✔
793
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
11!
794
    return 0;
×
795
  }
796
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
11✔
797
}
798

799
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
6,307✔
800
  pMeta->changed = true;
6,307✔
801
  switch (pReq->action) {
6,307!
802
    case TSDB_ALTER_TABLE_ADD_COLUMN:
1,308✔
803
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
804
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF:
805
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
1,308✔
806
    case TSDB_ALTER_TABLE_DROP_COLUMN:
227✔
807
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
227✔
808
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
788✔
809
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
788✔
810
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
84✔
811
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
84✔
812
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
3,654✔
813
      return metaUpdateTableTagValue(pMeta, version, pReq);
3,654✔
814
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
10✔
815
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
10✔
816
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
104✔
817
      return metaUpdateTableOptions2(pMeta, version, pReq);
104✔
818
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
16✔
819
      return metaUpdateTableColCompress2(pMeta, version, pReq);
16✔
820
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
76✔
821
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
76✔
822
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
40✔
823
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
40✔
824
    default:
×
825
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
826
      break;
827
  }
828
}
829

830
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
1✔
831
  if (!tsTtlChangeOnWrite) return 0;
1!
832

833
  if (changeTimeMs <= 0) {
1!
834
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
835
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
836
  }
837

838
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
1✔
839

840
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
1✔
841
}
842

843
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
3,841,907✔
844
  if (!tsTtlChangeOnWrite) return 0;
3,841,907!
845

846
  metaWLock(pMeta);
×
847
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
1✔
848
  metaULock(pMeta);
1✔
849
  return ret;
1✔
850
}
851

852
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
219,707✔
853
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
854
  if (IS_VAR_DATA_TYPE(type)) {
219,707✔
855
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
36,963✔
856
  } else {
857
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
182,744✔
858
  }
859

860
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
219,707!
861
  if (*ppTagIdxKey == NULL) {
219,756!
862
    return terrno;
×
863
  }
864

865
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
219,756✔
866
  (*ppTagIdxKey)->cid = cid;
219,756✔
867
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
219,756✔
868
  (*ppTagIdxKey)->type = type;
219,756✔
869

870
  // refactor
871
  if (IS_VAR_DATA_TYPE(type)) {
219,756✔
872
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
37,020✔
873
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
37,020✔
874
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
37,020✔
875
  } else {
876
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
182,736✔
877
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
182,736✔
878
  }
879

880
  return 0;
219,756✔
881
}
882

883
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
214,982✔
884
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
214,982!
885
}
214,971✔
886

887
static void colCompressDebug(SHashObj *pColCmprObj) {
720,898✔
888
  void *p = taosHashIterate(pColCmprObj, NULL);
720,898✔
889
  while (p) {
7,663,367✔
890
    uint32_t cmprAlg = *(uint32_t *)p;
6,942,617✔
891
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
6,942,617✔
892
    p = taosHashIterate(pColCmprObj, p);
6,942,489✔
893

894
    uint8_t l1, l2, lvl;
895
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
6,943,537✔
896

897
    const char *l1str = columnEncodeStr(l1);
6,943,103✔
898
    const char *l2str = columnCompressStr(l2);
6,942,812✔
899
    const char *lvlstr = columnLevelStr(lvl);
6,942,544✔
900
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
6,942,415✔
901
  }
902
  return;
720,750✔
903
}
904

905
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
720,877✔
906
  int rc = 0;
720,877✔
907

908
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
720,877✔
909
  if (pColCmprObj == NULL) {
720,874!
910
    pColCmprObj = NULL;
×
911
    return TSDB_CODE_OUT_OF_MEMORY;
×
912
  }
913

914
  void      *pData = NULL;
720,874✔
915
  int        nData = 0;
720,874✔
916
  SMetaEntry e = {0};
720,874✔
917
  SDecoder   dc = {0};
720,874✔
918

919
  *ppColCmprObj = NULL;
720,874✔
920

921
  metaRLock(pMeta);
720,874✔
922
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
720,927✔
923
  if (rc < 0) {
720,923✔
924
    taosHashClear(pColCmprObj);
2✔
925
    metaULock(pMeta);
2✔
926
    return TSDB_CODE_FAILED;
2✔
927
  }
928
  int64_t version = ((SUidIdxVal *)pData)[0].version;
720,921✔
929
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
720,921✔
930
  if (rc < 0) {
720,899!
931
    metaULock(pMeta);
×
932
    taosHashClear(pColCmprObj);
×
933
    metaError("failed to get table entry");
×
934
    return rc;
×
935
  }
936

937
  tDecoderInit(&dc, pData, nData);
720,899✔
938
  rc = metaDecodeEntry(&dc, &e);
720,893✔
939
  if (rc < 0) {
720,826!
940
    tDecoderClear(&dc);
×
941
    tdbFree(pData);
×
942
    metaULock(pMeta);
×
943
    taosHashClear(pColCmprObj);
×
944
    return rc;
×
945
  }
946
  if (withExtSchema(e.type)) {
720,826!
947
    SColCmprWrapper *p = &e.colCmpr;
720,831✔
948
    for (int32_t i = 0; i < p->nCols; i++) {
7,663,885✔
949
      SColCmpr *pCmpr = &p->pColCmpr[i];
6,942,711✔
950
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
6,942,711✔
951
      if (rc < 0) {
6,942,981!
UNCOV
952
        tDecoderClear(&dc);
×
953
        tdbFree(pData);
×
954
        metaULock(pMeta);
×
955
        taosHashClear(pColCmprObj);
×
956
        return rc;
×
957
      }
958
    }
959
  } else {
960
    tDecoderClear(&dc);
×
961
    tdbFree(pData);
×
962
    metaULock(pMeta);
×
963
    taosHashClear(pColCmprObj);
×
964
    return 0;
×
965
  }
966
  tDecoderClear(&dc);
721,174✔
967
  tdbFree(pData);
720,877✔
968
  metaULock(pMeta);
720,910✔
969

970
  *ppColCmprObj = pColCmprObj;
720,921✔
971
  colCompressDebug(pColCmprObj);
720,921✔
972

973
  return 0;
720,806✔
974
}
975
// refactor later
976
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
10,015✔
977
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
10,012✔
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