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

taosdata / TDengine / #4940

27 Jan 2026 10:23AM UTC coverage: 66.832% (-0.1%) from 66.931%
#4940

push

travis-ci

web-flow
fix: asan invalid write issue (#34400)

7 of 8 new or added lines in 2 files covered. (87.5%)

822 existing lines in 141 files now uncovered.

204293 of 305680 relevant lines covered (66.83%)

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

50
    SColCmpr *pCol = p + nCols;
3,003,992✔
51
    pCol->id = pSchema->colId;
3,003,992✔
52
    pCol->alg = compress;
3,003,992✔
53
    pWp->nCols = nCols + 1;
3,003,992✔
54
    pWp->version = ver;
3,003,992✔
55
  } else {
56
    for (int32_t i = 0; i < nCols; i++) {
20,621,476✔
57
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
20,621,476✔
58
      if (pOCmpr->id == pSchema->colId) {
20,621,476✔
59
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
56,212✔
60
        if (left) {
56,212✔
61
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
34,542✔
62
        }
63
        nCols--;
56,212✔
64
        break;
56,212✔
65
      }
66
    }
67
    pWp->nCols = nCols;
56,212✔
68
    pWp->version = ver;
56,212✔
69
  }
70
  return 0;
3,060,204✔
71
}
72

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

89
int32_t dropTableExtSchema(SMetaEntry *pEntry, int32_t dropColId, int32_t newColNum) {
83,118✔
90
  // no ext schema, no need to drop
91
  if (!pEntry->pExtSchemas) return 0;
83,118✔
92
  if (dropColId == newColNum) {
14,076✔
93
    // drop the last column
94
    pEntry->pExtSchemas[dropColId - 1] = (SExtSchema){0};
5,624✔
95
  } else {
96
    // drop a column in the middle
97
    memmove(pEntry->pExtSchemas + dropColId, pEntry->pExtSchemas + dropColId + 1,
8,452✔
98
            (newColNum - dropColId) * sizeof(SExtSchema));
8,452✔
99
  }
100
  for (int32_t i = 0; i < newColNum; i++) {
61,704✔
101
    if (hasExtSchema(pEntry->pExtSchemas + i)) return 0;
58,908✔
102
  }
103
  taosMemoryFreeClear(pEntry->pExtSchemas);
2,796✔
104
  return 0;
2,796✔
105
}
106

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

116
    SColRef *pCol = p + nCols;
53,624✔
117
    if (NULL == pColRef) {
53,624✔
118
      pCol->hasRef = false;
5,278✔
119
      pCol->id = pSchema->colId;
5,278✔
120
    } else {
121
      pCol->hasRef = pColRef->hasRef;
48,346✔
122
      pCol->id = pSchema->colId;
48,346✔
123
      if (pCol->hasRef) {
48,346✔
124
        tstrncpy(pCol->refDbName, pColRef->refDbName, TSDB_DB_NAME_LEN);
19,902✔
125
        tstrncpy(pCol->refTableName, pColRef->refTableName, TSDB_TABLE_NAME_LEN);
19,902✔
126
        tstrncpy(pCol->refColName, pColRef->refColName, TSDB_COL_NAME_LEN);
19,902✔
127
      }
128
    }
129
    pWp->nCols = nCols + 1;
53,624✔
130
    pWp->version++;
53,624✔
131
  } else {
132
    for (int32_t i = 0; i < nCols; i++) {
119,238✔
133
      SColRef *pOColRef = &pWp->pColRef[i];
119,238✔
134
      if (pOColRef->id == pSchema->colId) {
119,238✔
135
        int32_t left = (nCols - i - 1) * sizeof(SColRef);
30,454✔
136
        if (left) {
30,454✔
137
          memmove(pWp->pColRef + i, pWp->pColRef + i + 1, left);
29,242✔
138
        }
139
        nCols--;
30,454✔
140
        break;
30,454✔
141
      }
142
    }
143
    pWp->nCols = nCols;
30,454✔
144
    pWp->version++;
30,454✔
145
  }
146
  return 0;
84,078✔
147
}
148

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

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

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

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

171
  return 0;
7,182,360✔
172
}
173

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

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

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

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

210
  for (int32_t i = 0; i < pRef->nCols; i++) {
5,678,406✔
211
    SColRef* pColRef = &pRef->pColRef[i];
5,127,324✔
212
    if (pColRef->hasRef) {
5,127,324✔
213
      code = taosHashPut(pColRefHash, pColRef->refTableName, strlen(pColRef->refTableName), NULL, 0);
2,794,006✔
214
      if (code) {
2,794,006✔
215
        goto _return;
×
216
      }
217
    }
218
  }
219

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

225
  memcpy(pMetaRsp->pColRefs, pRef->pColRef, pRef->nCols * sizeof(SColRef));
551,082✔
226
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
551,082✔
227
  if (tableType == TSDB_VIRTUAL_NORMAL_TABLE) {
551,082✔
228
    pMetaRsp->tuid = pEntry->uid;
315,802✔
229
  } else if (tableType == TSDB_VIRTUAL_CHILD_TABLE) {
235,280✔
230
    pMetaRsp->tuid = pEntry->uid;
235,280✔
231
    pMetaRsp->suid = pEntry->ctbEntry.suid;
235,280✔
232
  }
233

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

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

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

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

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

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

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

275
  int16_t nCols = taosArrayGetSize(pTagVals);
253,199✔
276
  for (int i = 0; i < nCols; i++) {
615,215✔
277
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
362,016✔
278
    char     type = pTagVal->type;
362,016✔
279

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

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

313
    if (term != NULL) {
362,016✔
314
      int32_t ret = indexMultiTermAdd(terms, term);
362,016✔
315
      if (ret < 0) {
362,016✔
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);
253,199✔
325
  indexMultiTermDestroy(terms);
253,199✔
326

327
  taosArrayDestroy(pTagVals);
253,199✔
328
  return code;
253,199✔
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) {
109,517✔
336
int32_t code = 0;
109,517✔
337
#ifdef USE_INVERTED_INDEX
338
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
109,517✔
339
    return TSDB_CODE_INVALID_PARA;
×
340
  }
341
  void       *data = pCtbEntry->ctbEntry.pTags;
109,517✔
342
  const char *tagName = pSchema->name;
109,517✔
343

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

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

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

360
  int16_t nCols = taosArrayGetSize(pTagVals);
109,517✔
361
  for (int i = 0; i < nCols; i++) {
314,006✔
362
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
204,489✔
363
    char     type = pTagVal->type;
204,489✔
364

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

368
    SIndexTerm *term = NULL;
204,489✔
369
    if (type == TSDB_DATA_TYPE_NULL) {
204,489✔
370
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
371
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
204,489✔
372
      if (pTagVal->nData > 0) {
202,319✔
373
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
202,319✔
374
        if (val == NULL) {
202,319✔
375
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
376
        }
377
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
202,319✔
378
        if (len < 0) {
202,319✔
379
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
380
        }
381
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
202,319✔
382
        type = TSDB_DATA_TYPE_VARCHAR;
202,319✔
383
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
202,319✔
384
        taosMemoryFree(val);
202,319✔
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,170✔
389
      double val = *(double *)(&pTagVal->i64);
1,302✔
390
      int    len = sizeof(val);
1,302✔
391
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
1,302✔
392
    } else if (type == TSDB_DATA_TYPE_BOOL) {
868✔
393
      int val = *(int *)(&pTagVal->i64);
868✔
394
      int len = sizeof(val);
868✔
395
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
868✔
396
    }
397
    if (term != NULL) {
204,489✔
398
      int32_t ret = indexMultiTermAdd(terms, term);
204,489✔
399
      if (ret < 0) {
204,489✔
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);
109,517✔
409
  indexMultiTermDestroy(terms);
109,517✔
410
  taosArrayDestroy(pTagVals);
109,517✔
411
  return code;
109,517✔
412
_exception:
×
413
  indexMultiTermDestroy(terms);
×
414
  taosArrayDestroy(pTagVals);
×
415
#endif
416
  return code;
×
417
}
418

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

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

429
  metaWLock(pMeta);
17,722✔
430
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
96,576✔
431
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
78,854✔
432
    tb_uid_t suid = 0;
78,854✔
433
    int8_t   sysTbl = 0;
78,854✔
434
    int      type;
78,854✔
435
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
78,854✔
436
    if (code) return code;
78,854✔
437
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
78,854✔
438
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
73,962✔
439
      if (pVal) {
73,962✔
440
        nCtbDropped = *pVal + 1;
58,474✔
441
      } else {
442
        nCtbDropped = 1;
15,488✔
443
      }
444
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
73,962✔
445
      if (code) return code;
73,962✔
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);
78,854✔
453
  }
454
  metaULock(pMeta);
17,722✔
455

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

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

476
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
24,128✔
477
  int32_t code = 0;
24,128✔
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,128✔
483
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
24,128✔
484
  if (code < 0) {
24,128✔
485
    return code;
×
486
  }
487

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

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

497
  while (1) {
175,981✔
498
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
200,109✔
499
    if (ret < 0) {
200,109✔
500
      break;
24,128✔
501
    }
502

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

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

531
  return 0;
24,128✔
532
}
533

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

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

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

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

554
end:
17,722✔
555
  taosArrayDestroy(tbUids);
24,128✔
556

557
  return code;
24,128✔
558
}
559

560
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
12,237,765✔
561
  metaRLock(pMeta);
12,237,765✔
562

563
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
12,251,285✔
564

565
  metaULock(pMeta);
12,234,294✔
566

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

571
  return ret;
12,241,527✔
572
}
573

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

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

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

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

602
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
78,854✔
603
  if (pME->type == TSDB_CHILD_TABLE) {
78,854✔
604
    ctx.ttlDays = pME->ctbEntry.ttlDays;
73,962✔
605
  } else {
606
    ctx.ttlDays = pME->ntbEntry.ttlDays;
4,892✔
607
  }
608

609
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
78,854✔
610
  if (ret < 0) {
78,854✔
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;
78,854✔
615
}
616

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

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

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

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

644
  if (type) *type = e.type;
78,854✔
645

646
  if (e.type == TSDB_CHILD_TABLE) {
78,854✔
647
    if (pSuid) *pSuid = e.ctbEntry.suid;
73,962✔
648
    void *tData = NULL;
73,962✔
649
    int   tLen = 0;
73,962✔
650

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

657
        tDecoderInit(&tdc, tData, tLen);
73,962✔
658
        ret = metaDecodeEntry(&tdc, &stbEntry);
73,962✔
659
        if (ret < 0) {
73,962✔
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;
73,962✔
667
        
668
        ret = metaStableTagFilterCacheUpdateUid(
73,962✔
669
          pMeta, &e, &stbEntry, STABLE_TAG_FILTER_CACHE_DROP_TABLE);
670
        if (ret < 0) {
73,962✔
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;
73,962✔
677
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
73,962✔
678
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
73,962✔
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++) {
386,450✔
687
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
312,488✔
688
            if (!IS_IDX_ON(pTagColumn)) continue;
312,488✔
689
            STagIdxKey *pTagIdxKey = NULL;
73,962✔
690
            int32_t     nTagIdxKey;
73,962✔
691

692
            const void *pTagData = NULL;
73,962✔
693
            int32_t     nTagData = 0;
73,962✔
694

695
            STagVal tagVal = {.cid = pTagColumn->colId};
73,962✔
696
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
73,962✔
697
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
73,962✔
698
                pTagData = tagVal.pData;
×
699
                nTagData = (int32_t)tagVal.nData;
×
700
              } else {
701
                pTagData = &(tagVal.i64);
73,962✔
702
                nTagData = tDataTypes[pTagColumn->type].bytes;
73,962✔
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,
73,962✔
711
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
712
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
73,962✔
713
              if (ret < 0) {
73,962✔
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);
73,962✔
719
            pTagIdxKey = NULL;
73,962✔
720
          }
721
        }
722
        tDecoderClear(&tdc);
73,962✔
723
      }
724
      tdbFree(tData);
73,962✔
725
    }
726
  }
727

728
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
78,854✔
729
  if (ret < 0) {
78,854✔
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);
78,854✔
734
  if (ret < 0) {
78,854✔
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);
78,854✔
739
  if (ret < 0) {
78,854✔
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);
78,854✔
745
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
78,854✔
746

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

749
  if (e.type == TSDB_CHILD_TABLE) {
78,854✔
750
    ret =
751
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
73,962✔
752
    if (ret < 0) {
73,962✔
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;
73,962✔
758
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
73,962✔
759
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
73,962✔
760
    if (ret < 0) {
73,962✔
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);
73,962✔
765
    if (ret < 0) {
73,962✔
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,892✔
775
    // drop schema.db (todo)
776

777
    --pMeta->pVnode->config.vndStats.numOfNTables;
4,892✔
778
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
4,892✔
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);
78,854✔
817
  if (ret < 0) {
78,854✔
818
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
78,854✔
819
              tstrerror(ret));
820
  }
821

822
  tDecoderClear(&dc);
78,854✔
823
  tdbFree(pData);
78,854✔
824

825
  return 0;
78,854✔
826
}
827

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

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

844
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
11,529,594✔
845
  pMeta->changed = true;
11,529,594✔
846
  switch (pReq->action) {
11,529,594✔
847
    case TSDB_ALTER_TABLE_ADD_COLUMN:
3,364,540✔
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,364,540✔
851
    case TSDB_ALTER_TABLE_DROP_COLUMN:
83,118✔
852
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
83,118✔
853
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
472,745✔
854
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
472,745✔
855
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
42,725✔
856
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
42,725✔
857
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
7,411,786✔
858
      return metaUpdateTableTagValue(pMeta, version, pReq);
7,411,786✔
859
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
3,155✔
860
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
3,155✔
861
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
23,926✔
862
      return metaUpdateTableOptions2(pMeta, version, pReq);
23,926✔
863
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
5,848✔
864
      return metaUpdateTableColCompress2(pMeta, version, pReq);
5,848✔
865
    case TSDB_ALTER_TABLE_ALTER_COLUMN_REF:
70,177✔
866
      return metaAlterTableColumnRef(pMeta, version, pReq, pMetaRsp);
70,177✔
867
    case TSDB_ALTER_TABLE_REMOVE_COLUMN_REF:
51,574✔
868
      return metaRemoveTableColumnRef(pMeta, version, pReq, pMetaRsp);
51,574✔
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,266,759✔
876
  if (!tsTtlChangeOnWrite) return 0;
20,266,759✔
877

878
  if (changeTimeMs <= 0) {
20,266,759✔
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,266,759✔
884

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

888
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
724,283,701✔
889
  if (!tsTtlChangeOnWrite) return 0;
724,283,701✔
890

891
  metaWLock(pMeta);
20,275,235✔
892
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
20,266,759✔
893
  metaULock(pMeta);
20,266,759✔
894
  return ret;
20,266,759✔
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,
81,143,822✔
898
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
899
  if (IS_VAR_DATA_TYPE(type)) {
81,143,822✔
900
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
9,431,491✔
901
  } else {
902
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
71,712,331✔
903
  }
904

905
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
81,163,194✔
906
  if (*ppTagIdxKey == NULL) {
81,158,148✔
907
    return terrno;
×
908
  }
909

910
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
81,148,746✔
911
  (*ppTagIdxKey)->cid = cid;
81,152,091✔
912
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
81,153,822✔
913
  (*ppTagIdxKey)->type = type;
81,152,716✔
914

915
  // refactor
916
  if (IS_VAR_DATA_TYPE(type)) {
81,145,181✔
917
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
9,460,235✔
918
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
9,455,134✔
919
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
9,454,654✔
920
  } else {
921
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
71,684,946✔
922
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
71,690,093✔
923
  }
924

925
  return 0;
81,145,649✔
926
}
927

928
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
78,028,619✔
929
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
78,028,619✔
930
}
78,030,113✔
931

932
static void colCompressDebug(SHashObj *pColCmprObj) {
84,993,191✔
933
  void *p = taosHashIterate(pColCmprObj, NULL);
84,993,191✔
934
  while (p) {
697,960,837✔
935
    uint32_t cmprAlg = *(uint32_t *)p;
612,942,103✔
936
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
612,941,580✔
937
    p = taosHashIterate(pColCmprObj, p);
612,970,411✔
938

939
    uint8_t l1, l2, lvl;
613,220,405✔
940
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
613,212,424✔
941

942
    const char *l1str = columnEncodeStr(l1);
613,218,938✔
943
    const char *l2str = columnCompressStr(l2);
613,089,921✔
944
    const char *lvlstr = columnLevelStr(lvl);
612,912,247✔
945
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
612,966,679✔
946
  }
947
  return;
85,018,734✔
948
}
949

950
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
85,024,913✔
951
  int rc = 0;
85,024,913✔
952

953
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
85,024,913✔
954
  if (pColCmprObj == NULL) {
85,015,925✔
955
    pColCmprObj = NULL;
×
956
    return TSDB_CODE_OUT_OF_MEMORY;
×
957
  }
958

959
  void      *pData = NULL;
85,015,925✔
960
  int        nData = 0;
85,012,960✔
961
  SMetaEntry e = {0};
85,025,649✔
962
  SDecoder   dc = {0};
85,024,617✔
963

964
  *ppColCmprObj = NULL;
85,026,412✔
965

966
  metaRLock(pMeta);
85,028,124✔
967
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
85,030,754✔
968
  if (rc < 0) {
85,024,122✔
969
    taosHashCleanup(pColCmprObj);
14,525✔
970
    metaULock(pMeta);
14,525✔
971
    return TSDB_CODE_FAILED;
14,525✔
972
  }
973
  int64_t version = ((SUidIdxVal *)pData)[0].version;
85,009,597✔
974
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
85,007,557✔
975
  if (rc < 0) {
85,005,428✔
976
    metaULock(pMeta);
×
977
    taosHashCleanup(pColCmprObj);
×
978
    metaError("failed to get table entry");
×
979
    return rc;
×
980
  }
981

982
  tDecoderInit(&dc, pData, nData);
85,005,428✔
983
  rc = metaDecodeEntry(&dc, &e);
85,014,441✔
984
  if (rc < 0) {
84,984,044✔
985
    tDecoderClear(&dc);
×
986
    tdbFree(pData);
×
987
    metaULock(pMeta);
×
988
    taosHashCleanup(pColCmprObj);
×
989
    return rc;
×
990
  }
991
  if (withExtSchema(e.type)) {
84,984,044✔
992
    SColCmprWrapper *p = &e.colCmpr;
85,003,690✔
993
    for (int32_t i = 0; i < p->nCols; i++) {
698,161,275✔
994
      SColCmpr *pCmpr = &p->pColCmpr[i];
613,158,547✔
995
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
613,165,226✔
996
      if (rc < 0) {
613,157,585✔
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);
84,962,123✔
1012
  tdbFree(pData);
85,005,433✔
1013
  metaULock(pMeta);
85,009,334✔
1014

1015
  *ppColCmprObj = pColCmprObj;
85,007,357✔
1016
  colCompressDebug(pColCmprObj);
85,006,129✔
1017

1018
  return 0;
85,018,881✔
1019
}
1020
// refactor later
1021
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
3,958,166✔
1022
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
3,958,166✔
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